-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirectsyscalls.py
405 lines (347 loc) · 17.6 KB
/
directsyscalls.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
# This file is Copyright 2020 Volatility Foundation and licensed under the Volatility Software License 1.0
# which is available at https://www.volatilityfoundation.org/license/vsl-v1.0
import io
import logging
import struct
from typing import List, Tuple, Iterable
from volatility3.framework import interfaces, exceptions, renderers, constants, symbols
from volatility3.framework.configuration import requirements
from volatility3.plugins.windows import pslist, dlllist, vadinfo, malfind
from volatility3.framework.objects import utility
from volatility3.framework.symbols import intermed
from volatility3.framework.symbols.windows.extensions import pe
from volatility3.framework.renderers import format_hints
vollog = logging.getLogger(__name__)
try:
import pefile
except ImportError:
vollog.info(
"Python pefile module not found, plugin (and dependent plugins) not available"
)
raise
class DirectSyscalls(interfaces.plugins.PluginInterface):
"Display DirectSyscalls"
_version = (1, 0, 0)
_required_framework_version = (2, 0, 0)
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.config['primary'] = self.context.modules[self.config['kernel']].layer_name
self.config['nt_symbols'] = self.context.modules[self.config['kernel']].symbol_table_name
self.kaddr_space = self.config['primary']
self._config = self.config
@classmethod
def get_requirements(cls) -> List[interfaces.configuration.RequirementInterface]:
# Since we're calling the plugin, make sure we have the plugin's requirements
return [
requirements.ModuleRequirement(
name="kernel",
description="Windows kernel",
architectures=["Intel32", "Intel64"],
),
requirements.SymbolTableRequirement(name="nt_symbols", description="Windows kernel symbols"),
requirements.ListRequirement(
name="pid",
description="Filter on specific process IDs",
element_type=int,
optional=True,
),
requirements.PluginRequirement(
name="pslist", plugin=pslist.PsList, version=(2, 0, 0)
),
requirements.VersionRequirement(
name="dlllist", component=dlllist.DllList, version=(2, 0, 0)
),
requirements.VersionRequirement(
name="vadinfo", component=vadinfo.VadInfo, version=(2, 0, 0)
),
#requirements.VersionRequirement(
# name="malfind", component=malfind.Malfind, version=(2, 0, 0)
#),
]
def get_dll_exports(
cls,
context: interfaces.context.ContextInterface,
pe_table_name: str,
layer_name: str,
base_address: int,
) -> Tuple[Tuple[str, str]]:
"""Get File and Product version information from PE files.
Args:
context: volatility context on which to operate
pe_table_name: name of the PE table
layer_name: name of the layer containing the PE file
base_address: base address of the PE (where MZ is found)
"""
if layer_name is None:
raise TypeError("Layer must be a string not None")
pe_data = io.BytesIO()
dos_header = context.object(
pe_table_name + constants.BANG + "_IMAGE_DOS_HEADER",
offset=base_address,
layer_name=layer_name,
)
for offset, data in dos_header.reconstruct():
pe_data.seek(offset)
pe_data.write(data)
pe = pefile.PE(data=pe_data.getvalue(), fast_load=True)
pe.parse_data_directories()
exports = []
for exp in pe.DIRECTORY_ENTRY_EXPORT.symbols:
#print(exp.address, exp.address_offset)
exports.append(((pe.OPTIONAL_HEADER.ImageBase + exp.address), exp.name, exp.ordinal))
return exports
@classmethod
def list_injections(
cls,
context: interfaces.context.ContextInterface,
kernel_layer_name: str,
symbol_table: str,
proc: interfaces.objects.ObjectInterface,
) -> Iterable[Tuple[interfaces.objects.ObjectInterface, bytes]]:
"""Generate memory regions for a process that may contain injected
code. same as malfind function but instead reading 64 bytes lets read all the vad, we can
merge this function with additional arg extract_size to master in the future
Args:
context: The context to retrieve required elements (layers, symbol tables) from
kernel_layer_name: The name of the kernel layer from which to read the VAD protections
symbol_table: The name of the table containing the kernel symbols
proc: an _EPROCESS instance
Returns:
An iterable of VAD instances and the first 64 bytes of data containing in that region
"""
proc_id = "Unknown"
try:
proc_id = proc.UniqueProcessId
proc_layer_name = proc.add_process_layer()
except exceptions.InvalidAddressException as excp:
vollog.debug(
"Process {}: invalid address {} in layer {}".format(
proc_id, excp.invalid_address, excp.layer_name
)
)
return
proc_layer = context.layers[proc_layer_name]
for vad in proc.get_vad_root().traverse():
protection_string = vad.get_protection(
vadinfo.VadInfo.protect_values(
context, kernel_layer_name, symbol_table
),
vadinfo.winnt_protections,
)
write_exec = "EXECUTE" in protection_string and "WRITE" in protection_string
# the write/exec check applies to everything
if not write_exec:
continue
if (vad.get_private_memory() == 1 and vad.get_tag() == "VadS") or (
vad.get_private_memory() == 0
and protection_string != "PAGE_EXECUTE_WRITECOPY"
):
if malfind.Malfind.is_vad_empty(proc_layer, vad):
continue
data = proc_layer.read(vad.get_start(), vad.get_size(), pad=True)
yield vad, data
def extract_reg_at_offset(self, prev_instructions, reg_id, offset=0):
""" Extract the value of eax at specific offset from disassembly
note: could be better identify (in some cases) using unicorn
Return:
eax/rax value
"""
c_index = -1
instruction_to_reg = []
for inst in prev_instructions[::-1]:
# get eax value from the mov mnemonic
if inst.mnemonic == 'mov' and (inst.op_str.startswith('eax') or inst.op_str.startswith('rax')):
data_to_display = '\n'
eax_new_val = inst.operands[1].imm
instruction_to_reg.append(inst)
for i in instruction_to_reg[::-1]:
data_to_display += f"{hex(offset+i.address)}\t{i.mnemonic} {i.op_str}\n"
return eax_new_val, data_to_display
# extract the value from memory (we can use this function recursive to extract the other registers inside the [] if needed)
elif inst.mnemonic == 'mov' and (inst.op_str.startswith('eax') or inst.op_str.startswith('rax')):
return None, ''#pass # prev_instructions[:c_index??]
# we dont know how to get this value from pop
elif inst.mnemonic == 'pop' and (inst.op_str.startswith('eax') or inst.op_str.startswith('rax')):
return None, ''#pass
# we dont know how to get this value from pop
elif inst.mnemonic in ['test', '???']:
return None, ''#pass
c_index -= 1
instruction_to_reg.append(inst)
return None, None
def _generator(self, data):
pe_table_name = intermed.IntermediateSymbolTable.create(
self.context, self.config_path, "windows", "pe", class_types=pe.class_types
)
try:
has_capstone = True
import capstone
except ImportError:
has_capstone = False
vollog.debug("Disassembly library capstone not found")
kernel = self.context.modules[self.config["kernel"]]
is_32bit_arch = not symbols.symbol_table_is_64bit(
self.context, kernel.symbol_table_name
)
process_list = [proc for proc in data]
# now go through the process and dll lists
for proc in process_list:
proc_id = "Unknown"
try:
proc_id = proc.UniqueProcessId
process_name = proc.ImageFileName.cast(
"string",
max_length=proc.ImageFileName.vol.count,
errors="replace",
)
proc_layer_name = proc.add_process_layer()
except exceptions.InvalidAddressException as excp:
vollog.debug(
"Process {}: invalid address {} in layer {}".format(
proc_id, excp.invalid_address, excp.layer_name
)
)
continue
# if we're on a 64 bit kernel, we may still need 32 bit disasm due to wow64
if is_32bit_arch or proc.get_is_wow64():
architecture = "intel"
if has_capstone:
capst = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_32)
else:
architecture = "intel64"
if has_capstone:
capst = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
for entry in proc.load_order_modules():
try:
BaseDllName = entry.BaseDllName.get_string()
except exceptions.InvalidAddressException:
BaseDllName = renderers.UnreadableValue()
try:
DllBase = format_hints.Hex(entry.DllBase)
except exceptions.InvalidAddressException:
DllBase = renderers.UnreadableValue()
if BaseDllName == 'ntdll.dll':
break
try:
# get all ntdll exports
ntdll_exports = self.get_dll_exports(
self._context, pe_table_name, proc_layer_name, entry.DllBase
)
except exceptions.InvalidAddressException as ex:#(exceptions.InvalidAddressException, ValueError, AttributeError) as ex:
vollog.log(
constants.LOGLEVEL_VVV,
f"Error while pefile {process_name}, {proc_id}\n{ex}",
)
return
syscalls = sorted([i for i in ntdll_exports if i[1] and i[1].startswith(b'Zw')])
for proc in process_list:
process_name = utility.array_to_string(proc.ImageFileName)
for vad, data in self.list_injections(
self.context, kernel.layer_name, kernel.symbol_table_name, proc
):
#print(data[0x1200:0x1300], type(data))
# if we're on a 64 bit kernel, we may still need 32 bit disasm due to wow64
if is_32bit_arch or proc.get_is_wow64():
if has_capstone:
eax_or_rax_id = capstone.x86.X86_REG_EAX
capst = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_32)
capst.detail = True
else:
if has_capstone:
eax_or_rax_id = capstone.x86.X86_REG_RAX
capst = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_64)
capst.detail = True
prev_instructions = []
syscall_like = [b'\x0f\x05', # Syscall
#b'\x0f\x34', # Sysenter # remove a lot of FP's and kinda old and not really used (enable if you use such os)
#b'\xcd\x2e', # int 0x2e
]
start_address = vad.get_start()
extend_after_by = 20
for c_syscall in syscall_like:
c_syscall_len = len(c_syscall)
for c_byte_index in range(0, len(data) - c_syscall_len, 1):
c_data = data[c_byte_index:c_byte_index+c_syscall_len]
if c_data == c_syscall:
# Try to dissasembly the data to find the syscall instruction
for start_from in range(25 if c_byte_index > 25 else c_byte_index, 0, -1):
extended_chunk = data[c_byte_index-start_from:c_byte_index+extend_after_by]
inst_data = "\n"
flag_good_data = False
prev_instructions = []
for i in capst.disasm(extended_chunk, 0):
prev_instructions.append(i)
inst_data += f"{hex((start_address+c_byte_index-start_from)+i.address)}\t{i.mnemonic} {i.op_str}"
# we decide that the data is good if the disass contains our instruction
if c_syscall in bytes(i.opcode)[:c_syscall_len]:
#print(start_from)
# display only current syscall and above
if start_from > 10:
inst_data = ''
prev_instructions = []
continue
if flag_good_data:
break
eax, data_to_display = self.extract_reg_at_offset(prev_instructions, eax_or_rax_id, vad.get_start())
if eax:
c_direct_syscall = syscalls[eax][1].decode()
else:
eax = -1
c_direct_syscall = 'failed to identify'
inst_data += f"\t<{c_direct_syscall} [syscall]>\n"
flag_good_data = True
else:
inst_data += "\n"
# if we found our hash in the Dissasembly -> break
if flag_good_data:
c_byte_index += extend_after_by
yield (
0,
(
proc.UniqueProcessId,
process_name,
format_hints.Hex(vad.get_start()),
format_hints.Hex(vad.get_end()),
vad.get_tag(),
vad.get_protection(
vadinfo.VadInfo.protect_values(
self.context,
kernel.layer_name,
kernel.symbol_table_name,
),
vadinfo.winnt_protections,
),
vad.get_commit_charge(),
vad.get_private_memory(),
eax,
c_direct_syscall,
inst_data,
),
)
break
def run(self):
filter_func = pslist.PsList.create_pid_filter(self.config.get("pid", None))
kernel = self.context.modules[self.config["kernel"]]
return renderers.TreeGrid(
[
("PID", int),
("Process", str),
("Start VPN", format_hints.Hex),
("End VPN", format_hints.Hex),
("Tag", str),
("Protection", str),
("CommitCharge", int),
("PrivateMemory", int),
("Syscall EAX", int),
("Syscall Funtion", str),
("Disasm", str),
],
self._generator(
pslist.PsList.list_processes(
context=self.context,
layer_name=kernel.layer_name,
symbol_table=kernel.symbol_table_name,
filter_func=filter_func,
)
),
)