SlideShare a Scribd company logo
CNIT 127: Exploit Development




Ch 2: Stack Overflows in Linux
Updated 2-8-21
Topics
• Buffers in C


• Information Disclosure


• gdb: Gnu Debugger


• Segmentation Fault


• The Stack


• Functions and the Stack


• Stack Buffer Overflow
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
Preparing a Debian Machine
• Tools needed to compile in 32-bit and debug


sudo apt updat
e

sudo apt install build-essential gcc-
multilib gdb -y
Exploit A: Information Disclosure
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>


int main()


{


int array[5] = {1, 2, 3, 4, 5};


printf("%dn", array[5]);


}
Reading Past End of Array
• We can read data that we shouldn't be seeing


• Information disclosure vulnerabilty
Using gdb (GNU Debugger)
• Source code debugging


• Because we compiled with gcc -g
Using gdb (GNU Debugger)
• gdb commands


list
	
	
	
show source code


run
	
	
	
execute program


break
	
	
insert breakpoint


x
	
	
	
	
examine memory
Exploit B: Denial of Service
Reading Past End of Array
• printf uses a format string


• %x means print in hexadecimal
Reading Past End of Array
• Program has crashed


• Denial of service
Debug
Insert breakpoint and run
Memory Map
• Stack ends at 0xffffe000
Memory Map
• Stack ends at 0xffffe000


• Trying to read past this address caused a segmentation fault
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
CNIT 127: Ch 2: Stack Overflows in Linux
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");
}
Example of a Function
Debug and Set Breakpoints
In main()
Stack frame goes from esp to ebp
In function()
Stack frame goes from esp to ebp
Examine the Stack Frame
• Highlighted region is the stack frame of
function()


• The next word is the return pointer
Disassemble Main
• To call a function:


• push arguments onto the stack


• call the function
Disassemble Function
• Prolog:


• push ebp onto stack


• mov esp into ebp, starting a new stack frame


• sub from esp, reserving room for local variables
Saved Return Address
• Next word
after stack
frame


• Address of
next
instruction to
be executed
in main()
Stack Buffer Overflow Exploit
Stack Buffer Overflow Vulnerability
gets() reads user input


Does not limit its length
Compile and Run
Segmentation fault indicates an illegal operation
Debug and Set Breakpoint
Break after gets()
Stack After HELLO
• ASCII values for HELLO appear in the
words outlined in red


• Return value is outlined in green
ASCII
•Google "ASCII"


•0x41 is A


•0x42 is B


•etc.
Stack After AAAAA...
• Stack frame is filled with letters


• Return value is overwritten with 0x45454545
Examining the Crash
• eip value is 0x45454545


• Controlled by user input!
gdb Commands
list
	
	
	
	
	
	
	
	
show source code


run
	
	
	
	
	
	
	
	
execute program


break
	
	
	
	
	
	
	
insert breakpoint


x
		
	
	
	
	
	
	
	
examine memory


disassemble
	
	
	
	
show asssembly code


continue
	
	
	
	
	
resume execution


info registers
	
	
	
see registers


info proc mapping
	
see memory map
CNIT 127: Ch 2: Stack Overflows in Linux

More Related Content

PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 3: Shellcode
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 1: Before you Begin
PDF
CNIT 127: Ch 3: Shellcode
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127 Ch Ch 1: Before you Begin
127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 3: Shellcode
127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 1: Before you Begin
CNIT 127: Ch 3: Shellcode
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch 3: Shellcode
CNIT 127 Ch Ch 1: Before you Begin

What's hot (20)

PDF
CNIT 127 Ch 2: Stack overflows on Linux
PDF
CNIT 127: 3: Shellcode
PDF
CNIT 127: Ch 8: Windows overflows (Part 2)
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 4: Introduction to format string bugs
PDF
CNIT 127 Ch 8: Windows overflows (Part 1)
PDF
CNIT 127: Ch 8: Windows overflows (Part 1)
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
PDF
CNIT 127: 4: Format string bugs
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127: Ch 4: Introduction to format string bugs
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127: Ch 18: Source Code Auditing
PDF
CNIT 127 14: Protection Mechanisms
PDF
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
PDF
CNIT 127: Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 5: Introduction to heap overflows
PDF
CNIT 127 Ch 6: The Wild World of Windows
PDF
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127: 3: Shellcode
CNIT 127: Ch 8: Windows overflows (Part 2)
CNIT 127 Ch 4: Introduction to format string bugs (rev. 2-9-17)
CNIT 127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 4: Introduction to format string bugs
CNIT 127 Ch 8: Windows overflows (Part 1)
CNIT 127: Ch 8: Windows overflows (Part 1)
CNIT 127 Lecture 7: Intro to 64-Bit Assembler (not in book)
CNIT 127: 4: Format string bugs
CNIT 127 Ch 3: Shellcode
CNIT 127: Ch 4: Introduction to format string bugs
CNIT 127 14: Protection Mechanisms
CNIT 127: Ch 18: Source Code Auditing
CNIT 127 14: Protection Mechanisms
CNIT 127 Lecture 7: Intro to 64-Bit Assembler
CNIT 127: Ch 2: Stack overflows on Linux
CNIT 127 Ch 5: Introduction to heap overflows
CNIT 127 Ch 6: The Wild World of Windows
CNIT 127 Ch 4: Introduction to format string bugs
Ad

Similar to CNIT 127: Ch 2: Stack Overflows in Linux (20)

