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:
-
Call
AddVectoredExceptionHandler(1, MyHandlerFunction);-
(1) = call this handler first if multiple handlers are registered.
-
MyHandlerFunction= your custom code to run when exceptions happen.
-
-
Make something crash intentionally:
-
Access forbidden memory (
mov eax, [0x00000000]). -
Or trigger a
STATUS_GUARD_PAGE_VIOLATION.
-
-
When exception happens, Windows calls your VEH handler.
-
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
Post a Comment