Utilizing the Available Safety Features in Your Programming Language

Một phần của tài liệu hack proofing your network second edition phần 4 doc (Trang 21 - 25)

Combating unexpected user data is not a new thing—in fact, many programming languages and applications already have features that allow you to reduce or mini- mize the risks of tainted data vulnerabilities. Many of the features use the sandbox concept of keeping the tainted data quarantined until it is properly reviewed and cleaned. A few of the more popular language features follow.

Perl

Perl has a “taint” mode, which is enabled with the –T command-line switch.

When running in taint mode, Perl will warn of situations where you directly pass user data into one of the following commands: bind, chdir, chmod, chown, chroot, con- nect, eval, exec, fcntl, glob, ioctl, kill, link, mkdir, require, rmdir, setpgrp, setpriority, socket, socketpair, symlink, syscall, system, truncate, umask, unlink, as well as the –s switch and backticks.

Passing tainted data to a system function will result in Perl refusing to execute your script with the following message: Insecure dependency in system while running with -T switch at (script) line xx.

To “untaint” incoming user data, you must use Perl’s matching regex (m///) to verify that the data matches your expectations.The following example verifies that the incoming user data is lowercase letters only:

#!/usr/bin/perl -T

# must setup a secure environment (system/OS dependant)

$ENV{PATH}="/bin";

delete $ENV{ENV};

delete $ENV{BASH_ENV};

# this is tainted

$echo=$ARGV[0];

# check to see if it's only lower-case letters if ($echo =~/^([a-z]+)$/) {

# we resave the command...

$echo=$1;

# ...and use it in a system function system("/bin/echo $echo");

} else {

print "Sorry, you gave unexpected data\n";

}

The most important part of this code is the testing of the incoming data:

If ($echo =~ /^([a-z]+)$/) {

$echo = $1;

This regex requires that the entire incoming string (the ^ and $ force this) have only lowercase letters ([a-z]), and at least one letter (the + after [a-z]).

When untainting variables, you must be careful that you are indeed limiting the data. Note the following untaint code:

if ($data =~ /^(.*)$/) {

$data = $1;

This is wrong; the regex will match anything, therefore not limiting the

incoming data—in the end it serves only as a shortcut to bypass Perl’s taint safety checks.

PHP

PHP includes a “safe_mode” configuration option that limits the uses of PHP’s system functions. Although it doesn’t directly help you untaint incoming user data, it will serve as a safety net should an attacker find a way to bypass your taint checks.

When safe mode is enabled, PHP limits the following functions to only be able to access files owned by the user ID (UID) of PHP (which is typically the UID of the Web server), or files in a directory owned by the PHP UID: include, readfile, fopen, file, link, unlink, symlink, rename, rmdir, chmod, chown, and chgrp.

Further, PHP limits the use of exec, system, passthru, and popen to only be able to run applications contained in PHP_SAFE_MODE_EXEC_DIR directory (which is defined in php.h when PHP is compiled). Mysql_Connect is limited to only allow database connections as either the UID of the Web server or UID of the currently running script.

Finally, PHP modifies how it handles HTTP-based authentication to prevent various spoofing tricks (which is more of a problem with systems that contain many virtually hosted Web sites).

ColdFusion/ColdFusion Markup Language

ColdFusion features integrated sandbox functionality in its Advanced Security configuration menu that you can use to limit the scope of system functions should an attacker find a way to bypass your application checks.You can define systemwide or user-specific policies and limit individual CFML tags in various ways. Examples of setting up policies and sandboxes are available at the following URLs:

■ www.allaire.com/Handlers/index.cfm?ID=7745&Method=Full

■ www.allaire.com/Handlers/index.cfm?ID=12385&Method=Full

ASP

Luckily, ASP (VBScript and JScript) does not contain many system-related func- tions to begin with. In fact, file-system functions are all that are available (by default).

ASP does contain a configuration switch that disallows “../” notation to be used in file-system functions, which limits the possibility of an attacker gaining access to a file not found under the root Web directory.To disable parent paths, you need to open up the Microsoft Management Console (configuration console for IIS), select the target Web site, go to Properties | Home Directory | Configuration | Application Options, and uncheck Enable Parent Paths, as shown in Figure 7.3.

If you do not need file-system support in your ASP documents, you can remove it all together by unregistering the File System Object by running the following command at a console command prompt:

regsvr32 scrrun.dll /u

Figure 7.3Disabling Parent Paths Prevents an Attacker from Using “..”

Directory Notation to Gain Access to Files Not in Your Web Root

MySQL

The MySQL database contains the ability to read data in from or out to files during queries using the following syntax in a query:

SELECT * INTO FILE "/file/to/save.db" FROM table

You can limit this behavior by not granting “file” permissions to any users in MySQL’s built-in privilege table.

Một phần của tài liệu hack proofing your network second edition phần 4 doc (Trang 21 - 25)

Tải bản đầy đủ (PDF)

(82 trang)