-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
181 lines (147 loc) · 6.52 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
#!/usr/bin/python3
import argparse
import logging
import os
import pickle
import sys
import time
import helpers
import restore as rest
import sandbox.core as sand
import sandbox.whonix as who
import usb.core
logger = logging.getLogger(__name__)
logging.basicConfig()
logger.setLevel(logging.DEBUG)
def main():
path = os.path.dirname(os.path.realpath(__file__))
parser = argparse.ArgumentParser(usage="%(prog)s [options]")
parser.add_argument("--verbose", "-v",
action="store_true",
default=False,
help="Display verbose information"
)
parser.add_argument("--restore", "-r",
action="store_true",
default=False,
help="Restore system to its original state (bringing network interfaces back up \
removing usb device from allowed usbguard list)"
)
parser.add_argument("--sandbox", "-s",
type=str,
action="store",
nargs='+',
required=True,
help="Specify the name or uuid of your virtual box"
)
parser.add_argument("--interfaces", "-i",
type=str,
action="store",
nargs='+',
help="Specify interface names to disconnect from"
)
parser.add_argument("--whonix", "-w",
action="store_true",
default=False,
help="Specify the whonix flag to mount the USB device in your whonix workstation \
(needs whonix gateway as well)"
)
args = parser.parse_args()
verbose = args.verbose
restore = args.restore
sandbox_id = args.sandbox
interfaces = args.interfaces
whonix = args.whonix
if restore:
rest.restore_changes()
sys.exit()
# check if usbguard is installed
if not usb.core.USBGuard.check_if_installed():
logger.error(f"\n\nPlease download and install usbguard from https://github.com/USBGuard/usbguard\n\n")
raise SystemExit
usb_objects = list()
# while no usb device is attached
while len(usb_objects) <= 0:
usb_devices = helpers.lookup_usb_devices()
if verbose:
logger.debug(usb_devices)
key_list = list()
[key_list.append(k) for k, v in usb_devices.items() for i in v if i.find("MassStorage") != -1]
usb_objects = [usb.core.USB(device_id=usb_devices[k][0],
vendor_id=usb_devices[k][1],
product_id=usb_devices[k][2],
serial=usb_devices[k][3]) for k in key_list]
if verbose:
for obj in usb_objects:
logger.debug(obj.vendor_id)
logger.debug(obj.product_id)
logger.debug(obj.device_id)
logger.debug(obj.serial)
print("\n\nWaiting for usb mass storage device to connect...\n\n")
time.sleep(5)
NoneType = type(None)
if not isinstance(interfaces, NoneType):
network_interfaces = interfaces
else:
network_interfaces = helpers.get_network_interfaces()
try:
network_interfaces.remove("lo")
network_interfaces = [x for x in network_interfaces if not x.startswith("docker")]
except ValueError:
logger.error("Couldn't ignore loopback interface - interface not found.")
pass
print(f"\n\nFound {len(network_interfaces)} network interfaces: {network_interfaces}")
for interface in network_interfaces:
logger.debug(f"Disconnecting {interface} interface...")
usb.core.USB.connect_disconnect_network_interfaces("disconnect", interface)
# make changes persistent and note them in the pickle logfile (to be able to --restore)
with open('interfaces.pickle', 'ab') as f:
pickle.dump(network_interfaces, f, pickle.HIGHEST_PROTOCOL)
if verbose:
logger.debug(f"Sandbox ID/name given: {sandbox_id}")
# initialise sandbox objects with uuid or name
try:
if whonix:
if helpers.check_if_sandbox_uuid(str(sandbox_id[0])):
sandbox = who.Whonix(uuid=sandbox_id[0], device_ids=[x.device_id for x in usb_objects])
else:
sandbox = who.Whonix(name=sandbox_id[0], device_ids=[x.device_id for x in usb_objects])
if helpers.check_if_sandbox_uuid(str(sandbox_id[1])):
gateway = who.Whonix(name=sandbox_id[1], device_ids=[x.device_id for x in usb_objects])
else:
gateway = who.Whonix(uuid=sandbox_id[1], device_ids=[x.device_id for x in usb_objects])
else:
gateway = None
if helpers.check_if_sandbox_uuid(str(sandbox_id)):
sandbox = sand.Sandbox(uuid=sandbox_id, device_ids=[x.device_id for x in usb_objects])
else:
sandbox = sand.Sandbox(name=sandbox_id, device_ids=[x.device_id for x in usb_objects])
except IndexError:
logger.error("You need to specify both a sandbox uuid/name and a gateway uuid/name with the --whonix flag!")
raise SystemExit
# Before running VirtualBox check if current user is in the vboxuser group in order to use USB devices
if not helpers.check_user_is_in_vboxgroup():
logger.error(f"Please add your user to the vboxuser group using the following command: \n\n"
f"sudo usermod -a -G vboxusers $USER\n\n")
raise SystemExit
if verbose:
logging.debug("Name: ", sandbox.name)
logging.debug("UUID: ", sandbox.uuid)
if whonix:
gateway.run_sandbox()
sandbox.run_sandbox()
usb_uuids = helpers.get_usb_uuid(usb_objects)
if verbose:
print("Looked up USB uuids from device_id: ", usb_uuids)
sandbox.mount_usb_to_sandbox(usb_uuids)
print(f"\n\nSuccessfully mounted USB to {sandbox.sandbox_id}.")
# If Sandbox VM is closed before USB device gets removed -> block device on host using usbguard to avoid automount
try:
while helpers.is_vm_running(sandbox.sandbox_id):
# print(f"VM {sandbox.sandbox_id} is still up and running.")
time.sleep(1)
finally:
usbguard = usb.core.USBGuard(device_ids=[x.device_id for x in usb_objects])
usbguard.block_device()
if __name__ == '__main__':
main()