Intro to Exploitation Stack Overflows James McFadyen UTD Computer Security Group 10/20/2011.

Slides:



Advertisements
Similar presentations
Buffer Overflows Nick Feamster CS 6262 Spring 2009 (credit to Vitaly S. from UT for slides)
Advertisements

Smashing the Stack for Fun and Profit
Defenses. Preventing hijacking attacks 1. Fix bugs: – Audit software Automated tools: Coverity, Prefast/Prefix. – Rewrite software in a type safe languange.
David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
The art of exploitation
CMSC 414 Computer and Network Security Lecture 22 Jonathan Katz.
Introduction to exploitation
Review: Software Security David Brumley Carnegie Mellon University.
Foundations of Network and Computer Security J J ohn Black Lecture #30 Nov 26 th 2007 CSCI 6268/TLEN 5831, Fall 2007.
Buffer Overflows By Tim Peterson Joel Miller Dan Block.
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
Gabe Kanzelmeyer CS 450 4/14/10.  What is buffer overflow?  How memory is processed and the stack  The threat  Stack overrun attack  Dangers  Prevention.
Windows XP SP2 Stack Protection Jimmy Hermansson Johan Tibell.
September 22, 2014 Pengju (Jimmy) Jin Section E
Control hijacking attacks Attacker’s goal: – Take over target machine (e.g. web server) Execute arbitrary code on target by hijacking application control.
CMSC 414 Computer and Network Security Lecture 20 Jonathan Katz.
University of Washington CSE 351 : The Hardware/Software Interface Section 5 Structs as parameters, buffer overflows, and lab 3.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Lecture 6: Buffer Overflow CS 436/636/736 Spring 2014 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
Exploiting Buffer Overflows on AIX/PowerPC HP-UX/PA-RISC Solaris/SPARC.
Buffer Overflows : An In-depth Analysis. Introduction Buffer overflows were understood as early as 1972 The legendary Morris Worm made use of a Buffer.
Introduction: Exploiting Linux. Basic Concepts Vulnerability A flaw in a system that allows an attacker to do something the designer did not intend,
Mitigation of Buffer Overflow Attacks
CNIT 127: Exploit Development Ch 4: Introduction to Format String Bugs.
CSCD 303 Essential Computer Security Spring 2013 Lecture 17 Buffer Overflow Attacks.
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
Derived from "x86 Assembly Registers and the Stack" by Rodney BeedeRodney Beede x86 Assembly Registers and the Stack Nov 2009.
Overflow Examples 01/13/2012. ACKNOWLEDGEMENTS These slides where compiled from the Malware and Software Vulnerabilities class taught by Dr Cliff Zou.
Smashing the Stack Overview The Stack Region Buffer Overflow
Overflows & Exploits. In the beginning 11/02/1988 Robert Morris, Jr., a graduate student in Computer Science at Cornell, wrote an experimental, self-replicating,
Lecture 8: Buffer Overflow CS 436/636/736 Spring 2013 Nitesh Saxena *Adopted from a previous lecture by Aleph One (Smashing the Stack for Fun and Profit)
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
Lecture 9: Buffer Ovefflows and ROP EEN 312: Processors: Hardware, Software, and Interfacing Department of Electrical and Computer Engineering Spring 2014,
CNIT 127: Exploit Development Ch 1: Before you begin.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Buffer Overflows Taught by Scott Coté.-. _ _.-. / \.-. ((___)).-. / \ /.ooM \ / \.-. [ x x ].-. / \ /.ooM \ -/ \ /-----\-----/---\--\ /--/---\-----/-----\ / \-
CS642: Computer Security X86 Review Process Layout, ISA, etc. Drew Davidson
Information Security - 2. A Stack Frame. Pushed to stack on function CALL The return address is copied to the CPU Instruction Pointer when the function.
Foundations of Network and Computer Security J J ohn Black CSCI 6268/TLEN 5550, Spring 2013.
Security Attacks Tanenbaum & Bo, Modern Operating Systems:4th ed., (c) 2013 Prentice-Hall, Inc. All rights reserved.
EXPLOITATION CRASH COURSE – FALL 2013 UTD Computer Security Group – Andrew Folloder csg.utdallas.edu (credit: Scott Hand)
ROP Exploit. ROP Return Oriented Programming (ROP): is a hacking exploit technique where you exploit buffer overflow to inject a chain of gadgets. Each.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2014.
Basic of Buffer Over Flow
1 Introduction to Information Security , Spring 2016 Lecture 2: Control Hijacking (2/2) Avishai Wool.
Mitigation against Buffer Overflow Attacks
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
CS 177 Computer Security Lecture 9
The Hardware/Software Interface CSE351 Winter 2013
Introduction to Information Security
CSC 495/583 Topics of Software Security Stack Overflows (2)
CSC 495/583 Topics of Software Security Return-oriented programming
CMSC 414 Computer and Network Security Lecture 21
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Advanced Buffer Overflow: Pointer subterfuge
Foundations of Network and Computer Security
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
CNT4704: Analysis of Computer Communication Network Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Fall 2011.
System Calls David Ferry CSCI 3500 – Operating Systems
Week 2: Buffer Overflow Part 1.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
Understanding and Preventing Buffer Overflow Attacks in Unix
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
System and Cyber Security
Computer Architecture and System Programming Laboratory
Return-to-libc Attacks
Presentation transcript:

