Buffer overflows and various code injection methods Raghunathan Srinivasan CSE 539, 2/2/2011.

Slides:



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

Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
Exploring Security Vulnerabilities by Exploiting Buffer Overflow using the MIPS ISA Andrew T. Phillips Jack S. E. Tan Department of Computer Science University.
CSc 352 Programming Hygiene Saumya Debray Dept. of Computer Science The University of Arizona, Tucson
ITEC 352 Lecture 27 Memory(4). Review Questions? Cache control –L1/L2  Main memory example –Formulas for hits.
CS 4284 Systems Capstone Godmar Back Linking and Loading.
Prof. Necula CS 164 Lecture 141 Run-time Environments Lecture 8.
Set 20 Interrupts. INTERRUPTS The Pentium has a mechanism whereby external devices can interrupt it. Devices such as the keyboard, the monitor, hard disks.
15-213/ Intro to Computer Systems by btan with reference to Spring 10’s slides.
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.
Malware resistance. Outline Preliminaries –Virtual Address Layout –Stack Layout –Verification Problem Remote Attestation –Methods –Code Injection Interrupts.
CS 536 Spring Run-time organization Lecture 19.
3/17/2008Prof. Hilfinger CS 164 Lecture 231 Run-time organization Lecture 23.
Overview C programming Environment C Global Variables C Local Variables Memory Map for a C Function C Activation Records Example Compilation.
Microprocessors Frame Pointers and the use of the –fomit-frame-pointer switch Feb 25th, 2002.
Run-time Environment and Program Organization
CSE 451 Section 4 Project 2 Design Considerations.
September 22, 2014 Pengju (Jimmy) Jin Section E
Address Obfuscation: An Efficient Approach to Combat a Broad Range of Memory Error Exploits Sandeep Bhatkar, Daniel C. DuVarney, and R. Sekar Stony Brook.
What are Exception and Interrupts? MIPS terminology Exception: any unexpected change in the internal control flow – Invoking an operating system service.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Assembly, Stacks, and Registers Kevin C. Su 9/26/2011.
Fall 2008CS 334: Computer SecuritySlide #1 Smashing The Stack A detailed look at buffer overflows as described in Smashing the Stack for Fun and Profit.
CS 3204 Operating Systems Godmar Back Lecture 11.
More Network Security Threats Worm = a stand-alone program that can replicate itself and spread Worms can also contain manipulation routines to perform.
University of Washington Today Memory layout Buffer overflow, worms, and viruses 1.
Brian E. Brzezicki. This tutorial just illustrates the underlying concepts of buffer overflows by way of an extremely simple stack overflow  Most buffer.
Recitation 4: The Stack & Lab3 Andrew Faulring Section A 30 September 2002.
Automatic Diagnosis and Response to Memory Corruption Vulnerabilities Presenter: Jianyong Dai Jun Xu, Peng Ning, Chongkyung Kil, Yan Zhai, Chris Bookhot.
University of Washington Today Happy Monday! HW2 due, how is Lab 3 going? Today we’ll go over:  Address space layout  Input buffers on the stack  Overflowing.
Exploitation Of Windows Buffer Overflows. What is a Buffer Overflow A buffer overflow is when memory is copied to a location that is outside of its allocated.
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,
Part II Let’s make it real Memory Layout of a Process.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Operating Systems ECE344 Ashvin Goel ECE University of Toronto Unix System Calls and Posix Threads.
JMU GenCyber Boot Camp Summer, Introduction to Penetration Testing Elevating privileges – Getting code run in a privileged context Exploiting misconfigurations.
1 Understanding Pointers Buffer Overflow. 2 Outline Understanding Pointers Buffer Overflow Suggested reading –Chap 3.10, 3.12.
CS 155 Section 1 PP1 Eu-Jin Goh. Setting up Environment Demo.
Buffer Overflow Attack- proofing of Code Binaries Ramya Reguramalingam Gopal Gupta Gopal Gupta Department of Computer Science University of Texas at Dallas.
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
University of Amsterdam Computer Systems – the instruction set architecture Arnoud Visser 1 Computer Systems The instruction set architecture.
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.
A process is a program in execution A running system consists of multiple processes – OS processes Processes started by the OS to do “system things” –
Buffer Overflow Buffer overflows are possible because C doesn’t check array boundaries Buffer overflows are dangerous because buffers for user input are.
Protecting Memory What is there to protect in memory?
The Hardware/Software Interface CSE351 Winter 2013
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Protection of System Resources
Jason Puncher Software Designer Apriil 20, 2016
Exploiting & Defense Day 2 Recap
Run-time organization
CS 5204 Operating Systems Linking and Loading Godmar Back.
Recitation: Attack Lab
C Prog. To Object Code text text binary binary Code in files p1.c p2.c
CS 4284 Systems Capstone Linking and Loading Godmar Back.
Assembly Language Programming II: C Compiler Calling Sequences
The Runtime Environment
The Runtime Environment
Machine Level Representation of Programs (IV)
System Calls David Ferry CSCI 3500 – Operating Systems
Buffer Overflows.
CS5123 Software Validation and Quality Assurance
Instructors: Majd Sakr and Khaled Harras
System and Cyber Security
FIGURE Illustration of Stack Buffer Overflow
Presentation transcript:

