SlideShare a Scribd company logo
1 of 45
Download to read offline
The Stack and Buffer Overflows
March 29th
Overview
● Registers Review
● Introduction to the Stack
● CDECL Calling Conventions
● Introduction to Buffer Overflows
● Buffer Overflow to Local Variable Corruption
● Buffer Overflow to Return Pointer Corruption
EAX Accumulator (usually used to store return
value or function argument)
EBX Base register
ECX Counter
EDX Data (can sometimes be combined with
EAX for larger numbers)
ESP Stack Pointer (top of stack frame)
EBP Base Pointer (bottom of stack frame)
EIP Instruction Pointer (current instruction)
EDI Destination Index
ESI Source Index
What is the stack?
● A LIFO chunk of memory
● Contains variables relevant to local scope
● “Made” of units called frames, which represent the local scope
● In general, contains everything not malloc()’d
Source: Mitch Adair, FSU Computer Science 2013
Subroutine Calling
● Allocates a new stack frame (establishes new local scope)
● Allows for easy code reuse (C functions)
● Follows specific call conventions to ensure the program can continue after
subroutine execution
● We will look at CDECL, which is used by GCC for i386*
https://www.cs.virginia.edu/~evans/cs216/guides/x86.html
Caller responsibilities
● Save caller saved registers (EAX, ECX, EDX) if their values are relevant by
pushing them to the stack (the subroutine may return with them modified)
● Parameters are pushed in reverse order (last parameter first.) This will store
the first parameter at the lowest address
● call the subroutine, which will push the return address to the stack and then
branch to the subroutine
Callee responsibilities
● Push EBP to the stack, and then move EBP to ESP, “creating” a new stack
frame.
● Make space on the stack for local variables by decrementing ESP, and place
local variables on the stack. At this point, local variables are at known offsets
above EBP and parameters are at known offsets below EBP
● Save EBX, EDI, and ESI on the stack if they will be changed (ESP and EBP
are saved by other means)
● Execute subroutine logic
Callee cleanup
● Leave return value in EAX
● Restore values of EBX, EDI, and ESI
● Deallocate current stack frame (Move ESP to EBP, handled by leave)
● Restore EBP to stored EBP (handled by leave)
● Return to the calling address (ret)
Caller cleanup
● Remove parameters from the stack
● Restore caller saved registers (EAX, ECX, EDX)
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Assembly to C
mov eax,DWORD PTR [ebx-0x4]
mov eax,DWORD PTR [eax]
sub esp,0x4
push eax
push 0x20
lea eax,[ebp-0x28]
push eax
call 80483c0 <fgets@plt>
add esp,0x10
fgets(userpass, 32, stdin);
Buffer Overflows
● Occurs when memory is written past the area that was allocated for it
● Generally caused by functions that write data without bounds checking i.e.
scanf, gets, strcpy
● Allows attacker to write arbitrary data into stack frame, possibly overwriting
other values or the return pointer
Popular Buffer Overflows
● Twilight Princess Wii Homebrew - Horse name can be manipulated to be
extremely long, causing a buffer overflow which loads a file at the root of the
inserted SD card, giving arbitrary code execution
● Similar bug existed in LEGO Indiana Jones and Super Smash Bros. Brawl
● PS2 Independence Exploit
GDB Commands
next - next instruction, steps over functions
step - next instruction, steps into functions
break - set breakpoint
stack # - show the stack # deep
01_bufferoverflow
Goal: Change value of admin
Stack when leaving checkAdmin()
checkpass
checkpass
checkpass
checkpass
int admin
GCC
Old EBX
Old EBP
Old EIP
Crafting a payload
● This can be done by passing typing 16 A’s and hitting enter, but that is less
fun
● Ideally, we send a 20 byte string, with the last 4 bytes set to the value we
want admin to take
● Sending any more than 20 bytes could lead to interesting behavior if we aren’t
careful
● Problem: Some byte values can’t be typed easily
● Warning: x86 is little endian, so in each word (4 bytes), the least significant
byte will be on the left hand side
Crafting a payload
● printf ‘x69x85x04x08’
● python -c ‘print “x69x85x04x08”’
● perl -e ‘print “x69x85x04x08”’
Crafting a payload efficiently
● Python has a struct library that will pack binary data
● python -c "import struct; print 'A' * 16 + struct.pack('<i', 1337)"
● Will fill checkpass with A’s, and will then set admin to equal 1337
Stack when leaving checkAdmin() after exploit
checkpass
checkpass
checkpass
checkpass
admin = 1337
GCC
Old EBX
Old EBP
Old EIP
above admin in memory
what we want to modify
unchecked memory writing
admin returned
checkAdmin != 0 == win
02_bufferoverflow
01_bufferoverflow 02_bufferoverflow
Goal: call getFlag()
Stack when leaving checkAdmin()
checkpass
checkpass
checkpass
checkpass
GCC
Old EBX
Old EBP
Old EIP
Where to go?
Crafting a payload
● 28 Garbage Bytes
● Return Address: 0x08048569
● python -c "import struct; print 'A' * 28 + struct.pack('<i', 0x08048569)"
● Profit?
Stack when leaving checkAdmin() after exploit
unchecked memory writing
Return address overwritten
Called directly
Questions?
Resources
RPISEC Class - https://github.com/RPISEC/MBE
Past Presentations - https://csg.utdallas.edu/presentations/binary/
x86 Assembly Guide - https://www.cs.virginia.edu/~evans/cs216/guides/x86.html
Practice - http://pwnable.kr/
Future Events
● 4/5/17 - Shellcoding: Writing your own Win()
● Potentially format string vulnerabilities, ROP, and ALSR bypassing

