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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题