-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAnalyze_Shellcode.py
More file actions
395 lines (354 loc) · 13.9 KB
/
Analyze_Shellcode.py
File metadata and controls
395 lines (354 loc) · 13.9 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
# vim: set fileencoding=utf-8 :
# «Analyze Shellcode» for Hopper 4
# Copyright (c) 2018, Daniel Roethlisberger <daniel@roe.ch>
# https://github.com/droe/hopper-scripts
#
# This aims at detecting and annotating typical shellcode patterns in Hopper:
# - Known code blocks
# - Popping the return address from the stack as a way to reference data
# - Calling well-known imports by their name hash
# It is not very useful with fully handcrafted shellcode, unless one or more of
# these techniques was used.
#
# For best results:
# 1) Load shellcode at any base address, disabling automatic analysis
# 2) Modify -> Disassemble whole segment
# 3) Run this script
#
# The script asks if it should mark everything unknown and disassemble before
# walking all the disassembled instructions, looking for the above patterns.
# Except for this optional initial disassembling pass, the script does not
# attempt to change code to data and vice-versa. For more tricky shellcode,
# typical workflow is to fix disassembly manually where needed and let the
# script run again to do the annotations.
import hopper_api as api
IMPORTS = (
"kernel32.dll!LoadLibraryA",
"kernel32.dll!GetVersion",
"kernel32.dll!GetLastError",
"kernel32.dll!SetUnhandledExceptionFilter",
"kernel32.dll!CreateFileA",
"kernel32.dll!DeleteFileA",
"kernel32.dll!ReadFile",
"kernel32.dll!ReadFileEx",
"kernel32.dll!WriteFile",
"kernel32.dll!WriteFileEx",
"kernel32.dll!SetEvent",
"kernel32.dll!GetTempPathA",
"kernel32.dll!CloseHandle",
"kernel32.dll!VirtualAlloc",
"kernel32.dll!VirtualAllocEx",
"kernel32.dll!VirtualFree",
"kernel32.dll!CreateProcessA",
"kernel32.dll!WriteProcessMemory",
"kernel32.dll!CreateRemoteThread",
"kernel32.dll!GetProcAddress",
"kernel32.dll!WaitForSingleObject",
"kernel32.dll!Sleep",
"kernel32.dll!WinExec",
"kernel32.dll!ExitProcess",
"kernel32.dll!CreateThread",
"kernel32.dll!ExitThread",
"kernel32.dll!CreateNamedPipeA",
"kernel32.dll!CreateNamedPipeW",
"kernel32.dll!ConnectNamedPipe",
"kernel32.dll!DisconnectNamedPipe",
"kernel32.dll!lstrlenA",
"ntdll.dll!RtlCreateUserThread",
"ntdll.dll!RtlExitUserThread",
"advapi32.dll!RevertToSelf",
"advapi32.dll!StartServiceCtrlDispatcherA",
"advapi32.dll!RegisterServiceCtrlHandlerExA",
"advapi32.dll!SetServiceStatus",
"advapi32.dll!OpenSCManagerA",
"advapi32.dll!OpenServiceA",
"advapi32.dll!ChangeServiceConfig2A",
"advapi32.dll!CloseServiceHandle",
"user32.dll!GetDesktopWindow",
"ws2_32.dll!WSAStartup",
"ws2_32.dll!WSASocketA",
"ws2_32.dll!WSAAccept",
"ws2_32.dll!bind",
"ws2_32.dll!listen",
"ws2_32.dll!accept",
"ws2_32.dll!closesocket",
"ws2_32.dll!connect",
"ws2_32.dll!recv",
"ws2_32.dll!send",
"ws2_32.dll!setsockopt",
"ws2_32.dll!gethostbyname",
"wininet.dll!InternetOpenA",
"wininet.dll!InternetConnectA",
"wininet.dll!HttpOpenRequestA",
"wininet.dll!HttpSendRequestA",
"wininet.dll!InternetErrorDlg",
"wininet.dll!InternetReadFile",
"wininet.dll!InternetSetOptionA",
"winhttp.dll!WinHttpOpen",
"winhttp.dll!WinHttpConnect",
"winhttp.dll!WinHttpOpenRequest",
"winhttp.dll!WinHttpSendRequest",
"winhttp.dll!WinHttpReceiveResponse",
"winhttp.dll!WinHttpReadData",
"dnsapi.dll!DnsQuery_A",
"pstorec.dll!PStoreCreateInstance",
)
# First match per start address wins.
KNOWN_BLOCKS = (
{
'name': 'dll_call_by_hash',
'proc': True,
'comment': """
Metasploit x86 call-by-hash by Stephen Fewer
https://github.com/rapid7/metasploit-framework/blob/master/external/source/shellcode/windows/x86/src/block/block_api.asm
""",
'start': (
0x60, # pushal
0x89, 0xE5, # mov ebp, esp
0x31, 0xD2, # xor edx, edx
0x64, 0x8B, 0x52, 0x30, # mov edx, dword [fs:edx+0x30]
0x8B, 0x52, 0x0C, # mov edx, dword [edx+0xc]
0x8B, 0x52, 0x14, # mov edx, dword [edx+0x14]
),
'end': (
0x61, # popal
0x59, # pop ecx
0x5A, # pop edx
0x51, # push ecx
0xFF, 0xE0, # jmp eax
0x58, # pop eax
0x5F, # pop edi
0x5A, # pop edx
0x8B, 0x12, # mov edx, dword [edx]
0xEB, 0x86, # jmp <offset>
),
}, {
'name': 'dll_call_by_hash',
'proc': True,
'comment': """
Metasploit x64 call-by-hash by Stephen Fewer
https://github.com/rapid7/metasploit-framework/blob/master/external/source/shellcode/windows/x64/src/block/block_api.asm
""",
'start': (
0x41, 0x51, # push r9
0x41, 0x50, # push r8
0x52, # push rdx
0x51, # push rcx
0x56, # push rsi
0x48, 0x31, 0xD2, # xor rdx, rdx
0x65, 0x48, 0x8B, 0x52, 0x60, # mov rdx, qword [gs:rdx+0x60]
0x48, 0x8B, 0x52, 0x18, # mov rdx, qword [rdx+0x18]
0x48, 0x8B, 0x52, 0x20, # mov rdx, qword [rdx+0x20]
),
'end': (
0x5E, # pop rsi
0x59, # pop rcx
0x5A, # pop rdx
0x41, 0x58, # pop r8
0x41, 0x59, # pop r9
0x41, 0x5A, # pop r10
0x48, 0x83, 0xEC, 0x20, # sub rsp, 0x20
0x41, 0x52, # push r10
0xFF, 0xE0, # jmp rax
0x58, # pop rax
0x41, 0x59, # pop r9
0x5A, # pop rdx
0x48, 0x8B, 0x12, # mov rdx, qword [rdx]
0xE9, 0x4F, 0xFF, 0xFF, 0xFF, # jmp <offset>
),
},
)
class ImportHashes:
CHECK = 'kernel32.dll!LoadLibraryA'
METHODS = ('msf', )
CHECKSUMS = {
'msf': 0x0726774C,
}
def __init__(self):
self._hashmaps = {}
for which in self.METHODS:
#print('Method %s' % which)
self._hashmaps[which] = {}
for spec in IMPORTS:
m, f = spec.split('!')
h = self._hash(which, m, f)
if not h in self._hashmaps[which]:
#print('Adding 0x%08x %s' % (h, spec))
self._hashmaps[which][h] = spec
assert(self.CHECKSUMS[which] in self._hashmaps[which])
assert(self._hashmaps[which][self.CHECKSUMS[which]] == self.CHECK)
def __contains__(self, x):
return any([x in self._hashmaps[t] for t in self._hashmaps])
def __getitem__(self, k):
# Currently returns the first match; should improve this to handle
# collisions between different hashing methods in a more useful way
for t in self._hashmaps:
if k in self._hashmaps[t]:
return self._hashmaps[t][k]
raise KeyError(k)
def _ror32(self, x, bits):
return (x >> bits | x << (32 - bits)) & 0xFFFFFFFF
def _wide(self, s):
out = []
for c in s:
out.append(c)
out.append("\x00")
return ''.join(out)
def _hash_ror(self, module, function, bits):
mhash = 0
fhash = 0
for c in self._wide(module + "\x00"):
mhash = self._ror32(mhash, bits) + ord(c)
for c in function + "\x00":
fhash = self._ror32(fhash, bits) + ord(c)
return (mhash + fhash) & 0xFFFFFFFF
def _hash(self, which, module, function):
if which == 'msf':
return self._hash_ror(module.upper(), function, 13)
raise NotImplementedError(which)
class KnownBlocksHelper:
# XXX this works, but badly needs a rewrite
def __init__(self, seg, addr, size):
self._seg = seg
self._addr = addr
self._size = size
self._buf = seg.readBytes(addr, size)
def compare_bytes(self, pos, literals):
if pos < self._addr or pos >= self._addr + self._size:
raise ValueError("pos out of bounds")
if pos + len(literals) > self._addr + self._size:
return False
for i in range(len(literals)):
if self._buf[pos + i - self._addr] != chr(literals[i]):
return False
return True
def find_bytes(self, pos, literals):
if pos < self._addr or pos >= self._addr + self._size:
raise ValueError("pos out of bounds")
if pos + len(literals) > self._addr + self._size:
return -1
remaining_size = self._size - (pos - self._addr)
for i in range(remaining_size - len(literals)):
if self.compare_bytes(pos + i, literals):
return pos + i
return -1
def yield_matches(self, literals):
match_addr = self.find_bytes(self._addr, literals)
while match_addr != -1:
yield match_addr
match_addr = self.find_bytes(match_addr + 1, literals)
def yield_known_blocks(self):
matched_addrs = set()
for block in KNOWN_BLOCKS:
for start_addr in self.yield_matches(block['start']):
if start_addr in matched_addrs:
continue
if 'end' in block:
end_addr = self.find_bytes(start_addr + 1, block['end'])
if not end_addr > start_addr:
# if end cannot be found here, it will never be found
break
end_addr += len(block['end'])
else: # block['size']?
end_addr = start_addr + len(block['start'])
yield block, start_addr, end_addr
matched_addrs.add(start_addr)
def first_stack_instruction(where, pos, n=16):
for ins in where.instructions(pos):
if ins.raw == None:
break
if ins.raw.isAConditionalJump() or ins.raw.isAnInconditionalJump():
break
op = ins.op
if op in ('hlt', 'int', 'enter', 'leave'):
break
if op.startswith('ret') or op.startswith('iret') or \
op.startswith('sys'):
break
if op.startswith('push') or op.startswith('pop'):
return ins
if ins.addr > pos + n:
break
return None
def main():
print("Arch: %s" % api.executable.arch)
seg = api.segments.current()
sel = api.selection()
if sel.is_range():
print("operating on current selection")
shellcode = sel
else:
ans = api.message("Mark segment as undefined and disassemble?",
['Cancel', 'No', 'Yes'])
if ans == 0:
return
elif ans == 2:
seg.mark_as_undefined()
seg.disassemble()
print("operating on current segment")
shellcode = seg
print("analyzing range %x:%x" % (shellcode.start, shellcode.end))
# identify and mark known blocks
kbhelper = KnownBlocksHelper(seg.raw,
shellcode.start, len(shellcode))
for block, start_addr, end_addr in kbhelper.yield_known_blocks():
print("---> found known block '%s' at %x" % (block['name'],
start_addr))
name = "%s_%x" % (block['name'], start_addr)
api.set_label(start_addr, name)
if 'proc' in block and block['proc']:
api.mark_as_procedure(start_addr)
if 'comment' in block and block['comment']:
api.set_comment(start_addr, block['comment'])
if 'inline_comment' in block and block['inline_comment']:
api.set_icomment(start_addr, block['inline_comment'])
if 'offsets' in block:
for offset, offset_name in block['offsets']:
offset_addr = start_addr + offset
offset_name = "%s_%x" % (offset_name, offset_addr)
api.set_label(offset_addr, offset_name)
# xref or annotate call, pop reg combo
for ins in shellcode.instructions():
if ins.op != 'call':
continue
arg = ins.arg(0)
if not arg.startswith('0x'):
continue
target_addr = int(arg, 16)
stack_ins = first_stack_instruction(shellcode, target_addr)
if stack_ins == None or stack_ins.op != 'pop':
continue
if api.get_label(target_addr) == None:
api.set_label(target_addr, "pop_retaddr_%x" % target_addr)
#reg = stack_ins.arg(0)
print("---> found call + pop retaddr combo at %x -> %x" % (
ins.addr, target_addr))
loaded_addr = ins.addr + len(ins)
if loaded_addr == shellcode.end:
# Hopper silently ignores xrefs to EOF
api.set_icomment(stack_ins.addr, "end of shellcode")
else:
api.add_reference(stack_ins.addr, loaded_addr)
if api.get_label(loaded_addr) == None:
api.set_label(loaded_addr, "retaddr_%x" % loaded_addr)
# annotate known import hashes
hashes = ImportHashes()
hash_ops = {
# op, arg index
'push': 0,
'mov': 1,
'movabs': 1,
}
for ins in shellcode.instructions():
op = ins.op
if not op in hash_ops:
continue
arg = ins.arg(hash_ops[op])
if not arg.startswith('0x'):
continue
cand_hash = int(arg, 16)
if cand_hash in hashes:
name = hashes[cand_hash]
api.set_icomment(ins.addr, name)
if __name__ == '__main__':
api.run(main)