自动化零信任设备健康检查

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
function Get-DeviceCompliance {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$ComputerName
)

# 验证TPM状态
$tpmStatus = Get-Tpm -ComputerName $ComputerName -ErrorAction SilentlyContinue
# 检查BitLocker加密状态
$bitlocker = Get-BitLockerVolume -MountPoint $env:SystemDrive -ErrorAction SilentlyContinue
# 获取防病毒状态
$avStatus = Get-MpComputerStatus -ErrorAction SilentlyContinue

[PSCustomObject]@{
ComputerName = $ComputerName
TPMEnabled = $tpmStatus.TpmPresent
SecureBoot = (Confirm-SecureBootUEFI).SecureBootEnabled
BitLockerStatus = $bitlocker.VolumeStatus
AntivirusEnabled = $avStatus.AMServiceEnabled
LastUpdate = (Get-HotFix | Sort-Object InstalledOn -Descending | Select-Object -First 1).InstalledOn
}
}

# 执行企业终端健康检查
$devices = 'PC001','PC002','PC003'
$report = $devices | ForEach-Object {
Get-DeviceCompliance -ComputerName $_ -Verbose
}

# 生成合规性报告
$report | Export-Csv -Path "ZeroTrust_Compliance_Report_$(Get-Date -Format yyyyMMdd).csv" -NoTypeInformation

本脚本实现以下零信任核心检查项:

  1. TPM芯片启用状态验证
  2. Secure Boot安全启动配置
  3. 系统盘BitLocker加密状态
  4. 防病毒实时监控状态
  5. 系统最后更新日期

扩展建议:

  • 与Azure AD条件访问策略集成
  • 添加自动修复功能
  • 实现实时监控告警机制

PowerShell 技能连载 - 安全策略配置指南

PowerShell执行策略是脚本安全的第一道防线,通过灵活配置平衡功能与安全。

1
2
3
4
5
# 查看当前执行策略
Get-ExecutionPolicy -List

# 设置远程签名策略
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser

脚本签名验证

  1. 创建代码签名证书:

    1
    2
    3
    4
    5
    6
    7
    $certParams = @{
    Subject = 'CN=PowerShell Scripts'
    Type = 'CodeSigning'
    KeyUsage = 'DigitalSignature'
    KeyLength = 2048
    }
    $cert = New-SelfSignedCertificate @certParams
  2. 签名脚本文件:

    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增强控制

PowerShell实现供应链安全自动化扫描

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function Invoke-ModuleVulnerabilityScan {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[string]$ModuleName
)

# 获取模块版本信息
$module = Get-InstalledModule -Name $ModuleName -ErrorAction Stop

# 调用漏洞数据库API
$response = Invoke-RestMethod -Uri "https://vulndb.example.com/api/modules/$($module.Name)/$($module.Version)"

# 生成安全报告
[PSCustomObject]@{
ModuleName = $module.Name
Version = $module.Version
Vulnerabilities = $response.vulns.Count
Critical = $response.vulns | Where-Object { $_.severity -eq 'Critical' } | Measure-Object | Select-Object -Expand Count
LastUpdated = $module.PublishedDate
} | Export-Csv -Path "$env:TEMP\ModuleSecurityScan_$(Get-Date -Format yyyyMMdd).csv" -Append
}

# 扫描常用模块
'PSReadLine', 'Pester', 'Az' | ForEach-Object {
Invoke-ModuleVulnerabilityScan -ModuleName $_ -Verbose
}

核心功能:

  1. 自动化检测已安装PowerShell模块版本
  2. 对接漏洞数据库API进行安全检查
  3. 生成包含严重性等级的安全报告

扩展方向:

  1. 集成软件物料清单(SBOM)生成
  2. 添加自动补丁更新功能
  3. 与CI/CD流水线集成实现预发布扫描