PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that...

47
PHP Aspis: Using Partial Taint Tracking To Protect Against Injection Attacks USENIX WebApps 2011 Portland, OR, USA Ioannis Papagiannis, Matteo Migliavacca, Peter Pietzuch Department of Computing, Imperial College London

Transcript of PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that...

Page 1: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

PHP Aspis:

Using Partial Taint Tracking To

Protect Against Injection Attacks

USENIX WebApps 2011

Portland, OR, USA

Ioannis Papagiannis, Matteo Migliavacca, Peter Pietzuch

Department of Computing, Imperial College London

Page 2: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

2

Injection Vulnerability Example

USENIX WebApps 2011

<?php

$name=$GET[„name‟];

$sql =

“SELECT *

FROM USERS

WHERE user=”.

$name;

mysql_query($sql);

?>

Page 3: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

3

Injection Vulnerability Example

USENIX WebApps 2011

<?php

$name=$GET[„name‟];

$sql =

“SELECT *

FROM USERS

WHERE user=”.

;

mysql_query($sql);

?>

http://…?name=

Page 4: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

4

Sanitisation

USENIX WebApps 2011

<?php

$name=$GET[„name‟];

$sql =

“SELECT *

FROM USERS

WHERE user=”.

;

mysql_query($sql);

?>

http://…?name=Yiannis

Page 5: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

5

Taint Tracking

USENIX WebApps 2011

<?php

$name=$GET[„name‟];

$sql =

“SELECT *

FROM USERS

WHERE user=”.

$name;

mysql_query($sql);

?>

1

2

3

taint data in entry points

propagate taint

use taint to guide sanitisation

Page 6: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

6

Taint Tracking in PHP

No support

1. Suggested but ignored

[Venema06]

2. Custom research interpreters

[Yip09, Pietraszek06]

USENIX WebApps 2011

taint

tracking

“modifications to the Zend engine should be avoided.

Changes here result in incompatibilities with the rest

of the world, and hardly anyone will ever adapt to

specially patched Zend engines. … Therefore, this

method is generally considered bad practice”

The PHP Manual

Page 7: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

7

PHP is popular

USENIX WebApps 2011

data provided by langpop.com

Page 8: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

8

PHP Aspis Contributions

Source-to-source transformations

Partial Taint Tracking

taint

tracking!

USENIX WebApps 2011

2

1

Page 9: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

9

PHP Aspis Contributions

Source-to-source transformations

Partial Taint Tracking

taint

tracking!

USENIX WebApps 2011

2

1

Page 10: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

10

Why source transformations?

USENIX WebApps 2011

• Adopt officially

• Custom Runtime

• Source code transformations On demand

Portable

1

Page 11: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

11

Why source transformations?

USENIX WebApps 2011

Challenges:

1. Not everything is an object (how can you attach taint to strings?)

2. The interpreter cannot be edited

(no metaprogramming)

• Adopt officially

• Custom Runtime

• Source code transformations On demand

Portable

1

Page 12: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

12

What is the performance overhead?

Partial taint tracking: Code is not equally vulnerable

Third-party plugin code

Code that handles user data

year WordPress WordPress

Plugins

2009 2 13

2010 2 10

CVE WordPress-Platform Injection Vulnerabilities

CVE # Functionality

2009-2851 Display user comments

2009-3891 File upload handler

2010-4257 Trackback handling

2010-4536 Display user comments

CVE WordPress-Core Injection Vulnerabilities

USENIX WebApps 2011

2

Page 13: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

13

2. DESIGN 1. Introduction

USENIX WebApps 2011

3. Implementation

4. Evaluation

Page 14: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

14

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

Page 15: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

15

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

“Yiannis”

Taint

Input HTTP

Request

Page 16: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

16

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

“Yiannis”

Taint

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

Input HTTP

Request

HTML Output

SQL Query

Eval Statement

Page 17: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

17

PHP Aspis Overview

USENIX WebApps 2011

WordPress

“Yiannis”

Taint

WordPress plugin

WordPress Core

Input HTTP

Request

HTML Output

SQL Query

Eval Statement

Page 18: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

18

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

“Yiannis”

Taint

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

Input HTTP

Request

HTML Output

SQL Query

Taint meta-data

Eval Statement

Vulnerability prevention 1

2

Page 19: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

19

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

“Yiannis”