Buffer overflows and various code injection methods Raghunathan Srinivasan CSE 539, 2/2/2011

What is the deal with overflows Why does it exist? Can we get rid of it? Why cant we get rid of it?

Since 80 % of the general population uses Microsoft OS lets google Microsoft buffer overflow

Bounds checking? int main() { int a[4]; int n; scanf(“%d”, &n); while (n>0){ scanf(“%d”, &a[n]); n--; }

int main() { int a[4]; int n; scanf(“%d”, &n); if (n>3) n=3;// return while (n>0){ scanf(“%d”, &a[n]); n--; }

Why buffer overflow is possible Are our machines different? What does the stack look like?

Takeaways? How secure is any code? What would happen if we all used different architecture, custom compiled OS?

Benefits of custom compilation Randomize application memory Modify the relative distance between Return address and locals on stack for every binary –Attacker needs to determine correct input values on every binary –Return of investment is lower

Randomize the stack frame of every routine –Add padding between local variables and return address –Makes buffer overflow exploits difficult So how to randomize the code –Source code? –Executable?

Binary re writing No net instructions added (or subtracted) Change arguments for adding space on stack Every instruction that use locations on stack (local variables) has to be fixed

void foo() { char buffer[1024]; gets(buffer); } push %ebp mov %esp,%ebp sub $0x408,%esp lea -0x400(%ebp),%eax mov %eax,(%esp) call 80482c8 leave ret

So what instructions need to be modified? A) B) C)

Was this done Yes Use objdump to parse out the text Identify instructions Determine max pad for each function Go and re write instructions

Code injection Mprotect Ptrace Let take a look at the man page of these system calls

Lets write code #include #include #include #include #include /* for PAGESIZE */ #ifndef PAGESIZE #define PAGESIZE 4096 #endif int test(); int main() { int a; char *location = &test; char *d = &test; test(); printf("\nAttempting not possible stuff"); fflush(NULL); d = (char *)(((int) d) & ~(PAGESIZE-1)); if (mprotect(d, 1024, PROT_WRITE|PROT_EXEC)) { perror("Couldn't mprotect"); exit(errno); } location [1] = 0xc3; test(); printf("\nShould not be here"); fflush(NULL); return 0; } int test() { int i; printf("\n hello from test"); return 0; }

What does this show If an application wants to, it can cause havoc on itself. Is this useful? But this is a system call All system calls are available to every binary Can you make the execution jump to mprotect with correct stack arguments?

ptrace Parent process may observe and control a child process Essentially debugger

fork Creates a child process Execution returns back twice at the same location If return value is 0, it’s a child, else parent Code example 1

Example 2 PTRACE_TRACEME –Process allows parent to trace it. When child executes a system call (any signal), the control causes it to wait and sends control to parent which is waiting. PTRACE_CONT –Parent resumes the stopped child

Example 3 Reads a word at offset addr in the child's USER area, which holds the registers and other information about the process

Example 6 PTRACE_ATTACH –Attaches to the process specified in pid, making it a traced "child" of the current process; the behavior of the child is as if it had done a PTRACE_TRACEME. PTRACE_GETREGS –Copies the child's general purpose or floating-point registers, respectively, to location data in the parent. PTRACE_PEEKTEXT –Reads a word at the location addr in the child's memory, returning the word as the result of the ptrace() call.

Example 7 PTRACE_SETREGS –Copies the child's general purpose or floating- point registers, respectively, from location data in the parent.

Example 8 Do it yourself at home