29.11.2012 Views

2nd USENIX Conference on Web Application Development ...

2nd USENIX Conference on Web Application Development ...

2nd USENIX Conference on Web Application Development ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

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

Sanitisati<strong>on</strong> htmlentities()<br />

functi<strong>on</strong>s htmlspecialchars()<br />

echo() ⇒ AspisAntiXss()<br />

Guarded sinks print() ⇒ AspisAntiXss()<br />

...<br />

Table 2: Excerpt of the definiti<strong>on</strong> of the XSS taint category<br />

plied to PHP source code to (a) ensure its correct operati<strong>on</strong><br />

in the presence of taint meta-data; (b) propagate<br />

taint meta-data correctly; and (c) attach checks that inspect<br />

taint meta-data before each “sensitive” operati<strong>on</strong>.<br />

Finally, we discuss how the n<strong>on</strong>-tracking code can interact<br />

with the parts of the applicati<strong>on</strong> that have been transformed<br />

to track taint in §3.3<br />

3.1 Taint Representati<strong>on</strong><br />

PHP Aspis uses character-level taint tracking, i.e. tracks<br />

the taint of each string character individually [19]. Traditi<strong>on</strong>al<br />

variable-level taint tracking implementati<strong>on</strong>s (e.g.<br />

Ruby’s safe levels) require the developer to untaint values<br />

explicitly before they are used. Instead, PHP Aspis prevents<br />

injecti<strong>on</strong> attacks transparently, and for this, it needs<br />

to know the exact characters that originate from the user.<br />

For example, c<strong>on</strong>sider an applicati<strong>on</strong> that c<strong>on</strong>catenates a<br />

user-provided value with a static HTML template, stores<br />

the result in $v and then returns $v to the client as a resp<strong>on</strong>se.<br />

Inferring that variable $v is tainted is of little use<br />

because $v also c<strong>on</strong>tains applicati<strong>on</strong>-generated HTML.<br />

Instead, PHP Aspis uses character-level taint meta-data<br />

and <strong>on</strong>ly sanitises the user-generated parts of $v.<br />

3.1.1 Taint Categories<br />

PHP Aspis can track multiple independent and user provided<br />

taint categories. A taint category is a generic way<br />

of defining how an applicati<strong>on</strong> is supposed to sanitise data<br />

and how PHP Aspis should enforce that the applicati<strong>on</strong><br />

always sanitises data before they are used.<br />

Each taint category is defined as a set of sanitisati<strong>on</strong><br />

functi<strong>on</strong>s and a set of guarded sinks. Sanitisati<strong>on</strong> functi<strong>on</strong>s<br />

can be PHP library functi<strong>on</strong>s or can be defined<br />

by the applicati<strong>on</strong>. A sanitisati<strong>on</strong> functi<strong>on</strong> is called by<br />

the applicati<strong>on</strong> to transform untrusted user data so that<br />

they cannot be used for a particular type of injecti<strong>on</strong><br />

attack. Comm<strong>on</strong>ly, sanitisati<strong>on</strong> functi<strong>on</strong>s either transform<br />

unsafe character sequences to safe equivalents (e.g.<br />

htmlentities) or filter out a subset of potentially dangerous<br />

occurrences (e.g. by removing but not<br />

). Calls to sanitisati<strong>on</strong> functi<strong>on</strong>s by the applicati<strong>on</strong><br />

are intercepted and PHP Aspis untaints the corresp<strong>on</strong>ding<br />

data to avoid sanitising them again.<br />

Guarded sinks are functi<strong>on</strong>s that protect data flow to<br />

sensitive sink functi<strong>on</strong>s. When a call to a sink functi<strong>on</strong><br />

5<br />

is made, PHP Aspis invokes the guard with references to<br />

the parameters passed to the sink functi<strong>on</strong>. The guard<br />

is a user-provided functi<strong>on</strong> that has access to the relevant<br />

taint category meta-data and typically invokes <strong>on</strong>e<br />

