TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Advertisement
Securing your PHP applications Part 1
   As long as there are programming languages people will try to hack them, fortunately for us this means we have to have our wits about us when writing applications. In this 2 part article we'll be discussing different ways that hackers try and break into our applications and how we go about protecting our applications from possible harm.

Database Security

Many websites fall under the attack known as SQL Injection. SQL injection occurs when a malicious user experiments on a form to gain information about a database. After gaining sufficient knowledge, usually from database error messages the attacker is equipped to exploit the form for any possible vulnerabilities by injecting SQL into form fields. With SQL Injection a hacker can retrieve your data, insert, delete, basicly can do anything with your database.

A very common example is:

<?php

$username = $_POST['username'];

query = "SELECT * FROM users WHERE username= $username";

?>

Here it is easy for a hacker to try and experiment with your form by giving it statements such as 'OR 1' or 'SELECT username'.

This is easily fixable by using mysql_real_escape_string. What this does is take a string that is going to be used and return the same string with all SQL Injection attempts safely escaped. It will replace those troublesome quotes(') a user might enter with \'.

Example:

<?php

$username = $_POST['username'];

$username = mysql_real_escape_string($username);

query = "SELECT * FROM users WHERE username= $username";

?>

It is always best to make sure that whenever user input is required to use mysql_real_escape_string to ensure that whatever has been given is clean and won't harm your application. Remember NEVER TRUST USER INPUT!

Session Security

Mainly there are 2 types of session hacking, Session Fixation and Session Hijacking. When a user first encounters a page in your application that calls session_start(), a session is created for the user. PHP generates a random session identifier to identify the user, and then it sends a Set-Cookie header to the client. By default, the name of this cookie is PHPSESSID, but it is possible to change the cookie name in php.ini or by using the session_name() function. On subsequent visits, the client identifies the user with the cookie, and this is how the user's data is recalled.

It is possible to set the session identifier through manual input this way a hacker is able to "ride" a session.

An example of this is:

http://yourdomain.com/index.php?PHPSESSID=283

An easy way of preventing this from happening is to regenerate your sessions id every time a user logs in.

Example:

<?php

session_start();
// A user just logged in now call the session_regenerate_id() function
{
session_regenerate_id();
}

?>

This is a quick way to protect your site from any would be hacker. Unfortunately it doesn't protect your site from Session Hijacking, this happens when the person discovers another's session id rather than providing his own. So we would have to identify the person using the session to prevent this. One way of doing this is by using the User-Agent request header. Because it is highly unlikely that a user will change browsers using the same session we'll use this header to identify our user.

When a user logs in identify their User-Agent:

<?php

session_start();
// A user just logged in now call the session_regenerate_id() function
{
session_regenerate_id();
$_SESSION['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
}

?>

Now to prevent our would be hacker from accommodating our session we'll have to check the User-Agent every now and then. Call this up on subsequent pages or every page if you prefer:

<?php

if ($_SESSION['user_agent'] != $_SERVER['HTTP_USER_AGENT'])
{
// Bye now Mr hacker
session_destroy();
exit;
}

?>

Implementing these easy techniques are the best route to go for protecting your applications from malicious attacks. Next time we'll discuss protecting your Filesystem and protection from Cross-Site Scripting better known as XSS. Enjoy!
Report this Article
Last 5 Article Reviews Read All Reviews
   Good Article
Review added by ryanmr on 06-14-2008
These are good things to practice. User Agent is better than IP Address because a User Agent can't actually change during a session while a dial up's IP could possibly change every few requests.
   Really Good!
Review added by SpYkE112 on 06-01-2008
Really good article, covers the principels of simple protection good :)

Maybe you could think of some function which does it all for you? I don't know :)
   Great basic preventative measures
Review added by Highway of Life on 05-07-2008
Good article.
When using session_regenerate_id(), it’s a good idea to delete the old session, as session_regenerate_id() does not delete the old session and so is still capable of being hijacked, although this is less of a concern if the new session is only used once logged in, it would still be good practice to destroy the old session id.
User agent is a good way to validate a session, yet that can still be spoofed to match, it should hash the user agent to an md5 and compare the hashed value to the users' agent on the page load, but also the first two or three sets of the user's IP should also be validated against.
   Very Informative!
Review added by Erutan409 on 04-04-2008
Just when I thought I had all the right coded tricks up my sleeve you go and unveil a plethora of useful information that I'll implementing into my scripts. What you wrote makes sense. Session can be very tricky and with the examples/tricks you pointed out in your article they can be put to good use; and should!

All times are GMT. The time now is 08:49 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design