-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.py
158 lines (121 loc) · 5.51 KB
/
helpers.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
import re
import subprocess
from collections import defaultdict
from typing import DefaultDict, List
from usb.core import USB
def check_if_sandbox_uuid(sandbox_id: str) -> bool:
"""
Check if sandbox_id is a UUID.
:return: True if sandbox_is is UUID, else if it is a sandbox name -> return false
:rtype: bool
"""
if re.match("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", str(sandbox_id)):
return True
else:
return False
def check_user_is_in_vboxgroup() -> bool:
"""
Check if current user is in vboxgroup.
:return: True if user is in vboxgroup, else False
:rtype: bool
"""
groups = subprocess.Popen(["groups"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = groups.communicate()
groups = stdout.decode('UTF-8').splitlines()
for group in groups:
if group.find("vboxusers") != -1:
return True
return False
def get_network_interfaces() -> List[str]:
"""
Returns a list of network interfaces.
The list of network interfaces is later used to shut down the interfaces
:return: list of network interfaces
:rtype: list[str]
"""
ints = subprocess.Popen(["ip", "link", "show"], stdout=subprocess.PIPE)
ints = subprocess.check_output(["grep",
"-oE", "[0-9]{1,2}:.*: <"], stdin=ints.stdout)
ints = ints.decode('UTF-8').splitlines()
ints = [re.search(": (.*):", x).group(1) for x in ints]
return ints
def is_vm_running(sandbox_id: str) -> bool:
"""
Checks if a the sandbox VM is still up and running.
:param sandbox_id: the sandbox_id is either the name or the uuid of the box, depending on the user's input
:type sandbox_id: str
:return: Returns True if VM is running, else False
:rtype: bool
"""
running_vms = subprocess.Popen(["vboxmanage", "list", "runningvms"],
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
stdout, stderr = running_vms.communicate()
running_vms = stdout.decode('UTF-8').splitlines()
running_vms = [re.search("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", x).group(0)
for x in running_vms]
for uuid in running_vms:
if uuid == sandbox_id:
return True
return False
def get_usb_uuid(usb_objects: List[USB]) -> List[str]:
"""
Finds UUID of USB mass storage based on the device_id stored in the USB objects.
:param usb_objects: List of USB objects
:type usb_objects: list[USB]
:return: List of UUIDS of all attached USB mass storages
:rtype: list[str]
"""
usb_uuids = list()
usbhosts = subprocess.Popen(["vboxmanage", "list", "usbhost"], stdout=subprocess.PIPE)
usbhosts = subprocess.check_output(["grep",
"-e", "UUID: ",
"-e", "VendorId: ",
"-e", "ProductId: "], stdin=usbhosts.stdout)
usbhosts = usbhosts.decode('UTF-8').splitlines()
usbhosts = [x.replace(" ", "") for x in usbhosts]
temp = list()
[temp.append(usbhosts[i] + usbhosts[i + 1] + usbhosts[i + 2]) \
for i in range(0, len(usbhosts)) if usbhosts[i].find('UUID:') != -1]
temp_dict = defaultdict(str)
for x in temp:
uuid = re.search("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}", x).group(0)
vendor_id = re.search("(?<=VendorId:0x)[0-9a-f]{4}", x).group(0)
product_id = re.search("(?<=ProductId:0x)[0-9a-f]{4}", x).group(0)
device_id = vendor_id + ":" + product_id
temp_dict[uuid] = device_id
# lookup uuids from device_id (e.g. 8087:0026 -> e0c6ec26-4ad1-4d55-97c8-f0f35c538e95)
[usb_uuids.append(k) for k, v in temp_dict.items() for obj in usb_objects if obj.device_id == v]
return usb_uuids
def lookup_usb_devices() -> DefaultDict[int, list]:
"""
Query available USB devices that are listed in the "lsusb" command
:return: Returns a dict with an index (key) and USB device information (value) such as device_id, vendor_id,
product_id, serial_number, interface_class
:rtype: DefaultDict[int, list]
"""
grep_keywords = ['idVendor', 'idProduct', 'iSerial', 'bInterfaceClass']
device_id = subprocess.Popen("lsusb", stdout=subprocess.PIPE)
device_id = subprocess.check_output(["grep",
"-oE", "ID [a-f0-9]{4}:[a-f0-9]{4}"], stdin=device_id.stdout)
lsusb_verbose = subprocess.Popen(["lsusb", "-v", ], stdout=subprocess.PIPE)
lsusb_verbose = subprocess.check_output(["grep",
"-e", grep_keywords[0],
"-e", grep_keywords[1],
"-e", grep_keywords[2],
"-e", grep_keywords[3]], stdin=lsusb_verbose.stdout)
device_id = device_id.decode('UTF-8').splitlines()
device_id = [str(x).replace("ID ", "") for x in device_id]
lsusb_verbose = lsusb_verbose.decode('UTF-8').splitlines()
lsusb_verbose = [str(x).replace(" ", "") for x in lsusb_verbose]
usb_devices = defaultdict(list)
idx = -1
for x in lsusb_verbose:
if x.find('idVendor') != -1:
idx = idx + 1
usb_devices[idx].append(device_id[idx])
usb_devices[idx].append(x)
# remove duplicates
for k, v in usb_devices.items():
usb_devices[k] = list(dict.fromkeys(v))
return usb_devices