-
Notifications
You must be signed in to change notification settings - Fork 3
/
nimwhispers.py
executable file
·178 lines (154 loc) · 5.57 KB
/
nimwhispers.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
#!/usr/bin/python3
import json
import random
import argparse
SEED = random.randint(2 ** 28, 2 ** 32 - 1)
def get_function_hash(function_name):
h = SEED
name = function_name.replace('Nt', 'Zw', 1)
ror8 = lambda v: ((v >> 8) & (2 ** 32 - 1)) | ((v << 24) & (2 ** 32 - 1))
for segment in name:
partial_name_short = ord(segment)
h ^= partial_name_short + ror8(h)
return h
def combine_arguments(arguments):
return ',\n '.join(
f'{argument["name"]}: {argument["type"]}'
for argument in arguments
)
def generate_function(name, arguments):
return f'''
proc {name}*({combine_arguments(arguments)}): NTSTATUS {{.asmNoStackFrame.}} =
asm """
push rcx
push rdx
push r8
push r9
sub rsp, 32
mov rcx, {hex(get_function_hash(name))}
call FindSyscall
add rsp, 32
pop r9
pop r8
pop rdx
pop rcx
mov r10, rcx
syscall
ret
"""
'''
def get_all_types(types, prototypes, all_functions):
visited = set()
param_types = set(
param["type"]
for name in all_functions
for param in prototypes[name]["params"]
)
def get_all_definitions(identifiers):
while identifiers:
i = identifiers.pop()
if i in visited:
continue
visited.add(i)
for type in types:
if type["identifier"] == i:
depencies = set(d for d in type["dependencies"])
yield from get_all_definitions(depencies)
yield type["definition"]
yield None
yield from get_all_definitions(param_types)
def generate(allowed_functions, outf, _filter=False):
with open("./data/base.nim", "r") as basef, \
open(f"./out/{outf}.nim", "w") as whisperf, \
open("./data/prototypes.json", "r") as protf, \
open("./data/types.json", "r") as typesf:
base = basef.read()
# Replace seed
base = base.replace("###SEED###", f"const seed = int64({hex(SEED)})")
# Replace functions
prototypes = json.load(protf)
all_functions = [
name
for name in prototypes
if name in allowed_functions or not _filter
]
functions = "\n".join(
generate_function(name, prototypes[name]["params"])
for name in all_functions
)
base = base.replace("###FUNCTIONS###", functions)
# Replace types
types = json.load(typesf)
definitions = "\n\n".join(
definition
for definition in get_all_types(types, prototypes, all_functions)
if not definition is None
)
base = base.replace("###TYPES###", definitions)
print(f"[*] Done, written to ./out/{outf}.nim")
whisperf.write(base)
if __name__ == "__main__":
print("""
_______ .__ __ __.__ .__
\ \ |__| _____/ \ / \ |__ |__| ____________ ___________ ______
/ | \| |/ \ \/\/ / | \| |/ ___/\____ \_/ __ \_ __ \/ ___/
/ | \ | Y Y \ /| Y \ |\___ \ | |_> > ___/| | \/\___ \
\____|__ /__|__|_| /\__/\ / |___| /__/____ >| __/ \___ >__| /____ >
\/ \/ \/ \/ \/ |__| \/ \/
@SECFORCE_LTD
""")
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--preset', help='Preset ("all", "common")', required=False)
parser.add_argument('-f', '--functions', help='Comma-separated functions', required=False)
parser.add_argument('-o', '--out-file', help='Output basename (w/o extension)', required=True)
args = parser.parse_args()
if args.preset == 'all':
print("[*] Generating ALL functions")
generate([], args.out_file)
elif args.preset == 'common':
print("[*] Generating Common functions")
generate(
{'NtCreateProcess',
'NtCreateThreadEx',
'NtOpenProcess',
'NtOpenProcessToken',
'NtTestAlert',
'NtOpenThread',
'NtSuspendProcess',
'NtSuspendThread',
'NtResumeProcess',
'NtResumeThread',
'NtGetContextThread',
'NtSetContextThread',
'NtClose',
'NtReadVirtualMemory',
'NtWriteVirtualMemory',
'NtAllocateVirtualMemory',
'NtProtectVirtualMemory',
'NtFreeVirtualMemory',
'NtQuerySystemInformation',
'NtQueryDirectoryFile',
'NtQueryInformationFile',
'NtQueryInformationProcess',
'NtQueryInformationThread',
'NtCreateSection',
'NtOpenSection',
'NtMapViewOfSection',
'NtUnmapViewOfSection',
'NtAdjustPrivilegesToken',
'NtDeviceIoControlFile',
'NtQueueApcThread',
'NtWaitForMultipleObjects'},
args.out_file,
_filter=True
)
elif args.preset:
parser.error('\n[!] Invalid preset provided. Must be "all" or "common".')
elif not args.functions:
parser.error("""\n[!] Either functions or preset must be specified.
EXAMPLE: ./nimwhispers.py --preset common --out-file nimwhispers')
EXAMPLE: ./nimwhispers.py --functions NtTestAlert,NtGetCurrentProcessorNumber --out-file nimwhispers""")
else:
functions = args.functions.split(',') if args.functions else []
print(f"[*] Generating the following functions {args.functions}")
generate(functions, args.out_file, _filter=True)