2011-01-28 28 views
2

我还在学习有关SQL注入,但总是对我来说最好的办法是使用的例子,所以这是我的代码部分:我可以对此代码执行SQL注入吗?

$sql = "INSERT INTO `comments` (`id`, `idpost`, `comment`, `datetime`, `author`, `active`) 
     VALUES (NULL, '" . addslashes($_POST['idcomment']) . "', '" . 
     addslashes($_POST['comment']) . "', NOW(), '" . 
     addslashes($_POST['name']) . "', '1');"; 

    mysql_query($sql); 

知道了所有的POST变量是由用户输入,可以你向我展示了如何为这个脚本注入?所以我可以更多地了解这个漏洞。谢谢!

我的数据库服务器是MySQL。

+2

请读得好的问题,是如何使注射,而不是如何防止它 – DomingoSL 2011-01-28 18:31:26

+0

请使用[PDO ](http://net.tutsplus.com/tutorials/php/why-you-should-be-using-phps-pdo-for-database-access/)来保护自己免受sql注入。 PDO准备好的声明(请参阅PDO链接)是安全的。 – Alfred 2011-01-28 22:17:07

回答

3

大多数其他答案似乎完全错过了这个问题的观点。

也就是说,根据上面的示例(尽管代码不遵循最佳实践使用mysql_real_escape_string()),但使用addslashes()时无法注入任何真正有害的东西。

但是,如果你忽略它,用户可以输入一个字符串到name场,看起来像:

some name'; DROP TABLE comments; -- 

的目标是结束当前的语句,然后执行自己。 --是一个注释,用于确保处理注入的字符串后通常不会出现任何内容。

但是(再次),我的理解是,默认情况下MySQL在单个语句执行结束时自动关闭数据库连接。所以,即使我尝试删除一个表,MySQL也会导致第二条语句失败。

但是,这不是唯一的SQL注入类型,我建议读一些关于该主题的内容。我的研究从dev.mysql.com打开了这个文件,这是相当不错的:http://dev.mysql.com/tech-resources/articles/guide-to-php-security-ch3.pdf


编辑,又一想:

根据一旦进入到数据库中发生了什么数据,我可能不会想要注入任何SQL。我可能希望注入一些在将数据发布回Cross-Site Scripting (XSS)攻击中的网页时运行的HTML/JavaScript。这也是需要注意的事情。

5

请勿使用addslashes(),请始终使用mysql_real_escape_string()。有已知的edge cases where addslashes() is not enough

如果从头开始新的东西,最好使用支持预先准备好的语句(如PDO或mysqli)的数据库包装器。

+2

+ PDO/mysqli +准备的statemets建议 - 绝对是一个很好的方法来做到这一点。 – mfonda 2011-01-28 18:07:20

1

正如之前所说,字符串,使用mysql_real_escape_string()代替addslashes()为整数,使用intval()

/* little code cleanup */ 

$idcomment = intval($_POST['idcomment']); 
$comment = mysql_real_escape_string($_POST['comment']); 
$name = mysql_real_escape_string($_POST['name']); 

$sql = "INSERT INTO comments (idpost, comment, datetime, author, active) 
     VALUES ($idcomment, '$comment', NOW(), '$name', 1)"; 

mysql_query($sql); 
0

Addslashes只处理引号。

但这里也有一些比较重要的情况:

Be careful on whether you use double or single quotes when creating the string to be escaped: 

$test = 'This is one line\r\nand this is another\r\nand this line has\ta tab'; 

echo $test; 
echo "\r\n\r\n"; 
echo addslashes($test); 

$test = "This is one line\r\nand this is another\r\nand this line has\ta tab"; 

echo $test; 
echo "\r\n\r\n"; 
echo addslashes($test); 

还有一句:

In particular, MySQL wants \n, \r and \x1a escaped which addslashes does NOT do. Therefore relying on addslashes is not a good idea at all and may make your code vulnerable to security risks. 

还有一:

Be very careful when using addslashes and stripslashes in combination with regular expression that will be stored in a MySQL database. Especially when the regular expression contain escape characters! 

To store a regular expression with escape characters in a MySQL database you use addslashes. For example: 

$l_reg_exp = addslashes(�[\x00-\x1F]�); 

After this the variable $l_reg_exp will contain: [\\x00-\\x1F]. 

When you store this regular expression in a MySQL database, the regular expression in the database becomes [\x00-\x1F]. 

When you retrieve the regular expression from the MySQL database and apply the PHP function stripslashes(), the single backslashes will be gone! 

The regular expression will become [x00-x1F] and your regular expression might not work! 

记住,魔术可能会发生:

  • addslashes从数据库

你的范例检索后可能会错过一些

  • 添加到数据库
  • 之前只是一个摘录。真正的问题可能在此处不可见还有


    (基于php.net这是非常往往比手动本身多提宝贵意见)

  • 相关问题