XSS/CSRF/Clickjacking/MITM 名詞整理
此文章是 FrontendMaster 上的 Advanced Web Development Quiz 課程筆記
問題
Connect the terms with their definitions
Match the answers to the correct options.
[1] XSS
[2] CSRF
[3] UI Redressing
[4] MITM
[a] allows attackers to inject malicious scripts into web pages viewed by others
[b] tricks users into interacting with disguised or hidden elements
[c] tricks users into executing unwanted actions by exploiting their authenticated session
[d] allows attackers to intercept and modify communication between two parties without their knowledgeXSS - Cross-Site Scripting
攻擊者將惡意的腳本注入網頁中來竊取資料,通常是因為開發者沒有 sanitization 使用者輸入的內容導致的
範例:在部落格編輯器用 innerHTML 做即時預覽,若使用者插入無
src但有onerror事件的<img>標籤,就能透過這個方法取得document.cookie,並帶 cookie 發請求至惡意的 API,或是 CSRF、竊取 localStorage、操作 DOM 之類的行為防範:永遠不信任使用者輸入內容,sanitization 輸入(可以直接使用成熟的 sanitizer),並盡量避免
innerHTML、document.write、eval等方法,搭配 CSP 會更好
CSRF - Cross-Site Request Forgery
利用使用者已驗證的 session,誘導執行不想要的動作,如自動送出表單
範例:登入銀行後 cookie 未設定 SameSite 或未驗證 CSRF token,去到惡意網站時,該站自動透過
onload對銀行 API 發起請求,可以偽裝成使用者本人的操作防範:用 CSRF token,讓伺服器必須要驗證客戶端的 token。設定
SameSitecookie 使其僅限同站存取,並搭配檢查Origin/Referer,對敏感操作重新驗證
UI Redressing - Clickjacking
用偽裝或隱藏的元素誘導使用者互動,例如疊加一個不可見的 iframe
範例:惡意網站用一個透明的銀行
iframe蓋住按鈕,使用者點擊時觸發 iframe 內的按鈕,來觸發操作防範:使用 CSP 的
frame-ancestors
MITM - Man-in-the-Middle
攻擊者攔截並修改使用者端與伺服器之間的通訊
範例:網站使用 HTTP 會讓攻擊者可以讀取 headers、data,甚至修改內容,把回應的內容注入惡意程式碼,或把轉帳收帳帳號替換
防範:強制使用 HTTPS/TLS,做到加密、完整性、伺服器身份驗證,讓中間人無法透過攔截通訊來偷看或竄改內容
答案
[1] XSS
[a] allows attackers to inject malicious scripts into web pages viewed by others
[2] CSRF
[c] tricks users into executing unwanted actions by exploiting their authenticated session
[3] UI Redressing
[b] tricks users into interacting with disguised or hidden elements
[4] MITM
[d] allows attackers to intercept and modify communication between two parties without their knowledge