34
An Empirical Study of PHP Security Mechanism Usage Experience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and Thorsten Holz Ruhr-University Bochum, Germany ISSTA 2015, July 13-17, Baltimore, Maryland, USA

Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

Embed Size (px)

Citation preview

Page 1: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

An Empirical Study of PHP Security Mechanism UsageExperience Report:

Experience Report: An Empirical Study of PHP Security Mechanism Usage

Johannes Dahse and Thorsten HolzRuhr-University Bochum, Germany

ISSTA 2015, July 13-17, Baltimore, Maryland, USA

Page 2: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

2

An Empirical Study of PHP Security Mechanism UsageExperience Report: 1. Introduction

2. Security Mechanisms 3. Static Enumeration4. Empirical Study

Page 3: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

3

An Empirical Study of PHP Security Mechanism UsageExperience Report:

1.1 Web Application State

● 82% of all websites run PHP as server-side language● Weakly-typed language; requires more checks, introduces oddities● 25% of all found vulnerabilities (CVE database) are related to PHP● XSS and SQLi still one of the most commonly found security flaws

00 01 02 03 04 05 06 07 08 09 10 11 12 13 140

1000

2000

3000

4000

50006000

7000

8000

Other

PHP

JS

Python

Perl

RubyCFM

Java

ASP

PHP

0 10 20 30 40 50 60 70 80 90

w3techs.com coelho.net/php_cve.html

1. Introduction2. Security Mechanisms 3. Static Enumeration4. Empirical Study

Page 4: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

4

An Empirical Study of PHP Security Mechanism UsageExperience Report:

1.2 Security Mechanisms

● Best-practice security guidelines evolved, but no standards● Developers apply their own favorite security mechanism● Different programming patterns for input validation or

input sanitization emerged, with advantages and drawbacks● Some work generically, others work only for a certain markup context● Programming mistakes and misplacing due to common pitfalls

can still lead to vulnerabilities

1. Introduction2. Security Mechanisms 3. Static Enumeration4. Empirical Study

Page 5: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

5

An Empirical Study of PHP Security Mechanism UsageExperience Report:

1.3 Research Questions

RQ1. Which security mechanisms are available?

RQ2. Which pitfalls might these mechanisms imply?

RQ3. Which security mechanisms are used how often in modern (web)

applications?

RQ4. Which security mechanism is used to prevent which vulnerability

type in which markup context?

RQ5. Which pitfalls occur in practice?

Essential for developers, code auditors, and static analysis engineers

1. Introduction2. Security Mechanisms 3. Static Enumeration4. Empirical Study

Page 6: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

6

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2. Security Mechanisms

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 7: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

7

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.1 Taint-style Vulnerabilities

source

sensitive sink

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 8: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

8

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.1 Types of Security Mechanisms

sourcesource

sensitive sink

sanitization

sensitive sink

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 9: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

9

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.1 Types of Security Mechanisms

sourcesource

sensitive sink

source

sensitive sink

validationsanitization

sensitive sink

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 10: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

10

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.1 Types of Security Mechanisms

sourcesource

sensitive sink

source

sensitive sink

validationsanitization

sensitive sink

source

sensitive sink

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 11: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

11

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.2 Generic Input Sanitization

1 $id = $_GET['id']; 2 echo intval($id); 3 mysql_query('SELECT * FROM t WHERE id='.(int)$id);

● Explicit Type Casting

source

sensitive sink

sanitization

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 12: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

12

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.3 Context-Sensitive Input Sanitization

1 $url = htmlentities($_GET['id']); 2 echo '<a href=““>' . $url . '</a>'; 3 echo “<a href='$url'>click</a>“; 4 echo '<a href=“' . $url . '“>click</a>';

● Converting

● Escaping 5 $id = mysql_real_escape_string($_GET['id']); 6 mysql_query(“SELECT * FROM t WHERE id = '$id'“); 7 mysql_query('SELECT * FROM t WHERE id = ' . $id);

source

sensitive sink

sanitization

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 13: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

13

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.3 Context-Sensitive Input Sanitization

1 $url = htmlentities($_GET['id']); “ → &quot; 2 echo '<a href=““>' . $url . '</a>'; < → &lt; 3 echo “<a href='$url'>click</a>“; 4 echo '<a href=“' . $url . '“>click</a>';

● Converting

● Escaping 5 $id = mysql_real_escape_string($_GET['id']); 6 mysql_query(“SELECT * FROM t WHERE id = '$id'“); 7 mysql_query('SELECT * FROM t WHERE id = ' . $id);

