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

admin 2023年5月18日18:34:12评论51 views字数 1515阅读5分3秒阅读模式

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

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

远程执行Exchange Powershell命令可以通过Powershell建立powershell session 实现。而在渗透测试中,我们需要尽可能避免使用Powershell,而是通过程序去实现。本文将要介绍通过Python实现远程执行Exchange Powershell命令的细节,分享使用Python实现TabShell利用的心得。

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

本文件将介绍以下内容:

执行 Exchange Powershell 命令的实际方法

开发细节

TabShell利用细节

【技术原创】渗透技巧——Exchange Powershell的Python实现        0x02 执行 Exchange Powershell 命令的实际方法

1.使用Powershell连接Exchange服务器,执行Exchange Powershell命令

命令示例:

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

需要注意以下问题:

需要域内主机上执行

需要fqdn,不支持IP

连接url可以选择http或者https

认证方式可以选择Basic或者Kerberos

2.使用Python连接Exchange服务器,执行Exchange Powershell命令

这里需要使用pypsrp

命令示例:

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

【技术原创】渗透技巧——Exchange Powershell的Python实现        0x03 开发细节

这里需要了解具体的通信格式,我采用的方法是使用pypsrp,打开调试信息,查看具体发送的数据格式

1.启动调试信息

将调试信息写到文件,代码如下:

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

2.增加调试输出内容

修改文件pypsrp/wsman.py,在def send(self, message: bytes)中添加调试输出信息

具体代号位置:

https://github.com/jborean93/pypsrp/blob/master/src/pypsrp/wsman.py#L834,添加代码:

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

https://github.com/jborean93/pypsrp/blob/master/src/pypsrp/wsman.py#L841,添加代码:

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

输出结果显示如下图

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

3.数据包数据结构

可参考之前的文章《渗透技巧——远程访问Exchange Powershell》

经过对比分析,在编写程序上还需要注意以下细节:

(1)Kerberos认证的实际情况

示例代码:

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

(2)通信数据格式

类型为POST

header需要包裹:'Accept-Encoding': 'identity'

(3)认证流程

需要先进行Kerberos认证,返回长度为0

再次发送数据,进行通信,返回正常内容

(4)数据编码

发送和接收的数据平均做了编码

发送过程序的代码显示示例代码:

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

注:

hostname必须为小写字符

接收过程序的解码示例代码:

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

完整展示示例代码如下:

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

完整代码的输出结果如下图

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

【技术原创】渗透技巧——Exchange Powershell的Python实现        0x04 TabShell利用细节

TabShell的公开POC使用Powershell连接取接Exchange服务器,执行特殊构造的Exchange Powershell命令接触,为便于分析中间的通信数据,可以采用以下方法擦拭中间:

1.通过Flask构建本地代理服务器

方法可参考之前的文章《ProxyShell利用分析3——添加用户和文件写入》

2.通过Flask实现SSRF

SSRF漏洞可选择CVE-2022-41040或CVE-2022-41080

3.在Flask中输出中间的通信数据

关键字代码示例:

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

根据通信数据,我们可以很容易地写出TabShell的Python现代代码,完整代码的输出结果如下图

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

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

本文件介绍了通过 Python 实现远程执行 Exchange Powershell 命令的细节,分享使用 Python 实现 TabShell 使用的心得。

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

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

原文始发于微信公众号(嘶吼专业版):【技术原创】渗透技巧——Exchange Powershell的Python实现

  • 左青龙
  • 微信扫一扫
  • weinxin
  • 右白虎
  • 微信扫一扫
  • weinxin
admin
  • 本文由 发表于 2023年5月18日18:34:12
  • 转载请保留本文链接(CN-SEC中文网:感谢原作者辛苦付出):
                   【技术原创】渗透技巧——Exchange Powershell的Python实现https://cn-sec.com/archives/1743285.html

发表评论

匿名网友 填写信息