forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 586
/
hooks_internal.py
273 lines (228 loc) · 8.62 KB
/
hooks_internal.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
import json
import logging
from json.decoder import JSONDecodeError
import jq as jq
import terminaltables
from sqlalchemy import and_
from sqlalchemy.orm import Session
from empire.server.core.db import models
from empire.server.core.hooks import hooks
log = logging.getLogger(__name__)
def ps_hook(db: Session, task: models.AgentTask):
"""
This hook watches for the 'ps' command and writes the processes into the processes table.
For Powershell Agents, the data comes back (as of 4.1) as JSON.
For Python Agents, the data comes back in the typical 'ls' format.
For C# Agents, no support yet.
AFAIK, it is not easy to convert the shell tables into JSON, but I found this jq wizardry
on StackOverflow, so that's what we'll stick with for now for the python results, even though it is imperfect.
https://unix.stackexchange.com/a/243485
"""
if task.input.strip() not in ["ps", "tasklist"] or task.agent.language == "csharp":
return
if task.agent.language == "python":
output = (
jq.compile(
"""[sub("\n$";"") | splits("\n") | sub("^ +";"") | [splits(" +")]] | .[0] as $header | .[1:] | [.[] | [. as $x | range($header | length) | {"key": $header[.], "value": $x[.]}] | from_entries]"""
)
.input(task.output.split("\r\n ..Command execution completed.")[0])
.first()
)
else:
try:
output = json.loads(task.output)
except JSONDecodeError:
log.warning(
"Failed to decode JSON output from ps command. Most likely, the command returned an error."
)
return
existing_processes = (
db.query(models.HostProcess.process_id)
.filter(models.HostProcess.host_id == task.agent.host_id)
.all()
)
existing_processes = [p[0] for p in existing_processes]
for process in output:
process_name = process.get("CMD") or process.get("ProcessName") or ""
process_id = process.get("PID")
arch = process.get("Arch")
user = process.get("UserName")
if process_id:
# new process
if int(process_id) not in existing_processes:
db.add(
models.HostProcess(
host_id=task.agent.host_id,
process_id=process_id,
process_name=process_name,
architecture=arch,
user=user,
)
)
# update existing process
elif int(process_id) in existing_processes:
db_process: models.HostProcess = (
db.query(models.HostProcess)
.filter(
and_(
models.HostProcess.host_id == task.agent.host_id,
models.HostProcess.process_id == process_id,
)
)
.first()
)
if not db_process.agent:
db_process.architecture = arch
db_process.process_name = process_name
db_process.user = user
for process in existing_processes:
# mark processes that are no longer running stale
if process not in [int(p.get("PID")) for p in output]:
db_process: models.HostProcess | None = (
db.query(models.HostProcess)
.filter(
and_(
models.HostProcess.host_id == task.agent.host_id,
models.HostProcess.process_id == process,
)
)
.first()
)
db_process.stale = True
def ps_filter(db: Session, task: models.AgentTask):
"""
This filter converts the JSON results of the ps command and converts it to a PowerShell-ish table.
if the results are from the Python or C# agents, it does nothing.
"""
if task.input.strip() not in [
"ps",
"tasklist",
] or task.agent.language not in ["powershell", "ironpython"]:
return db, task
try:
output = json.loads(task.output)
except JSONDecodeError:
log.warning(
"Failed to decode JSON output from ps command. Most likely, the command returned an error."
)
return db, task
output_list = []
for rec in output:
output_list.append(
[
rec.get("PID"),
rec.get("ProcessName"),
rec.get("Arch"),
rec.get("UserName"),
rec.get("MemUsage"),
]
)
output_list.insert(0, ["PID", "ProcessName", "Arch", "UserName", "MemUsage"])
table = terminaltables.AsciiTable(output_list)
table.inner_row_border = False
table.outer_border = False
table.inner_column_border = False
task.output = table.table
return db, task
def ls_filter(db: Session, task: models.AgentTask):
"""
This filter converts the JSON results of the ls command and converts it to a PowerShell-ish table.
if the results are from the Python or C# agents, it does nothing.
"""
task_input = task.input.strip().split()
if (
len(task_input) == 0
or task_input[0] not in ["ls", "dir"]
or task.agent.language != "powershell"
):
return db, task
try:
output = json.loads(task.output)
except JSONDecodeError:
log.warning(
"Failed to decode JSON output from ls command. Most likely, the command returned an error."
)
return db, task
output_list = []
for rec in output:
output_list.append(
[
rec.get("Mode"),
rec.get("Owner"),
rec.get("LastWriteTime"),
rec.get("Length"),
rec.get("Name"),
]
)
output_list.insert(0, ["Mode", "Owner", "LastWriteTime", "Length", "Name"])
table = terminaltables.AsciiTable(output_list)
table.inner_row_border = False
table.outer_border = False
table.inner_column_border = False
task.output = table.table
return db, task
def ipconfig_filter(db: Session, task: models.AgentTask):
"""
This filter converts the JSON results of the ifconfig/ipconfig command and converts it to a PowerShell-ish table.
if the results are from the Python or C# agents, it does nothing.
"""
if (
task.input.strip() not in ["ipconfig", "ifconfig"]
or task.agent.language != "powershell"
):
return db, task
output = json.loads(task.output)
if isinstance(output, dict): # if there's only one adapter, it won't be a list.
output = [output]
output_list = []
for rec in output:
for key, value in rec.items():
output_list.append([key, f": {value}"])
output_list.append([])
table = terminaltables.AsciiTable(output_list)
table.inner_heading_row_border = False
table.inner_row_border = False
table.outer_border = False
table.inner_column_border = False
task.output = table.table
return db, task
def route_filter(db: Session, task: models.AgentTask):
"""
This filter converts the JSON results of the route command and converts it to a PowerShell-ish table.
if the results are from the Python or C# agents, it does nothing.
"""
if task.input.strip() not in ["route"] or task.agent.language != "powershell":
return db, task
output = json.loads(task.output)
output_list = []
for rec in output:
output_list.append(
[
rec.get("Destination"),
rec.get("Netmask"),
rec.get("NextHop"),
rec.get("Interface"),
rec.get("Metric"),
]
)
output_list.insert(0, ["Destination", "Netmask", "NextHop", "Interface", "Metric"])
table = terminaltables.AsciiTable(output_list)
table.inner_row_border = False
table.outer_border = False
table.inner_column_border = False
task.output = table.table
return db, task
def initialize():
hooks.register_hook(hooks.BEFORE_TASKING_RESULT_HOOK, "ps_hook_internal", ps_hook)
hooks.register_filter(
hooks.BEFORE_TASKING_RESULT_FILTER, "ps_filter_internal", ps_filter
)
hooks.register_filter(
hooks.BEFORE_TASKING_RESULT_FILTER, "ls_filter_internal", ls_filter
)
hooks.register_filter(
hooks.BEFORE_TASKING_RESULT_FILTER, "ipconfig_filter_internal", ipconfig_filter
)
hooks.register_filter(
hooks.BEFORE_TASKING_RESULT_FILTER, "route_filter_internal", route_filter
)