Intro to Exploitation Stack Overflows James McFadyen UTD Computer Security Group 10/20/2011

Intro to Exploitation Only an intro to stack overflow Basic theory and application One of many types of exploitation

Outline What is a buffer overflow? Tools Vulnerable C Functions Remember the memory Learn to love assembly Stack overflow Protection Mechanisms ret2libc in Linux

Buffer Overflow “In computer security and programming, a buffer overflow, or buffer overrun, is an anomaly where a program, while writing data to a buffer, overruns the buffer's boundary and overwrites adjacent memory. This is a special case of violation of memory safety.” - Wikipedia

Buffer Overflow In our examples.. Give the program too much input, hijack the instruction pointer (EIP) Control EIP Execute arbitrary code locally or remotely Achieve what we want as elevated user

Tools Linux  GDB, gcc, vi, perl/python/ruby, readelf, objdump, ltrace, strace, ropeme Windows  WinDBG, OllyDBG, ImmunityDBG, IDA, Python, Mona (ImmunityDBG plugin)

Vulnerable C Code strcpy(), strncpy() strcat(), strncat() sprintf(), snprintf() gets() sscanf() Many others...

Vulnerable C Code strcpy() doesn't check size If we have char buf[128]; strcpy(buf, userSuppliedString); This makes it too easy...

Vulnerable C Code char *strncpy(char *dest, const char *src, size_t n); We have a size, but what if.. strncpy(somebuffer, str, strlen(str)); or.. strncpy(somebuffer, str, sizeof(somebuffer)); Where str is supplied by user

Vulnerable C Code Common bug, proper fix: strncpy(somebuffer, str, sizeof(somebuffer)-1);

Vulnerable C Code char *strncat(char *dest, const char *src, size_t n); Ex: int vulnerable(char *str1, char *str2) { char buf[256]; strncpy(buf, str1, 100); strncat(buf, str2, sizeof(buf)-1); return; }

Vulnerable C Code Fix: strncat(buf, str2, sizeof(buf) - strlen(buf) -1);

Remember the Memory Text Data BSS Heap Stack Initialized global and static variables Uninitialized global and static variables Program scratch space. Local variables, pass arguments, etc.. Code segment, machine instr. Dynamic space. malloc(...) / free(...) new(...) / ~ Low High * Taken from Mitchell Adair's “Stack Overflows”

