Penetration Technique: Python Implementation of Exchange PowerShell

Onedaysec
3 min read
0 views
docx image 1769396802076 0 f7fde612b1

Penetration Technique: Python Implementation of Exchange PowerShell

0x00 Preface

Remote execution of Exchange PowerShell commands can be achieved by establishing a PowerShell session using PowerShell. However, in penetration testing, we need to avoid using PowerShell as much as possible and instead implement it through programs. This article will introduce the details of remotely executing Exchange PowerShell commands via Python and share insights on exploiting TabShell using Python.

0x01 Introduction

This document will cover the following content:

Practical Methods for Executing Exchange PowerShell Commands

Development Details

TabShell Exploitation Details

0x02 Practical Methods for Executing Exchange PowerShell Commands

1. Connect to the Exchange server using PowerShell and execute Exchange PowerShell commands

Command Example:

【技术原创】渗透技巧——Exchange Powershell的Python实现

The following issues need attention:

Must be executed on a domain-joined host

Requires FQDN; IP is not supported

The connection URL can use HTTP or HTTPS

Authentication method can be Basic or Kerberos

2. Use Python to connect to the Exchange server and execute Exchange PowerShell commands

We need to use pypsrp here

Command example:

【技术原创】渗透技巧——Exchange Powershell的Python实现

0x03 Development Details

We need to understand the specific communication format here. The method I adopted is to use pypsrp, enable debug information, and check the specific data format sent.

1. Enable debug information

Write debug information to a file, the code is as follows:

【技术原创】渗透技巧——Exchange Powershell的Python实现

2. Add debug output content

Modify the file pypsrp/wsman.py and add debug output information in the def send(self, message: bytes) method

Specific code location:

https://github.com/jborean93/pypsrp/blob/master/src/pypsrp/wsman.py#L834, add the code:

【技术原创】渗透技巧——Exchange Powershell的Python实现https://github.com/jborean93/pypsrp/blob/master/src/pypsrp/wsman.py#L841, add the code:

【技术原创】渗透技巧——Exchange Powershell的Python实现The output result is shown in the following figure

【技术原创】渗透技巧——Exchange Powershell的Python实现

3. Data Packet Data Structure

You can refer to the previous article "Penetration Techniques – Remote Access to Exchange PowerShell"

After comparative analysis, the following details need to be noted when writing the program:

(1) Actual situation of Kerberos authentication

Sample code:

【技术原创】渗透技巧——Exchange Powershell的Python实现

(2) Communication data format

Type is POST

The header needs to include: 'Accept-Encoding': 'identity'

(3) Authentication process

You need to perform Kerberos authentication first, which returns a length of 0

Send data again to communicate, and normal content will be returned

(4) Data encoding

Both the sent and received data are encoded

The code for the sending process is shown in the sample code:

【技术原创】渗透技巧——Exchange Powershell的Python实现

Note:

The hostname must be in lowercase characters

Sample decoding code for the receiving process:

【技术原创】渗透技巧——Exchange Powershell的Python实现The complete sample code is shown below:

【技术原创】渗透技巧——Exchange Powershell的Python实现【技术原创】渗透技巧——Exchange Powershell的Python实现The output result of the complete code is shown in the following figure

【技术原创】渗透技巧——Exchange Powershell的Python实现

0x04 TabShell Exploitation Details

The public POC of TabShell uses PowerShell to connect to the Exchange server and execute specially constructed Exchange PowerShell commands. To facilitate the analysis of intermediate communication data, the following methods can be used to intercept the intermediate data:

1. Build a local proxy server via Flask

The method can refer to the previous article "ProxyShell Exploitation Analysis 3 - Adding Users and File Writing"

2. Implement SSRF via Flask

For the SSRF vulnerability, you can choose CVE-2022-41040 or CVE-2022-41080

3. Output intermediate communication data in Flask

Key code example:

【技术原创】渗透技巧——Exchange Powershell的Python实现Based on the communication data, we can easily write the Python implementation code for TabShell. The output result of the complete code is shown in the following figure

【技术原创】渗透技巧——Exchange Powershell的Python实现

0x05 Summary

This document introduces the details of remotely executing Exchange PowerShell commands via Python and shares the experience of using Python to implement TabShell.

Related Questions & Answers

How did the author use Python to exploit TabShell, and what tools were used to analyze the communication?

The author used a local Flask proxy to intercept the communication data from a PowerShell-based TabShell POC (exploiting CVE-2022-41040 or CVE-2022-41080 via SSRF). By capturing the raw packets, they understood the payload format and wrote a Python script to replicate the exploit. This method is similar to the approach in [Penetration Technique: Python Implementation of Exchange PowerShell](/news/penetration-technique-python-implementation-of-exchange-powershell) and reduces reliance on PowerShell for post-exploitation.

What are the key considerations when implementing Kerberos authentication for Exchange PowerShell via Python?

You must first perform Kerberos authentication, which returns a zero-length response, then resend the data to get normal content. The hostname must be in lowercase, and the POST request must include the header 'Accept-Encoding: identity'. Both sent and received data are encoded—the article provides sample encoding/decoding code. This process is essential for correctly implementing the authentication flow seen in [Penetration Technique: Remote Access to Exchange PowerShell](/news/penetration-technique-remote-access-to-exchange-powershell).

How can you capture and analyze the raw communication data when developing a Python client for Exchange PowerShell?

Enable debug output in the pypsrp library by modifying wsman.py to log the `send` method's data. Alternatively, use a local Flask proxy to intercept traffic between a PowerShell TabShell POC and the Exchange server, as described in the article. This allows you to replicate the exact data structure in Python—a technique also used in [ProxyShell Exploitation Analysis](/news/penetration-technique-python-implementation-of-exchange-powershell).

Why should we use Python instead of PowerShell for executing Exchange PowerShell commands during penetration testing?

Using PowerShell directly can trigger security monitoring solutions, so implementing it via a program like Python reduces detection risk. The article demonstrates using the pypsrp library to connect to an Exchange server, authenticate (Kerberos or Basic), and execute commands without invoking PowerShell. This approach is detailed in [Penetration Technique: Python Implementation of Exchange PowerShell](/news/penetration-technique-python-implementation-of-exchange-powershell).

Continue Reading