JSON Injection (2024)

JSON Injection (1)

What is JSON injection?

JSON injection is a vulnerability that lets a malicious hacker inject malicious data into JSON streams or use malicious JSON streams to modify application behavior. There are two types of JSON injections, server-side and client-side:

  • Server-side JSON injection happens when data from an untrusted source is not sanitized by the server and is written directly to a JSON stream.
  • Client-side JSON injection happens when data from an untrusted JSON source is not sanitized and is parsed directly using the JavaScript eval function.
Severity:JSON Injection (2) JSON Injection (3) medium severity
Prevalence:JSON Injection (4) JSON Injection (5) discovered rarely
Scope:JSON Injection (6) JSON Injection (7) may appear in all apps that use JSON
Technical impact:privilege escalation, cross-site scripting
Worst-case consequences:full system compromise
Quick fix:sanitize user input, do not use the JS eval function

What is JSON?

JSON (JavaScript Object Notation) is a lightweight data interchange format used for communication between applications. It performs a similar role to XML but is simpler and better suited to processing in JavaScript.

Many web applications use this format to communicate and serialize/deserialize data. Some web applications also use JSON to store important information, such as user data. JSON is commonly used in RESTful APIs and AJAX applications.

What is JSON hijacking?

While JSON hijacking (a subset of cross-site script inclusion – XSSI) also involves the JSON format, it is a slightly different attack, in some ways similar to cross-site request forgery (CSRF). Attackers can use JSON hijacking to intercept JSON data sent from a web server to a web application. A typical JSON hijacking attack might look like this:

  1. The attacker creates a malicious website containing a script tag that references a JSON data URL of the web application under attack and includes code to hijack the JSON data.
  2. A user logged into the targeted web application is tricked into visiting the malicious website (usually using social engineering).
  3. Since the same-origin policy (SOP) allows JavaScript from any website to be included and executed in the context of any other site, the user’s web browser loads the JSON data in the context of the malicious site.
  4. The malicious website hijacks the JSON data.

Example of a server-side JSON injection attack

A simple server-side JSON injection could be performed in PHP as follows:

  1. The server stores user data as a JSON string, including the account type.
  2. User name and password values are taken directly from user input parameters without validation or sanitization.
  3. The JSON string is built using simple concatenation:
$json_string = '{"accountType":"user","userName":"'.$_GET['userName'].'","pass":"'.$_GET['pass'].'"}';
  1. A malicious user appends data to their user name entered into an input form or delivered in an HTTP header. This data is sent to the back-end unsanitized:
john%22,%22accountType%22:%22administrator%22
  1. The resulting JSON string stored by the application back-end is:
{ "accountType":"user", "userName":"john", "accountType":"administrator", "pass":"password"}
  1. When reading the stored string, the JSON parser (json_decode) encounters two accountType entries and accepts the last one, granting john administrator privileges without any authentication. Note that, strictly speaking, the behavior of json_decode is not incorrect – RFC-7159 for the JSON format states that “the names within an object SHOULD be unique” but not that they must be unique, leaving some room for interpretation.

Example of a client-side JSON injection attack

A simple client-side JSON injection could be performed as follows:

  1. The initial JSON string is the same as in the previous example.
  2. The server gets the JSON data, including a malicious payload, from an untrusted source and does not sanitize it.
  3. The client parses the JSON string using eval:
var result = eval("(" + json_string + ")");document.getElementById("#accountType").innerText = result.account;document.getElementById("#userName").innerText = result.name; document.getElementById("#pass").innerText = result.pass;
  1. The accountType value injected by the attacker is:
user"});alert(document.cookie);({"accountType":"user
  1. The eval function executes the alert call.
  2. Parsing the malicious string results in a cross-site scripting (XSS) attack (document.cookie is disclosed).

Potential consequences of a JSON injection attack

The consequences of JSON injection highly depend on the way JSON data is used by the web application. However, in some cases, they may be quite severe:

  • If JSON is used to store authentication data and the app is susceptible to server-side JSON injection, the attacker may gain access to an administrative account in the application. Depending on the privileges of that administrative account, they could then obtain highly sensitive data or perform malicious actions.
  • If a web application is vulnerable to client-side JSON injection, it may be used for attacks that involve reflected XSS, for example, in phishing or spam campaigns.

While JSON injection on its own may not seem very dangerous, it is often only one step in a longer chain of attacks, so in some cases it can have severe consequences, up to and including full system compromise.

How to detect JSON injection vulnerabilities?