Remember the Memory: The Stack ESP EBP RET arguments... previous stack frame local variables... High Low EBP EBP - x EBP + x * Taken from Mitchell Adair's “Stack Overflows”

Love the Assembly EIP – Extended Instruction Pointer ESP – Extended Stack Pointer EBP – Extended Base Pointer EAX ECX EDX ESI EDI Next Instruction executed Data register Source index Destination Index Counter register Accumulator register EBX Base register Top of stack Base Pointer * Taken from Mitchell Adair's “Stack Overflows”

Stack Overflow ESP EBP RET argc *argv[] EBP char buf[100] 100 bytes 4 bytes * Taken from Mitchell Adair's “Stack Overflows”

Stack Overflow ESP EBP RET argc *argv[] EBP 100 bytes 4 bytes RET overwritten RET 108 bytes ( 0x41 * 108) Ret will pop the instruction pointer off of the stack EIP will now point to 0x Ex: $./program $(python -c 'print "A" * 108 ') * Taken from Mitchell Adair's “Stack Overflows”

Stack Overflow ESP EBP RET argc *argv[] EBP 100 bytes 4 bytes 0xdeadbeef RET 104 bytes ( 0x41 * 104 EIP will now point to 0xdeadbeef We can now point EIP where we want Ex: $./program $(python -c 'print "A" * “\xef\xbe\xad\xde” ') * Taken from Mitchell Adair's “Stack Overflows”

Stack Overflow $./program $(python -c 'print "A" * “\xef\xbe\xad\xde” ') We have 104 bytes for a payload  Payload can be anything, but for our purpose we would spawn a shell  The payload will be fixed size, so when we insert it, we must reduce the # of A's by the size of the payload

Stack Overflow $./program $(python -c 'print "A" * “\xef\xbe\xad\xde” ') If we had a 32 byte payload.. (real payload will not be a bunch of \xff) $./program $(python -c 'print "A" * 72 + “\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff” + “\xef\xbe\xad\xde” ') We have adjusted the buffer so the payload will fit We will then have to point EIP (\xef\xbe\xad\xde) to our payload on the stack

Stack Overflow $./program $(python -c 'print "A" * 72 + “\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\ xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff” + “\xef\xbe\xad\xde” ') “\xef\xbe\xad\xde” would be replaced with the address of our payload EIP will now point to the address of our payload, which will spawn a shell NOPs help create a bigger “landing area” This technique is not very effective anymore... why?

Protection Mechanisms (Windows) DEP – Data execution Prevention  Can't execute on the stack /GS Flag – cookie / canary  detects if stack has been altered SafeSEH – Structured Exception Handler  Try / except, catches exceptions ASLR - Address Space Layout Randomization  Randomizes addresses in memory

Protection Mechanisms (Linux) NX – Stack Execute Invalidation  Processor feature  Like DEP, can't execute on the stack Stack Smashing Protection – cookie / canary  Generally enabled by default ASLR - Address Space Layout Randomization Many other compiler protections...

ret2libc Bypasses NX Point EIP to a function in libc  system(), exec() etc... system(“/bin/sh”);  We will get a shell by using the system() function in libc

ret2libc $./program $(python -c 'print "A" * “\xef\xbe\xad\xde” ') We don't need the payload where the A's are anymore We now will point EIP to the address of system(), then the next 4 bytes will be a return address, followed by system() arguments (which will be /bin/sh) $./program $(python -c 'print "A" * address_of_system + return_address + payload ')

Demo! How to use GDB for exploitation  Exploring the stack  Finding important memory addresses (ret2libc)  Breakpoints  Using Perl/Python/Ruby for arguments in GDB Basic Stack Overflow Ret2libc

Additional Resources works works

Sources “Source Code Auditing” - Jared Demott “Smashing the stack in 2010” - Andrea Cugliari + Mariano Graziano “Stack Overflows” - Mitchell Adair libc_attack libc_attack