-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusbhound.py
247 lines (217 loc) · 7.31 KB
/
usbhound.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
import winreg
import re
import argparse
import hashlib
parser = argparse.ArgumentParser(prog="usbhound.py",description="Gives you the list of storage devices that has been connected to your device")
parser.add_argument("-v",help="verbose",action="store_true")
parser.add_argument("-o",help="save the output to a file ",action="store",type=str,default="")
parser.add_argument("-e",help="Outputs extra information",action="store_true")
parser.add_argument("-s",help="Saves the hash value of the output file",action="store_true")
args = parser.parse_args()
def store(*argv):
global output
for op in argv:
output+=op
print(op,end=" ")
print("")
output+="\n"
def hash_file(filename):
hash = hashlib.sha256()
with open(filename,'rb') as file:
chunk = 0
while chunk != b'':
chunk = file.read(1024)
hash.update(chunk)
return hash.hexdigest()
output=""
hashing = args.s
file=args.o
users=[]
paths=[]
dev=[]
extra_dev=[]
extra_flag=True
def sniff():
global file
global output
global hashing
global file
global users
global paths
global dev
global extra_dev
global extra_flag
subkey=r"SYSTEM\CurrentControlSet\Enum\USBSTOR"
try:
if file != "":
fhandle=open(file,"w")
except:
print("Couldnt open file\nExitting...")
quit()
try:
if args.v:
store("\n\nAccessing the registry hive : HKEY_LOCAL_MACHINE")
store(r"Accessing the subkey : SYSTEM\CurrentControlSet\Enum\USBSTOR")
hive_handle = winreg.ConnectRegistry(None,winreg.HKEY_LOCAL_MACHINE)
path = winreg.OpenKey(hive_handle,subkey)
except OSError:
store("\nCould not open the given hive")
quit()
#FINDING BASIC INFO
try:
for i in range(winreg.QueryInfoKey(path)[0]):
curr_dev={'ven':'','prod':'','ver':'','serial_no':'','vid':'','pid':'','vol':'','letter':'','alt_name':[],'guid':'','curr_user':True,'users':[]}
value=winreg.EnumKey(path,i)
temp_path = subkey+"\\"+value
paths.append(temp_path)
path = winreg.OpenKey(hive_handle,subkey)
curr_dev['ven']=re.findall('Ven_(.+?)&',value)[0]
curr_dev['prod']=re.findall('Prod_(.+?)&',value)[0]
curr_dev['ver']=re.findall('Rev_(.*)?',value)[0]
dev.append(curr_dev)
except OSError:
store("Some Error Occured while accessing the registry")
store("\n",str(i+1)," Devices identified.\n")
#FINDING SERIAL NUMBERS
j=0
try:
for i in paths:
temp_path = winreg.OpenKey(hive_handle,i)
dev[j]['serial_no'] = re.findall('(.+?)&',winreg.EnumKey(temp_path,0))[0]
j+=1
except OSError:
store("Error Occured while accessing the serial numbers")
winreg.CloseKey(path)
winreg.CloseKey(temp_path)
subkey =r"SYSTEM\CurrentControlSet\Enum\USB"
path = winreg.OpenKey(hive_handle,subkey)
if args.v:
store(r"Accessing the subkey : SYSTEM\CurrentControlSet\Enum\USB")
for i in range(winreg.QueryInfoKey(path)[0]):
try:
value=winreg.EnumKey(path,i)
temp_path=winreg.OpenKey(hive_handle,subkey+"\\"+value)
for j in range(len(dev)):
if(winreg.EnumKey(temp_path,0)==dev[j]['serial_no']):
dev[j]['vid'] = re.findall('VID_(.+?)&',value)[0]
dev[j]['pid'] = re.findall('PID_(....)',value)[0]
except OSError:
store("Some Error Occured while accessing the registry")
winreg.CloseKey(path)
winreg.CloseKey(temp_path)
subkey = r"SOFTWARE\Microsoft\Windows Portable Devices\Devices"
try:
if args.v:
store(r"Accessing the subkey : SOFTWARE\Microsoft\Windows Portable Devices\Devices")
path = winreg.OpenKey(hive_handle,subkey)
for i in range(winreg.QueryInfoKey(path)[0]):
extra_flag=True
for j in dev:
value=(winreg.EnumKey(path,i))
temp_path=winreg.OpenKey(hive_handle,subkey+"\\"+value)
info=winreg.EnumValue(temp_path,0)
if re.search(j['serial_no'],value):
j['vol']=info[1]
extra_flag=False
break
temp=re.findall("{(.*)}",value)
if extra_flag:
if temp!=[]:
extra_dev.append([re.findall(".*-.*-.*-.*-(.*)",temp[0])[0],info[1]])
else:
extra_dev.append(["",info[1]])
except OSError:
store("Error Occured while accessing the registry")
winreg.CloseKey(path)
winreg.CloseKey(temp_path)
subkey = r"SYSTEM\MountedDevices"
try:
if args.v:
store(r"Accessing the subkey : SYSTEM\MountedDevices")
path = winreg.OpenKey(hive_handle,subkey)
for i in range(winreg.QueryInfoKey(path)[1]):
value=winreg.EnumValue(path,i)[1]
value=str(value)
data = ''.join([str(i) for i in re.findall(r".*?x00(.)",value)])
drive=winreg.EnumValue(path,i)[0]
for j in dev:
if j['serial_no'] != "":
if re.search(j['serial_no'],data):
temp=re.findall(r"(.):",drive)
if temp !=[] :
j['letter']=temp[0]
else:
if j['letter']=="":
j['letter']="N/A"
guid = re.findall("{(.*)}",drive)
if guid !=[]:
j['guid'] = guid[0]
for k in extra_dev:
if re.search(k[0].upper(),drive.upper()):
if k[0] != "":
j['alt_name'].append(k[1])
extra_dev.remove(k)
except OSError:
store("Error Occured while accessing the registry")
winreg.CloseKey(path)
#CURRENT USER
if args.v:
store("Accessing the registry hive : HKEY_CURRENT_USER")
store(r"Accessing the subkey : Software\Microsoft\Windows\CurrentVersion\Explorer\Mountpoints2")
hive_handle_user = winreg.ConnectRegistry(None,winreg.HKEY_CURRENT_USER)
subkey = r"Software\Microsoft\Windows\CurrentVersion\Explorer\Mountpoints2"
path = winreg.OpenKey(hive_handle_user,subkey)
for i in range(winreg.QueryInfoKey(path)[1]):
for j in dev:
guid = re.findall("{(.*)}")
if guid != []:
if guid[0] == j['guid']:
print("guid :: ",guid[0])
j['curr_user']=True
store("\n")
for i in dev:
store("---------------------------------------------")
store("Vendor :: ",i['ven'])
store("Product :: ",i['prod'])
store("Version :: ",i['ver'])
store("Serial Number :: ",i['serial_no'])
store("Product ID :: ",i['vid'])
store("Vendor ID :: ",i['pid'])
store("Drive Letter :: ",i['letter'])
store("GUID :: ",i['guid'])
if i['vol'] =="":
store("Volume Name :: ","N/A")
else:
store("Volume Name :: ",i['vol'])
store("Alternate Volume name :: ",str(i['alt_name']))
store("Used by current user :: ",str(i['curr_user']),"\n")
if args.e:
if extra_dev == []:
store("-------------------------------------------------")
store("No extra information to display")
store("-------------------------------------------------")
else:
store("-------------------------------------------------")
store("These devices were also connected on this device")
store("-------------------------------------------------")
for i in extra_dev:
store(str(i[1]))
if file != "":
try:
if args.v:
print("\n------------------------------------------------------------------")
print("Outputting to the file :: ",file)
fhandle.write(output)
fhandle.close()
if hashing:
hash=hash_file(file)
file=file+".sha256"
if args.v:
print("Storing the hash to the file :: ",file)
print("------------------------------------------------------------------")
fhandle=open(file,"w")
fhandle.write(hash)
fhandle.close()
except:
print("Couldnt open file\n")
sniff()