L4P3 Stack Overflow
L4P3 Stack Overflow
Stack
Local variables
Stack operation
Stack: A stack is an abstract data type. Stack contains objects in which the last object placed on the stack will be the first object removed (last in, first out queue, or a LIFO).
PUSH adds an element at the top of the stack. POP removes the element at the top of the stack.
The stack consists of logical stack frames that are pushed when calling a function and popped when returning. Stack Pointer (SP) points to the top of the stack Frame pointer points to a fixed location within a frame. FP is used to access to the variables in stack
Stack
address decreases
Function A
Top of stack
Function
void fun(int a, int b, int c) { char buffer1[5]; char buffer2[10]; } void main() { fun(1,2,3); }
Stack
main
fun
Procedure prolog/epilog
Procedure prologue:
prologue
Procedure prolog/epilog
Procedure epilog:
the stack must be cleaned up again movl %ebp,%esp popl %ebp ret
prologue
epilog
High level languages need function and procedure. Stack helps to implement this.
The stack is used to dynamically allocate the local variables used in functions, to pass parameters to the functions, and to return values from the function.
Stack in Action
Stack in action
Memory address increases
void fun(int a, int b, int c) { char buffer1[5]; char buffer2[10]; } void main() { fun(1,2,3); }
Top of stack
void main(int argc, char *argv[]) { char buffer[512]; if (argc > 1) { strcpy(buffer,argv[1]); printf(%s,buffer); } }
nothing happens
Instead of hello by giving suitable input we can modify the behavior of vul1, e.g. to spawn a shell
char buffer2[10];
int *ret; ret = buffer1 + 12; (*ret) += 8; } void main() { int x;
Demo example{2,3}.c
x = 0;
function(1,2,3); x = 1; printf("%d\n",x);
\x55\x89\xe5\x83\xec\x10.....\xc9\xc3
Shellcode (1)
In most cases we'll simply want the program to spawn a shell, from where we can then issue other commands as we wish.
Place the code with are trying to execute in the buffer we are overflowing, and overwrite the return address so it points back into the buffer.
Shellcode (2)
Memory address decreases 3 2 1 Saved IP Saved EBP Buffer1
Top of stack
Demo
Classification of bufferoverflow
Local buffer overflow: increase privilege on the local machine Remote buffer overflow: gain access to a remote machine
locking down of permissions on memory pages: prevent the creation of writable/executable file mappings (anonymous mappings are already made non-executable) we also refuse to turn a writable or non-executable mapping into an executable one and vice versa.
introducing randomness into the virtual memory layout for a particular process
varying levels of randomness during the process of loading a binary so that the binary mapping, dynamic library linking and stack memory regions are all randomized before the process begins executing
Compiler-Enforced Protection
Special values called canaries may be inserted into arbitrary points in memory to detect the corruption of saved control structures
The basic concept of overflowing a buffer to modify a return address or function pointer on the stack may be addressed by placing canary values in a location that would cause them to be overflowed before the return address may be reached
StackGuard
StackGuard adds code at the RTL level to the function_prologue and function_epilogue functions within GCC to provide the generation and validation of the stack canary random canary directly before the return address Canary changes, it means that there is stack smashing Defeating StackGuard
Possibility of bypassing StackGuard by overwriting local variables that could then be used to compromise the protection. overwriting function pointers and frame pointers stored on the stack can also lead to compromise. Protection against nonstack-based attack vectors such as heap overflows is also beyond the scope of StackGuard
StackShield
the return address is copied to the Global Ret Stack the return address is copied from the Global Ret Stack to the applications stack, overwriting any possible compromise Defeating StackShield Methods for defeating StackShield are similar to those used to bypass StackGuard protection