VEH = Vectored Exception Handling

VEH Hooking is very powerful and stealthy. Let's break it down super clearly:


๐Ÿ“š What is VEH Hooking?

VEH = Vectored Exception Handling.

In Windows, a VEH is a custom function you can register that catches exceptions (errors) when your program crashes, before normal Structured Exception Handling (SEH) kicks in.

๐Ÿ‘‰ Hackers and malware use VEH to catch and modify execution flow without touching the original code on disk!


๐Ÿง  Simple logic:

  • Instead of overwriting functions (like inline hooking), you register a handler.

  • Then you force a controlled exception (like a memory access violation).

  • When the exception happens, Windows calls your VEH.

  • Your VEH modifies context (the CPU registers) and redirects execution to your shellcode or payload.

✨ No patching code, no visible jumps — it's extremely stealthy.


๐Ÿ› ️ How to Set Up a VEH Hook:

  1. Call AddVectoredExceptionHandler(1, MyHandlerFunction);

    • (1) = call this handler first if multiple handlers are registered.

    • MyHandlerFunction = your custom code to run when exceptions happen.

  2. Make something crash intentionally:

    • Access forbidden memory (mov eax, [0x00000000]).

    • Or trigger a STATUS_GUARD_PAGE_VIOLATION.

  3. When exception happens, Windows calls your VEH handler.

  4. Inside the VEH, you can:

    • Fix the crash.

    • Change EIP/RIP (Instruction Pointer) to jump to your shellcode.

    • Resume execution.


๐Ÿ“œ Simple Code Example (C++)

LONG WINAPI MyVEH(EXCEPTION_POINTERS* ExceptionInfo) {      // If it's the kind of exception we want      if (ExceptionInfo->ExceptionRecord->ExceptionCode == EXCEPTION_ACCESS_VIOLATION) {          // Redirect execution          ExceptionInfo->ContextRecord->Rip = (DWORD64)MyShellcode;  // x64          return EXCEPTION_CONTINUE_EXECUTION;      }      return EXCEPTION_CONTINUE_SEARCH;  }    void SetupVEH() {      AddVectoredExceptionHandler(1, MyVEH);  }  

๐Ÿ”Ž Now whenever an access violation happens ➔ your shellcode runs.


๐ŸŽฏ Why is VEH Hooking Dangerous (and interesting)?

Feature Why it matters
๐Ÿ›ก️ No code modification Nothing obvious in disassembly. No JMP inserted.
๐Ÿ‘ป Hard to detect Code looks clean, AV/EDR tools have trouble seeing the hook.
๐ŸŽฏ Full control You can hijack execution at any point you can trigger an exception.
๐Ÿ”ฅ Powerful for exploit dev Especially in ROP chains, privilege escalations, shellcode loaders.

๐Ÿงจ Bonus: Even Smarter VEH Tricks

  • Page Guard VEH: Mark memory with PAGE_GUARD, and handle page accesses one by one.

  • VEH Shellcode: Some advanced malware installs a VEH and then erases itself from memory.


๐Ÿ‘€ Memory view when VEH is active:

  • No changes to function bytes.

  • Exception handling table has extra entries.

  • New handlers visible via:

    • WinDbg command: !veh

    • x64dbg Plugin: "VEH Breakpoints" tab.


๐Ÿ”ฅ Summary:

Term Meaning
VEH A special way to handle exceptions before SEH.
VEH Hooking Using exceptions + VEH to redirect code execution stealthily.


Comments

Popular posts from this blog

Avoiding detection when injecting a DLL into a game process

Some global variable set, mystery function

FULL REFLECTIVE INJECTION PACK