dongwei3151 2013-09-04 15:11
浏览 223
已采纳

通过替换单引号来防止SQL注入

I've read that replacing user input isn't safe for the SQL injection. I would like to know (possibly with an example) what's wrong in something like this (PHP):

function formatsql($testo){
    return str_replace("'", "'", $testo);
}

$username = formatsql($_POST["username"]);
$password = formatsql($_POST["password"]);

$query = "SELECT id FROM utenti WHERE user='$username' AND password='$password'";
  • 写回答

1条回答 默认 最新

  • dozug64282 2013-09-04 16:00
    关注

    I would like to know what's wrong in something like this (PHP)

    The idea.

    It has been proved to be flawed long time ago.

    First of all, this code does alter the data. It is good approach for the abstract musings but it will crash your application in the real life. As a matter of fact, it's unacceptable behavior. However, you could escape your quotes instead of replacing them.

    Second, you are (like most of PHP folks) under the delusion that replacing some characters makes your data safe. While it is not. Every PHP user who cares to reinvent a wheel in the field of injection protection always assume that only strings being added to the query. They never realize it explicitly though, nor imagine any other parts exists in SQL query. While such a replacement would be as harmless for any other SQL literal as a chicken. And the very name of your function is a sure proof for my words.
    Say, you have a code like this

    $limit = formatsql($_POST["limit"]);
    $query = "SELECT id FROM utenti LIMIT $limit";
    

    which will welcome any script-kiddie to play with your db.

    Also, there is a term "user input" in your reasoning, which is a sure sign of the second order injection.

    Taking a step further, let's observe two kinds of applications: some sort of silly home page script and relatively big web application. Although your code is quite all right for the former one, in a latter one rules change. Sometimes we can have these two parts of the code

    $username = formatsql($_POST["username"]);
    $password = formatsql($_POST["password"]);
    

    and

    $query = "SELECT id FROM utenti WHERE user='$username' AND password='$password'";
    

    dramatically separated from each other. And here we can slip into many and many troubles, such as double escaping, wrong escaping, no escaping at all.

    This is why manual escaping has been considered bad practice already long time ago.

    Instead, prepared statements have to be used, as they guarantee that

    • complete formatting applied instead of silly "escaping" or "replacing"
    • different formatting applied for the different data types.
    • formatting applied right in place where it have to be - not sooner nor later.
    • proper formatting applied unconditionally, independently from developer's will or air.

    This is why prepared statements considered the only proper way long time ago already.

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办