登录注入实例
提示:了解Mysql和php可能更容易理解 构造Mysql语句时利用一些方法使某些不应该返回的数据返回或者执行某种不该执行的命令 这个过程就叫注入 那我们来举个例子:
$sql='select * from users where userName='$userName' and password='$password';
代码逻辑(建议阅读)来自: https://blog.csdn.net/qq_39630587/article/details/79768838
假如我们想登录username为1的用户 我们应该怎么构建的sql语句??? 如下例子
select * from users where userName=1 and password=正确密码
select * from users where userName=1
当然我们不知道正确密码我们可以试试第二种怎么实现(屏蔽 and password=xxx mysql怎么实现注释屏蔽那?? 答案就是#号就是注释号 # 后面的内容都被注释 yes!! 我们找到了 现在开始构造语句
select * from users where userName=1 # and password=xxx
这个语句等效于
select * from users where userName=1
那么我们怎么又不是去提交sql语句给后端 我们看看后端代码
<?php include_once("function/database.php");
// $userName = $_POST['userName']; // $password = $_POST['password']; $userName = addslashes($_POST['userName']); $password = addslashes($_POST['password']); getConnect(); $loginSQL = "select * from users where userName='$userName' and password='$password'"; ..... (省略 )我们提交的账户就是变量 userName 密码就是变量 password 但是这里使用addslashes函数(函数过滤了 单引号(’)双引号(")反斜杠()) 呵???这就刚刚好没屏蔽#号我们你像推理可以得出 账户应该是 1 密码可以随便填(因为被#号注释了 导致这个逻辑始终成立
加强方案
1.替换参数各种敏感字符(# ’ or and 等) 仅仅用addslashes函数是不安全的 保证参数的安全
2.使用pdo分两次发生sql语句 99.9%的安全 其中不安全原因来自于配置问题进行编码边缘操作 提示
现在程序的php sql注入几率很小,加上如果使用pdo 就基本不可能了