
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
342
|
Chapter 10: Securing Web Servers
Executing Programs
Most scripting languages let you run external programs. This is a golden opportu-
nity for nasty tricks. Check the pathname of the external program and remove any
metacharacters that would allow multiple commands. Avoid passing commands
through a shell interpreter.
PHP
Escape any possible attempts to slip in extra commands with this PHP function:
$safer_input = escapeshellarg($input);
system("some_command $safer_input");
or:
system(escapeshellcmd("some_command $input"));
These PHP functions invoke the shell and are vulnerable to misuse of shell metachar-
acters:
system, passthru, exec, popen, preg_replace (with the /e option), and the
backtick (
`command`) operator.
If
safe_mode is set, only programs within safe_mode_exec_dir can be executed, and
only files owned by the owner of the PHP script can be accessed.
The PHP function
eval($arg) executes its argument $arg as PHP code. There’s no
equivalent to
safe_mode for this, although the disable_functions option lets you turn
off selected functions. Don’t execute any command with embedded user data.
Perl
Taint mode will not let you pass unaltered user input to the functions system, exec,
eval, or the backtick (`command`) operator. Untaint them before executing, as
described earlier.
Uploading Files ...