-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcasper_agent.py
executable file
·885 lines (686 loc) · 26.5 KB
/
casper_agent.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
#!/usr/bin/env python3
from rich.console import Console
console = Console()
original_print = print
print = console.log # hijack the print
from requests_html import AsyncHTMLSession, HTMLSession
import toml
import typer
import faker
import datetime
import subprocess
import time
from datetime import timedelta
import glob
import random
import multiprocessing
import threading
# import shlex
import uuid
import json
import socket
import smtplib
import poplib
import ssl
import sys
from typing import List
from pathlib import Path
import re
import asyncio
app = typer.Typer() # for cli operations
fake = faker.Faker()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("1.1.1.1", 80))
AGENT_IP = s.getsockname()[0]
s.close()
AGENT_UUID = uuid.uuid3(
uuid.NAMESPACE_DNS, str(uuid.getnode())
).hex # based on mac address
AGENT_PLATFORM = sys.platform
AGENT_TAGS = []
DEBUG = True
BEACON = True # disable the beacon with ./casper_client --disable-beacon
ONLY_BEACON = False
C2_BEACON_SERVER = "http://localhost:5000"
C2_BEACON_ENDPOINT = (
"/beacon" # for beaconing stats about operations to casper_server.py
)
IS_BEACONING = False # this will be used to signal whether or not we are beaconing.
LAST_ACTION = "" # for the beacon....
LAST_RESULT = "" # basic results.
OPS_DIR = "./ops"
OPS = []
DOWNLOAD_DIR = "./DOWNLOADED_FILES"
@app.callback()
def load_config(
config_file: Path = Path("config.toml"),
debug: bool = True,
c2_server:str = 'http://localhost:5000',
):
global AGENT_UUID
global AGENT_TAGS
global DEBUG
global BEACON
global ONLY_BEACON
global C2_BEACON_SERVER
global C2_BEACON_ENDPOINT
global OPS_DIR
global DOWNLOAD_DIR
try:
config = toml.loads(open(config_file).read())
AGENT_UUID = config.get(
"AGENT_UUID", uuid.uuid3(uuid.NAMESPACE_DNS, str(uuid.getnode())).hex
)
AGENT_TAGS = config.get("AGENT_TAGS", [])
DEBUG = config.get("DEBUG", True)
BEACON = config.get("BEACON", True)
if c2_server != 'http://localhost:5000':
print(f'overriding c2-server from config file... {c2_server} ')
C2_BEACON_SERVER = c2_server
else:
C2_BEACON_SERVER = config.get("C2_BEACON_SERVER", "http://localhost:5000")
C2_BEACON_ENDPOINT = config.get("C2_BEACON_ENDPOINT", "/beacon")
OPS_DIR = config.get("OPS_DIR", "./ops")
DOWNLOAD_DIR = config.get("DOWNLOAD_DIR", DOWNLOAD_DIR)
except BaseException as e:
print(e)
print("failed to load config... ")
sys.exit()
def _jitter(jitter_amount=0) -> int:
if jitter_amount == 0:
return 0
return random.randint(jitter_amount * -1, jitter_amount)
def delay(base_time, jitter_amount):
if jitter_amount != 0:
jitter_amount = _jitter(jitter_amount=jitter_amount)
time.sleep(abs(base_time + jitter_amount))
@app.command()
def get_uuid():
"""this returns the uuid of agent on THIS computer (useful for targeting ops.)
derived from uuid.uuid3(uuid.NAMESPACE_DNS, str(uuid.getnode())).hex # based on mac address"""
original_print(AGENT_UUID)
def beacon(interval: int = 5, jitter_amount: int = 0):
"""only send beacons"""
global LAST_ACTION
global IS_BEACONING
if BEACON == False or IS_BEACONING == True:
return
IS_BEACONING = True
while True:
BEACON_PAYLOAD = {
"uuid": AGENT_UUID,
"time": str(datetime.datetime.now()),
"ip": AGENT_IP,
"platform": AGENT_PLATFORM,
"tags": AGENT_TAGS,
"last_action": LAST_ACTION,
}
if DEBUG:
print(f"beacon payload: {BEACON_PAYLOAD}")
try:
sess = HTMLSession()
r = sess.post(
f"{C2_BEACON_SERVER}{C2_BEACON_ENDPOINT}", json=BEACON_PAYLOAD
)
new_ops = json.loads(r.text).get("OPS")
if DEBUG:
print(f"beacon: {r}; retrieved new operations {new_ops}")
if ONLY_BEACON == False and len(new_ops) > 0:
if DEBUG:
print("Executing new operations... ")
operate(ops=new_ops, new_ops=True)
except BaseException as e:
print(f"Error beaconing: {e}")
# time.sleep(interval + _jitter(jitter_amount=jitter_amount))
delay(interval, jitter_amount)
def in_time_range(hours_of_operation: List[str], msg="") -> bool:
"""a list of ["00:00:00", "23:59:59"]
"""
if hours_of_operation == None:
print('hours of op not specified')
return True
start_time = hours_of_operation[0]
end_time = hours_of_operation[1]
current = datetime.datetime.now()
current = current.replace(year=2001, month=1, day=1)
start_time = datetime.datetime.strptime(
f"01/01/01 {start_time}", "%m/%d/%y %H:%M:%S"
)
end_time = datetime.datetime.strptime(f"01/01/01 {end_time}", "%m/%d/%y %H:%M:%S")
while not start_time <= current <= end_time:
current = datetime.datetime.now()
current = current.replace(year=2001, month=1, day=1)
delta = start_time - current
if DEBUG:
print(msg, "waiting for hours of operation", delta)
if delta < timedelta(seconds=30):
time.sleep(1)
else:
print("sleeping 10 seconds...")
time.sleep(10)
if DEBUG:
print(msg, "in time range")
return True
def agent_is_target(
target_ids: List[str] = [],
target_platforms: List[str] = [],
target_tags: List[str] = [],
):
if target_ids == [] and target_platforms == [] and target_tags == []: # got all defaults therefore it wasn't a targeted op.
return True
if target_ids == ["all"] : # this is a targeted op targeting 'all'; for backwards compatibility ; will deprecate
print("this is a deprecated target mode... just omit and all agents will be targeted ")
return True
if AGENT_UUID in target_ids:
print(f"I am a valid target uuid {AGENT_UUID}")
return True
elif AGENT_PLATFORM in target_platforms:
print(f"I am a valid target platform: {AGENT_PLATFORM}")
return True
for tag in AGENT_TAGS:
if tag in target_tags:
print(f"I have a target tag {tag}")
return True
return False
import pyppeteer
async def _visit_page_helper(url: str):
"""Helper to parse obfuscated / JS-loaded profiles like Facebook.
We need a separate function to handle requests-html's async nature
in a threaded program... """
session = AsyncHTMLSession()
browser = await pyppeteer.launch({
'ignoreHTTPSErrors': True,
'headless': True,
'handleSIGINT': False,
'handleSIGTERM': False,
'handleSIGHUP': False,
# 'dumpio':True, # show the output from chromedriver
})
session._browser = browser
resp = await session.get(url)
await resp.html.arender(timeout=20)
await session.close()
# await browser.close()
return resp
@app.command()
def visit_page(
url: str,
start_offset: int = 0,
repeat_every: int = 0,
hours_of_operation: List[str] = None,
start_jitter: int = 0,
repeat_jitter: int = 0,
) -> bool:
print("in visit thread")
"""visit an arbitrary url. can be configured to repeat"""
if hours_of_operation:
in_time_range(hours_of_operation, msg=f"visit_page {url}")
if DEBUG:
print(
f'{"-"*40 }\nVisiting {url} in {start_offset} seconds; will repeat every {repeat_every} +/- {repeat_jitter} seconds. 0 means does not repeat'
)
delay(start_offset, start_jitter)
global LAST_ACTION
try:
if repeat_every > 0:
while True:
# if hours_of_operation:
# if in_time_range(hours_of_operation, msg=f"visit_page {url}") == False:
# return # we've breached the hours of operation...stop working...
start_time = time.time()
try:
resp = asyncio.run(_visit_page_helper(url)) # this is required because you can't render the html from a thread other than the main python thread of the main interpreter
# original_print(resp.html.absolute_links)
except BaseException as e:
print(f'repeat visit error ******{e}*****')
end_time = time.time()
action = f"visit_page: Got {resp.status_code} from {resp.url} in {end_time - start_time} seconds; sleeping {repeat_every} +/- {repeat_jitter} seconds..."
if DEBUG:
print(action)
LAST_ACTION = action
delay(repeat_every, repeat_jitter)
else:
start_time = time.time()
try:
resp = asyncio.run(_visit_page_helper(url)) # this is required because you can't render the html from a thread other than the main python thread of the main interpreter
# original_print(resp.html.absolute_links)
except BaseException as e:
print(f'repeat visit error ******{e}*****')
end_time = time.time()
action = f"visit_page: Got {resp.status_code} from {resp.url} in {end_time - start_time} seconds;; {resp.text[:25]} all done..."
print(action)
LAST_ACTION = action
return True
except BaseException as e:
print('big exception', e)
return False
def _save_file(resp, download_dir:DOWNLOAD_DIR):
fname = ''
if "Content-Disposition" in resp.headers.keys():
fname = re.findall("filename=(.+)", resp.headers["Content-Disposition"])[0]
else:
fname = resp.url.split("/")[-1]
output_path = f'{download_dir}/{fname}'
with open(output_path, 'wb') as f:
f.write(resp.content)
print(f'Done downloading {fname}. saved {len(resp.content)} bytes to "{output_path}"...')
@app.command()
def download_file(
url: str,
start_offset: int = 0,
repeat_every: int = 0,
hours_of_operation: List[str] = None,
start_jitter: int = 0,
repeat_jitter: int = 0,
download_dir: str = ""
) -> bool:
"""download file from url. can be configured to repeat"""
global DOWNLOAD_DIR
if hours_of_operation:
in_time_range(hours_of_operation, msg=f"download {url}")
if DEBUG:
print(
f'{"-"*40 }\ndownloading {url} in {start_offset} seconds; will repeat every {repeat_every} +/- {repeat_jitter} seconds. 0 means does not repeat'
)
delay(start_offset, start_jitter)
global LAST_ACTION
try:
if repeat_every > 0:
while True:
start_time = time.time()
try:
resp = asyncio.run(_visit_page_helper(url)) # this is required because you can't render the html from a thread other than the main python thread of the main interpreter
_save_file(resp, download_dir=download_dir)
except BaseException as e:
print(f'download error ******{e}*****')
end_time = time.time()
action = f"download: Got {resp.status_code} from {resp.url} in {end_time - start_time} seconds; sleeping {repeat_every} +/- {repeat_jitter} seconds..."
if DEBUG:
print(action)
LAST_ACTION = action
delay(repeat_every, repeat_jitter)
else:
start_time = time.time()
try:
resp = asyncio.run(_visit_page_helper(url)) # this is required because you can't render the html from a thread other than the main python thread of the main interpreter
_save_file(resp, download_dir=download_dir)
except BaseException as e:
print(f'download error ******{e}*****')
end_time = time.time()
action = f"download: Got {resp.status_code} from {resp.url} in {end_time - start_time} seconds; {len(resp.content)} bytes all done..."
print(action)
LAST_ACTION = action
return True
except BaseException as e:
print('download big exception', e)
return False
@app.command()
def run_command(
cmd: str,
start_offset: int = 0,
repeat_every: int = 0,
hours_of_operation: List[str] = None,
start_jitter: int = 0,
repeat_jitter: int = 0,
) -> bool:
"""run an arbitrary system command. the command should not be interactive or blocking in set to repeat"""
if hours_of_operation:
in_time_range(hours_of_operation, msg=f"run_command {cmd}")
if DEBUG:
print(
f'{"-"*40 }\nRunning {cmd} in {start_offset } seconds +/- {start_jitter} seconds of jitter; will repeat every {repeat_every} seconds +/- {repeat_jitter} seconds of jitter. 0 means does not repeat'
)
delay(start_offset, start_jitter)
global LAST_ACTION
try:
if repeat_every > 0:
while True:
output = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
action = f'run_command "{cmd}" yields "{output.stdout.read().decode()}"'
if DEBUG:
print(action)
LAST_ACTION = action
delay(repeat_every, repeat_jitter)
else:
output = subprocess.Popen(
cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
action = f'run_command "{cmd}" yields "{output.stdout.read().decode()}"'
if DEBUG:
print(action)
LAST_ACTION = action
return True
except BaseException as e:
print(e)
return False
def _send_email(**kwargs):
"""same keyword args as send_email()"""
context = None
if kwargs.get("encrypted", False):
context = ssl.create_default_context()
with smtplib.SMTP(kwargs.get("smtp_server",'localhost'), kwargs.get("smtp_port",1025)) as s:
# s.login(kwargs.get('smtp_user'), kwargs.get('smtp_pass'))
s.sendmail(
kwargs.get("sender",''), kwargs.get("recipient_list",[]), kwargs.get("email_body",'')
)
@app.command()
def send_email(
recipient_list: List[str],
sender: str = None,
email_subj: str = None,
email_body: str = None,
email_headers: str = None,
attachments: List[str] = [],
smtp_server: str = "localhost",
smtp_port: int = 1025,
smtp_user: str = "[email protected]",
smtp_pass: str = "somepass",
encrypted: bool = False,
start_offset: int = 0,
repeat_every: int = 0,
hours_of_operation: List[str] = None,
start_jitter: int = 0,
repeat_jitter: int = 0,
) -> bool:
"""ONLY PARTIALLY IMPLEMENTED; send an email to multiple recipent addresses
test server -> python -m smtpd -c DebuggingServer -n localhost:1025
https://realpython.com/python-send-email/
"""
if sender == None:
sender = fake.email()
if email_body == None:
email_body = " ".join(fake.sentences(random.randint(1, 10)))
if email_subj == None:
email_subj = " ".join(fake.sentence(1))
if hours_of_operation:
in_time_range(
hours_of_operation,
msg=f"send_email to {recipient_list} with subject {email_subj}",
)
if DEBUG:
print(
f"sending email to {recipient_list} from {sender}; subject {email_subj}; headers {email_headers}; body {email_body}; via {smtp_server}:{smtp_port} in {start_offset } seconds +/- {start_jitter} seconds of jitter; will repeat every {repeat_every} seconds +/- {repeat_jitter} seconds of jitter. 0 means does not repeat"
)
delay(start_offset, start_jitter)
func_params = {
"recipient_list": recipient_list,
"sender": sender,
"email_subj": email_subj,
"email_body": email_body,
"email_headers": email_headers,
"attachments": attachments,
"smtp_server": smtp_server,
"smtp_port": smtp_port,
"smtp_user": smtp_user,
"smtp_pass": smtp_pass,
"encrypted": encrypted,
}
global LAST_ACTION
try:
if repeat_every > 0:
while True:
sender = fake.email()
email_body = " ".join(fake.sentences(random.randint(1, 10)))
email_subj = " ".join(fake.sentence(1))
func_params = {
"recipient_list": recipient_list,
"sender": sender,
"email_subj": email_subj,
"email_body": email_body,
"email_headers": email_headers,
"attachments": attachments,
"smtp_server": smtp_server,
"smtp_port": smtp_port,
"smtp_user": smtp_user,
"smtp_pass": smtp_pass,
"encrypted": encrypted,
}
action = f"send_email: continually sending email to {recipient_list} from {sender}; subject {email_subj}; headers {email_headers}; body {email_body}"
_send_email(**func_params)
if DEBUG:
print(action)
LAST_ACTION = action
delay(repeat_every, repeat_jitter)
else:
# one shot
action = f"send_email: sending an email to {recipient_list} from {sender}; subject {email_subj}; headers {email_headers}; body {email_body}"
_send_email(**func_params)
LAST_ACTION = action
return True
except BaseException as e:
print(e)
if smtp_server == 'localhost' and smtp_port == 1025:
print('''It looks like you are running the default smtp server settings.
try running "cd email_server && python smtp_debugging_server.py" or "python -m smtpd -c DebuggingServer -n localhost:1025" both operate on 1025 for MTA features.
You'll probably want to run the POP3 server pop3_debugging_server.py in the same dir. operates on port 1110 by default for MDA features
''')
return False
@app.command()
def random_visit(
urls: List[str],
start_offset: int = 0,
repeat_every: int = 0,
hours_of_operation: List[str] = None,
start_jitter: int = 0,
repeat_jitter: int = 0,
) -> bool:
"""randomly visit a list of urls"""
if hours_of_operation:
in_time_range(hours_of_operation, msg=f"random_visit unknown url... ")
if DEBUG:
print(
f'{"-"*40 }\nVisiting one of {urls} in {start_offset} seconds; will repeat every {repeat_every} seconds. 0 means do not repeat'
)
delay(start_offset, start_jitter)
try:
if repeat_every > 0:
while True:
url = random.choice(urls)
print(f"Picking random url: {url}")
visit_page(url)
delay(repeat_every, repeat_jitter)
else:
url = random.choice(urls)
print(f"Picking random url: {url}")
visit_page(url)
return True
except BaseException as e:
print(e)
return False
def _wander(url, dwell=30, depth=5, current_depth=0):
if current_depth > depth:
return False
if DEBUG:
print(f'current depth {current_depth}')
# sess = HTMLSession()
# # print(f'visiting {url}')
# resp = sess.get(url, verify=False)
# # breakpoint()
# resp.html.render()
try:
resp = asyncio.run(_visit_page_helper(url))
except BaseException as e:
print('wander error using _visit_page_helper: ', e)
return False
urls = list(resp.html.absolute_links)
if len(urls) == 0:
return True
next_url = random.choice(urls)
if DEBUG:
print(f"available urls: {urls}")
print("next_url is", next_url)
delay(dwell, 1)
_wander(next_url, dwell=dwell, depth=depth, current_depth=current_depth + 1)
return True
@app.command()
def wander(
url: str,
wander_depth: int = 5,
current_depth: int = 0,
wander_dwell: int = 30,
start_offset: int = 0,
repeat_every: int = 0,
hours_of_operation: List[str] = None,
start_jitter: int = 0,
repeat_jitter: int = 0,
) -> bool:
"""randomly follow links on a page upto depth links deep with a delay of delay in between"""
if hours_of_operation:
in_time_range(hours_of_operation, msg=f"wandering {url}")
delay(start_offset, start_jitter)
try:
if repeat_every > 0:
while True:
print(f"Wandering url: {url}")
_wander(url, dwell=wander_dwell, depth=wander_depth, current_depth=current_depth)
delay(repeat_every, repeat_jitter)
else:
print(f"Wandering url: {url}")
_wander(url, dwell=wander_dwell, depth=wander_depth, current_depth=current_depth)
return True
except BaseException as e:
print('big wander error:', e)
return False
def load_ops(ops_dir=OPS_DIR, op_filter="*.toml") -> list:
print(f"loading operations from {ops_dir}")
ops = []
op_files = glob.glob(f"{ops_dir}/{op_filter}")
for op_file in op_files:
with open(op_file) as f:
obj = toml.loads(f.read())
ops.append(obj)
print("Loaded", ops)
return ops
@app.command()
def operate(
ops=[],
no_ops: bool = False,
op_filter="*.toml",
new_ops=False,
ops_dir: Path = Path("./ops"),
disable_beacon: bool = False,
tags: List[str] = [],
):
"""actually fire off the operations defined in the ops.toml files
op_filter is set to *.toml and will match all *.toml files in ops_dir
"""
global AGENT_TAGS
global IS_BEACONING
if ops == [] and new_ops == False:
ops = load_ops(ops_dir=ops_dir, op_filter=op_filter)
if no_ops == True:
ops = []
if tags != []:
AGENT_TAGS = tags
console.rule("BEGIN OPERATION")
if IS_BEACONING == False:
if disable_beacon == True and IS_BEACONING == False:
print("beacon disabled")
else:
print('starting beacon')
t = threading.Thread(target=beacon)
t.start()
for op in ops:
tasks = op.get("tasks")
for task in tasks:
if (
agent_is_target(
target_ids=task.get("target_uuids", []),
target_platforms=task.get("target_platforms", []),
target_tags=task.get("target_tags", []),
)
== False
):
# I am not a valid target for this tasking... move on to the next tasking...
print(f"I am not a valid target for task: {task.get('action')}")
continue
print(f"I am a valid target for task: {task.get('action')}")
# all of the common args for tasks are here
kwargs = {
"start_offset": task.get("start_offset", 0),
"repeat_every": task.get("repeat_every", 0),
"hours_of_operation": task.get("hours_of_operation", None),
"start_jitter": task.get("start_jitter", 0),
"repeat_jitter": task.get("repeat_jitter", 0),
}
if task.get("action") == "visit":
args = (task.get("url"),)
kwargs.update({})
t = threading.Thread(target=visit_page, args=args, kwargs=kwargs, daemon=True)
t.start()
elif task.get("action") == "download":
args = (task.get("url"),)
kwargs.update({
"download_dir": task.get("download_dir", DOWNLOAD_DIR)
})
t = threading.Thread(target=download_file, args=args, kwargs=kwargs, daemon=True)
t.start()
elif task.get("action") == "run":
args = (task.get("cmd"),)
kwargs.update({})
t = threading.Thread(
target=run_command, args=args, kwargs=kwargs, daemon=True
)
t.start()
elif task.get("action") == "random_visit":
args = (task.get("urls"),)
kwargs.update({})
t = threading.Thread(target=random_visit, args=args, kwargs=kwargs, daemon=True)
t.start()
elif task.get("action") == "wander":
args = (task.get("starting_url"),)
kwargs.update({
'wander_dwell': task.get('wander_dwell', 30),
'wander_depth': task.get('wander_depth', 0),
'repeat_every': task.get('repeat_every', 30),
'repeat_jitter': task.get('repeat_jitter', 0),
})
t = threading.Thread(target=wander, args=args, kwargs=kwargs, daemon=True)
t.start()
elif task.get("action") == "send_email":
args = (
task.get("recipient_list",[]),
task.get("sender"),
)
kwargs.update(
{
"email_subj": task.get("email_subj"),
"email_body": task.get("email_body"),
"email_headers": task.get("email_headers"),
"start_offset": task.get("start_offset", 0),
"repeat_every": task.get("repeat_every", 0),
}
)
t = threading.Thread(
target=send_email, args=args, kwargs=kwargs, daemon=True
)
t.start()
print(f"Done processing {len(ops)} ops...")
while True:
time.sleep(1) # keep the main thread and interpreter running to support the background threads and processes for visit_page and
@app.command()
def remote_ops(server: str = C2_BEACON_SERVER):
sess = HTMLSession()
resp = sess.get(f"{server}/newops")
print(resp.text)
@app.command()
def flush_ops(server:str = C2_BEACON_SERVER):
sess = HTMLSession()
resp = sess.get(f"{server}/flushops")
print(resp.text)
@app.command()
def push_op(files: List[Path]):
"""TESTING load an opfile from an arbitrary path."""
ops = []
for file in files:
with open(file) as f:
obj = toml.loads(f.read())
ops.append(obj)
sess = HTMLSession()
resp = sess.post(f"{C2_BEACON_SERVER}/newops", json=ops)
print(resp.json())
if __name__ == "__main__":
load_config()
app()