CS 475: Lecture 3 Software Vulnerabilities: Rachel Greenstadt April 14, 2015
CS 475: Lecture 3 Software Vulnerabilities: Rachel Greenstadt April 14, 2015
CS 475: Lecture 3 Software Vulnerabilities: Rachel Greenstadt April 14, 2015
Software Vulnerabilities
Rachel Greenstadt
April 14, 2015
Types of Software
Vulnerabilities
• Databases : SQL Injection
• Web apps : XSS
• Broken crypto
• Buffer overflows (and related bugs)
• And more
History : Morris Worm
• Worm was released in 1988 by Robert Morris
• Graduate student at Cornell, son of NSA chief scientist
• Convicted under Computer Fraud and Abuse Act, sentenced to 3 years of
probation and 400 hours of community service
• Worm was intended to propagate slowly and harmlessly measure the size of
the Internet
• Due to a coding error, it created new copies as fast as it could and overloaded
infected machines
• The return address must be saved before the procedure is called. The steps
in the transfer of control to execute a procedure are
1. Save the return address
2. Call procedure (using a branch instruction).
3. Execute the procedure.
4. Return from the procedure (branch to the return address).
Nested procedure
jal: jump and link
jr: jump register
$ra return address
• When the jal B instruction is executed, the return address in register $ra for
procedure A will be overwritten with the return address for procedure B.
Procedure B will return correctly to A, but when procedure A executes the jr
instruction, it will return again to the return address for B, which is the next
instruction after jal B in procedure A. This puts procedure A in an infinite loop.
• To implement the linkage for nested procedures, the return address for each
procedure must be saved somewhere other than register $ra. Note that the
procedure call/return sequence is a LIFO process: the last procedure called is
the first to return. A stack is the natural data structure for saving the return
addresses for nested procedure calls.
System Stack
• The system stack provides a convenient mechanism for dynamically allocating
storage for the various data associated with the execution of a procedure
including:
• parameters
• saved registers
• local variables
• return address
• The system stack is located at the top of the user memory space and grows
downward toward smaller memory addresses. Register $esp is the stack
pointer to the system stack. It contains the address of the first empty location
at the top of the stack.
Linux process memory layout
0xC0000000
user stack
%esp
shared libraries
0x40000000
brk
run time heap
Loaded
from exec 0x08048000
unused
0
System stack
The frame pointer is stored in register $ebp, also called
$fp. A stack frame consists of the memory on the stack
between the frame pointer and the stack pointer.
IA-32 Registers
• $esp : Stack Pointer (SP) : points to the top of the stack (lowest mem
addr)
• Points to last used word in stack or next available word location on stack
(implementation dependent)
• If $ebp for some stack frame is stored at addr X then $eip for that frame is stored at addr X
+4
• Used to reference local vars and parameters since the distance from those to the
frame pointer will not change whereas the distance from those to the stack pointer
will (as other functions are called and the stack pointer is decrem’d …)
• Increment $esp by 4
• RET will load the value stored in $esp into the $eip
Linux process memory layout
0xC0000000
user stack
%esp
shared libraries
0x40000000
brk
run time heap
Loaded
from exec 0x08048000
unused
0
What are buffer overflows?
• Suppose a web server contains a function:
void func(char *str) {
char buf[128]; /* Allocate local buffer
128 bytes reserved on stack */
• When the function is invoked, a new frame with local variables is pushed onto
the stack:
What if buffer is overstuffed?
• Memory pointed to by str is copied onto the stack
void main() {
char large_string[256];
int i;
for( i = 0; i < 255; i++)
large_string[i] = 'A';
function(large_string); }
Buffer Overflows
Buffer Overflows
Buffer Overflows
Buffer Overflows
Buffer Overflows
Buffer Overflows
Buffer Overflows
Buffer Overflows
Executing Attack Code
• Suppose buffer contains attacker-created string
• For example, *str contains a string read from the
network as input to network daemon
1.Guess
- time consuming
- being wrong by 1 byte
will lead to
segmentation fault or
invalid instruction
How to find Shellcode
init(buf);
buf[a] = v;
}
Correct form:
int func(char *user) {
}
History
• First exploit discovered in June 2000.
• Examples:
• wu-ftpd 2.* : remote root
• Linux rpc.statd: remote root
• IRIX telnetd: remote root
• BSD chpass: local root
Vulnerable functions
Any function using a format string.
Printing:
printf, fprintf, sprintf, …
vprintf, vfprintf, vsprintf, …
Exploit
• Dumping arbitrary memory:
• Walk up stack until desired pointer is found.
• printf( “%08x.%08x.%08x.%08x|%s|”)
• Writing to arbitrary memory:
• printf( “hello %n”, &temp) -- writes ‘6’ into temp.
• printf( “%08x.%08x.%08x.%08x.%n”)