-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathusbdevices.sh
1194 lines (1112 loc) · 37.3 KB
/
usbdevices.sh
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
#!/bin/bash
###############################################
# Utility to present information about #
# previously connected USB devices #
# in a clear and related manner. #
# Jacky Fox 2012 #
# Dependancies regdump.pl #
###############################################
#############################################
# variable settings #
#############################################
regdump_location="/usr/local/bin/regdump.pl"
expert_log_location="usb_expert.log" # location of unfiltered output for evidential purposes
log_location="usb.log" # location of sorted and related usb log
device_counter=1
long_name_1="not blank"
vendorid_database="usb.ids"
months=( dummy Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec ) # array for epoch conversion
i=0
x=0
bslash='\'
crlf="\r\n"
lfcr="\n\r"
lfcr=`echo -e $lfcr`
linefeed=$'\n'
##########################################
# command line help function #
##########################################
# comment out lines for unrequired hives/files
cmd_line_help_func() {
echo "usage : "$0" -S ../hives/SYSTEM "
echo " -S (path and name of System hive) -S /hives/system"
#echo " -N (path and name of ntuser.dat hive) -N hives/NTUSER.DAT"
echo " -A (path and name of setupapi.log file) -A ../hives/setupapi.dev.log"
echo " -W (path and name of Software hive) -W /samples/software"
echo " -D (default location of hives) -D myhives"
#echo " -C (path and name of Security hive) -C ../hives/SECURITY"
#echo " -M (path and name of the SAM) -M ../hives/SAM"
echo " -E (switch off expert logs) -E off"
echo " -L (give explicit path for logs) -L /logdir"
echo " -K (explicit path to .lnk files -K myhives/linkfiles"
echo " default location myhives/lnk)"
exit
}
#################################################
# Check current control set number #
# HKLM\SYSTEM\Select #
# Assigns: $ccs #
#################################################
current_control_set_func () {
temp_string=`perl $regdump_location $system_hive_location "Select" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" > $expert_log_location # first entry so new file ie >
fi
if [ -z "$temp_string" ] # if this is empty
then
ccs="Not available (check SYSTEM hive location)"
else
ccs=`echo ${temp_string##*Current (REG_DWORD) = 0x}` #cut out leading info
ccs=`echo ${ccs:5:3}` #cut off trailing info
fi
}
###################################################################
# Get the computer name from #
# HKLM\SYSTEM\CurrentControlSet\Control\ComputerName\ComputerName #
# Assigns: $comp_name #
###################################################################
pull_out_compname_func () {
temp_string=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\ComputerName\ComputerName" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location # append entries to existing file ie >>
fi
if [ -z "$temp_string" ]
then
echo Unable to extract computer name check SYSTEM hive location
else
comp_name=`echo "${temp_string##*ComputerName (REG_SZ) = }"` # cut off leading info
comp_name=`echo ${comp_name%%$linefeed*}` # truncate at linefeed
fi
}
#################################################################
# Get the windows version from #
# HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProductName #
# Assigns: $win_ver #
# $wpd #
#################################################################
pull_out_winver_func () {
temp_string=`perl $regdump_location $software_hive_location "Microsoft\Windows NT\CurrentVersion" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ]
then
echo Unable to extract windows version check software hive location
else
win_ver=`echo "${temp_string##*ProductName (REG_SZ) = }"`
win_ver=`echo ${win_ver%%$linefeed*}`
fi
echo $win_ver | grep -q "Windows 7\|Windows Vista" # check if windows is >= Vista, if it is windows
if [ $? = "0" ] # portable devices may contain additional last drive
then # assignations and mountdev searches on s/n not parentid_
wpd="yes"
fi
}
##############################################
# Pull out the information from usbstor #
# This requires getting a long device name #
# and checking for the serial number(s) #
# beneath it #
# HKLM\System\CurrentControlSet\Enum\USBSTOR #
# Assigns : $long_name_[$i] #
# $serial_num_[$i] #
# $friendly_name_[$i] #
# $parentid_pf_[$i] #
# $parentid_unicode_[$i] #
# $sn_unicode_[$i] #
# unicode values are used to search mounted #
# devices later for guids & drive assignments#
##############################################
pull_out_usbstor_func () {
temp_string=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\USBSTOR" -v 2>/dev/null`
if [ $? != "0" ]
then
echo "There are no usb devices associated with this system"
exit
fi
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ] # if this is empty
then
echo="Check SYSTEM hive location"
else
i=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't change
do
temp1=$temp_string
temp_string=`echo ${temp_string#*..?}` # cut as far as device long name
if [ "$temp1" != "$temp_string" ]
then
long_name_[$i]=`echo ${temp_string%% *}` # take id as far as next space
temp_string_sn=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\USBSTOR"$bslash${long_name_[$i]} -v` # get the serial number(s)
if [ "$logging" != "off" ]
then
echo "$temp_string_sn" >> $expert_log_location
fi
temp2=$temp_string_sn
num_sns=`echo "$temp_string_sn" | grep -c '\.\.'` # how many serial numbers
while [ "$num_sns" -gt "0" ]
do
temp_string_sn=$temp2
temp_string_sn=`echo ${temp_string_sn#*..?}`
temp2=`echo ${temp_string_sn# *}`
serial_num_[$i]=`echo ${temp_string_sn%% *}`
temp_string_sn=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\USBSTOR"$bslash${long_name_[$i]}$bslash${serial_num_[$i]} -v` # get the serial number
if [ "$logging" != "off" ]
then
echo "$temp_string_sn" >> $expert_log_location
fi
string_empty=`echo $temp_string_sn | grep -c "FriendlyName"`
if [ $string_empty != 0 ]
then
friendly_name_[$i]=`echo "${temp_string_sn##*FriendlyName (REG_SZ) = }"`
friendly_name_[$i]=`echo ${friendly_name_[$i]%%$linefeed*}`
else
friendly_name_[$i]=" "
fi
if [ "$wpd" != "yes" ] # if it's xp
then
string_empty=`echo $temp_string_sn | grep -c "ParentIdPrefix"`
if [ $string_empty != 0 ]
then
parentid_pf_[$i]=`echo "${temp_string_sn##*ParentIdPrefix (REG_SZ) = }"`
parentid_pf_[$i]=`echo ${parentid_pf_[$i]%%$linefeed*}`
#### conversion to unicode ####
parentid__utf16_[$i]=`echo ${parentid_pf_[$i]} |od -x`
x=5 # x tracks position of 2 char hex code
while [ "$x" -gt "0" ]
do
(( x=x+5 ))
newchar=${parentid__utf16_[$i]:$x:2} # high char
if [ "$newchar" != "0a" ]
then
parentid_unicode_[$i]=${parentid_unicode_[$i]}$newchar" 00 "
newchar=${parentid__utf16_[$i]:($x-2):2} # low char
if [ "$newchar" != "0a" ]
then
parentid_unicode_[$i]=${parentid_unicode_[$i]}$newchar" 00 "
else
x=0 #stop the loop
fi
else
x=0 #stop the loop
fi
done
else
parentid_pf_[$i]=" "
fi
fi
#### convert serial # to unicode to get guids also if >=vista for parentid_s ####
sn_utf16_[$i]=`echo ${serial_num_[$i]} |od -x`
x=5 # x tracks position of 2 char hex code
while [ "$x" -gt "0" ]
do
(( x=x+5 ))
newchar=${sn_utf16_[$i]:$x:2} # high char
if [ "$newchar" = "00" ]
then
(( x=x+3 )) #skip the info at left side
else
if [ "$newchar" != "0a" ]
then
sn_unicode_[$i]=${sn_unicode_[$i]}$newchar" 00 "
newchar=${sn_utf16_[$i]:($x-2):2} # low char
if [ "$newchar" = "00" ]
then
(( x=x+3 )) #skip the info at left side
else
if [ "$newchar" != "0a" ]
then
sn_unicode_[$i]=${sn_unicode_[$i]}$newchar" 00 "
else
x=0 #stop the loop
fi
fi
else
x=0 #stop the loop
fi
fi
done
if [ "$wpd" = "yes" ] # if it's vista or >
then
parentid_unicode_[$i]=${sn_unicode_[$i]} # mountd needs s/n not parentid_
fi
(( num_sns-- ))
(( i++ ))
if [ "$num_sns" -gt "0" ] # more s/n? set longname & loop
then
long_name_[$i]=${long_name_[$i-1]}
fi
done
fi
done
((device_counter=i-1))
fi
}
#################################################################
# Get product id and vendor id from #
# HKLM\System\CurrentControlSet\Enum\USB\Vid_9999&Pid_9999 #
# HKLM\System\CurrentControlSet\Enum\ACPI #
# Assigns : $pid_num[$i] #
# $vid_num[$i] #
# $enumusb_last_insertion_[$i] #
# $vid_name[$i] #
# possibly vendor name from id if usb.id can be found #
#################################################################
pull_out_pidvid_func () {
#### first setup an array of all vid, pids, serial numbers and times ####
temp_string=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\USB" -v`
## get the epoch time of the Enum key to compare to other enum tree values to check if they are device specific ##
if [ "$wpd" = "yes" ]
then
temp_enum=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\ACPI" -v`
usbtime=`echo "${temp_enum%%\]*}"`
usbtime=`echo "${usbtime##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
if [ "${usbtime:5:1}" = "0" ] # date command gets upset with 08 & 09 ??
then
mon=${usbtime:6:1}
else
mon=${usbtime:5:2}
fi
epoch_usb=`date +%s -d ${months[$mon]}" "${usbtime:8:2}", "${usbtime:0:4}" "${usbtime:11:8}`
fi
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ] # if this is empty
then
echo="Check SYSTEM hive location"
else
x=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't changed then on last entry
do
temp1=$temp_string
temp_string=`echo ${temp_string#*..?}` # cut as far as next vidpid
if [ "$temp1" != "$temp_string" ]
then
pidvid[$x]=`echo ${temp_string%% *}` # take id as far as next space
vid[$x]=${pidvid[$x]:4:4}
pid[$x]=${pidvid[$x]:13:4}
tempy=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\USB"$bslash${pidvid[$x]} -v`
if [ "$logging" != "off" ]
then
echo "$tempy" >> $expert_log_location
fi
tempy1="blank"
while [ "${tempy}" != "${tempy1}" ]
do
tempy=`echo ${tempy#*..?}` # cut as far as the serial num
if [ "${tempy1}" != "${tempy}" ]
then
echo $tempy | grep "{" > /dev/null
if [ "$?" = "0" ]
then
# no subkeys
tempy1=$tempy
(( x++ ))
else
tempy1=`echo ${tempy%% *}` # take away any trailing info
pvsn[$x]=${tempy1}
pvtime[$x]=`perl $regdump_location $system_hive_location "ControlSet$ccs\Enum\USB"$bslash${pidvid[$x]}$bslash${pvsn[$x]} -v`
if [ "$logging" != "off" ]
then
echo "${pvtime[$x]}" >> $expert_log_location
fi
pvtime[$x]=`echo "${pvtime[$x]%%\]*}"`
pvtime[$x]=`echo "${pvtime[$x]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
y=$x
(( x++ ))
vid[$x]=${vid[$y]}
pid[$x]=${pid[$y]}
pidvid[$x]=${pidvid[$y]}
fi
fi
done
fi
done
((num_vidpid=x-1))
fi
#### then loop to search for matching serial numbers and assign vids, pids and Enum\USB last accessed date ####
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
x=0
while [ $x -lt $num_vidpid ]
do
(( x++ ))
if [ "${serial_num_[$i]%&*}" = "${pvsn[$x]}" ]
then
enumusb_last_insertion_[$i]=${pvtime[$x]}
if [ "$wpd" = "yes" ]
then
if [ "${pvtime[$x]:5:1}" = "0" ] # date command gets upset with 08 & 09 ??
then
mon=${pvtime[$x]:6:1}
else
mon=${pvtime[$x]:5:2}
fi
epoch_pvt=`date +%s -d ${months[$mon]}" "${pvtime[$x]:8:2}", "${pvtime[$x]:0:4}" "${pvtime[$x]:11:8}`
diff=$(( $epoch_pvt - $epoch_usb ))
if [[ $diff -lt "20" && $diff -gt "-20" ]] # if there are less than 20 seconds then value may not be device specific
then
enumusb_last_insertion_[$i]=${enumusb_last_insertion_[$i]}" Time may not be device specific"
fi
fi
pid_num[$i]=${pid[$x]}
vid_num[$i]=${vid[$x]}
if [ -f $vendorid_database ]
then
vid_lower=`echo ${vid_num[$i]} | tr '[:upper:]' '[:lower:]'`
vid_name[$i]=`cat $vendorid_database | grep -n $vid_lower`
vid_name[$i]=`echo "${vid_name[$i]##*:$vid_lower}"`
vid_name[$i]=`echo ${vid_name[$i]%%$linefeed*}`
vid_num[$i]=${vid_num[$i]}" ("${vid_name[$i]}")"
fi
x=$num_vidpid
fi
done
done
}
######################################################################
# Add last insertion date from deviceclasses to array from guids #
# HKLM\System\CurrentControlSet\Control\DeviceClasses\{53f56307....} #
# Assigns: $last_insertion_[$i] #
######################################################################
match_devclass_307_func () {
temp_string=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\DeviceClasses\{53f56307-b6bf-11d0-94f2-00a0c91efb8b}" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ] # if this is empty
then
echo="Check SYSTEM hive location"
else
x=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't changed then on last entry
do
temp1=$temp_string
temp_string=`echo ${temp_string#*..?}` # cut as far as id
if [ "$temp1" != "$temp_string" ]
then
devclass_sn[$x]=`echo ${temp_string%% *}` # take id as far as next space
devclass_sn[$x]=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\DeviceClasses\{53f56307-b6bf-11d0-94f2-00a0c91efb8b}"$bslash${devclass_sn[$x]} -v`
if [ "$logging" != "off" ]
then
echo "${devclass_sn[$x]}" >> $expert_log_location
fi
dctime[$x]=`echo "${devclass_sn[$x]%%\]*}"`
dctime[$x]=`echo "${dctime[$x]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
dcsn[$x]=`echo ${devclass_sn[$x]%%\#\{*}` # cut out trailing info
dcsn[$x]=`echo ${dcsn[$x]##*\#}` # cut out leading info
(( x++ ))
fi
done
((num_devclass_sn=x-1))
fi
#### loop to search for matching serial numbers and assign last accessed date ####
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
x=0
while [ $x -lt $num_devclass_sn ]
do
(( x++ ))
if [ "${serial_num_[$i]}" = "${dcsn[$x]}" ]
then
last_insertion_[$i]=${dctime[$x]}
x=$num_devclass_sn
fi
done
done
}
######################################################################
# Add last insertion date from deviceclasses to array from guids #
# HKLM\System\CurrentControlSet\Control\DeviceClasses\{53f5630d....} #
# Assigns: $last_insertion2_[$i] #
######################################################################
match_devclass_30d_func () {
temp_string=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\DeviceClasses\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ] # if this is empty
then
echo="Check SYSTEM hive location"
else
x=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't changed then on last entry
do
temp1=$temp_string
temp_string=`echo ${temp_string#*..?}` # cut as far as id
if [ "$temp1" != "$temp_string" ]
then
devclass_sn[$x]=`echo ${temp_string%% *}` # take id as far as next space
devclass_sn[$x]=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\DeviceClasses\{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}"$bslash${devclass_sn[$x]} -v`
if [ "$logging" != "off" ]
then
echo "${devclass_sn[$x]}" >> $expert_log_location
fi
dctime[$x]=`echo "${devclass_sn[$x]%%\]*}"`
dctime[$x]=`echo "${dctime[$x]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
dcsn[$x]=`echo ${devclass_sn[$x]%%\#\{*}` # cut out trailing info
dcsn[$x]=`echo ${dcsn[$x]##*\#}` # cut out leading info
(( x++ ))
fi
done
((num_devclass_sn=x-1))
fi
#### loop to search for matching serial numbers and assign last accessed date ####
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
x=0
while [ $x -lt $num_devclass_sn ]
do
(( x++ ))
if [ "${serial_num_[$i]}" = "${dcsn[$x]}" ]
then
last_insertion2_[$i]=${dctime[$x]}
x=$num_devclass_sn
fi
done
done
}
######################################################################
# Add last insertion date from deviceclasses to array from guids #
# HKLM\System\CurrentControlSet\Control\DeviceClasses\{a5dcbf10....} #
# Assigns : $last_insert_dc_a5_[$i] #
######################################################################
match_devclass_a5_func () {
temp_string=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\DeviceClasses\{a5dcbf10-6530-11d2-901f-00c04fb951ed}" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ] # if this is empty
then
echo="Check SYSTEM hive location"
else
x=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't changed then on last entry
do
temp1=$temp_string
temp_string=`echo ${temp_string#*..?}` # cut as far as id
if [ "$temp1" != "$temp_string" ]
then
devclass_sn[$x]=`echo ${temp_string%% *}` # take id as far as next space
devclass_sn[$x]=`perl $regdump_location $system_hive_location "ControlSet$ccs\Control\DeviceClasses\{a5dcbf10-6530-11d2-901f-00c04fb951ed}"$bslash${devclass_sn[$x]} -v`
if [ "$logging" != "off" ]
then
echo "${devclass_sn[$x]}" >> $expert_log_location
fi
dctime[$x]=`echo "${devclass_sn[$x]%%\]*}"`
dctime[$x]=`echo "${dctime[$x]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
dcsn[$x]=`echo ${devclass_sn[$x]%%\#\{*}` # cut out trailing info
dcsn[$x]=`echo ${dcsn[$x]##*\#}` # cut out leading info
(( x++ ))
fi
done
((num_devclass_sn=x-1))
fi
#### loop to search for matching serial numbers and assign last accessed date ####
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
x=0
while [ $x -lt $num_devclass_sn ]
do
(( x++ ))
if [ "${serial_num_[$i]%%&?}" = "${dcsn[$x]}" ] # the dcsn serial number has no &? eg &0 on the end
then
last_insert_dc_a5_[$i]=${dctime[$x]}
x=$num_devclass_sn
fi
done
done
}
######################################################################
# Add last test date from EMDMgmt to array from guids #
# HKLM\Software\Microsoft\Windows NT\CurrentVersion\EMDMgmt #
# The first time listed is the time of an attempted test #
# If a second time is listed it shows that the test completed #
# Assigns: $last_testtime_[$i] #
# $volume_serial_num_[$i] #
# $volume_sn_hex_[$i] #
######################################################################
match_last_testtime_func () {
temp_string=`perl $regdump_location $software_hive_location "Microsoft\Windows NT\CurrentVersion\EMDMgmt" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ] # if this is empty
then
echo="Check SOFTWARE hive location"
else
x=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't changed then on last entry
do
temp1=$temp_string
temp_string=`echo "${temp_string#*..?}"` # cut as far as id
if [ "$temp1" != "$temp_string" ]
then
emdmgmt_sn[$x]=`echo "${temp_string%%$linefeed*}"` # take id as far as next linefeed
# do not search volumes other than usbstor devices vista has kanji codes here
if [ "${emdmgmt_sn[$x]:4:7}" = "USBSTOR" ]
then
vol_sn[$x]=`echo ${emdmgmt_sn[$x]##*_}`
emdmgmt_sn[$x]=`perl $regdump_location $software_hive_location "Microsoft\Windows NT\CurrentVersion\EMDMgmt"$bslash"${emdmgmt_sn[$x]}" -v`
if [ "$logging" != "off" ]
then
echo "${emdmgmt_sn[$x]}" >> $expert_log_location
fi
ltt=`echo ${emdmgmt_sn[$x]##*LastTestedTime (REG_QWORD) = }`
ltt[$x]=`echo ${ltt:21:2}${ltt:18:2}${ltt:15:2}${ltt:12:2}${ltt:9:2}${ltt:6:2}${ltt:3:2}${ltt:0:2}`
if [ ${ltt[$x]} != "0000000000000000" ]
then
ltt[$x]=`echo $((0x${ltt[$x]}/10000000-11644473600))` #convert wintime to unix time
ltt[$x]=`date -d @${ltt[$x]}` #convert unixtime
else
ltt[$x]=" " # if last tested time is zeros just put in a space
fi
emtime[$x]=`echo "${emdmgmt_sn[$x]%%\]*}"`
emtime[$x]=`echo "${emtime[$x]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
emtime[$x]=${emtime[$x]}" "${ltt[$x]} # add contents (if any)of last tested time
emsn[$x]=`echo ${emdmgmt_sn[$x]%%\#\{*}` # cut out trailing info
emsn[$x]=`echo ${emsn[$x]##*\#}` # cut out leading info
(( x++ ))
fi
fi
done
((num_emdmgmt_sn=x-1))
fi
#### loop to search for matching serial numbers and assign last accessed date ####
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
x=0
while [ $x -lt $num_emdmgmt_sn ]
do
(( x++ ))
if [ "${serial_num_[$i]}" = "${emsn[$x]}" ]
then
last_testtime_[$i]=${emtime[$x]}
volume_serial_num_[$i]=${vol_sn[$x]}
volume_sn_hex_[$i]=`echo "ibase=A;obase=16;${volume_serial_num_[$i]}" | bc`
x=$num_emdmgmt_sn
fi
done
done
}
###################################################################
# Add info from Mountdev to array by stripping MountedDevices #
# output as far as a unicode device serial number and checking for#
# an associated drive letter if Vista or later do the same with #
# windows portable devices #
# HKLM\System\MountedDevices #
# HKLM\Software\Microsoft\Windows Portable Devices\Devices(vista+)#
# Assigns : $drive_letter_[$i] (can have multiple entries) #
# $vol_id_[$i] (the guid) #
###################################################################
match_mountdev_func () {
mountdev_output=`perl $regdump_location $system_hive_location "MountedDevices" -v`
if [ "$logging" != "off" ]
then
echo "$mountdev_output" >> $expert_log_location
fi
guidmd_output=$mountdev_output # leave a string intact for matching guids later
num_dosd=`echo "$mountdev_output" | grep -c "DosDevices"`
x=0
while [ $x -lt $num_dosd ]
do
(( x++ ))
mountdev_output=`echo "${mountdev_output#*DosDevices}"`
mt_drive_letter[$x]=`echo "${mountdev_output:1:1}"`
drive_data[$x]=`echo "${mountdev_output#*(REG_BINARY) = }"`
drive_data[$x]=`echo -e "${drive_data[$x]%%\\\\*}"` # to hunt for a \ you need \\\\
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
if [ -n "${parentid_unicode_[$i]}" ] # if a parent id code exists check for a match in mtdev data
then
hit_parentid=`echo ${drive_data[$x]} | grep -c "${parentid_unicode_[$i]}"`
if [ "$hit_parentid" = "1" ]
then
drive_letter_[$i]=${mt_drive_letter[$x]}": (mountdev)"
i=$device_counter
fi
fi
done
done
#### checking mountdev for guids, search for s/ns under given long entries ####
num_guids=`echo "$guidmd_output" | grep -c "Volume"`
x=0
while [ $x -lt $num_guids ]
do
(( x++ ))
guidmd_output=`echo "${guidmd_output#*Volume}"`
guid[$x]=`echo "${guidmd_output:0:38}"`
guid_data[$x]=`echo "${guidmd_output%%\\\\*}"`
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
if [ -n "${sn_unicode_[$i]}" ] # if a serial num exists check for a match in mtdev data
then
if [ "$wpd" = "yes" ]
then
hit_sn=`echo ${guid_data[$x]} | grep -c "${sn_unicode_[$i]}"`
else
if [ -n "${parentid_unicode_[$i]}" ] # make sure parentid_ not blank or it will match everything
then
hit_sn=`echo ${guid_data[$x]} | grep -c "${parentid_unicode_[$i]}"`
fi
fi
if [ "$hit_sn" = "1" ]
then
vol_id_[$i]=${guid[$x]}
i=$device_counter
hit_sn=0
fi
fi
done
done
##### If > Vista checking wpd for last drive assignations ####
if [ "$wpd" = "yes" ]
then
#### make a 2d array containing all the drives letters allocated & s/n(s) ####
temp_string=`perl $regdump_location $software_hive_location "Microsoft\Windows Portable Devices\Devices" -v`
if [ "$logging" != "off" ]
then
echo "$temp_string" >> $expert_log_location
fi
if [ -z "$temp_string" ]
then
echo="Check SYSTEM hive location"
else
i=1
temp1="blank"
while [ "$temp1" != "$temp_string" ] # check to see if string doesn't change
do
temp1=$temp_string
temp_string=`echo ${temp_string#*..?}` # cut as far as next key
if [ "$temp1" != "$temp_string" ]
then
port_dev[$i]=`echo ${temp_string%% *}` # take id as far as next space
temp_string_pd[$i]=`perl $regdump_location $software_hive_location "Microsoft\Windows Portable Devices\Devices"$bslash${port_dev[$i]} -v` # get the specific portable device(s)
if [ "$logging" != "off" ]
then
echo "${temp_string_pd[$i]}" >> $expert_log_location
fi
string_empty=`echo ${temp_string_pd[$i]} | grep -c "FriendlyName"`
if [ $string_empty != 0 ]
then
pd_time[$i]=`echo "${temp_string_pd[$i]%%\]*}"`
pd_time[$i]=`echo "${pd_time[$i]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
pd_fname_[$i]=`echo "${temp_string_pd[$i]##*FriendlyName (REG_SZ) = }"`
pd_fname_[$i]=`echo ${pd_fname_[$i]%%$linefeed*}`
pd_fname_[$i]=${pd_fname_[$i]}" "${pd_time[$i]}
else
pd_fname_[$i]=" "
fi
fi
(( i++ ))
done
num_of_pdentries=$i
fi
#### check the serial numbers in portable devices with main array proper to allocate drive assignations ####
x=0
while [ $x -lt $num_of_pdentries ]
do
(( x++ ))
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
sn=`echo ${serial_num_[$i]} | tr '[:lower:]' '[:upper:]'` # need to convert case to upper to get matches
hit_sn=`echo ${temp_string_pd[$x]} | grep -c "$sn"`
if [ "$hit_sn" = "1" ]
then
if [ -z "${drive_letter_[$i]}" ]
then
drive_letter_[$i]=${pd_fname_[$x]}
else
add_if_diff=`echo ${drive_letter_[$i]} | grep -c "${pd_fname_[$x]}"`
if [ "$add_if_diff" -lt "1" ] # if already in string don't add it
then # again two types ## or ?? entries in Vista
drive_letter_[$i]=${drive_letter_[$i]}"\n\r\t\t: "${pd_fname_[$x]}
fi # the \n etc above are for screen formatting
fi
i=$device_counter
fi
done
done
fi
}
#############################################
# get first install date and time of usb #
# from setupapi log files #
#############################################
usb_first_install_func () {
if [ "$logging" != "off" ]
then
if [ -f $setupapi_location ]
then
cat $setupapi_location >> $expert_log_location
fi
fi
if [ "$wpd" = "yes" ] #if its >= Vista
then
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
firstbit=`grep "${serial_num_[$i]}\|Section start" $setupapi_location` # get the lines we want
firstbit=`grep -A 2 "${serial_num_[$i]}]" $setupapi_location` # get the lines we want
firstbit=`echo "${firstbit##*Section start }"` # cut out leading info
if [ "${firstbit:0:1}" != "" ]
then
first_installed_[$i]=`echo "${firstbit:0:23} (Local Time)"` # cut out trailing info
else
first_installed_[$i]="not recorded in the given setupapi.log"
fi
done
else
#### the xp bit ####
i=0
while [ $i -lt $device_counter ]
do
(( i++ ))
setupapi_data=`cat $setupapi_location`
caps_sn=`echo ${serial_num_[$i]} | tr '[:lower:]' '[:upper:]'`
string_empty=`grep -c $caps_sn $setupapi_location`
if [ $string_empty != 0 ]
then
setupapi_data=`grep -A 1 $caps_sn $setupapi_location` # -A gives you the next line too with date
setupapi_data=`echo "${setupapi_data##*[}"` # cut out up to [
first_installed_[$i]=`echo "${setupapi_data:0:19} (Local Time)"`
else
first_installed_[$i]="not recorded in the given setupapi.log"
fi
done
fi
}
##################################################################################
# Check for mountpoint2 matches in ntuser.dats (if they exist) and username #
# HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2 #
# HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer (xp) #
# HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders (vista+) #
# Assigns: $username_[$i] #
##################################################################################
ntuser_mp2_func () {
#### check to see if there are any ntuser.dats in the default dir ####
ntulist=`find $default_location -iname ntuser*`
num_user_hives=0
while [ -n "$ntulist" ]
do
(( num_user_hives++ ))
ntu_hive[$num_user_hives]=`echo "${ntulist%%$linefeed*}"`
ntulist=`echo "${ntulist#*${ntu_hive[$num_user_hives]}}"`
ntulist=`echo "${ntulist#*$linefeed}"`
done
#### if there are userhives inspect mountpoint2 keys for device volume guids ####
if [ $num_user_hives -gt "0" ]
then
#### populate mp2[?] for each hive ####
x=0
while [ $x -ne $num_user_hives ]
do
(( x++ ))
mp2[$x]=`perl $regdump_location ${ntu_hive[$x]} "Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2" -v 2>/dev/null`
if [ "$logging" != "off" ]
then
echo "${mp2[$x]}" >> $expert_log_location
fi
done
i=0 # number of usbdevices
x=0 # number of userhives
while [ $i -ne $device_counter ]
do
(( i++ ))
if [ "${vol_id_[$i]:0:1}" = "{" ]
then
x=0
#### search thru the mounpoint2s in all hives for it ####
while [ $x -ne $num_user_hives ]
do
(( x++ ))
echo ${mp2[$x]} | grep ${vol_id_[$i]} > /dev/null
if [ $? = "0" ]
then
temp[$i]=`perl $regdump_location ${ntu_hive[$x]} "Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2"$bslash${vol_id_[$i]} -v`
if [ "$logging" != "off" ]
then
echo "${temp[$i]}" >> $expert_log_location
fi
mp2_time[$i]=`echo "${temp[$i]%%\]*}"`
mp2_time[$i]=`echo "${mp2_time[$i]##*\[}" | tr "T" " " | tr "Z" " "`"(UTC)"
#### check if xp or vista + to get username from hive ####
if [ "$wpd" != "yes" ]
then # it's xp
mp2_uname[$i]=`perl $regdump_location ${ntu_hive[$x]} "Software\Microsoft\Windows\CurrentVersion\Explorer" -v`
if [ "$logging" != "off" ]
then
echo "${mp2_uname[$i]}" >> $expert_log_location
fi
mp2_uname[$i]=`echo "${mp2_uname[$i]##*Logon User Name (REG_SZ) = }"`
mp2_uname[$i]=`echo "${mp2_uname[$i]%%$linefeed*}"`
else # it's vista+
mp2_uname[$i]=`perl $regdump_location ${ntu_hive[$x]} "Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" -v`
if [ "$logging" != "off" ]
then
echo "${mp2_uname[$i]}" >> $expert_log_location
fi
mp2_uname[$i]=`echo "${mp2_uname[$i]##*CD Burning (REG_SZ) = }"`
mp2_uname[$i]=`echo "${mp2_uname[$i]#*C:?Users?}"`
mp2_uname[$i]=`echo "${mp2_uname[$i]%%\\\\*}"`
fi
mp2_uname[$i]=${mp2_uname[$i]}" "${mp2_time[$i]}
#### this is to check in case multiple users used the same device ?####
if [ -z "${username_[$i]}" ]
then
username_[$i]=${mp2_uname[$i]}
else
username_[$i]=${username_[$i]}"\n\r\t\t: "${mp2_uname[$i]}
# the \n etc above are for screen formatting
fi
fi
done
fi
done
else
echo "No user hives found in default directory usb devices can't be associated with usernames"
fi
}
#########################################################
# Check for .lnk files and any volume id associations #
# Assigns: $assoc_lnks[$i] #
#########################################################
link_file_check () {
temp=`ls $lnkpath`
p=$IFS
IFS=$'\n'
for thefile in `echo "$temp"`
do
hexfile=`xxd -u -ps $lnkpath"/"$thefile`
## loop thru all the volumeids to search for a match
i=0
while [ "$i" -lt "$device_counter" ]
do
(( i++ ))
if [ "${volume_serial_num_[$i]}" != "" ]
then
endian_sn=${volume_sn_hex_[$i]:6:2}${volume_sn_hex_[$i]:4:2}${volume_sn_hex_[$i]:2:2}${volume_sn_hex_[$i]:0:2}
hit=`echo $hexfile | grep -c "$endian_sn"`
if [ $hit -gt "0" ]
then
if [ -z ${assoc_lnks[$i]} ]
then
assoc_lnks[$i]=$thefile
else
assoc_lnks[$i]=${assoc_lnks[$i]}"\n\r\t\t: "$thefile
fi
i=$device_counter