More Related Content

What's hot

An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROPSaumil Shah
 
How Functions Work
How Functions WorkHow Functions Work
How Functions WorkSaumil Shah
 
Runtime Symbol Resolution
Runtime Symbol ResolutionRuntime Symbol Resolution
Runtime Symbol ResolutionKen Kawamoto
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)Angel Boy
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XSℕicolas ℝ.
 
CNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: ShellcodeCNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: ShellcodeSam Bowne
 
Triton and symbolic execution on gdb
Triton and symbolic execution on gdbTriton and symbolic execution on gdb
Triton and symbolic execution on gdbWei-Bo Chen
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniqueAngel Boy
 
Course lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingJonathan Salwan
 
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 4: Introduction to format string bugsCNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 4: Introduction to format string bugsSam Bowne
 
Finding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyPriyanka Aash
 
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이GangSeok Lee
 
CNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you BeginCNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you BeginSam Bowne
 
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]RootedCON
 
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: ShellcodeCNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: ShellcodeSam Bowne
 
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxSam Bowne
 
L293D IC interfacing with ATmega16
L293D IC interfacing with ATmega16 L293D IC interfacing with ATmega16
L293D IC interfacing with ATmega16 Varun A M
 
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on LinuxCNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on LinuxSam Bowne
 

What's hot (20)

An introduction to ROP
An introduction to ROPAn introduction to ROP
An introduction to ROP
 
Klee and angr
Klee and angrKlee and angr
Klee and angr
 
How Functions Work
How Functions WorkHow Functions Work
How Functions Work
 
ROP
ROPROP
ROP
 
Runtime Symbol Resolution
Runtime Symbol ResolutionRuntime Symbol Resolution
Runtime Symbol Resolution
 
Pwning in c++ (basic)
Pwning in c++ (basic)Pwning in c++ (basic)
Pwning in c++ (basic)
 
Overloading Perl OPs using XS
Overloading Perl OPs using XSOverloading Perl OPs using XS
Overloading Perl OPs using XS
 
CNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: ShellcodeCNIT 127: Ch 3: Shellcode
CNIT 127: Ch 3: Shellcode
 
Triton and symbolic execution on gdb
Triton and symbolic execution on gdbTriton and symbolic execution on gdb
Triton and symbolic execution on gdb
 
Play with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit TechniquePlay with FILE Structure - Yet Another Binary Exploit Technique
Play with FILE Structure - Yet Another Binary Exploit Technique
 
Course lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented ProgrammingCourse lecture - An introduction to the Return Oriented Programming
Course lecture - An introduction to the Return Oriented Programming
 
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 4: Introduction to format string bugsCNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 4: Introduction to format string bugs
 
Finding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated DisassemblyFinding Xori: Malware Analysis Triage with Automated Disassembly
Finding Xori: Malware Analysis Triage with Automated Disassembly
 
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
[2012 CodeEngn Conference 06] pwn3r - Secuinside 2012 CTF 예선 문제풀이
 
CNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you BeginCNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 1: Before you Begin
 
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
Ilfak Guilfanov - Decompiler internals: Microcode [rooted2018]
 
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: ShellcodeCNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: Shellcode
 
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on LinuxCNIT 127: Ch 2: Stack overflows on Linux
CNIT 127: Ch 2: Stack overflows on Linux
 
L293D IC interfacing with ATmega16
L293D IC interfacing with ATmega16 L293D IC interfacing with ATmega16
L293D IC interfacing with ATmega16
 
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on LinuxCNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on Linux
 

Similar to Stack and Buffer Overflows Explained

Basic ASM by @binaryheadache
Basic ASM by @binaryheadacheBasic ASM by @binaryheadache
Basic ASM by @binaryheadachecamsec
 
Return Oriented Programming - ROP
Return Oriented Programming - ROPReturn Oriented Programming - ROP
Return Oriented Programming - ROPMihir Shah
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)Selomon birhane
 
Buffer overflow attacks
Buffer overflow attacksBuffer overflow attacks
Buffer overflow attacksJapneet Singh
 
other-architectures.ppt
other-architectures.pptother-architectures.ppt
other-architectures.pptJaya Chavan
 
lec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdflec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdfhasan58964
 
04basic Concepts
04basic Concepts04basic Concepts
04basic ConceptsZhiwen Guo
 
Instruction addressing and execution
Instruction addressing and executionInstruction addressing and execution
Instruction addressing and executionSilvia
 
Exploiting Memory Overflows
Exploiting Memory OverflowsExploiting Memory Overflows
Exploiting Memory OverflowsAnkur Tyagi
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptMuhammad Sikandar Mustafa
 
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 2: Stack Overflows in LinuxCNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 2: Stack Overflows in LinuxSam Bowne
 
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
Creating a Fibonacci Generator in Assembly - by Willem van KetwichCreating a Fibonacci Generator in Assembly - by Willem van Ketwich
Creating a Fibonacci Generator in Assembly - by Willem van KetwichWillem van Ketwich
 
127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on LinuxSam Bowne
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsAsuka Nakajima
 
Devirtualizing FinSpy
Devirtualizing FinSpyDevirtualizing FinSpy
Devirtualizing FinSpyjduart
 
Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsCysinfo Cyber Security Community
 

Similar to Stack and Buffer Overflows Explained (20)

Exploitation Crash Course
Exploitation Crash CourseExploitation Crash Course
Exploitation Crash Course
 
Basic ASM by @binaryheadache
Basic ASM by @binaryheadacheBasic ASM by @binaryheadache
Basic ASM by @binaryheadache
 
Return Oriented Programming - ROP
Return Oriented Programming - ROPReturn Oriented Programming - ROP
Return Oriented Programming - ROP
 
[ASM]Lab6
[ASM]Lab6[ASM]Lab6
[ASM]Lab6
 
N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)N_Asm Assembly arithmetic instructions (sol)
N_Asm Assembly arithmetic instructions (sol)
 
Buffer overflow attacks
Buffer overflow attacksBuffer overflow attacks
Buffer overflow attacks
 
other-architectures.ppt
other-architectures.pptother-architectures.ppt
other-architectures.ppt
 
lec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdflec15_x86procedure_4up.pdf
lec15_x86procedure_4up.pdf
 
04basic Concepts
04basic Concepts04basic Concepts
04basic Concepts
 
Assembly fundamentals
Assembly fundamentalsAssembly fundamentals
Assembly fundamentals
 
Instruction addressing and execution
Instruction addressing and executionInstruction addressing and execution
Instruction addressing and execution
 
Exploiting Memory Overflows
Exploiting Memory OverflowsExploiting Memory Overflows
Exploiting Memory Overflows
 
Advanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter pptAdvanced procedures in assembly language Full chapter ppt
Advanced procedures in assembly language Full chapter ppt
 
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 2: Stack Overflows in LinuxCNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 2: Stack Overflows in Linux
 
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
Creating a Fibonacci Generator in Assembly - by Willem van KetwichCreating a Fibonacci Generator in Assembly - by Willem van Ketwich
Creating a Fibonacci Generator in Assembly - by Willem van Ketwich
 
127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux127 Ch 2: Stack overflows on Linux
127 Ch 2: Stack overflows on Linux
 
Buffer Overflow Demo by Saurabh Sharma
Buffer Overflow Demo by Saurabh SharmaBuffer Overflow Demo by Saurabh Sharma
Buffer Overflow Demo by Saurabh Sharma
 
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading SkillsReverse Engineering Dojo: Enhancing Assembly Reading Skills
Reverse Engineering Dojo: Enhancing Assembly Reading Skills
 
