Skip to content

安全防护

Windows 安全是保护计算机和数据的重要环节。本章介绍 Windows 系统的安全功能和防护措施。

Windows 安全概述

安全威胁类型

威胁类型说明防护措施
病毒感染文件的恶意程序杀毒软件
木马伪装成正常程序安全意识、扫描
勒索软件加密文件勒索赎金备份、防护软件
钓鱼攻击诱骗获取信息安全意识
网络攻击远程入侵系统防火墙、更新

Windows 安全中心

powershell
# 打开 Windows 安全中心
Start-Process "windowsdefender:"

# 或通过设置
start ms-settings:windowsdefender

# 安全中心功能模块
# - 病毒和威胁防护
# - 账户保护
# - 防火墙和网络保护
# - 应用和浏览器控制
# - 设备安全性
# - 设备性能和运行状况
# - 家庭选项

Windows Defender 防病毒

查看防护状态

powershell
# 查看 Windows Defender 状态
Get-MpComputerStatus

# 查看关键状态信息
Get-MpComputerStatus | Select-Object AntivirusEnabled, RealTimeProtectionEnabled, IoavProtectionEnabled, AntispywareEnabled

# 查看病毒定义版本
Get-MpComputerStatus | Select-Object AMServiceVersion, AntispywareSignatureVersion, AntivirusSignatureVersion

更新病毒定义

powershell
# 更新病毒定义
Update-MpSignature

# 查看更新源
Get-MpPreference | Select-Object SignatureFallbackOrder, SignatureDefinitionUpdateFileSharesSources

# 从特定源更新
Update-MpSignature -UpdateSource MicrosoftUpdateServer

扫描系统

powershell
# 快速扫描
Start-MpScan -ScanType QuickScan

# 完全扫描
Start-MpScan -ScanType FullScan

# 自定义扫描
Start-MpScan -ScanType CustomScan -ScanPath "C:\SuspiciousFolder"

# 查看扫描历史
Get-MpThreatDetection

# 查看检测到的威胁
Get-MpThreat

配置防护设置

powershell
# 查看当前配置
Get-MpPreference

# 启用实时保护
Set-MpPreference -DisableRealtimeMonitoring $false

# 启用云保护
Set-MpPreference -MAPSReporting 2

# 启用自动样本提交
Set-MpPreference -SubmitSamplesConsent 3

# 添加排除项
# 排除文件类型
Add-MpPreference -ExclusionExtension "tmp"

# 排除文件夹
Add-MpPreference -ExclusionPath "C:\MyApp"

# 排除进程
Add-MpPreference -ExclusionProcess "myapp.exe"

# 移除排除项
Remove-MpPreference -ExclusionPath "C:\MyApp"

处理威胁

powershell
# 查看威胁详情
Get-MpThreat | Format-List *

# 隔离威胁
Remove-MpThreat

# 恢复隔离的文件
Get-MpThreatDetection | ForEach-Object { 
    $_ | Select-Object Resources, ActionSuccess 
}

# 查看隔离区
Get-MpThreatDetection | Where-Object { $_.ActionId -eq 2 }

Windows 防火墙

防火墙概述

Windows 防火墙通过规则控制网络流量:

text
防火墙配置文件:
├── 域配置文件 - 域网络环境
├── 专用配置文件 - 家庭或工作网络
└── 公用配置文件 - 公共网络环境

规则类型:
├── 入站规则 - 控制进入的连接
├── 出站规则 - 控制外出的连接
└── 连接安全规则 - 配置安全连接

查看防火墙状态

powershell
# 查看防火墙状态
Get-NetFirewallProfile

# 查看所有配置文件状态
Get-NetFirewallProfile | Select-Object Name, Enabled

# 查看特定配置文件
Get-NetFirewallProfile -Name Public

# 使用 netsh 命令
netsh advfirewall show allprofiles
netsh advfirewall show currentprofile

启用/禁用防火墙

powershell
# 启用所有配置文件的防火墙
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True

