2021-07-15 22:12:15 +02:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
#variables:
|
2021-07-16 22:10:01 +02:00
|
|
|
random_pass=2
|
2021-07-16 02:10:33 +02:00
|
|
|
zeroing_pass=1
|
2021-07-16 02:34:28 +02:00
|
|
|
prereq_list="pv smartmontools hdparm"
|
2021-07-16 22:07:29 +02:00
|
|
|
YUM_CMD=$(which yum)
|
|
|
|
APT_CMD=$(which apt-get)
|
2021-07-16 02:10:33 +02:00
|
|
|
|
|
|
|
#check that a disk has been provided:
|
|
|
|
if [ -z "$1" ]
|
|
|
|
then
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "USAGE: \"./wiper.sh diskname\", for example: \"./wiper.sh sdb\""
|
2021-07-16 02:10:33 +02:00
|
|
|
exit
|
2021-07-15 22:12:15 +02:00
|
|
|
fi
|
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
#check the disk exists:
|
|
|
|
if [ ! -e "/dev/$1" ]; then
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "ERROR: /dev/$1 does not exists. exiting."
|
2021-07-16 02:10:33 +02:00
|
|
|
exit
|
2021-07-15 22:12:15 +02:00
|
|
|
fi
|
|
|
|
|
2021-08-05 01:56:30 +02:00
|
|
|
#check that a disk has been provided:
|
|
|
|
if [ "$2" == "--override" ]
|
|
|
|
then
|
|
|
|
override=1
|
|
|
|
fi
|
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
#prerequisites:
|
2021-07-16 22:07:29 +02:00
|
|
|
|
|
|
|
if [[ ! -z $APT_CMD ]]; then
|
|
|
|
for prereq in $prereq_list; do
|
|
|
|
PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $prereq|grep "install ok installed")
|
|
|
|
#echo Checking for $prereq: $PKG_OK
|
2021-07-16 02:10:33 +02:00
|
|
|
if [ "" = "$PKG_OK" ]; then
|
2021-07-16 22:07:29 +02:00
|
|
|
echo "INFO: $prereq is not present. Setting up $prereq."
|
|
|
|
sudo apt-get --yes install $prereq
|
2021-07-16 02:10:33 +02:00
|
|
|
fi
|
2021-07-16 22:07:29 +02:00
|
|
|
done
|
|
|
|
elif [[ ! -z $YUM_CMD ]]; then
|
|
|
|
for prereq in $prereq_list; do
|
|
|
|
if ! rpm -qa | grep -qw $prereq; then
|
|
|
|
yum install $prereq
|
|
|
|
fi
|
|
|
|
done
|
|
|
|
else
|
|
|
|
echo "ERROR: error can't find the correct installer for the prerequisites"
|
|
|
|
exit 1;
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "---"
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
|
2021-07-15 22:12:15 +02:00
|
|
|
#collect data about disk type:
|
|
|
|
disk_type=$(smartctl -a /dev/$1 | grep -i "Rotation Rate:" | cut -d':' -f2 | tr -d " ")
|
|
|
|
|
|
|
|
#store if the disk is an ssd:
|
|
|
|
if [[ $disk_type == *"SolidStateDevice"* ]]; then
|
|
|
|
disk_ssd=1
|
|
|
|
# echo "disk is ssd"
|
|
|
|
elif [[ $disk_type == *"rpm"* ]]; then
|
|
|
|
disk_ssd=0
|
|
|
|
# echo "disk is not ssd"
|
|
|
|
else
|
|
|
|
echo "disk type unknown, exiting"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
2021-08-05 01:56:30 +02:00
|
|
|
#
|
|
|
|
if [[ "$override" -eq 1 ]]; then
|
|
|
|
echo "INFO: continuing to wipe since override has been issued"
|
|
|
|
else
|
|
|
|
#check for disk errors, differentiating by device type since smart output is different between sata and sas drives:
|
|
|
|
if smartctl -a /dev/$1 | grep -q "SATA"; then
|
2021-07-15 22:12:15 +02:00
|
|
|
#echo "Type of disk: SATA"
|
|
|
|
if smartctl -a /dev/$1 | grep -q "No Errors Logged"; then
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "INFO: This is a SATA disk, detecting no errors, going on:"
|
2021-07-15 22:12:15 +02:00
|
|
|
else
|
|
|
|
sata_model=$(smartctl -a /dev/$1 | grep -i "Device Model:" | cut -d':' -f2 | tr -d " ")
|
|
|
|
sata_serial=$(smartctl -a /dev/$1 | grep -i "Serial number:" | cut -d':' -f2 | tr -d " ")
|
|
|
|
echo "!!! ERRORS !!!"
|
|
|
|
echo "SATA errors, aborting!!!"
|
|
|
|
echo "NO WIPING NEEDED, JUST DESTROY THE DISK MECHANICALLY"
|
|
|
|
echo "!!! EXITING !!!"
|
|
|
|
echo ""
|
|
|
|
echo "Model: $sata_model"
|
|
|
|
echo "Serial: $sata_serial"
|
|
|
|
exit
|
|
|
|
fi
|
2021-08-05 01:56:30 +02:00
|
|
|
elif smartctl -a /dev/$1 | grep -q "SAS"; then
|
2021-07-15 22:12:15 +02:00
|
|
|
#echo "Type of disk: SAS"
|
|
|
|
sas_errors=$(smartctl -a /dev/$1 | grep "Elements in grown defect list" | cut -d':' -f2 | tr -d " ")
|
|
|
|
if [ "$sas_errors" -gt 0 ]; then
|
|
|
|
sas_vendor=$(smartctl -a /dev/$1 | grep -i "Vendor:" | cut -d':' -f2 | tr -d " ")
|
|
|
|
sas_model=$(smartctl -a /dev/$1 | grep -i "Product:" | cut -d':' -f2 | tr -d " ")
|
|
|
|
sas_serial=$(smartctl -a /dev/$1 | grep -i "Serial number:" | cut -d':' -f2 | tr -d " ")
|
|
|
|
echo "!!! ERRORS !!!"
|
|
|
|
echo "Elements in grown defect list: " $sas_errors
|
|
|
|
echo "NO WIPING NEEDED, JUST DESTROY THE DISK MECHANICALLY"
|
|
|
|
echo "!!! EXITING !!!"
|
|
|
|
echo ""
|
|
|
|
echo "Vendor: $sas_vendor"
|
|
|
|
echo "Model: $sas_model"
|
|
|
|
echo "Serial: $sas_serial"
|
|
|
|
exit
|
|
|
|
else
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "INFO: This is a SAS disk, detecting no errors, going on:"
|
2021-07-15 22:12:15 +02:00
|
|
|
fi
|
2021-08-05 01:56:30 +02:00
|
|
|
else
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "ERROR: the disk type is none of the expected ones, exiting"
|
2021-07-15 22:12:15 +02:00
|
|
|
exit
|
2021-08-05 01:56:30 +02:00
|
|
|
fi
|
2021-07-15 22:12:15 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#warning if is an ssd
|
|
|
|
if [ "$disk_ssd" -eq 1 ]; then
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "WARNING: DISK IS AN SSD, Remember that sectors are reallocated thus unwanted data might remain on the flash."
|
2021-07-16 02:10:33 +02:00
|
|
|
#TODO: ATA Secure erase? https://grok.lsu.edu/article.aspx?articleid=16716
|
2021-07-15 22:12:15 +02:00
|
|
|
fi
|
|
|
|
|
|
|
|
#calculate disk bytes:
|
|
|
|
disk_blocks=$(cat /proc/partitions | grep -w $1 | tr -s ' ' | cut -d " " -f4);
|
|
|
|
disk_bytes=$(( 1024*disk_blocks ))
|
|
|
|
|
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
#wipe:
|
2021-07-16 22:10:01 +02:00
|
|
|
#see: https://wiki.archlinux.org/title/Securely_wipe_disk/Tips_and_tricks#dd_-_advanced_example
|
2021-07-15 22:12:15 +02:00
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
for r_pass in $(seq 1 $random_pass); do
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "INFO: Random pass $r_pass of $random_pass :"
|
2021-07-16 02:10:33 +02:00
|
|
|
openssl enc -pbkdf2 -pass pass:"$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt </dev/zero | pv --progress --eta --rate --bytes --size $disk_bytes | dd of=/dev/$1 bs=2M oflag=direct iflag=fullblock
|
|
|
|
done
|
2021-07-15 22:12:15 +02:00
|
|
|
|
2021-07-16 02:10:33 +02:00
|
|
|
for z_pass in $(seq 1 $zeroing_pass); do
|
2021-07-16 02:34:28 +02:00
|
|
|
echo "INFO: Zeroing pass $z_pass of $zeroing_pass :"
|
2021-07-16 02:20:52 +02:00
|
|
|
dd if=/dev/zero | pv --progress --eta --rate --bytes --size $disk_bytes | dd of=/dev/$1 bs=2M oflag=direct iflag=fullblock
|
2021-07-16 02:10:33 +02:00
|
|
|
done
|
2021-07-15 22:12:15 +02:00
|
|
|
|
|
|
|
echo "!!! FINISHED wiping $1 !!!"
|
2021-07-16 22:07:29 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#some of the sources used:
|
|
|
|
#https://stackoverflow.com/questions/12806176/checking-for-installed-packages-and-if-not-found-install
|
|
|
|
#https://stackoverflow.com/questions/19477682/bash-script-determine-vendor-and-install-system-apt-get-yum-etc
|
|
|
|
#https://jschumacher.info/2016/03/erasing-with-openssl/
|
2021-07-16 22:10:01 +02:00
|
|
|
#https://wiki.archlinux.org/title/Securely_wipe_disk/Tips_and_tricks#dd_-_advanced_example
|
2021-08-05 01:56:30 +02:00
|
|
|
#https://serverfault.com/questions/6440/is-there-an-alternative-to-dev-urandom
|