-
Notifications
You must be signed in to change notification settings - Fork 352
/
Hook.nim
173 lines (129 loc) · 6.17 KB
/
Hook.nim
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#[
Author: Fabian Mosch, Twitter: @ShitSecure
License: BSD 3-Clause
]#
import winim
type
typeMessageBox* = proc (hWnd: HWND, lpText: LPCSTR, lpCaption: LPCSTR, uType: UINT): int32 {.stdcall.}
type
MyNtFlushInstructionCache* = proc (processHandle: HANDLE, baseAddress: PVOID, numberofBytestoFlush: ULONG): NTSTATUS {.stdcall.}
type
HookedFunction* {.bycopy.} = object
origFunction*: typeMessageBox
functionStub*: array[16, BYTE]
HookTrampolineBuffers* {.bycopy.} = object
originalBytes*: HANDLE ## (Input) Buffer containing bytes that should be restored while unhooking.
originalBytesSize*: DWORD ## (Output) Buffer that will receive bytes present prior to trampoline installation/restoring.
previousBytes*: HANDLE
previousBytesSize*: DWORD
var ntdlldll = LoadLibraryA("ntdll.dll")
if (ntdlldll == 0):
echo "[X] Failed to load ntdll.dll"
var ntFlushInstructionCacheAddress = GetProcAddress(ntdlldll,"NtFlushInstructionCache")
if isNil(ntFlushInstructionCacheAddress):
echo "[X] Failed to get the address of 'NtFlushInstructionCache'"
var ntFlushInstructionCache*: MyNtFlushInstructionCache
ntFlushInstructionCache = cast[MyNtFlushInstructionCache](ntFlushInstructionCacheAddress)
proc fastTrampoline*(installHook: bool, addressToHook: LPVOID, jumpAddress: LPVOID, buffers: ptr HookTrampolineBuffers = nil): bool
proc restoreHook() : void
var gHookedFunction*: HookedFunction
var messageBoxAddress*: HANDLE
var messageBox*: typeMessageBox
proc myMessageBox(hWnd: HWND, lpText: LPCSTR, lpCaption: LPCSTR, uType: UINT): int32 =
restoreHook()
MessageBoxA(hWnd, "Hooked!", "Hooked!", uType)
if(fastTrampoline(true, cast[LPVOID](messageBoxAddress), cast[LPVOID](myMessageBox), nil)):
echo "[+] Re-Hooked 'MessageBoxA'"
else:
echo "[-] Failed to re-hook 'MessageBoxA'"
return 0
proc restoreHook() : void =
var buffers: HookTrampolineBuffers
buffers.originalBytes = cast[HANDLE](addr gHookedFunction.functionStub[0])
buffers.originalBytesSize = DWORD(sizeof(gHookedFunction.functionStub))
if(fastTrampoline(false, cast[LPVOID](messageBoxAddress), cast[LPVOID](myMessageBox), &buffers)):
echo "[+] Restored 'MessageBoxA'"
else:
echo "[-] Failed to restore 'MessageBoxA'"
proc fastTrampoline(installHook: bool; addressToHook: LPVOID; jumpAddress: LPVOID;
buffers: ptr HookTrampolineBuffers): bool =
var trampoline: seq[byte]
if defined(amd64):
trampoline = @[
byte(0x49), byte(0xBA), byte(0x00), byte(0x00), byte(0x00), byte(0x00), byte(0x00), byte(0x00), # mov r10, addr
byte(0x00),byte(0x00),byte(0x41), byte(0xFF),byte(0xE2) # jmp r10
]
var tempjumpaddr: uint64 = cast[uint64](jumpAddress)
copyMem(&trampoline[2] , &tempjumpaddr, 6)
elif defined(i386):
trampoline = @[
byte(0xB8), byte(0x00), byte(0x00), byte(0x00), byte(0x00), # mov eax, addr
byte(0x00),byte(0x00),byte(0xFF), byte(0xE0) # jmp eax
]
var tempjumpaddr: uint32 = cast[uint32](jumpAddress)
copyMem(&trampoline[1] , &tempjumpaddr, 3)
var dwSize: DWORD = DWORD(len(trampoline))
var dwOldProtect: DWORD = 0
var output: bool = false
if (installHook):
if (buffers != nil):
if ((buffers.previousBytes == 0) or buffers.previousBytesSize == 0):
echo "[-] Previous Bytes == 0"
return false
copyMem(unsafeAddr buffers.previousBytes, addressToHook, buffers.previousBytesSize)
if (VirtualProtect(addressToHook, dwSize, PAGE_EXECUTE_READWRITE, &dwOldProtect)):
copyMem(addressToHook, addr trampoline[0], dwSize)
output = true
else:
if (buffers != nil):
if ((buffers.originalBytes == 0) or buffers.originalBytesSize == 0):
echo "[-] Original Bytes == 0"
return false
dwSize = buffers.originalBytesSize
if (VirtualProtect(addressToHook, dwSize, PAGE_EXECUTE_READWRITE, &dwOldProtect)):
copyMem(addressToHook, cast[LPVOID](buffers.originalBytes), dwSize)
output = true
var status = ntFlushInstructionCache(GetCurrentProcess(), addressToHook, dwSize)
if (status == 0):
echo "[+] NtFlushInstructionCache success"
else:
echo "[-] NtFlushInstructionCache failed: ", toHex(status)
VirtualProtect(addressToHook, dwSize, dwOldProtect, &dwOldProtect)
return output
proc hookFunction(funcname: string, dllName: LPCSTR, hookFunction: LPVOID): bool =
var addressToHook: LPVOID = cast[LPVOID](GetProcAddress(GetModuleHandleA(dllName), funcname))
messageBoxAddress = cast[HANDLE](addressToHook)
var buffers: HookTrampolineBuffers
var output: bool = false
if (addressToHook == nil):
return false
buffers.previousBytes = cast[HANDLE](addressToHook)
buffers.previousBytesSize = DWORD(sizeof(addressToHook))
gHookedFunction.origFunction = cast[typeMessageBox](addressToHook)
var pointerToOrigBytes: LPVOID = addr gHookedFunction.functionStub
copyMem(pointerToOrigBytes, addressToHook, 16)
output = fastTrampoline(true, cast[LPVOID](addressToHook), hookFunction, &buffers)
return output
echo "[*] Loading User32.dll"
var user32dll = LoadLibraryA("user32.dll")
if (user32dll == 0):
echo "[-] Failed to load user32.dll"
quit(1)
echo "[*] Getting the address of 'MessageBoxA'"
messageBoxAddress = cast[HANDLE](GetProcAddress(user32dll,"MessageBoxA"))
if (messageBoxAddress == 0):
echo "[X] Failed to get the address of 'MessageBoxA'"
quit(1)
messageBox = cast[typeMessageBox](messageBoxAddress)
echo "[*] Hooking 'MessageBoxA'"
if (hookFunction("MessageBoxA", "user32.dll", cast[LPVOID](myMessageBox))):
echo "[+] Hooked 'MessageBoxA'"
else:
echo "[-] Failed to hook 'MessageBoxA'"
quit(1)
echo "[*] Calling Hooked MessageBoxA!"
discard messageBox(0, "Hello World", "Hello World", 0)
echo "[*] Restoring old MessageBoxA"
restoreHook()
echo "[*] Calling Original MessageBoxA!"
discard messageBox(0, "Hello World", "Hello World", 0)