# 禁用防火墙(不推荐)
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False

# 启用特定配置文件
Set-NetFirewallProfile -Profile Private -Enabled True

# 使用 netsh 命令
netsh advfirewall set allprofiles state on
netsh advfirewall set allprofiles state off

管理防火墙规则

powershell
# 查看所有规则
Get-NetFirewallRule

# 查看启用的入站规则
Get-NetFirewallRule -Direction Inbound -Enabled True -Action Allow

# 查看特定程序的规则
Get-NetFirewallRule -DisplayName "*Chrome*"

# 查看规则详情
Get-NetFirewallRule -DisplayName "允许端口 80" | Get-NetFirewallPortFilter

# 查看规则的端口信息
Get-NetFirewallRule | Get-NetFirewallPortFilter | Where-Object { $_.LocalPort -eq 80 }

创建防火墙规则

powershell
# 创建入站规则 - 允许端口
New-NetFirewallRule -DisplayName "允许端口 8080" `
    -Direction Inbound `
    -LocalPort 8080 `
    -Protocol TCP `
    -Action Allow `
    -Enabled True

# 创建入站规则 - 允许程序
New-NetFirewallRule -DisplayName "允许程序" `
    -Direction Inbound `
    -Program "C:\Program Files\MyApp\app.exe" `
    -Action Allow

# 创建出站规则 - 阻止程序联网
New-NetFirewallRule -DisplayName "阻止程序联网" `
    -Direction Outbound `
    -Program "C:\Program Files\MyApp\app.exe" `
    -Action Block

# 创建规则并指定配置文件
New-NetFirewallRule -DisplayName "允许远程桌面" `
    -Direction Inbound `
    -LocalPort 3389 `
    -Protocol TCP `
    -Action Allow `
    -Profile Private

# 使用 netsh 创建规则
netsh advfirewall firewall add rule name="允许端口 8080" dir=in action=allow protocol=tcp localport=8080

修改和删除规则

powershell
# 修改规则
Set-NetFirewallRule -DisplayName "允许端口 8080" -Enabled False

# 禁用规则
Disable-NetFirewallRule -DisplayName "允许端口 8080"

# 启用规则
Enable-NetFirewallRule -DisplayName "允许端口 8080"

# 删除规则
Remove-NetFirewallRule -DisplayName "允许端口 8080"

# 使用 netsh 删除规则
netsh advfirewall firewall delete rule name="允许端口 8080"

重置防火墙

powershell
# 重置防火墙到默认设置
netsh advfirewall reset

# 恢复默认策略
Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow

用户账户控制(UAC)

UAC 概述

用户账户控制(User Account Control)防止未经授权的系统更改:

text
UAC 工作原理:
├── 管理员批准模式
├── 标准用户权限运行
├── 需要时提升权限
└── 弹出确认对话框

配置 UAC

powershell
# 通过控制面板配置
# 控制面板 → 用户账户 → 更改用户账户控制设置

# 通过注册表配置
# UAC 级别(ConsentPromptBehaviorAdmin):
# 0 - 不通知(不推荐)
# 1 - 仅当程序尝试更改时通知(不降低桌面)
# 2 - 仅当程序尝试更改时通知(默认)
# 3 - 始终通知
# 5 - 始终通知(最安全)

Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" `
    -Name "ConsentPromptBehaviorAdmin" -Value 5

# 启用/禁用 UAC
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" `
    -Name "EnableLUA" -Value 1  # 1=启用, 0=禁用(不推荐)

BitLocker 驱动器加密

BitLocker 概述

BitLocker 是 Windows 内置的磁盘加密功能:

  • 加密整个驱动器
  • 保护数据安全
  • 支持 TPM 芯片
  • 多种解锁方式

启用 BitLocker

powershell
# 查看 BitLocker 状态
Get-BitLockerVolume

# 检查是否支持 BitLocker
Get-ComputerInfo -Property BitLocker*

