using z-ray for lightning fast security analysis€¦ · using z-ray for lightning fast security...

73
Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Upload: others

Post on 21-Jun-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Using Z-Ray for Lightning Fast Security Analysis

Martin BednorzZendCon Las Vegas 2018

1

Page 2: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Introduction

● 10+ years of web development experience

● IT security background○ Web application security

○ Incremental static code analysis

● CTO / Co-Founder RIPS Technologies○ Static code analysis for security with strong focus on PHP

2

Page 3: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Usual Workflow

3

Page 4: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Usual Workflow

4

Page 5: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Usual Workflow

5

Page 6: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Improved Workflow

6

Page 7: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Improved Workflow

7

Idea: Combine runtime information with static code analysis

Page 8: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Static Code Analysis

8

Page 9: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Transform code into abstract syntax tree (AST)

9

Page 10: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Transform code into abstract syntax tree (AST)

10

$cookie = $_COOKIE['text'];

Page 11: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Transform code into abstract syntax tree (AST)

11

$cookie = $_COOKIE['text'];

$cookie = $_COOKIE['text'];

Page 12: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Transform code into abstract syntax tree (AST)

12

$cookie = $_COOKIE['text'];

$cookie = $_COOKIE['text'];

Assign

$cookie $_COOKIE

'text'

variable array

string

var expr

dim

Page 13: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Split AST into basic blocks

13

Page 14: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Split AST into basic blocks

● Analyze data flow within each basic block

14

Page 15: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Split AST into basic blocks

● Analyze data flow within each basic block

● Summarize data flow in block and function summaries

15

Page 16: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Connect basic blocks to a control flow graph

16

Page 17: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Perform backwards-directed taint analysis for each sensitive sink

17

Page 18: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Simplified Approach

Perform backwards-directed taint analysis for each sensitive sink

18

Page 19: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Context-Sensitive Taint Analysis

1 $id = $_POST['id'];2 if (...) {3 $id = (int)$id;4 } else {5 $id = htmlentities($id);6 }7 echo "<div id='$id'>...";

19

Page 20: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Context-Sensitive Taint Analysis

1 $id = $_POST['id'];2 if (...) {3 $id = (int)$id;4 } else {5 $id = htmlentities($id);6 }7 echo "<div id='$id'>...";

20

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Page 21: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Context-Sensitive Taint Analysis

21

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Variable $id is used in sensitive sink

Markup context:

HTML attribute single-quoted

Page 22: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Context-Sensitive Taint Analysis

22

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Sanitized: integer only

No further actions required

Page 23: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Context-Sensitive Taint Analysis

23

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Sanitizes only: “ < >

Page 24: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Context-Sensitive Taint Analysis

24

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Sanitizes only: “ < >

Vulnerable: All user input allowed

except characters stated above

Page 25: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Results

● WordPress RCE

● Magento RCE

● Joomla! LDAP injection

● Moodle RCE

● wooCommerce PHP Object Injection

● Roundcube RCE

● phpMyAdmin RCE

● …

Visit ripstech.com/vulndb for more

25

Page 26: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Performance

26

Wordpress (333 KLOC) 13m

Magento (2.4 MLOC) 30m

Joomla! (722 KLOC) 11m

Moodle (2.2 MLOC) 39m

Page 27: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Performance

27

Wordpress (333 KLOC) 13m

Magento (2.4 MLOC) 30m

Joomla! (722 KLOC) 11m

Moodle (2.2 MLOC) 39m

Lightning fast compared to other SAST solutions that scan 8h or 1 week.

Page 28: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Incremental Analysis

28

● State-of-the-art: Static analysis of only the code that changed

● Problem: function definition changes○ All call sites need reanalysis

○ If a function is called in a function, it needs reanalysis as well

○ Changed to global variables

○ ....

● Average of only 50% scan time improvement in our experiments

Page 29: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Boost Code Analysis with Z-Ray

29

Page 30: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Z-Ray

● Available with Zend Server

● Runtime (dynamic) analysis

● Deep insights into your PHP application○ Inspect

○ Debug

○ Optimize

● Many plugins and extensions available

30

Page 31: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Z-Ray

31

Page 32: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Execution Times

32

Page 33: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Database Query Information

33

Page 34: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Application-Specific Information

34

Page 35: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Stacktrace

35

Page 36: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Stacktrace - Used Files

36

// index.phpinclude('functions.php');switch($_GET['page']) {

case 'a': include('page_a.php');

case 'b': include('page_b.php');}

// page_a.phpdo_something();

// page_b.phpinclude('export.php');do_something_export();

Page 37: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Stacktrace - Used Files

37

Page 38: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Stacktrace - Used Files

38

index.php?page=a

Page 39: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Stacktrace - Used Files

39

index.php?page=b

Page 40: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Stacktrace - Used Files

40

Page 41: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information

41

Page 42: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

42

Page 43: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

