2-2-1_xss

XSS

什么说XSS

全称: Cross Site Script
中文: 跨站脚本
用途: 盗取用户信息,串改页面实现钓鱼,制造蠕虫等。

概念: 通过 HTML注入串改页面,插入恶意脚本,当用户浏览网页时,实现控制用户浏览器行为的一种攻击方式。

原理

用xss盗取cookie来控制用户权限。

  • 存储型

    • 特点:内容说存储在数据库中
    • 随意访问网页时候被触发。如逛论坛时候被触发。渗透测试一般用alert来显示。黑客则直接提权。
    • 可用firebug插件进行查看。
    • eg: <img src="#" onerror="alert("error")">
  • 放射型

    • 要求用户主动访问携带xss脚本的链接,触发xss。如邮件或者聊天工具发送xss链接等。

前端代码

1
2
3
4
url: great.php?name=<img src=@ onerror=alert(/xss/)>
html:
<img src="@" onerror="alert(/xss/)">

后端代码

1
2
3
4
5
6
7
8
<?php
header("content-type:text/html;charset=utf-8");
if( isset($_REQUEST["name"] ){
$name = $_REQUEST["name"];
}else{
$name = "";
}
echo "welcome",$name;
  • DOM型
    需要用户主动点击访问带xss脚本的链接代码,触发xss

前端代码

1
2
3
# 注意,不在url参数中,而是在hash中
url: localhost:8888/demo/admin/login.html?#Fail.<img src=# onerror=(/xss/)>
html: <span id="xsss"> Fail.<img src="@" onerror="alert(/xss/)">

原理代码

1
2
3
4
<script>
var errorMsg = location.hash.substring(1);
document.getElementById("xsss").innerHTML=decodeURIComponnent(errorMsg);
</script>

案例

存储型xss

1
hello <src="#" onerror="alert(/xss/)">

放射型xss

正常流程

1
2
url: http://localhost:8080/great.php?name=test
返回: welcome test

植入xss

1
2
url: http://localhost:8080/great.php/name=test<img src=a onerror=alert(/xss/)>
html: welcome test<img src=a onerror=alert(/xss/)>

DOM型xss

1
2
登陆失败后url返回hash  #Fail.
url: localhost:8080/login.html?#Fail.

修改hash,后请按地址栏刷新按钮

1
url: localhost:8080/login.html?#Fail.<img src=a onerror=alert(/xss/)>

原理代码

1
2
3
4
<script>
var errorMsg = location.hash.substring(1);
document.getElementById("xsss").innerHTML=decodeURIComponnent(errorMsg);
</script>

总结

xss类型 存储型 放射型 DOM型
数据存储 数据库 URL URL
触发过程 1.黑客构造xss脚本 2- 用户访问污染页面 正常用户访问xss脚本url 正常用户访问xss脚本url
输出主体 后端web应用程序 后端web应用程序 前端Javascript
输出位置 HTTP响应中 HTTP响应中 动态构造的DOM节点