Try   HackMD

Cross-Site Request Forgery (CSRF) - 筆記

tags: web security web attacks

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
本站筆記已同步更新到我的個人網站囉! 歡迎參觀與閱讀,體驗不同的視覺感受!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
什麼是Cross-Site Request Forgery (CSRF)

Cross-Site Request Forgery (CSRF),翻譯成跨站請求偽造,是一種常見的攻擊手法,使用者在不知情的情況下,代表了攻擊者向伺服器發送請求。以下簡單說明:

角色

  • A網站:銀行網站
  • B網站:惡意網站
  • 使用者

攻擊流程

  • 使用者登入A網站,獲得A銀行網站的cookie或auth token,並儲存到瀏覽器
  • 使用者並未登出A網站(session是active狀態),此時使用者瀏覽B網站,攻擊者將惡意連結藏在B網站中,誘使使用者點擊
  • 使用者在不知情的情況下對A網站發送請求,惡意連結中,加入A網站的domain,例如:
    http://bank.com/transfer?recipient=attacker&amount=100000
  • 因為使用者已經獲得A網站的auth token或cookie,若伺服器沒有其他條件驗證,攻擊者就可以獲得請求的內容。

通常,狀態改變(state-changing)的請求容易成為CSRF的攻擊目標,狀態改變意指伺服器的狀態改變,例如POST、PUT、DELETE等方法。常見的請求包含修改密碼、購物付款、轉帳、修改安全設定等,攻擊者經常使用社交軟體傳送惡意連結,例如內嵌在email或即時聊天室中。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
如何防止CSRF攻擊

  • 使用CSRF tokens,增加難以預測的參數,產生一個一次性、具有時效的token,先跟伺服器取得token,放進request header (authorization) 中,伺服器檢查是有效token才接受request。基於安全理由,CSRF token並不會存在用戶端瀏覽器的cookie中
  • 多一道驗證機制:對所有含有敏感資訊的操作,增加驗證機制,例如要求使用者再次輸入密碼等。
  • 在伺服器端檢查referrer header:伺服器檢查請求的來源網域
  • Set-Cookie設定SameSite屬性:
Set-Cookie: <cookie-name>=<cookie-value>; SameSite=Strict
Set-Cookie: <cookie-name>=<cookie-value>; SameSite=Lax

設定Strict:cookie只和same site的請求一起送出
設定Lax:只有特定的cross-site請求可以帶著cookie一起送出,詳情可以參考這篇文章


參考資料

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
 本站內容僅為個人學習記錄,如有錯誤歡迎留言告知、交流討論!