Giter VIP home page Giter VIP logo

magic_gadget_finder's Introduction

Finding the magic gadget

The magic gadget

Basically the magic gadget is some code residing in the libc which is opening a shell when being executed. This can be used when trying to do a ret2libc in a ROP-style-exploit.

The magic gadget code has to either call execve or issue the corresponding syscall directly, while /bin/sh is set as first argument.

Overview (AMD64):

  • execve

    • rdi: Pointer to /bin/sh
    • rsi: Pointer to argv
    • rdx: Pointer to env
  • syscall

    • rax: Syscallnumber of execve: 59 oder 0x3b
    • rdi: Pointer to /bin/sh
    • rsi: Pointer to argv
    • rdx: Pointer to env

magic.py

Is a little python-script that uses radare2 and r2pipe-python, to find such magic gadgets automatically for a given libc.

  $ ./magic.py libc-2.19.so
  [x] Analyze all flags starting with sym. and entry0 (aa)
  [x] Analyze len bytes of instructions for references (aar)
  [x] Analyze function calls (aac)
  [*] Use -AA or aaaa to perform additional experimental analysis.
  [x] Constructing a function name for fcn.* and sym.func.* functions (aan)
  Searching 7 bytes from 0x00000270 to 0x003aaa20: 2f 62 69 6e 2f 73 68 
  Searching 7 bytes in [0x270-0x3aaa20]
  hits: 1
  ----------------------------------------------
  Found /bin/sh @ 0x001639a0
  ----------------------------------------------
  0x00041374   488b052d3b3600  mov rax, qword [rip + 0x363b2d]
  0x0004137b   488d3d1e261200  lea rdi, qword [rip + 0x12261e]
  0x00041382       488d742430  lea rsi, qword [rsp + 0x30]
  0x00041387 c7052f61360000000000  mov dword [rip + 0x36612f], 0
  0x00041391 c7052961360000000000  mov dword [rip + 0x366129], 0
  0x0004139b           488b10  mov rdx, qword [rax]
  0x0004139e       e86d8f0700  call sym.execve

  ----------------------------------------------
  Here's your magic gadget:
  Offset: 0x00000000041374

Unfortunately, at the moment magic.py only works with 64-bit libraries on the x86-64 architecture.

TODO

  • Cleanup the code
  • Add commandline option for search-depth
  • Extend for 32-Bit libraries
  • Test with more libcs

magic_gadget_finder's People

Contributors

m1ghtym0 avatar

Watchers

James Cloos avatar hotelzululima avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.