Return Oriented Programming

Slides:



Advertisements
Similar presentations
ROP is Still Dangerous: Breaking Modern Defenses Nicholas Carlini et. al University of California, Berkeley USENIX Security 2014 Presenter: Yue Li Part.
Advertisements

Recitation 4 Outline Buffer overflow –Practical skills for Lab 3 Code optimization –Strength reduction –Common sub-expression –Loop unrolling Reminders.
Smashing the Stack for Fun and Profit
David Brumley Carnegie Mellon University Credit: Some slides from Ed Schwartz.
CS457 – Introduction to Information Systems Security Software 3 Elias Athanasopoulos
Introduction to Information Security ROP – Recitation 5 nirkrako at post.tau.ac.il itamarg at post.tau.ac.il.
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.
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
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
Writing Metasploit Plugins from vulnerability to exploit Saumil Shah ceo, net-square hack.lu - Luxembourg 2006.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2011.
Assembly, Stacks, and Registers Kevin C. Su 9/26/2011.
Exploiting Buffer Overflows on AIX/PowerPC HP-UX/PA-RISC Solaris/SPARC.
Mitigation of Buffer Overflow Attacks
CIS 450 – Network Security Chapter 7 – Buffer Overflow Attacks.
CNIT 127: Exploit Development Ch 4: Introduction to Format String Bugs.
Buffer Overflow CS461/ECE422 Spring Reading Material Based on Chapter 11 of the text.
Introduction to InfoSec – Recitation 2 Nir Krakowski (nirkrako at post.tau.ac.il) Itamar Gilad (itamargi at post.tau.ac.il)
Buffer Overflow Attack-proofing by Transforming Code Binary Gopal Gupta Parag Doshi, R. Reghuramalingam The University of Texas at Dallas 11/15/2004.
Exploitation possibilities of memory related vulnerabilities
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.
Stack-based buffer overflows Yves Younan DistriNet, Department of Computer Science Katholieke Universiteit Leuven Belgium
What is exactly Exploit writing?  Writing a piece of code which is capable of exploit the vulnerability in the target software.
Part I The Basic Idea software sequence of instructions in memory logically divided in functions that call each other – function ‘IE’ calls function.
Introduction to Information Security ROP – Recitation 5.
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!!
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.
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.
Software Security. Bugs Most software has bugs Some bugs cause security vulnerabilities Incorrect processing of security related data Incorrect processing.
1 Introduction to Information Security , Spring 2016 Lecture 2: Control Hijacking (2/2) Avishai Wool.
Gnu Debugger (GDB) Topics Overview Quick Reference Card Readings: Quick Reference Card February 4, 2010 CSCE 212Honors Computer Organization.
Exploiting & Defense Day 1 Recap
Introduction to Information Security
Shellcode COSC 480 Presentation Alison Buben.
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.
Buffer Overflow Walk-Through
Introduction to Information Security
The Hardware/Software Interface CSE351 Winter 2013
CSCE 212Honors Computer Organization
Introduction to Information Security
Exploiting & Defense Day 2 Recap
CSC 495/583 Topics of Software Security Return-oriented programming
Recitation: Attack Lab
Buffer Overflow Walk-Through
CMSC 414 Computer and Network Security Lecture 21
Summary by - Bo Zhang and Shuang Guo [Date: 03/31/2014]
CS 465 Buffer Overflow Slides by Kent Seamons and Tim van der Horst
Advanced Buffer Overflow: Pointer subterfuge
Assembly Language Programming II: C Compiler Calling Sequences
Format String.
Machine Level Representation of Programs (IV)
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.
Week 2: Buffer Overflow Part 2.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2016.
CSCE 212Honors Computer Organization
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2013.
CAP6135: Malware and Software Vulnerability Analysis Buffer Overflow : Example of Using GDB to Check Stack Memory Cliff Zou Spring 2010.
Computer Architecture and System Programming Laboratory
Return-to-libc Attacks
Presentation transcript:

Return Oriented Programming Courtesy of Saumil Shah

Functions Revisited • How is a function called? •  What does the stack frame look like?

Calling a function frame look like? • Add two ints x, y • add(3,4) void add(int x, int y) { int sum; sum = x + y; printf("%d\n", sum); } int main() add(3, 4); •  What does the calling frame look like?

Stack frame for add(3,4) frame for add() return address from add() call add 3 4

Return from add(3,4) • add() is about to return. •  RET after epilogue of add(). •  Where does ESP point to? –  immediately before the RET •  What does the stack look like?

Before the RET ESP return address from add() 3 4

