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 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi