-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb-nuker.sh
executable file
·600 lines (549 loc) · 16.9 KB
/
usb-nuker.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
#!/bin/bash
#---------------------------------------------------------------#
# usb-nuker.sh #
# Description: #
# Menu driven script made to both wipe copious amounts #
# of USB devices and/or nuke them with a specified #
# system image. Do not plug in any of the USB devices #
# you want to modify before starting this script. #
# #
# Author: Aaron Beckett #
# Date: 02/12/2015 #
#---------------------------------------------------------------#
echo
echo "*** NOTE ***:"
echo "To ensure proper function of this program, do not remove or"
echo "insert any USB devices while this script is running unless"
echo "prompted to do so."
echo "---------------------------------------------------------------"
#---------------------------------------------------------------#
# INITIALIZE VARIABLES #
#---------------------------------------------------------------#
#
## Initialize temporary directory and temp files used for registering inserted USB devices
#
mytmpdir=$(mktemp -d /tmp/nuker.XXXXXX) || { echo "Failed to create temp dir"; exit 1; }
mylist=$(mktemp $mytmpdir/list1) || { echo "Failed to create temp file"; exit 1; }
mylist2=$(mktemp $mytmpdir/list2) || { echo "Failed to create temp file"; exit 1; }
mylist3=$(mktemp $mytmpdir/list3) || { echo "Failed to create temp file"; exit 1; }
myimg="$$.img" # can't make an image file in tmp because then we couldn't use "open" to mount it when checking its validity. Don't know why
# Set the temporary files to delete whenever the program exits
trap "rm -fr $mytmpdir" EXIT
trap "rm -f $myimg" EXIT
#
## Initialize global variables
#
declare -a list=()
declare -a excluded=()
declare -a targets=()
imgpath=""
valid_img=0
target_status="i"
#
## Set the mode of execution based on the CL arguments
#
interactive_mode=0
quick_nuke_mode=0
quick_wipe_mode=0
if [ $# -gt 0 ]; then
for op in $@; do
if [ "$op" == "-n" -o "$op" == "--quick-nuke" ]; then
quick_nuke_mode=1
elif [ "$op" == "-w" -o "$op" == "--quick-wipe" ]; then
quick_wipe_mode=1
else
echo "*** ERROR ***: Invalid command line argument."
echo "'$op' is not a valid argument."
exit 1
fi
done
else
interactive_mode=1
fi
#---------------------------------------------------------------#
# FUNCTIONS #
#---------------------------------------------------------------#
#------------------------------------------
# Name: get_usb_list
# Description:
# Stores a list of the USB devices currently plugged
# into the machine into the "list" array variable.
##
get_usb_list ()
{
system_profiler SPUSBDataType | grep "BSD Name" | egrep -v s[0-9]\$ | awk '{print $3}' > $mylist
list=( `cat "$mylist" `)
}
#------------------------------------------
# Name: find_eligible_usbs
# Description:
# Stores a list of the USB devices plugged in and NOT part of
# the "excluded" array into the "list" array variable.
##
find_eligible_usbs ()
{
get_usb_list
for safe_usb in "${excluded[@]}"; do
list=( ${list[@]/"$safe_usb"/} )
done
}
#------------------------------------------
# Name: validate_img
# Description:
# Checks a file to see if it is a valid disk image.
##
validate_img ()
{
echo "Validating selected disk image..."
valid_img=0
if [ "$imgpath" != "" ]; then
# Try to mount the file and see if it succeeds
diskutil list | egrep /dev/disk > $mylist
open $imgpath
sleep 3 # This is needed to wait for the disk to spin up
diskutil list | egrep /dev/disk > $mylist2
grep -f $mylist -v $mylist2 | sed 's/[/]dev[/]*//g' > $mylist3
list=( `cat "$mylist3" `)
if [ ${#list[@]} -eq 1 ]; then
disk=${list[0]}
part="/dev/$disk"
part+="s1"
{
diskutil unmountDisk $disk
fsck_msdos $part
} > /dev/null
if [ $? -eq 0 ]; then
diskutil eject $disk > /dev/null
echo "Selected disk image is valid."
valid_img=1
else
echo
echo "*** ERROR ***: There was a problem validating the disk image."
echo -e "The file at:\n\t$imgpath"
echo "is not a valid FAT file system."
valid_img=0
fi
else
echo
echo "*** ERROR ***: There was a problem validating the disk image."
echo -e "The file at:\n\t$imgpath"
echo "cannot be mounted as a disk."
valid_img=0
fi
else
echo
echo "*** No disk image has been selected. ***"
fi
}
#------------------------------------------
# Name: select_img
# Description:
# Asks the user to select a disk image or read one off a
# USB drive. Stores the path to the image in the "imgpath"
# variable.
##
select_img ()
{
selected=0
while [ $selected -eq 0 ]; do
echo
echo "Would you like to:"
echo " 1. Choose an image file from your computer, or"
echo " 2. Copy an image from a master USB?"
echo " 3. Go to Main Menu"
read -p "Option: " choice
echo
if [ "$choice" == "1" ]; then
############ Specify path of a .img file ############
read -p "Enter full path name of the .img file: " imgpath
echo
# If the path points to an existing file, check to make sure the
# file is a valid disk image
if [ -f "$imgpath" ]; then
validate_img
if [ $valid_img -eq 1 ]; then
selected=1
fi
fi
elif [ "$choice" == "2" ]; then
############ Find Master USB and get the master image ############
rm -f $myimg
read -p "Insert USB with valid disk image then press [Enter]..." -s
echo
echo "Registering master USB..."
sleep 3 # sleep for 3 seconds to ensure the usb drive has spun up
# Get list of usb devices added that are not part of 'excluded' or 'targets'
get_usb_list
for usb in "${excluded[@]}" "${targets[@]}"; do
list=( ${list[@]/"$usb"/} )
done
if [ ${#list[@]} -eq 1 ]; then
master=${list[0]}
echo "Master USB registered."
echo "Copying disk image from master USB..."
diskutil unmountDisk $master &> /dev/null
dd if=/dev/r$master of=$myimg bs=1024k
imgpath="$myimg"
diskutil mountDisk $master &> /dev/null
echo "Disk image copied to temporary file."
echo
diskutil eject $master
read -p "Remove the master USB device then press [Enter]..." -s
echo
# Validate disk image pulled
validate_img
if [ $valid_img -eq 1 ]; then
selected=1
fi
else
echo
echo "*** NOTE ***: You inserted the wrong number of USB devices."
echo " You must insert one and only one USB device while registering"
echo " the master device. Safely eject any devices you just inserted"
echo " and try again."
fi
elif [ "$choice" == "3" ]; then
selected=1
else
echo
echo "*** Invalid Option ***: Enter '1', '2', or '3'."
fi
done
}
#------------------------------------------
# Name: save_img
# Description:
# Asks user for a location to save the disk image currently
# used by the nuker and saves the image to that location.
##
save_img ()
{
if [ "$imgpath" != "" -a $valid_img -eq 1 ]; then
read -p "Enter filename: " path
echo
# Make sure the path is not an empty string
if [ "$path" == "" ]; then
echo "*** You must enter a path to save the disk image at. ***"
break
fi
# If the path doesn't end with '.img' then append it to the path
if [[ "$path" != *.img ]]; then
path="$path.img"
fi
# If the path does not already refer to an existing file then copy the disk image
if [ ! -f "$path" ]; then
echo "Saving disk image..."
cp $imgpath $path
echo "Disk image saved."
else
echo "*** ERROR ***: Invalid path."
echo "That path already has a file associated with it."
fi
else
echo "*** No valid disk image has been selected. ***"
fi
}
#------------------------------------------
# Name: wipe_targets
# Description:
# Overwrites all USBs in the "targets" array with zeros.
##
wipe_targets ()
{
echo "Clearing all data from USB targets..."
for device in "${targets[@]}"; do
{
diskutil unmount $device
diskutil unmountDisk $device
} &> /dev/null
dd if=/dev/zero of=/dev/r$device bs=1024k &
done
wait
for device in "${targets[@]}"; do
{
diskutil mountDisk $device
} &> /dev/null
done
echo "USB data cleared."
}
#------------------------------------------
# Name: nuke_targets
# Description:
# Nuke all USBs in the "targets" array with the system image.
##
nuke_targets ()
{
if [ "$imgpath" != "" -a $valid_img -eq 1 ]; then
echo "Writing master image to USB targets..."
for device in "${targets[@]}"; do
{
diskutil unmount $device
diskutil unmountDisk $device
} &> /dev/null
dd if=$imgpath of=/dev/r$device bs=1024k &
done
wait
for device in "${targets[@]}"; do
# Test the FAT file system
part="/dev/$device"
part+="s1"
{
diskutil unmountDisk $device
fsck_msdos $part
} > /dev/null
if [ $? -ne 0 ]; then
echo
echo "*** ERROR ***: There was a problem copying the disk image to $device."
echo "$device does not have a valid FAT file system."
echo
else
diskutil mountDisk $device
fi
done
echo "Master image copied to USB targets."
else
echo "*** No valid disk image has been selected. ***"
fi
}
#------------------------------------------
# Name: eject_targets
# Description:
# Eject all USB targets.
##
eject_targets ()
{
if [ ${#targets[@]} -ne 0 ]; then
echo "Ejecting USB targets..."
for device in "${targets[@]}"; do
diskutil eject $device
done
unset targets
echo "Finished ejecting USB targets."
read -p "Remove the ejected devices then press [Enter]..." -s
echo
else
echo "*** There are no USB targets to eject. ***"
fi
}
#---------------------------------------------------------------#
# Initialize list of USB devices #
# #
# Compile list of usb devices currently plugged in so that we #
# can exclude them from being nuked. The names of the safe #
# USBs are stored in an array called "excluded". #
#---------------------------------------------------------------#
get_usb_list
excluded=("${list[@]}")
#################################################################
#################################################################
#---------------------------------------------------------------#
# Run Usb-Nuker #
#---------------------------------------------------------------#
if [ $interactive_mode -ne 1 ]; then
#-----------------------------------------------------------#
# NUCLEAR OPTION #
# Two independent but non-exclusive modes: #
# 1. Quick-wipe-mode: #
# Automatically wipe all USBs inserted as targets. #
# 2. Quick-nuke-mode: #
# Take whatever is on the master USB and copy it #
# automatically to all USB devices registered as #
# targets. #
# #
# NOTE: Running the Quick-Nuker in both modes will first #
# wipe, then nuke the targets. #
#-----------------------------------------------------------#
#
echo "Running the QUICK-NUKER: Press [Ctrl+C] at any time to exit."
echo "---------------------------------------------------------------"
if [ $quick_nuke_mode -eq 1 ]; then
# Prompt user to insert master USB
read -p "Insert Master USB with valid disk image then press [Enter]..." -s
echo
echo "Registering master USB..."
sleep 3 # sleep for 3 seconds to ensure the usb drive has spun up
find_eligible_usbs
if [ ${#list[@]} -ne 1 ]; then
echo
echo "*** ERROR ***: You inserted the wrong number of USB devices."
echo " You must insert one and only one USB device while registering"
echo " the master device. Safely eject any devices you just inserted"
echo " and try again."
exit 1
else
master=${list[0]}
excluded=("${excluded[@]}" "$master")
echo "Master USB registered."
fi
fi
# Prompt user to insert target USBs
echo
read -p "Insert USB devices to target then press [Enter]..." -s
echo
echo "Registering USB devices..."
sleep 5 # sleep for 5 seconds to ensure the usb drivers have spun up
find_eligible_usbs
if [ ${#list[@]} -lt 1 ]; then
echo
echo "*** ERROR ***: No USB devices were registered as targets."
echo "You must insert at least one USB device to target."
echo
echo "*** NOTE ***: You should wait until all inserted USB devices"
echo "have been noticed by the computer before pressing [Enter]."
exit 1
else
targets=("${list[@]}")
echo "USB devices registered as targets."
fi
# Give them one last chance to change their minds...
echo
echo "*** WARNING ***:"
echo "The Quick-Nuker is about to overwrite ALL data on the"
echo "targeted USB devices. This action cannot be undone."
echo
while [ "$proceed" != "y" ]; do
read -p "Would you like to continue? (y/n): " proceed
echo
if [ "$proceed" == "n" ]; then
echo "Exiting USB Nuker..."
exit 0
fi
done
if [ $quick_wipe_mode -eq 1 ]; then
# Wipe all data from USB targets
wipe_targets
fi
if [ $quick_nuke_mode -eq 1 ]; then
# Pull master image from master USB
echo
echo "Copying disk image from master USB..."
diskutil unmountDisk $master &> /dev/null
dd if=/dev/r$master of=$myimg bs=1024k
imgpath="$myimg"
diskutil mountDisk $master &> /dev/null
echo "Master USB copied to temporary file."
# Validate disk image pulled
echo
validate_img
if [ $valid_img -ne 1 ]; then
echo
echo "*** WARNING ***:"
echo "The disk image from the master USB is not a complete FAT disk image."
while [ "$ni" != "y" ]; do
read -p "Would you like to continue anyway? (y/n): " ni
if [ "$ni" == "n" ]; then
echo "Exiting USB Nuker..."
exit 0
fi
done
fi
# Write master image to target USBs
nuke_targets
# Eject the master USB
echo
echo "Ejecting master USB device..."
diskutil eject $master &> /dev/null
echo "Maser USB ejected."
fi
# Eject all target USB devices
eject_targets
else
#-----------------------------------------------------------#
# Select a disk image #
#-----------------------------------------------------------#
select_img
#-----------------------------------------------------------#
# MAIN MENU #
# 1. Wipe USB devices #
# 2. Nuke USBs with master image #
# 3. Wipe USB devices AND nuke them with image #
# 4. Select New Master Image #
# 5. Save Master Image #
# 6. Eject All USB Targets #
# 7. Exit #
#-----------------------------------------------------------#
#
finished=0 # Variable sentry that watches for the user to select exit
## The select command below will make a menu and loop automatically
## but will not display the menu options each time. The while loop
## allows us to force the menu options to display each loop through.
while [ $finished -ne 1 ]; do
echo
echo "MAIN MENU:"
echo "---------------------------------------------------------------"
PS3='Select option: '
options=("Wipe USB devices" "Nuke USBs with master image" "Wipe USB devices AND nuke them with image" "Select New Master Image" "Save Master Image" "Eject All USB Targets" "Exit")
select opt in "${options[@]}"
do
echo
## Determine if we need to select new USB targets
if [ "$opt" == "Wipe USB devices" -o "$opt" == "Nuke USBs with master image" -o "$opt" == "Wipe USB devices AND nuke them with image" ]; then
if [ ${#targets[@]} -ne 0 ]; then
echo "You already have valid USB targets, would you like to:"
echo " (t): Target them again, or"
echo " (i): Insert new targets?"
echo
read -p "(t/i): " target_status
echo
if [ "$target_status" == "i" ]; then
eject_targets
fi
else
target_status="i"
fi
## Compile new list of USB targets if needed
if [ "$target_status" == "i" ]; then
read -p "Insert USB devices to target then press [Enter]..." -s
echo
echo "Registering USB devices..."
sleep 5
find_eligible_usbs
targets=("${list[@]}")
if [ ${#targets[@]} -gt 0 ]; then
echo "USB devices registered."
echo
else
echo "*** WARNING ***: No USB devices were registered."
break
fi
fi
fi
## Take the requested action on the target USBs (or quit)
case $opt in
"Wipe USB devices")
read -p "Are you sure you want to wipe ALL data from ALL targeted USB devices? (y/n): " choice
if [ "$choice" == "y" ]; then
wipe_targets
fi
;;
"Nuke USBs with master image")
read -p "Are you sure you want to overwrite ALL targeted USB devices with the selected disk image? (y/n): " choice
if [ "$choice" == "y" ]; then
nuke_targets
fi
;;
"Wipe USB devices AND nuke them with image")
read -p "Are you sure you want to wipe ALL data from ALL targeted USBs AND overwrite ALL targeted USB devices with the selected disk image? (y/n): " choice
if [ "$choice" == "y" ]; then
wipe_targets
nuke_targets
fi
;;
"Select New Master Image")
select_img
;;
"Save Master Image")
save_img
;;
"Eject All USB Targets")
eject_targets
;;
"Exit")
finished=1
;;
*) echo invalid option;;
esac
break
done
done
fi