Skip to content
Access control Write-up Advanced

Alternative link

The report describes a class of NTLM relay attacks against Microsoft Exchange Server, where an attacker coerces one Exchange server to authenticate to them and relays that authentication to another Exchange server, bypassing authentication and potentially achieving remote code execution. The vulnerabilities arise because Exchange servers trust each other's machine accounts, which have the right to impersonate any user.

blog.orange.tw

Published on blog.orange.tw

This is a cross-post blog from DEVCORE. You can check the series on:

The author, on blog.orange.tw

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.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 10 min read.

Context

This is the fourth and final part of a series on attack surfaces in Microsoft Exchange. The earlier parts covered ProxyLogon, ProxyOracle and ProxyShell. This part, ProxyRelay, is about NTLM relay between Exchange servers.

The feature at the centre of it is the Exchange Client Access Service (CAS) split into a Frontend and a Backend. The Frontend proxies HTTP requests to the Backend. The Backend authorises a request to impersonate any user by checking whether the login identity holds the Extended Right ms-Exch-EPI-Token-Serialization. During Exchange Server installation the mailbox server is added to the Exchange Servers Active Directory group automatically, and every object in that group has the Token-Serialisation right by default.

In the April 2021 patch Microsoft hardened the authentication part of the CAS Frontend: all HTTP requests that need a Kerberos Ticket must be authenticated first. That stopped unauthenticated HTTP requests from reaching the CAS Backend and mitigated the earlier Proxy attacks. The question this article answers is whether that is enough.

Setup

The attacks share one template. EX01 is the first Exchange Server, EX02 the second Exchange Server, and ATTACKER the attacker-controlled server. In every case the attacker coerces EX01 into initiating an NTLM authentication to him and relays it to EX02.

Two tools do the work:

  • printerbug.py coerces a server into initiating an SMB connection.
  • ntlmrelayx.py catches the NTLM authentication and relays it to another Exchange Server.

The coercion relies on the Printer Bug (MS-RPRN). It lets an attacker make any domain-joined machine start an SMB connection with its own Machine Account to the attacker. This works as designed, which is why it has been used for NTLM relaying for years.

The precondition that makes the relay interesting is the group membership described above: the Machine Account of an Exchange Server carries the Token-Serialisation right, so a relayed authentication made with that account can impersonate any user in Exchange services. Because the relay is cross-machine, the same-host restriction on NTLM relay does not apply.

Finding it

After the April 2021 patch the author kept thinking about whether the mitigation could be bypassed. The observation was that the Frontend check only fixes the code path Microsoft considered problematic. The author had already proposed several attacks and possible weaknesses in the POC 2021 and HITCON 2021 talks.

From the original on blog.orange.tw

The first of those predictions materialised in ProxyNotShell, which reuses the path confusion of ProxyShell but attaches a pre-known authentication. The author notes it still appears to need a valid authentication (and says he had not had time to dig into it). The other way, hinted at in the talks, avoids fighting the auth enhancement face to face: relay the NTLM authentication among Exchange Servers.

The idea has two properties that make it attractive. Being a cross-machine relay, it is not limited by the same-host restriction. And because the NTLM authentication is initiated by the Machine Account of an Exchange Server, the relayed authentication owns the Token-Serialisation right, which allows impersonating any user in Exchange services.

The same attack surface was found and reported to MSRC independently by Dlive from Tencent Xuanwu Lab, which is why the two share most of the CVE acknowledgements.

Exploitation

The idea applies to several contexts, each producing a different vulnerability:

CVE Context
CVE-2021-33768 Relay to Exchange FrontEnd
CVE-2022-21979 Relay to Exchange BackEnd
CVE-2021-26414 Relay to Exchange DCOM
CVE-2022-24477 Relay to other services of Exchange

Round 1: relay to the Exchange Frontend

The first context relays the authentication to the Frontend of another Exchange Server. The relayed identity is the Exchange Machine Account, which owns the Token-Serialisation right, so any user can be impersonated. The showcase relays the NTLM authentication from EX01 to the Frontend EWS service of EX02. The relay-to-frontend-EWS attack is implemented by customising httpattack.py.

  1. Run ntlmrelayx.py on the ATTACKER server to wait for NTLM authentications.
  2. Use printerbug.py to coerce EX01 to initiate an SMB connection to ATTACKER.
  3. Receive the SMB connection on ATTACKER and relay the NTLM blobs to EX02.
  4. Complete the NTLM handshakes to get full access to the EWS endpoint.

With full access to EWS, the target mailbox can be taken over through EWS operations. The demo dumps the secret under the administrator's mailbox.

Or watch on YouTube
From the original on blog.orange.tw

