-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy path__main__.py
executable file
·1422 lines (1216 loc) · 49 KB
/
__main__.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
from typing import Any, List, Dict, Union
import cmd2
from cmd2 import Cmd
from cmd2.argparse_custom import Cmd2ArgumentParser
import configparser
import NetworkManager
import subprocess
from colorama import Fore, Style, Back
import functools
import queue
import argparse
import os.path
import tempfile
import signal
import shlex
import time
import dbus
import sys
import os
from htb import util
from htb import Connection, Machine, VPN
from htb.exceptions import *
from htb.scanner.scanner import Tracker, Scanner, Service
from htb.scanner import AVAILABLE_SCANNERS
import htb.scanner
class HackTheBox(Cmd):
""" Hack the Box Command Line Interface """
OS_ICONS = {
"freebsd": "\uf3a4",
"windows": "\uf17a",
"linux": "\uf17c",
"android": "\uf17b",
"solaris": "\uf185",
"other": "\uf233",
}
_singleton = None
ASSIGNED = "\x00assigned\x00"
max_completion_items = 50
case_insensitive = True
def __init__(self, resource="~/.htbrc", *args, **kwargs):
super(HackTheBox, self).__init__(*args, **kwargs)
# Find file location referencing "~"
path_resource = os.path.expanduser(resource)
if not os.path.isfile(path_resource):
raise RuntimeError(f"{resource}: no such file or directory")
# Read configuration
parser = configparser.ConfigParser(interpolation=None)
parser.read(path_resource)
# Save the configuration for later
self.config_path: str = path_resource
self.config = parser
# Extract relevant information
email = parser["htb"].get("email", None)
password = parser["htb"].get("password", None)
api_token = parser["htb"].get("api_token", None)
session = parser["htb"].get("session", None)
# if session is not None:
# self.pwarning("attempting to use existing session")
# Ensure we have an API token
if api_token is None:
raise RuntimeError("no api token provided!")
# Construct the connection object
self.cnxn: Connection = Connection(
api_token=api_token,
email=email,
password=password,
existing_session=session,
analysis_path=self.config["htb"].get("analysis_path", "~/htb"),
twofactor_prompt=self.twofactor_prompt,
subscribe=True,
config=self.config,
)
self.prompt = (
f"{Fore.CYAN}htb{Fore.RESET} {Style.BRIGHT+Fore.GREEN}➜{Style.RESET_ALL} "
)
# List of job trackers
self.jobs: List[Tracker] = []
self.job_events: queue.Queue = queue.Queue()
# Enable self in python
self.self_in_py = True
# Aliases
self.aliases["exit"] = "quit"
self.cnxn.subscribe("repl", self._on_notification)
@classmethod
def get(cls, *args, **kwargs) -> "HackTheBox":
""" Get the singleton object for the HackTheBox REPL
:param args: Positional arguments passed directly to __init__
:param kwargs: Keyword arguments passed directly to __init__
:return: The singleton HackTheBox instance
"""
if cls._singleton is None:
cls._singleton = HackTheBox(*args, **kwargs)
return cls._singleton
def _on_notification(self, message):
""" Display notification information from a asynchronous update """
server = "-".join(self.cnxn.lab.hostname.split(".")[0].split("-")[1:])
# Ignore messages for other servers
if message["server"] != server:
return True
with self.terminal_lock:
self.async_alert(message["title"].lower().split("]")[1])
return True
def twofactor_prompt(self) -> str:
self.pwarning("One Time Password: ", end="")
sys.stderr.flush()
return self.read_input("")
def poutput(self, msg: Any = "", end: str = "\n", apply_style: bool = True) -> None:
if apply_style:
msg = f"[{Style.BRIGHT+Fore.BLUE}-{Style.RESET_ALL}] {msg}"
super(HackTheBox, self).poutput(msg, end=end)
def psuccess(
self, msg: Any = "", end: str = "\n", apply_style: bool = True
) -> None:
if apply_style:
msg = f"[{Style.BRIGHT+Fore.GREEN}+{Style.RESET_ALL}] {msg}"
super(HackTheBox, self).poutput(msg, end=end)
def pwarning(
self, msg: Any = "", end: str = "\n", apply_style: bool = True
) -> None:
if apply_style:
msg = f"[{Style.BRIGHT+Fore.YELLOW}?{Style.RESET_ALL}] {msg}"
super(HackTheBox, self).pwarning(
msg, end=end, apply_style=False,
)
def async_alert(self, msg: str, new_prompt: str = None):
""" Asynchronous alert, and redraw prompt """
msg = f"[{Style.BRIGHT+Fore.YELLOW}!{Style.RESET_ALL}] {msg}"
super(HackTheBox, self).async_alert(msg, new_prompt)
def perror(self, msg: Any = "", end: str = "\n", apply_style: bool = True) -> None:
if apply_style:
msg = f"[{Style.BRIGHT+Fore.RED}!{Style.RESET_ALL}] {msg}"
super(HackTheBox, self).perror(
msg, end=end, apply_style=False,
)
jobs_parser = Cmd2ArgumentParser(description="Manage background scanner jobs")
@cmd2.with_argparser(jobs_parser)
@cmd2.with_category("Management")
def do_jobs(self, args: argparse.Namespace) -> bool:
""" Manage running background scanner jobs """
actions = {"list": self._jobs_list, "kill": self._jobs_kill}
actions[args.action](args)
return False
def _jobs_list(self, args: argparse.Namespace) -> None:
""" List the background scanner jobs """
# Grab any pending events
try:
while True:
t = self.job_events.get_nowait()
t.thread.join()
t.thread = None
# t.status = "completed"
except queue.Empty:
pass
table = [["", "Host", "Service", "Scanner", "Status"]]
for ident, job in enumerate(self.jobs):
style = Style.DIM if job.thread is None else ""
table.append(
[
">" + style + str(ident),
job.machine.name,
f"{job.service.port}/{job.service.protocol} ({job.service.name})",
job.scanner.name,
job.status,
]
)
self.ppaged("\n".join(util.build_table(table)))
def _jobs_kill(self, args: argparse.Namespace) -> None:
""" Stop a running background scanner job """
# Ensure the job exists
if args.job_id < 0 or args.job_id >= len(self.jobs):
self.perror(f"{args.job_id}: no such job")
return
job = self.jobs[args.job_id]
if job.thread is None:
self.pwarning(f"{args.job_id}: already completed")
return
# Inform it should stop
self.poutput(f"killing job {args.job_id}")
job.stop = True
# Argument parser for `machine` command
machine_parser = Cmd2ArgumentParser(
description="View and manage active and retired machines"
)
@cmd2.with_argparser(machine_parser)
@cmd2.with_category("Hack the Box")
def do_machine(self, args: argparse.Namespace) -> bool:
""" View and manage active and retired machines """
actions = {
"list": self._machine_list,
"start": self._machine_start,
"stop": self._machine_stop,
"own": self._machine_own,
"info": self._machine_info,
"cancel": self._machine_cancel,
"reset": self._machine_reset,
"scan": self._machine_scan,
"enum": self._machine_enum,
}
actions[args.action](args)
return False
def _machine_list(self, args: argparse.Namespace) -> None:
""" List machines on hack the box """
# Grab all machines
machines = self.cnxn.machines
if args.state != "all":
machines = [m for m in machines if m.retired == (args.state != "active")]
if args.owned != "all":
machines = [
m
for m in machines
if (m.owned_root and m.owned_user) == (args.owned == "owned")
]
if args.todo:
machines = [m for m in machines if m.todo]
# Pre-calculate column widths to output correctly formatted header
name_width = max([len(m.name) for m in machines]) + 2
ip_width = max([len(m.ip) for m in machines]) + 2
id_width = max([len(str(m.id)) for m in machines]) + 1
diff_width = 12
rating_width = 5
owned_width = 7
state_width = 13
# Lookup tables for creating the difficulty ratings
rating_char = [
"\u2581",
"\u2582",
"\u2583",
"\u2584",
"\u2585",
"\u2586",
"\u2587",
"\u2588",
]
rating_color = [*([Fore.GREEN] * 3), *([Fore.YELLOW] * 4), *([Fore.RED] * 3)]
# Build initial table with headers
table = [
["", "", "OS", "Name", "Address", "Difficulty", "Rate", "Owned", "State"]
]
# Create the individual machine rows
for m in machines:
style = Style.DIM if m.owned_user and m.owned_root else ""
# Grab OS Icon
try:
os_icon = HackTheBox.OS_ICONS[m.os.lower()]
except KeyError:
os_icon = HackTheBox.OS_ICONS["other"]
# Create scaled difficulty rating. Highest rated is full. Everything
# else is scaled appropriately.
max_ratings = max(m.ratings)
if max_ratings == 0:
ratings = m.ratings
else:
ratings = [float(r) / max_ratings for r in m.ratings]
difficulty = ""
for i, r in enumerate(ratings):
difficulty += rating_color[i] + rating_char[round(r * 6)]
difficulty += Style.RESET_ALL + style
# "$" for user and "#" for root
owned = f"^{'$' if m.owned_user else ' '} {'#' if m.owned_root else ' '}"
# Display time left/terminating/resetting/off etc
if m.spawned and not m.terminating and not m.resetting:
state = m.expires
elif m.terminating:
state = "terminating"
elif m.resetting:
state = "resetting"
else:
state = "off"
# Show an astrics and highlight state in blue for assigned machine
if m.assigned:
state = Fore.BLUE + state + Fore.RESET
assigned = f"{Fore.BLUE}*{Style.RESET_ALL} "
else:
assigned = " "
table.append(
[
f"{style}{m.id}",
assigned,
f"{os_icon} {m.os}",
m.name,
m.ip,
difficulty,
f"{m.rating:.1f}",
owned,
state,
]
)
# print data
self.ppaged("\n".join(util.build_table(table)))
def _machine_start(self, args: argparse.Namespace):
""" Start a machine """
m = args.machine
a = self.cnxn.assigned
if m.spawned and self.cnxn.assigned is None:
self.poutput(
f"{m.name}: machine already running, transferring ownership..."
)
m.assigned = True
return
elif m.spawned and self.cnxn.assigned is not None:
self.poutput(
f"unable to transfer machine. {self.cnxn.assigned.name} is currently assigned."
)
return
self.psuccess(f"starting {m.name}")
try:
m.spawned = True
except RequestFailed as e:
self.perror(f"request failed: {e}")
def _machine_reset(self, args: argparse.Namespace) -> None:
""" Stop an active machine """
m = args.machine
if not m.spawned:
self.poutput(f"{m.name}: not running")
return
self.psuccess(f"{m.name}: scheduling reset")
m.resetting = True
def _machine_stop(self, args: argparse.Namespace) -> None:
""" Stop an active machine """
if not args.machine.spawned:
self.poutput(f"{args.machine.name} is not running")
return
self.psuccess(f"scheduling termination for {args.machine.name}")
args.machine.spawned = False
def _machine_info(self, args: argparse.Namespace) -> None:
""" Show detailed machine information
NOTE: This function is gross. I'm not sure of a cleaner way to build
the pretty graphs and tables than manually like this. I need to
research some other python modules that may be able to help
"""
# Shorthand for args.machine
m = args.machine
if m.spawned and not m.resetting and not m.terminating:
state = f"{Fore.GREEN}up{Fore.RESET} for {m.expires}"
elif m.terminating:
state = f"{Fore.RED}terminating{Fore.RESET}"
elif m.resetting:
state = f"{Fore.YELLOW}resetting{Fore.RESET}"
else:
state = f"{Fore.RED}off{Fore.RESET}"
if m.retired:
retiree = f"{Fore.YELLOW}retired{Fore.YELLOW}"
else:
retiree = f"{Fore.GREEN}active{Fore.RESET}"
try:
os_icon = HackTheBox.OS_ICONS[m.os.lower()]
except KeyError:
os_icon = HackTheBox.OS_ICONS["other"]
output = []
output.append(
f"{Style.BRIGHT}{Fore.GREEN}{m.name}{Fore.RESET} - {Style.RESET_ALL}{m.ip}{Style.BRIGHT} - {Style.RESET_ALL}{os_icon}{Style.BRIGHT} {Fore.CYAN}{m.os}{Fore.RESET} - {Fore.MAGENTA}{m.points}{Fore.RESET} points - {state}"
)
output.append("")
output.append(f"{Style.BRIGHT}Difficulty{Style.RESET_ALL}")
output.extend(["", "", "", "", ""])
# Lookup tables for creating the difficulty ratings
rating_char = [
"\u2581",
"\u2582",
"\u2583",
"\u2584",
"\u2585",
"\u2586",
"\u2587",
"\u2588",
]
rating_color = [*([Fore.GREEN] * 3), *([Fore.YELLOW] * 4), *([Fore.RED] * 3)]
# Create scaled difficulty rating. Highest rated is full. Everything
# else is scaled appropriately.
max_ratings = max(m.ratings)
ratings = [round((float(r) / max_ratings) * 40) for r in m.ratings]
difficulty = ""
for i, r in enumerate(ratings):
for row in range(1, 6):
if r > (5 - row) * 8 and r <= (5 - row + 1) * 8:
output[-6 + row] += rating_color[i] + rating_char[r % 8] * 3
elif r > (5 - row) * 8:
output[-6 + row] += rating_color[i] + rating_char[7] * 3
else:
output[-6 + row] += " "
output.append(
f"{Fore.GREEN}Easy {Fore.YELLOW} Medium {Fore.RED} Hard{Style.RESET_ALL}"
)
output.append("")
output.append(
f"{Style.BRIGHT}Rating Matrix ({Fore.CYAN}maker{Fore.RESET}, {Style.DIM}{Fore.GREEN}user{Style.RESET_ALL}{Style.BRIGHT}){Style.RESET_ALL}"
)
output.extend(["", "", "", "", ""])
column_widths = [6, 8, 6, 8, 6]
for i in range(5):
for row in range(5):
content = f"{'MMAA':^{column_widths[i]}}"
if (m.matrix["maker"][i] * 4) >= ((row + 1) * 8):
content = content.replace("MM", f"{Fore.CYAN}{rating_char[7]*2}")
elif (m.matrix["maker"][i] * 4) > (row * 8):
content = content.replace(
"MM",
f"{Fore.CYAN}{rating_char[round(m.matrix['maker'][i]*4) % 8]*2}",
)
else:
content = content.replace("MM", " " * 2)
if (m.matrix["aggregate"][i] * 4) >= ((row + 1) * 8):
content = content.replace(
"AA",
f"{Style.DIM}{Fore.GREEN}{rating_char[7]*2}{Style.RESET_ALL}",
)
elif (m.matrix["aggregate"][i] * 4) > (row * 8):
content = content.replace(
"AA",
f"{Style.DIM}{Fore.GREEN}{rating_char[round(m.matrix['maker'][i]*4) % 8]*2}{Style.RESET_ALL}",
)
else:
content = content.replace("AA", " " * 2)
output[-1 - row] += content
output.append(
f"{'Enum':^{column_widths[0]}}{'R-Life':^{column_widths[1]}}{'CVE':^{column_widths[2]}}{'Custom':^{column_widths[3]}}{'CTF':^{column_widths[4]}}"
)
output.append("")
user_width = max([6, len(m.blood["user"]["name"]) + 2])
output.append(
f"{Style.BRIGHT} {'User':<{user_width}}Root{Style.RESET_ALL}"
)
output.append(
f"{Style.BRIGHT}Owns {Style.RESET_ALL}{Fore.YELLOW}{m.user_owns:<{user_width}}{Fore.RED}{m.root_owns}{Style.RESET_ALL}"
)
output.append(
f"{Style.BRIGHT}{'Blood':<6}{Style.RESET_ALL}{m.blood['user']['name']:<{user_width}}{m.blood['root']['name']}"
)
if len(m.services):
output.append("")
table = [["Port", "Protocol", "Name", "Version"]]
for service in m.services:
table.append(
[f"{service.port}", f"{service.protocol}", service.name, ""]
)
output.extend(util.build_table(table))
else:
output.append("")
output.append(f"{Style.BRIGHT}No enumerated services.{Style.RESET_ALL}")
self.ppaged("\n".join(output))
def _machine_own(self, args: argparse.Namespace) -> None:
""" Submit a machine own (user or root) """
if args.machine.submit(args.flag, difficulty=args.rate):
self.psuccess(f"correct flag for {args.machine.Name}!")
else:
self.perror(f"incorrect flag")
def _machine_cancel(self, args: argparse.Namespace) -> None:
""" Cancel pending termination or reset """
if len(args.cancel) == 0 or "t" in args.cancel:
if args.machine.terminating:
args.machine.terminating = False
self.psuccess(f"{args.machine.name}: pending termination cancelled")
if len(args.cancel) == 0 or "r" in args.cancel:
if args.machine.resetting:
args.machine.resetting = False
self.psuccess(f"{args.machine.name}: pending reset cancelled")
def wait_for_machine(self, machine: Machine) -> bool:
# Start the machine if we don't have a machine assigned
self.pwarning(f"starting {machine.name}")
machine.spawned = True
# Ensure the device is up before we scan
self.pwarning(f"waiting for machine to respond to ping...")
try:
# Wait for a positive ping response
while True:
if (
subprocess.call(
["ping", "-c", "1", machine.ip],
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL,
)
== 0
):
self.psuccess(f"received ping response!")
break
time.sleep(5)
except KeyboardInterrupt:
self.perror("no ping response received. cancelling enumeration.")
return False
else:
# Give the machine some time to start services after networking comes up
self.poutput(f"waiting for services to start...")
time.sleep(10)
# Ensure we grab the newest machine status
self.cnxn.invalidate_cache()
return True
def _machine_enum(self, args: argparse.Namespace) -> None:
""" Perform initial service enumeration """
if not args.machine.spawned:
if self.cnxn.assigned is not None:
# We can't assign this machine, if we already have a machine assigned.
self.pwarning(
f"unable to start {args.machine.name}. {self.cnxn.assigned.name} is already assigned."
)
return
elif not self.wait_for_machine(args.machine):
return
if args.machine.analysis_path is None:
self.pwarning("initializing analysis structure")
try:
args.machine.init(self.cnxn.analysis_path)
# except OSError as e:
# self.perror(f"failed to create directory structure: {e}")
# return
except EtcHostsFailed:
self.perror("failed to add host to /etc/hosts")
return
if len(args.machine.services) == 0 or args.force:
self.poutput("enumerating machine services")
try:
args.machine.enumerate()
except MasscanFailed:
self.perror("masscan failed")
except NmapFailed:
self.perror("nmap failed")
else:
self.poutput(
f"{args.machine.name} already enumerated ({len(args.machine.services)} service(s) detected)"
)
def _machine_scan(self, args: argparse.Namespace) -> None:
""" Scan the open service for the given machine """
# args.machine shorthand
m = args.machine
if not m.spawned:
if self.cnxn.assigned is not None:
self.perror(
f"unable to start {m.name}. {self.cnxn.assigned.name} is currently assigned."
)
return
elif not self.wait_for_machine(m):
return
if args.recommended:
scanners = [s for s in AVAILABLE_SCANNERS if s.recommended and s.match(m)]
elif args.scanner:
scanners = [s for s in AVAILABLE_SCANNERS if s.name == args.scanner]
else:
scanners = [s for s in AVAILABLE_SCANNERS if s.match(m)]
if args.service:
port = int(args.service.split("/")[0])
protocol = args.service.split("/")[1]
services = [
s for s in m.services if s.port == port and s.protocol == protocol
]
else:
services = m.services
if len(services) == 0:
self.perror("no matching services found")
return
# Get scanners that match a service specified/present
if args.recommended:
scanners = [
s
for s in scanners
if s.recommended and any([s.match_service(svc) for svc in services])
]
elif args.scanner:
scanners = [
s
for s in scanners
if s.name == args.scanner
and any([s.match_service(svc) for svc in services])
]
else:
scanners = [
s for s in scanners if any([s.match_service(svc) for svc in services])
]
if len(scanners) == 0:
self.perror(f"no matching scanners found")
return
# Iterate over all scanners and services to run the correct scans
for service in services:
for scanner in scanners:
if not scanner.match_service(service):
continue
self.poutput(
f"beginning {scanner.name} scan on {service.port}/{service.protocol} ({service.name})"
)
tracker = m.scan(scanner, service, silent=args.background)
if args.background:
# Transfer control of the scan to the `jobs` command
tracker.events = self.job_events
tracker.lock.release()
self.jobs.append(tracker)
else:
# Monitor the scan progress in the forground, and give
# options to cancel or background the scan
self.monitor_scan(tracker)
def monitor_scan(self, tracker: Tracker) -> None:
""" Monitor a foreground scan """
# Setup local event queue for completion
events = queue.Queue()
tracker.events = events
tracker.lock.release()
# Exception used when C-z is pressed to background a task
class GoToSleep(Exception):
pass
def background_me(signo, stack):
""" Transfer running task to background thread """
# Turn off the signal handler
signal.signal(signal.SIGTSTP, signal.SIG_DFL)
raise GoToSleep
try:
# Register C-z handler
signal.signal(signal.SIGTSTP, background_me)
try:
tracker = tracker.events.get()
except KeyboardInterrupt:
self.pwarning(
f"cancelling {tracker.scanner.name} for {tracker.service.port}/{tracker.service.protocol}"
)
tracker.stop = True
# Restore previous signal
signal.signal(signal.SIGTSTP, signal.SIG_DFL)
except GoToSleep:
with tracker.lock:
self.pwarning(
f"backgrounding {tracker.scanner.name} for {tracker.service.port}/{tracker.service.protocol}"
)
tracker.silent = True
tracker.events = self.job_events
self.jobs.append(tracker)
# Argument parser for `machine` command
lab_parser = Cmd2ArgumentParser(description="View and manage lab VPN connection")
@cmd2.with_argparser(lab_parser)
@cmd2.with_category("Hack the Box")
def do_lab(self, args: argparse.Namespace) -> bool:
""" Execute the various lab sub-commands """
actions = {
"status": self._lab_status,
"switch": self._lab_switch,
"config": self._lab_config,
"connect": self._lab_connect,
"disconnect": self._lab_disconnect,
"import": self._lab_import,
}
actions[args.action](args)
return False
def _lab_status(self, args: argparse.Namespace) -> None:
""" Print the lab VPN status """
lab = self.cnxn.lab
output = []
output.append(
f"{Style.BRIGHT}Server: {Style.RESET_ALL}{Fore.CYAN}{lab.name}{Fore.RESET} ({lab.hostname}:{lab.port})"
)
if lab.active:
output.append(
f"{Style.BRIGHT}Status: {Style.RESET_ALL}{Fore.GREEN}Connected{Fore.RESET}"
)
output.append(
f"{Style.BRIGHT}IPv4 Address: {Style.RESET_ALL}{Style.DIM+Fore.GREEN}{lab.ipv4}{Style.RESET_ALL}"
)
output.append(
f"{Style.BRIGHT}IPv6 Address: {Style.RESET_ALL}{Style.DIM+Fore.MAGENTA}{lab.ipv6}{Style.RESET_ALL}"
)
output.append(
f"{Style.BRIGHT}Traffic: {Style.RESET_ALL}{Fore.GREEN}{lab.rate_up}{Fore.RESET} up, {Fore.CYAN}{lab.rate_down}{Fore.RESET} down"
)
else:
output.append(
f"{Style.BRIGHT}Status: {Style.RESET_ALL}{Fore.RED}Disconnected{Fore.RESET}"
)
self.poutput("\n".join(output))
def _lab_switch(self, args: argparse.Namespace) -> None:
""" Switch labs """
try:
self.cnxn.lab.switch(args.lab)
except RequestFailed as e:
self.perror(f"failed to switch: {e}")
else:
self.psuccess(f"lab switched to {args.lab}")
def _lab_config(self, args: argparse.Namespace) -> None:
""" Download OVPN configuration file """
try:
self.poutput(self.cnxn.lab.config.decode("utf-8"), apply_style=False)
except AuthFailure:
self.perror("authentication failure (did you supply email/password?)")
def _lab_connect(self, args: argparse.Namespace) -> None:
""" Connect to the Hack the Box VPN using NetworkManager """
# Attempt to grab the VPN if it exists, and import it if it doesn't
connection, uuid = self._nm_import_vpn(name="python-htb", force=False)
if connection is None:
# nm_import_vpn handles error output
return
# Check if this connection is active on any devices
for active_connection in NetworkManager.NetworkManager.ActiveConnections:
if active_connection.Uuid == uuid:
self.poutput(f"vpn connection already active")
return
# Activate the connection
for device in NetworkManager.NetworkManager.GetDevices():
# Attempt to activate the VPN on each wired and wireless device...
# I couldn't find a good way to do this intelligently other than
# trying them until one worked...
if (
device.DeviceType == NetworkManager.NM_DEVICE_TYPE_ETHERNET
or device.DeviceType == NetworkManager.NM_DEVICE_TYPE_WIFI
):
try:
active_connection = NetworkManager.NetworkManager.ActivateConnection(
connection, device, "/"
)
if active_connection is None:
self.perror("failed to activate vpn connection")
return
except dbus.exceptions.DBusException:
continue
else:
break
else:
self.perror("vpn connection failed")
return
# Wait for VPN to become active or transition to failed
while (
active_connection.VpnState
< NetworkManager.NM_VPN_CONNECTION_STATE_ACTIVATED
):
time.sleep(0.5)
if (
active_connection.VpnState
!= NetworkManager.NM_VPN_CONNECTION_STATE_ACTIVATED
):
self.perror("vpn connection failed")
return
self.psuccess(
f"connected w/ ipv4 address: {active_connection.Ip4Config.Addresses[0][0]}/{active_connection.Ip4Config.Addresses[0][1]}"
)
def _lab_disconnect(self, args: argparse.Namespace) -> None:
""" Disconnect from Hack the Box VPN via Network Manager """
if "lab" not in self.config or "connection" not in self.config["lab"]:
self.perror('lab vpn configuration not imported (hint: use "lab import")')
return
for c in NetworkManager.NetworkManager.ActiveConnections:
if c.Uuid == self.config["lab"]["connection"]:
NetworkManager.NetworkManager.DeactivateConnection(c)
self.psuccess("vpn connection deactivated")
break
else:
self.poutput("vpn connection not active or not found")
def _lab_import(self, args: argparse.Namespace) -> None:
""" Import OpenVPN configuration into NetworkManager """
# Import the connection
c, uuid = self._nm_import_vpn(args.name, force=args.reload)
# "nm_import_vpn" handles error/warning output
if c is None:
return
self.psuccess(f"imported vpn configuration w/ uuid {uuid}")
def _nm_import_vpn(self, name, force=True) -> NetworkManager.Connection:
""" Import the VPN configuration with the specified name """
# Ensure we aren't already managing a connection
try:
c, uuid = self._nm_get_vpn_connection()
if force:
c.Delete()
else:
return c, uuid
except ConnectionNotFound:
pass
except InvalidConnectionID:
self.pwarning("invalid connection id found in configuration; removing.")
# We need to download and import the OVPN configuration file
with tempfile.NamedTemporaryFile() as ovpn:
# Write the configuration to a file
ovpn.write(self.cnxn.lab.config)
ovpn.flush()
# Import the connection w/ Network Manager CLI
p = subprocess.run(
["nmcli", "c", "import", "type", "openvpn", "file", ovpn.name],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
if p.returncode != 0:
self.perror("failed to import vpn configuration")
self.perror(
"tip: try importing the config manually and fixing any network manager issues:\n\tnmcli connection import type openvpn file {your-ovpn-file}"
)
self.perror("nmcli stderr output:\n" + p.stderr.decode("utf-8"))
return None, None
# Parse the UUID out of the output
try:
uuid = p.stdout.split(b"(")[1].split(b")")[0].decode("utf-8")
except:
self.perror("unexpected output from nmcli")
self.perror(
"tip: try importing the config manually and fixing any network manager issues:\n\tnmcli connection import type openvpn file {your-ovpn-file}"
)
self.perror("nmcli stderr output:\n" + p.stderr.decode("utf-8"))
self.perror("nmcli stdout output:\n" + p.stdout.decode("utf-8"))
return None, None
try:
# Grab the connection object
connection = NetworkManager.Settings.GetConnectionByUuid(uuid)
# Ensure the routing settings are correct
connection_settings = connection.GetSettings()
connection_settings["connection"]["id"] = name
connection_settings["ipv4"]["never-default"] = True
connection_settings["ipv6"]["never-default"] = True
connection.Update(connection_settings)
except dbus.exceptions.DBusException as e:
self.perror(f"dbus error during connection lookup: {e}")
return None, None
# Save the uuid in our configuration file
self.config["lab"] = {}
self.config["lab"]["connection"] = uuid
with open(self.config_path, "w") as f:
self.config.write(f)
return connection, uuid
def _nm_get_vpn_connection(self) -> NetworkManager.Connection:
""" Grab the NetworkManager VPN configuration object """
if "lab" not in self.config or "connection" not in self.config["lab"]:
raise ConnectionNotFound
try:
# Grab the connection
c = NetworkManager.Settings.GetConnectionByUuid(
self.config["lab"]["connection"]
)
except dbus.exceptions.DBusException as e:
raise InvalidConnectionID(str(e))
return c, self.config["lab"]["connection"]
@cmd2.with_category("Hack the Box")
def do_invalidate(self, args) -> None:
""" Invalidate API cache """
self.cnxn.invalidate_cache()
def complete_machine(
self, running=None, active=None, term_or_reset=None
) -> List[cmd2.argparse_custom.CompletionItem]:
""" Return a list of CompletionItems for machines """
result = []
for m in self.cnxn.machines:
# Match active
if active is not None and m.expired == active:
continue
# Match running
if running is not None and m.spawned != running:
continue
# Match terminating or resetting
if term_or_reset is not None and not m.terminating and not m.resetting: