This article is one of a collection of primer articles, providing a basic introduction to various topics, for reference in other – more in-depth – articles. These primer articles are written as people come to security pages from various backgrounds, be it a software engineer who wants to understand an issue they need to fix (or prevent one from occurring in the first place) or a penetration tester with no background in software engineering, who wants to understand and know how to exploit a particular issue.
This primer is on No-SQL databases and specifically regarding MongoDB.
MongoDB is one of several 'No-SQL' databases, other examples include: Couchbase, DynamoDB, Memcached, Redis, and a plethora of others!
MongoDB can be deployed and managed by IT teams locally within an organisation’s own infrastructure (Cloud or otherwise), or it is available as a cloud service, as MongoDB Atlas.
There are a variety of differences between these two broad categories of database technology, not least because there is huge variability in the No-SQL space. For example, some of these store Key-Value pairs (e.g., Couchbase), whilst databases such as MongoDB store their data as JSON (JavaScript Object Notation) Documents.
At broad level, some important common differences are as follows:
No-SQL database advantages, when compared with SQL databases, they:
No-SQL database disadvantages, when compared with SQL databases, they:
Note: neither list above is complete, nor do the advantages or disadvantages universally apply!
A MongoDB does not store ‘Records’, or ‘Rows’, nor does it have a concept of 'Tables' as per traditional SQL databases. Instead, MongoDB uses 'Documents' and 'Collections'.
The Document being the closest equivalent to a row/record and the Collection being the closest equivalent to a table.
An interesting feature of this is that the data is - potentially - denormalised, and the structure of Documents in the same Collection can vary wildly.
MongoDB stores its data as JSON documents (or more specifically BSON (Binary JavaScript Object Notation) which is a binary encoded JSON document).
However, for the purposes of simplicity, in this article we will show BSON represented as JSON!
If we consider a simple example of a database system that held three attributes in a user table: UserId, UserName, and Password.
In a traditional SQL database, this would be represented in a table format:
UserId | UserName | Password |
1 | Yossarian | Catch22 |
Whereas with JSON, this could look like:
{
id: 1,
username: 'Yossarian',
password: 'Catch22'
}
Note: yes, there is absolutely a glaring security flaw here, but that is not the focus of this article!
In terms of interacting with the databases, the following presents how to select data in both scenarios:
Traditional SQL Query:
SELECT UserId FROM TUser WHERE UserName = 'Yossarian' AND Password = 'Catch22'
No-SQL Query:
{ UserName: 'Yossarian', Password: 'Catch22' }
The following image shows what this might look like in the MongoDB Compass application:
As can be seen, submitting such a query, will return the associated JSON document.
Of course, there are a wide range of queries that can be executed, and very importantly - especially from the perspective of an attacker - there are various MongoDB commands that can be submitted, parsed, and executed by MongoDB itself.
This has been a very basic introduction to No-SQL databases, and very specifically MongoDB! As can be seen, these behave in a different manner than traditional SQL databases, and present some advantages and some disadvantages.
However, fundamentally, they are still data stores that can be queried by constructing queries using external input, which is very important from a security perspective.
Cookie Notice
We use cookies to ensure that we give you the best experience on our website. Please confirm you are happy to continue.