对预防CSRF的质疑

发布于 2024-12-01 01:55:03 字数 359 浏览 2 评论 0原文

我对 CSRF 预防有一个疑问。许多网站表示可以通过使用每个会话随机生成的“令牌”来防止 CSRF。

现在我的疑问是, 假设我有一个像这样的函数:

$.post("abcd.php",{'fbuid':userid,'code':'<?php echo  md5($_SESSION['randcode']); ?>'}

现在这个 md5 哈希显然可以通过源代码对任何黑客可见。他可以简单地打开这个页面,生成一个令牌,并保持页面打开,这样会话就不会被破坏,并使用另一个选项卡或其他任何东西来开始黑客攻击,

不是吗?

或者我对代币的想法不正确?

感谢您的帮助:D

I had one doubt about CSRF prevention. A lot of sites say that CSRF can be prevented by using 'tokens' which are randomly generated per session.

Now my doubt is,
suppose i have a function like :

$.post("abcd.php",{'fbuid':userid,'code':'<?php echo  md5($_SESSION['randcode']); ?>'}

now this md5 hash would obviously be visible to any hacker through the source code.He could simply open this page, generate a token, and keep the page open, so that the session doesn't get destroyed, and useanother tab or anything else , to start hacking,

No ?

Or is my idea of tokens incorrect ?

Thanks for your help :D

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

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

发布评论

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

评论(4

背叛残局 2024-12-08 01:55:03

我认为您误解了需要做什么。为了防止 CSRF,您需要创建一个令牌并为该会话保存它。然后,您需要使用该令牌附加所有提交和 AJAX 调用。

为了让其他人将您转到您网站上的页面,他们需要在同一会话中有权访问该请求。确实,人们可以解析 HTML 并查找令牌。但是,当他们尝试在您的网站上请求 http 调用时,他们将创建一个会话。新会话将有一个新令牌,该令牌与传递的令牌不匹配。

接下来您会问如果您可以复制 cookie 和会话 ID 结果会怎么样。这不是受保护的东西。我可以简单地坐在任何人的计算机上并复制他们所有的cookie,然后我将以他们的身份登录。

I think you are misunderstanding what needs to be done. To protect against CSRF you need to create a token and save it for that session. Then you need to append all your submits and AJAX calls with that token.

For another person to send you to a page on your website they would need to have access to the request with in the same session. It is true that one could parse the HTML and look for the token. But when they try to request a http call on your website they will have a new session created. The new session will have a new token which will not match the token that was passed.

Next you will ask what if you can copy the cookies and the session id as a result. This is not something that is protected. I can simply sit anybody's computer and copy all their cookies and I will then be logged in as them.

你げ笑在眉眼 2024-12-08 01:55:03

正如 kapep 指出的那样,您混淆了输入验证和跨站点表单发布这两个独立的问题。无论如何,您都必须验证您的输入,因此,如果您有健全的输入验证,那么恶意攻击者使用自己的会话令牌的情况就已经得到处理。 CSRF 保护并不是为了保护数据,它只是为了确保只有您自己的应用程序中的表单才能将数据发送回该应用程序。 CSRF 保护只是阻止其他人从他们在自己网站上发布的表单直接将数据发布到您的应用程序中。

需要注意的一个具体点是,该令牌对于页面上运行的任何 JavaScript 都是可见的,因此一旦存在跨站脚本 (XSS) 漏洞,您的 CSRF 保护就会失效。

请参阅跨站脚本预防备忘单

As kapep points out, you are confusing the two seperate issues of input validation and cross-site form posting. You must validate your inputs anyway, so the case of your malicious attacker using his own session token is already handled if you have sound input validation. CSRF protection is not there to protect the data, it is simply to ensure that only forms from your own application can post data back to that application. the CSRF protection is simply stopping other people being able to post data directly into your app from forms they put up on their own site.

One specific point to be aware of is that the token is visible to any javascript running on your page, so as soon as you have a cross-site scripting (XSS) vulnerability, your CSRF protection is defeated.

See Cross-site scripting and the prevention cheat sheet

还不是爱你 2024-12-08 01:55:03

您应该使用每个请求令牌。

  1. 生成令牌并将其存储在会话中。
  2. 将令牌传递给客户端。
  3. 执行行动。
  4. 销毁令牌。

令牌更安全,不能多次使用。

You should use a per request token.

  1. Generate a token and store it in the session.
  2. Pass the token to the client.
  3. Execute actions.
  4. Destroy the token.

The token is safer and cannot be used more than one time.

鲜血染红嫁衣 2024-12-08 01:55:03

我将被盗令牌定义为由其他人使用的令牌,而不是您将令牌发送到的人。如果您向某人发送令牌,他实际上无法从自己那里窃取它。

如果您担心用户可以使用自己的令牌运行恶意脚本,那么您的设计似乎已被破坏。您无法阻止用户发送您不打算接收的数据。您的工作是验证任何数据,会话令牌只是用于识别同一客户端的多个请求。

如果您通过不安全的 http 发送该令牌,则可能会出现安全问题。然后,通过监控客户端网络,它很容易被盗。

I would define a stolen token as a token that is used by someone else, and not the one you have send the token to. If you send someone a token he can't really steal it from himself.

If you are concerned that a user can run a malicious script with his own token, your design seems to be broken. You can't prevent a user from sending data that you didn't indented to receive. It's your job to validate any data, the session token is just there to identify multiple requests by the same client.

It could be a security issue if you send that token over unsecured http. Then it could easily be stolen by monitoring the clients network.

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