PowerShell执行策略是脚本安全的第一道防线,通过灵活配置平衡功能与安全。
1 2 3 4 5
| Get-ExecutionPolicy -List
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
|
脚本签名验证
- 创建代码签名证书:
1 2 3 4 5 6 7
| $certParams = @{ Subject = 'CN=PowerShell Scripts' Type = 'CodeSigning' KeyUsage = 'DigitalSignature' KeyLength = 2048 } $cert = New-SelfSignedCertificate @certParams
|
- 签名脚本文件:
1 2 3 4 5 6
| $signParams = @{ Certificate = $cert FilePath = 'script.ps1' TimestampServer = 'http://timestamp.digicert.com' } Set-AuthenticodeSignature @signParams
|
安全日志分析
1 2 3 4 5 6 7
| Get-WinEvent -FilterHashtable @{ LogName = 'Microsoft-Windows-PowerShell/Operational' Id = 4104 } | Where-Object { $_.Message -match '可疑命令' }
|
最佳实践:
- 使用AllSigned策略生产环境
- 定期轮换签名证书
- 启用脚本块日志记录
- 结合AppLocker增强控制