| 1 | #!/bin/bashscip-shell filesystem . . `/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 [scip-shell system . . `[`. -z "$1" ] # No filename specified. |
| 19 | then |
| 20 | echoscip-shell system . . echo. "Usage: `basenamescip-shell system . . basename. $0` filename" |
| 21 | exit $E_BADARGS |
| 22 | fi |
| 23 | |
| 24 | file=$1 |
| 25 | |
| 26 | if [scip-shell system . . `[`. ! -e "$file" ] |
| 27 | then |
| 28 | echoscip-shell system . . echo. "File \"$file\" not found." |
| 29 | exit $E_NOT_FOUND |
| 30 | fi |
| 31 | |
| 32 | echoscip-shell system . . echo.; echoscip-shell system . . echo. -n "Are you absolutely sure you want to blot out \"$file\" (y/n)? " |
| 33 | read answer |
| 34 | case "$answer" in |
| 35 | [nN]) echoscip-shell system . . echo. "Changed your mind, huh?" |
| 36 | exit $E_CHANGED_MIND |
| 37 | ;; |
| 38 | *) echoscip-shell system . . echo. "Blotting out file \"$file\".";; |
| 39 | esac |
| 40 | |
| 41 | |
| 42 | flength=$(lsscip-shell system . . ls. -l "$file" | awkscip-shell system . . awk. '{print $5}') # Field 5 is file length. |
| 43 | pass_count=1 |
| 44 | |
| 45 | chmodscip-shell system . . chmod. u+w "$file" # Allow overwriting/deleting the file. |
| 46 | |
| 47 | echoscip-shell system . . echo. |
| 48 | |
| 49 | while [scip-shell system . . `[`. "$pass_count" -le "$PASSES" ] |
| 50 | do |
| 51 | echoscip-shell system . . echo. "Pass #$pass_count" |
| 52 | syncscip-shell system . . sync. # Flush buffers. |
| 53 | ddscip-shell system . . dd. if=/dev/urandomscip-shell filesystem . . `/dev/urandom`. of=$file bs=$BLOCKSIZE count=$flength |
| 54 | # Fill with random bytes. |
| 55 | syncscip-shell system . . sync. # Flush buffers again. |
| 56 | ddscip-shell system . . dd. if=/dev/zeroscip-shell filesystem . . `/dev/zero`. of=$file bs=$BLOCKSIZE count=$flength |
| 57 | # Fill with zeros. |
| 58 | syncscip-shell system . . sync. # Flush buffers yet again. |
| 59 | let "pass_count += 1" |
| 60 | echoscip-shell system . . echo. |
| 61 | done |
| 62 | |
| 63 | |
| 64 | rmscip-shell system . . rm. -f $file # Finally, delete scrambled and shredded file. |
| 65 | syncscip-shell system . . sync. # Flush buffers a final time. |
| 66 | |
| 67 | echoscip-shell system . . echo. "File \"$file\" blotted out and deleted."; echoscip-shell system . . 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 |