• 主页
  • 相册
  • 随笔
  • 目录
  • 存档
Total 244
Search AboutMe

  • 主页
  • 相册
  • 随笔
  • 目录
  • 存档

跨站点请求伪造

2020-06-17

《白帽子讲Web安全》笔记

1. 简介

CSRF的全名是Cross Site Request Forgery,翻译成中文就是跨站点请求伪造。

1.1. 与xss区别

从 COOKIE 的角度说,XSS 是盗取COOKIE 干坏事,而 CSRF 是借 COOKIE 干坏事,实质上并没拿到受害者的 COOKIE。


XSS:用户–>信任的网页A–>不知情的跳转–>未知的网页B–>恶意操作(窃取A站的用户数据)

CSRF: 用户–>未知的网页A–>不知情的跳转–>信任的网页B–>恶意操作(以B站用户身份瞎搞)


  • xss:用户过分信任网站,放任来自浏览器地址栏代表的那个网站代码在自己本地任意执行。如果没有浏览器的安全机制限制,xss代码可以在用户浏览器为所欲为;
  • csrf:网站过分信任用户,放任来自所谓通过访问控制机制的代表合法用户的请求执行网站的某个特定功能。

2. 进阶

浏览器所持有的Cookie分为两种:一种是“Session Cookie”,又称“临时Cookie”;另一种是“Third-party Cookie”,也称为“本地Cookie “

两者的区别在于,Third-party Cookie是服务器在Set-Cookie时指定了Expire时间,只有到了Expire时间后Cookie才会失效,所以这种Cookie会保存在本地;而Session Cookie则没有指定Expire时间,所以浏览器关闭后,Session Cookie就失效了。

  • 访问这个页面,发现浏览器同时接收了这两个Cookie
  • 这时再打开一个新的浏览器Tab页,访问同一个域中的不同页面。因为新Tab页在同一个浏览器进程中,因此Session Cookie将被发送。
  • 此时在另外一个域中,有一个页面http://www.b.com/csrf=test.html,此页面构造了CSRF以访问www.a.com,这时却会发现,只能发送出Session Cookie,而Third-party Cookie被禁止了。

2.1. get/post

  • x-frame-options:deny: 禁止iframe调用

2.2. CSRF Worm

3. CSRF的防御

3.1. 验证码

csrf攻击的过程,往往是在用户不知情的情况下构造了网络请求。而验证码,则强制用户必须与应用进行交互,才能完成最终请求。

3.2. Referer Check

比如一个“论坛发帖”的操作,在正常情况下需要先登录到用户后台,或者访问有发帖功能的页面。在提交“发帖”的表单时,Referer的值必然是发帖表单所在的页面。如果Referer的值不是这个页面,甚至不是发帖网站的域,则极有可能是CSRF攻击。

Referer Check的缺陷在于,服务器并非什么时候都能取到Referer

  • 关于Http请求header之Referer讲解 - 简书

3.3. Anti CSRF Token

3.3.1. Token的使用原则

4. Post vs Get

GET 有个安全隐患在于 url 可能被服务器的 access log 记录并保存.
敏感字符串不建议通过 GET 方法请求.

在利用from发送请求的时代.get请求会夹带信息在地址栏里,而post不会.post会把信息放在body里面.

因为参数放在地址栏里的get请求,会让别人看得见.一些信息难免会暴露.

HTTP Methods

  • GET
  • POST
  • PUT
  • HEAD
  • DELETE
  • PATCH
  • OPTIONS
GETPOST
BACK button/ReloadHarmlessData will be re-submitted (the browser should alert the user that the data are about to be re-submitted)
BookmarkedCan be bookmarkedCannot be bookmarked
CachedCan be cachedNot cached
Encoding typeapplication/x-www-form-urlencodedapplication/x-www-form-urlencoded or multipart/form-data. Use multipart encoding for binary data
HistoryParameters remain in browser historyParameters are not saved in browser history
Restrictions on data lengthYes, when sending data, the GET method adds the data to the URL;and the length of a URL is limited (maximum URL length is 2048 characters)No restrictions
Restrictions on data typeOnly ASCII characters allowedNo restrictions. Binary data is also allowed
SecurityGET is less secure compared to POST because data sent is part of the URL, Never use GET when sending passwords or other sensitive information!POST is a little safer than GET because the parameters are not stored in browser history or in web server logs
VisibilityData is visible to everyone in the URLData is not displayed in the URL

5. 其他参考

  • HTTP Methods GET vs POST
  • 如何用简洁生动的语言说明 XSS 和 CSRF 的区别? - 知乎
  • Security
  • Web Security
  • Note
html5安全
浏览器安全
  1. 1. 1. 简介
    1. 1.1. 1.1. 与xss区别
  2. 2. 2. 进阶
    1. 2.1. 2.1. get/post
    2. 2.2. 2.2. CSRF Worm
  3. 3. 3. CSRF的防御
    1. 3.1. 3.1. 验证码
    2. 3.2. 3.2. Referer Check
    3. 3.3. 3.3. Anti CSRF Token
      1. 3.3.1. 3.3.1. Token的使用原则
  4. 4. 4. Post vs Get
  5. 5. 5. 其他参考
© 2024 何决云 载入天数...