0
我在寫一個基本的許可證驗證的PHP下載腳本。目標文件大約50MB,適用於某些文件。其他人無法完成它,有時會重試它。PHP文件下載站點
下面是腳本:
$method = $_GET['method'];
if($method == "webdownload") {
$airlineid = $_GET['airline'];
$sql = "SELECT * FROM airlines WHERE airlineid='$airlineid'";
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
if($row['licensekey'] == "")
die("Invalid airline id");
$filename = $row['code'].'_installer.exe';
$file_path = '../resources/application/files/'.$row['airlineid'].'/'.$row['clientversion'].'/application_installer.exe';
if($row['licensestate'] != "OK")
die("The license associated with this downloaded has been deauthorized.");
if(!is_file($file_path))
die("The file associated with this version for this airline appears to be invalid.");
//download code here - it runs once only, if refreshed it will not allow it.
header('Content-type: application/exe');
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Length: ".filesize($file_path));
header("Content-Transfer-Encoding: binary");
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
//header('X-Sendfile: '.$file_path); I tried this - it had no effect and I want the portability.
$file = @fopen($file_path,"rb");
while(!feof($file)) {
$buffer = fread($file, 1024 * 8);
print($buffer);
flush();
}
close($file);
}
編輯:經諮詢,我發現劇本,等等,是很容易受到SQL注入。我用這個函數替換了直接變量SQL表達式:
function secure_string($raw) {
$sid = strtolower($raw);
$sid = str_replace("'","_SINGLE_QUOTE", $sid);
$sid = str_replace('"','_DOUBLE_QUOTE', $sid);
$cmd[0] = "insert";
$cmd[1] = "select";
$cmd[2] = "union";
$cmd[3] = "delete";
$cmd[4] = "modify";
$cmd[5] = "replace";
$cmd[6] = "update";
$cmd[7] = "create";
$cmd[8] = "alter";
for($index = 0; $index <= 8; $index++) {
$sid = str_replace($cmd[$index],"_SQL_COMMAND", $sid);
}
return $sid;
}
這足以阻止SQL注入嗎?
EDIT2:我已經使用這個函數與PDO準備函數一起消除這個漏洞。感謝100x讓我學到這一課而不會造成災難性的結果。
快速問題,當我輸入''或1 = 1'作爲航空公司查詢字符串參數時會發生什麼? – acqu13sce
立即,沒有。但是,我查了一下,當使用'或'1'='1(因爲它寫在那裏)作爲參數時,它下載了列表中的第一個文件。我有一個小心臟病發作。我想我明天會花費我的SQL注入漏洞。好,趕快,謝謝。 –
從我的示例sql注入結束時,我錯過了'--',當時我注意到我無法編輯我的評論。我建議你看看PDO和參數化查詢,以此作爲防止此類攻擊的簡單方法 – acqu13sce