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:
preload preload preload