SlideShare a Scribd company logo
CNIT 127: Exploit Development



Ch 2: Stack Overflows in Linux
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 -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 -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
• <main+3> puts a's value, 1, onto the stack
• <main+5> puts b's value, 2, onto the stack
• <main+7> 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 at ret
Disassemble main
• Next instruction after the call to
return_input is 0x08048453
Run Till First Breakpoint
• Highlighted values are the saved EBP and
the RET address
Continue and Input 40 Characters
• 30 characters are stored correctly
• Next four overwrite stored EBP with 0x44444444
('DDDD')
• Next four overwrite RET
Examine $eip, Step One Instruction
• x/1i means "Examine one instruction"
• stepi executes one machine language
instruction
Observe Overwritten Registers
• ebp and eip are 0x44444444 = 'DDDD'
Stack Overflow
Controlling eip
• 0x44444444 is invalid and causes the
program to halt
• We can put any valid memory address
there
• However, at the point of the crash we
have returned to main() so we should
choose an instruction in main()
Call to return_input
• 0x0804844e
– stored backwards in a string
– "x4ex84x04x08"
Python Exploit Code
• sys.stdout.write doesn't put a space or
linefeed at end
How to Debug with Arguments

More Related Content

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 1: Before you Begin
PDF
CNIT 127: Ch 3: Shellcode
PDF
127 Ch 2: Stack overflows on Linux
PDF
CNIT 127 Ch 3: Shellcode
PDF
CNIT 127: Ch 8: Windows overflows (Part 2)
PDF
CNIT 127 Ch 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 1: Before you Begin
CNIT 127: Ch 3: Shellcode
127 Ch 2: Stack overflows on Linux
CNIT 127 Ch 3: Shellcode
CNIT 127: Ch 8: Windows overflows (Part 2)
CNIT 127 Ch Ch 1: Before you Begin

What's hot (20)

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

Viewers also liked (20)

PDF
CNIT 123 Ch 1: Ethical Hacking Overview
PDF
Ch 2: TCP/IP Concepts Review
PDF
Ch 6: Enumeration
PDF
Ch 12: Cryptography
PDF
Ch 13: Network Protection Systems
PDF
Ch 10: Hacking Web Servers
PDF
Ch 5: Port Scanning
PDF
CNIT 128 5: Mobile malware
PDF
CNIT 126 7: Analyzing Malicious Windows Programs
PDF
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
PDF
Ch 4: Footprinting and Social Engineering
PDF
CNIT 121: 10 Enterprise Services
PDF
CNIT 127 Ch 6: The Wild World of Windows
PDF
CISSP Prep: Ch 3. Asset Security
PDF
CNIT 128: 6: Mobile services and mobile Web (part 1: Beginning Through OAuth)
PDF
Practical Malware Analysis Ch13
PDF
Practical Malware Analysis: Ch 9: OllyDbg
PDF
CNIT 121: 8 Forensic Duplication
PDF
CNIT 128 Ch 6: Mobile services and mobile Web (part 2: SAML to end)
PDF
Ch 8: Desktop and Server OS Vulnerabilites
CNIT 123 Ch 1: Ethical Hacking Overview
Ch 2: TCP/IP Concepts Review
Ch 6: Enumeration
Ch 12: Cryptography
Ch 13: Network Protection Systems
Ch 10: Hacking Web Servers
Ch 5: Port Scanning
CNIT 128 5: Mobile malware
CNIT 126 7: Analyzing Malicious Windows Programs
CNIT 129S: 12: Attacking Users: Cross-Site Scripting (Part 2 of 3)
Ch 4: Footprinting and Social Engineering
CNIT 121: 10 Enterprise Services
CNIT 127 Ch 6: The Wild World of Windows
CISSP Prep: Ch 3. Asset Security
CNIT 128: 6: Mobile services and mobile Web (part 1: Beginning Through OAuth)
Practical Malware Analysis Ch13
Practical Malware Analysis: Ch 9: OllyDbg
CNIT 121: 8 Forensic Duplication
CNIT 128 Ch 6: Mobile services and mobile Web (part 2: SAML to end)
Ch 8: Desktop and Server OS Vulnerabilites
Ad

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

