Advertisement
zfkhole

Untitled

Oct 4th, 2014
273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 6.77 KB | None | 0 0
  1. #!/bin/bash
  2. ###########################################################################
  3. ############### A quick way to securely erase a list of files
  4. ###########################################################################
  5.  
  6. ww="which wipe >/dev/null"
  7. eval $ww
  8. if [ "$?" != "0" ]; then echo "!! You need to install wipe to use this tool !!"; unset ww; exit 1; fi
  9. ####
  10. declare -i ctstat #files deleted
  11. declare -i ctfail #files failed
  12. declare -i ctignore #files ignored (already removed)
  13. cfd=""
  14. cff=""
  15. cfi=""
  16. ctstat=0
  17. ctfail=0
  18. ctignore=0
  19. laststat=""
  20. devseed="/dev/random"
  21. tmprnd="./.rmprnd"
  22. _retval=""
  23. wforce=""
  24. wrecurse=""
  25. wkeep=""
  26. wcomopts="-Sr -R $devseed -Ma -Q3 -iq$wforce$wrecurse$wkeep"
  27. wcom="wipe $wcomopts"
  28. bvb="n"
  29. ###
  30. # this is the only way to get aliasing to work
  31. shopt -s expand_aliases
  32. alias echo='echo -e' #gets removed in quit
  33. #####
  34. #functions
  35. usage() {
  36.     echo "usage: $0 [options] <file1 [\"file2\" \'file3\' ...]>"
  37.     echo "usage: $0 -h|--help\t\tshow this"
  38.     echo "usage: $0 -f|--force\t\tforce wipe, do not prompt"
  39.     echo "usage: $0 -r|--recurse\t\tallow the removal of the entire directory tree"
  40.     echo "usage: $0 -k\t\tdo not delete the file after wiping it"
  41.     echo "usage: $0 quickwipe may encounter problems with gathering random data (\"low entropy\")\n\t\t\tif this happens, the program will try to gather random data from 3 other sources before giving up\n\t\t\tdata sources (in order): /dev/random, /dev/urandom/, 'openssl rand' output"
  42.     quit 0
  43. }
  44. quit() {
  45.     unalias echo
  46.     shopt -u expand_aliases
  47.     unset devseed
  48.     rm -f $tmprnd
  49.     unset tmprnd
  50.     unset _retval
  51.     unset wforce
  52.     unset wrecurse
  53.     unset wcom
  54.     unset wcomopts
  55.     unset wkeep
  56.     unset gencrstemp
  57.     unset arg1
  58.     unset arg2
  59.     unset shorti
  60.     unset ctstat
  61.     unset ctfail
  62.     unset ctignore
  63.     unset laststat
  64.     unset cfd
  65.     unset cff
  66.     unset cfi
  67.     unset bvb
  68.     unset ww
  69.     echo "(exitting with code $1)"
  70.     exit $1
  71. }
  72. _reseed() {
  73.     #devseed must be updated prior to calling this function
  74.  
  75.     orand="openssl rand -out $devseed 10000000" #10mb of random data
  76.     eval $orand
  77.     return $?
  78. }
  79. execwipe() {
  80.     #devseed must be updated prior to calling this function
  81.  
  82.     wcomopts="-Sr -R $devseed -Ma -Q3 -iq$wforce$wrecurse$wkeep"
  83.     wcom="wipe $wcomopts $1 2>/dev/null"
  84.     eval $wcom
  85.     return $?
  86. }
  87. genshorti() {
  88.     shorti=""
  89.     arg1="$1"
  90.     arg1=${#arg1}
  91.     if [ "$arg1" -gt "7" ]; then shorti="${i:0:7}..."; else shorti="$i"; fi
  92. }
  93. _ks() {
  94.     #cfd files del
  95.     #cff file fail
  96.     #cfi file ignore
  97.  
  98.     _arg1="$1"
  99.  
  100.     # file was forced/failed
  101.     if [ "$#" == "2" ]; then _arg2="$2"
  102.     else _arg2=""; fi
  103.     if [ "$_arg2" == "f" ]; then cff="$cff $_arg1"; ((ctfail++)); return 0; fi
  104.    
  105.     # check file existence
  106.     if [ ! -f "$_arg1" ]; then cfd="$cfd $_arg1"; ((ctstat++)); return 0
  107.     else cff="$cff $_arg1"; ((ctfail++)); fi
  108. }
  109. keepstat() {
  110.     #gather some stats (only works if --keep is NOT enabled)
  111.     if [ "$wkeep" == "k" ]; then return 0; fi
  112.  
  113.     arg1="$1"
  114.  
  115.     # get 2nd var
  116.     if [ "$#" == "2" ]; then arg2="$2"
  117.     else arg2=""; fi
  118.  
  119.     # file was ignored (i = ignore flag)
  120.     if [ "$arg2" == "i" ]; then cfi="$cfi $arg1"; ((ctignore++)); laststat="$arg1"; return 0; fi
  121.    
  122.     # duplicate call
  123.     if [ "$laststat" == "$arg1" ]; then
  124.        
  125.         # only run _ks if the force flag it given
  126.         if [ "$arg2" == "f" ]; then
  127.             _ks "$arg1" "f"
  128.             laststat="$arg1"
  129.         fi
  130.     else
  131.         _ks "$arg1"
  132.         laststat="$arg1"
  133.         #echo "ls $laststat || curr $arg1"
  134.     fi
  135. }
  136.  
  137. # https://gist.github.com/cosimo/3760587
  138. OPTS=`getopt -o hfrkv --long help,force,recurse,keep,verbose -n 'parse-options' -- "$@"`
  139. if [ $? != 0 ]; then echo "Failed parsing options..."; quit 1; fi
  140. eval set -- "$OPTS"
  141.  
  142. # parse options
  143. while true; do
  144.         case "$1" in
  145.                 ( -h | --help )
  146.                         usage
  147.                         ;;
  148.         ( -f | --force )
  149.             wforce="f"
  150.             ;;
  151.         ( -r | --recurse )
  152.             wrecurse="r"
  153.             ;;
  154.         ( -k | --keep )
  155.             wkeep="k"
  156.             ;;
  157.         ( -v | --verbose )
  158.             bvb="y"
  159.             ;;
  160.         ( -- ) shift; break ;;
  161.         ( -* ) echo "$0: error - unrecognized option $1" 1>&2; quit 1;;
  162.         ( * ) break ;;
  163.     esac
  164.     shift
  165. done
  166.  
  167. # if there are no more arguments, show usage and quit
  168. if [ "$#" -lt "1" ]; then usage; quit 1; fi
  169.  
  170.  
  171. # parse files
  172. for i in "$@"; do
  173.     # check existence of file
  174.     if [ ! -f "$i" ]; then echo "wipe: file not found"; keepstat "$i" "i"; shift; continue; fi
  175.  
  176.     # try wipe with /dev/random
  177.     devseed="/dev/random"; genshorti $i
  178.     echo -n "trying quickwipe($devseed) on \"$shorti\" -->  "
  179.     execwipe $i
  180.  
  181.         # WIPEFAIL on /dev/random
  182.         if [ "$?" != "0" ]; then echo "failed!"
  183.         else keepstat "$i"; shift; continue; fi
  184.  
  185.     # try wipe with /dev/urandom
  186.     devseed="/dev/urandom"; genshorti $i
  187.     echo -n "\ttrying quickwipe($devseed) on \"$shorti\" -->  "
  188.     execwipe $i
  189.  
  190.         # WIPEFAIL on /dev/urandom
  191.         if [ "$?" != "0" ]; then echo "failed!"
  192.         else keepstat "$i"; shift; continue; fi
  193.  
  194.     # try wiping with $tmprnd
  195.     devseed="$tmprnd"
  196.     echo -n "\ttrying to seed $devseed... "
  197.     _reseed
  198.  
  199.         # SEEDFAIL on $tmprnd
  200.         if [ "$?" != "0" ]; then echo "failed!"
  201.         else
  202.             genshorti $i
  203.             echo -n "\ttrying quickwipe($devseed) on \"$shorti\" -->  "
  204.             # seeding $tmprnd succeeded, try wiping
  205.             devseed="$tmprnd"
  206.             execwipe $i
  207.             _retval="$?"
  208.         fi
  209.  
  210.         # WIPEFAIL on $tmprnd
  211.         if [ "$_retval" != "0" ]; then echo "failed!"
  212.         else keepstat "$i"; shift; continue; fi
  213.  
  214.     # try seeding /dev/random
  215.     devseed="/dev/random"
  216.     echo -n "\ttrying to seed $devseed... "
  217.     _reseed
  218.  
  219.         # SEEDFAIL on /dev/random
  220.         if [ "$?" != "0" ]; then echo "failed!"
  221.         else
  222.             genshorti $i
  223.             echo -n "\ttrying quickwipe($devseed) on \"$shorti\" -->  "
  224.             #seeding /dev/random succeeded, try wipping
  225.             devseed="/dev/random"
  226.             execwipe $i
  227.             _retval="$?"
  228.         fi
  229.             # WIPEFAIL on /dev/random
  230.             if [ "$_retval" != "0" ]; then echo "failed!"
  231.             else keepstat "$i"; shift; continue; fi
  232.  
  233.     # try seeding /dev/urandom
  234.     devseed="/dev/urandom"
  235.     echo -n "\ttrying to seed $devseed... "
  236.     _reseed
  237.  
  238.         # final stop ... if wipe fails, shift & continue
  239.         # SEEDFAIL on /dev/urandom
  240.         if [ "$?" != "0" ]; then genshorti $i; echo "failed!\n!!\tout of options! quickwipe on \"$shorti...\" FAILED"; keepstat "$i" "f"; shift; continue
  241.         else
  242.             genshorti $i
  243.             echo -n "trying quickwipe($devseed) on \"$shorti\" -->  "
  244.             #seeding /dev/urandom succeeded, try wipping
  245.             devseed="/dev/urandom"
  246.             execwipe $i
  247.             _retval="$?"
  248.         fi
  249.             # WIPEFAIL on /dev/urandom
  250.             if [ "$_retval" != "0" ]; then genshorti $i; echo "failed!\n!!\tout of options! quickwipe on \"$shorti...\" FAILED"; keepstat "$i" "f"; shift; continue
  251.             else keepstat "$i"; shift; continue; fi
  252.  
  253.     # move to next argument
  254.     shift
  255. done
  256.  
  257. if [ "$wkeep" == "" ]; then echo "\nfinished:\n$ctstat files wiped\n$ctfail files failed to be wiped\n$ctignore files ignored"; fi
  258. if [ "$bvb" == "y" ]; then echo "\nfiles wiped: $cfd\nfiles ignored: $cfi\nfiles failed: $cff"; fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement