简体   繁体   English

在 CSRF 保护中使用 PHP 会话是否安全?

[英]Is using PHP Sessions safe with CSRF Protection?

I have a script file called get_user_unfo.php that just returns a JSON file of the current logged-in user's data.我有一个名为get_user_unfo.php的脚本文件, get_user_unfo.php返回当前登录用户数据的 JSON 文件。

When requested, the file first checks the CSRF token and then does a SQL search of the users table based on a session variable called user_id , which is set when the user logs in which is how it returns the JSON object.收到请求时,该文件首先检查 CSRF 令牌,然后根据名为user_id的会话变量对users表进行 SQL 搜索,该变量在用户登录时设置,返回 JSON 对象的方式。

What would happen if someone from evil.com tried to request the PHP file from my wesbite using AJAX?如果来自evil.com某人尝试使用 AJAX 从我的网站请求 PHP 文件会发生什么?

A) Would nothing get returned because they're calling my file from a different website? A) 因为他们从不同的网站调用我的文件,所以不会返回任何内容吗? If this is true and no results are returned then why is it advised to use long CSRF tokens?如果这是真的并且没有返回结果,那么为什么建议使用长 CSRF 令牌? Surely just 1 or 0 would suffice?当然只有10就足够了吗?

or...或者……

B) Would the request still be valid and would my website return other users information? B) 请求是否仍然有效,我的网站是否会返回其他用户信息?

I'm answering my own question here as I discovered the answer.当我发现答案时,我在这里回答我自己的问题。

It's clear to me now that I did not understand what CSRF propperly was and how it was supposed to be used.现在我很清楚,我不明白 CSRF 是什么以及它应该如何使用。

To answer my question, neither a or b are correct.要回答我的问题,a 或 b 都不正确。

The way CSRF works is best described here : CSRF 的工作方式在这里得到了最好的描述:

Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted actions on a web application in which they're currently authenticated跨站点请求伪造 (CSRF) 是一种攻击,它迫使最终用户在当前已通过身份验证的 Web 应用程序上执行不需要的操作

So if a user went onto evil.com that had a page that did ajax requests to mydomain.com/change-email then without any protection in place this would go through because the unsuspecting user would be logged in on mydomain.com and so the ajax request would be playing with that users information.因此,如果用户访问evil.com的页面向mydomain.com/change-email发出 ajax 请求,那么在没有任何保护的情况下,这将通过,因为毫无戒心的用户将登录 mydomain.com,因此ajax 请求将使用该用户信息。

The reason you need a token in place is so that the form and the request can communicate.您需要适当的令牌的原因是为了表单和请求可以进行通信。 So if evil.com tried to do an ajax request from a page now from mydomain.com then the values would not be the same.因此,如果 evil.com 现在尝试从来自 mydomain.com 的页面执行 ajax 请求,则值将不相同。

The reason you dont want to use a 1 or a 0 either is for the same reason.您不想使用 1 或 0 的原因也是出于同样的原因。 An attacker could set up an ajax request that submitted a 1 or a 0 and the request would work.攻击者可以设置提交 1 或 0 的 ajax 请求,该请求将起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM