douqiang6448 2012-11-14 21:25
浏览 44
已采纳

不使用mysqli_real_escape_string()输入sanitizaton

I want to strip user input of SQL executable statements, but the way the codebase is I don't want to establish a DB connection which mysqli_real_escape_string(connection, string) requires.

What is an elegant regex/replace one-liner to sanitize the strings?

<?php
$naughty = array("\\", "{", "}", ")", "(", "[", "]", "\"", ";", ":", ">", "&");
$sanitized = str_replace($naughty, " ", $input_string);
?> 

---UPDATE---- Example solution. Thanks for the input guys.

<?PHP 
      $testsql = "';\"\\ bork bork bork %A 0x33
";
      echo $testsql;
     /* $translate = array("0x" => "[HEX]",  ";" => "[SEMICOLON]", "'" => "[QUOTE]", "%" => "[PERCENT]","\"" => "[DOUBLEQUOTE]",
      "\\" => "[BACKSLASH]" ); */
      $translate = array("0x" => " ",  ";" => " ", "'" => " ", "%" => " ","\"" => " ", "\\" => " ");
      echo "<br>";
      $testsql=strtr($testsql, $translate);
      echo $testsql;
?>
  • 写回答

2条回答 默认 最新

  • duanjuebin2519 2012-11-14 22:02
    关注

    While I definitely agree with the comments and answer that using prepared statements, and not doing this yourself is both smarter, better in many ways and always recommended when you can use them. While you may not have a security problem now, one may suddenly appear in the future; and the risk with prepared statements is just much, much lower. If you can find a workaround for it, do it!

    I feel like I have to give that disclaimer, in order to be taken seriously here.

    Because, I also believe it's definitely possible to do all the escaping manually; given that the environment is controlled. This is no different than escaping for other formats, such as javascript. Sometimes this is needed. I, for one, once had a need to create a SQL export script with no database available.

    To do this correctly, the following must be assumed:

    • The input string is UTF-8. You validated this.
    • When you execute the mysql string later down the road, the full sql statement is in UTF-8, and so is the connection.
    • No crazy stuff is going on with php's mbstring function overloading.
    • You don't trust my untested code, and verify what I'm saying ;)

    Why is UTF-8 so important? Read this article

    Given those two points, to emulate mysql_real_escape_string, you must escape the following characters:

    NUL (ASCII 0), 
    , , \, ', ", and Control-Z. 
    

    Source: php.net

    To do this, this should be sufficient:

    $translate = array(
      "\x00" => '\\0',
      "
    "   => '\
    ',
      ""   => '\',
      "'"    => '\\\'',
      "\""   => '\\"',
      "\x1a" => '\\Z',
    );
    
    $output = "'" . strtr($input, $translate) . "'";
    

    As you can see, I did still make sure that my output was surrounded by quotes. Without that, this escaping technique (nor mysqli::real_escape_string) would not be sufficient.

    Alternative techniques I've seen:

    • Splitting up every byte, and surrounding it with MySQL's CHAR function.
    • Base64-encoding the string in PHP, and base64-decoding it with MySQL 5.6's FROM_BASE64 function.
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