52
©2009 Justin C. Klein Keane PHP Code Auditing Session 3 – Tools of the Trade & Crafting Malicious Input Justin C. Klein Keane [email protected]

Php code-auditing

Embed Size (px)

DESCRIPTION

 

Citation preview

Page 1: Php code-auditing

©2009 Justin C. Klein Keane

PHP Code Auditing

Session 3 – Tools of the Trade & Crafting Malicious Input

Justin C. Klein [email protected]

Page 2: Php code-auditing

©2009 Justin C. Klein Keane

Setting Up Environment

Install VMWare workstation, or player Fusion on the Mac

Download the target host Unzip the host files then start the host in

VMWare

Page 3: Php code-auditing

©2009 Justin C. Klein Keane

Get VMWare Image Running

If prompted, say you moved the image

Page 4: Php code-auditing

©2009 Justin C. Klein Keane

CentOS Image Booting

Once image boots log in with root/password

Page 5: Php code-auditing

©2009 Justin C. Klein Keane

Find the IP Address

Get the IP address of the virtual machine using# /sbin/ifconfig eth0

Page 6: Php code-auditing

©2009 Justin C. Klein Keane

Ensure Apache is Running

Page 7: Php code-auditing

©2009 Justin C. Klein Keane

Upload the Exercise

Page 8: Php code-auditing

©2009 Justin C. Klein Keane

Extract the Exercise

Page 9: Php code-auditing

©2009 Justin C. Klein Keane

Install the Database

Page 10: Php code-auditing

©2009 Justin C. Klein Keane

Check the Application

Page 11: Php code-auditing

©2009 Justin C. Klein Keane

Troubleshooting

If you get a blank screen, check the web server and MySQL server:

# service httpd status

# service mysqld status

If you need to start services use: # /etc/rc.d/init.d/httpd restart

# /etc/rc.d/init.d/mysqld restart

Page 12: Php code-auditing

©2009 Justin C. Klein Keane

Troubleshooting Cont.

Check the log files: # tail /var/log/httpd/error_log

Page 13: Php code-auditing

©2009 Justin C. Klein Keane

Install Eclipse PDT

Download PDT all in one from http://www.eclipse.org/pdt/

Alternatively install Eclipse from http://www.eclipse.org/downloads/

Be sure to download “Eclipse IDE for Java Developers”

Page 14: Php code-auditing

©2009 Justin C. Klein Keane

Install PDT if Necessary

Use instructions at http://wiki.eclipse.org/PDT/Installation

Some platforms, such as Fedora, may have packages for PHP development, these may be more stable than a manual install of PDT

Page 15: Php code-auditing

©2009 Justin C. Klein Keane

Install RSE

Install the Remote System Explorer tools Help -> Software Updates Click the “Add Site” button Enter the URL

http://download.eclipse.org/dsdp/tm/downloads/

Select Remote System Explorer Core, Remote System Explorer End-User Runtime, Remote System Explorer Extender SDK, and RSE SSH Service

Page 16: Php code-auditing

©2009 Justin C. Klein Keane

Install the RSE Components

Click “Install”

Page 17: Php code-auditing

©2009 Justin C. Klein Keane

Open Eclipse

Open Eclipse Default “perspective” is dull and doesn't suit our

purposes Click Window -> Show View -> Remote System In the new window right click and select “new

connection”

Page 18: Php code-auditing

©2009 Justin C. Klein Keane

Add New Connection

Select “SSH Only”, click Next

Page 19: Php code-auditing

©2009 Justin C. Klein Keane

Connection Details

Fill in VMWare host information, click Finish

Page 20: Php code-auditing

©2009 Justin C. Klein Keane

Connect to Remote Host

Click the down arrow for the host, then “Sftp Files” then “Root” and enter credentials

Page 21: Php code-auditing

©2009 Justin C. Klein Keane

View Source

Page 22: Php code-auditing

©2009 Justin C. Klein Keane

Look for Potential SQL Injection

Page 23: Php code-auditing

©2009 Justin C. Klein Keane

Testing the Injection

First we'll try the injection using manual methods

Next we'll use some tools to help us out Sometimes manual testing may be impossible

Page 24: Php code-auditing

©2009 Justin C. Klein Keane

Manual Testing

Page 25: Php code-auditing

©2009 Justin C. Klein Keane

Using Tamper Data

To start Firefox Tamper Data plugin select Tools -> Tamper Data

Click “Start Tamper” in the upper left Fill in your test values again and submit When prompted click “Tamper”

Page 26: Php code-auditing

