PHP further security on anti-CSRF token

风格不统一 提交于 2019-12-03 22:42:06

However, this process seems a little flawed since by including the token value in a hidden POST field an attack can simply just look at the website source code

No they can't.

Alice runs a website. Bob visits the website. Mallory is attacking Bob's account.

Bob gets a nonce token when he visits Alice's website.

If Mallory visited the site, Mallory would get a different nonce (because Mallory would have a different session).

If Mallory generated a form with malicious data in it (on her website) and tricked Bob into submitting it, then the nonce Mallory put in the form would not match the nonce in Bob's session and the submission would be rejected.

cryptic ツ

What you need to do is make the hidden field the MD5 or SHA1 hash of the session ID with a salt. That way you compare the submitted value with the hash of the session ID plus salt and if they match it is valid. If an attacker can guess the token then they have already stolen the session id and would be pointless to do anymore protecting since login has already been hijacked. It's really as simple as that. Here is some great info per OWASP on how to prevent CSRF https://www.owasp.org/index.php/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet

Let's review the attack scenario:

  1. You have a server at example.com and you use CSRF tokens in your forms.
  2. Each CSRF token is unique, specific to a user and only valid for some time.
  3. A malicious third party, Eve, tricks one of your users, Alice, to come to her site, attempting to mount a CSRF attack.
  4. If Eve simply tricks Alice into submitting a form to your server without CSRF token, your server will reject it.
  5. If Eve also has an account on your server and tries to get any token to submit with the form, this will fail because the token is not valid for Alice.

This leaves this scenario: Using Javascript, Eve fetches a form from your server as Alice, then submits this form back, including a valid token. I.e. Eve completely impersonates Alice for the entire process of a regular form submission using Javascript. This is prevented by the Same Origin Policy. Eve's Javascript won't be able to fetch information from your server, Alice's browser will prevent this as it violates the Same Origin Policy.

That is, assuming there are no security holes in the browser which allow Eve to circumvent that policy. This also means that you need to guard against XSS, i.e. against Eve being able to inject one of her scripts into your website, so regular visitors to your site will run Eve's scripts as part of your site, from the same origin.


As a bit of self-promotion, I've just implemented a signature based CSRF token library, which you may want to look at: Kunststube\CSRFP. I'd also like to solicit peer review and criticism of it, while I'm at it.

At first, you have to keep in mind, that you cannot prevent hackers from attacking your application, only you can make things harder.

The idea is come clearly when you thinking about what is the main goal of CSRF attacks, The CSRF is an attack that tricks the victim into loading a page that contains a malicious request. It is malicious in the sense that it inherits the identity and privileges of the victim to perform an undesired function on the victim's behalf, like change the victim's e-mail address, home address, or password, or purchase something. CSRF attacks generally target functions that cause a state change on the server but can also be used to access sensitive data.

So as above, attackers don't make attack directly to your web page, they need bridge, that's it they need a Victim, so they can use victim identity and privileges to execute actions.

when you said:

However, this process seems a little flawed since by including the token value in a hidden POST field an attack can simply just look at the website source code

it's doesn't make sense, because attacker will not attack himself.

i hope this was help full.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!