Taint

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

Input HTTP

Request

HTML Output

SQL Query

Eval Statement

Taint meta-data Vulnerability prevention

1 2

Page 20: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

20

Taint Meta-data

Character Level

o Precise information

USENIX WebApps 2011

1

“SELECT *

FROM USERS

WHERE user=yiannis”;

untainted

tainted

Variable Level

o Leads to false positives

“SELECT *

FROM USERS

WHERE user=yiannis”;

tainted

Page 21: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

21

Taint Meta-data

USENIX WebApps 2011

1

More than 1 bit of taint meta-data is required

“SELECT *

FROM USERS

WHERE user=yiannis”;

untainted

Partial sanitisation (e.g. )

untainted (for SQL Injection)

Character Level

o Precise information

“SELECT *

FROM USERS

WHERE user=yiannis”;

untainted

tainted

Variable Level

o Leads to false positives

“SELECT *

FROM USERS

WHERE user=yiannis”;

tainted

Page 22: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

22

Example

Taint Categories

USENIX WebApps 2011

1

“SELECT *

FROM USERS

WHERE user=yiannis”;

Taint Category

SQL Injection XSS Eval Injection

untainted untainted untainted

tainted tainted

Page 23: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

23

Example

Taint Categories

USENIX WebApps 2011

1

“SELECT *

FROM USERS

WHERE user=yiannis”;

Generic way to define:

How an application is

supposed to sanitise

What to do if it doesn’t

Sanitisation

Functions

htmlentities()

htmlspecialchars()

Guarded

Sinks

echo()→AspisAntiXSS()

print()→AspisAntiXSS() …

XSS taint category excerpt

Taint Category

SQL Injection XSS Eval Injection

untainted untainted untainted

tainted tainted

Page 24: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

24

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

“Yiannis”

Taint

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

Input HTTP

Request

HTML Output

SQL Query

Taint meta-data Vulnerability prevention

Eval Statement

1 2

Page 25: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

25

PHP Aspis Overview

USENIX WebApps 2011

PHP Aspis Transformed Application

“Yiannis”

Taint

Tracking Code

PHP Statements Library Calls

Sanitisation Operations

Non Tracking Code

Input HTTP

Request

HTML Output

SQL Query

Taint meta-data Vulnerability prevention

Eval Statement

1 2

Page 26: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

26

Which vulnerabilities can be prevented?

USENIX WebApps 2011

2

Possible Data Flows

to

from

Tracking Non Tracking

Tracking

Non Tracking

Page 27: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

27

Tracking code only

USENIX WebApps 2011

2

PHP Aspis Transformed Application

Tracking Code

Non Tracking Code

Input HTTP

Request

“Yiannis”

Taint

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 28: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

28

Non Tracking code only

USENIX WebApps 2011

2

PHP Aspis Transformed Application

Tracking Code

Non Tracking Code

Input HTTP

Request

“Yiannis”

Taint

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 29: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

29 USENIX WebApps 2011

2

PHP Aspis Transformed Application

Tracking Code

Non Tracking Code

Input HTTP

Request

“Yiannis”

Taint

Non Tracking to Tracking

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 30: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

30 USENIX WebApps 2011

2

PHP Aspis Transformed Application

Tracking Code

Non Tracking Code

Input HTTP

Request

“Yiannis”

Taint

Tracking/Non Tracking mixes

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 31: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

31 USENIX WebApps 2011

2

PHP Aspis Transformed Application

Tracking Code

Non Tracking Code

Input HTTP

Request

“Yiannis”

Taint

Tracking to Non Tracking

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 32: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

32 USENIX WebApps 2011

2

PHP Aspis Transformed Application

Tracking Code

Non Tracking Code

Input HTTP

Request

“Yiannis”

Taint

Tracking to Non Tracking

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 33: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

33

Summary: Prevented Vulnerabilities

USENIX WebApps 2011

2

Vulnerabilities Prevented

Tracking-code only

Tracking to non tracking

Vulnerabilities Not Prevented

Non Tracking-code only

Non Tracking to Tracking

Tracking/Non Tracking mixes

to

From

Tracking Non

Tracking

Tracking

Non

Tracking

to

from

Tracking Non

Tracking

Tracking

Non

Tracking

Page 34: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

34

3. IMPLEMENTATION

1. Introduction

2. Design

