CSRF防御方法
我正在尝试使用每个表单中的隐藏密钥和特殊的临时 cookie 来保护我的 .NET 网站免受 CSRF 攻击,因此当用户发布表单时,我可以比较临时 cookie 密钥和表单中的隐藏密钥。
但我不想使用 Session 或其他共享对象来保留这些临时密钥,所以我想出了这种方法:
- 浏览器请求表单(GET)。
- 应用程序生成一个密钥,[userId] + [当前日期时间],对称 使用我的应用程序的密钥加密 知道。
- 应用程序将该密钥放在隐藏字段中 在表单中,并发送了一个 cookie 那把钥匙也是。浏览器 POST 表单。
应用程序确保:
- cookie 值和隐藏表单值相同。
- 从解密后的值中可以得到一个[userId],它就是当前的用户id。
- 可以从解密的值中获取[DateTime]。
- 获取的[DateTime] 时间不超过 15 分钟。
否则,拒绝 POST 并显示错误。
你看到什么缺陷了吗?
亲切的问候。
I'm trying to protect my .NET web site against CSRF attacks using a hidden key in every form and an special temp cookie, so when the user POST the form I can compare the temp cookie key and the hidden key in the form.
But I don't wanna use Session
or other shared object to keep those temp keys, so I've come up with this way:
- Browser ask for a form (GET).
- App generates a key, [userId] +
[currentDateTime], symmetrically
encrypted with a key that my app
knows. - App put that key in a hidden field
in the form, and sent a cookie with
that key too. Browser POST the form. App ensures that:
- The cookie value and hidden form value are the same.
- Can obtain an [userId] from the decrypted value, and it's the current user id.
- Can obtain a [DateTime] from the decrypted value.
- [DateTime] obtained is not more than 15 min old.
Otherwise, reject POST and show error.
Do you see any flaw?
Kind regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您描述的策略通常有效,被称为“双重提交饼干”。 您应该了解一些事情
The strategy you describe works in general, and is known as "Double Submitting Cookies". BUT there are a few things that you should know about
为什么不使用内置的 ViewState 用户密钥?使用用户的登录名填充该内容即可完成。然而,这不会过期。
如果您没有经过身份验证的用户,或者因为不喜欢视图状态而不喜欢这种方法,那么您可以编写一个 HTTP 模块,该模块插入一个隐藏的表单字段,就像我在 codeplex - 您应该能够调整其到期时间。
Why don't you use the built in ViewState userkey? Populate that with the user's login name and you're done. This however doesn't do expiry.
If you don't have authenticated users or don't like that approach because you don't like viewstate then you could write an HTTP Module which inserts a hidden form field like the one I wrote and published on codeplex - you should be able to adjust that for expiry.