After the RET return address from add() ESP 3 4

Another function • Stack overflow in func1. •  Can we call add(5, 6), instead of running a shellcode? function void add(int x, int y) { int sum; sum = x + y; printf("%d\n", sum); } void func1(char *s) char buffer[128]; strcpy(buffer, s); int main() func1(argv[1]);

Stack frame for func1() buffer return address from func1 s

strcpy(buffer, s) uffer AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA return address from func1 AAAA s AAAA AAAA AAAA

Before the RET uffer AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA ESP return address from func1 AAAA s AAAA AAAA AAAA

After the RET uffer AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAA return address from func1 EIP = 0x41414141 ESP AAAA s AAAA AAAA

Return to add() • Insert a fake frame in the buffer. •  Make func1() return to: add(01010101, 02020202)

return address from add() strcpy(buffer, s) uffer AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA return address from func1 address of add() return address from add() s 01010101 02020202

return address from add() Before func1() returns uffer AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA ESP address of add() return address from func1 return address from add() s 01010101 02020202

return address from add() Before add() returns uffer AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAA address of add() return address from func1 ESP return address from add() s 01010101 02020202

Return to add() • By carefully creating a frame... •  ...we can make the program "return to our •  We •  We our function". control the parameters. also control where to jump to after function returns.

Stack frame before add(3, 4) • Dump the stack: (gdb) x/10x $esp 0xbffff41c: 0x08048471 0x00000003 0x00000004 0x0804851b 0xbffff42c: 0x00292ff4 0x08048510 0x00000000 0xbffff4b8 return address from add() 0x08048471 param1 3 4 param2

Overflowing func1() • Overflow func1 and... ...return to add(01010101, •  Create a fake frame. •  Overwrite stack memory. 02020202) return from func1 return from add param1 param2 0x080484bd 0x42424242 0x01010101 0x02020202

ESP • Where will ESP be after add? returning from • Verify AAAAAA...140...AAAAAA 080484bd 42424242 01010101 02020202 ESP •  Verify (gdb) x/64x $esp 0xbffff2e4: 0x01010101 0x02020202 0x00292f00 0x08048510 0xbffff2f4: 0x00000000 0xbffff378 0x00153bd6 0x00000002

Chaining functions • After add(01010101, 02020202), we want to run add(03030303, 04040404). •  How should we set up the frames? •  First, study the frame after add() returns.

Where does the new frame go? address of add ?? 03030303 AAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAA address of add 42424242 address of add 01010101 ?? 02020202 03030303 04040404

Where does the new frame go? • We get only ONE chance at strcpy. •  How do we preserve params 01010101 and 02020202? •  We can only APPEND the second frame below our first frame. •  We have to unwind the first frame before returning to the second frame. •  Answer: Return to ”pop pop ret”!

Chaining the frames address of add address of POP/POP/RET 01010101 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA address of add add(01010101, 02020202) address of POP/POP/RET 01010101 02020202 address of add add(03030303, 04040404) 42424242 03030303 04040404

Keeping ESP in control ESP address of add address of POP/POP/RET AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Return from func1 ESP address of add address of POP/POP/RET 01010101 02020202 address of add 42424242 03030303 04040404

Keeping ESP in control address of add ESP address of POP/POP/RET AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Return from func1 address of add Return to add() ESP address of POP/POP/RET 01010101 02020202 address of add 42424242 03030303 04040404

Keeping ESP in control address of add address of POP/POP/RET ESP AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Return from func1 address of add Return to add() address of POP/POP/RET Return to POP/POP/RET ESP 01010101 02020202 address of add 42424242 03030303 04040404

Keeping ESP in control address of add address of POP/POP/RET 01010101 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Return from func1 address of add Return to add() address of POP/POP/RET Return to POP/POP/RET 01010101 POP 02020202 POP ESP address of add 42424242 03030303 04040404

Keeping ESP in control address of add address of POP/POP/RET 01010101 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Return from func1 address of add Return to add() address of POP/POP/RET Return to POP/POP/RET 01010101 POP 02020202 POP address of add RET - Return to add() ESP 42424242 03030303 04040404

Keeping ESP in control address of add address of POP/POP/RET 01010101 AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA Return from func1 address of add Return to add() address of POP/POP/RET Return to POP/POP/RET 01010101 POP 02020202 POP address of add RET - Return to add() 42424242 Finally EIP = 0x42424242 ESP 03030303 04040404

