安全 Shaun Clowes的文章Exploiting Common Vulnerabilities in PHP Applications的確寫的很棒,
考慮到了很多方面,我這個文章只是狗尾續貂,補充一些其它沒怎麼提到的問題。本文側重於解決問題,而不是
攻擊。
1、古老的欺騙SQL語句
在預設模式下,即使是你忘了把php.ini拷到/usr/local/lib/php.ini下,php還是開啟magic_quotes_gpc=on。
這樣所有從GET/POST/Cookie來的變數的單引號(')、雙引號(")、反斜線backslash()以及空字元NUL
(the null byte)都會被加上反斜線,以使資料庫能夠正確查詢。
但是在php-4-RC2的時候引入了一個設定檔php.ini-optimized,這個最佳化的php.ini卻是
magic_quotes_gpc=off的。某些網管看到optimized字樣也許就會把php.ini-optimized拷到
/usr/local/lib/php.ini,這時就比較危險。象比較簡單的驗證,假設沒有過濾必要的字元:
select * from login where user='$HTTP_POST_VARS[user]' and pass='$HTTP_POST_VARS[pass]'
我們就可以在使用者框和密碼框輸入1‘ or 1='1通過驗證了。這是非常古董的方法了,這個語句會
替換成這樣:
select * from login where user='1' or 1='1' and pass='1' or 1='1'
因為or 1='1'成立,所以通過了。
解決的辦法最好就是過濾所有不必要的字元,還有就是推薦對於從GET/POST/Cookie來的並且用在SQL
中的變數加一個自訂的函數:
function gpc2sql($str) {
if(get_magic_quotes_gpc()==1)
return $str;
else
return addslashes($str);
}
主要是為了你的程式能安全移植在各種系統裡。
參考文獻
1、PHP 4 ChangeLog (http://www.php.net/ChangeLog-4.php)
2、A Study In Scarlet - Exploiting Common Vulnerabilities in PHP Applications
(http://www.securereality.com.au/studyinscarlet.txt)及analysist的翻譯。
3、Remote command execution vulnerabilities in phpMyAdmin and phpPgAdmin
(http://www.securereality.com.au/sradv00008.txt)