🥹Buffer Overflow

Check Windows PE protections

Winchecksec

https://github.com/trailofbits/winchecksec

Compiling on Windows

git clone https://github.com/trailofbits/winchecksec.git
cd winchecksec
mkdir build
cd build
cmake ..
cmake --build . --config Release

Download last release

https://github.com/trailofbits/winchecksec/releases

Usage

.\Release\winchecksec.exe <PATH>.exe

Tools

Immunity Debugger

Mona

Mona installation


Buffer OverFlow

Launch Immunity Debugger, then “Open” or “Attach” the .exe file.

Mona configuration

All mona commands must be run in the terminal inside Immunity Debugger (in the red rectangle).

Mona commands

Set the current working directory :

Fuzzing

Use fuzzer.py or fuzzer2.py, until the application crash inside Immunity Debugger.

You just have to modify those two variables of the scripts above :

  • IP

  • PORT

When the application crashes, EIP should be equal to 41414141 (hex value of “AAAA”).


Crash replication & controlling EIP

Pattern

Generate a cyclic pattern to found the exact offset of the crash :

The size must be higher than the crash offset. Now modify the payload variable by the cyclic pattern :

Re-run the exploit, the application should crash. To find the exact offset of the crash use :

Size is the same as the one used to create the pattern. The result should be something like :

Get the offset, modify in exploit.py:

  • The offset variable by the offset

  • The retn variable by “BBBB”

  • Remove the payload variable

Re-run exploit.py, EIP should be equal to 42424242 (hex value of “BBBB”). You now control EIP !


Finding bad characters

Certain byte characters can cause issues in the development of exploits. We must run every byte through the program to see if any characters cause issues. By default, the null byte (\x00) is always considered a bad character as it will truncate shellcode when executed.

We will send bad characters recursively and analyze if they need to be removed. Let generate the list of bad characters with mona :

Copy the results in the variable payload. And re-run exploit.py, the application should crash. Now to found those bad characters use this command :

If BadChars are found, we need to exclude them as well.

Then compare again :

Repeat those two steps until the results status returns Unmodified, this indicates that no more bad characters exist.


Finding a jump point

JMP ESP - Inside the .exe

JMP ESP - inside a DLL

We need to found a .dll were Rebase, SafeSEH, ASLR, NXCompat are sets to False. When you found it, run the command below to search for a JMP ESP (FFE4), inside the dll :

Return address

Choose an address in the results and update exploit.py :

  • Setting the retn variable to the address, written backwards (little-endian)


Generate payload

Now we generate our shellcode without the badchars that we found :

Copy the generated shellcode and update exploit.py :

  • Setting the payload variable equal to the shellcode


Prepend NOPs

A NOP-sled is a technique for exploiting stack buffer overflows. It solves the problem of finding the exact address of the buffer by effectively increasing the size of the target area, \x90 represents a NOP in assembly. This instruction will literally do nothing and continue on with code execution.


Start a listener

Last updated