source

sensitive sink

sanitization

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 14: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

14

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.3 Context-Sensitive Input Sanitization

1 $url = htmlentities($_GET['id']); “ → &quot; 2 echo '<a href=““>' . $url . '</a>'; < → &lt; 3 echo “<a href='$url'>click</a>“; 'onclick='alert(1) 4 echo '<a href=“' . $url . '“>click</a>';

● Converting

● Escaping

source

sensitive sink

sanitization

5 $id = mysql_real_escape_string($_GET['id']); 6 mysql_query(“SELECT * FROM t WHERE id = '$id'“); 7 mysql_query('SELECT * FROM t WHERE id = ' . $id);

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 15: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

15

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.3 Context-Sensitive Input Sanitization

1 $url = htmlentities($_GET['id']); “ → &quot; 2 echo '<a href=““>' . $url . '</a>'; < → &lt; 3 echo “<a href='$url'>click</a>“; 'onclick='alert(1) 4 echo '<a href=“' . $url . '“>click</a>';

● Converting

● Escaping

source

sensitive sink

sanitization

5 $id = mysql_real_escape_string($_GET['id']); 6 mysql_query(“SELECT * FROM t WHERE id = '$id'“); 7 mysql_query('SELECT * FROM t WHERE id = ' . $id);

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

javascript:alert(1)

Page 16: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

16

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.3 Context-Sensitive Input Sanitization

1 $url = htmlentities($_GET['id']); 2 echo '<a href=““>' . $url . '</a>'; 3 echo “<a href='$url'>click</a>“; 4 echo '<a href=“' . $url . '“>click</a>';

● Converting

● Escaping 5 $id = mysql_real_escape_string($_GET['id']); ' → \' 6 mysql_query(“SELECT * FROM t WHERE id = '$id'“); 7 mysql_query('SELECT * FROM t WHERE id = ' . $id);

source

sensitive sink

sanitization

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 17: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

17

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.3 Context-Sensitive Input Sanitization

1 $url = htmlentities($_GET['id']); 2 echo '<a href=““>' . $url . '</a>'; 3 echo “<a href='$url'>click</a>“; 4 echo '<a href=“' . $url . '“>click</a>';

● Converting

● Escaping

source

sensitive sink

sanitization

5 $id = mysql_real_escape_string($_GET['id']); ' → \' 6 mysql_query(“SELECT * FROM t WHERE id = '$id'“); 7 mysql_query('SELECT * FROM t WHERE id = ' . $id);

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

1 or 1=1

Page 18: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

18

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.4 Generic Input Validation

1 $id = $_GET['id']; 2 if(is_numeric($id)) { echo $id; } 3 if(is_int($id) === true) { echo $id; } 4 if((int)$id) { echo $id; } 5 if($id = (int)$id) { echo $id; }

● Type Validation

● Comparing 6 $name = $_GET['name']; 7 if($name == 'issta') { echo $name; } 8 if($name === 'issta') { echo $name; } 9 if($name == 15) { echo $name; }10 if($name === 15) { echo $name; }

source

sensitive sink

validation

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 19: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

19

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.4 Generic Input Validation

1 $id = $_GET['id']; 2 if(is_numeric($id)) { echo $id; } 3 if(is_int($id) === true) { echo $id; } 4 if((int)$id) { echo $id; } 5 if($id = (int)$id) { echo $id; }

● Type Validation

● Comparing 6 $name = $_GET['name']; 7 if($name == 'issta') { echo $name; } 8 if($name === 'issta') { echo $name; } 9 if($name == 15) { echo $name; }10 if($name === 15) { echo $name; }

source

sensitive sink

validation

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

1<svg onload=alert(1)>

Page 20: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

20

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.4 Generic Input Validation

1 $id = $_GET['id']; 2 if(is_numeric($id)) { echo $id; } 3 if(is_int($id) === true) { echo $id; } 4 if((int)$id) { echo $id; } 5 if($id = (int)$id) { echo $id; }

● Type Validation

● Comparing 6 $name = $_GET['name']; 7 if($name == 'issta') { echo $name; } 8 if($name === 'issta') { echo $name; } 9 if($name == 15) { echo $name; }10 if($name === 15) { echo $name; }

source

sensitive sink

validation

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 21: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

21

An Empirical Study of PHP Security Mechanism UsageExperience Report:

2.4 Generic Input Validation

