我的 CSRF 保护方法安全吗?
Is my CSRF protection method secure?
我一直在使用 PHP 进行自己的 CSRF 保护。从我读到的内容来看,我决定使用 cookie 来实现我的保护,但对于我的方法是否可以抵御 CSRF 攻击感到有点困惑。
所以我的方法如下:
用户发送请求登录
服务器检查是否设置了 CSRF 令牌,如果没有,则创建一个并将其存储在他们的 Session 中,并使用该令牌创建一个 Cookie
通过检查它是否在 POST 请求中来验证 CSRF 令牌,如果不在 $_COOKIE
中检查令牌
如果令牌无效,请发回消息...
我决定使用 cookie 来存储令牌,因为这适用于 Ajax 请求,而且我不必在每次使用 Ajax POST 时都包含它.
我感到困惑的是攻击者不能只发出请求吗? POST 或 GET,因为 cookie 在那里,它只是随请求一起发送,因此是一个有效的请求,因为令牌每次都随浏览器一起发送?
只要"User sends request to login"指的是登录页面的初始GET请求,这是正确的。 CSRF 应生成并存储在 GET 请求中,并在每个 POST.
期间进行验证
What I am confused about is couldn't an attacker just make a request; POST or GET and because the cookie is there it just gets sent with the request anyway, thus being a valid request as the token is sent with the browser every time?
是的。 CSRF 令牌不是秘密的。他们只是确认 POST 仅在发出预期的 GET 请求后才发出。这意味着某人只有在他们首先请求表格时才能提交表格。它可以防止不良网站将带有 POST 的用户发送到您的网站。它不会阻止攻击者发出 GET 请求、获取令牌并进行有效 POST.
来自OWASP:
With a little help of social engineering (such as sending a link via email or chat), an attacker may trick the users of a web application into executing actions of the attacker's choosing. If the victim is a normal user, a successful CSRF attack can force the user to perform state changing requests.
cookie 不应包含 CSRF 令牌。只有客户端会话存储应该包含它。而且你不应该反对 cookie 中的 CSRF。
如果您要检查与 cookie 一起发送的 CSRF,您将绕过 CSRF 背后的想法。
一个简单的攻击场景是国外网站的隐藏表单:
<form method="method" action="http://site-to-gain-access-to.tld/someactionurl">
<!-- some form data -->
</form>
并且这个隐藏表单将在没有用户干预的情况下使用javascript执行。如果用户在站点 site-to-gain-access-to.tld
上登录并且没有 CSRF 保护处于活动状态,那么就像用户本身会触发该操作一样,因为用户的会话 cookie 将随该请求一起发送.因此服务器会假定是用户触发了该请求。
如果您现在将 CSRF 令牌放入您的 cookie 中,您将遇到与会话相同的问题。
CSRF 令牌必须始终仅作为请求正文或 url 的一部分发送,而不是作为 cookie。
所以突出显示的部分:
Server checks if a CSRF token is set, if not create one and store it in their Session and create a Cookie with the token as well
Validate the CSRF token through checking if it is in the POST request, if not then check for the token in $_COOKIE
会破坏 CSRF 保护。 CSRF 是作为明文存储在 cookie 中还是使用服务器端加密都没有关系。
我一直在使用 PHP 进行自己的 CSRF 保护。从我读到的内容来看,我决定使用 cookie 来实现我的保护,但对于我的方法是否可以抵御 CSRF 攻击感到有点困惑。
所以我的方法如下:
用户发送请求登录
服务器检查是否设置了 CSRF 令牌,如果没有,则创建一个并将其存储在他们的 Session 中,并使用该令牌创建一个 Cookie
通过检查它是否在 POST 请求中来验证 CSRF 令牌,如果不在 $_COOKIE
中检查令牌
如果令牌无效,请发回消息...
我决定使用 cookie 来存储令牌,因为这适用于 Ajax 请求,而且我不必在每次使用 Ajax POST 时都包含它.
我感到困惑的是攻击者不能只发出请求吗? POST 或 GET,因为 cookie 在那里,它只是随请求一起发送,因此是一个有效的请求,因为令牌每次都随浏览器一起发送?
只要"User sends request to login"指的是登录页面的初始GET请求,这是正确的。 CSRF 应生成并存储在 GET 请求中,并在每个 POST.
期间进行验证What I am confused about is couldn't an attacker just make a request; POST or GET and because the cookie is there it just gets sent with the request anyway, thus being a valid request as the token is sent with the browser every time?
是的。 CSRF 令牌不是秘密的。他们只是确认 POST 仅在发出预期的 GET 请求后才发出。这意味着某人只有在他们首先请求表格时才能提交表格。它可以防止不良网站将带有 POST 的用户发送到您的网站。它不会阻止攻击者发出 GET 请求、获取令牌并进行有效 POST.
来自OWASP:
With a little help of social engineering (such as sending a link via email or chat), an attacker may trick the users of a web application into executing actions of the attacker's choosing. If the victim is a normal user, a successful CSRF attack can force the user to perform state changing requests.
cookie 不应包含 CSRF 令牌。只有客户端会话存储应该包含它。而且你不应该反对 cookie 中的 CSRF。
如果您要检查与 cookie 一起发送的 CSRF,您将绕过 CSRF 背后的想法。
一个简单的攻击场景是国外网站的隐藏表单:
<form method="method" action="http://site-to-gain-access-to.tld/someactionurl">
<!-- some form data -->
</form>
并且这个隐藏表单将在没有用户干预的情况下使用javascript执行。如果用户在站点 site-to-gain-access-to.tld
上登录并且没有 CSRF 保护处于活动状态,那么就像用户本身会触发该操作一样,因为用户的会话 cookie 将随该请求一起发送.因此服务器会假定是用户触发了该请求。
如果您现在将 CSRF 令牌放入您的 cookie 中,您将遇到与会话相同的问题。
CSRF 令牌必须始终仅作为请求正文或 url 的一部分发送,而不是作为 cookie。
所以突出显示的部分:
Server checks if a CSRF token is set, if not create one and store it in their Session and create a Cookie with the token as well
Validate the CSRF token through checking if it is in the POST request, if not then check for the token in $_COOKIE
会破坏 CSRF 保护。 CSRF 是作为明文存储在 cookie 中还是使用服务器端加密都没有关系。