Custom Hooking Function
As we know manually implementing malware techniques is the most optimal approach for avoiding IoC & security mechanisms.
Creating Detour Hook
A detour hook is just a jump instruction placed at the first few instructions of function we are hooking. This is called a "Trampoline".
Assembly Instructions (64-bit)
A trampoline looks like:
pAddress
: The address of our Detour function (64 bit).
Move pAddress
to r10
register. Jump to the memory address located in r10
register.
Machine Byte Code (64-bit):
0x49
(REX prefix) Instruction involving a register. (use the 64-bit version of the default operand size.)0xBA
is the actual opcode for themov
instruction.0x41
(REX prefix) 64-bit register. Indicates the use of ther8
throughr15
registers.0xFF
is the actual opcode for thejmp
instruction.
64-Bit Hook (API function patch):
NOTE: We can retrieve pAddress with GetProcAddress (preferably with a custom version of GetProcAddress).
Writing the Hook
Before overwriting the function, we need to update the memory permissions to RWX.
Where pFunctionToHook
is the address of the function to hook, and uTrampoline
is the jump shellcode.
Unhooking
After the hooked function is called we want to unhook the function, the bytes that were overwritten should be restored with those that were originally there. Before we hook the function, we should store the original bytes in a buffer. pOriginalBytes
Restoring permissions:
Full Example:
As a recap, it's always recommended, if feasible, to implement your own method of a exploitation technique. This can greatly improve your chances of bypassing security measures.
Last updated