PDF
Smashing the stack for fun and profit
PPTX
Buffer overflow attacks
PDF
Basic buffer overflow part1
ODP
Exploiting Memory Overflows
PDF
Hacker Thursdays: An introduction to binary exploitation
PDF
Exploitation Crash Course
PDF
The Stack and Buffer Overflows
PPT
Software Exploitation Techniques by Amit Malik
PDF
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
PPTX
How Functions Work
PDF
Buffer overflow attack
PPTX
C++ Memory Management
PDF
(8) cpp stack automatic_memory_and_static_memory
PDF
StackOverflow
PDF
Presentation buffer overflow attacks and theircountermeasures
PPTX
Introduction to Linux Exploit Development
PPTX
Stack-Based Buffer Overflows
PDF
Stack Frame Protection
PPTX
Reversing malware analysis training part4 assembly programming basics
PDF
Buffer overflow attacks
Smashing the stack for fun and profit
Buffer overflow attacks
Basic buffer overflow part1
Exploiting Memory Overflows
Hacker Thursdays: An introduction to binary exploitation
Exploitation Crash Course
The Stack and Buffer Overflows
Software Exploitation Techniques by Amit Malik
Smash the Stack: Writing a Buffer Overflow Exploit (Win32)
How Functions Work
Buffer overflow attack
C++ Memory Management
(8) cpp stack automatic_memory_and_static_memory
StackOverflow
Presentation buffer overflow attacks and theircountermeasures
Introduction to Linux Exploit Development
Stack-Based Buffer Overflows
Stack Frame Protection
Reversing malware analysis training part4 assembly programming basics
Buffer overflow attacks
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
Institutional Correction lecture only . . .
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
01-Introduction-to-Information-Management.pdf
PDF
Classroom Observation Tools for Teachers
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
RMMM.pdf make it easy to upload and study
PPTX
master seminar digital applications in india
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Complications of Minimal Access Surgery at WLH
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
Pharma ospi slides which help in ospi learning
PPTX
Cell Structure & Organelles in detailed.
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
Institutional Correction lecture only . . .
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
01-Introduction-to-Information-Management.pdf
Classroom Observation Tools for Teachers
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
GDM (1) (1).pptx small presentation for students
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
Anesthesia in Laparoscopic Surgery in India
Pharmacology of Heart Failure /Pharmacotherapy of CHF
VCE English Exam - Section C Student Revision Booklet
RMMM.pdf make it easy to upload and study
master seminar digital applications in india
102 student loan defaulters named and shamed – Is someone you know on the list?
Complications of Minimal Access Surgery at WLH
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Final Presentation General Medicine 03-08-2024.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Pharma ospi slides which help in ospi learning
Cell Structure & Organelles in detailed.
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf

CNIT 127: Ch 2: Stack Overflows in Linux

  • 1. CNIT 127: Exploit Development 
 
 Ch 2: Stack Overflows in Linux Updated 2-8-21
  • 2. Topics • Buffers in C • Information Disclosure • gdb: Gnu Debugger • Segmentation Fault • The Stack • Functions and the Stack • Stack Buffer Overflow
  • 3. 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
  • 4. Preparing a Debian Machine • Tools needed to compile in 32-bit and debug sudo apt updat e sudo apt install build-essential gcc- multilib gdb -y
  • 6. 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> int main() { int array[5] = {1, 2, 3, 4, 5}; printf("%dn", array[5]); }
  • 7. Reading Past End of Array • We can read data that we shouldn't be seeing • Information disclosure vulnerabilty
  • 8. Using gdb (GNU Debugger) • Source code debugging • Because we compiled with gcc -g
  • 9. Using gdb (GNU Debugger) • gdb commands list show source code run execute program break insert breakpoint x examine memory
  • 10. Exploit B: Denial of Service
  • 11. Reading Past End of Array • printf uses a format string • %x means print in hexadecimal
  • 12. Reading Past End of Array • Program has crashed • Denial of service
  • 14. Memory Map • Stack ends at 0xffffe000
  • 15. Memory Map • Stack ends at 0xffffe000 • Trying to read past this address caused a segmentation fault
  • 17. 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
  • 18. Stack • POP takes items off the stack – pop eax – pop ebx
  • 19. 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
  • 21. 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
  • 23. 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
  • 24. 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
  • 25. 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"); }
  • 26. Example of a Function
  • 27. Debug and Set Breakpoints
  • 28. In main() Stack frame goes from esp to ebp
  • 29. In function() Stack frame goes from esp to ebp
  • 30. Examine the Stack Frame • Highlighted region is the stack frame of function() • The next word is the return pointer
  • 31. Disassemble Main • To call a function: • push arguments onto the stack • call the function
  • 32. Disassemble Function • Prolog: • push ebp onto stack • mov esp into ebp, starting a new stack frame • sub from esp, reserving room for local variables
  • 33. Saved Return Address • Next word after stack frame • Address of next instruction to be executed in main()
  • 35. Stack Buffer Overflow Vulnerability gets() reads user input Does not limit its length
  • 36. Compile and Run Segmentation fault indicates an illegal operation
  • 37. Debug and Set Breakpoint Break after gets()
  • 38. Stack After HELLO • ASCII values for HELLO appear in the words outlined in red • Return value is outlined in green
  • 39. ASCII •Google "ASCII" •0x41 is A •0x42 is B •etc.
  • 40. Stack After AAAAA... • Stack frame is filled with letters • Return value is overwritten with 0x45454545
  • 41. Examining the Crash • eip value is 0x45454545 • Controlled by user input!
  • 42. gdb Commands list show source code run execute program break insert breakpoint x examine memory disassemble show asssembly code continue resume execution info registers see registers info proc mapping see memory map