CSRF (Cross-site request forgery) attack example and prevention in PHP

how to prevent csrf attack in javascript
csrf attack example
how to prevent csrf attack in php
csrf post example
cross site request forgery prevention
csrf token implementation
ajax request vulnerable to cross site request forgery
cross-site request forgery fix

I have an website where people can place a vote like this:


This will place a vote on item 25. I want to only make this available for registered users, and only if they want to do this. Now I know when someone is busy on the website, and someone gives them a link like this:


then the vote will be places for him on the item without him wanting to do this.

I have read the explanation on the OWASP website, but i don't really understand it

Is this an example of CSRF, and how can I prevent this. The best thing i can think off is adding something to the link like a hash. But this will be quite irritating to put something on the end of all the links. Is there no other way of doing this.

Another thing can someone maybe give me some other example of this, because the website seems fairly fugue to me.

phpmaster, Cross-site request forgery (CSRF) is a common and serious exploit where a process.php ), and finally an example attack ( harmless.html ). In this tutorial, we will walk through a simple example of what cross-site request forgery (CSRF) is, and how we can prevent it using a token In just 3 simple steps: Generate a random token in the PHP session – $_SESSION[‘token’] = substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 32;

First, GET request shouldn't be used to alter states on the server, so for your vote service I would recommend POST/PUT. This is only a guideline, but a clever one.

So to your question, CSRF is a client issue so it doesn't matter what kind of server language you use (PHP in your case). The standard fix is the same and goes like this: Have a random value in the URI/POST-data and the same value in the Cookie header. If those matches you could be sure there is no CSRF. There are a lot of info about how this could be done here on StackOverflow eg. this one. Good luck!

PHP Security - Cross-Site Request Forgery, Cross-Site Request Forgery (CSRF) is an attack that forces an end user to execute unwanted Duration: 14:43 Posted: May 7, 2017 Hello, friends today we are going to learn Cross-Site Request Forgery Prevention in PHP by using random token in each request.In Cross-Site Request Forgery (CSRF) attack the victim unintentionally sends web request that takes advantage of their logged in session on a particular site to leak server data, change session state or to manipulate user’s account.

OWASP has a CSRFGuard for PHP, and ESAPI for PHP that I wrote a long time ago for XMB -> UltimaBB -> GaiaBB.


It seems some others have cleaned up that code and allowed for stronger tokens:


thanks, Andrew

Fixing CSRF vulnerability in PHP Applications, Cross Site Request Forgery or CSRF is an attack that forces a malicious action to an innocent Another bad example of CSRF patching is URL Rewriting. This is another PHP implementation of CSRF protection in PHP. How to Prevent Cross-Site Request Forgery Attacks An attacker can launch a CSRF attack when he knows which parameters and value combination are being used in a form. Therefore, by adding an additional parameter with a value that is unknown to the attacker and can be validated by the server, you can prevent CSRF attacks.

There are 3 players in a CSRF attack

  1. the victim website (your voting website in your example) [knows his logged in users cookies]
  2. your client's browser (while he is logged in) [knows his cookies]
  3. an attacker website [Doesn't know the logged-in users cookies]

CSRF attacks depend on 2 facts

  1. browsers send cookies automatically with every request
  2. we depend on cookies to identify our logged-in users (e.g: setcookie("sessionID", "0123456789ABCDEF", time()+3600); )

If an attacker could by away or another make a logged-in user requests this

// http://victim.website/vote/30

for example by putting the link on the attacker website or send it in an email, the logged in client browser will send the identifying cookies(sessionID) along with this request, which will make the victim website think that his logged-in user really wants to vote!

But if the victim's website more clever and verifies the requests of his logged-in users with additional GET or POST parameter (not cookies), the attacker now is in a problem because GET and POST parameters are not sent automatically by browsers, and he has to guess it.

// http://victim.website/vote/30?csrfSecret=0123456789ABCDEF

The attacker doesn't know the csrfSecret parameter which is a secret between the victim website and his client (just like the session token), so the attacker has no way to build the URL that he wants forge a request by.

Similarly, if the vote is done by POST requests, the attacker will not be able to make the form on his website(or third party website) because he doesn't know the secret between the victim's website and his users.

<form method="post" action="http://victim.website/vote" >
    <input type="hidden" name="vote" value="30">
    <input type="hidden" name="csrfSecret" value="????? I don't know it :(">

CSRF (Cross-site request forgery) attack example and prevention in , This could become an example of CSRF if : that link is fetched (via an <img> tag, for example) : forgery; from another site : cross-site. Cross Site Request Forgery or CSRF is an attack that forces a malicious action to an innocent website from end user’s (valid user) browser when he/she is running a valid session of the website. If user is authenticated on a website, every action performed from his browser will belong to him.

Cross Site Request Forgery (CSRF), Cross Site Request Forgery (CSRF) on the main website for The OWASP Foundation. If the victim is a normal user, a successful CSRF attack can force the user to See the CSRF Prevention Cheat Sheet for prevention measures. You can use CSRFProtector Project to protect your php applications or any project  We’ll get to that in a second. First, let’s take a step back: that was a CSRF attack. In short, Cross-Site Request Forgery is a web security exploit where an attacker induces a victim to perform an action they didn’t mean to. In this case, the attacker tricked you into unintentionally transferring them money. How It Happened

Cross-Site Request Forgery Prevention, Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a malicious For example, this attack could result in a transfer of funds, changing a password For Java: OWASP CSRF Guard or Spring Security; For PHP and Apache:  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. 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

PHP CSRF Prevention - Steve Clifton, Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a In the below example, the user may end up landing at their account  Cross-Site Request Forgery Prevention Cheat Sheet¶ Introduction¶. Cross-Site Request Forgery (CSRF) is a type of attack that occurs when a malicious web site, email, blog, instant message, or program causes a user's web browser to perform an unwanted action on a trusted site when the user is authenticated.

  • You are right, it's about as easy to forge a GET as an POST request. Though I don't agree that it's necessary to have a token that expire. If the attacker are able to get hold of your session data you have bigger trouble than some extra votes. But your suggested fix still work since the key is just to have a token/random value in the Cookie and in the request data (saved in a cookie or tied to the users session key).
  • Thank you both for the suggestions. I will change all the links to have this token. I have to agree this is a much saver method. However I am not going to implement the token expiration, i agree with MygGaN on this.
  • Any of you guys have an idea on what to do when I use AJAX for the voting. Should i just reuse the same key while the user is on the page, and only generate a new token when he refreshes. Or do i have to supply an new token for all the links whenever a vote is done.
  • Not sure there is a definitive answer to that question, but you have to think that refreshing the token "too often" can lead to troubles ; especially, what if the user has several tabs opened on your site in his browser, and the token changes from one of those ?
  • POST requests should always be required for operations that modify the server state, as otherwise web crawlers, pre-crawlers ect. may trigger state changes