Return-to-libc Attacks

Slides:



Advertisements
Similar presentations
Smashing the Stack for Fun and Profit
Advertisements

David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
Introduction to Information Security ROP – Recitation 5 nirkrako at post.tau.ac.il itamarg at post.tau.ac.il.
Computer Security: Principles and Practice EECS710: Information Security Professor Hossein Saiedian Fall 2014 Chapter 10: Buffer Overflow.
Computer Security: Principles and Practice First Edition by William Stallings and Lawrie Brown Lecture slides by Lawrie Brown Chapter 11 – Buffer Overflow.
Lecture 16 Buffer Overflow modified from slides of Lawrie Brown.
CMSC 414 Computer and Network Security Lecture 22 Jonathan Katz.
Review: Software Security David Brumley Carnegie Mellon University.
1 CHAPTER 8 BUFFER OVERFLOW. 2 Introduction One of the more advanced attack techniques is the buffer overflow attack Buffer Overflows occurs when software.
1 Homework Reading –PAL, pp , Machine Projects –Finish mp2warmup Questions? –Start mp2 as soon as possible Labs –Continue labs with your.
Assembly תרגול 8 פונקציות והתקפת buffer.. Procedures (Functions) A procedure call involves passing both data and control from one part of the code to.
Andrea Bittau, Adam Belay, Ali Mashtizadeh, David Maziéres, Dan Boneh
September 22, 2014 Pengju (Jimmy) Jin Section E
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.
Mitigation of Buffer Overflow Attacks
Buffer Overflow CS461/ECE422 Spring Reading Material Based on Chapter 11 of the text.
Buffer Overflow. Introduction On many C implementations, it is possible to corrupt the execution stack by writing past the end of an array. Known as smash.
Buffer Overflow Attack Proofing of Code Binary Gopal Gupta, Parag Doshi, R. Reghuramalingam, Doug Harris The University of Texas at Dallas.
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Introduction to Information Security ROP – Recitation 5.
Reminder Bomb lab is due tomorrow! Attack lab is released tomorrow!!
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.
VM: Chapter 7 Buffer Overflows. csci5233 computer security & integrity (VM: Ch. 7) 2 Outline Impact of buffer overflows What is a buffer overflow? Types.
Beyond Stack Smashing: Recent Advances In Exploiting Buffer Overruns Jonathan Pincus and Brandon Baker Microsoft Researchers IEEE Security and.
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.
Introduction to Information Security
Let’s look at an example
Shellcode COSC 480 Presentation Alison Buben.
Buffer Overflows ...or How I Learned to Never Trust the User
Mitigation against Buffer Overflow Attacks
Instructions for test_function
Chapter 14 Functions.
Recitation 5: Attack Lab
Storage Classes There are three places in memory where data may be placed: In Data section declared with .data in assembly language in C - Static) On the.
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Protecting Memory What is there to protect in memory?
Recitation: Attack Lab
Introduction to Information Security
143A: Principles of Operating Systems Lecture 4: Calling conventions
CSC 495/583 Topics of Software Security Stack Overflows (2)
Introduction to Compilers Tim Teitelbaum
CSC 495/583 Topics of Software Security Return-oriented programming
Chapter 14 Functions.
Recitation: Attack Lab
CMSC 414 Computer and Network Security Lecture 21
Buffer Overflow.
SEED Workshop Buffer Overflow Lab
Advanced Buffer Overflow: Pointer subterfuge
Recitation: Attack Lab
Software Security Lesson Introduction
Format String.
Understanding Program Address Space
Return-to-libc Lab Zutao Zhu 09/25/2009.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2015.
Week 2: Buffer Overflow Part 1.
Week 2: Buffer Overflow Part 2.
Reverse Engineering for CTFs
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.
FIGURE Illustration of Stack Buffer Overflow
Chapter 14 Functions.
Format String Vulnerability
Race Condition Vulnerability
Chapter 14 Functions.
Implementing Functions: Overview
Presentation transcript:

Return-to-libc Attacks

Non-executable Stack countermeasure How to defeat the countermeasure Outline Non-executable Stack countermeasure How to defeat the countermeasure Tasks involved in the attack Function Prologue and Epilogue Launching attack

Non-executable Stack Running shellcode in C program Calls shellcode Consider the above code that places a shellcode in a buffer on the stack, casts the buffer as a function and calls the function.

Non-executable Stack With executable stack With non-executable stack title

How to Defeat This Countermeasure Jump to existing code: e.g. libc library. Function: system(cmd): cmd argument is a command which gets executed.

Environment Setup This code has potential buffer overflow problem in vul_func() Buffer overflow problem