USENIX WebApps 2011

4. Evaluation

Page 35: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

35

Storing Taint Meta-Data

USENIX WebApps 2011

Original value Aspis-protected value

“Hello”

array (

“Hello”, TaintCats

)

12

array (

12, TaintCats

)

Store taint in place

Interoperation with non-tracking code

Use arrays

2-10x faster than object initialisation

Scalar assignment semantics

Page 36: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

36

Taint Tracking Transformations

USENIX WebApps 2011

Statements & Expressions must

1. operate with Aspis-protected values

2. propagate taint correctly

3. return Aspis-protected values

Original expression Transformed Expression

$s.$t concat($s,$t)

if ($v) {} if ($v[0]) {}

$j = $i++ $j = postincr($i)

Page 37: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

37

PHP Function Library

USENIX WebApps 2011

Library functions do not work with Aspis-protected values

use interceptors!

Default Interceptor

strip input taint

add empty output taint

good as the default fclose(), fopen()

Custom Interceptors

guess the taint of the output substr()

reimplement the function sort()

More custom interceptors, less false negatives

o Default: drop taint, not abort the call

o Support existing applications without developer intervention

Page 38: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

38

The rest…

USENIX WebApps 2011

Taint representation Expressions Statements PHP Library Function Calls Taint initialisation Tracking/Non Tracking Calls Calls to sanitisation functions Calls to sinks Variable variables Variable function calls Include Statements Dynamic code generation

Page 39: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

39

4. EVALUATION

1. Introduction

2. Design

3. Implementation

USENIX WebApps 2011

Page 40: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

40

Goals

Is PHP Aspis effective in preventing injection attacks?

What is the performance overhead?

USENIX WebApps 2011

2

1

Page 41: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

41

Goals

Is PHP Aspis effective in preventing injection attacks?

What is the performance overhead?

USENIX WebApps 2011

2

1

Page 42: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

42

Evaluation methodology

USENIX WebApps 2011

WordPress

“Yiannis”

Taint

Plugin

Tracking Code

WordPress Core Non Tracking Code

Use all reported WordPress plugins to the CVE since 1/1/2010

Replay the provided attack vectors where possible

CVE attack vector Tainted?

1

Tainted?

Page 43: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

43

WordPress Plugins Results

USENIX WebApps 2011

1

Total plugins with injection vulnerabilities: 15

Tested plugins: 14

o 1 plugin was not publicly available

Vulnerabilities prevented: 12

o 10 XSS cases

o 2 SQL Injection cases

Vulnerabilities not prevented: 2 (false negatives)

o Stored XSS attacks

o PHP Aspis does not track taint in the database

No observed false positives

Page 44: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

44

PHP Aspis Overhead

USENIX WebApps 2011

2

0

0.2

0.4

0.6

0.8

1

1.2

1.4

DBGen

0

50

100

150

200

250

300

350

400

450

500

PrimeGen

0

50

100

150

200

250

300

350

400

450

WordPress

Off

On

Partial

Page Generation

(ms)

Issues DB queries Generates prime numbers

Blogging platform

Page 45: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

45

Conclusion

Partial taint tracking

o Can be applied by source code transformations

o Is suitable for applications that support plugins

o Is effective for real world plugin vulnerabilities

PHP Aspis design favours false negatives

o False negatives are a consequence of partial taint tracking

o False positives break existing applications

“Reasonable” performance overhead

o Suitable for deployments where security is more important than

performance

o Partial taint tracking for the WordPress case: 2.2x slowdown

USENIX WebApps 2011

Page 46: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

46

The End

Ioannis Papagiannis

DoC, Imperial College London

$git clone git://github.com/jpapayan/aspis.git

[email protected]

USENIX WebApps 2011

Page 47: PHP Aspis: Using Partial Taint Tracking To Protect Against ...Third-party plugin code Code that handles user data year WordPress WordPress Plugins 2009 2 13 2010 2 10 CVE WordPress-Platform

47

References

USENIX WebApps 2011

[1] Venema, W. Runtime taint support proposal. In PHP Internals mailing list

(2006)

[2] Yip, A. Wang X., et all. Improving application security with data flow assertions.

In SOSP 2009

[3] Pietraszek, T and Berghe, C. Defending against injection attacks through

context sensitive string evaluation. In Recent Advances in Intrusion Detection

(2006)