Forum Moderators: coopster

Message Too Old, No Replies

is this download-script hackerproof

         

carsten888

11:39 am on Jan 18, 2008 (gmt 0)

10+ Year Member



url looks something like: download.php?fileid=hgv%7jhb238cx0askj=_==
fileid is base64 encoded filename

I want to restrict downlaods to:
- files from directory /media
- only download from the same domain

is the underneath code hackerproof to ensure this?

please feedback.

<?php

$fileName = base64_decode($_GET['fileid']);

$dir = "media/";

$file = $dir.$fileName;

//check for dot before looking for extension
$gotDot = strpos($fileName,".");

if($gotDot){
$extension = "";
$i = strlen($fileName);
while (substr($fileName, $i, 1)!= ".") {
$extension = substr($fileName, $i--, 1) . $extension;
}
$extension = strtolower($extension);
}

//check if theres no slash in the filename against hacking
$gotForwardSlash = strpos($fileName,"/");
$gotBackwardSlash = strpos($fileName,"\\");

//check if the page calling this download is from the same domain against hacking
$serverName = $_SERVER['SERVER_NAME'];
$previousUrl = $_SERVER['HTTP_REFERER'];
$sameDomain = strpos($previousUrl,$serverName);

if(!$gotForwardSlash &&!$gotBackwardSlash && $gotDot && $sameDomain == 7){
//force the download
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-length: "' . filesize($file) . '"');
header('Content-Type: "' . $extension . '"');
header('Content-Disposition: attachment; filename="' . $fileName . '"');
readfile($file);
}else{
echo "file can not be downloaded";
}

?>

eelixduppy

12:44 am on Jan 21, 2008 (gmt 0)



At a quick glance it looks ok. As long as your input is properly sanitized then you should be alright.