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.

try to use them. For example, what happens if the attacker puts a single quote in<br />

the data? Will that end the quoting, allowing the rest of the attacker’s data to do<br />

malicious things? If there are such escapes, you should check for them. In this<br />

particular example, you might be able to replace quotes in the attacker’s data<br />

with a backslash followed by a quote.<br />

When designing your own quoting mechanisms, do not allow escapes.<br />

Following from the previous point, if you need to filter data instead of rejecting<br />

potentially harmful data, it is useful to provide functions that properly quote an<br />

arbitrary piece of data for you. For example, you might have a function that<br />

quotes a string for a database, ensuring that the input will always be interpreted<br />

as a single string and nothing more. Such a function would put quotes around<br />

the string and additionally escape anything that could thwart the surrounding<br />

quotes (such as a nested quote).<br />

The better you understand the data, the better you can filter it.<br />

Rough heuristics like “accept the following characters” do not always work well<br />

for data validation. Even if you filter out all bad characters, are the resulting<br />

combinations of benign characters a problem? For example, if you pass<br />

untrusted data through a shell, do you want to take the risk that an attacker<br />

might be able to ignore metacharacters but still do some damage by throwing in<br />

a well-placed shell keyword?<br />

The best way to ensure that data is not bad is to do your very best to understand<br />

the data and the context in which that data will be used. Therefore, even if<br />

you’re passing data on to some other component, if you need to trust the data<br />

before you send it, you should parse it as accurately as possible. Moreover, in situations<br />

where you cannot be accurate, at least be conservative, and assume that<br />

the data is malicious.<br />

See Also<br />

Recipes 1.7, 3.3, 3.9, 3.11<br />

3.2 Preventing Attacks on Formatting Functions<br />

<strong>Problem</strong><br />

You use functions such as printf( ) or syslog( ) in your program, and you want to<br />

ensure that you use them in such a way that an attacker cannot coerce them into<br />

behaving in ways that you do not intend.<br />

Preventing Attacks on Formatting Functions | 75<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!