I use a php-upload-script that creates a new random 4-byte number for each uploaded file, then the XOR content of the file with these 4 bytes (repeating them as often as necessary), and finally attaches 4 bytes to the file before saving.
To download 4 bytes, you must again disconnect from the file, the contents will be processed again using XORed and the result will be sent to the client.
Thus, I can be sure that the files that I save on the server will not be executed or have any potential value for any application. In addition, I do not need an additional database for storing file names.
Here is the code I use for this:
Download:
<?php $outputfilename = $_POST['filename']; $inputfile = $_FILES["myblob"]["tmp_name"]; $tempfilename="temp.tmp"; if( move_uploaded_file($inputfile, $tempfilename) ) { $XORstring = random_bytes(4); $tempfile=fopen($tempfilename, "r"); $outputfile=fopen($outputfilename, "w+"); flock($outputfilename, LOCK_EX); fwrite($outputfilename, $XORbytes1); while ( $buffer = fread($tempfile, 4) ) { $buffer = $buffer ^ $XORstring; fwrite($outputfilename, $buffer); } flock($outputfilename, LOCK_UN); fclose($tempfile); fclose($outputfile); unlink($tempfilename); } exit(0); ?>
Download:
<?php $inputfilename = $_POST['filename']; $tempfilename = "temp.tmp"; $inputfile=fopen($inputfilename, "r"); $tempfile=fopen($tempfilename, "w+"); flock($tempfile, LOCK_EX); $XORstring = fread($inputfile, 4); while ( $buffer = fread($inputfile, 4) ) { $buffer = $buffer ^ $XORstring; fwrite($tempfile, $buffer); } flock($tempfile, LOCK_UN); fclose($inputfile); fclose($tempfile); readfile($tempfile); unlink($tempfile); exit(0); ?>
Max M. Jul 17 '17 at 8:15 2017-07-17 08:15
source share