Web Hacking Lesson 1
This exercise is designed to expose you to several of the top threat vectors facing web based applications, specifically PHP/MySQL applications. 'Threat vector' is a common term used in computer security to connote ways in which an attacker will attempt to compromise a system. System is used in a broad sense here because a compromised web application can easily lead to a compromised web server which in term could lead to a compromised operating system.
Defending web based application is of critical importance because they are some of the most common targets of attackers. When web application security is defeated it can lead to any number of outcomes, largely determined by the motives of the attacker. Because we can never fully predict motives of an attacker web based applications must be defended against all threats, regardless of how small they seem. What may at first seem like an inconsequential threat may turn out to be the linchpin of a complex attack. Developers must defend against not only existing attacks but also emerging ones and future vectors that have not yet been considered. While this may at first seem to be an insurmountable task, it is in fact possible using best practices and insuring that an application may only behave as it was intended. Enforcement of design becomes the key to preventing many web application vulnerabilities.
During this first exercise we will look at several common attack routes from the perspective of an attacker (i.e. without the source code of the application available). Once familiar with these attacks we will look at source code an examine strategies to foil such attacks.
It is important that when simulating an attacker that we have the best tools available to exploit web based applications. Such tools will allow us to send malformed URL requests, intercept and alter form submissions, and examine, alter, and even create our own cookies. The Firefox web browser has many extensions that will allow us to do all of these things. Once Firefox is installed download and install the web developer extension (https://addons.mozilla.org/en-US/firefox/addon/60) as well as the tamper data extension (https://addons.mozilla.org/en-US/firefox/addon/966). These extensions will allow you greater ease in exploiting web applications.
Installing the Test Site
In order run through the exercises in this series you'll need to download and install the test site (which is a very simple blogging application). The test site (madirish_training.zip) is distributed as a zipped directory that you should be able to unzip with pretty much any archiving program.
The site is a PHP (http://www.php.net) and MySQL (http://www.mysql.com) application that should be able to run on pretty much any web server. If you don't have a web server, PHP or MySQL installed don't worry. The easiest way to get them installed is to check out XAMPP (http://sourceforge.net/projects/xampp/). You can download and install XAMPP and get Apache (http://www.apache.org), PHP and MySQL installed. Alternatively you can install each component on your own. All three can run on Windows or Linux so you can use whatever hardware you have right now. I won't go into specifics of installation since each piece has pretty good documentation on the installation process on their website.
One you've got these prerequisite software pieces installed you need to install the test site. Go ahead and unzip the test site in your htdocs directory (or whatever web root you're using). You can then point your web browser to your site name and add 'madirish/install.php' to the URL. For instance, if everything is installed on your local machine, point your browser to 'http://localhost/madirish/install.php'. This page will ask you some questions about your database and install the mysql database (called hack_test) and make sure that your web server can write to the necessary directories for the application to work.
Note that if you don't feel comfortable using the install page you can install the database quite easily using the script db.sql found in the sql directory in the training. To install this way simply open a command prompt, navigate to the unzipped training directory and type:
mysql -u root - p < sql/db.sql
Once you've completed the exercises you may want to see if you can 'fix' the test site so that it's no longer vulnerable to the exploits described below. Be sure to delete the test site when you're done or keep it behind a firewall, since it's deliberately designed to be insecure and could be used to compromise your computer!
You can start the exercise by pointing your browser at your install URL with "index.php" added to it. In my case this URL is "http://localhost/madirish/index.php".
Cross Site Scripting
Cross site scripting (CSS or XSS) is a common web application vulnerability that can allow a malicious user to abuse an application in many ways. The test site has several XSS vulnerabilities. The first of these is presented by the way the application handles failed logins. Pull up the application page and attempt to log in using any made up username and password. You'll note that the application correctly handles the error message by not revealing whether or not the user exists. For instance, some applications may respond to a failed login with a message such as 'password incorrect' or 'user account does not exist'. These types of messages leak information about the failed login that can be useful for an attacker. For instance, if an attacker is trying to brute force an account they must correctly guess the account name and password. Brute forcing is an attack based on guessing. For instance, the attacker could simply try one username with every password they could think of, then move on. Computers are quite good at brute forcing, with automated attack programs that can tirelessly guess usernames and passwords. Given enough time any username and password combination can be guessed by a computer. Giving the attacker clues as to whether an account exists reduces the time it takes to brute force that account because the attacker does not have to guess at account names, merely passwords.
The sample application correctly alerts the users that their login attempt failed, but does not hint as to whether or not it was the username or password that did not match. However, there is a critical flaw in how the application alerts the user to this fact. Can you spot the flaw? Can you exploit this flaw to cause the page to redirect to "http://www.MadIrish.net"?
<script>alert('Sorry, could not log in with those credentials.');</script>
Try changing the URL so that it reads something else, for instance, try going to the URL "index.php?msg=foo" and observe what happens, view the resulting page source code. Can you spot the flaw yet?
You can see that the URL variable is being included directly in the page source. Now try going to the URL "index.php?msg=One moment');location.href=('http://www.sas.upenn.edu" and observe what happens.
Now, this may seem innocuous enough, but lets take the attack one step forward and URL encode the entire query string. To do this we simply craft the URL using the hexidecimal values for each character that the browser will properly interpret, but which a user won't be able to understand. Try going to the URL:
Notice what happened? Now, an end user certainly won't understand what this URL is up to, but you can clearly see the hidden redirect. What if instead of the University of Pennsylvania web site this redirect pointed to a malicious phishing site that harvested usernames and passwords then redirected the user right back to the URL "http://localhost/madirish/?msg=Account confirmed, pleas log in". Many users would never notice they had ever visited the phishing page. This type of error could be exploited to harvest usernames and passwords from our application by sending an e-mail to users that said the user needed to log into the site using the above URL to confirm their account.
How can this attack be prevented? The easiest way is to enforce that the application behaves exactly as it was intended. The actual PHP code used reads:
Now, if the code was altered so that it read:
The message string could be altered, but it would never affect how the application functioned. In essence we want to restrict the URL variable from escaping out of the single quotes in the alert. It is tempting to simply do a string replace and replace all single quotes with an escaped single quote, so that the URL:
was converted into the string:
which would effectively foil this attack. However, this approach fails if the attacker enters the URL:
because escaping the single quote converts the string into the value:
which in fact escapes the back slash, rather than the single quote. Trying to plan for variances in URL crafting will ultimately fail. The only safe way to enforce the parameter is correctly interpreted is to use PHP's native functionality (rather than "rolling your own" solution).