SQL注入绕过PHP过滤器



在工作中尝试学习各种SQL注入技术时,我陷入了以下问题。我正试图为以下代码编写一个SQL注入。我的目标是只输入已知注册用户的用户名(例如:test(,并附加额外的输入,绕过以下过滤器,最终将被注入最后一行的SQL语句中,使其成为true,并以注册用户的身份登录。我有点不知道如何绕过这一系列的过滤器(尽管我猜我可以使用空白字符的替代品来通过其中一个检查?(什么样的输入可以绕过这一点?谢谢

function sqli_filter($string) {
$filtered_string = $string;
$filtered_string = str_replace("--","",$filtered_string);
$filtered_string = str_replace(";","",$filtered_string);
$filtered_string = str_replace("/*","",$filtered_string);
$filtered_string = str_replace("*/","",$filtered_string);
$filtered_string = str_replace("//","",$filtered_string);
$filtered_string = str_replace(" ","",$filtered_string);
$filtered_string = str_replace("#","",$filtered_string);
$filtered_string = str_replace("||","",$filtered_string);
$filtered_string = str_replace("admin'","",$filtered_string);
$filtered_string = str_replace("UNION","",$filtered_string);
$filtered_string = str_replace("COLLATE","",$filtered_string);
$filtered_string = str_replace("DROP","",$filtered_string);
return $filtered_string;
}
function login($username, $password) {
$escaped_username = $this->sqli_filter($username);
// get the user's salt
$sql = "SELECT salt FROM users WHERE eid='$escaped_username'";
$result = $this->db->query($sql);
$user = $result->next();
// make sure the user exists
if (!$user) {
notify('User does not exist', -1);
return false;
}
// verify the password hash
$salt = $user['salt'];
$hash = md5($salt.$password);
error_log(print_r($escaped_username));
$sql = "SELECT user_id, name, eid FROM users WHERE eid='$escaped_username' AND password='$hash'";

请不要构建自己的过滤器。当你意识到自己忽略了一些事情时,你会后悔的。

下面是一个字符串的例子,它会注入你的过滤器:

'   union   all select  password    from    users   where   type    =   'Admin

(请注意,这些是制表符,而不是单引号后的空格(

这是一个演示https://3v4l.org/o8ClJ.您的字符串显示为:

SELECT salt FROM users WHERE eid='' union   all select  password    from    users   where   type    =   'Admin'

它将是可执行的SQL(假设存在列(。

使用参数化查询和准备好的语句。它将处理你需要做的一切。

附加阅读:如何防止PHP中的SQL注入
https://www.owasp.org/index.php/SQL_Injection_Prevention_Cheat_Sheet

更改处理SQL注入的方式。请改用参数。

$stmt = $this->db->prepare("SELECT salt FROM users WHERE eid= ?");
$stmt->bind_param("si", $username);

有关详细信息,请查看此链接。

最新更新