Jan 10

Its a common misconception that as MongoDB does not use SQL it is not vulnerable to SQL injection attacks. PHP uses objects rather than SQL to pass queries to the MongoDB server; for example the following script selects an item form MongoDB where the username equals ‘bob’ and the password equals ‘password’.

$collection->find(array(

       "username" => $_GET['username'],

       "passwd" => $_GET['passwd']

));

This is equivalent to the SQL syntax

mysql_query("SELECT * FROM collection
       WHERE username=" . $_GET['username'] . ",
       AND passwd=" . $_GET['passwd'])

In a normal SQL injection attack we can replace either of the two input parameters with a string such that the SQL query always returns true. e.g.

login.php?username=admin&passwd=" OR 1 --

That wont work with MongoDB; however if we can pass in an object to the PHP MongoDB driver we could alter the query in a similar fashion. Luckily PHP provides us with a way to pass objects as GET or POST parameters:

login.php?username=admin&passwd[$ne]=1

This creates the MongoDB query

$collection->find(array(
     "username" => "admin",
     "passwd" => array("$ne" => 1)
));

Which is the equivalent to the following SQL statement which, unless the password is “1″ will always return true.

mysql_query("SELECT * FROM collection
    WHERE username="admin",
    AND passwd!=1

The solution is to ensure your variables are properly typed before they are passed into the MongoDB driver. The following code is not vulnerable to MongoDB injection:

$collection->find(array(
     "username" => (string)$_GET['username'],
     "passwd" => (string)$_GET['passwd']
));

Tagged with:
Nov 10

You can find fllow information in mongodb log

Thu Nov 10 23:17:42 [initandlisten] MongoDB starting : pid=19198 port=27017 dbpath=/data/db/ 64-bit host=localhost.localdomain
Thu Nov 10 23:17:42 [initandlisten] db version v2.0.1, pdfile version 4.5
Thu Nov 10 23:17:42 [initandlisten] git version:
                                  3a5cf0e2134a830d38d2d1aae7e88cac31bdd684
Thu Nov 10 23:17:42 [initandlisten] build info: Linux bs-linux64.10gen.cc 2.6.21.7-2.ec2.v1.2.fc8xen #1 SMP Fri Nov 20 17:48:28 EST 2009 x86_64
                                 BOOST_LIB_VERSION=1_41
Thu Nov 10 23:17:42 [initandlisten] options: { config: "etc/mongo.conf", dbpath:
                        "/data/db/", fork: "1", journal: "1", logpath: 
                        "/opt/wwh/mongo/logs/mongodb.log", maxConns: 200, port: 27017,
                        repair: true, repairpath: "/tmp", syncdelay: 30.0 }
                        **************
old lock file: /data/db/mongod.lock.  probably means unclean shutdown,
but there are no journal files to recover.
this is likely human error or filesystem corruption.
found 3 dbs.
see: http://dochub.mongodb.org/core/repair for more information
*************

Start Repair:

   Step 1: stop mongod & backup and delete mongodb.log

kill -2 `ps aux |grep mongod |grep -v grep |awk ‘{print $2}’`

rm –f mongodb.log

   Step 2: Dlete mongod process lock

rm –f /data/db/mongod.lock

   Step 3: Start Reapir mongod

bin/mongod –f etc/mongodb.conf –repair –repairpath /tmp

Tagged with:
Sep 03

Mongodb, so long to fire the thing actually had a good look.  Carefully until no time learn new things, always feel lack of energy.  The advantage of buying a book on fragmented in the VPS on the build, test, to see the implementation code.  Feeling quite interesting a database. Although the feeling it is very simple, especially when it is looking at the code feel so.  But this is not what is another example of KISS, or something simple but useful most popular.

Since they saw their implementation, can not fail to output something.  Just did not update the blog for many years, on a simple analysis of the safety mongodb, Minato number first.

The security situation in the default configuration

By default, mongod is listening on 0.0.0.0 above.  And any client can be connected directly 27017, and no certification.  Advantage is that the developer or dba can get started immediately, without fear of being a bunch of configuration get the upset. Downside is, it is obvious that if you directly on a public server so build mongodb, so everyone can access and modify your database data.  By default, mongod is no administrator account. So unless you use the database in the admin db.addUser () command to add the administrator account, and use the – auth argument started mongod, or in a database that anyone can execute all commands without authentication.  Including delete and shutdown.

In addition, mongod will default listening 28017 port, also binds to all ip.  This is a mongod native web monitoring interface  From which you can obtain the current connection to the database, log, status, operating system and other information.  If you open the – rest parameters, or even directly through the web interface to query data, perform mongod command.  I spent an evening trying to scan a B segment of the domestic and foreign a B segment.  The result is open 78 overseas mongodb, and 60 domestic.  I randomly picked one of 10 attempts to connect, and only one machine plus the administrator account to do the certification, while others are all undefended city. Shows that the problem is quite serious.

In fact Mongodb itself has a very detailed security configuration guidelines , obviously he is thought of, but he is safe to push to the user to solve the task, this strategy is to bias their ease of use, for safety, then was sidelined .

User information is stored and the certification process

MySQL will be similar to the system user information stored in the mysql.user table.  mongodb will also be users of the system username, pwd stored in admin.system.users collection.  One pwd = MD5 (username + ": Mongo:" + real_password) .  This in itself is not a problem.  username and: mongo: equivalent to the original password plus a salt value, even if the attacker access to the database stored in md5 hash, also can not simply from the rainbow tables found in the original password.

We look at mongodb interaction on the client how to achieve certification. mongo client and server interactions are based on clear, so it is easy to network sniffing, etc. crawl. Here we use a database that comes with mongosniff, can dump the client and server interaction for all packets:

[root@localhost bin]# ./mongosniff –source NET lo
sniffing 27017

127.0.0.1:34142  –>> 127.0.0.1:27017 admin.$cmd  62 bytes  id:8        8
        query: { getnonce: 1.0 }  ntoreturn: -1 ntoskip: 0
127.0.0.1:27017  <<–  127.0.0.1:34142   81 bytes  id:7 7 – 8
        reply n:1 cursorId: 0
        { nonce: "df97182fb47bd6d0", ok: 1.0 }
127.0.0.1:34142  –>> 127.0.0.1:27017 admin.$cmd  152 bytes  id:9       9
        query: { authenticate: 1.0, user: "admin", nonce: "df97182fb47bd6d0", key: "3d839522b547931057284b6e1cd3a567" }  ntoreturn: -1 ntoskip: 0
127.0.0.1:27017  <<–  127.0.0.1:34142   53 bytes  id:8 8 – 9
        reply n:1 cursorId: 0
        { ok: 1.0 }

 
  • The first step, client to server sends a command getnonce, apply a random value to the server nonce. server returns a 16-bit nonce.  The value returned here is not the same every time.
  • The second step, client will be entered by the user of the password through the algorithm to generate a key, the Key = MD5 (nonce + username + MD5 (username + ": Mongo:" + real_passwd)) , and the user name together with, nonce returned with to the server. server receives data, whether the first than the last nonce generated nonce, and then compare key == md5 (nonce + username + pwd).  If the same is verified by .

As the start to finish no password hash over the network, but use a similar mechanism to the challenge, and every time nonce values ​​are different, so even if the attacker to intercept the key value, useless way through replay attacks by certification.

However, when the attacker access to the database stored in pwd hash, the authentication mechanism does not play a role.  Even if the attacker does not break out the pwd hash the password corresponding to the original.  But can still send md5 (nonce + username + pwd) directly through the server’s certificate.  This server is actually the user’s pwd hash as the real password to verify, there is no text-based password authentication.  At this point, and I had analyzed the mysql authentication mechanism is actually no essential difference. Of course, this may not be regarded as weak authentication mechanism, but after all, to get the username and pwd mongodb likely will be even greater.

image

However, the monitoring interface of the Web there are a number of different certification.  When the client source is not localhost, where the user authentication process is based on The certification process is similar with mongo.  But a major difference: here’s nonce is not randomized, but each time the default is "abc" .

Using this feature, if the attacker grabbed a successful administrator login, so he can replay this packet, directly to Web monitoring page.

Similarly, an attacker can brute force through this interface directly mongo username and password.  In fact 27017 and 28017 are not limited to the password to do guess, but the Web because no time to get nonce, so will be easier.

JavaScript implementation and protection of

Mongodb itself one of the biggest feature is that he is using the javascript language as a command-driven.  Hackers would be more concerned about this, because of its command of the degree of support, is to get permission mongodb whether after further penetrate key.  Javascript standard library itself is actually quite weakWhether spidermonkey or v8 engine is actually not the system, the file related to the operation support.  In this regard, mongodb do some expansion . You can see, ls / cat / cd / hostname even runProgram have been in the context of a Javascript implementation.  See here is not can not wait?  mongo shell in type ls ("./"), try to see return.

How the results so familiar? Haha, yes, in fact, are these api to achieve in the context of the client. A little joke:) So if you can do in the server side js it?  The answer is yes. Use db.eval (code) – in fact the underlying implementation is db. $ Cmd.findOne ({$ eval: code}) – We can implement the server side js code.

Of course, there are in the server side js context expansion .  Obviously mongod into account the security issues (and possibly other reasons), so in here and did not provide such a powerful client. Of course mongodb is constantly updated, long-term interest in this list, maybe later have a similar load_file / exec like to achieve.

Eliminate the problems caused by server problems js implementation can be used noscripting parameters.Directly prohibit server-side js code execution.

Tagged with:
preload preload preload