wiper.sh 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. #!/usr/bin/env bash
  2. #variables:
  3. random_pass=2
  4. zeroing_pass=1
  5. prereq_list="pv smartmontools hdparm"
  6. YUM_CMD=$(which yum)
  7. APT_CMD=$(which apt-get)
  8. #check that a disk has been provided:
  9. if [ -z "$1" ]
  10. then
  11. echo "USAGE: \"./wiper.sh diskname\", for example: \"./wiper.sh sdb\""
  12. exit
  13. fi
  14. #check the disk exists:
  15. if [ ! -e "/dev/$1" ]; then
  16. echo "ERROR: /dev/$1 does not exists. exiting."
  17. exit
  18. fi
  19. #check that a disk has been provided:
  20. if [ "$2" == "--override" ]
  21. then
  22. override=1
  23. fi
  24. #prerequisites:
  25. if [[ ! -z $APT_CMD ]]; then
  26. for prereq in $prereq_list; do
  27. PKG_OK=$(dpkg-query -W --showformat='${Status}\n' $prereq|grep "install ok installed")
  28. #echo Checking for $prereq: $PKG_OK
  29. if [ "" = "$PKG_OK" ]; then
  30. echo "INFO: $prereq is not present. Setting up $prereq."
  31. sudo apt-get --yes install $prereq
  32. fi
  33. done
  34. elif [[ ! -z $YUM_CMD ]]; then
  35. for prereq in $prereq_list; do
  36. if ! rpm -qa | grep -qw $prereq; then
  37. yum install $prereq
  38. fi
  39. done
  40. else
  41. echo "ERROR: error can't find the correct installer for the prerequisites"
  42. exit 1;
  43. fi
  44. echo "---"
  45. #collect data about disk type:
  46. disk_type=$(smartctl -a /dev/$1 | grep -i "Rotation Rate:" | cut -d':' -f2 | tr -d " ")
  47. #store if the disk is an ssd:
  48. if [[ $disk_type == *"SolidStateDevice"* ]]; then
  49. disk_ssd=1
  50. # echo "disk is ssd"
  51. elif [[ $disk_type == *"rpm"* ]]; then
  52. disk_ssd=0
  53. # echo "disk is not ssd"
  54. else
  55. echo "disk type unknown, exiting"
  56. exit
  57. fi
  58. #
  59. if [[ "$override" -eq 1 ]]; then
  60. echo "INFO: continuing to wipe since override has been issued"
  61. else
  62. #check for disk errors, differentiating by device type since smart output is different between sata and sas drives:
  63. if smartctl -a /dev/$1 | grep -q "SATA"; then
  64. #echo "Type of disk: SATA"
  65. if smartctl -a /dev/$1 | grep -q "No Errors Logged"; then
  66. echo "INFO: This is a SATA disk, detecting no errors, going on:"
  67. else
  68. sata_model=$(smartctl -a /dev/$1 | grep -i "Device Model:" | cut -d':' -f2 | tr -d " ")
  69. sata_serial=$(smartctl -a /dev/$1 | grep -i "Serial number:" | cut -d':' -f2 | tr -d " ")
  70. echo "!!! ERRORS !!!"
  71. echo "SATA errors, aborting!!!"
  72. echo "NO WIPING NEEDED, JUST DESTROY THE DISK MECHANICALLY"
  73. echo "!!! EXITING !!!"
  74. echo ""
  75. echo "Model: $sata_model"
  76. echo "Serial: $sata_serial"
  77. exit
  78. fi
  79. elif smartctl -a /dev/$1 | grep -q "SAS"; then
  80. #echo "Type of disk: SAS"
  81. sas_errors=$(smartctl -a /dev/$1 | grep "Elements in grown defect list" | cut -d':' -f2 | tr -d " ")
  82. if [ "$sas_errors" -gt 0 ]; then
  83. sas_vendor=$(smartctl -a /dev/$1 | grep -i "Vendor:" | cut -d':' -f2 | tr -d " ")
  84. sas_model=$(smartctl -a /dev/$1 | grep -i "Product:" | cut -d':' -f2 | tr -d " ")
  85. sas_serial=$(smartctl -a /dev/$1 | grep -i "Serial number:" | cut -d':' -f2 | tr -d " ")
  86. echo "!!! ERRORS !!!"
  87. echo "Elements in grown defect list: " $sas_errors
  88. echo "NO WIPING NEEDED, JUST DESTROY THE DISK MECHANICALLY"
  89. echo "!!! EXITING !!!"
  90. echo ""
  91. echo "Vendor: $sas_vendor"
  92. echo "Model: $sas_model"
  93. echo "Serial: $sas_serial"
  94. exit
  95. else
  96. echo "INFO: This is a SAS disk, detecting no errors, going on:"
  97. fi
  98. else
  99. echo "ERROR: the disk type is none of the expected ones, exiting"
  100. exit
  101. fi
  102. fi
  103. #warning if is an ssd
  104. if [ "$disk_ssd" -eq 1 ]; then
  105. echo "WARNING: DISK IS AN SSD, Remember that sectors are reallocated thus unwanted data might remain on the flash."
  106. #TODO: ATA Secure erase? https://grok.lsu.edu/article.aspx?articleid=16716
  107. fi
  108. #calculate disk bytes:
  109. disk_blocks=$(cat /proc/partitions | grep -w $1 | tr -s ' ' | cut -d " " -f4);
  110. disk_bytes=$(( 1024*disk_blocks ))
  111. #wipe:
  112. #see: https://wiki.archlinux.org/title/Securely_wipe_disk/Tips_and_tricks#dd_-_advanced_example
  113. for r_pass in $(seq 1 $random_pass); do
  114. echo "INFO: Random pass $r_pass of $random_pass :"
  115. 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
  116. done
  117. for z_pass in $(seq 1 $zeroing_pass); do
  118. echo "INFO: Zeroing pass $z_pass of $zeroing_pass :"
  119. dd if=/dev/zero | pv --progress --eta --rate --bytes --size $disk_bytes | dd of=/dev/$1 bs=2M oflag=direct iflag=fullblock
  120. done
  121. echo "!!! FINISHED wiping $1 !!!"
  122. #some of the sources used:
  123. #https://stackoverflow.com/questions/12806176/checking-for-installed-packages-and-if-not-found-install
  124. #https://stackoverflow.com/questions/19477682/bash-script-determine-vendor-and-install-system-apt-get-yum-etc
  125. #https://jschumacher.info/2016/03/erasing-with-openssl/
  126. #https://wiki.archlinux.org/title/Securely_wipe_disk/Tips_and_tricks#dd_-_advanced_example
  127. #https://serverfault.com/questions/6440/is-there-an-alternative-to-dev-urandom