Exploit Development III
A simple SEH exploit
Hello again! In this quick tutorial I am going to take you through the development and exploitation of a SEH – Based exploit. Let’s begin with the basics and then walk through the development and exploitation of an application.
What is SEH?
An exception is an event that occurs during the execution of a program. It requires the execution of code outside the normal flow of control. The SEH blocks of code are encapsulated with each block having one or more associated handlers. Each handler specifies some form of filter condition on the type of exception it handles, when an exception is raised by code in a protected block, the set of corresponding handlers is searched in order and the first one with a matching filter condition is executed.
A single method can have multiple structured exception handling blocks, and the blocks can also be nested within each other.
SEH structure:
It contains an exception record with a machine-independent description of an exception, a context record with a machine-dependent description of the processor context at the time of the exception.
How does it work?
The exception handlers are linked to each other, they form a linked list chain on the stack, and sit relatively close to the bottom of the stack, when an exception occurs, it retrieves the head of the SEH chain and goes through the list and tries to find the suitable handler to close the application properly.
Can it be abused somehow? Yes :-)
When exploiting an SEH overwrite an attacker needs to overwrite the handler attribute of the EXCEPTION_RESITRATION_RECORD with the address of an instruction sequence similar to POP POP RET. When an exception is triggered, this causes to pass the execution to this overwritten address which subsequently returns to the location on the stack of the NEXT attribute of the EXCEPTION_REGISTRATION_RECORD. Then the Next attribute is also controlled by the attacker, but if we recall the stack layout the Next attribute is below the Handler Attribute.
Note: This limits the attacker to 4 bytes before running into the handler address.
In any case, by overwriting the Next attribute with the instructions that jump to the handler attribute the attacker typically has enough room for arbitrary shellcode, and this is exactly what happens.
Differences with a basic overwrite of EIP:
On a regular stack-based buffer overflow, we can overwrite the return address of EIP, and when doing a SEH-Based overflow we continue to overwrite the stack after EIP until we reach and overwrite the default exception handler.The following exploit code: https://www.exploit-db.com/exploits/44155/ exploits a remote SEH-based buffer overflow by overwriting SEH and after a short jump it executes a shellcode in the stack.
In the following screenshot I have used a payload full of A’s ( 5000 ) and we can see how EIP is overwritten and an Access Violation is triggered. In order to get the Stack-Executable we need to disable DEP.
Using Mona.py, I have created a pattern to find the offset that needs to be used for EIP, relaunch the exploit and then find the offset and SEH to overwrite.
Here you can see the commands used to find the offset.
After we have overwritten the SEH we can jump to our shellcode, in this example I have used a NOPs Led for debugging and halted the execution with CC.
And here we can see how we got code execution ( our shellcode ) that is calling CALC as a proof of concept. The exploit it’s already doing the calculation of the shellcode size so you can replace it with null-free shellcode if you wish to execute something different.
Where to get Mona: https://github.com/corelan/mona
Where to get Pycharm: https://www.jetbrains.com/pycharm/download/#section=linux
MSDN SEH: https://msdn.microsoft.com/en-us/library/windows/desktop/ms680657(v=vs.85).aspx
Last updated