PDF
05_Return_to_Libc.pdf
PPTX
test
PDF
07 control+structures
PDF
Exploitation Crash Course
PPTX
Buffer overflow attacks
PDF
Intro. to static analysis
PPTX
JVM Memory Model - Yoav Abrahami, Wix
PDF
Efficient Bytecode Analysis: Linespeed Shellcode Detection
PPTX
Bypassing DEP using ROP
PPT
PPTX
PPTX
PPTX
Dr.C S Prasanth-Physics ppt.pptx computer
PDF
JavaOne 2015 Java Mixed-Mode Flame Graphs
PDF
(8) cpp stack automatic_memory_and_static_memory
PDF
fg.workshop: Software vulnerability
PDF
Advanced features of Lisp functions Advanced features of Lisp functions
PDF
Process Address Space: The way to create virtual address (page table) of user...
PPTX
Linux Initialization Process (1)
PPT
L4 functions
05_Return_to_Libc.pdf
test
07 control+structures
Exploitation Crash Course
Buffer overflow attacks
Intro. to static analysis
JVM Memory Model - Yoav Abrahami, Wix
Efficient Bytecode Analysis: Linespeed Shellcode Detection
Bypassing DEP using ROP
Dr.C S Prasanth-Physics ppt.pptx computer
JavaOne 2015 Java Mixed-Mode Flame Graphs
(8) cpp stack automatic_memory_and_static_memory
fg.workshop: Software vulnerability
Advanced features of Lisp functions Advanced features of Lisp functions
Process Address Space: The way to create virtual address (page table) of user...
Linux Initialization Process (1)
L4 functions

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)

PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PPTX
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
Anesthesia in Laparoscopic Surgery in India
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
Business Ethics Teaching Materials for college
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PPTX
Introduction and Scope of Bichemistry.pptx
PPTX
Cell Structure & Organelles in detailed.
PPTX
NOI Hackathon - Summer Edition - GreenThumber.pptx
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
UNDER FIVE CLINICS OR WELL BABY CLINICS.pptx
Pre independence Education in Inndia.pdf
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Microbial disease of the cardiovascular and lymphatic systems
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Open folder Downloads.pdf yes yes ges yes
Anesthesia in Laparoscopic Surgery in India
Open Quiz Monsoon Mind Game Final Set.pptx
Business Ethics Teaching Materials for college
The Final Stretch: How to Release a Game and Not Die in the Process.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
Introduction and Scope of Bichemistry.pptx
Cell Structure & Organelles in detailed.
NOI Hackathon - Summer Edition - GreenThumber.pptx
TR - Agricultural Crops Production NC III.pdf
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
school management -TNTEU- B.Ed., Semester II Unit 1.pptx

CNIT 127 Ch 2: Stack overflows on Linux

  • 1. CNIT 127: Exploit Development
 
 Ch 2: Stack Overflows in Linux
  • 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 -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 -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"); }
  • 39. • <main+3> puts a's value, 1, onto the stack • <main+5> puts b's value, 2, onto the stack • <main+7> calls function, which implicitly pushes RET (EIP) onto the stack
  • 40. • 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
  • 41. Stack Buffer Overflow Vulnerability
  • 44. Set Breakpoints • At call to gets • And at ret
  • 45. Disassemble main • Next instruction after the call to return_input is 0x08048453
  • 46. Run Till First Breakpoint • Highlighted values are the saved EBP and the RET address
  • 47. Continue and Input 40 Characters • 30 characters are stored correctly • Next four overwrite stored EBP with 0x44444444 ('DDDD') • Next four overwrite RET
  • 48. Examine $eip, Step One Instruction • x/1i means "Examine one instruction" • stepi executes one machine language instruction
  • 49. Observe Overwritten Registers • ebp and eip are 0x44444444 = 'DDDD'
  • 51. Controlling eip • 0x44444444 is invalid and causes the program to halt • We can put any valid memory address there • However, at the point of the crash we have returned to main() so we should choose an instruction in main()
  • 52. Call to return_input • 0x0804844e – stored backwards in a string – "x4ex84x04x08"
  • 53. Python Exploit Code • sys.stdout.write doesn't put a space or linefeed at end
  • 54. How to Debug with Arguments