首页 > 解决方案 > 如何在这个登录页面模拟 XSS

问题描述

登录.php

<html>
<head>
<title> Login </title>
</head>
<style>
    #footer {
   position:absolute;
   bottom:0;
   width:100%;
   height:60px;
}
</style>
<body>
<div class="container">
    <form method="post" action="logged.php">
        <div id="div_login">
            <h1>Login</h1>
            <div>
                <input type="text" class="textbox" id="txt_uname" name="txt_uname" placeholder="Username" />
            </div>
            <div>
                <input type="password" class="textbox" id="txt_pwd" name="txt_pwd" placeholder="Password"/>
            </div>
            <div>
                <input type="submit" value="Submit" name="but_submit" id="but_submit" />
            </div>
        </div>
        <div class='container' id='footer'>
    <h3>Leave us some feedback ! </h3>
    <div>
                <input type="text" class="textbox" id="txt_feedback" name="txt_feedback" placeholder="Feedback" />
            </div>
</div>
    </form>
</div>
</body>
</html>

记录的.php

<?php
include "configs.php";

if(isset($_POST['but_submit'])){

    $uname = mysqli_real_escape_string($con,$_POST['txt_uname']);
    $password = $_POST['txt_pwd'];
    $fback=$_POST['txt_feedback'];

    if ($uname != "" && $password != ""){

        $sql_query = "select count(*) as cntUser from users where username='".$uname."' and password='".$password."'";
        $result = mysqli_query($con,$sql_query);
        $row = mysqli_fetch_array($result);
        $sql = "UPDATE users SET feedback='$fback' WHERE username='".$uname."' and password='".$password."'";
        if ($con->query($sql) === TRUE) {
            echo "Record updated successfully";
        } else {
            echo "Error updating record: " . $con->error;
        }
        $count = $row['cntUser'];

        if($count > 0){
            $_SESSION['uname'] = $uname;
            header('Location: home.php');
        }else{
            echo "Invalid username and password";
        }

    }

}

我希望反馈输入字段具有 xss 漏洞,以便我可以通过提供脚本作为字段的输入来触发 JavaScript 警报框。我该如何做到这一点?截至目前,它只是进入主页而不会触发任何警报框。我已经在这个页面上实现了 SQL 注入,效果很好。

标签: javascriptphphtmlxss

解决方案


我认为您将其用作有关如何防止 XSS 的学习练习

更改以下内容:

echo "Invalid username and password";

为了实现XSS:

echo "Invalid username:".$_POST['txt_uname']." and password";

然后在用户名字段中输入以下文本

<image src="x" onerror="alert('XSS')">

为了防止像我上面所做的那样回显变量时出现 XSS,您可以使用 htmlentities() 包装变量,如下面的代码:

echo "Invalid username:".htmlentities($_POST['txt_uname'])." and password";

这是实现 XSS 的几种方法之一,因此您最好从特定站点学习,这些站点更详细地了解防止 XSS 的不同方法和解决方案。


推荐阅读