Patching the Frontend

Microsoft assigned CVE-2021-33768 and released a patch in July 2021 that makes the Frontend non-relayable. Logging in as a Machine Account on the Frontend is not a regular operation, so the mitigation is a check IsSystemOrMachineAccount() on the Frontend Proxy-Handler, ensuring that no Frontend logon is a Machine Account.

Round 2: relay to the Exchange Backend

A simple check mitigates the Frontend, but the Backend is harder. The Backend verifies Frontend requests by checking whether the caller is a Machine Account, so that is a regular operation. The Backend needs the Machine Account, which holds the Extended Right ms-Exch-EPI-Token-Serialization, to impersonate the desired user. Three showcases attack the Backend.

2-1: attacking Backend /EWS

The relay-to-frontend EWS attack can be re-applied to the Backend without changes apart from the target port, which moves from 443 to 444.

2-2: attacking Backend /RPC

The second showcase attacks Outlook Anywhere. Exchange defines several internal RPC services that can operate the mailbox directly. They have a public interface reachable through /Rpc/*, and users reach their own mailbox over RPC-over-HTTP as described in MS-RPCH. For the underlying mechanism the author recommends the research Attacking MS Exchange Web Interfaces by Arseniy Sharoglazov.

The core logic is the same as attacking EWS: /Rpc/* is served over HTTP/HTTPS, so it is relay-able. Once authentication is bypassed and the route /Rpc/RpcProxy.dll is reachable, any user can be impersonated and their mailbox operated over RPC-over-HTTP. The implementation ports a lot of the Ruler Project to Impacket. The result is that the authentication is bypassed with the Printer Bug and any user's mailbox is operated through Outlook Anywhere.

  1. Establish RCP_IN_DATA and RCP_OUT_DATA channels to EX02 for RPC I/O.
  2. Trigger PrinterBug on EX01 and relay to EX02 to complete NTLM handshakes.
  3. Attach X-CommonAccessToken headers to indicate we are Exchange Admin on both HTTP headers.
  4. Interact with Outlook Anywhere through the coding work upon MS-OXCRPC and MS-OXCROPS over MS-RPCH.

2-3: attacking Backend /PowerShell

The third showcase relays to Exchange PowerShell. Once authentication on the Backend IIS is bypassed, a ProxyShell-like exploit is possible again. With the ability to execute arbitrary Exchange Cmdlets, finding a post-authentication RCE to chain is not hard, because the attacker is Exchange Admin. There are hundreds of Cmdlets for Exchange Management, and past cases (CVE-2020-16875, CVE-2020-17083, CVE-2020-17132, CVE-2021-31207 and more) have shown that this is not a difficult task.

Because the team decided not to participate in Pwn2Own, this exploit chain was not implemented and is left as an exercise to the reader.

2-4: patching the Backend

Microsoft assigned CVE-2022-21979 and patched it in August 2022. The patch permanently eliminates all relay attacks on the Backend by forcibly turning on Extended Protection Authentication in IIS.

Round 3: relay to Windows DCOM

This part is credited to Dlive. MS-DCOM has been known to be relay-able since Sylvain Heiniger's research on relaying NTLM authentication over RPC. Dlive's contribution is an RCE chain built on the group inheritance of Exchange Servers in Active Directory environments.

The idea is that the Local Administrators group of an Exchange Server includes the group member Exchange Trusted Subsystem, and all Exchange Servers are in this group by default. That means the Machine Account EX01$ is also a local administrator of EX02. With that, the impact of relay-to-MS-DCOM is maximised and applies perfectly to Exchange Server.

Dlive demonstrated the attack in his DEFCON 29 talk. He did not publish exploit code, but the Wireshark screenshot on p45 of his slides already hints at everything and is enough to reproduce it.

Or watch on YouTube
From the original on blog.orange.tw
  1. Coerce EX01 to initiate a connection, and relay the NTLM to the Endpoint Mapper (port 135) of EX02 to get the Interface of MMC20.Application.
  2. Coerce EX01 again, and relay the NTLM to the dynamic port allocated by the EPMapper, and call ExecuteShellCommand(...) under iMMC->Document->ActiveView.
  3. Run arbitrary commands for fun and profit.

Writing the whole exploit is described as fun, like mixing dcomexec.py and ntlmrelayx.py together. Readers who want to understand the DCOM mechanism better are encouraged to write their own exploit code by hand.

Patching DCOM

Microsoft assigned CVE-2021-26414 and patched the DCOM relay in June 2021. Due to compatibility, the hardening on the server side is disabled by default. A server administrator has to activate the patch manually by creating the following registry key. If the administrator did not read the documentation carefully, the Exchange Server is probably still vulnerable after the June patch.

Text
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Ole\AppCompat\RequireIntegrityActivationAuthenticationLevel

According to the FAQ on the CVE page, Microsoft addressed the issue with a three-phase rollout. At the time of writing the rollout was in phase one, and the patch would not be activated by default until June 14, 2022, so the RCE was still exploitable on the latest version of Exchange Server. The author adds that Microsoft announced the second phase and enabled the hardening on the server side by default on June 14, 2022, so Exchange Servers with the latest Windows patch should be safe.

Round 4: relay to other Exchange services

Services on Exchange Server that use NTLM as their authentication method may be vulnerable too. At the time of writing one such service had been found and reported to MSRC. The author believes there are more, and calls this a good target for anyone who wants to discover vulnerabilities on Exchange Server.

Root cause

The Backend accepts a Machine Account as a Frontend caller and grants it the Token-Serialisation right, which is what lets it impersonate any user. Because the Machine Account of one Exchange Server is a local administrator of the other (through Exchange Trusted Subsystem in Local Administrators), the same relayed identity also reaches DCOM on the second server.

The Frontend fix for CVE-2021-33768 (rejecting Machine Account logons on the Frontend Proxy-Handler) does not help against the Backend, where Machine Account logons are a regular operation. The DCOM fix for CVE-2021-26414 was disabled by default for compatibility, so a server that installed only the June 2021 patch is probably still vulnerable.

Impact

An attacker can bypass Exchange authentication, and in some chains get code execution without user interaction. Concretely, the showcases allow taking over a target mailbox through EWS, operating any user's mailbox through Outlook Anywhere over /Rpc/RpcProxy.dll, and, with a post-authentication RCE chained to Exchange PowerShell, executing arbitrary commands. The DCOM chain, as demonstrated by Dlive, runs arbitrary commands on the second Exchange Server. All of this depends on the attacker being able to coerce an Exchange Server into an NTLM authentication (the Printer Bug) and on a second Exchange Server being available to relay to, which is common in corporate networks built for high availability and site resilience.

Fix

  • CVE-2021-33768 (July 2021): the Frontend Proxy-Handler checks IsSystemOrMachineAccount() so that no Frontend logon is a Machine Account.
  • CVE-2022-21979 (August 2022): Extended Protection Authentication is forcibly turned on in IIS on the Backend, which permanently eliminates relay attacks there.
  • CVE-2021-26414 (June 2021): DCOM relay hardening, disabled by default and activated by creating HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Ole\AppCompat\RequireIntegrityActivationAuthenticationLevel. It was enforced by default from June 14, 2022.
  • CVE-2022-24477 covers relay to other services of Exchange.

The fix arrived as a Cumulative Update rather than a regular Patch Tuesday release. Exchange Server 2019 CU 12 and Exchange Server 2016 CU 23 were released on April 20, 2022, but the patch was not enabled by default, and the activation methods were not published until August 09, 2022. The author found on April 21, 2022 that the exploit still worked on the latest version, and MSRC confirmed the CU contained the code change but needed manual activation.

Variants to test

  • Relay to /Rpc/RpcProxy.dll on the Backend and attach X-CommonAccessToken headers. A positive result is mailbox operations over Outlook Anywhere as an arbitrary user.
  • Relay to Exchange PowerShell on the Backend and attempt to run Exchange Cmdlets. A positive result is Cmdlet execution as Exchange Admin, which can then be chained to a post-authentication RCE.
  • Relay to the Endpoint Mapper on port 135 of the second server, then to the dynamic port it allocates, and call ExecuteShellCommand(...) under iMMC->Document->ActiveView. A positive result is arbitrary command execution on the second Exchange Server.
  • Look for other Exchange services that use NTLM as their authentication method and try the same coercion and relay pattern against them.

Written with AI from the full original report and checked against it passage by passage; anything it could not back up was removed or toned down. The original is the record. 21 min read.

What happened, in plain words

This is the last article in a series about weak points in Microsoft Exchange, the mail server software many companies run. The earlier articles covered attacks called ProxyLogon, ProxyOracle and ProxyShell. This one, ProxyRelay, is about a trick called NTLM relay between two Exchange servers.

To follow it you only need to know that a company often runs more than one Exchange server, so that mail keeps working if one of them fails. One server sits at the front and talks to the outside world (the Frontend), and another sits behind it and does the real work with mailboxes (the Backend). The Frontend passes requests to the Backend.

In April 2021 Microsoft changed the Frontend so that it first checks who is asking before passing a request on. That closed the earlier attacks. The question this article asks is whether that check is enough. The answer is no: an attacker can make one Exchange server log in to another Exchange server on his behalf, and the second server trusts that login completely.

The author reported the problem to Microsoft in June 2021. Microsoft said it was a design issue that needed a lot of code changes and testing, so it wanted to ship the fix in a big bundled update (a Cumulative Update) rather than a normal monthly patch. The author agreed to wait. Microsoft released Exchange Server 2019 CU 12 and Exchange Server 2016 CU 23 on April 20, 2022, but the fix was not switched on by default, and the instructions to switch it on were not published until August 09, 2022. The author had considered showing the attack at the Pwn2Own Vancouver 2021 contest but dropped the idea, because the goal was not to earn prize money but to make the world more secure.

The idea

After the April 2021 patch the author kept wondering whether the new check could be got around. The patch only fixed the specific code Microsoft saw as the problem. In his POC 2021 and HITCON 2021 talks he had already described several attacks and possible weaknesses.

From the original on blog.orange.tw

One of those predictions later appeared in the wild as ProxyNotShell. That attack reuses a trick from ProxyShell (confusing the server about which path a request is for) but adds a login the attacker already knows. The author notes it still seems to need a valid login, and says he had not had time to look into it closely. The other way, hinted at in the talks, avoids fighting the new login check head on: relay the login between Exchange servers.

Before going further, three ideas need explaining.

What NTLM is. NTLM is an old way for a computer to prove who it is to another computer. Instead of sending a password, the two machines exchange a short series of messages, called a handshake, and each side checks a value derived from the password. It is like a doorman who does not ask for your name but asks you to answer a challenge only the real person could answer.

What a Machine Account is. Every computer that joins a Windows company network (a domain) gets its own account in the directory, with a name ending in a dollar sign, for example EX01$. This is not a person's account; it is the computer's own identity. Servers use it to talk to each other.

What NTLM relay is. If an attacker can make a victim computer start a login to the attacker, the attacker does not need to crack anything. He can simply pass the login messages on to a third computer and let that third computer believe the victim is logging in to it. It is like catching a signed letter addressed to you and forwarding it, unchanged, to someone else who accepts the signature.

There is a well-known way to make a Windows machine start such a login: the Printer Bug, based on a protocol called MS-RPRN. It lets an attacker ask any machine in the domain to open a file-sharing connection (SMB) to the attacker, using the machine's own account. This is how the feature is designed to work, which is why attackers have used it for NTLM relay for years. The author credits Lee Christensen, Will Schroeder and Matt Nelson, whose DerbyCon 2018 talk popularised it.

Now the key detail. In Exchange, the Backend lets a request act as any user if the login making the request holds a special permission called an Extended Right, ms-Exch-EPI-Token-Serialization. When Exchange Server is installed, the mailbox server is automatically added to an Active Directory group called Exchange Servers, and every object in that group has this permission by default. In other words, the machine account of an Exchange server is allowed to pretend to be any user.

Put those two facts together and the idea is simple. Companies often run several Exchange servers for high availability and site resilience. Can we relay the NTLM login from one Exchange server to another?

This has two advantages. First, because the relay goes from one machine to a different machine, a usual restriction that blocks relay back to the same machine does not apply. Second, because the login is made by an Exchange server's Machine Account, the relayed login carries the Token-Serialisation permission, so the attacker can impersonate any user in Exchange services.

The same attack surface was found and reported to Microsoft independently by Dlive from Tencent Xuanwu Lab, which is why the two share most of the CVE acknowledgements. A CVE is a public identifier for a security flaw, so people can refer to the same bug unambiguously.

The vulnerabilities

This is not one bug but a whole attack surface, so the same idea applies in different places and produces different vulnerabilities. The impact is that an attacker can get past Exchange's login checks, and in some chains run code without any user doing anything. These are the related CVEs:

CVE Where the relay goes
CVE-2021-33768 Relay to Exchange FrontEnd
CVE-2022-21979 Relay to Exchange BackEnd
CVE-2021-26414 Relay to Exchange DCOM
CVE-2022-24477 Relay to other services of Exchange

All the attacks follow the same pattern. EX01 is the first Exchange Server, EX02 is the second Exchange Server, and ATTACKER is the server the attacker controls. In every case the attacker makes EX01 start an NTLM login to him, and passes it on to EX02.

Two tools do the work:

  • printerbug.py makes a server start an SMB connection.
  • ntlmrelayx.py catches the NTLM login and passes it on to another Exchange Server.

Round 1: relay to the Exchange Frontend

In the first case the login is relayed to the Frontend of another Exchange Server. The identity being relayed is the Exchange Machine Account, which holds the Token-Serialisation permission, so any user can be impersonated. The demonstration relays the NTLM login from EX01 to the Frontend EWS service of EX02.

EWS stands for Exchange Web Services. It is an API: a way for programs to talk to Exchange over the web instead of a person clicking in a mail app. Think of it as a service counter where a program can ask for mail, calendars and contacts. The author implemented this attack by customising a file called httpattack.py.

Here is the overview:

  1. Run ntlmrelayx.py on the ATTACKER server to wait for NTLM authentications.
  2. Use printerbug.py to coerce EX01 to initiate an SMB connection to ATTACKER.
  3. Receive the SMB connection on ATTACKER and relay the NTLM blobs to EX02.
  4. Complete the NTLM handshakes to get full access to the EWS endpoint.

In plain words: step 1 sets up the attacker's server as a listener, like leaving a phone off the hook waiting for a call. Step 2 tricks EX01 into calling the attacker. Step 3 takes the login messages that arrive and forwards them to EX02, so EX02 thinks it is talking to EX01. Step 4 finishes the login exchange, and because the identity is EX01's machine account, the attacker now has full access to the EWS endpoint on EX02.

With full access to EWS, the target mailbox can be taken over through EWS operations. The demonstration dumps the secret under the administrator's mailbox.

Or watch on YouTube
From the original on blog.orange.tw

Patching the Frontend

Microsoft assigned CVE-2021-33768 and released a patch in July 2021 that makes the Frontend non-relayable. Logging in as a Machine Account on the Frontend is not a normal thing to do, so the fix is simple: a check called IsSystemOrMachineAccount() on the Frontend Proxy-Handler, which makes sure no Frontend login is a Machine Account. The Proxy-Handler is the piece of code that receives an incoming request and decides what to do with it.

Round 2: relay to the Exchange Backend

A simple check fixes the Frontend. The Backend is harder. The Backend checks whether a request from the Frontend comes from a Machine Account, so Machine Account logins there are normal and cannot simply be refused. The Backend needs the Machine Account, which holds the Extended Right ms-Exch-EPI-Token-Serialization, to impersonate the user the request is for. Three demonstrations attack the Backend.

2-1: attacking Backend /EWS

The relay-to-frontend EWS attack can be used against the Backend with one change: the target port moves from 443 to 444. A port is like a numbered door on a server; 443 is the usual door for secure web traffic, and here the Backend listens on 444.

2-2: attacking Backend /RPC

The second demonstration attacks Outlook Anywhere. Outlook Anywhere lets Outlook reach a mailbox over ordinary web connections instead of a special mail network connection. Exchange defines several internal RPC services that can operate the mailbox directly. RPC means Remote Procedure Call: a way for one program to call a function on another computer as if it were local. These services have a public interface reachable through /Rpc/*, and users reach their own mailbox over RPC-over-HTTP, described in Microsoft's MS-RPCH specification. For the underlying mechanism the author recommends the research Attacking MS Exchange Web Interfaces by Arseniy Sharoglazov.

The core logic is the same as attacking EWS: /Rpc/* is served over HTTP/HTTPS, so it can be relayed to. Once the login check is bypassed and the route /Rpc/RpcProxy.dll is reachable, any user can be impersonated and their mailbox operated over RPC-over-HTTP. The implementation ports a lot of the Ruler Project to Impacket. The result is that the login check is bypassed with the Printer Bug and any user's mailbox is operated through Outlook Anywhere.

The steps are:

  1. Establish RCP_IN_DATA and RCP_OUT_DATA channels to EX02 for RPC I/O.
  2. Trigger PrinterBug on EX01 and relay to EX02 to complete NTLM handshakes.
  3. Attach X-CommonAccessToken headers to indicate we are Exchange Admin on both HTTP headers.
  4. Interact with Outlook Anywhere through the coding work upon MS-OXCRPC and MS-OXCROPS over MS-RPCH.

Step by step: step 1 opens two channels, one for data going in and one for data coming out, so the attacker can hold a two-way conversation with the RPC service on EX02. Step 2 is the same trick as before: make EX01 log in to the attacker and forward that login to EX02. Step 3 adds a header to the web requests. A header is a small labelled field attached to a request, like the "From" line on a letter, and X-CommonAccessToken is the field Exchange uses to carry the identity of the caller; here it is set to say the caller is an Exchange Admin. Step 4 does the actual mailbox work by speaking the Microsoft protocols for RPC over HTTP, MS-OXCRPC and MS-OXCROPS.

2-3: attacking Backend /PowerShell

The third demonstration relays to Exchange PowerShell. PowerShell is Microsoft's command-line tool for automating tasks; Exchange exposes a remote PowerShell endpoint so administrators can run management commands. Once the login check on the Backend IIS is bypassed, a ProxyShell-like exploit is possible again. IIS is Microsoft's web server software, the part that receives web requests. With the ability to run arbitrary Exchange Cmdlets, finding a post-authentication RCE to chain is not hard, because the attacker is Exchange Admin. A Cmdlet is a single PowerShell command, and RCE means remote code execution: running your own code on someone else's machine. There are hundreds of Cmdlets for Exchange Management, and past cases (CVE-2020-16875, CVE-2020-17083, CVE-2020-17132, CVE-2021-31207 and more) have shown that this is not a difficult task.

Because the team decided not to take part in Pwn2Own, this exploit chain was not implemented and is left as an exercise to the reader.

2-4: patching the Backend

Microsoft assigned CVE-2022-21979 and patched it in August 2022. The patch permanently removes all relay attacks on the Backend by forcibly turning on Extended Protection Authentication in IIS. Extended Protection Authentication is a setting that ties a login to the specific connection it was made on, so a login caught on one connection cannot be replayed on another.

Round 3: relay to Windows DCOM

This part is credited to Dlive. DCOM is a Microsoft technology that lets one program on a computer ask another program, possibly on a different computer, to do something. MS-DCOM has been known to be relay-able since Sylvain Heiniger's research on relaying NTLM authentication over RPC. Dlive's contribution is a chain that ends in code execution, built on how group membership is inherited between Exchange servers in Active Directory environments. Active Directory is the directory that holds accounts and groups for a Windows company network.

The idea is that the Local Administrators group of an Exchange Server includes the group member Exchange Trusted Subsystem, and all Exchange Servers are in this group by default. That means the Machine Account EX01$ is also a local administrator of EX02. With that, the impact of relay-to-MS-DCOM is maximised and applies perfectly to Exchange Server.

Dlive demonstrated the attack in his DEFCON 29 talk. He did not publish exploit code, but the Wireshark screenshot on p45 of his slides already hints at everything and is enough to reproduce it. Wireshark is a program that records and displays network traffic, so the screenshot shows the exact messages that were exchanged.

Or watch on YouTube
From the original on blog.orange.tw

The steps are:

  1. Coerce EX01 to initiate a connection, and relay the NTLM to the Endpoint Mapper (port 135) of EX02 to get the Interface of MMC20.Application.
  2. Coerce EX01 again, and relay the NTLM to the dynamic port allocated by the EPMapper, and call ExecuteShellCommand(...) under iMMC->Document->ActiveView.
  3. Run arbitrary commands for fun and profit.

In plain words: the Endpoint Mapper is a directory service on port 135 that tells you which numbered door a given service is listening on. Step 1 asks it where MMC20.Application lives; that is a component that can be driven remotely. Step 2 makes EX01 log in again and forwards that login to the door the Endpoint Mapper named, then calls a function that runs a shell command. Step 3 is the payoff: commands of the attacker's choosing run on EX02.

Writing the whole exploit is described as fun, like mixing dcomexec.py and ntlmrelayx.py together. Readers who want to understand the DCOM mechanism better are encouraged to write their own exploit code by hand.

Patching DCOM

Microsoft assigned CVE-2021-26414 and patched the DCOM relay in June 2021. Because of compatibility concerns, the hardening on the server side is switched off by default. A server administrator has to activate the patch by hand by creating the following registry key. The registry is a central store of settings on Windows, and a key is one entry in it.

Text
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Ole\AppCompat\RequireIntegrityActivationAuthenticationLevel

This line is the path of the registry entry that turns the protection on. If the administrator did not read the documentation carefully, the Exchange Server is probably still vulnerable after the June patch.

According to the FAQ on the CVE page, Microsoft handled the issue in three phases. At the time of writing the rollout was in phase one, and the patch would not be on by default until June 14, 2022, so the RCE was still exploitable on the latest version of Exchange Server. The author adds that Microsoft announced the second phase and turned the hardening on by default on the server side on June 14, 2022, so Exchange Servers with the latest Windows patch should be safe.

Round 4: relay to other Exchange services

Services on Exchange Server that use NTLM as their login method may be vulnerable too. At the time of writing one such service had been found and reported to Microsoft. The author believes there are more, and calls this a good target for anyone who wants to find vulnerabilities on Exchange Server.

Why it works

The Backend accepts a Machine Account as a Frontend caller and grants it the Token-Serialisation right, which is what lets it impersonate any user. Because the Machine Account of one Exchange Server is a local administrator of the other (through Exchange Trusted Subsystem in Local Administrators), the same relayed identity also reaches DCOM on the second server.

The April 2021 Frontend change did not stop this because it only required a login before requests needing a Kerberos Ticket reached the Backend. Kerberos is another login system, newer than NTLM, based on tickets issued by a central authority. A relayed Machine Account login satisfies that requirement. The Frontend fix for CVE-2021-33768 (refusing Machine Account logins on the Frontend Proxy-Handler) does not help against the Backend, where Machine Account logins are normal. The DCOM fix for CVE-2021-26414 was off by default for compatibility, so a server that installed only the June 2021 patch is probably still vulnerable.

What an attacker gets

An attacker can get past Exchange's login checks, and in some chains run code without any user doing anything. Concretely, the demonstrations allow taking over a target mailbox through EWS, operating any user's mailbox through Outlook Anywhere over /Rpc/RpcProxy.dll, and, with a post-authentication RCE chained to Exchange PowerShell, running arbitrary commands. The DCOM chain, as demonstrated by Dlive, runs arbitrary commands on the second Exchange Server. All of this depends on the attacker being able to make an Exchange Server start an NTLM login (the Printer Bug) and on a second Exchange Server being available to relay to, which is common in corporate networks built for high availability and site resilience.

The fixes

  • CVE-2021-33768 (July 2021): the Frontend Proxy-Handler checks IsSystemOrMachineAccount() so that no Frontend login is a Machine Account.
  • CVE-2022-21979 (August 2022): Extended Protection Authentication is forcibly turned on in IIS on the Backend, which permanently removes relay attacks there.
  • CVE-2021-26414 (June 2021): DCOM relay hardening, off by default and activated by creating HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Ole\AppCompat\RequireIntegrityActivationAuthenticationLevel. It was enforced by default from June 14, 2022.
  • CVE-2022-24477 covers relay to other services of Exchange.

The fix arrived as a Cumulative Update rather than a regular Patch Tuesday release. Exchange Server 2019 CU 12 and Exchange Server 2016 CU 23 were released on April 20, 2022, but the patch was not on by default, and the activation methods were not published until August 09, 2022. The author found on April 21, 2022 that the exploit still worked on the latest version, and Microsoft confirmed the CU contained the code change but needed manual activation.

Things to try

  • Relay to /Rpc/RpcProxy.dll on the Backend and attach X-CommonAccessToken headers. A positive result is mailbox operations over Outlook Anywhere as an arbitrary user.
  • Relay to Exchange PowerShell on the Backend and attempt to run Exchange Cmdlets. A positive result is Cmdlet execution as Exchange Admin, which can then be chained to a post-authentication RCE.
  • Relay to the Endpoint Mapper on port 135 of the second server, then to the dynamic port it allocates, and call ExecuteShellCommand(...) under iMMC->Document->ActiveView. A positive result is arbitrary command execution on the second Exchange Server.
  • Look for other Exchange services that use NTLM as their login method and try the same coercion and relay pattern against them.

Timeline

  • Jun 02, 2021 - We reported the vulnerability to Microsoft through the MSRC portal.
  • Jun 03, 2021 - MSRC opened the case. (No. 65594)
  • Jun 03, 2021 - We attached a 90-days Vulnerability Disclosure Policy to MSRC. The deadline is Sep 01, 2021.
  • Jun 11, 2021 - MSRC replied that they are aiming to complete it before September.
  • Jul 22, 2021 - MSRC said the case doesn't look like it will be fully resolved by September.
  • Jul 25, 2021 - We said we could extend the deadline and let us know the new estimated date.
  • Aug 25, 2021 - We asked for the estimated date again.
  • Sep 01, 2021 - MSRC said this case has been expanding into a design change and the intended release date is December 2021.
  • Sep 08, 2021 - We asked is it possible to shorten the time frame because we would like to disclose this at conferences.
  • Sep 17, 2021 - MSRC replied there are not quick and simple fixes but design level changes, they can't get the changes in October.
  • Oct 25, 2021 - We decided not to disclose this at conferences and gave the team a fair time for fixing and testing. We hoped this bug could be fixed as scheduled in December 2021.
  • Dec 21, 2021 - We asked for updates on this case.
  • Dec 22, 2021 - MSRC replied they aimed to include this patch in a CU (Cumulative Update) instead of an SU (Security Update) due to the level of changes. The next CU release date will be in March 2022.
  • Apr 04, 2022 - We asked that we don't see the CU in March. When is the new release date?
  • Apr 13, 2022 - MSRC replied the CU is delayed, and the current release date is on April 20, 2022.
  • Apr 20, 2022 - Microsoft released Exchange Server 2019 CU 12 and Exchange Server 2016 CU 23.
  • Apr 21, 2022 - We found our exploit still works fine on the latest version of Exchange Server and asked is this bug really fixed?
  • Apr 27, 2022 - MSRC replied the CU contain the code change, but it needs to be activated manually or with a script. There are still some testing concerns but the manual activation process will be public on May 10, 2022.
  • May 11, 2022 - MSRC said the documentation and the script are mapped for the Patching Tuesday of June 2022 (Jun 14, 2022).
  • Jun 10, 2022 - MSRC said there are still having some issues on testing and they are looking to release this in July 2022.
  • Jul 04, 2022 - We asked if it will release in this month's Patching Tuesday.
  • Aug 10, 2022 - Don't see anything, asked again.
  • Aug 18, 2022 - Microsoft released the CVE and the patch activation documentation!

Closing

This series has finally come to an end. Over the past two years many ups and downs made the journey unusual: from the earliest bug collision with a bad actor and the panic of attacks seen in the wild, to the Pwn2Own contest and talks accepted at top hacker conferences. The author says he has a clear conscience that nothing wrong was done, but that without understanding the context there were many incorrect speculations and inaccurate media reports about his company and himself, and even low blows. There were happy moments too, such as winning a first Master-of-Pwn title at Pwn2Own and the Best Server-Side bug award at the Pwnie Awards, but the gossip and trolling were hard on him.

He is glad to close this research and start something new, describing himself as nothing but a security nerd who would rather spend time on hacks. He asks for patience with his sometimes short and unclear sentences, since writing and presenting in a non-native language takes him four to five times as long and many words are lost in refining. He hopes that one day there will be no language barrier, and that people can sit in a bar with beers and talk about hacks, culture and hacking all night.

Glossary

  • Active Directory: the directory that stores accounts, computers and groups for a Windows company network.
  • API: a defined way for one program to ask another program to do something, like a service counter for software.
  • Backend: the Exchange server component that does the real work with mailboxes, behind the Frontend.
  • CAS (Client Access Service): the Exchange part that receives client connections, split into a Frontend and a Backend.
  • Cmdlet: a single command in PowerShell.
  • Cumulative Update (CU): a large bundled update that contains many changes at once, as opposed to a small monthly patch.
  • CVE: a public identifier for a security flaw, so everyone can refer to the same bug unambiguously.
  • DCOM: a Microsoft technology that lets one program ask another program, possibly on another computer, to do something.
  • Endpoint Mapper: a directory service on port 135 that tells you which numbered port a given service listens on.
  • Extended Protection Authentication: a setting that ties a login to the specific connection it was made on, so it cannot be replayed elsewhere.
  • Extended Right: a fine-grained permission in Active Directory, here ms-Exch-EPI-Token-Serialization.
  • EWS (Exchange Web Services): an API that lets programs work with mail, calendars and contacts over the web.
  • Frontend: the Exchange server component that faces clients and passes requests to the Backend.
  • Header: a small labelled field attached to a web request, like the "From" line on a letter.
  • High availability / site resilience: running several servers so that mail keeps working if one fails or a site goes down.
  • IIS: Microsoft's web server software, which receives web requests.
  • Kerberos: a login system based on tickets issued by a central authority, newer than NTLM.
  • Machine Account: the directory account a computer gets when it joins a domain, named like EX01$.
  • MS-RPCH: Microsoft's specification for carrying RPC over HTTP.
  • MS-RPRN: Microsoft's print system protocol, abused by the Printer Bug to make a machine connect out.
  • NTLM: an older challenge-and-response login method where machines prove identity without sending the password.
  • NTLM relay: catching a victim's login messages and forwarding them to a third computer that accepts them.
  • Outlook Anywhere: a feature that lets Outlook reach a mailbox over ordinary web connections.
  • Port: a numbered door on a server that a particular service listens on.
  • Post-authentication RCE: a way to run your own code that only works after you are logged in.
  • PowerShell: Microsoft's command-line tool for automating tasks.
  • Printer Bug: a designed behaviour of MS-RPRN that lets an attacker make a machine start an SMB connection to him.
  • Proxy-Handler: the piece of code that receives an incoming request and decides what to do with it.
  • RCE (remote code execution): running your own code on someone else's machine.
  • Registry: a central store of settings on Windows; a key is one entry in it.
  • RPC (Remote Procedure Call): a way for one program to call a function on another computer as if it were local.
  • SMB: the file-sharing protocol Windows machines use to open connections to each other.
  • Token-Serialisation right: the Exchange permission that lets a login act as any user.
  • Wireshark: a program that records and displays network traffic.

Similar reports

View all

Same bug class and techniques, clearest first.

Broken access control

The lesson behind this report.

A user can do something their role should not allow: reach an admin function, act on a feature they have not paid for, or skip a permission check. IDOR is the special case where the object belongs to someone else.