Devirtualizing FinSpy
Devirtualizing FinSpyDevirtualizing FinSpy
Devirtualizing FinSpy
 
Reversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basicsReversing malware analysis training part4 assembly programming basics
Reversing malware analysis training part4 assembly programming basics
 

More from UTD Computer Security Group

UTD Computer Security Group - Cracking the domain
UTD Computer Security Group - Cracking the domainUTD Computer Security Group - Cracking the domain
UTD Computer Security Group - Cracking the domainUTD Computer Security Group
 

More from UTD Computer Security Group (20)

Py jail talk
Py jail talkPy jail talk
Py jail talk
 
22S kickoff 2.0 (kickoff + anonymity talk)
22S kickoff 2.0 (kickoff + anonymity talk)22S kickoff 2.0 (kickoff + anonymity talk)
22S kickoff 2.0 (kickoff + anonymity talk)
 
Cloud talk
Cloud talkCloud talk
Cloud talk
 
UTD Computer Security Group - Cracking the domain
UTD Computer Security Group - Cracking the domainUTD Computer Security Group - Cracking the domain
UTD Computer Security Group - Cracking the domain
 
Forensics audio and video
Forensics   audio and videoForensics   audio and video
Forensics audio and video
 
Computer networks and network security
Computer networks and network securityComputer networks and network security
Computer networks and network security
 
Intro to python
Intro to pythonIntro to python
Intro to python
 
Powershell crash course
Powershell crash coursePowershell crash course
Powershell crash course
 
Intro to cybersecurity
Intro to cybersecurityIntro to cybersecurity
Intro to cybersecurity
 
Intro to Bash
Intro to BashIntro to Bash
Intro to Bash
 
Web Exploitation
Web ExploitationWeb Exploitation
Web Exploitation
 
Network Exploitation
Network ExploitationNetwork Exploitation
Network Exploitation
 
Penetration Testing: Celestial
Penetration Testing: CelestialPenetration Testing: Celestial
Penetration Testing: Celestial
 
Introduction to Exploitation
Introduction to ExploitationIntroduction to Exploitation
Introduction to Exploitation
 
Cryptography Crash Course
Cryptography Crash CourseCryptography Crash Course
Cryptography Crash Course
 
Fuzzing - Part 2
Fuzzing - Part 2Fuzzing - Part 2
Fuzzing - Part 2
 
Fuzzing - Part 1
Fuzzing - Part 1Fuzzing - Part 1
Fuzzing - Part 1
 
Protostar VM - Heap3
Protostar VM - Heap3Protostar VM - Heap3
Protostar VM - Heap3
 
Heap Base Exploitation
Heap Base ExploitationHeap Base Exploitation
Heap Base Exploitation
 
Return Oriented Programming
Return Oriented ProgrammingReturn Oriented Programming
Return Oriented Programming
 

Recently uploaded

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr LapshynFwdays
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Recently uploaded (20)

Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
"Federated learning: out of reach no matter how close",Oleksandr Lapshyn
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Stack and Buffer Overflows Explained

  • 1. The Stack and Buffer Overflows March 29th
  • 2. Overview ● Registers Review ● Introduction to the Stack ● CDECL Calling Conventions ● Introduction to Buffer Overflows ● Buffer Overflow to Local Variable Corruption ● Buffer Overflow to Return Pointer Corruption
  • 3. EAX Accumulator (usually used to store return value or function argument) EBX Base register ECX Counter EDX Data (can sometimes be combined with EAX for larger numbers) ESP Stack Pointer (top of stack frame) EBP Base Pointer (bottom of stack frame) EIP Instruction Pointer (current instruction) EDI Destination Index ESI Source Index
  • 4. What is the stack? ● A LIFO chunk of memory ● Contains variables relevant to local scope ● “Made” of units called frames, which represent the local scope ● In general, contains everything not malloc()’d
  • 5. Source: Mitch Adair, FSU Computer Science 2013
  • 6. Subroutine Calling ● Allocates a new stack frame (establishes new local scope) ● Allows for easy code reuse (C functions) ● Follows specific call conventions to ensure the program can continue after subroutine execution ● We will look at CDECL, which is used by GCC for i386*
  • 8. Caller responsibilities ● Save caller saved registers (EAX, ECX, EDX) if their values are relevant by pushing them to the stack (the subroutine may return with them modified) ● Parameters are pushed in reverse order (last parameter first.) This will store the first parameter at the lowest address ● call the subroutine, which will push the return address to the stack and then branch to the subroutine
  • 9. Callee responsibilities ● Push EBP to the stack, and then move EBP to ESP, “creating” a new stack frame. ● Make space on the stack for local variables by decrementing ESP, and place local variables on the stack. At this point, local variables are at known offsets above EBP and parameters are at known offsets below EBP ● Save EBX, EDI, and ESI on the stack if they will be changed (ESP and EBP are saved by other means) ● Execute subroutine logic
  • 10. Callee cleanup ● Leave return value in EAX ● Restore values of EBX, EDI, and ESI ● Deallocate current stack frame (Move ESP to EBP, handled by leave) ● Restore EBP to stored EBP (handled by leave) ● Return to the calling address (ret)
  • 11. Caller cleanup ● Remove parameters from the stack ● Restore caller saved registers (EAX, ECX, EDX)
  • 12. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 13. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 14. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 15. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 16. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 17. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 18. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 19. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 20. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 21. Assembly to C mov eax,DWORD PTR [ebx-0x4] mov eax,DWORD PTR [eax] sub esp,0x4 push eax push 0x20 lea eax,[ebp-0x28] push eax call 80483c0 <fgets@plt> add esp,0x10 fgets(userpass, 32, stdin);
  • 22. Buffer Overflows ● Occurs when memory is written past the area that was allocated for it ● Generally caused by functions that write data without bounds checking i.e. scanf, gets, strcpy ● Allows attacker to write arbitrary data into stack frame, possibly overwriting other values or the return pointer
  • 23. Popular Buffer Overflows ● Twilight Princess Wii Homebrew - Horse name can be manipulated to be extremely long, causing a buffer overflow which loads a file at the root of the inserted SD card, giving arbitrary code execution ● Similar bug existed in LEGO Indiana Jones and Super Smash Bros. Brawl ● PS2 Independence Exploit
  • 24. GDB Commands next - next instruction, steps over functions step - next instruction, steps into functions break - set breakpoint stack # - show the stack # deep
  • 26.
  • 27. Goal: Change value of admin
  • 28. Stack when leaving checkAdmin() checkpass checkpass checkpass checkpass int admin GCC Old EBX Old EBP Old EIP
  • 29. Crafting a payload ● This can be done by passing typing 16 A’s and hitting enter, but that is less fun ● Ideally, we send a 20 byte string, with the last 4 bytes set to the value we want admin to take ● Sending any more than 20 bytes could lead to interesting behavior if we aren’t careful ● Problem: Some byte values can’t be typed easily ● Warning: x86 is little endian, so in each word (4 bytes), the least significant byte will be on the left hand side
  • 30. Crafting a payload ● printf ‘x69x85x04x08’ ● python -c ‘print “x69x85x04x08”’ ● perl -e ‘print “x69x85x04x08”’
  • 31. Crafting a payload efficiently ● Python has a struct library that will pack binary data ● python -c "import struct; print 'A' * 16 + struct.pack('<i', 1337)" ● Will fill checkpass with A’s, and will then set admin to equal 1337
  • 32. Stack when leaving checkAdmin() after exploit checkpass checkpass checkpass checkpass admin = 1337 GCC Old EBX Old EBP Old EIP
  • 33. above admin in memory what we want to modify unchecked memory writing admin returned checkAdmin != 0 == win
  • 35.
  • 38. Stack when leaving checkAdmin() checkpass checkpass checkpass checkpass GCC Old EBX Old EBP Old EIP
  • 40. Crafting a payload ● 28 Garbage Bytes ● Return Address: 0x08048569 ● python -c "import struct; print 'A' * 28 + struct.pack('<i', 0x08048569)" ● Profit?
  • 41. Stack when leaving checkAdmin() after exploit
  • 42. unchecked memory writing Return address overwritten Called directly
  • 44. Resources RPISEC Class - https://github.com/RPISEC/MBE Past Presentations - https://csg.utdallas.edu/presentations/binary/ x86 Assembly Guide - https://www.cs.virginia.edu/~evans/cs216/guides/x86.html Practice - http://pwnable.kr/
  • 45. Future Events ● 4/5/17 - Shellcoding: Writing your own Win() ● Potentially format string vulnerabilities, ROP, and ALSR bypassing