# 启用 BitLocker(使用 TPM)
Enable-BitLocker -MountPoint "C:" -EncryptionMethod XtsAes256 -UsedSpaceOnly -TpmProtector

# 启用 BitLocker(使用密码)
Enable-BitLocker -MountPoint "D:" -EncryptionMethod XtsAes256 -PasswordProtector

# 启用 BitLocker(使用恢复密钥)
Enable-BitLocker -MountPoint "D:" -EncryptionMethod XtsAes256 -RecoveryKeyProtector

# 备份恢复密钥到文件
$volume = Get-BitLockerVolume -MountPoint "C:"
$keyProtector = $volume.KeyProtector | Where-Object { $_.KeyProtectorType -eq "RecoveryPassword" }
Backup-BitLockerKeyProtector -MountPoint "C:" -KeyProtectorId $keyProtector.KeyProtectorId

管理 BitLocker

powershell
# 查看加密状态
Get-BitLockerVolume | Select-Object MountPoint, VolumeStatus, EncryptionPercentage, ProtectionStatus

# 暂停加密
Suspend-BitLocker -MountPoint "C:"

# 恢复加密
Resume-BitLocker -MountPoint "C:"

# 解锁驱动器
Unlock-BitLocker -MountPoint "D:" -Password

# 禁用 BitLocker
Disable-BitLocker -MountPoint "C:"

# 完全解密
Disable-BitLocker -MountPoint "D:" -RemoveKeyProtector

Windows 更新安全

配置 Windows 更新

powershell
# 打开 Windows 更新设置
start ms-settings:windowsupdate

# 查看更新历史
Get-WuHistory

# 安装安全更新
Install-WindowsUpdate -Category "Security Updates" -AcceptAll

# 查看待安装的更新
Get-WindowsUpdate

# 仅安装重要更新
Get-WindowsUpdate -Category "Critical Updates" | Install-WindowsUpdate -AcceptAll

更新安全设置

powershell
# 配置自动更新(通过注册表)
# 0 - 从不检查更新(不推荐)
# 1 - 检查更新但让我选择是否下载和安装
# 2 - 下载更新但让我选择是否安装
# 3 - 自动安装更新(推荐)
# 4 - 自动安装更新并重启

Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" `
    -Name "AUOptions" -Value 3 -Type DWord

