国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL和PHP中的SQL注入式漏洞解決方法

2024-07-24 12:55:57
字體:
供稿:網(wǎng)友

sql注入式漏洞是許多php程序的主要安全危害,產(chǎn)生的原因是在向數(shù)據(jù)庫執(zhí)行插入等語句時,者允許最終用戶操作變量(例如根據(jù)表單提交內(nèi)容顯示相應(yīng)信息),通常是_get、_post或_session等全局變量。

讓我們看以下的代碼:

以下為引用的內(nèi)容: <?php
query = "select news_title, news_text ";
query .= "from news";
query .= "where news_id=". _get['id'];

mysql_query(query);
?>

如果認為其中的_get[‘id’]會永遠是個數(shù)值型的值那將是很嚴重的錯誤。最終用戶可以改變這個變量的值,例如"0; delete from news;",那么query語句就會變成下面的值:

select news_title, news_text from news where news_id=0; delete from news;

這將產(chǎ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 script 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ù)來增強安全性,代碼如下:

以下為引用的內(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中如果安全的進行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 script
die("invalid input");
}
}

// _post['name'] should be a string
// _post['id'] should be an integer, if not the script 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);
?>
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 绥江县| 缙云县| 红河县| 抚顺市| 鄄城县| 葫芦岛市| 嘉荫县| 苏尼特右旗| 庆元县| 潮安县| 渭南市| 永川市| 遂平县| 贡山| 岳阳县| 建昌县| 大连市| 民权县| 融水| 长汀县| 峨眉山市| 涡阳县| 盐边县| 沙河市| 绥芬河市| 聂荣县| 洛浦县| 甘孜| 当雄县| 五家渠市| 千阳县| 潮安县| 银川市| 康保县| 平昌县| 呼玛县| 盈江县| 三原县| 资中县| 虹口区| 大安市|