Environment Setup “Non executable stack” countermeasure is switched on, StackGuard protection is switched off and address randomization is turned off. Root owned Set-UID program.

Overview of the Attack Task A : Find address of system(). To overwrite return address with system()’s address. Task B : Find address of the “/bin/sh” string. To run command “/bin/sh” from system() Task C : Construct arguments for system() To find location in the stack to place “/bin/sh” address (argument for system())

Task A : To Find system()’s Address. Debug the vulnerable program using gdb Using p (print) command, print address of system() and exit(). Libc library is loaded in the memory when a program runs. For the same program, the library is loaded in the same location. Hence, we can use gdb to find the location of the library functions.

Task B : To Find “/bin/sh” String Address Export an environment variable called “MYSHELL” with value “/bin/sh”. MYSHELL is passed to the vulnerable program as an environment variable, which is stored on the stack. We can find its address. The exported environment variables in the shell process is always passed to the child process.Hence, when a new environment variable is exported and vulnerable program is run, the environment variable is passed to the vulenarbale code and hence, in it’s memory.

Task B : To Find “/bin/sh” String Address Export “MYSHELL” environment variable and execute the code. Code to display address of environment variable

Task B : Some Considerations Address of “MYSHELL” environment variable is sensitive to the length of the program name. If the program name is changed from env55 to env77, we get a different address. Environment variables are stored in the stack region of the process, but before the environment variables are pushed, the name of the program is pushed first which affects the memory locations of the environment variables. It took us quite a while to figure out why simply changing the file name can make the attack fail.

Task C : Argument for system() Arguments are accessed with respect to ebp. Argument for system() needs to be on the stack. Need to know where exactly ebp is after we have “returned” to system(), so we can put the argument at ebp + 8. Frame for the system() function

Task C : Argument for system() Function Prologue esp : Stack pointer ebp : Frame Pointer The start of the function call contains 3 instructions as mentioned. When a function is called, return address (RA) is pushed into the stack. This is the beginning of the function before function prologue gets executed. The stack pointer (esp register) points at RA location. The previous frame pointer is pushed in the stack, so when the function returns, the caller’s frame pointer is recovered. The stack pointer now points to the previous frame pointer. The frame pointer (ebp) is pointed to the current stack pointer now so that the frame pointer always points to the old frame pointer. The stack pointer now moves by N bytes to leave space for the local variables of the function.

Task C : Argument for system() Function Epilogue esp : Stack pointer ebp : Frame Pointer The stack pointer now points where the frame pointer points to in order to release the stack space allocated for the local variables. The previous frame pointer is assigned to %ebp to recover the frame pointer of the caller’s function. The return address is popped from the stack and the program jumps to that address. This instruction moves the stack pointer.

Function Prologue and Epilogue example 1 2 Function prologue Function epilogue 1 The code can be compiled into assembly code using gcc -s. 2 8(%ebp) ⇒ %ebp + 8

How to Find system()’s Argument Address? Change ebp and esp Modified Return Address vul_func() epilogue system() prologue Use of system()’s argument In order to find the system() argument, we need to understand how the ebp and esp registers change with the function calls. Between the time when return address is modified and system argument is used, vul_func() returns and system() prologue begins.

Memory Map to Understand system() Argument

Flow Chart to understand system() argument Return address is changed to system() address. ebp is replaced by esp after vul_func() epilogue Jump to system() “/bin/sh” is stored in ebp+8 ebp is set to current value of esp system() prologue is executed Check the memory map ebp + 4 is treated as return address of system(). We can put exit() address so that on system() return exit() is called and the program doesn’t crash.

Malicious Code ebp + 12 ebp + 8 ebp + 4 We are inside the vulnerable function (before return occurs), so ebp currently points to the stack frame of the vulnerable function.

Launch the attack Execute the exploit code and then the vulnerable code

Return-Oriented Programming In the return-to-libc attack, we can only chain two functions together The technique can be generalized: Chain many functions together Chain blocks of code together The generalized technique is called Return-Oriented Programming (ROP)

Chaining Function Calls (without Arguments)

Chaining Function Calls with Arguments Idea: skipping function prologue

Chaining Function Calls with Arguments Idea: using leave and ret

Chaining Function Calls with Zero in the Argument Idea: using a function call to dynamically change argument to zero on the stack Sequence of function calls (T is the address of the zero): use 4 sprint() to change setuid()’s argument to zero, before the setuid function is invoked. Invoke setuid(0) before invoking system(“/bin/sh”) can defeat the privilege-dropping countermeasure implemented by shell programs.

Summary The Non-executable-stack mechanism can be bypassed To conduct the attack, we need to understand low-level details about function invocation The technique can be further generalized to Return Oriented Programming (ROP)