Reverse engineering ToC
ASM basic syntax
General Purpose Registers
Memory pointers
Control registers
$ gcc a.c -i a -m32 # compile for 32 bits
$ gcc a.c -i a -m64 # compile for 64 bits
$ gcc a.c    -fno-stack-protector # disables stack protection
$ objdump -d bin # get the ASM of a binary
$ gdb overflow core # examine core dump
> info registers # examine EIP, etc
  • Compile windows executable in linux:
  • $ apt-get install mingw32;
    i586-mingw32msvc-gcc slmail-win-fixed.c -lws2_32 -o s.exe
  • NOP is \0x90 useful for shellcodes to add the padding. Higher chance of guessing the right address
  • The stack grows down the address space: as more data is added to the stack, it is added at increasingly lower address value
  • 4 bytes per char, i.e. array[5] allocates 0x20 = 30 bytes
  • The order of the bytes is reversed because IA32 machines are little-endian, reverse the address to use in the payload
  • The address problem
    Shellcodes
    $ gcc -static 
  • Then, we can use $ strace ./bin to see the syscalls used by a binary
  • Test the shellcode with
  • char shellcode[] = "\x55"
        "\x48\x89\xe5"
        "\xbf\xe4\x65\x49\x00"
        "\xe8\xd2\x0e\x00\x00i"
        "\x5d"
        "\xc3";
        int main()
        {
            int  *ret;
            ret = (int *) &ret + 2;
            (*ret) = (int)shellcode;
        }
  • You can not put nulls \x00 in a shellcode
  • 0000000000401110 <main>:
        401110: 55                   push   %rbp
        401111: 48 89 e5             mov    %rsp,%rbp
        401114: bf e4 65 49 00       mov    $0x4965e4,%edi
        401119: e8 d2 0e 00 00       callq  401ff0 <__libc_system>
        40111e: 5d                   pop    %rbp
        40111f: c3                   retq
  • The middle column is the hex opcodex we want \x55 etc
  • Format strings
    Analyzing firmware images
    $ binwalk -e file.bin # extract compress bin images
    $ unyaffs file # extract .unyaffs2 filesystem mount points
    $ dd if=image.zip of=firmware.zip bs=64 skip=1 # extract firmware images
    $ fcrackzip file # cracks zip file