Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Buffer Overflow Demo by Saurabh Sharma
1. Buffer Overflows by: Saurabh Sharma
2. BUFFER
3. Buffer: The memory area where the user input is stored. Overflow: The user input exceeds the maximum size of the buffer, overwriting the other areas of the memory and corrupting those areas. Anatomy of Buffer Overflows
4. void get_input() { char buf[1024]; gets(buf); } void main(intargc, char*argv[]){ get_input(); } User controls the input. Malicious user can supply the input of more than 500 chars. So what ?? User can supply a malicious input which can execute some other exe. This can also be your cmd.exe and may lead to the system compromise. A small example
5. Text: Contains instructions Data: Contains initialized variables BSS: Contains uninitialized global and static variables(initialized to 0) Heap: Contains dynamic, uninitialized data(malloc()) Stack: Contains function arguments and local variables Memory overview
6. Stack Frame:holds variables and data for function Stack grows from higher memory location to lower memory location Heap: lower to higher Memory overview
7. General purpose: For basic calculations. ESI, EDI: Used mostly with arrays Flags: Outcome of several instructions set the flags Segment: Code, stack, data. EBP:Base pointer, points to the beginning of the current stack frame ESP: Stack pointer, points to the top of the stack EIP: Instruction pointer, points to the next instruction REGISTERS
8. Stack is a LIFO data structure. Temporary memory, formed when the function called. A new stack frame created when the function is called. The return address is saved just above the local variables. Stack Layout Lower address parameters Return addr(saved EIP) Saved EBP Stack grows Local variables Higher address
9. So, if the EIP can be controlled, the next instruction to be executed can be controlled. Stack Layout Lower address parameters Return addr(saved EIP) Saved EBP Stack grows Local variables Higher address
10. Machine code which is injected into the overflown buffer Does the work for you WORK: executing a third program, adding an administrator etc. SHELLCODE
15. Buffer size must be checked Use alternative functions e.g. strncpy(dst, src, dst_size-1) instead of strcpy(dst, src) Other protection mechanisms like /GS(stack cookie), ASLR, SafeSEH compilation PREVENTION