| 1 | #!/bin/bash |
| 2 | # blot-out.sh: Erase "all" traces of a file. |
| 3 | |
| 4 | # This script overwrites a target file alternately |
| 5 | #+ with random bytes, then zeros before finally deleting it. |
| 6 | # After that, even examining the raw disk sectors by conventional methods |
| 7 | #+ will not reveal the original file data. |
| 8 | |
| 9 | PASSES=7 # Number of file-shredding passes. |
| 10 | # Increasing this slows script execution, |
| 11 | #+ especially on large target files. |
| 12 | BLOCKSIZE=1 # I/O with /dev/urandom requires unit block size, |
| 13 | #+ otherwise you get weird results. |
| 14 | E_BADARGS=70 # Various error exit codes. |
| 15 | E_NOT_FOUND=71 |
| 16 | E_CHANGED_MIND=72 |
| 17 | |
| 18 | if [ -z "$1" ] # No filename specified. |
| 19 | then |
| 20 | echo "Usage: `basename $0` filename" |
| 21 | exit $E_BADARGS |
| 22 | fi |
| 23 | |
| 24 | file=$1 |
| 25 | |
| 26 | if [ ! -e "$file" ] |
| 27 | then |
| 28 | echo "File \"$file\" not found." |
| 29 | exit $E_NOT_FOUND |
| 30 | fi |
| 31 | |
| 32 | echo; echo -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? " |
| 33 | read answer |
| 34 | case "$answer" in |
| 35 | [nN]) echo "Changed your mind, huh?" |
| 36 | exit $E_CHANGED_MIND |
| 37 | ;; |
| 38 | *) echo "Blotting out file \"$file\".";; |
| 39 | esac |
| 40 | |
| 41 | |
| 42 | flength=$(ls -l "$file" | awk '{print $5}') # Field 5 is file length. |
| 43 | pass_count=1 |
| 44 | |
| 45 | chmod u+w "$file" # Allow overwriting/deleting the file. |
| 46 | |
| 47 | echo |
| 48 | |
| 49 | while [ "$pass_count" -le "$PASSES" ] |
| 50 | do |
| 51 | echo "Pass #$pass_count" |
| 52 | sync # Flush buffers. |
| 53 | dd if=/dev/urandom of=$file bs=$BLOCKSIZE count=$flength |
| 54 | # Fill with random bytes. |
| 55 | sync # Flush buffers again. |
| 56 | dd if=/dev/zero of=$file bs=$BLOCKSIZE count=$flength |
| 57 | # Fill with zeros. |
| 58 | sync # Flush buffers yet again. |
| 59 | let "pass_count += 1" |
| 60 | echo |
| 61 | done |
| 62 | |
| 63 | |
| 64 | rm -f $file # Finally, delete scrambled and shredded file. |
| 65 | sync # Flush buffers a final time. |
| 66 | |
| 67 | echo "File \"$file\" blotted out and deleted."; echo |
| 68 | |
| 69 | |
| 70 | exit 0 |
| 71 | |
| 72 | # This is a fairly secure, if inefficient and slow method |
| 73 | #+ of thoroughly "shredding" a file. |
| 74 | # The "shred" command, part of the GNU "fileutils" package, |
| 75 | #+ does the same thing, although more efficiently. |
| 76 | |
| 77 | # The file cannot not be "undeleted" or retrieved by normal methods. |
| 78 | # However . . . |
| 79 | #+ this simple method would *not* likely withstand |
| 80 | #+ sophisticated forensic analysis. |
| 81 | |
| 82 | # This script may not play well with a journaled file system. |
| 83 | # Exercise (difficult): Fix it so it does. |
| 84 | |
| 85 | |
| 86 | |
| 87 | # Tom Vier's "wipe" file-deletion package does a much more thorough job |
| 88 | #+ of file shredding than this simple script. |
| 89 | # http://www.ibiblio.org/pub/Linux/utils/file/wipe-2.0.0.tar.bz2 |
| 90 | |
| 91 | # For an in-depth analysis on the topic of file deletion and security, |
| 92 | #+ see Peter Gutmann's paper, |
| 93 | #+ "Secure Deletion of Data From Magnetic and Solid-State Memory". |
| 94 | # http://www.cs.auckland.ac.nz/~pgut001/pubs/secure_del.html |