The best way to detect JSON injection vulnerabilities varies depending on whether they are already known or unknown.

  • If you only use commercial or open-source software and do not develop software of your own, it may be enough to identify the exact version of the system or application you are using. If the identified version is susceptible to JSON injection, you can assume that your software is vulnerable. You can identify the version manually or use a suitable security tool, such as a software composition analysis (SCA) solution for web applications or a network scanner for networked systems and applications.
  • If you develop your own software or want the ability to potentially find previously unknown JSON injection vulnerabilities (zero-days) in known applications, you must be able to successfully exploit the JSON injection vulnerability to be certain that it exists. This requires either performing manual pentesting with the help of security researchers or using a vulnerability scanner tool that can use automation to exploit web vulnerabilities. Examples of such tools are Invicti and Acunetix by Invicti. We recommend using this method even for known vulnerabilities.

How to prevent JSON injection vulnerabilities in web applications?

As with most vulnerabilities, the key to maintaining web application security and preventing JSON injections is to sanitize data. This applies to both server-side and client-side JSON injections.

To prevent server-side JSON injections, sanitize all data before serializing it to JSON. For example, if you use Java, a good option to sanitize JSON data is to use the OWASP JSON Sanitizer available on GitHub. An even better practice is never to write JSON data manually but always using framework functions that perform sanitization.

The best way of preventing client-side JSON injections is never to use the eval function to evaluate JSON data. Whenever you use the eval function with untrusted data that contains JavaScript code, that code will be executed – and it could be malicious. To eliminate this risk, use JSON.parse instead.

How to mitigate JSON injection attacks?

  • The only way to prevent the severe consequences of server-side JSON injections, apart from scanning for them early in the development cycle, is to design applications so they do not load sensitive information, such as account information and privileges, from user-controlled or otherwise untrusted JSON data.
  • You can completely eliminate the risk of client-side JSON injections by enforcing Content Security Policy, which by default prevents the use of eval. This forces the developers to use the safer JSON.parse method instead.

Frequently asked questions

What are JSON injection attacks?

JSON injection attacks happen when unsanitized JSON data containing a malicious payload is accepted and parsed by a web application or browser. Server-side JSON injection attacks are possible if input data is not sanitized by the server and is written directly to a JSON stream. Client-side JSON injection attacks are possible if incoming JSON data is not sanitized and is parsed directly using the JavaScript eval function.

Learn more about the dangers of missing input validation and sanitization.

What is JSON hijacking?

JSON hijacking happens when a malicious web page causes the victim’s web browser to retrieve JSON data from a targeted web application. The web browser then passes the JSON data to a web server controlled by the attacker.

Read about cross-site request forgery (CSRF) attacks.

How dangerous are JSON injections?

JSON injections are not very common and not as dangerous as SQL injection or other severe vulnerabilities, but they can still be used in attack chains that lead to other, more dangerous attacks, such as cross-site scripting (XSS).

Read more about cross-site scripting (XSS).

ClassificationID
CAPEC153
CWE74, 116
WASC20
OWASP 2021A3

Written by: Tomasz Andrzej Nidecki, reviewed by: Benjamin Daniel Mussler

JSON Injection (2024)

FAQs

What is a JSON injection attack? ›

JSON injection is a vulnerability that lets a malicious hacker inject malicious data into JSON streams or use malicious JSON streams to modify application behavior.

What are the risks of JSON injection? ›

JSON Injection

This can happen if user inputs are not properly sanitized before being included in JSON structures. Example: An attacker might craft JSON data that includes malicious code, which can lead to unexpected behavior or security issues if the data is processed without validation.

What is JSON hijacking? ›

JSON Hijacking is a sophisticated cyber attack that targets web applications by exploiting vulnerabilities in the way JSON (JavaScript Object Notation) data is handled.

What does JSON sanitizer do? ›

The sanitizer takes JSON like content, and interprets it as JS eval would. Specifically, it deals with these non-standard constructs. Single quoted strings are converted to JSON strings. Hex escapes are converted to JSON unicode escapes.

Can a JSON be malicious? ›

Attackers can insert malicious scripts or commands in JSON strings, which, if inadequately filtered, lead to unauthorised data exposure or system compromise. A study by Synk.io pointed out dependencies in JSON could be exploited to launch prototype pollution attacks in Node.

Is XSS possible in JSON? ›

JSON based cross-site scripting (XSS) protection enables you to configure relaxations that allow specific patterns and block the rest. For example, the Web App Firewall currently has a default set of more than 100 SQL keywords.

Do hackers use JSON? ›

JSON injection is a type of cyber-attack where an attacker injects malicious code into JSON data streams to alter the normal execution of web applications. This can result in data loss, data modification, or denial of service, making it a significant security risk.

Why would someone use JSON? ›

JSON is popular with developers because it's a flexible format for data exchange that enjoys wide support in modern programming languages and software systems. It's text based and lightweight and has an easy-to-parse data format, meaning it requires no additional code to understand and interpret the data provided.

What are the pros and cons of JSON? ›

In conclusion, JSON is a lightweight and easy-to-use data interchange format that has become widely used for data exchange between systems. It has several advantages such as platform independence and self-description, but also some limitations such as limited data types and no built-in schema validation.