43

Page 44: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

44

Page 45: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

45

Page 46: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

46

Page 47: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

47

Page 48: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Performance

48

admin();

user();

guest();

Page 49: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Example

1 $id = $_POST['id'];2 if (...) {3 $id = (int)$id;4 } else {5 $id = htmlentities($id);6 }7 echo "<div id='$id'>...";

49

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Page 50: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Example

1 $id = $_POST['id'];2 if (...) {3 $id = (int)$id;4 } else {5 $id = htmlentities($id);6 }7 echo "<div id='$id'>...";

50

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Page 51: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Example

1 $id = $_POST['id'];2 if (...) {3 $id = (int)$id;4 } else {5 $id = htmlentities($id);6 }7 echo "<div id='$id'>...";

51

$id = $_POST['id'];

$id = (int)$id; $id = htmlentities($id);

echo "<div id='$id'>...";

Page 52: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Example

1 $id = $_POST['id'];2 if (...) {3 $id = (int)$id;4 } else {5 $id = htmlentities($id);6 }7 echo "<div id='$id'>...";

52

$id = $_POST['id'];

$id = (int)$id;

echo "<div id='$id'>...";

Page 53: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Pitfall

// ...if (!isset($_SESSION['id'])) {

$_SESSION['id'] = select_id();}select_from_db($_SESSION['id']);

53

Page 54: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Pitfall

// ...if (!isset($_SESSION['id'])) {

$_SESSION['id'] = select_id();}select_from_db($_SESSION['id']);

54

// ...

$_SESSION['id'] = select_id();

select_from_db($_SESSION['id']);

Page 55: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Pitfall

// ...if (!isset($_SESSION['id'])) {

$_SESSION['id'] = select_id();}select_from_db($_SESSION['id']);

55

// ...

$_SESSION['id'] = select_id();

select_from_db($_SESSION['id']);

Page 56: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Pitfall

// ...if (!isset($_SESSION['id'])) {

$_SESSION['id'] = select_id();}select_from_db($_SESSION['id']);

56

// ...

$_SESSION['id'] = select_id();

select_from_db($_SESSION['id']);

Page 57: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Verification

57

Page 58: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Verification

58

http://mysite.com/search?category=book

Page 59: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Verification

59

http://mysite.com/search?category=book&t=

Page 60: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Verification

60

http://mysite.com/search?category=book&t=<script>alert(1);</script>

Page 61: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Request Information - Verification

61

http://mysite.com/search?category=book&t=’ onclick=’alert(1);’

Page 62: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype

● Integrate into already available Zend Server plugin○ Zend Server UI plugin

○ Scan deployed applications or virtual hosts

○ Full scans only

● Zend Server Z-Ray plugin○ Scan single requests

○ Implement the most significant performance optimizations

62

Page 63: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype

63

Zend Server Plugin

UI

Page 64: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype

64

Zend Server Plugin

UI Z-RayZ-Ray API

● Add Z-Ray component to our plugin○ Access data via the Z-Ray API

○ Run first batch of optimizations

Page 65: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype

65

Zend Server Plugin

UI Z-RayZ-Ray API

Static Code Analysis

● Add Z-Ray component to our plugin○ Access data via the Z-Ray API

○ Run first batch of optimizations

● Send relevant source code to static code analysis tool

Page 66: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype

66

Zend Server Plugin

UI Z-RayZ-Ray API

Static Code Analysis

Z-Ray

● Add Z-Ray component to our plugin○ Access data via the Z-Ray API

○ Run first batch of optimizations

● Send relevant source code to static code analysis tool

● Extend taint analysis with data provided by Z-Ray

Page 67: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype Implementation

67

Page 68: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype Implementation

● Full scan○ ~2,4M Lines of Code

○ ~30 Minutes scan time

● QuickScan○ ~70k Lines of Code

○ ~1 Minutes scan time

● Can still be greatly improved

68

Page 69: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype Implementation

● Full scan○ ~2,4M Lines of Code

○ ~30 Minutes scan time

● QuickScan○ ~70k Lines of Code

○ ~1 Minutes scan time

● Can still be greatly improved

69

Page 70: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Prototype Implementation

● Full scan○ ~2,4M Lines of Code

○ ~30 Minutes scan time

● QuickScan○ ~70k Lines of Code

○ ~1 Minutes scan time

● Can still be greatly improved

70

Page 71: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Demo

71

Page 72: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Conclusion

● Lightning fast security analysis for single requests

● Verify patches or single components much quicker○ Allows for a workflow similar to tests

● Still some work required○ Improve taint analysis with runtime information

○ Fix some of the pitfalls

72

Page 73: Using Z-Ray for Lightning Fast Security Analysis€¦ · Using Z-Ray for Lightning Fast Security Analysis Martin Bednorz ZendCon Las Vegas 2018 1

Thank you!

Any questions?

73