Penetration Technique: Remote Access to Exchange PowerShell

Onedaysec
4 min read
0 views
docx image 1769396808297 0 1e8077e5c0

Penetration Technique: Remote Access to Exchange PowerShell

0x00 Preface

Exchange PowerShell is based on PowerShell Remoting, which usually requires accessing port 80 of the Exchange Server from a domain-joined host, with many restrictions. This article introduces an implementation method that does not rely on initiating connections from domain-joined hosts, expanding the scope of application.

Note:

This method was fixed in CVE-2022–41040. Fix location: RemoveExplicitLogonFromUrlAbsoluteUri(string absoluteUri, string explicitLogonAddress) in C:\Program Files\Microsoft\Exchange Server\V15\Bin\Microsoft.Exchange.HttpProxy.Common.dll, as shown in the following figure

【技术原创】渗透技巧——远程访问Exchange Powershell

0x01 Introduction

This article will introduce the following content:

Implementation Ideas

Implementation Details

0x02 Implementation Ideas

In conventional usage, the following issues need to be noted when using Exchange PowerShell:

All domain users can connect to Exchange PowerShell

Connections need to be initiated from a domain-joined host

The connection address needs to use FQDN; IP is not supported

Conventional usage cannot initiate connections from outside the domain, but as we know, connections can be initiated from outside the domain via ProxyShell, using SSRF to execute Exchange PowerShell

Furthermore, after applying the ProxyShell patch, the SSRF that supports NTLM authentication was not removed, and we can access Exchange Powershell again via NTLM authentication

0x03 Implementation Details

In code implementation, we can add NTLM authentication to pass credentials; example code:

【技术原创】渗透技巧——远程访问Exchange Powershell

When executing Exchange Powershell commands, we can choose pypsrp or Flask; for specific details, refer to previous articles: ProxyShell Exploitation Analysis 2 – CVE-2021-34523 and ProxyShell Exploitation Analysis 3 – Adding Users and File Writing

Both pypsrp and Flask achieve command execution by establishing a web proxy to filter and modify communication data

To increase the code's applicability, another implementation method is chosen here: simulate the normal communication data of Exchange Powershell to achieve command execution

Reference code available at: https://gist.github.com/rskvp93/4e353e709c340cb18185f82dbec30e58

The code uses Python2 and implements ProxyShell exploitation

Based on this code, rewrite it to support Python3, with the function of accessing Exchange Powershell via NTLM authentication to execute commands. The specific details to note are as follows:

1. There are differences in string formatting between Python2 and Python3

(1)

Code usable under Python2:

【技术原创】渗透技巧——远程访问Exchange Powershell

When using the above code under Python3, it is necessary to convert Str to bytes, and to avoid invisible character parsing issues, the code structure has been redesigned. Code usable under Python3:

【技术原创】渗透技巧——远程访问Exchange Powershell

(2)

Code usable under Python2:

【技术原创】渗透技巧——远程访问Exchange PowershellWhen using the above code in Python3, you need to convert Str to bytes. Example code available for Python3:

【技术原创】渗透技巧——远程访问Exchange Powershell

(3)

Code available for Python2:

【技术原创】渗透技巧——远程访问Exchange Powershell【技术原创】渗透技巧——远程访问Exchange Powershell

When using the above code in Python3, you need to convert Str to bytes. To avoid invisible character parsing issues, you cannot use .decode('utf-8') here; instead, use .decode('ISO-8859-1')

Example code available for Python3:

【技术原创】渗透技巧——远程访问Exchange Powershell

2. XML file format supporting Exchange Powershell commands

XML file format example 1:

【技术原创】渗透技巧——远程访问Exchange Powershell

The corresponding command to execute is: Get-RoleGroupMember "Organization Management"

XML file format example 2:

【技术原创】渗透技巧——远程访问Exchange Powershell

The corresponding command to execute is: Get-Mailbox -Identity administrator

Through format analysis, the following conclusions can be drawn:

(1) The Cmd attribute corresponds to the command name

For example:

【技术原创】渗透技巧——远程访问Exchange Powershell

(2) Pay attention to the format of the incoming command parameters

If only one parameter is passed in, the corresponding format is:

【技术原创】渗透技巧——远程访问Exchange PowershellIf 2 parameters are passed in, the corresponding format is:

【技术原创】渗透技巧——远程访问Exchange Powershell

If 4 parameters are passed in, the corresponding format is:

【技术原创】渗透技巧——远程访问Exchange PowershellTo this end, we can use the following code to implement parameter filling:

【技术原创】渗透技巧——远程访问Exchange PowershellImplementation code for constructing the XML file format:

【技术原创】渗透技巧——远程访问Exchange Powershell【技术原创】渗透技巧——远程访问Exchange Powershell【技术原创】渗透技巧——远程访问Exchange PowershellAfter combining the above details, we can get the final implementation code, and the execution result of the code is as shown in the following figure

【技术原创】渗透技巧——远程访问Exchange Powershell

0x04 Summary

This article introduces the implementation method of remote access to Exchange PowerShell. Its advantage is that it does not rely on initiating connections from hosts within the domain, and this method was fixed in CVE-2022–41040.

Related Questions & Answers

What vulnerability and fix are associated with this remote Exchange PowerShell technique?

The technique was fixed in CVE-2022–41040, which patched the `RemoveExplicitLogonFromUrlAbsoluteUri` method in Exchange's `Microsoft.Exchange.HttpProxy.Common.dll`. This patch prevents the NTLM-authenticated remote PowerShell access described in the [article](/news/penetration-technique-remote-access-to-exchange-powershell). The underlying issue is related to [NTLM over HTTP](/news/penetration-basics-implementation-of-webshell-supporting-ntlm-over-http-protocol) and ProxyShell’s SSRF bypasses.

How are Exchange PowerShell commands formatted in the XML file used for execution?

The XML file uses a `Cmd` attribute for the command name (e.g., `Get-Mailbox`) and structures parameters with nested elements. For one parameter it uses a single property, for two parameters it uses two `Property` elements, and for four parameters it uses four. Parameters are filled programmatically using a template. Example formats are shown in the [article](/news/penetration-technique-remote-access-to-exchange-powershell), such as `Get-RoleGroupMember "Organization Management"` or `Get-Mailbox -Identity administrator`.

What are the key code differences when adapting the technique from Python2 to Python3?

The main differences involve string and bytes handling. In Python2, strings could be used directly, but Python3 requires explicit conversion of `Str` to `bytes`. For example, `.decode('utf-8')` must be replaced with `.decode('ISO-8859-1')` to avoid invisible character parsing issues. The article provides specific code adjustments for these conversions, as detailed in the [original article](/news/penetration-technique-remote-access-to-exchange-powershell).

How does the implementation achieve credential passing without a domain-joined host?

The implementation adds NTLM authentication to pass credentials, enabling remote access to Exchange PowerShell from any host. It builds upon techniques from [ProxyShell exploitation](/news/penetration-technique-python-implementation-of-exchange-powershell) and uses either pypsrp or Flask as a web proxy to filter and modify communication data, or simulates normal Exchange PowerShell communication data directly.

What is the main advantage of the remote Exchange PowerShell access technique described in the article?

The technique allows executing Exchange PowerShell commands without requiring a domain-joined host or FQDN, expanding attack surface beyond conventional methods. It leverages [NTLM authentication](/news/penetration-technique-remote-access-to-exchange-powershell) and bypasses restrictions fixed in CVE-2022–41040. This approach is particularly useful for post-ProxyShell scenarios where SSRF is patched but NTLM-enabled remote PowerShell remains accessible.

Continue Reading