2010/06/27

Securely Erase the Content of A Drive... The Easy Way!

Do you sometimes have somewhat sensitive data that has been deleted on a drive but you would feel better if it were unrecoverable? And you don't want to wipe-out the whole drive (keep the data that is already in). Such a situation occurs a lot when changing PC at work. You know the next guy will likely be a computer expert; if that person has a penchant for evil, you definitely do not what that person to have access to the data that used to be on that computer.


Leave this script running overnight and you can be (kind of) confident nothing will be recoverable.





#python 3

import random
import os

def _getRandStr(rand_str_len):
  lib = "abcdefghjiklmnopqrstuvwxyz123456789"
  
  randomStr = []
  
  while len(randomStr) < rand_str_len:
    randomStr += lib[ random.randint(0,len(lib)-1) ]
  
  assert len(randomStr) == rand_str_len
  return "".join(randomStr)

def writeToHDUntilException():
  randomfolder = None
  nfilesInFolder = 0
  while True:
    if randomfolder is None or nfilesInFolder > 9:
      randomfolder = _getRandStr(16)
      if not os.path.isdir(randomfolder):
        os.mkdir(randomfolder)
      nfilesInFolder = 0
    fh = open( randomfolder + '/' + _getRandStr(32) + '.jpg', 'wb' )
    fh.write( _getRandStr(128).encode()*1024*8 )
    fh.close()
    nfilesInFolder += 1

if __name__ == '__main__':
  writeToHDUntilException()


If you leave that script running for long enough then you will eventually have overwritten all the free nodes of the HD. Delete the files created and the content that was there beforehand will be unrecoverable. Quite handy for USB keys.

1 comment:

Anonymous said...

lib[ random.randint(0,len(lib)-1) ]
should be written as
random.choice(lib)