1 / 21

Introduction to Information Security

Introduction to Information Security. ROP – Recitation 5 nirkrako at post.tau.ac.il itamarg at post.tau.ac.il. Return Oriented Programming. Return oriented programming is a different way to control the flow of EIP in a program Motivation:

lily
Télécharger la présentation

Introduction to Information Security

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Information Security ROP – Recitation 5 nirkrako at post.tau.ac.il itamarg at post.tau.ac.il

  2. Return Oriented Programming • Return oriented programming is a different way to control the flow of EIP in a program • Motivation: • Write or Execute: as a result of overflows, the first prevention technique is to make: • Executable memory segments read-o • nly • Writeable memory segments Non-Executable. • Most slides in this presentation were taken as is from: • Return-oriented Programming: Exploitation without Code Injection • By Erik Buchanan, Ryan Roemer, Stefan Savage, HovavShachamfrom the University of California, San Diego • http://cseweb.ucsd.edu/~hovav/dist/blackhat08.pdf

  3. Getting started • Need control of memory around %esp • Rewrite stack: • Buffer overflow on stack • Format string vuln to rewrite stack contents • Move stack: • Overwrite saved frame pointer on stack; on leave/ret, move %esp to area under attacker control • Overflow function pointer to a register spring for %esp: • set or modify %esp from an attacker-controlled register • then return

  4. Schematic: return to libc • Control hijacking without executing code stack libc.so args ret-addr exec() sfp printf() local buf “/bin/sh”

  5. Return to libc • Stack progress trace

  6. Returning to Code Chunks (aka Gadgets) • Instead of working with small “opcodes” and %eip, we now use larger chunks of code and %esp • All the “larger chunks” do multiple register manipulations, and we must consider the effect of all of them. • Not everything we want is possible directly, so we have to be creative and work around the problem. • All chunks end with 0xc3 (RET) • We are effectively using a new ‘language’ to code.

  7. Chunk guidelines • All chunk ends with 0xc3 • Chunks should be as minimal as possible, containing minimum amount of data • Chunks are better if they appear in more “stable” and common libraries such as: libc. (and can then be reused for different binaries). • Chunks can not contain Junks. • If the CPU can not interpret the junk in the chunk, it will stop the program with illegal instruction exception.

  8. ROP – Machine level • Stack pointer (%esp) determines which instruction • sequence to fetch & execute • Processor doesn’t automatically increment %esp; — but • the “ret” at end of each instruction sequence does

  9. No-op equivalent • No-op instruction does nothing but advance %eip • Return-oriented equivalent: • point to return instruction • advances %esp • Useful in nop sled

  10. Loading Immediates • Instructions can encode constants • Return-oriented equivalent: • Store on the stack; • Pop into register to use

  11. Control flow • Ordinary programming: • (Conditionally) set %eip to new value • Return-oriented equivalent: • (Conditionally) set %esp to new value

  12. Multiple instruction sequence • Sometimes more than one instruction sequence needed • to encode logical unit • Example: load from memory into register: • Load address of source word into %eax • Load memory at (%eax) into %ebx

  13. Conditional Jump #0 • Negative causes the carry flag to be turn on. • Carry flag can be used in conjunction with ADC.

  14. Conditional Jump #1 • ADDR TO: XOR EAX,EAX ; RET • ADDR TO: POP ECX ; RET • DWORD 0 • ADDR TO: ADC CL, AL ; RET • ADDR TO: ROL ECX, 1; RET • ADDR TO: ROL ECX, 1; RET • ADDR TO: XCHG EAX, ECX ; RET • ADDR TO: ADD ESP, EAX ; RET. • ADDR TO: POP ESP ; RET # Go somewhere else. • ADDR TO: EXIT

  15. Gadget summary • We can write complex shellcode by returning to relevant gadgets. • All gadgets end with ret. (0xc3) • Gadgets can not contain junk (everything must be interpretable) • “JMP” is analogous to finding code that modifies the ESP. • We don’t have to maintain the original alignment of code (on x86). • Example: • MOV EAX, 0x5DC3 • This is interpreted into: B8 5D C3 • However, • POP EBP • RETN • This is interpreted into: • 5D C3 • Using rop_ptrace.py we debug the executable and are able to locate relevant gadgets • To have we everything flowing correctly and make sure we are aware of where ESP and EIP are pointing to at all times.

  16. Infosec ROP Tools • ./rop_ptrace.py • ./memmap.py • ./disas_at_va.py

  17. ./rop_ptrace.py • ./rop_ptrace.py • Usage: ./rop_ptrace.py [filename] [depth] “OPCODE” • rop_ptrace.py helps you by loading the binary into memory, therefore causing the creation of all linked shared objects (SO). The list of libraries can also be shown via shell command ‘ldd filename’. • After loading the binary and waiting for the SOs to load it will search for code chunks ending with ROP and then look back until [depth] bytes. • rop_ptrace.py then codes and disassembles the chunk • rop_ptrace.py uses distorm3 to disassemble and python-ptrace library to debug the executable.

  18. ./rop_ptrace.py cont. • [rop_ptrace.py example]

  19. ./memmap.py • Usage: ./memmap.py [filename] • Prints the libraries, memories mapped to, and their permissions. • Usage: ./memmap.py [filename] [string] • Attempts to locate the string within the mapped memory sections. • memmap.py uses python-ptrace

  20. ./memmap.py cont. • [memmap.py example]

  21. ./disas_at_va.py cont. • disas_at_va.py [filename] [va] [length] • Disassembles [length] number of bytes at virtual address [va] after loading the binary [filename] to memory. • Uses python-ptrace and distorm3.

More Related