securely overwrite files with Python

Mathias Waack M.Waack at gmx.de
Fri Mar 5 16:28:55 EST 2004


Bart Nessux wrote:

> Is there a shred module in Python? You know, the kind that
> overwrites files that one doesn't want others to see? I can call
> the unix program like this:
> 
> x = os.popen("/usr/bin/shred -uvz NAME_OF_FILE")
> x.read()
> x.close()
> 
> But, I'd like a platform independant (portable) way of doing this
> if possible.

First: It is not possible. Let me cite some sentences from shred(1):

       CAUTION: Note  that  shred  relies  on  a  very  important
       assumption:  that the filesystem overwrites data in place.
       This is the traditional way to do things, but many  modern
       filesystem  designs  do  not satisfy this assumption.

But you can make recovery a bit harder by simply overwriting the file
(in fact thats just what shred does): 

(just to describe the idea, I'm sure it will not work;)

f = file("file_to_shred","a+")
size = os.stat("file_to_shred").st_size
f.seek(0)
for i in xrange(size):
  f.write("x")
for i in xrange(size):
  f.write(0)
f.close()
os.unlink("file_to_shred")

To achieve real security you need much knowledge about the
underlaying hardware and filesystem. 

Mathias



More information about the Python-list mailing list