-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmasqueradeprocess.py
180 lines (156 loc) · 6.81 KB
/
masqueradeprocess.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
# 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
from typing import List, Tuple
from volatility3.framework import interfaces, exceptions, renderers, constants
from volatility3.framework.configuration import requirements
from volatility3.plugins.windows import pslist, dlllist
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 MasqueradeProcess(interfaces.plugins.PluginInterface):
"Display masquerade processes by original name"
_version = (1, 0, 0)
_required_framework_version = (2, 0, 0)
@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)
),
]
def get_pe_file(
cls,
context: interfaces.context.ContextInterface,
pe_table_name: str,
layer_name: str,
base_address: int,
) -> Tuple[Tuple[str, str]]:
"""Get pefile PE object from file.
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)
return pe
def _generator(self, data):
pe_table_name = intermed.IntermediateSymbolTable.create(
self.context, self.config_path, "windows", "pe", class_types=pe.class_types
)
# now go through the process and dll lists
for proc in data:
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
# break after the first, since we want only the executable and not other dlls
# so entry would only be the executable name
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()
try:
original_name = -1
c_pefile = self.get_pe_file(self._context, pe_table_name, proc_layer_name, entry.DllBase)
c_pefile.parse_data_directories(directories=[pefile.DIRECTORY_ENTRY['IMAGE_DIRECTORY_ENTRY_RESOURCE']])
file_info = c_pefile.FileInfo
for c_fn in file_info:
for entry in c_fn:
if entry.name == "StringFileInfo":
for string_entry in entry.StringTable:
if b"OriginalFilename" in string_entry.entries:
original_name = string_entry.entries[b"OriginalFilename"].decode()
# if we cant find this file original name then continue
if original_name == -1 or original_name.lower() == BaseDllName.lower():
break
yield (
0,
(
proc_id,
process_name,
DllBase,
BaseDllName,
original_name,
),
)
except (exceptions.InvalidAddressException, ValueError, AttributeError) as ex:
vollog.log(
constants.LOGLEVEL_VVV,
f"Error while pefile {process_name}, {proc_id}\n{ex}",
)
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 Name", str),
("Image Address", int),
("Image Name", str),
("Original Name", 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,
)
),
)