or more sanitisati<strong>on</strong> functi<strong>on</strong>s for that taint category.<br />

For example, Table 2 shows an excerpt of an XSS<br />

taint category definiti<strong>on</strong>. It specifies that a user-provided<br />

string can be safely echoed to the user after either<br />

htmlentities or htmlspecialchars has been invoked<br />

<strong>on</strong> it. The sec<strong>on</strong>d part exhaustively lists all functi<strong>on</strong>s<br />

that can output strings to the user (e.g. echo,<br />

print, etc.) and guards them with an external filtering<br />

functi<strong>on</strong> (AspisAntiXss). The guard either aborts<br />

the print operati<strong>on</strong> or sanitises any remaining characters.<br />

The administrator can change the definiti<strong>on</strong>s of taint categories<br />

according to the requirements of the applicati<strong>on</strong>.<br />

By listing all the sanitisati<strong>on</strong> functi<strong>on</strong>s of an applicati<strong>on</strong><br />

in the relevant taint category, PHP Aspis can closely<br />

m<strong>on</strong>itor the applicati<strong>on</strong>’s sanitisati<strong>on</strong> efforts. When applied<br />

to a well designed applicati<strong>on</strong>, PHP Aspis untaints<br />

user data as they get sanitised by the applicati<strong>on</strong>, but before<br />

they reach the sink guards. Thus, sink guards can<br />

apply a simple, applicati<strong>on</strong> agnostic, sanitisati<strong>on</strong> operati<strong>on</strong><br />

(e.g. htmlentities) acting as a “safety net”.<br />

On the other hand, an applicati<strong>on</strong> may not define explicit<br />

sanitisati<strong>on</strong> functi<strong>on</strong>s or these functi<strong>on</strong>s may be<br />

omitted from the relevant taint category. In such cases,<br />

sink guards have to replicate the filtering logic of the applicati<strong>on</strong>.<br />

In general, however, sink guards lack c<strong>on</strong>textual<br />

informati<strong>on</strong> and this prevents them from enforcing<br />

c<strong>on</strong>text-aware filtering, e.g. guards cannot enforce sanitisati<strong>on</strong><br />

that varies according to the current user.<br />

A different taint category must be used for each type of<br />

injecti<strong>on</strong> vulnerability. PHP Aspis tracks different taint<br />

categories independently from each other. For example,<br />

when a sanitisati<strong>on</strong> functi<strong>on</strong> of an XSS taint category is<br />

called <strong>on</strong> a string, the string is still c<strong>on</strong>sidered unsanitised<br />

for all other taint categories. This ensures that a sanitisati<strong>on</strong><br />

functi<strong>on</strong> for handling <strong>on</strong>e type of injecti<strong>on</strong> vulnerability<br />

is not used to sanitise data for another type.<br />

3.1.2 Storing taint meta-data<br />

It is challenging to represent taint meta-data so that it supports<br />

arbitrary taint categories and character-level taint<br />

tracking. This is due to the following properties of PHP:<br />

P1 PHP is not object-oriented. Although it supports<br />

objects, built-in types such as string cannot be<br />

augmented transparently with taint meta-data. This<br />

precludes soluti<strong>on</strong>s that rely <strong>on</strong> altered class libraries<br />

[6].<br />

P2 PHP does not offer direct access to memory. Any soluti<strong>on</strong><br />

must track PHP references because variables’<br />

memory addresses cannot be used [18].<br />

<str<strong>on</strong>g>USENIX</str<strong>on</strong>g> Associati<strong>on</strong> <strong>Web</strong>Apps ’11: <str<strong>on</strong>g>2nd</str<strong>on</strong>g> <str<strong>on</strong>g>USENIX</str<strong>on</strong>g> <str<strong>on</strong>g>C<strong>on</strong>ference</str<strong>on</strong>g> <strong>on</strong> <strong>Web</strong> Applicati<strong>on</strong> <strong>Development</strong> 17

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

Saved successfully!

Ooh no, something went wrong!