Chained Frames • Create another buffer overflow payload: AAAAAA...140...AAAAAA 080484bd 08048422 01010101 02020202 distance to EIP address of add POP/POP /RET param1 param2 080484bd 42424242 03030303 04040404 address of add return from add param1 param2

It's all about ESP! • ESP is the new EIP. •  ROP involves keeping the ESP moving through the frames on the stack. •  Frames can be chained by returning to proper sequences of instructions that appropriately unwind the parameters from the stack.

Code Execution – The ROP Way • Piece together snippets of code •  Gadgets – primitive operations, to be searched for within the process binary shared libraries. or •  Every gadget must end with a RET.

A RET E RET RET Execute A, take next card Execute D, take next card binobj1 f2() f1() f3() A RET RET D Execute A, take next card binobj2 () C RET Execute D, take next card Execute C, take next card f5 f4() Execute B, take next card RET binobj3 f7 f6() Execute D, take next card () f8() B E RET RET RET Execute E, take next card

Thinking in ROP terms EAX = 0x14 ECX = [77ffe0300] [08041000] = 0x4000 POP EAX; RET EAX = 0x14 14 Load immediate POP EAX; RET ECX = [77ffe0300] 77ffe0300 MOV ECX, [EAX]; RET Read memory POP EAX; RET [08041000] = 0x4000 08041000 Write memory POP ECX; RET 4000 MOV [EAX], ECX; RET

Thinking in ROP terms func1(10, 20) func1 POP/POP/RET 10 20 Call function 10 20

CALL DWORD PTR [EAX]; RET Thinking in ROP terms POP EAX; RET fptr (*fptr)(3, 4) CALL DWORD PTR [EAX]; RET 10 Call function pointer 20 POP EAX; RET fptr MOV EAX, [EAX]; RET CALL EAX; RET 10 20

Gadgets ROP primitives. Each gadget corresponds to a “frame” that must be set up on the stack Must end with a RET!

Generic Techniques Run arbitrary shellcode. Difficult to transform entire shellcode to ROP frames. Common trick: Use ROP to change protection of existing memory to RWX Works when shellcode is at a predictable location.

Case Study JNLP overflow on IE8, Windows 7 Java Network Launch Protocol enables applications to be launched on a client desktop that using resources which are hosted on a remote server. JNLP plug-in for Internet Explorer has a buffer overflow vulnerability Extra long docbase parameter passed to JNLP.

What is a browser? More Attack Surface!

Browser Architecture Mime types ActiveX toolbars libraries Flash <div> <img> <object> <embed> <iframe> <body> <form> <input> <table> <style> <script> HTML+CSS JavaScript DOM ActiveX Mime types toolbars Flash libraries

Browser’s “syscalls” Mime types ActiveX QuickTime libraries Flash <object CLSID>=XXX-XXXX-XXX DOM ActiveX Mime types QuickTime Flash libraries HTML+CSS JavaScript

Browser’s “syscalls” Mime types ActiveX QuickTime libraries Flash <object CLSID>=XXX-XXXX-XXX DOM ActiveX Mime types QuickTime Flash libraries HTML+CSS JavaScript

Browser’s “syscalls” Mime types ActiveX QuickTime libraries Flash <object CLSID>=XXX-XXXX-XXX DOM ActiveX Mime types QuickTime Flash libraries HTML+CSS JavaScript

Where are we? We have a stack overflow. We control EIP and We control the memory at [ESP]. Shellcode…where does it go? …and how do we jump to it?

Browsers vs. other software Shellcode pre-loaded via JavaScript Shellcode part of the payload buffer For(i=0; i < 1000; i++) {buf +=“A”} “AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA…” Unicode – Null byte (any byte) friendly Avoid Null bytes and certain characters Easy to obfuscate – use JavaScript Difficult to obfuscate – shellcode encoders

Challenges ROP frames can get very large. 10-20 times the size of classic shellcode. May not have enough space on the stack. ROP frames may contain NULL bytes which the target program won’t like.

Heap Spraying Shellcode NOP sled Shellcode NOP sled Shellcode NOP sled <script> : nops = build_large_nosled(); a = new Array(); for(i = 0; i < 50; i++) a[i] = shellcode + nops; </script> <html> exploit trigger condition goes there </html> Shellcode NOP sled Shellcode NOP sled Shellcode NOP sled

Browser Exploit AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA address Address = ESP not EIP AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA address AAAAAAAA

ROP Frames Converting arbitrary shellcode to ROP is very difficult. Non-scalable. We require a generic approach. A “ROP Stage”: Load classic shellcode onto heap Invoke VirtualProtect Set memory protection to RWX Return to shellcode.