19.09.2017 Views

the-web-application-hackers-handbook

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 9 n Attacking Data Stores 343<br />

NoSQL advocates claim this has several advantages, mainly in handling very<br />

large data sets, where <strong>the</strong> data store’s hierarchical structure can be optimized<br />

exactly as required to reduce <strong>the</strong> overhead in retrieving data sets. In <strong>the</strong>se<br />

instances a conventional database may require complex cross-referencing of<br />

tables to retrieve information on behalf of an <strong>application</strong>.<br />

From a <strong>web</strong> <strong>application</strong> security perspective, <strong>the</strong> key consideration is how<br />

<strong>the</strong> <strong>application</strong> queries data, because this determines what forms of injection<br />

are possible. In <strong>the</strong> case of SQL injection, <strong>the</strong> SQL language is broadly similar<br />

across different database products. NoSQL, by contrast, is a name given to a<br />

disparate range of data stores, all with <strong>the</strong>ir own behaviors. They don’t all use<br />

a single query language.<br />

Here are some of <strong>the</strong> common query methods used by NoSQL data stores:<br />

n Key/value lookup<br />

n XPath (described later in this chapter)<br />

n Programming languages such as JavaScript<br />

NoSQL is a relatively new technology that has evolved rapidly. It has not<br />

been deployed on anything like <strong>the</strong> scale of more mature technologies such as<br />

SQL. Hence, research into NoSQL-related vulnerabilities is still in its infancy.<br />

Fur<strong>the</strong>rmore, due to <strong>the</strong> inherently simple means by which many NoSQL implementations<br />

allow access to data, examples sometimes discussed of injecting into<br />

NoSQL data stores can appear contrived.<br />

It is almost certain that exploitable vulnerabilities will arise in how NoSQL<br />

data stores are used in today’s and tomorrow’s <strong>web</strong> <strong>application</strong>s. One such<br />

example, derived from a real-world <strong>application</strong>, is described in <strong>the</strong> next section.<br />

Injecting into MongoDB<br />

Many NoSQL databases make use of existing programming languages to provide<br />

a flexible, programmable query mechanism. If queries are built using<br />

string concatenation, an attacker can attempt to break out of <strong>the</strong> data context<br />

and alter <strong>the</strong> query’s syntax. Consider <strong>the</strong> following example, which performs<br />

a login based on user records in a MongoDB data store:<br />

$m = new Mongo();<br />

$db = $m->cmsdb;<br />

$collection = $db->user;<br />

$js = “function() {<br />

return this.username == ‘$username’ & this.password == ‘$password’; }”;<br />

$obj = $collection->findOne(array(‘$where’ => $js));<br />

if (isset($obj[“uid”]))<br />

{<br />

$logged_in=1;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!