


MySQL和PHP中的SQL注入式漏洞解決方法
添加時間:2014-5-31 17:25:40
添加:
思海網(wǎng)絡(luò)
SQL注入式漏洞是許多PHP程序的主要安全危害,產(chǎn)生的原因是在向數(shù)據(jù)庫執(zhí)行插入等語句時,web開發(fā)者允許最終用戶操作變量(例如根據(jù)表單提交內(nèi)容顯示相應(yīng)信息),通常是_GET、_POST或_SESSION等全局變量。
如果認(rèn)為其中的_GET[‘id’]會永遠(yuǎn)是個數(shù)值型的值那將是很嚴(yán)重的錯誤。最終用戶可以改變這個變量的值,例如"0; Delete FROM news;",那么query語句就會變成下面的值:
Select news_title, news_text FROM news Where news_id=0; Delete FROM news;
這將產(chǎn)生很嚴(yán)重的后果。
驗證數(shù)值型數(shù)據(jù)
數(shù)值型數(shù)據(jù)是最容易驗證的,PHP有一個自帶的函數(shù)叫 is_numeric()可以返回ture值來判斷是否是數(shù)值型,這個函數(shù)并不是MySQL自帶的,因此可在任何數(shù)據(jù)庫平臺的php程序中用于驗證數(shù)字。
下面是修改后的代碼:
驗證非數(shù)值型數(shù)據(jù)
非 數(shù)值型數(shù)據(jù)的驗證稍有點麻煩。PHP有個叫Magic Quotes的特殊功能。當(dāng)它激活時,PHP會自動過濾掉_GET和_POST全局變量中的反斜線符號(\),雙引號(”),單引號(’)和空白字符。問 題是并不是所有的服務(wù)器都能打開了這個功能,所以必須檢測服務(wù)器是否開通了這個功能。可以使用get_magic_quotes_gpc()函數(shù)來判定 maigc quotes功能是否打開。
在MySQL查詢語句可以使用mysql_real_escape_string()函數(shù)來增強(qiáng)安全性,代碼如下:
輸出到頁面
為正確顯示字符中的引號和反斜線,應(yīng)使用stripslashes()函數(shù)
最終整合
最后可以建立一個簡單的函數(shù)來解決在PHP中如果安全的進(jìn)行MySQL查詢字符。值得注意的是,如果要輸出到WEB頁面上還需要使用stripslashes。
以下為引用的內(nèi)容: <?PHP
function VerifyInput(input, forceInt = false)
{
if (is_numeric(input))
{
return input;
}
elseif (!forceInt)
{
if (get_magic_quotes_gpc())
{
// if magic quotes is enabled, get rid of those
// pesky slashes
input = stripslashes(input);
}
// convert the input variable into a MySQL safe string.
input = mysql_real_escape_string(input);
return input;
}
else
{
// if input not an integer and forceInt = true,
// kill
die("Invalid Input");
}
}
// _POST['name'] should be a string
// _POST['id'] should be an integer, if not the dies
id = _POST['id'];
name = _POST['name'];
query = "Update users SET name=". VerifyInput(name) ." ";
query .= "Where id=". VerifyInput(id, true);
// query should be safe to run
mysql_query(query);
讓我們看以下的代碼:
以下為引用的內(nèi)容: <?PHP query = "Select news_title, news_text "; query .= "FROM news"; query .= "Where news_id=". _GET['id']; mysql_query(query); ?> |
如果認(rèn)為其中的_GET[‘id’]會永遠(yuǎn)是個數(shù)值型的值那將是很嚴(yán)重的錯誤。最終用戶可以改變這個變量的值,例如"0; Delete FROM news;",那么query語句就會變成下面的值:
Select news_title, news_text FROM news Where news_id=0; Delete FROM news;
這將產(chǎn)生很嚴(yán)重的后果。
驗證數(shù)值型數(shù)據(jù)
數(shù)值型數(shù)據(jù)是最容易驗證的,PHP有一個自帶的函數(shù)叫 is_numeric()可以返回ture值來判斷是否是數(shù)值型,這個函數(shù)并不是MySQL自帶的,因此可在任何數(shù)據(jù)庫平臺的php程序中用于驗證數(shù)字。
下面是修改后的代碼:
以下為引用的內(nèi)容: <?PHP if (!is_numeric(_GET['id'])) { // id's not numeric? // kill the before the query can run die("The id must be numeric!"); } query = "Select news_title, news_text "; query .= "FROM news"; query .= "Where news_id=". _GET['id']; mysql_query(query); ?> |
驗證非數(shù)值型數(shù)據(jù)
非 數(shù)值型數(shù)據(jù)的驗證稍有點麻煩。PHP有個叫Magic Quotes的特殊功能。當(dāng)它激活時,PHP會自動過濾掉_GET和_POST全局變量中的反斜線符號(\),雙引號(”),單引號(’)和空白字符。問 題是并不是所有的服務(wù)器都能打開了這個功能,所以必須檢測服務(wù)器是否開通了這個功能。可以使用get_magic_quotes_gpc()函數(shù)來判定 maigc quotes功能是否打開。
在MySQL查詢語句可以使用mysql_real_escape_string()函數(shù)來增強(qiáng)安全性,代碼如下:
以下為引用的內(nèi)容: <?PHP // Fix a _POST variable called firstName for MySQL firstName = _POST['firstName']; if (get_magic_quotes_gpc()) { // If magic quotes is enabled - turn the string back into an unsafe string firstName = stripslashes(firstName); } // Now convert the unsafe string into a MySQL safe string firstName= mysql_real_escape_string(firstName); // firstName should now be safe to insert into a query ?> |
輸出到頁面
為正確顯示字符中的引號和反斜線,應(yīng)使用stripslashes()函數(shù)
以下為引用的內(nèi)容: <?PHP firstName = _POST['firstName']; if (get_magic_quotes_gpc()) { // If magic quotes is enabled - turn the string back into an unsafe string firstName = stripslashes(firstName); } // Now convert the unsafe string into a MySQL safe string firstName = mysql_real_escape_string(firstName); // Safe query mysql_query("Insert INTO Names VALUES('". firstName ."')"); // Page output should look proper echo "Hello ". htmlentities(stripslashes(firstName)); ?> |
最終整合
最后可以建立一個簡單的函數(shù)來解決在PHP中如果安全的進(jìn)行MySQL查詢字符。值得注意的是,如果要輸出到WEB頁面上還需要使用stripslashes。
以下為引用的內(nèi)容: <?PHP
function VerifyInput(input, forceInt = false)
{
if (is_numeric(input))
{
return input;
}
elseif (!forceInt)
{
if (get_magic_quotes_gpc())
{
// if magic quotes is enabled, get rid of those
// pesky slashes
input = stripslashes(input);
}
// convert the input variable into a MySQL safe string.
input = mysql_real_escape_string(input);
return input;
}
else
{
// if input not an integer and forceInt = true,
// kill
die("Invalid Input");
}
}
// _POST['name'] should be a string
// _POST['id'] should be an integer, if not the dies
id = _POST['id'];
name = _POST['name'];
query = "Update users SET name=". VerifyInput(name) ." ";
query .= "Where id=". VerifyInput(id, true);
// query should be safe to run
mysql_query(query);
?>
關(guān)鍵字:MySQL、PHP、數(shù)據(jù)庫
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機(jī)IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解