Is JSON hijacking still an issue? ›

Note: Older Browsers were more vulnerable to JSON Hijacking. As of now, this vulnerability has been fixed in modern Browsers.

How do I get rid of JSON? ›

Delete property to Remove a JSON Attribute

After deletion, the property is not available for use before it is added back again. This operator is created to be used on object properties, not on variables or functions. This operator should not be used on predefined JavaScript object properties.

What is JSON on my computer? ›

The JSON data format is an open standard file (. json) and data format used for data interchange through various forms of technology. The most common use of JSON data and files is to read data from a server for a website or web application to display — and change data given the correct permissions.

Do I need to sanitize JSON? ›

To prevent server-side JSON injections, sanitize all data before serializing it to JSON. For example, if you use Java, a good option to sanitize JSON data is to use the OWASP JSON Sanitizer. The best method to prevent client-side JSON injections is never to use the eval function to evaluate JSON data.

What is client-side JSON injection? ›

Description: Client-side JSON injection (DOM-based)

DOM-based JSON injection arises when a script incorporates controllable data into a string that is parsed as a JSON data structure and then processed by the application.

Are JSON files safe? ›

JSON alone is not much of a threat. After all, it's only a data-interchange format. The real security concerns with JSON arise in the way that it is used. If misused, JSON-based applications can become vulnerable to attacks such as JSON hijacking and JSON injection.

What is an injection attack? ›

Injection attacks insert malicious code into a program, leading to unauthorized command execution or data access. Though injection attacks come in various forms, key types include: SQL injection: Exploits database vulnerabilities through malicious SQL statements, affecting data integrity and availability.

What is an XSS injection attack? ›

Cross-site scripting (XSS) is an attack in which an attacker injects malicious executable scripts into the code of a trusted application or website. Attackers often initiate an XSS attack by sending a malicious link to a user and enticing the user to click it.

Top Articles
How to Change Your DNS Server on Windows 10 and Mac : HelloTech How
Here’s what happens to your investments when you’re away for the holidays
Housing near Juneau, WI - craigslist
The Definitive Great Buildings Guide - Forge Of Empires Tips
Gunshots, panic and then fury - BBC correspondent's account of Trump shooting
Lowes 385
Pbr Wisconsin Baseball
Sinai Web Scheduler
Best Private Elementary Schools In Virginia
Carter Joseph Hopf
Nestle Paystub
Red Heeler Dog Breed Info, Pictures, Facts, Puppy Price & FAQs
2135 Royalton Road Columbia Station Oh 44028
Cyndaquil Gen 4 Learnset
Nesz_R Tanjiro
Sizewise Stat Login
Webcentral Cuny
Acts 16 Nkjv
Lola Bunny R34 Gif
Bekijk ons gevarieerde aanbod occasions in Oss.
Dallas Mavericks 110-120 Golden State Warriors: Thompson leads Warriors to Finals, summary score, stats, highlights | Game 5 Western Conference Finals
College Basketball Picks: NCAAB Picks Against The Spread | Pickswise
Yisd Home Access Center
Doki The Banker
THE FINALS Best Settings and Options Guide
Yonkers Results For Tonight
Jobs Hiring Near Me Part Time For 15 Year Olds
A Cup of Cozy – Podcast
Valic Eremit
Ihub Fnma Message Board
Wsbtv Fish And Game Report
Yale College Confidential 2027
Section 408 Allegiant Stadium
Downtown Dispensary Promo Code
Cinema | Düsseldorfer Filmkunstkinos
TJ Maxx‘s Top 12 Competitors: An Expert Analysis - Marketing Scoop
Vadoc Gtlvisitme App
Plasma Donation Racine Wi
Bi State Schedule
Www.craigslist.com Syracuse Ny
Navigating change - the workplace of tomorrow - key takeaways
Devotion Showtimes Near Mjr Universal Grand Cinema 16
Empire Visionworks The Crossings Clifton Park Photos
19 Best Seafood Restaurants in San Antonio - The Texas Tasty
B.C. lightkeepers' jobs in jeopardy as coast guard plans to automate 2 stations
Dwc Qme Database
Flappy Bird Cool Math Games
R/Gnv
Huntsville Body Rubs
Westport gun shops close after confusion over governor's 'essential' business list
Diccionario De Los Sueños Misabueso
Latest Posts
Article information

Author: Kelle Weber

Last Updated:

Views: 6154

Rating: 4.2 / 5 (53 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Kelle Weber

Birthday: 2000-08-05

Address: 6796 Juan Square, Markfort, MN 58988

Phone: +8215934114615

Job: Hospitality Director

Hobby: tabletop games, Foreign language learning, Leather crafting, Horseback riding, Swimming, Knapping, Handball

Introduction: My name is Kelle Weber, I am a magnificent, enchanting, fair, joyous, light, determined, joyous person who loves writing and wants to share my knowledge and understanding with you.