SlideShare a Scribd company logo
CNIT 127: Exploit Development



Ch 2: Stack Overflows in Linux
Rev. 1-27-18
Stack-based Buffer Overflows
• Most popular and best understood
exploitation method
• Aleph One's "Smashing the Stack for Fun
and Profit" (1996)
– Link Ch 2a
• Buffer
– A limited, contiguously allocated set of
memory
– In C, usually an array
C and C++ Lack Bounds-Checking
• It is the programmer's responsibility to ensure
that array indices remain in the valid range
#include <stdio.h>
#include <string.h>
int main() {
int array[5] = {1, 2, 3, 4, 5};
printf("%dn", array[5]);
}
Using gdb (GNU Debugger)
• Compile with symbols
– gcc -g --no-pie -o ch2a ch2a.c
• Run program in debugger
– gdb ch2a
• Show code, place breakpoint, run to it
– list
– break 7
– run
• Examine the memory storing "array"
– x/10x &array
Reading Past End of Array
CNIT 127 Ch 2: Stack overflows on Linux
Reading Past End of Array
• array[5] = 0xb7fb63c4
Writing Past End of Array
Compile and Run, Crash
Debug
• Open in debugger
– gdb ch2b
• Run
– run
• Examine the memory storing "array"
– x/50x &array
CNIT 127 Ch 2: Stack overflows on Linux
Buffer Overflow
• Many RAM locations now contain 0xa
• But why, precisely, did that cause a crash?
Debug
• Examine registers
• info registers
• Examine assembly code near eip
• x/10i $eip-10
10 (0xa) is not in any register
Last Command Writes $0xa to RAM
• Evidently we went so far we exited the
RAM allocated to the program
Intel v. AT&T Format
• gdb uses AT&T format by default, which is
popular with Linux users
– mov source, destination
• Windows users more commonly use Intel
format
– MOV DESTINATION, SOURCE
Jasmin
Assembly Language Simulator
CNIT 127 Ch 2: Stack overflows on Linux
The Stack
LIFO (Last-In, First-Out)
• ESP (Extended Stack Pointer) register
points to the top of the stack
• PUSH puts items on the stack
– push 1
– push addr var
Stack
• POP takes items off the stack
– pop eax
– pop ebx
EBP (Extended Base Pointer)
• EBP is typically used for calculated
addresses on the stack
– mov eax, [ebp+10h]
• Copies the data 16 bytes down the stack
into the EAX register
Functions and the Stack
Purpose
• The stack's primary purpose is to make the
use of functions more efficient
• When a function is called, these things occur:
– Calling routine stops processing its instructions
– Saves its current state
– Transfers control to the function
– Function processes its instructions
– Function exits
– State of the calling function is restored
– Calling routine's execution resumes
Example
Using gdb (GNU Debugger)
• Compile with symbols
– gcc -g --no-pie -o ch2d ch2d.c
• Run program in debugger
– gdb ch2d
• Show code, place breakpoint, run to it
– list 1,12
– break 9
– break 11
– break 4
CNIT 127 Ch 2: Stack overflows on Linux
Using gdb (GNU Debugger)
• Run to breakpoint after line 9
• run
• Examine registers
– info reg
• Run to breakpoint after line 4
• continue
• Examine registers
– info reg
In main() before calling function()
• esp 0xbffff460
• ebp 0xbffff468 (start of stack frame)
• eip 0x8048414 <main+17>
In function()
• esp 0xbffff430
• ebp 0xbffff450 (start of stack frame)
• eip 0x8048401 <function+6>
Examine the Stack
– x/12x $esp
• Highlight is function()'s stack frame
• Outlined area shows these items
– Return address
– Arguments of function(1,2)
• Next to the left is the original value of $ebp
CNIT 127 Ch 2: Stack overflows on Linux
Using gdb (GNU Debugger)
• Run to breakpoint after line 11
• continue
• Examine registers
– info reg
In main() after calling function()
• esp 0xbffff460
• ebp 0xbffff468
• eip 0x8048420 <main+29>
Functions and the Stack
• Primary purpose of the stack
– To make functions more efficient
• When a function is called
– Push function's arguments onto the stack
– Call function, which pushes the return address
RET onto the stack, which is the EIP at the
time the function is called
Functions and the Stack
– Before function starts, a prolog executes,
pushing EBP onto the stack
– It then copies ESP into EBP
– Calculates size of local variables
– Reserves that space on the stack, by
subtracting the size from ESP
– Pushes local variables onto stack
Functions and the Stack
#include <stdio.h>
void function(int a, int b)
{
int array[5];
}
main()
{
function(1,2);
printf("This is where the

return address pointsn");
}
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on Linux
• <main+17> puts b's value, 2, onto the stack
• <main+19> puts a's value, 1, onto the stack
• <main+21> calls function, which implicitly pushes RET
(EIP) onto the stack
• Prolog
– Push EBP onto stack
– Move ESP into EBP
– Subtract 0x14 from stack to reserve space for array
• leave restores the original stack, same as
– mov esp, ebp
– pop ebp
Stack Buffer Overflow Vulnerability
Compile and Run
Disassemble return_input
Set Breakpoints
• At call to gets
• And after it
Disassemble main
• Next instruction after the call to return_input is
0x08048882
Run Till First Breakpoint
• Stack frame
highlighted
• Last word in
stack frame is
saved ebp
• Next word is
saved eip
Continue and Input 50 Characters
• Stored EBP is overwritten with
0x45444444 = "DDDE"
• Stored RET becomes 0x45454545 = "EEEE"
Continue to See Crash
Controlling eip
• 0x45454545 is invalid and causes the
program to halt
• Let's put this address there
Encoding eip as a String
• 0x0804887d
• Stored backwards as a string
• "x7dx88x04x08"
Python Exploit Code
Another Way
• Put the exploit into a text file
• Use input redirection <
How to Debug with Arguments

More Related Content

PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: Ch 3: Shellcode
PDF
CNIT 127 Ch 1: Before you Begin
PDF
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
PDF
CNIT 127: Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127: Ch 3: Shellcode
CNIT 127 Ch 1: Before you Begin
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 4: Introduction to format string bugs
127 Ch 2: Stack overflows on Linux

What's hot (20)

PDF
CNIT 127 Ch 5: Introduction to heap overflows
PDF
CNIT 127: Ch 2: Stack Overflows in Linux
PDF
CNIT 127: Ch 8: Windows overflows (Part 2)
PDF
CNIT 127: Ch 4: Introduction to format string bugs
PDF
CNIT 127 Ch Ch 1: Before you Begin
PDF
CNIT 127 Ch 3: Shellcode
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: 4: Format string bugs
PDF
CNIT 127: 3: Shellcode
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
PDF
CNIT 127: Ch 18: Source Code Auditing
PDF
CNIT 127: Ch 8: Windows overflows (Part 1)
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127 Ch 8: Windows overflows (Part 1)
PDF
CNIT 127 Ch 4: Introduction to format string bugs
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
PDF
Triton and symbolic execution on gdb
CNIT 127 Ch 5: Introduction to heap overflows
CNIT 127: Ch 2: Stack Overflows in Linux
CNIT 127: Ch 8: Windows overflows (Part 2)
CNIT 127: Ch 4: Introduction to format string bugs
CNIT 127 Ch Ch 1: Before you Begin
CNIT 127 Ch 3: Shellcode
127 Ch 2: Stack overflows on Linux
CNIT 127: 4: Format string bugs
CNIT 127: 3: Shellcode
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
CNIT 127: Ch 18: Source Code Auditing
CNIT 127: Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: Shellcode
CNIT 127 14: Protection Mechanisms
CNIT 127 14: Protection Mechanisms
CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
Triton and symbolic execution on gdb
Ad

Similar to CNIT 127 Ch 2: Stack overflows on Linux (20)

PDF
Intro. to static analysis
PDF
05_Return_to_Libc.pdf
PDF
Exploitation Crash Course
PPTX
Buffer overflow attacks
PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
PDF
07 control+structures
PPTX
test
PPT
PPTX
JVM Memory Model - Yoav Abrahami, Wix
PPTX
Return Oriented Programming (ROP chaining)
PDF
Advanced features of Lisp functions Advanced features of Lisp functions
PPTX
Bypassing DEP using ROP
PDF
(8) cpp stack automatic_memory_and_static_memory
PDF
Return Oriented Programming
PPTX
PDF
Максим Харченко. Erlang lincx
PDF
0.5mln packets per second with Erlang
PPTX
PDF
Diving Into Memory Allocation to Understand Buffer Overflow Better
PPTX
chap5.pptxasasasasadfdfdfdfdfddffdfdfdfdd
Intro. to static analysis
05_Return_to_Libc.pdf
Exploitation Crash Course
Buffer overflow attacks
Efficient Bytecode Analysis: Linespeed Shellcode Detection
07 control+structures
test
JVM Memory Model - Yoav Abrahami, Wix
Return Oriented Programming (ROP chaining)
Advanced features of Lisp functions Advanced features of Lisp functions
Bypassing DEP using ROP
(8) cpp stack automatic_memory_and_static_memory
Return Oriented Programming
Максим Харченко. Erlang lincx
0.5mln packets per second with Erlang
Diving Into Memory Allocation to Understand Buffer Overflow Better
chap5.pptxasasasasadfdfdfdfdfddffdfdfdfdd
Ad

More from Sam Bowne (20)

PDF
Introduction to the Class & CISSP Certification
PDF
Cyberwar
PDF
3: DNS vulnerabilities
PDF
8. Software Development Security
PDF
4 Mapping the Application
PDF
3. Attacking iOS Applications (Part 2)
PDF
12 Elliptic Curves
PDF
11. Diffie-Hellman
PDF
2a Analyzing iOS Apps Part 1
PDF
9 Writing Secure Android Applications
PDF
12 Investigating Windows Systems (Part 2 of 3)
PDF
10 RSA
PDF
12 Investigating Windows Systems (Part 1 of 3
PDF
9. Hard Problems
PDF
8 Android Implementation Issues (Part 1)
PDF
11 Analysis Methodology
PDF
8. Authenticated Encryption
PDF
7. Attacking Android Applications (Part 2)
PDF
7. Attacking Android Applications (Part 1)
PDF
5. Stream Ciphers
Introduction to the Class & CISSP Certification
Cyberwar
3: DNS vulnerabilities
8. Software Development Security
4 Mapping the Application
3. Attacking iOS Applications (Part 2)
12 Elliptic Curves
11. Diffie-Hellman
2a Analyzing iOS Apps Part 1
9 Writing Secure Android Applications
12 Investigating Windows Systems (Part 2 of 3)
10 RSA
12 Investigating Windows Systems (Part 1 of 3
9. Hard Problems
8 Android Implementation Issues (Part 1)
11 Analysis Methodology
8. Authenticated Encryption
7. Attacking Android Applications (Part 2)
7. Attacking Android Applications (Part 1)
5. Stream Ciphers

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PDF
RMMM.pdf make it easy to upload and study
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Basic Mud Logging Guide for educational purpose
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
Pre independence Education in Inndia.pdf
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
human mycosis Human fungal infections are called human mycosis..pptx
RMMM.pdf make it easy to upload and study
Module 4: Burden of Disease Tutorial Slides S2 2025
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Pharmacology of Heart Failure /Pharmacotherapy of CHF
O7-L3 Supply Chain Operations - ICLT Program
Basic Mud Logging Guide for educational purpose
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Abdominal Access Techniques with Prof. Dr. R K Mishra
VCE English Exam - Section C Student Revision Booklet
O5-L3 Freight Transport Ops (International) V1.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Pre independence Education in Inndia.pdf

CNIT 127 Ch 2: Stack overflows on Linux

  • 1. CNIT 127: Exploit Development
 
 Ch 2: Stack Overflows in Linux Rev. 1-27-18
  • 2. Stack-based Buffer Overflows • Most popular and best understood exploitation method • Aleph One's "Smashing the Stack for Fun and Profit" (1996) – Link Ch 2a • Buffer – A limited, contiguously allocated set of memory – In C, usually an array
  • 3. C and C++ Lack Bounds-Checking • It is the programmer's responsibility to ensure that array indices remain in the valid range #include <stdio.h> #include <string.h> int main() { int array[5] = {1, 2, 3, 4, 5}; printf("%dn", array[5]); }
  • 4. Using gdb (GNU Debugger) • Compile with symbols – gcc -g --no-pie -o ch2a ch2a.c • Run program in debugger – gdb ch2a • Show code, place breakpoint, run to it – list – break 7 – run • Examine the memory storing "array" – x/10x &array
  • 5. Reading Past End of Array
  • 7. Reading Past End of Array • array[5] = 0xb7fb63c4
  • 8. Writing Past End of Array
  • 10. Debug • Open in debugger – gdb ch2b • Run – run • Examine the memory storing "array" – x/50x &array
  • 12. Buffer Overflow • Many RAM locations now contain 0xa • But why, precisely, did that cause a crash?
  • 13. Debug • Examine registers • info registers • Examine assembly code near eip • x/10i $eip-10
  • 14. 10 (0xa) is not in any register
  • 15. Last Command Writes $0xa to RAM • Evidently we went so far we exited the RAM allocated to the program
  • 16. Intel v. AT&T Format • gdb uses AT&T format by default, which is popular with Linux users – mov source, destination • Windows users more commonly use Intel format – MOV DESTINATION, SOURCE
  • 20. LIFO (Last-In, First-Out) • ESP (Extended Stack Pointer) register points to the top of the stack • PUSH puts items on the stack – push 1 – push addr var
  • 21. Stack • POP takes items off the stack – pop eax – pop ebx
  • 22. EBP (Extended Base Pointer) • EBP is typically used for calculated addresses on the stack – mov eax, [ebp+10h] • Copies the data 16 bytes down the stack into the EAX register
  • 24. Purpose • The stack's primary purpose is to make the use of functions more efficient • When a function is called, these things occur: – Calling routine stops processing its instructions – Saves its current state – Transfers control to the function – Function processes its instructions – Function exits – State of the calling function is restored – Calling routine's execution resumes
  • 26. Using gdb (GNU Debugger) • Compile with symbols – gcc -g --no-pie -o ch2d ch2d.c • Run program in debugger – gdb ch2d • Show code, place breakpoint, run to it – list 1,12 – break 9 – break 11 – break 4
  • 28. Using gdb (GNU Debugger) • Run to breakpoint after line 9 • run • Examine registers – info reg • Run to breakpoint after line 4 • continue • Examine registers – info reg
  • 29. In main() before calling function() • esp 0xbffff460 • ebp 0xbffff468 (start of stack frame) • eip 0x8048414 <main+17>
  • 30. In function() • esp 0xbffff430 • ebp 0xbffff450 (start of stack frame) • eip 0x8048401 <function+6>
  • 31. Examine the Stack – x/12x $esp • Highlight is function()'s stack frame • Outlined area shows these items – Return address – Arguments of function(1,2) • Next to the left is the original value of $ebp
  • 33. Using gdb (GNU Debugger) • Run to breakpoint after line 11 • continue • Examine registers – info reg
  • 34. In main() after calling function() • esp 0xbffff460 • ebp 0xbffff468 • eip 0x8048420 <main+29>
  • 35. Functions and the Stack • Primary purpose of the stack – To make functions more efficient • When a function is called – Push function's arguments onto the stack – Call function, which pushes the return address RET onto the stack, which is the EIP at the time the function is called
  • 36. Functions and the Stack – Before function starts, a prolog executes, pushing EBP onto the stack – It then copies ESP into EBP – Calculates size of local variables – Reserves that space on the stack, by subtracting the size from ESP – Pushes local variables onto stack
  • 37. Functions and the Stack #include <stdio.h> void function(int a, int b) { int array[5]; } main() { function(1,2); printf("This is where the
 return address pointsn"); }
  • 40. • <main+17> puts b's value, 2, onto the stack • <main+19> puts a's value, 1, onto the stack • <main+21> calls function, which implicitly pushes RET (EIP) onto the stack
  • 41. • Prolog – Push EBP onto stack – Move ESP into EBP – Subtract 0x14 from stack to reserve space for array • leave restores the original stack, same as – mov esp, ebp – pop ebp
  • 42. Stack Buffer Overflow Vulnerability
  • 45. Set Breakpoints • At call to gets • And after it
  • 46. Disassemble main • Next instruction after the call to return_input is 0x08048882
  • 47. Run Till First Breakpoint • Stack frame highlighted • Last word in stack frame is saved ebp • Next word is saved eip
  • 48. Continue and Input 50 Characters • Stored EBP is overwritten with 0x45444444 = "DDDE" • Stored RET becomes 0x45454545 = "EEEE"
  • 50. Controlling eip • 0x45454545 is invalid and causes the program to halt • Let's put this address there
  • 51. Encoding eip as a String • 0x0804887d • Stored backwards as a string • "x7dx88x04x08"
  • 53. Another Way • Put the exploit into a text file • Use input redirection <
  • 54. How to Debug with Arguments