1 $id = $_GET['id']; 2 if(is_numeric($id)) { echo $id; } 3 if(is_int($id) === true) { echo $id; } 4 if((int)$id) { echo $id; } 5 if($id = (int)$id) { echo $id; }

● Type Validation

● Comparing 6 $name = $_GET['name']; 7 if($name == 'issta') { echo $name; } 8 if($name === 'issta') { echo $name; } 9 if($name == 15) { echo $name; }10 if($name === 15) { echo $name; }

source

sensitive sink

validation

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

15<svg onload=alert(1)>

Page 22: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

22

An Empirical Study of PHP Security Mechanism UsageExperience Report:

3. Static Enumeration

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 23: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

23

An Empirical Study of PHP Security Mechanism UsageExperience Report:

3.1 Approach

● Erroneous approach: Count occurrences of security related features● Over-approximation when features are used for other purposes

● Under-approximation when features are used in reusable code

● Our approach: Use modified version of our static analysis prototype ● Leverages backwards-directed context-sensitive taint analysis● Count security mechanism only when data reaches a sensitive sink

that was previously sanitized/validated and was previously tainted

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 24: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

24

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4. Empirical Study

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 25: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

25

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.1 Selected Applications

● 25 PHP applications● Open source, active and popular according to W3Tech's usage statistic

● Size of at least 20 KLOC

● Works standalone, does not extensively use reflection or framework

● 2.5 million lines of code (LOC) in total● 26,006 unique data flows analyzed

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 26: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

26

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.2 LOC and Markup Contexts

3 21 16 20 5 24 2 25 1 17 23 13 7 8 14 18 22 15 10 19 9 11 4 6 120

50000

100000

150000

200000

250000

300000

350000

LOC

3 21 16 20 5 24 2 25 1 17 23 13 7 8 14 18 22 15 10 19 9 11 4 6 120

500

1000

1500

2000

2500

3000

3500

HTMLSQLJS

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 27: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

27

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.3 Markup Contexts and Security Mechanisms

64%

31%5%

HTMLSQLJavaScript

53% 47%

SanitizationValidation

16%

19%65%

Explicit TypecastType ValidationOther

Markup Contexts Mechanism Types Top Mechanisms

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 28: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

28

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.3.1 HTML Markup Security

DQ Element SQ0

1000

2000

3000

4000

5000

6000

7000

8000

9000

10000 Replace

Regex Validate

Explicit Typecast

Comparing

Type Validation

Converting

Other

Element DQ SQ0

20

40

60

80

100

120

140

160

180 Converting

Comparing

Regex Replace

Escaping

Regex Validate

Replace

Other

52% 41%

5%

Mechanisms correctly applied Mechanisms wrongly applied

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 29: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

29

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.3.2 JavaScript Markup Security

Script Event0

200

400

600

800

1000

1200 Regex Replace

Null Validation

Replace

Comparing

Explicit Typecast

Type Validation

Other

Script Event0

10

20

30

40

50

60Truncate

Regex Replace

Regex Validate

Comparing

Replace

Converting

94%

6%

Mechanisms correctly applied Mechanisms wrongly applied

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 30: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

30

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.3.3 SQL Markup Security

SQ NQ DQ0

1000

2000

3000

4000

5000

6000 Prepare

Replace

Regex Validate

Comparing

Type Validation

Escape

Explicit Typecast

Other

NQ SQ DQ0

20

40

60

80

100

120

140

160

180

200 Regex Replace

Prepare

Truncate

Comparing

Replace

Regex Validate

Escaping

Other

67%31%

2%

Mechanisms correctly applied Mechanisms wrongly applied

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 31: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

31

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.4 Lessons LearnedPitfall density (bars) versus markup frequency (line)

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 32: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

32

An Empirical Study of PHP Security Mechanism UsageExperience Report:

4.5 Threads to Validity

● Only 25 popular applications

● Static code analysis is limited (FP, FN, mistakes)

● Misinterpretation of developer intention

● Caution to draw strong conclusions and to generalize

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 33: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

33

An Empirical Study of PHP Security Mechanism UsageExperience Report:

[email protected]

1. Introduction2. Security Mechanisms3. Static Enumeration4. Empirical Study

Page 34: Experience Report - ISSTAissta2015.cs.uoregon.edu/slides/dahse-issta15.pdfExperience Report: Experience Report: An Empirical Study of PHP Security Mechanism Usage Johannes Dahse and

34

An Empirical Study of PHP Security Mechanism UsageExperience Report:

Thank you! Enjoy the conference.