dongshan4316 2011-03-14 20:56
浏览 71
已采纳

mysql_real_escape_string还不够好?

So using %27 you can just SQL inject even though data is sanitized with mysql_real_escape_string

%27) SQL INJECTION HERE %2F*

What to do?

Edit with example:

$sql = sprintf("SELECT *, MATCH(post) AGAINST ('%s*' IN BOOLEAN MODE) AS score FROM Posts WHERE MATCH(post) AGAINST('%s*' IN BOOLEAN MODE)",
                mysql_real_escape_string($_GET['searchterm']),
                mysql_real_escape_string($_GET['searchterm']));

$results = $db->queryAsArray($sql);

If you pass in %27) SQL INJECTION HERE %2F* to the searchterm querystring, I get outputted on the page:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'BOOLEAN MODE)' at line 1

Thanks everyone for finding the problem in the db class..

  • 写回答

4条回答 默认 最新

  • dongtang4019 2011-03-14 21:29
    关注

    Reasoning from the method name queryAsArray, it seems that you’re using this DbBase class from the comments of the MySQL functions manual page. If so, it’s the query method that removes the escape character from the escaped quotation marks:

    function query($sql, &$records = null){
        $sql = str_replace(array('\\"', "\\'"), array('"', "'"), $sql);
        // …
    }
    

    Then it’s not a miracle that your example works (I simplified it):

    $input = "', BAD SQL INJECTION --";
    
    $sql = "SELECT '".mysql_real_escape_string($input)."'";
    var_dump($sql);  // string(33) "SELECT '\', BAD SQL INJECTION --'"
    //                      everything’s OK ↑
    
    $sql = str_replace(array('\\"', "\\'"), array('"', "'"), $sql);
    var_dump($sql);  // string(32) "SELECT '', BAD SQL INJECTION --'"
    //                                Oops! ↑
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能