MongoDB_and_NoSQL_injection_and_prevention
MongoDB_and_NoSQL_injection_and_prevention
net/publication/378463041
CITATIONS READS
0 844
1 author:
Omar Abuali
An-Najah National University
2 PUBLICATIONS 0 CITATIONS
SEE PROFILE
All content following this page was uploaded by Omar Abuali on 24 February 2024.
Abstract—With growing the use of NoSQL databases, like And in the another hand there is different ways used or
MongoDB, and knowing the offered Features like handling large suggested for prevent the NoSQL injections, in this paper we
amounts of data, the risk of NoSQL injection becomes a critical discussed 5 of them:
concern. This paper explores the problems and vulnerabilities
that might face NoSQL databases. The study presents four ‚ Web application firewall (WAF).
types of NoSQL injection attacks: Error-based, Union Query, ‚ User-side validation.
JavaScript injection, and Tautologies, also the paper represents ‚ parameterized queries.
a practical example of a Tautologies attack on the OQSAP ‚ Express Mongo Sanitize.
Juice Shop, showing how the manipulation of product reviews
‚ least-privilege model.
is done.To prevent these threats, the paper mentioned five
prevention methods: Web Application Firewall (WAF), User-side ‚ Server-side validation.
validation, Parameterized queries, Express Mongo Sanitize, and The rest of the paper is organized as follows: the paper starts
the Least-privilege model. Each method is discussed in detail for with a literature review in section 2, then the attacks will be
its effectiveness and the drawbacks they could have, and how listed and explained in section 3. Section 4 presents the pre-
security measures can affect the SW performance, moreover, the
paper introduces Server-side validation as a strong secondary vention methods being implemented and tested in some details
defense against NoSQL injection attacks. The findings show the and illustrates the implementation results. Section 5 presents
need for a multi-layered security approach, this would mix many a how security measures can affect the SW performance, then
preventive steps to ensure a good defense against NoSQL injection results in Section 6, The paper is then ends with the conclusion
attacks. The real-life example highlights the successful use of section.
server-side validation to stop the tautologies attack. This gives a
useful example of good safety measures. II. LITERATURE REVIEW
Index Terms—NoSQL Injection, MongoDB, Database security,
Open Web Application Security Project (OWASP), NoSQL In-
A. MongoDB
jection prevention methods MongoDB is an open-source database and one of many
NoSQL database system types identified as document- ori-
I. I NTRODUCTION ented, MongoDB stores data in JSON format style, there are
NoSQL, short for ”non-SQL or non-relational”, refers to a many features of MongoDB, and the most important ones
category of database management systems that differ from tra- are flexibility, scalability, and high-performance data dealing.
ditional relational database systems in significant ways. These MongoDB is considered scheme-less which means that there
systems are designed to handle large volumes of data and are are no relations between the collections that store the data
known for their flexibility, scalability, and high performance in inside. NoSQL databases are also used more in IoT because
the current era of rapid technological advancement, there is an provide an easy way to store this unstructured data. The
increasing need for databases capable of managing the huge collection in MongoDB is a set of documents similar to the
data that companies deal with, so that is what NoSQL DBs normal table in the relational database. MongoDB uses port
offer, organizations also using NoSQL databases like Amazon 27017 by default (Gupta, S. Singh, N. Tomar, D. 2018). And
DynamoDB, Cassandra, Manhattan, MongoDB. “According here is an example of an insert statement in MongoDB and
to OWASP injection has the third position of the top 10 a clear why injection with JSON structured query is more
list in the most critical security risks to web applications.” difficult than SQL:
The NoSQL injection attack works when the web application
db.books.insert({title:‘NoSQL injection’
accepts input so the attacker can send a malicious query or
,author: ‘O. omar’})
command to the database which could give him access to the
database then manipulate the data or drop tables also withdraw B. NoSQL injection in MongoDB
sensitive data from the database, in this way can break the NoSQL stores data in a complex way such as MongoDB
integrity and confidentiality which is from the 3 principles of which stores data in JSON format type, due to that, the
information security, and with NoSQL database there is multi attacker finds it harder to apply NoSQL injection compared
types of attacks can performed, so in this paper we choose to to the normal SQL injection which is known to most of the
talk about four types of attacks and one has been implemented: users. anyway, it is not 100 percent safe as every system
‚ Error-based can be compromised and hacked, however, systems with more
‚ Union Query. complex and protected security are harder to hack than normal
‚ JavaScript injection ones. There are many NoSQL injection attacks, and we will
‚ Tautologies discuss them in the next Section.
III. N O SQL INJECTION ATTACKS ‚ Requires careful configuration to be effective.
1) Error-based: In this injection, the attacker uses special ‚ Complex and Costly.
NoSQL characters such as ‘"\/$[].> ,then the attacker will ‚ False Positives
send them to the server and waits to check if the server returns 2) User-side validation: User validation refers to the pro-
an error, and that’s how the attacker can know what version cess when user input is checked by the browser without the
and database being used in the website back-end based on the need to send it to the server which gives the user immediate
errors that return from the servers. feedback without the need to refresh the page this is considered
2) Union Query: This attack is more commonly known an advantage, but the reason why we can’t rely on this type
among users as a type of SQL injection attack, but it exists of prevention because:
here, it works when the attacker uses a vulnerable parameter ‚ It can bypass by altering the JavaScript code loaded in
by exploiting it in order to change the returned dataset. There your browser.
are many uses of union queries, however, the most used ones ‚ Using tools like curl.
are authentication bypassing pages as well as data extraction, 3) parameterized queries: parameterized queries are a pow-
still, when it comes to JSON query structure, it is difficult to erful tool for preventing injection attacks but there a drawback
implement in NoSQL databases. to this type of prevention:
3) JavaScript injection: NoSQL databases introduced vul-
‚ Lack of Flexibility.
nerabilities that can allow JavaScript execution in the database,
‚ Extra Development Time.
resulting in compromising illegal data. The process of the
‚ Memory overhead.
injection is applied by passing unsensitized user input to
the queries for injection, this is due to enabling compli- The way how it works is by defining a NoSQL query structure
cated transactions by JavaScript. There are some risks to first and then passing parameters from the user later. In this
NoSQL features that the database presents, which are running way, the developer ensures that an attacker cannot change the
JavaScript execution in the database engines that may cause intent of a query like inserting a NoSQL command into the
a surface attack that is considered dangerous if the user input query parameters. This way makes it very hard for the attacker
field is unsensitized. to inject malicious code because the structure of the query has
4) Tautologies: Here in this attack type which is our been predefined, here is an example:
practical side in this paper, the attacker uses the NoSQL const filter = { name: omar };
operates like $ne or $gt to make the query always return const update = { $set: { age: 23 } };
true when it is sent to the server in this way he can bypass // Update document matching the filter
the authentication like login pages and edit or extract data const result = await
from our server by injecting query statement return always collection.updateOne(filter, update);
true(tautologies), console.log(result);
IV. N O SQL P REVENTION M ETHODS 4) Use Express Mongo Sanitize: Mongo sanitize plays a
NoSQL database as we mentioned it’s not completely middle-ware in the system to prevent the user from sending
protected from security vulnerabilities, including NoSQL in- any input without being sanitized before it is passed to the
jection. the NoSQL injection is a type of attack where the server and gaining unauthorized access to sensitive data, this
attacker injects malicious code into NoSQL data, so in order module works by searching for any keys in the user input
to prevent this type of attack it is importnant to implement the that begin with $ sign or contain a . from the req.body
following techniques: in order to fully remove these keys and associated data from
1) Web application firewall: WAF short for (Web applica- the object, or replace the suspicions characters with another
tion firewall), this type of firewall its works by filtering and allowed ones.
monitoring the HTTP traffic between web applications and 5) least-privilege model: Today most advanced attacks pre-
the internet to prevent attacks like Cross-site forgery, XSS, dominantly exploit privileged credentials, in a way to access
file inclusion, and NoSQL injection, WAF serves as a kind of the super-user and administrator privileges, by limiting the
reverse proxy that shields the server ensuring clients interact user’s privileges in this way we reduce the ability of the user
with it before accessing the server, all this based on a set to gain any unauthorized access to any activity that is not
of rules or policies which is designed to protect the server restricted to it, the propagation of malware is halted by imple-
from application vulnerabilities by filtering the harmful traffic, menting least privilege on endpoints. This prevents malware
based on (the OSI model) WAF is a protocol layer 7 which is attacks, such as NoSQL injection attacks, from leveraging
(application layer), that means is not capable to defend against elevated privileges to gain broader access, move laterally, and
all type of attacks, also this method of mitigating attacks install or execute malware, or inflict damage to the machine.
typically constitutes an integral part of an exhaustive toolbox 6) Server-side validation: In server-side validation it’s a
that altogether gives a wide-ranging coverage against different second line of defense, also considered more reliable and
types of attack, but the ”Cons” of this type of prevention is: secure because it’s under the control of the server this form
‚ Bypassing. of validation offers a higher level of security, it can block
harmful or incorrect data from infiltrating with database, Figure.1, it is taken from Burp Suite tool and what we see
moreover, server-side validation can tap into the database or is a normal request from the website we need to inject, in
other resources, and carry out more intricate or conditional this type of attack the attacker manipulates the id parameter.
validations. so that makes it more difficult and decreases the This tool allows the attacker to modify the request before it
ability to bypass or manipulate it, and because we consider reaches the server. The object is to inject a condition that
the user input cannot be trusted we add this type of defense always evaluates to TRUE(e.g.,using $ne). As a result, the
and validate the input after it is received from the browser, injected condition effectively instructs the server to update
by creating a new function that check the user input from all records where the id is not equal to -1 and set their
server-side. reviews to match the message written by the attacker. so this
could lead to unauthorized modification of multiple records
V. E FFECT S ECURITY MEASURES ON SW PERFORMANCE in the server database, as we can see on Figure.2, the server
In this section we need to keep in mind the performance accepted the injected condition from the attacker and
of the website after we add all these prevention methods to returned HTTP/1.1 200 OK, this is shown on Figure.3.
mitigate of multi-type of attacks may the company or the
server be exposed to, so the security measures in software
can impact the performance, let’s take an e.g:
1) encryption: :This way to protect the confidentiality of
the data but, it introduces computational overhead when it
comes to big amount of data.
2) authentication and authorization: :It will check the
user’s privileges and access but requires more processing, and
may slow down the response times of the system.
3) server and user validation and sanitization: :Is a very
effective way to prevent injection attacks as we did in this
paper but imposes extra computational burden to check the
Fig. 2. The injected condition.
input integrity.
4) resource limitations: :security measures might require
extra hardware capacity, which could conflict with the primary
functions of the software.
There is more but in general, the developer should balance
security and performance to optimize the software perfor-
mance in the same time maintain the security measures, it
is a big challenge for the developer but it is very important
for delivering a secure and efficient user experience
VI. R ESULTS
Our practical attack is manipulated all product reviews in
the website, so we decided to choose OWASP juice-shop for
penetration testing because it provides this type of vulnerabil-
ity ”Tautologies” named (NoSQL reviews manipulating), so Fig. 3. The injection is succeed.
now I will explain how we exploited this type of attack on the
website we mentioned
Here we have our prevention practical solution: