关注这个靶场的其他相关笔记:XSS - LABS —— 靶场笔记合集
0x01:过关流程
进入靶场,首先看传参和页面的回显点,如下图所示:
使用基础的 Payload 进行测试:
<script>alert(1)</script>
页面直接将我们传入的内容,又返回出来了,且没有弹窗,右击页面,查看源代码,可以发现 <
被转换成 HTML 实体编码了:
不过我们也发现了一个漏网之鱼,可以看到,下面的 value 字段中的 <
括号是未被转换的:
所以我们构造 Payload 尝试闭合 input 标签并弹窗,可以看到,轻松过关:
"><script>alert(1)</script>
0x02:源码分析
下面是 XSS LABS Level 2 的源码,以及我对其的部分注解:
<!DOCTYPE html><!--STATUS OK-->
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
// 修改 alert 的默认行为,跳到下一关
window.alert = function() {
confirm("完成的不错!");
window.location.href = "level3.php?writing=wait";
}
</script>
<title>欢迎来到level2</title>
</head>
<body>
<h1 align=center>欢迎来到level2</h1>
<?php
ini_set("display_errors", 0);
$str = $_GET["keyword"]; // 获取通过 GET 方式传入的 keyword 对应的值
echo "<h2 align=center>没有找到和" . htmlspecialchars($str) . "相关的结果.</h2>" . '<center>
<form action=level2.php method=GET>
<input name=keyword value="' . $str . '">
<input type=submit name=submit value="搜索"/>
</form>
</center>'; // 很明显的 h2 使用了 htmlspecialchars 转换了编码,但是 input 框没有过滤
?>
<center><img src=level2.png></center>
<?php
echo "<h3 align=center>payload的长度:" . strlen($str) . "</h3>";
?>
</body>
</html>