# 启用自动更新
Set-ItemProperty -Path "HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU" `
    -Name "NoAutoUpdate" -Value 0 -Type DWord

网络安全

网络安全设置

powershell
# 查看网络配置文件
Get-NetConnectionProfile

# 更改网络配置文件类型
Set-NetConnectionProfile -InterfaceAlias "以太网" -NetworkCategory Private

# 禁用网络发现
Set-NetFirewallRule -DisplayGroup "Network Discovery" -Enabled False

# 启用网络发现
Set-NetFirewallRule -DisplayGroup "Network Discovery" -Enabled True

# 禁用文件和打印机共享
Set-NetFirewallRule -DisplayGroup "File and Printer Sharing" -Enabled False

安全网络连接

powershell
# 查看 Wi-Fi 安全类型
netsh wlan show profiles

# 查看网络密码(仅限已保存的网络)
netsh wlan show profile name="网络名称" key=clear

# 删除不安全的网络配置
netsh wlan delete profile name="网络名称"

# 禁用自动连接
netsh wlan set profileparameter name="网络名称" connectionmode=manual

安全策略

本地安全策略

powershell
# 打开本地安全策略
secpol.msc

# 常见安全策略设置
# 账户策略 → 密码策略
# 账户策略 → 账户锁定策略
# 本地策略 → 审核策略
# 本地策略 → 用户权限分配
# 本地策略 → 安全选项

密码策略

powershell
# 查看密码策略
net accounts

# 设置密码最短长度
net accounts /minpwlen:8

# 设置密码复杂度要求
# 需要通过组策略或 secedit 命令

# 设置密码最长使用期限(天)
net accounts /maxpwage:90

# 设置账户锁定阈值
net accounts /lockoutthreshold:5

# 设置账户锁定时间(分钟)
net accounts /lockoutduration:30

审核策略

powershell
# 启用审核策略
# 需要使用 secpol.msc 或 auditpol 命令

# 查看审核策略
auditpol /get /category:*

# 启用登录审核
auditpol /set /subcategory:"Logon" /success:enable /failure:enable

# 启用对象访问审核
auditpol /set /subcategory:"File System" /success:enable /failure:enable

安全最佳实践

系统安全清单

text
基础安全措施:
├── 保持系统和软件更新
├── 启用 Windows Defender 实时保护
├── 启用防火墙
├── 使用强密码
├── 启用 UAC
├── 定期备份数据
├── 加密敏感数据
└── 谨慎处理邮件和下载

安全检查脚本

powershell
# 系统安全检查脚本
function Test-SystemSecurity {
    Write-Host "=== Windows 系统安全检查 ===" -ForegroundColor Cyan
    
    # 检查 Windows Defender
    $defender = Get-MpComputerStatus
    Write-Host "`n[Windows Defender]"
    Write-Host "  防病毒启用: $($defender.AntivirusEnabled)"
    Write-Host "  实时保护: $($defender.RealTimeProtectionEnabled)"
    Write-Host "  病毒定义版本: $($defender.AntivirusSignatureVersion)"
    
    # 检查防火墙
    Write-Host "`n[防火墙]"
    Get-NetFirewallProfile | ForEach-Object {
        Write-Host "  $($_.Name): $($_.Enabled)"
    }
    
    # 检查 UAC
    Write-Host "`n[用户账户控制]"
    $uac = Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System"
    Write-Host "  UAC 启用: $($uac.EnableLUA -eq 1)"
    Write-Host "  通知级别: $($uac.ConsentPromptBehaviorAdmin)"
    
    # 检查 Windows 更新
    Write-Host "`n[Windows 更新]"
    $updateService = Get-Service -Name wuauserv
    Write-Host "  更新服务状态: $($updateService.Status)"
    
    # 检查 BitLocker
    Write-Host "`n[BitLocker]"
    Get-BitLockerVolume | ForEach-Object {
        Write-Host "  $($_.MountPoint): $($_.ProtectionStatus)"
    }
    
    # 检查管理员账户
    Write-Host "`n[管理员账户]"
    Get-LocalGroupMember -Group "Administrators" | ForEach-Object {
        Write-Host "  $($_.Name)"
    }
}

# 运行检查
Test-SystemSecurity

定期安全维护

powershell
# 定期执行的安全维护任务

# 1. 更新病毒定义
Update-MpSignature

# 2. 运行快速扫描
Start-MpScan -ScanType QuickScan

# 3. 清理临时文件
Remove-Item -Path "$env:TEMP\*" -Recurse -Force -ErrorAction SilentlyContinue

# 4. 检查系统更新
Get-WindowsUpdate

# 5. 检查磁盘空间
Get-PSDrive -PSProvider FileSystem | Where-Object { $_.Free -lt 1GB }

# 6. 检查服务状态
Get-Service | Where-Object { $_.StartType -eq "Automatic" -and $_.Status -eq "Stopped" }

小结

本章介绍了 Windows 安全防护的主要内容:

  1. 安全概述:了解常见安全威胁和防护措施
  2. Windows Defender:使用内置防病毒软件
  3. 防火墙:配置网络访问控制
  4. UAC:管理用户账户控制设置
  5. BitLocker:加密保护磁盘数据
  6. Windows 更新:保持系统安全更新
  7. 网络安全:配置网络安全设置
  8. 安全策略:设置密码和审核策略
  9. 最佳实践:日常安全维护建议

建议:

  • 始终保持 Windows Defender 启用
  • 定期更新系统和软件
  • 使用强密码并启用多因素认证
  • 不要禁用 UAC
  • 加密存储敏感数据
  • 定期备份重要文件
  • 提高安全意识,警惕钓鱼攻击