Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Cleaning up your hard disk

by l3nz (Friar)
on Jan 26, 2004 at 12:08 UTC ( [id://324118]=CUFP: print w/replies, xml ) Need Help??

I have a firm supplied laptop that I'm going to return because I'm switching job. I'm having backups and deleting personal data from it, but just to be sure I developed this small app that fills all disk space with random sequences of different length, so that deleted data is actually overwritten. It then removes the files it created. A random number of files is created in order to overwrite unused directory buckets (note: this will likely trash your filesystem). Don't forget to clean up all your files before running it. On Win32 machines, you'd better also clean your browser cache and empty your recycle bin before running it. This is a hack, so no warranty whatsoever - use it at your own risk.

Use: launch the script passing the name of a directory where temporary files will be stored.

If you interrupt it for any reason, don't forget to delete the files it created.

use strict; my $fp = $ARGV[0] or die "use: cleandisk path\nA path is mandatory"; my $nf = 0; print "Filling with random data your entire hard disk.\n"; my $randomDataCacheLength = 50000; my $randomDataCache = load_rdc( $randomDataCacheLength ); my @filesWritten; my $noError = 1; while ( $noError ) { eval { my $filename = $fp . '/xx___FILLER____' . $nf++ . '.txt'; push @filesWritten, $filename; open F, ">$filename" or die "Disk Full"; print F substr( $randomDataCache, rand( $randomDataCacheLength + ) ); close F; if ( $nf % 50 == 0 ) { print "Writing ${nf}th entry\n"; } }; if ($@) { print "$@\n"; $noError = 0; }; } print "Now removing random data...\n"; map {unlink $_; } @filesWritten; sub load_rdc { my $c_max = shift; my $s = ""; for ( my $c = 0; $c < $c_max; $c++ ) { $s .= chr rand( 20 ) + 65; } return $s; }
To have a rough idea of the time it takes, this thing filled up circa 500 mb of empty space on my Pentium II - 450 (IDE) I'm using now in say 4-5 minutes and created 18,700 files.

I'd launch it repeatedly in order to play it extra safe.

Replies are listed 'Best First'.
Re: Cleaning up your hard disk
by Abigail-II (Bishop) on Jan 26, 2004 at 12:18 UTC
    On Unix, you'd do it in half a line:
    dd if=/dev/random of=/some/path
    You may replace /dev/random with /dev/urandom if you like, and you can wipe an entire disk or partition by giving the device file as argument to of.

    Abigail

      ... or just use something like wipe

      --
      b10m
        They tell me there is something like this within PGP, even if I have never tried it.
      Of course you could also
      open F, ">file.txt"; while() { print F chr rand( 20 ) + 65; }
      And break it when it's done.

        No, not a good idea.

        This will use all the bit patterns between 100_0001 and 101_0101. Of interest is that the upper 3 bit are always set to 010. This flaw might be sufficient to allow a sufficiently motivated person to extract something useful.

        This would be better written as

        1 while print F chr(rand(256));

        As an added bonus, when the file system is full, the print wil fail, the call will return 0 and bingo! you fall out of the while. At least I believe that that is what should happen. You'll forgive me if I don't try this out on my own filesystem...

Re: Cleaning up your hard disk
by elwarren (Priest) on Jan 26, 2004 at 23:35 UTC
    I suggest a heavy earth magnet because the truly paranoid know that new drives store all your secrets into the low level DRM partition that we mortals can't reach :-)

    ...insert nervous laughter...

    On a slightly serious note, I wonder if this is one of the reasons PGP suggests nine passes to wipe a disk? Whatever type of cache is used for something like this can only hold so much data, and this could potentially push your, uhm, "evidence" out of the cache.
      See Secure Deletion of Data from Magnetic and Solid-State Memory: "Since degaussing destroys the sync bytes, ID fields, error correction information, and other paraphernalia needed to identify sectors on the media, thus rendering the drive unusable, it makes the degaussing process mostly equivalent to physical destruction."

      This paper is cited in the documentation of the shred program, which uses 25 passes of overwriting as the default. shred -u -z -f $* seems to work okay in place of /bin/rm.

      It should work perfectly the first time! - toma

        Waaay offtopic, but...

        The juxtaposition of shred needing 25 passes to really overwrite securely and your signature is immensely amusing.


        Once it's Turing complete, everything else is just syntactic sugar.
Re: Cleaning up your hard disk
by zentara (Archbishop) on Jan 26, 2004 at 20:23 UTC
    Wouldn't you have to run this from a bootable cdrom with Perl on it? If you are wiping your whole hard drive, your OS will be deleted half way thru, and I guess it would hang? Maybe I'm missing something? Can the OS in memory keep running if it's disk files are wiped?

      Darik's Boot And Nuke looks like just the thing. Plus it deals with filesystem caching.

      Darik's Boot and Nuke ("DBAN") is a self-contained boot floppy that securely wipes the hard disks of most computers. DBAN will automatically and completely delete the contents of any hard disk that it can detect, which makes it an appropriate utility for bulk or emergency data destruction.

      It's also avaliable as a bootable cdrom image. However you save it, be sure to label it very clearly. :)

      -- Rocco Caputo - rcaputo@pobox.com - poe.perl.org

      The kernel will already be loaded into memory (RAM), and this should work for the most part.

      Though unconfirmed, I have heard tails of Windows (yes, Windows!) being formatted while it is still running. Windows is usually more obstinate about such things.

      Of all the approaches posted here, DD is by far the best. It's fast, it's clean, and it's error-proof. The above script is dangerous as it won't get everything, plus it's going to be terribly slow in comparision to dd.

      The suggestion of using something like Knoppix for the dd operation, though, is definitely best. This way you can get in the habit of doing this even on machines that do not have Perl available.

      He's not wiping the whole disk, he's just cleaning up the empty space so they cannot do an undelete on his files. For that simple purpose, the script works well.

      I might shrink my pagefile as small as possible so to squeeze out any paged data. Delete the hibernation file as well. Run his program and maybe defrag the disk in between runs so that nothing is left in the empty sectors. Lots of wasted space like that on modern 10-80gb drives. All this stuff is amateur forensics. Anyone that's ever used the grep or strings command knows you can find tasty little nuggets without much work.

      That's why openbsd has encrypted swap. Insane, but relevant in this discussion :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://324118]
Approved by grinder
Front-paged by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (4)
As of 2024-04-24 01:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found