CNIT 127: Exploit Development



Ch 2: Stack Overflows in Linux
Updated 8-25-18
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
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
Writing Past End of Array
• Program has crashed
• Denial of service
Print Out More Information
• printf uses a format string
• %x means print in hexadecimal
Segmentation Fault
• Cannot write to address c0000128
Debug
Insert
breakpoint
and run
Memory Map
• Stack ends at c0000000
• Trying to write 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
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 ebp to esp
In function()
Stack frame goes from ebp to esp
Examine the Stack Frame
• Highlighted region is the stack frame of
function()
• Below it is the stack frame of main()
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 Breakpoints
Break before and after gets()
Stack Before gets()
Return value is 0x080488e2
Stack After HELLO
• ASCII values appear in the words outlined
in red
• Return value is 0x080488e2
ASCII
•Google "ASCII"
•0x41 is A
•0x42 is B
•etc.
Stack After AAAAA...
• Stack frame is filled with many A characters
• Return value is overwritten with 0x41414141
Examining the Crash
• eip value is 0x41414141
• 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
127 Ch 2: Stack overflows on Linux

127 Ch 2: Stack overflows on Linux

  • 1.
    CNIT 127: ExploitDevelopment
 
 Ch 2: Stack Overflows in Linux Updated 8-25-18
  • 2.
    Topics • Buffers inC • 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.
  • 5.
    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]); }
  • 6.
    Reading Past Endof Array • We can read data that we shouldn't be seeing • Information disclosure vulnerabilty
  • 7.
    Using gdb (GNUDebugger) • Source code debugging • Because we compiled with gcc -g
  • 8.
    Using gdb (GNUDebugger) • gdb commands list show source code run execute program break insert breakpoint x examine memory
  • 9.
  • 10.
    Writing Past Endof Array • Program has crashed • Denial of service
  • 11.
    Print Out MoreInformation • printf uses a format string • %x means print in hexadecimal
  • 12.
    Segmentation Fault • Cannotwrite to address c0000128
  • 13.
  • 14.
    Memory Map • Stackends at c0000000 • Trying to write past this address caused a segmentation fault
  • 15.
  • 16.
    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
  • 17.
    Stack • POP takesitems off the stack – pop eax – pop ebx
  • 18.
    EBP (Extended BasePointer) • 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
  • 19.
  • 20.
    Purpose • The stack'sprimary 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
  • 22.
    Functions and theStack • 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
  • 23.
    Functions and theStack – 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
  • 24.
    Functions and theStack #include <stdio.h> void function(int a, int b) { int array[5]; } main() { function(1,2); printf("This is where the
 return address pointsn"); }
  • 25.
    Example of aFunction
  • 26.
    Debug and SetBreakpoints
  • 27.
    In main() Stack framegoes from ebp to esp
  • 28.
    In function() Stack framegoes from ebp to esp
  • 29.
    Examine the StackFrame • Highlighted region is the stack frame of function() • Below it is the stack frame of main()
  • 30.
    Disassemble Main • Tocall a function: • push arguments onto the stack • call the function
  • 31.
    Disassemble Function • Prolog: •push ebp onto stack • mov esp into ebp, starting a new stack frame • sub from esp, reserving room for local variables
  • 32.
    Saved Return Address •Next word after stack frame • Address of next instruction to be executed in main()
  • 33.
  • 34.
    Stack Buffer OverflowVulnerability gets() reads user input Does not limit its length
  • 35.
    Compile and Run Segmentationfault indicates an illegal operation
  • 36.
    Debug and SetBreakpoints Break before and after gets()
  • 37.
    Stack Before gets() Returnvalue is 0x080488e2
  • 38.
    Stack After HELLO •ASCII values appear in the words outlined in red • Return value is 0x080488e2
  • 39.
    ASCII •Google "ASCII" •0x41 isA •0x42 is B •etc.
  • 40.
    Stack After AAAAA... •Stack frame is filled with many A characters • Return value is overwritten with 0x41414141
  • 41.
    Examining the Crash •eip value is 0x41414141 • Controlled by user input!
  • 42.
    gdb Commands list showsource 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