Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Secure delete with PHP 5.3.x

Tags:

linux

php

Does someone knows an good PHP Solution to delete or better wipe an file from an linux system?

Scenario: File is encrypted and saved, when a download is requested the file is copyed to an temporary folder and decrypted. This is already working.

But how to remove the file from the temporary location after sending in to the user?

In my mind i have the following options:

  • Open the File via "fopen" and write 0,1 into it (think very slow)
  • Save file to Memcache instead of harddisk (could be a problem with my hoster)
  • Use somd 3rd pary tool on commandline or as cronjob (could be a problem to install)

Goal: Delete the file from hard disk, without the possibility to recover (wipe/overwrite)

like image 917
opHASnoNAME Avatar asked Jun 13 '12 09:06

opHASnoNAME


2 Answers

Call "shred" via exec/system/passthru

like image 63
breiti Avatar answered Nov 15 '22 19:11

breiti


Arguably the best is to never save the file in its decrypted state in the first place.

Rather, use stream filters to decrypt it on-the-fly and send it directly to the end-user.

Update

Your option 1 is actually not too bad if you consider this code:

$filename = 'path/to/file';
$size = filesize($filename);

$src = fopen('/dev/zero', 'rb');
$dest = fopen('/path/to/file', 'wb');

stream_copy_to_stream($src, $dest, $size);

fclose($src);
fclose($dest);

You could choose /dev/urandom as well, but that will be slow.

like image 29
Ja͢ck Avatar answered Nov 15 '22 19:11

Ja͢ck