©2009 Justin C. Klein Keane

That's Interesting

Page 27: Php code-auditing

©2009 Justin C. Klein Keane

Tamper

Fill in new values for Post Parameters Note that you can also tamper with Cookies and

Referer Data Click “OK” when you're happy with your values

Page 28: Php code-auditing

©2009 Justin C. Klein Keane

That's More Like It

Page 29: Php code-auditing

©2009 Justin C. Klein Keane

Checking Cookies

You can also view cookies using the Web Developer Plugin

select Cookies -> View Cookie Information

Page 30: Php code-auditing

©2009 Justin C. Klein Keane

Using Web Developer

Page 31: Php code-auditing

©2009 Justin C. Klein Keane

View Source

View -> Source in Firefox Look for comments, JavaScript and the like Sometimes source will reveal information you

may have missed

Page 32: Php code-auditing

©2009 Justin C. Klein Keane

JavaScript in Source

Page 33: Php code-auditing

©2009 Justin C. Klein Keane

Paros

Download Paros from http://www.parosproxy.org

Paros is Java based, so if Eclipse can run on your machine, so can Paros

Paros is a proxy, so it captures requests from your web browser to a server and responses from the server back to your browser

You can use it to alter your requests quite easily

Page 34: Php code-auditing

©2009 Justin C. Klein Keane

Start Up Paros

Page 35: Php code-auditing

©2009 Justin C. Klein Keane

Configure Firefox

You need to configure Firefox to use Paros as a proxy

Choose Edit -> Preferences, then Advanced -> Network -> Settings

Page 36: Php code-auditing

©2009 Justin C. Klein Keane

Configure Settings

Page 37: Php code-auditing

©2009 Justin C. Klein Keane

Create Request

Once Firefox is configured to utilize Paros browse through the site normally

Note how Paros records all your interactions Try submitting the login form Note that Paros records GET and POST

requests

Page 38: Php code-auditing

©2009 Justin C. Klein Keane

Paros in Action

Page 39: Php code-auditing

©2009 Justin C. Klein Keane

Paros Records Details

Page 40: Php code-auditing

©2009 Justin C. Klein Keane

Alter Requests

To alter a request click on it in the bottom window

Next right click and select “Resend” This opens a new window where you can alter

any of the send requests Change any data and click the “Send” button

Page 41: Php code-auditing

©2009 Justin C. Klein Keane

Paros Resend

Page 42: Php code-auditing

©2009 Justin C. Klein Keane

Response is Raw

Page 43: Php code-auditing

©2009 Justin C. Klein Keane

Bypassing the Login

In our manual code analysis we found a SQL injection vulnerability in the login form

A JavaScript check prevents easy manual testing

We could disable JavaScript or use Paros or Tamper Data to alter the data we're submitting for the login form

First let's examine the query

Page 44: Php code-auditing

©2009 Justin C. Klein Keane

Our Target

$sql = "select user_id from user where user_username = '" . $_POST['username'] . "'AND user_password = md5('" .$_POST['password'] . "')";

Page 45: Php code-auditing

©2009 Justin C. Klein Keane

Target SQL

select user_id from userwhere user_username = 'somename'and user_password = md5('somepass');

Page 46: Php code-auditing

©2009 Justin C. Klein Keane

Possible Permutation

select user_id from userwhere user_username = 'somename'

or 1='1'and user_password = md5('somepass');

What is the proper input to create this statement?

Page 47: Php code-auditing

©2009 Justin C. Klein Keane

Testing Your SQL

Page 48: Php code-auditing

©2009 Justin C. Klein Keane

Bypassing Loginwith SQL Injection

Page 49: Php code-auditing

©2009 Justin C. Klein Keane

We're In!

Page 50: Php code-auditing

©2009 Justin C. Klein Keane

Chained Exploits

Note that the exploitation of the authentication leads to access to new, potentially exploitable functionality

Authentication leads to cookie granting Admin functions are often “trusted”

Page 51: Php code-auditing

©2009 Justin C. Klein Keane

Steps to Remember

Look for vulnerabilities In the source code In the functional front end

Test your exploits in the “friendliest” environment possible

Use tools to recreate attacks in the live environment.

Page 52: Php code-auditing

©2009 Justin C. Klein Keane

For Next Time

-Install Paros Proxy

-Install Firefox and the Tamper Data and Web

Developer plug ins

-Download and install the sample SQL injection

application on your VM

-Identify at least 4 SQL injection vulnerabilities

-Develop exploits for each vulnerability

-Develop fixes for each vulnerability