Posted By

ksaver on 04/20/11


Tagged

delete rm Wipe dd


Versions (?)

karm.sh: A tiny secure deletion tool.


 / Published in: Bash
 

A tiny secure deletion tool...

Example:

$ karm.sh image1.jpg

karming image1.jpg... [OK]

$ karm.sh /etc/passwd

karming /etc/passwd... [ERR]:

/etc/passwd not karmed. Are you the owner?

  1. #!/usr/bin/env bash
  2. # karm.sh
  3. # A tiny secure deletion tool
  4. # Uses dd, fsync and /dev/urandom to overwrite
  5. # a file twice with a random data stream and delete it.
  6. # @_ksaver, Feb 2010.
  7. # Licence: Public Domain Code.
  8. # Use it with caution and under your own risk.
  9. # Not warranty provided at all.
  10. VERSION='0.4'
  11.  
  12. function karm(){
  13. # Gets file name from $1
  14. FILE="$1"
  15.  
  16. # Gets file size.
  17. SIZE=$(/usr/bin/du -k "$FILE")
  18. COUNT=${SIZE%"$FILE"*}
  19.  
  20. # check if urandom exists
  21. if [ -e '/dev/urandom' ]
  22. then
  23. RANDD='/dev/urandom'
  24. else
  25. RANDD='/dev/random'
  26. fi
  27.  
  28. # Send random stream to file...
  29. /bin/dd if=$RANDD of="$FILE" bs=1k count=$COUNT &> /dev/null
  30.  
  31. # If data was successfuly written...
  32. if [ $? = 0 ]
  33. then
  34. # forces writing of cached file to physical storage...
  35. (/usr/bin/env fsync "$FILE" &> /dev/null && return 0) \
  36. || ( sync && return 0)
  37. else
  38. return 1
  39. fi
  40. }
  41.  
  42. function kdelete(){
  43. # unlink the karmed file's inode...
  44. /bin/rm "$1" > /dev/null || return 1
  45. }
  46.  
  47. # _main_
  48. # if not arguments were given...
  49. if [ -z "$1" ]
  50. then
  51. echo -e "\tkarm $VERSION: Secure deleting tool."
  52. echo -e "\tSyntaxis: $0 <File_to_delete>"
  53. exit 1
  54. else
  55. FILE="$1"
  56.  
  57. # If file exists
  58. if [ -f "$FILE" ]
  59. then
  60. echo -n "karming $FILE... "
  61. karm "$FILE" && karm "$FILE" && kdelete "$FILE"
  62. if [ $? = 0 ]
  63. then
  64. # this far, everything has gone ok...
  65. echo " [OK]"
  66. exit 0
  67. else
  68. # May be you are not the owner of file
  69. echo -e "[ERR]:\n$FILE not karmed. Are you the owner?"
  70. exit 1
  71. fi
  72. else
  73. # File doesn't exist...
  74. echo "[ERR] $0: Is $FILE a valid file?"
  75. exit 1
  76. fi
  77. fi
  78. # EOF

Report this snippet  

You need to login to post a comment.