SEH Overflow

SEH Overview

Thread Environmental Block - TEB 
    Accessable via !teb in windbg
    Can view the linked list via the data type _EXCEPTION_REGISTRATION_RECORD with dt:

Exception list (_EXCEPTION_REGISTRATION_RECORD) includes two pointers:
    Next link in the list and the location of the exception handler

SEH Validation:

    On generation of an exception:
        ExceptionList linked list is gathered from the TEB
        The list is parsed and the exception handler is called (if Safe SEH is enabled it will compare the Exception Handler to the SafeSEH table)
        If no handler returns a success the application crashes

SEH Overflow:

Typically requires the buffer to be close to the start of the stack to reach

In windbg find the location via:
    !teb
    dt _EXCEPTION_REGISTRATION_RECORD

_except_handler prototype:  
     _except_handler(
         PEXCEPTION_RECORD ExceptionRecord, 
         VOID EstablisherFrame, 
         PCONTEXT ContextRecord, 
         PDISPATCHER_CONTEXT DispatcherContext)

    EstablisherFrame contains the address to the next Exception Handler

    Buffer overflow > 
    Overwrite a _EXCEPTION_REGISTRATION_RECORD > 
    After an exception is triggered the application will call _except_handler > 
    The 3rd arguement of the _except_handler,the EstablisherFrame, will point into the overflow buffer >
    Overwriting the EstablisherFrame with an address pointing to a Return Oriented Programing (ROP) gadget such as the common pop r32, pop r32, ret (pop the 3rd and 4th arguements of the _except_handler then return with the EstablisherFrame address) >
    Allows the attacker to reliably return execution into the attacker controlled buffer

    Utilize msf-pattern_create -l n to find the exact offset of the EstablisherFrame
    Trigger the exception and use !exchain in windbg to view the unique pattern which overwrote the EstablisherFrame
    Utilize msf-pattern_offset -l n -q 11223344(unique pattern)

Detect bad chars (fun :)

Locating pop, pop, ret ROP gadget:

"Island Hopping":

Last updated

Was this helpful?