PHP Audit - The art of finding 0days in webapps Part 1
0x01 - Introduction
[+] Who is this tutorial for?
Before we start I just want to state that I assume you have basic understanding about PHP, MySQL and some common vulnerabilities and how they work.
You don't have to be a professional programmer, but you need to be able to read and understand PHP code.
If you feel that you need to learn some more before getting in to this tutorial I'm going to redirect you to some tutorials below:
PHP tutorial:
Code
http://php.net/manual/en/tutorial.phpMySQL tutorial:
Code
http://dev.mysql.com/doc/refman/5.0/en/tutorial.htmlVulnerabilities wiki:
Code
https://www.owasp.org/index.php/Category:Attack[+] Who am I?
Nobody. I don't take any responsibilities for your actions, if you find your ass in jail after exploiting 8000 vulnerable Wordpress blogs don't blame me for teaching you.
But if you have any questions feel free to hit me up with a mail to join7 [+at+] riseup.net
0x02 - Setting up an Audit Environment
[+] Why use an Audit Environment?
You could just download the code you wish to audit, open it in your systems default text editor and start looking for vulnerabilities.
This is not preferable since there are a very high chance of you missing things and it will surely take a lot more time than by using an Audit Environment.
[+] Web server
We want to set up a Web Server were we can upload and test PHP applications.
If you don't have any experience with hosting you might want to have a look at XAMPP:
Code
http://www.apachefriends.org/en/xampp.htmlIf you wish to set it up on your own you should install PHP with MySQL.
Additional things as phpmyadmin and MySQL managers are of course useful too.
I'm not going to go into how to set up an Web Server, just google for it you're having problems.
Note: Since you're going to test possible vulnerable code on this web server I suggest you only use it on trusted networks and NEVER on a host that's public for the rest of the internet thugs out there.
[+] What are we auditing?
Now when we have our server set up we need to install additional stuff like WordPress, Joomla, MyBB or similar depending on what you're going to audit.
If you're going for a standalone CMS or just a PHP application you will of course not need any of the above but you'll probably some time in your career step into a WordPress blog or a MyBB forum.
Here are download links and some information on how to install them:
MyBB
![[Image: Yg0G9.jpg]](https://i.imgur.com/Yg0G9.jpg)
Download: http://www.mybb.com/downloads
Installing Guide: http://docs.mybb.com/Installing.html
Wordpress
![[Image: aBafF.jpg]](https://i.imgur.com/aBafF.jpg)
Download: http://wordpress.org/download/
Installing Guide: http://codex.wordpress.org/Installing_WordPress
Joomla
![[Image: xFM55.png]](https://i.imgur.com/xFM55.png)
Download: http://www.joomla.org/download.html
Installing Guide: http://www.joomla.org/about-joomla/getting-started.html
SMF
[IMG]http://s4.hubimg.com/u/5814063_f520.jpg[/IMG]
Download: http://download.simplemachines.org/
Installing Guide: http://wiki.simplemachines.org/smf/Installing
The list can go on...
0x03 - What to look for
[+] User input:
Most vulnerabilities are possible because the programmer forgets (or uses improper) Input Validation.
This true for SQL Injection, Cross-Site Scripting, File Inclusion, Server Side Include, Code Injection and File Upload vulnerabilities and a lot more.
[-] $_GET
Quote:An associative array of variables passed to the current script via the URL parameters.
Code
http://php.net/manual/en/reserved.variables.get.php[-] $_POST
Quote:An associative array of variables passed to the current script via the HTTP POST method.
Code
http://php.net/manual/en/reserved.variables.post.php[-] $_REQUEST
Quote:An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.
Code
http://php.net/manual/en/reserved.variables.request.php[-] $_COOKIE[/size]
Quote:An associative array of variables passed to the current script via HTTP Cookies.
Code
http://php.net/manual/en/reserved.variables.cookies.php[-] $_SERVER
Quote:$_SERVER -- $HTTP_SERVER_VARS [deprecated] — Server and execution environment information
Code
http://php.net/manual/en/reserved.variables.server.php[-] $_FILES
Quote:An associative array of items uploaded to the current script via the HTTP POST method.
Code
http://php.net/manual/en/reserved.variables.files.php[+] Possible vulnerable functions:
[-] SQL Injection:
Vulnerable Example:
PHP Code
<?php
$id = $_GET['id'];
$news = mysql_query( "SELECT * FROM `news` WHERE `id` = $id ORDER BY `id` DESC LIMIT 0,3" );
- mysql_*
- $db->* (For MyBB, look here for more info: http://docs.mybb.com/Plugins-Globals.html & http://docs.mybb.com/Database_Methods.html )
- $wpdb->* (For WordPress - Look here for more info: http://codex.wordpress.org/Class_Reference/wpdb )
There are alot of possibly vulnerables mysql_* functions.
Take a look at this reference for more information about MySQL functions:
Code
http://se1.php.net/manual/en/ref.mysql.phpSome common functions that is possibly vulnerable:
- mysql_db_query — Selects a database and executes a query on it
- mysql_fetch_array — Fetch a result row as an associative array, a numeric array, or both
- mysql_fetch_field — Get column information from a result and return as an object
- mysql_fetch_row — Get a result row as an enumerated array
- mysql_num_rows — Get number of rows in result
- mysql_result — Get result data
[-] File Inclusion:
Vulnerable Example:
PHP Code
<?php
$file = $_GET['file'];
if(isset($file))
{
include("pages/$file");
}
else
{
include("index.php");
}
?>- include()
- require()
If the application doesn't restrict how you include files you can read local files and possibly execute files from a remote server (if allow_url_fopen is enabled).
[-] Upload:
PHP Code
<?php
$target_path = "uploads/";
$target_path = $target_path . basename($_FILES['uploadedfile']['name']);
if (move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file " . basename($_FILES['uploadedfile']['name']) . " has been uploaded";
} else {
echo "There was an error uploading the file, please try again!";
}
?>- $_FILES[]
[-] Code Execution:
PHP Code
<?php
$myvar = "varname";
$x = $_GET['arg'];
eval("\$myvar = \$x;");
?>- passthru()
- system()
- eval()
- exec()
Note: Go to Part 2 of this tutorial: https://hackforums.net/showthread.php?tid=3108156



