In a 32 bits Windows binary, I see this code:

    push next
    push fs:[0]
    mov fs:[0], esp

I see that something happens on the int3 (an error), but I don't understand why, and how to follow execution while keeping control.


1 Answer 1



  1. the first 3 lines set an exception handler (an 'error catcher')

  2. the int3 generates an exception

  3. execution resumes at next


this trick is (ab)using Structured Exception Handling, a mechanism to define exception handlers, typically by compilers when try/catch blocks are used.

In 32bits versions of Windows, they can be set on the fly, without any pre-requirement (unless the binary is compiled with /SafeSEH).

The first element of the exception handlers' chain is pointed by the first member of the Thread Information Block (TIB), in turn a member of the Thread Environment Block (TEB), which is pointed to by fs:0 (which is also reachable 'directly' - via something like ds:7efdd00, depending on the OS version etc)

So here is what happens:

  1. the first two push reserve stack space for the structure _EXCEPTION_REGISTRATION_RECORD.

    1. the new top handler
    2. the previous top handler, which was until now at fs:[0]
  2. the mov sets the current stack position as the new structure. When an exception happens, next will be now the first called handler.

  3. int3 triggers an exception instantaneously (there are many other kinds of exception triggers).

  4. as an exception is triggered, Windows dispatches the exception to the first handler, and the next one if it's not handled, until one of them has handled it.

flowchart of Exception handling

Following execution

This is done here under OllyDbg 1.10. YMMV.

As we want to go through exceptions ourselves, we have to ask OllyDbg not to handle them:

  1. go to debugging options: Alt-O, tab Exceptions

  2. unselect INT3 breaks

And when an exception is triggered, we have to enforce that execution is done via exceptions (see below).

Here are 3 methods of increasing level to follow the exception handling execution safely:

step by step: set a breakpoint manually

As the handler has just been set on the stack, you can manually set a breakpoint then run.

  1. select the new handler address on the stack

    new handler on the stack

  2. Right-click or F10

    • select Follow in Dump
  3. in the dump window, open the menu (same shortcut)

    • select BreakPoint, then Hardware, on execution
  4. Execute: menu Debug/Run / shortcut F9 / command-line g

  5. Exceptions will be triggered

  6. Execute with exception handling: shortcut Shift-F9 / command-line ge.

shortcut: execute until exception handler via command-line

  • as the address is on the stack, the easiest way is to type via the command line ge [esp+4], which means, Go with Exceptions, until the 2nd address on the stack is encountered. Thus, no need to set and unset a breakpoint.

    • in the case of a more complex example, where the address might not be obvious on the stack anymore, then the absolute formula would be ge ds:[fs:[0]+4], which just gets the actual address from the TIB.

keeping full control: break on KiUserExceptionDispatcher

KiUserExceptionDispatcher is the Windows API handling all user-mode exceptions. Setting a breakpoint there guarantees that you keep full control - but then, you're in the middle of a Windows API ;)

In this case, you can ask OllyDbg to skip exceptions, as you will still break execution manually in any cases. You might also want to combine that with a script.

Of course, some advanced code might check that you set a breakpoint on it before triggering an exception.

  • 2
    "In 32bits versions of Windows, they can be set on the fly, without any pre-requirement." That's not quite correct, it depends on how the executable was compiled, e.g., if compiled with SafeSEH ntdll!RtlDispatchException (IIRC) will terminate your process if you registered an SEH handler on the fly.
    – newgre
    Apr 23, 2013 at 13:22
  • Also, a pretty common obfuscation technique is to modify the instruction pointer of the CPU context that is passed to the SEH handler. So it's usually a good idea to place a breakpoint before the call to NtContinue inside KiUserExceptionDispatcher. IDAStealth can do this for you automatically btw.
    – newgre
    Apr 23, 2013 at 13:31
  • 1
    Execution does resume at next and since the OS doesn't force you to return from the exception handler, that statement is technically correct. In fact, that's exactly how C++ exceptions are implemented in the CRT.
    – newgre
    Apr 23, 2013 at 13:34
  • 2
    don't forget that any Vectored Exception Handler which was registered already will receive control first. The SEH might never execute at all. Apr 23, 2013 at 17:59
  • 1
    This is a really great write-up, I'd love to see SEHOP added to this sometime for the sake of completeness. Also, just to be extremely nit-picky: when int3 fires, an interrupt does occur and an interrupt handler invoked, right? I understand that stepping through the kernel call would be impossible in ImmunityDebugger (and while kernel debugging with Windbg, IIRC), but I just wanted to clarify for my own understanding.
    – mrduclaw
    Apr 26, 2013 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.