DVWA-CSP Bypass

 CSP(Content-Security-Policy):指的是内容安全策略,它的本质是建立一个白名单,告诉浏览器哪些外部资源可以 加载和执行。我们只需要配置规则,如何拦截由浏览器自己来实现。

通常有两种方式来开启 CSP,一种是设置 HTTP 首部中的 Content-Security-Policy,一种              是设置 meta 标签的方式 <meta http-equiv="Content-Security-Policy">

CSP 也是解决 XSS 攻击的一个强力手段

script-src脚本:只信任当前域名

object-src:不信任任何URL,即不加载任何资源

style-src样式表:只信任http://cdn.example.org和THIRDPARTY | 名古屋でWebのコンサルティング・ディレクションをしています。

child-src:必须使用HTTPS协议加载。这个已从Web标准中删除,新版本浏览器可能不支持。

其他资源:没有限制其他资源

当启用CSP后,不符合CSP的外部资源会被阻止加载。

 一,low

源码分析:

<?php

$headerCSP = "Content-Security-Policy: script-src 'self' https://pastebin.com  example.com code.jquery.com https://ssl.google-analytics.com ;"; // allows js from self, pastebin.com, jquery and google analytics.

header($headerCSP);

# https://pastebin.com/raw/R570EE00

?>
<?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>
';

从代码中可以看出只信任以下域名:https://pastebin.com ,example.com code.jquery.com, https://ssl.google-analytics.com

首先访问https://pastebin.com网址

在New Paste里面写上JS代码:

 

 拉到最下面,点击

 

 

 

 点击include会弹窗。(如果一直没弹窗是因为https://pastebin.com这个网址是美国的)

二,Medium

源码分析:

<?php

$headerCSP = "Content-Security-Policy: script-src 'self' 'unsafe-inline' 'nonce-TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA=';";

header($headerCSP);

// Disable XSS protections so that inline alert boxes will work
header ("X-XSS-Protection: 0");

# <script nonce="TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA=">alert(1)</script>

?>
<?php
if (isset ($_POST['include'])) {
$page[ 'body' ] .= "
    " . $_POST['include'] . "
";
}
$page[ 'body' ] .= '
<form name="csp" method="POST">
    <p>Whatever you enter here gets dropped directly into the page, see if you can get an alert box to pop up.</p>
    <input size="50" type="text" name="include" value="" id="include" />
    <input type="submit" value="Include" />
</form>
';

unsafe-inline:当csp有Unsafe-inline时, 并且受限于csp无法直接引入外部js, 不过当frame-src
为self, 或者能引入当前域的资源的时候, 即有一定可能能够引入外部js。

nonce-source,仅允许特定的内联脚本块。如源码中:nonce-TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA='

直接输入备注即可

<script nonce="TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA=">alert(1)</script>

 

 三,Hight

源码分析:

<?php
$headerCSP = "Content-Security-Policy: script-src 'self';";

header($headerCSP);

?>
<?php
if (isset ($_POST['include'])) {
$page[ 'body' ] .= "
    " . $_POST['include'] . "
";
}
$page[ 'body' ] .= '
<form name="csp" method="POST">
    <p>The page makes a call to ' . DVWA_WEB_PAGE_TO_ROOT . '/vulnerabilities/csp/source/jsonp.php to load some code. Modify that page to run your own code.</p>
    <p>1+2+3+4+5=<span id="answer"></span></p>
    <input type="button" id="solve" value="Solve the sum" />
</form>

<script src="source/high.js"></script>
';
function clickButton() {
    var s = document.createElement("script");
    s.src = "source/jsonp.php?callback=solveSum";
    document.body.appendChild(s);
}

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();
    });
}

Coentent-Security除了自身,其余的外部资源全部过滤了。

 按钮执行的逻辑:

1,点击按钮,js创建一个script标签
2,src指向source/jsonp.php?callback=solveNum
3,appendChild(s)函数又将source/jsonp.php?callback=solveNum加入到DOM中
4,源码定义了一个solveNum函数,利用obj传参,若answer在obj中将会被执行
5,document对象可以访问HTML中的内容
6,getElementById(id)可返回对拥有指定 ID 的第一个对象的引用
7,innerHTML 属性设置或返回表格行的开始和结束标签之间的 HTML
8,这里的 script 标签会把远程加载的 solveSum({“answer”:“15”}) 当作 js 代码执行, 然后这个函数就会在页面显示答案

src指向的source/jsonp.php?callback=solveNum未作任何XSS的过滤,可以构造payload:

include=<script src="source/jsonp.php?callback=alert('cobot');"></script>

 四,Impossible

源码分析:

<?php

$headerCSP = "Content-Security-Policy: script-src 'self';";

header($headerCSP);

?>
<?php
if (isset ($_POST['include'])) {
$page[ 'body' ] .= "
    " . $_POST['include'] . "
";
}
$page[ 'body' ] .= '
<form name="csp" method="POST">
    <p>Unlike the high level, this does a JSONP call but does not use a callback, instead it hardcodes the function to call.</p><p>The CSP settings only allow external JavaScript on the local server and no inline code.</p>
    <p>1+2+3+4+5=<span id="answer"></span></p>
    <input type="button" id="solve" value="Solve the sum" />
</form>

<script src="source/impossible.js"></script>
';
function clickButton() {
    var s = document.createElement("script");
    s.src = "source/jsonp_impossible.php";
    document.body.appendChild(s);
}

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();
    });
}

与high等级不同,impossible等级执行JSONP调用,但不使用callback参数,而是硬编码要调用的函数。CSP设置只允许本地服务器上的外部javascript,不允许内联代码。

可以看到,callback传参删除,原本为变量的地方写死了。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值