MySQL PHP查询缺陷

我将做一个关于SQL注入的演示,还有什么比做我自己的php查询并将它们显示给类更好的方法.
问题是我的数据库和php代码运行正常,但似乎无法以我编写的编辑形式在UPDATE查询中发现漏洞.
如果有人可以看一下并告诉我如何插入“ DROP TABLE”命令或类似的命令,那将非常有帮助!

<?php
session_start();

require_once 'config.php';
error_reporting(E_ALL);
ini_set('display_errors', 1);

$userid = $_SESSION["userid"];
$fname = $_SESSION["fname"];
$lname = $_SESSION["lname"];
$gender = $_SESSION["gender"];
$email = $_SESSION["email"];

if($_SERVER["REQUEST_METHOD"] == "POST") {

    $userid = trim($_POST["userid"]);
    $fname = trim($_POST["fname"]);
    $lname = trim($_POST["lname"]);
    $gender = trim($_POST["gender"]);
    $email = trim($_POST["email"]);

    $query = "UPDATE user_details SET fname = '$fname', lname = '$lname', gender = '$gender', email = '$email' WHERE userid = '$userid' ";

    if (mysqli_query($link, $query)) {


        echo 'Update complete!';

    }
    echo mysqli_error($link);
    // else { 

    //     echo '<p>' . 'Woah something went really wrong dude' . '</p>' ;
    //     echo mysqli_error($link);
    // }


}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Edit</title>
    <link rel="stylesheet" href="bootstrap.css">
    <style type="text/css">
        body{ font: 14px sans-serif; }
        .wrapper{ width: 350px; padding: 20px; }
    </style>
</head>
<body>
    <div class="container">
        <form class= "form-inline" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
            <label>First Name</label>
            <input type="text" name="fname" class="form-control" value="<?php echo $_SESSION["fname"]; ?>">
            <label>Last Name</label>
            <input type="text" name="lname" class="form-control" value="<?php echo $_SESSION["lname"]; ?>">
            <label>Gender</label>
            <input type="text" name="gender" class="form-control" value="<?php echo $_SESSION["gender"]; ?>">
            <label>Email</label>
            <input type="text" name="email" class="form-control" value="<?php echo $_SESSION["email"]; ?>">
    </div> 
    <div class = "wrapper">
            <label>UserID</label>
            <input type="text" name="userid" class="form-control" value="<?php echo $_SESSION["userid"]; ?>">
            <button type="submit">Submit</button>

    </form>
    <form action="action.php"><button type="submit">Back</button></form>
    </div>    
</body>
</html>
最佳答案
在大多数情况下,mysqli防止多个查询,而MySQL本身具有某些内置的安全措施,以防止意外删除表和数据库.因此,您的DROP TABLE将不起作用.

但是您可以使用MySQL注入演示成功的登录尝试.

创建一个登录表单并将数据传递到验证页面.将您的查询写为

query =  "SELECT * FROM `user`
     WHERE `username` = '." username ".' AND `password` = '." password ".'";

现在,在登录表单中,将用户名输入为’OR’1’=’1,将密码输入为’OR’1’=’1.所以最后完整的查询看起来像这样,

SELECT * FROM `user` WHERE `username`='' OR '1'='1' AND `password`='' OR '1'='1';

这将返回用户表中的所有用户记录,简单地,应用程序将使用第一条记录进行登录.

转载注明原文:MySQL PHP查询缺陷 - 代码日志