Author Topic: CSRF - Getting your hands dirty..  (Read 1628 times)

0 Members and 1 Guest are viewing this topic.

Offline parad0x

  • VIP
  • Royal Highness
  • *
  • Posts: 638
  • Cookies: 118
    • View Profile
CSRF - Getting your hands dirty..
« on: November 15, 2015, 06:15:26 pm »
CSRF : Introduction


CSRF means Cross-Site Request Forgery
With that being said, let us see what is it to get an understanding of it so that we can exploit it ;)

According ot OWASP, 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. CSRF attacks specifically target state-changing requests, not theft of data, since the attacker has no way to see the response to the forged request. 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 like transferring funds, changing their email address, and so forth. If the victim is an administrative account, CSRF can compromise the entire web application.

So you can see how powerful this attack can be, of course when the web application is vulnerable, let's try to exploit it from the very basics, or you can say, from the ground up :)

Requirements

1. A web browser (kinda obvious, right)
2. A web server (am using XAMPP)
3. DVWA
4. Mind (as I don't want you to just blindly reproduce everything said in the tut and getting no knowledge)
5. A proxy ( this is optional as it will allow you to see how your request is going and what is repsonse)

Setup
Just setup DVWA and set its security to LOW as we are starting and then move onto the CSRF section.



Here, you see an option to change your password, a regular password changing prompt, huh? Just enter a new password, type again to confirm and click on change (I used trying as the new password), you'll see something like this.



Now see the URL, it has the request it sent to the server getting reflected in the URL bar of your browser (please note that this is not a case always but since it is showing here, we'll take the advantage of this) or otherwise you can pass the request from the proxy and see what is it requesting the server :)

Putting the hands in the mud : Beating Low Security

In the URL bar, change the password_new=trying&password_conf=trying to password_new=it_works&password_conf=it_works and submit it and you'll see your password got changed to it_works. Yeah, I know this is the same what we did above but it was just to make sure you don't forget it ;)

Now, think you are not a DVWA user but you need to change his password to System7 and you know that it is vuln to CSRF and you also know the request (what was in the URL bar), so, let's see how you'll exploit it.
Make an HTML page with minimal code so as to accomplish the task.



Here is the source of my HTML page that I used.
Code: (HTML) [Select]
<html>

<head>
<title>Change Password ;)</title>
</head>

<body>
<a href="http://127.0.0.1/dvwa/vulnerabilities/csrf/?password_new=System7&password_conf=System7&Change=Change#">Click me ;)</a>
</body>
</html>

Here, we just constructed a URL that will change the password to System7. To get this executed, you can use social engineering and get the dumbass click on it and his password will change.



That was the LOW security CSRF on DVWA. Now, we'll see how to evade a simple filter that is applied to make the web app secure but that doesn't makes it secure.

Getting deeper in mud

Set the DVWA Security to Medium and come back to CSRF again ;)

Now if you try to change the password via that html file, you'll see an error.



As you can see, it says That request didn't look correct. and you failed to change the password. We can take 2 approaches here, White box and black box but since we have access to source code, we'll take the white box approach. When you click on View Source button, you see the source code and what actually tests the authenticity of the request is this line
Code: (PHP) [Select]
// Checks to see where the request came from
    if( eregi( $_SERVER[ 'SERVER_NAME' ], $_SERVER[ 'HTTP_REFERER' ] ) )

About eregi function, it checks the occurrence of first string into the second string and returns the length of the matched string else returns FALSE.
I dunno what the fuck was wrong with my system today, I was unable to intercept the request or I would have shown you what was causing the problem.

Solution -> In the eregi function, we see that $_SERVER[ 'SERVER_NAME' ], which is 127.0.0.1 in our case is being searched withing the HTTP_REFERER field in the password changing request. Good thing is we can set this (or any header field calue on our own), but that is done in PHP, so we'll write a PHP file that will contain the following code:
Code: (PHP) [Select]
<html>

<?php

header("Referer: 127.0.0.1");

?>


<a href="http://127.0.0.1/dvwa/vulnerabilities/csrf/?password_new=Fucked&password_conf=Fucked&Change=Change#">Click me, I'll do it now ;)</a>
</html>

Save it as do_it.php in your web root directory and open it.



Now click on the Click me, I'll do it now ;) and you'll see the password changed in the URL bar.



You can also verify this thing in your proxy ;)

so, hope you guys got the basics of CSRF.
Any questions??
« Last Edit: November 15, 2015, 10:16:50 pm by iTpHo3NiX »

Offline ShadowCloud

  • Serf
  • *
  • Posts: 33
  • Cookies: 31
  • -My word is my bond
    • View Profile
Re: CSRF - Getting your hands dirty..
« Reply #1 on: November 23, 2015, 12:58:51 pm »
Good job :)

This is really one of those attacks that people look at and go "it's not really an issue is it?" and yep, it absolutely totally is.  This type of attack, especially combined with other attack vectors can leave a system devastated.

I might do a tutorial on how to prevent these attacks from a development point of view.
QA Engineer walks into a bar. Orders a beer. Orders 0 beers. Orders 999999999 beers. Orders a lizard. Orders -1 beers. Orders a sfdeljknesv.