SEH primer for CTP

4 minute read

UPDATE: CTP is cancelled <:( see more here


A primer on Structured Exception Handling (win32 exploitation)

This article is written to help those who want a conceptual grasp on SEH exploits, I wrote this to help myself obtain a clear understanding before my CTP course starts.

There are already many fantastic articles on this subject out there (some of which are linked throughout this article), which I encourage you to consume in order to get an even better understanding.

What is an exception Handler?

In simple terms, an Exception Handler is exactly as the name implies, it handles exceptions, that is to say, when a program reaches something outside it’s known functionality, an exception is raised (eg if a program wants an input between 2 and 5, and you put in 6).

The program then needs to know how to handle this, either with an error message, exit code, or whatever else.

For every exception handler, there is an Exception Registration Record structure. These structures are chained together to create a “linked list” (a linked list contains a sequence of data records).

When an exception is triggered the OS scans down the linked list to evaluate the other exception functions until it finds a suitable exception handler for the current exception. (If no error is found, windows places a generic handler at the end to ensure it wil be handled in some manner).

We can overwrite the SEH address with an overflow, and thus control it.

When an exception occurs and the Exception Handler (SEH) is called, it’s value is put in EIP. Since we have control over SEH, we now have control over EIP and the execution flow of the application.

As an exception occurs in a program, and is subsequently handled, the system pushes what is called an EXCEPTION_DISPOSITION Handler onto the stack. Within this structure is an “Establisher Frame” which points to the first handler record record (this handler record just so happens to contain the address of the nSEH and SEH) and is located at ESP+8.



Well, when we send an attack string which happens to overwrite the SEH, windows will zero out the CPU registers, thus breaking our jump to shellcode ;(

What we can do:

Overwrite SEH with a pointer to POP POP RETN.

Why would we want to do that?

Well, after passing the first exception, EIP is overwritten with the value of SEH. Second, the registers to which the bytes are popped into are not important, what is important is that when a POP occurs, ESP is shifted +4. When a RET is called, the ESP address are moved into EIP and executed.

If you recall The information regarding the establisher frame, the address of nSEH is at ESP+8, so by calling POP twice we end up at our nSEH address.

EG a program crashes, registers get zeroed and SP +8 is overwritten with crash string 41414141.


To demonstrate this, we use example exercise from (vulnerable software link is broken, software available on exploit-DB here).

Another invaluable resource for this subject is:

As the steps to exploitation have already been covered, I will not run through them again here, only provide a brief summary.

First, we crash the application with a pattern attack string to locate the offset to SEH, which mona will do the heavy lifting for.

As below we see SEH overwritten and offset located:


Calculate offsets with !mona findmsp


Now that we know the offset, we confirm control by crashing with another simple attack string as per the offset specified when we examined with !mona findmsp

buffer = "A"*608 + nSEH + SEH + "D"*1384

Proof of control:


Now we choose an address to point to in a module containing POP+POP+RET with !mona seh



  • I choose 0x6162e557
  • flip for little endian "\x57\xe5\x62\x61"
  • modify my malicious playlist generator (obtained from Fuzzy Security)
  • restart the program in immunity, set a break point by going to expression
  • Import playlist
  • Pass first exception (shift+F9)
  • Hit breakpoint
  • step through to call RET (F7)

Now as you can see below, EIP is now at the address of POP+POP+RET, step through until RET is called, and the next screenshot will show we have reached nSEH.



Now you may have guessed something based of the below sequence, that’s right! if we continue we end up in an infinite loop - this is because as we hit BBBB, ANOTHER exception is raised, thus calling POP+POP+RET again…

So now we need to replace nSEH with opcode that will JMP over the next few bytes of space. A safe amount would be 10, and pad the beginning of shellcode with NOPS.

The opcode for JMP is EB, so nSEH would be \xeb\x0a\x90\x90



Once this is done some shellcode is generated and placed AFTER SEH but BEFORE the rest of the attack string which should be big enough to overflow the application as below:

#!/usr/bin/python -w
nSEH="\xeb\x0A\x90\x90" # opcode to jump back over SEH to avoid loop and enter shellcode
SEH="\x57\xe5\x62\x61" # adress of non SEH protected module containing POP POP RET
buffer = "A"*608 + nSEH + SEH + shellcode+ "B"*(1384-len(shellcode))
textfile = open(filename , 'w')

Now we generate our malicious playlist and open it in again - this time immunity does not register a crash, and we are able to connect to the bind shell payload:


So the takeaways for exploiting SEH are:

  • As always calculate the overflow amount
  • calculate the offset to SEH
  • confirm control of nSEH and SEH
  • Use SEH to move up 8 to nSEH
  • Use nSEH to Jump back over SEH and into shellcode

This is my first article in a series to help myself/others prepare for CTP and continue quest for big brain.