Week8 M
Week8 M
Lab 8
Stack Frame
Logical block
pushed when calling a function
popped when returning
Contains:
parameters to functions
local variables
data necessary to recover program state
Function Calls
1. Push 3 arguments
2. Push return address
3. Copy SP into FP to create
new FP and save it on the
stack (SFP)
4. Advance SP to reserve
space for local variables and
state information
Buffer Overflow
What is a buffer?
a contiguous block of memory that holds multiple
instances of the same data type
Example
void copy(char *str) {
char buffer[16];
strcpy(buffer,str);
}
int main() {
char large_string[256];
int i;
for( i = 0; i < 255; i++)
large_string[i] = 'A';
large_string[255] = '\0';
copy(large_string);
return 0;
}
strcpy() is copying the contents of *str (larger_string[]) into buffer[] until string NULL character
buffer[] is much smaller than *str. (16 bytes vs. 256 bytes)
All 240 bytes after buffer in the stack are being overwritten (INCLUDING the SFP and RET)
large_string is filled with the character 'A (0x41)
RET = 0x41414141 which is outside of the process address space
When the function returns and tries to read the next instruction from that address
=> Segmentation Fault!!!
M
e
m
o
r
y
A
d
d
r
e
s
s
e
s
Function Arguments
Function Arguments
Return Address
Return Address
Char *bar
Char *bar
char buffer[16]
buffer[15]
char buffer[16]
buffer[0]
Unallocated Stack Space
\0
In 1st terminal
Run the web server under GDB and get traceback (bt) after the crash
./thttpd p <port number>
Find the pid for thttpd
ps aux | grep thttpd
Run gdb
$ gdb
$ (gdb) attach <pid>
In 2nd terminal
Send your crashing request using wget or curl
In 1st terminal
Continue (c), and when it crashes, do bt
Include this in lab8.txt
Steps 5 & 6
Describe how you would build a remote exploit in the
modified thttpd
Smashing the stack for Fun and Profit
This lecture
-fstack-protector option
GCC flag that protects against stack-based overflow
Random canary is inserted after local variables, first thing to get corrupted
Arguments
Return Address
Frame Pointer
Canary
Local Variables
Lab Hints
How to create assembly language files (.s files)
Remove the .o file
$ rm thttpd.o
Lab Hints
Adding options to ./configure and make
$ CC=gcc CFLAGS=options1 ./configure
$ CC=gcc CFLAGS='-fno-stack-protector' ./configure