douzhanglun4482 2017-12-05 04:51
浏览 450
已采纳

SQL注入中的“参数化查询/预处理语句”如何比转义用户输入更好

There are many articles telling to use parametrized queries instead of escaping user input. But doesn't show any examples. I would like to know a real example where “parameterized queries/prepared statements” prevents SQL injections, while escaping user input can’t. Can you give an example that parameterized query prevent the SQL injection attack when a user input to the query still contains a special character to cause harm? For example, a query that parameterized queries can take care of but the escaping user input can't take care of

Query like = ' or 1=1-- //I would like to know if there is a similar query that "parameterized queries/prepared statements" can prevent from SQL injection, but "escaping user input" can't

  • 写回答

1条回答 默认 最新

  • douzui0143 2017-12-05 05:33
    关注

    Q: Can you give an example that parameterized query prevent the SQL injection attack when a user input to the query still contains a special character to cause harm?

    A: There have been some multibyte character exploits in code that doesn't properly account for character sets, resulting in holes in the escaping mechanism. (Where the "escape string" thinks it's working on a string in particular encoding, but the actual bytes are in a different encoding, and sneakily sliding single quotes into the SQL text.)

    But I don't really think that's the strongest argument for prepared statements with bind placeholders.

    A strong argument is that when we look at the code, we see static SQL text, not dynamically generated...

     $sql = 'SELECT fee, fi FROM fo WHERE fum = ?'; 
     $dbh->prepare($sql);
    

    We see that code, and we look at that SQL text... and we recognize immediately, there is no way that the SQL text is going to be other than what we see. We don't have to look anywhere else in the code; we see it right there on two lines.

    When we see this:

     $sql = "SELECT fee, fi FROM fo WHERE fum = $fumval";
    

    That's double quotes, there's variable interpretation going on. Is $fumval guaranteed to be safe for inclusion in the SQL text, where did $fumval come from? Should there be single quotes around $fumval, or are we guaranteed that it's already enclosed in single quotes?

    Okay, maybe there's a line right before that:

     $fumval = $dbh->quote($unsafe_fumval);
    

    If that line isn't right above the generation of the SQL text, we need to go check... are we guaranteed the $fumval is safe?

    The point is this... the SQL is being dynamically constructed. It might be better if it was done like this:

    $sql = 'SELECT fee, fi FROM fo WHERE fum = ' . $dbh->quote($unsafe_fumval);
    

    For a simple statement, maybe it's six of one and half a dozen of the other. But when SQL statements get larger, involving multiple tables and dozens of column references and values, the dynamic construction gets harder to verify that there are not any problems in it.

    Is it possible to write secure code using dynamically generated SQL and "escape string" processing on values? YES.

    Is it possible to write vulnerable code that uses prepared statements with dynamically generated SQL text? YES.


    It's really the pattern of static SQL text, passing values provided through bind placeholders is what gets us the bang for our buck... code that is written in a way in which we can identify as not vulnerable to SQL injection.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)