{"id":38,"date":"2013-02-14T01:45:16","date_gmt":"2013-02-14T01:45:16","guid":{"rendered":"http:\/\/serialsignal.com\/blog\/?p=38"},"modified":"2020-09-29T22:18:54","modified_gmt":"2020-09-29T22:18:54","slug":"csrf","status":"publish","type":"post","link":"https:\/\/serialsignal.com\/blog\/csrf\/","title":{"rendered":"CSRF &#8211; Cross Site Request Forgery"},"content":{"rendered":"<p>There are a few security concepts every developer should understand and be able to implement before they are trusted with sensitive data. Things like <a href=\"https:\/\/www.owasp.org\/index.php\/SQL_Injection\">SQL injection<\/a>, <a title=\"Cross-site scripting (OWASP)\" href=\"https:\/\/www.owasp.org\/index.php\/Cross-site_Scripting_(XSS)\">XSS<\/a>, <a title=\"Salt Cryptography (wikipedia)\" href=\"http:\/\/en.wikipedia.org\/wiki\/Salt_(cryptography)\">salting<\/a> and a whole bunch of <a title=\"Attacks (OWASP)\" href=\"https:\/\/www.owasp.org\/index.php\/Category:Attack\">other things<\/a> can really cause you and your users a lot of trouble.<br \/>\nMaybe i&#8217;ll write something about those some other time, but for now i&#8217;m singling out CSRF which is fairly easy to exploit and can have some pretty fantastic results yet it seems, from my experience anyway, to get less attention than the other attacks I&#8217;ve mentioned.<\/p>\n<p><!--more--><\/p>\n<h2>So, what is it?<\/h2>\n<p>In short, CSRF is when an attacker is able to submit a request from the victim&#8217;s browser to a site that they are authenticated with. Wikipedia puts it like this:<\/p>\n<blockquote title=\"Wikipedia on CSRF\" cite=\"http:\/\/en.wikipedia.org\/wiki\/Cross-site_request_forgery\"><p>Cross-site request forgery, also known as a one-click attack or session riding and abbreviated as CSRF (sometimes pronounced sea-surf[1]) or XSRF, is a type of malicious exploit of a website whereby unauthorized commands are transmitted from a user that the website trusts.[2] Unlike cross-site scripting (XSS), which exploits the trust a user has for a particular site, CSRF exploits the trust that a site has in a user&#8217;s browser.<\/p><\/blockquote>\n<p>For example, if you log in to your banks website and someone tricks you in to clicking a link that looked something like this:<\/p>\n<p><tt>myfakeinsecurebank.com\/?transfer-to=badguy&amp;amount=200&amp;from=savings<\/tt><\/p>\n<p>If myfakeinsecurebank.com is careless, and they probably are with a URI like that, then you might have just been duped in to sending badguy $200 dollars!<br \/>\nThe website can&#8217;t tell if you are responsible for this request, all it knows is that it came from your browser and you are logged in.<br \/>\n&#8220;<em>Well, you shouldn&#8217;t put things you shouldn&#8217;t link to in the get string!<\/em>&#8221; you might be thinking to yourself and you&#8217;d have a point, but that really doesn&#8217;t prevent CSRF. The attacker could just make a website (this is where the &#8220;Cross Site&#8221; part comes from) that sends a post request or even a series of post requests to the target website to do all sorts of things.<\/p>\n<p>If you are logged in to a site and that site isn&#8217;t actively trying to prevent CSRF then the attacker can take advantage of that and submit requests to the site through your browser as if they were you.<\/p>\n<h2>Quick Example<\/h2>\n<p>Lets see it in action! Here is some pretty insecure\/quick code you might find on a site like myfakeinsecurebank.com:<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;height:400px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;!--<\/span>?php<br \/>\n<a href=\"http:\/\/www.php.net\/session_start\"><span class=\"kw3\">session_start<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"re0\">$namespace<\/span> <span class=\"sy0\">=<\/span> <span class=\"st_h\">'csrftest'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"re0\">$username<\/span> <span class=\"sy0\">=<\/span> <span class=\"st_h\">'user'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"re0\">$password<\/span> <span class=\"sy0\">=<\/span> <span class=\"st_h\">'pass'<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'logout'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"sy0\">||<\/span> <span class=\"sy0\">!<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/array\"><span class=\"kw3\">array<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span> <br \/>\n&nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> <a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'password'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">==<\/span> <span class=\"re0\">$username<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'password'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">==<\/span> <span class=\"re0\">$password<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ Loging the user &quot;in&quot; by storing the username<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$username<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re0\">$message<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span> <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; ? <span class=\"st_h\">'Hello '<\/span> <span class=\"sy0\">.<\/span> <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span><span class=\"re0\">$namespace<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">.<\/span> <span class=\"st_h\">'!'<\/span> <br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">:<\/span> <span class=\"st_h\">'Set your name below!'<\/span><span class=\"sy0\">;<\/span><br \/>\n?<span class=\"sy0\">--&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<h1><!--?=$message?--><\/h1>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<form action=\"\" method=\"POST\"><label>Your Name:<input name=\"name\" type=\"textbox\" value=\"&lt;?=(isset($_POST['name']) ? $_POST['name'] : '');?&gt;\" \/><\/label><br \/>\n<input type=\"submit\" value=\"Change\" \/><\/form>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p><!--?php\n} else {\n?--><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<form action=\"\" method=\"POST\"><label>Username:<input name=\"username\" type=\"textbox\" \/><\/label><br \/>\n<label>Password:<input name=\"password\" type=\"password\" \/><\/label><br \/>\n<input type=\"submit\" value=\"Login\" \/><\/form>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p><!--?php\n}\n<\/code--><br \/>\nHopefully its simple enough that a quick glance will give you the general idea. You log in with the hard-coded username and password and you are presented with a screen where you can give yourself some kind of a name.<br \/>\nHere it is in action:<br \/>\n<a href=\"http:\/\/mjump.kodingen.com\/SS\/CSRF\/target.php\">http:\/\/mjump.kodingen.com\/SS\/CSRF\/target.php<\/a><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p>Maybe, perhaps, one day you are minding your own business setting your name to various clever things and decide to hit up Google for some inspiration. A particular site stands out, almost too good to be true, and you click the link.<br \/>\n<a href=\"http:\/\/serialsignal.com\/csrf\/attack.php\">http:\/\/serialsignal.com\/csrf\/attack.php<\/a><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p>Maybe a bit of doubt comes over you as you think about clicking that link and maybe you don&#8217;t just click something with &#8220;csrf&#8221; and &#8220;attack&#8221; in the title; its just an example right? Its unlikely anyone really wanting to take advantage of you would be so obvious but fortunately http:\/\/serialsignal.com\/csrf\/attack.php is harmless unless you don&#8217;t have a sense of humor.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p>Here is the code behind it:<\/p>\n<div class=\"codecolorer-container html4strict railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/><\/div><\/td><td><div class=\"html4strict codecolorer\"><span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a>&gt;&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/br.html\"><span class=\"kw2\">br<\/span><\/a> <span class=\"sy0\">\/<\/span>&gt;<\/span><br \/>\nfunction attack() {<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/br.html\"><span class=\"kw2\">br<\/span><\/a> <span class=\"sy0\">\/<\/span>&gt;<\/span><br \/>\n&nbsp; &nbsp; document.forms['attack'].submit();<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/br.html\"><span class=\"kw2\">br<\/span><\/a> <span class=\"sy0\">\/<\/span>&gt;<\/span><br \/>\n}<span class=\"sc2\">&lt;<a href=\"http:\/\/december.com\/html\/4\/element\/br.html\"><span class=\"kw2\">br<\/span><\/a> <span class=\"sy0\">\/<\/span>&gt;<\/span><br \/>\n<span class=\"sc2\">&lt;<span class=\"sy0\">\/<\/span><a href=\"http:\/\/december.com\/html\/4\/element\/script.html\"><span class=\"kw2\">script<\/span><\/a>&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;html&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<form action=\"http:\/\/mjump.kodingen.com\/SS\/CSRF\/target.php\" method=\"POST\" name=\"attack\"><input name=\"name\" type=\"hidden\" value=\"Doofus\" \/><br \/>\n<input type=\"submit\" value=\"Click for free ipod lolz\" \/><\/form>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;html&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<div class=\"codecolorer-container html4strict railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"html4strict codecolorer\">&nbsp;<\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p>Visiting the link will do a pretty snappy javascript triggered post in to target.php changing your name to &#8220;Doofus&#8221;. If you have javascript enabled it will happen almost instantaneously. Clicking the button wont get you a free ipod, but if you have javascript disabled it might just entice you. As you can see it really doesn&#8217;t take much to exploit it and you can be assured serialsignal.com doesn&#8217;t share any session information with mjump.kodingen.com; they are entirely seperate sites, yet serialsignal was able to manipulate target.php through your logged in session anyway.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<h2>How to stop it?<\/h2>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p>There are <a href=\"https:\/\/www.owasp.org\/index.php\/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet\">several ways you can go about doing it<\/a> but I really do suggest the &#8220;Synchronizer Token Pattern&#8221; if possible. Essentially you generate a unique (per login or request) token which is embedded in each sensitive form as a hidden input. The principle is that the attacker will not be able to guess this token so a request forwarded through you without the token can be rejected. Properly implemented this has not only the benefit of being almost impossible to spoof from an external request but it also makes it possible for your users to make sure you are protecting them by viewing the source code.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<p>Here is a modified version of target.php that implements the most basic form of this pattern:<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;height:400px;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/>4<br \/>5<br \/>6<br \/>7<br \/>8<br \/>9<br \/>10<br \/>11<br \/>12<br \/>13<br \/>14<br \/>15<br \/>16<br \/>17<br \/>18<br \/>19<br \/>20<br \/>21<br \/>22<br \/>23<br \/>24<br \/>25<br \/>26<br \/>27<br \/>28<br \/>29<br \/>30<br \/>31<br \/>32<br \/>33<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;!--<\/span>?php<br \/>\n<a href=\"http:\/\/www.php.net\/session_start\"><span class=\"kw3\">session_start<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<a href=\"http:\/\/www.php.net\/define\"><span class=\"kw3\">define<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st_h\">'SESSION_NAMESPACE'<\/span><span class=\"sy0\">,<\/span> <span class=\"st_h\">'csrftest'<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"re0\">$username<\/span> <span class=\"sy0\">=<\/span> <span class=\"st_h\">'user'<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"re0\">$password<\/span> <span class=\"sy0\">=<\/span> <span class=\"st_h\">'pass'<\/span><span class=\"sy0\">;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'logout'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> &nbsp;csrfOk<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">||<\/span> <span class=\"sy0\">!<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/array\"><span class=\"kw3\">array<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span> <br \/>\n&nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> <a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'password'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">==<\/span> <span class=\"re0\">$username<\/span><br \/>\n&nbsp; &nbsp; <span class=\"sy0\">&amp;&amp;<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'password'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">==<\/span> <span class=\"re0\">$password<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"co1\">\/\/ Loging the user &quot;in&quot; by storing the username<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$username<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'csrf_token'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <a href=\"http:\/\/www.php.net\/hash\"><span class=\"kw3\">hash<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"st0\">&quot;sha512&quot;<\/span><span class=\"sy0\">,<\/span><a href=\"http:\/\/www.php.net\/mt_rand\"><span class=\"kw3\">mt_rand<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"nu0\">0<\/span><span class=\"sy0\">,<\/span><a href=\"http:\/\/www.php.net\/mt_getrandmax\"><span class=\"kw3\">mt_getrandmax<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span><span class=\"sy0\">;<\/span><br \/>\n<span class=\"br0\">&#125;<\/span><br \/>\n<br \/>\n<span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'username'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n<br \/>\n&nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span><a href=\"http:\/\/www.php.net\/isset\"><span class=\"kw3\">isset<\/span><\/a><span class=\"br0\">&#40;<\/span><span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">if<\/span> <span class=\"br0\">&#40;<\/span>csrfOk<span class=\"br0\">&#40;<\/span><span class=\"br0\">&#41;<\/span><span class=\"br0\">&#41;<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; <span class=\"br0\">&#125;<\/span> <span class=\"kw1\">else<\/span> <span class=\"br0\">&#123;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"re0\">$_POST<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span> <span class=\"sy0\">=<\/span> <span class=\"re0\">$_SESSION<\/span><span class=\"br0\">&#91;<\/span>SESSION_NAMESPACE<span class=\"br0\">&#93;<\/span><span class=\"br0\">&#91;<\/span><span class=\"st_h\">'name'<\/span><span class=\"br0\">&#93;<\/span><span class=\"sy0\">;<\/span><br \/>\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span class=\"kw1\">echo<\/span> <span class=\"st_h\">'<br \/>\n<br \/>\n&lt;h1--&gt; WATCH WHAT LINKS YOU CLICK!<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/>2<br \/>3<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"st_h\">';<br \/>\n}<br \/>\n}<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>$message = isset($_SESSION[SESSION_NAMESPACE][&#8216;name&#8217;])<br \/>\n? &#8216;Hello &#8216; . $_SESSION[SESSION_NAMESPACE][&#8216;name&#8217;] . &#8216;!&#8217;<br \/>\n: &#8216;Set your name below!&#8217;;<br \/>\n?&gt;<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<h1><!--?=$message?--><\/h1>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<form action=\"\" method=\"POST\"><label>Your Name:<input name=\"name\" type=\"textbox\" value=\"&lt;?=(isset($_POST['name']) ? $_POST['name'] : '');?&gt;\" \/><\/label><br \/>\n<input type=\"submit\" value=\"Change\" \/><br \/>\n<input name=\"csrf\" type=\"hidden\" value=\"&lt;?=$_SESSION[SESSION_NAMESPACE]['csrf_token'];?&gt;\" \/><\/form>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<form action=\"\" method=\"POST\"><input name=\"csrf\" type=\"hidden\" value=\"&lt;?=$_SESSION[SESSION_NAMESPACE]['csrf_token'];?&gt;\" \/><br \/>\n<input name=\"logout\" type=\"submit\" value=\"Log Out\" \/><\/form>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p><!--?php\n} else {\n?--><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<form action=\"\" method=\"POST\"><label>Username:<input name=\"username\" type=\"textbox\" \/><\/label><br \/>\n<label>Password:<input name=\"password\" type=\"password\" \/><\/label><br \/>\n<input type=\"submit\" value=\"Login\" \/><\/form>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p><!--?php\n}\n\nfunction csrfOk() {\n    $result = FALSE;\n     if (isset($_POST['csrf'])\n        && $_POST['csrf'] === $_SESSION[SESSION_NAMESPACE]['csrf_token']) {\n\n       $result = TRUE;\n    }\n\n    return $result;\n}\n<\/code--><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p><a href=\"http:\/\/mjump.kodingen.com\/SS\/CSRF\/target2.php\">http:\/\/mjump.kodingen.com\/SS\/CSRF\/target2.php<\/a><br \/>\n<strong>* Note:<\/strong> <em>Before you check out target2.php log out of <a href=\"http:\/\/mjump.kodingen.com\/SS\/CSRF\/target.php\">http:\/\/mjump.kodingen.com\/SS\/CSRF\/target.php<\/a>! They are on the same domain so if you are using the session from target.php there will be no token and target2.php will not let you do anything, even log out. So LOG OUT OF target.php! You don&#8217;t still want someone changing your name do you?<\/em><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>I generate a token and toss it in the session every time the user logs in; it will be unique and unguessable. DO NOT JUST HASH THE USERNAME OR ANY OTHER STATIC VALUE! If the attacker can guess the token then there is no point and its very easy to create an account and try basic things like md5 or base64 on the username to reproduce the token they see on the forms.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>Here is the attack.php thats pointed to target2.php:<br \/>\n<a href=\"http:\/\/serialsignal.com\/csrf\/attack2.php\">http:\/\/serialsignal.com\/csrf\/attack2.php<\/a><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>Its EXACTLY the same as the first one but it posts to target2.php instead of target.php. You can see that this time the attack doesn&#8217;t work. attack2.php would have to first find some way of getting the csrf token in order to affect target2. This is a basic implementation that is effective enough but you can get even better security if you have a different token on each form. I may go over how I do that method at a later date but, for an idea of how it works, I highly suggest you check out at<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p><a href=\"https:\/\/www.owasp.org\/index.php\/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Prevention_Frameworks\">https:\/\/www.owasp.org\/index.php\/Cross-Site_Request_Forgery_(CSRF)_Prevention_Cheat_Sheet#Prevention_Frameworks<\/a><\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>They also have some good tips, such as logging out of sites when you are done with them because even if its not open in a window, if server thinks your machine is logged in and isn&#8217;t keeping track of you CSRF attacks against it can be exploited. Throw some ajax in the mix and the attackers can scan a wide range of vulnerable sites you haven&#8217;t logged out of for valid sessions.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>If you use Zend Framework 1.x I recommend you check out <a href=\"http:\/\/framework.zend.com\/manual\/1.12\/en\/zend.form.standardElements.html#zend.form.standardElements.hash\">Zend_Element_Hash<\/a> and if you use ZF2 then you can check out the <a href=\"http:\/\/framework.zend.com\/manual\/2.0\/en\/modules\/zend.form.elements.html#csrf\">CSRF element<\/a>.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/code><\/p>\n<p>I also recommend you spend a bit of time reading around the OWASP site, its a great reference.<\/p>\n<div class=\"codecolorer-container php railscasts\" style=\"overflow:auto;white-space:nowrap;width:100%;\"><table cellspacing=\"0\" cellpadding=\"0\"><tbody><tr><td class=\"line-numbers\"><div>1<br \/><\/div><\/td><td><div class=\"php codecolorer\"><span class=\"sy0\">&lt;<\/span>code lang<span class=\"sy0\">=<\/span><span class=\"st0\">&quot;php&quot;<\/span><span class=\"sy0\">&gt;<\/span><\/div><\/td><\/tr><\/tbody><\/table><\/div>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>There are a few security concepts every developer should understand and be able to implement before they are trusted with sensitive data. Things like SQL injection, XSS, salting and a whole bunch of other things can really cause you and your users a lot of trouble. Maybe i&#8217;ll write something about those some other time, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,8,7,6,16,18],"tags":[21,19,9,20],"class_list":["post-38","post","type-post","status-publish","format-standard","hentry","category-csrf","category-general-concepts","category-php","category-programming","category-security","category-zend-framework","tag-code-examples","tag-csrf-2","tag-php-2","tag-security-2"],"_links":{"self":[{"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/posts\/38","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/comments?post=38"}],"version-history":[{"count":30,"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions"}],"predecessor-version":[{"id":69,"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/posts\/38\/revisions\/69"}],"wp:attachment":[{"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/media?parent=38"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/categories?post=38"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/serialsignal.com\/blog\/wp-json\/wp\/v2\/tags?post=38"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}