防伪令牌盐有什么用?

发布于 2024-08-04 12:09:40 字数 417 浏览 11 评论 0原文

在ASP.NET MVC 1.0中,有一个新功能可以处理跨站请求伪造安全问题:

 <%= Html.AntiForgeryToken() %>
[ValidateAntiForgeryToken]
public ViewResult SubmitUpdate()
{
    // ... etc
}

我发现每次呈现新表单时,以html表单生成的令牌都会不断变化。

我想知道这些token是怎么生成的?并且当使用某些软件扫描该站点时,它会报告另一个安全问题:会话已修复。为什么?既然token不断变化,怎么会出现这个问题呢?

还有另一个函数,就是 antiForgeryToken 的“salt”,但我真的知道它的用途,即使我们不使用“salt”来生成令牌,令牌也会改变一直以来,那么为什么要有这样的功能呢?

In ASP.NET MVC 1.0, there is a new feature for handling cross site request forgery security problem:

 <%= Html.AntiForgeryToken() %>
[ValidateAntiForgeryToken]
public ViewResult SubmitUpdate()
{
    // ... etc
}

I found the token generated in html form keep changing every time a new form is rendered.

I want to know how these token is generated? And when use some software to scan this site, it will report another security problem: Session fixed. Why? Since the token keep changed, how can this problem come ?

And there is another function, that is "salt" for the antiForgeryToken, but I really know what this used for, even through we don't use "salt" to generate the token, the token will changes all the time, so why have such function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

揽清风入怀 2024-08-11 12:09:40

有关 AntiForgeryToken 的大量信息如下: http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/

这是为了防止跨站点请求伪造 (CSRF)。单击“保存”提交表单并在服务器上执行某些操作(即保存用户的详细信息)是非常标准的行为。您如何知道提交表单的用户就是他们声称的用户?在大多数情况下,您会使用一些 cookie 或基于 Windows 的身份验证。

如果攻击者引诱您访问一个在隐藏的 IFRAME 中提交完全相同表单的网站,该怎么办?您的 cookie 会完整地提交,并且服务器不会将该请求视为与合法请求有任何不同。 (正如 gmail 所发现的:http://www.gnucitizen。 org/blog/google-gmail-e-mail-hijack-technique/

防伪造令牌通过在每次生成页面时创建额外的 cookie 令牌来防止这种形式的攻击。令牌同时存在于表单和 cookie 中,如果表单和 cookie 不匹配,我们就会遭受 CSRF 攻击(因为攻击者将无法使用上述攻击读取防伪令牌)。

从上面的文章来看,盐有什么作用:

Salt 只是一个任意字符串。不同的盐值意味着将生成不同的防伪令牌。这意味着即使攻击者设法以某种方式获取有效令牌,他们也无法在需要不同盐值的应用程序的其他部分重用它。

更新:令牌是如何生成的?下载源,并查看 AntiForgeryDataSerializer、AntiForgeryData 类。

Lots of info on the AntiForgeryToken here: http://blog.codeville.net/2008/09/01/prevent-cross-site-request-forgery-csrf-using-aspnet-mvcs-antiforgerytoken-helper/

This is to prevent a Cross-Site Request Forgery (CSRF). It's pretty standard behavior to click 'Save' sumbit a form and perform some action on the server, i.e. save a user's details. How do you know the user submitting the form is the user they claim to be? In most cases you'd use some cookie or windows based auth.

What if an attacker lures you to a site which submits exactly the same form in a little hidden IFRAME? Your cookies get submitted intact and the server doesn't see the request as any different to a legit request. (As gmail has discovered: http://www.gnucitizen.org/blog/google-gmail-e-mail-hijack-technique/)

The anti-forgery token prevents this form of attack by creating a additional cookie token everytime a page is generated. The token is both in the form and the cookie, if the form and cookie don't match we have a CSRF attack (as the attacker wouldn't be able to read the anti-forgery token using the attack described above).

And what does the salt do, from the article above:

Salt is just an arbitrary string. A different salt value means a different anti-forgery token will be generated. This means that even if an attacker manages to get hold of a valid token somehow, they can’t reuse it in other parts of the application where a different salt value is required.

Update: How is the token generated? Download the source, and have a look at the AntiForgeryDataSerializer, AntiForgeryData classes.

霞映澄塘 2024-08-11 12:09:40

您问了一些不相关的问题:

  1. 我不知道为什么您的安全软件报告“会话已修复”。尝试阅读报告附带的文档
  2. 防伪令牌:

这(大概)用于验证每个请求是否有效。因此,考虑到有人尝试提供指向页面 ?x=1 的链接,如果令牌未同时传递,则请求将被拒绝。此外,它(可以)防止重复发布同一项目。如果您单击“发布”两次,令牌可能会发生变化(每个请求),并且这种情况将通过以下方式检测到:

Session["nextToken"] = token;
WriteToken(token);

...

if( !Request["nextToken"] == Session["nextToken"] ){
    ...
}

// note: order in code is slightly different, you must take the token
// before regenerating it, obviously

我认为这种情况(它保护的攻击)的术语称为“CSRF”(跨站点请求)伪造),这些天。

You've ask a few unrelated problems:

  1. I don't know why your security software is reporting 'session fixed'. Try reading the documentation that comes with the report
  2. The anti-forgery token:

This is used (presumably) to validate that each request is valid. So consider that someone tries to present a link to the page ?x=1, if the token is not also passed, the request will be rejected. Further, it (may) prevent duplicate posting of the same item. If you click 'post' twice, the token will likely change (each request), and this case will be detected via something like:

Session["nextToken"] = token;
WriteToken(token);

...

if( !Request["nextToken"] == Session["nextToken"] ){
    ...
}

// note: order in code is slightly different, you must take the token
// before regenerating it, obviously

I think the term for this (the attack it protects) is called "CSRF" (Cross-Site Request Forgery), these days.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文