본문 바로가기

Windows

PowerShell로 Windows Firewall 규칙 관리하기

Windows Defender Firewall은 GUI 없이도 PowerShell로 빠르게 관리할 수 있다. 아래는 NetSecurity 모듈 기준의 핵심 명령만 추린 정리다.

1. 프로필 상태 확인

Get-NetFirewallProfile | Format-Table Name, Enabled

Domain, Private, Public 프로필이 켜져 있는지 먼저 확인한다.

2. 방화벽 켜기/끄기

Set-NetFirewallProfile -All -Enabled False
Set-NetFirewallProfile -All -Enabled True
Set-NetFirewallProfile -Profile Public -Enabled False

3. 새 규칙 만들기

New-NetFirewallRule -DisplayName "HTTP-Inbound" -Profile @("Domain","Private") -Direction Inbound -Action Allow -Protocol TCP -LocalPort @("80","443")
New-NetFirewallRule -Program "C:\Program Files (x86)\Mozilla Firefox\firefox.exe" -Action Block -Profile Domain,Private -DisplayName "Block Firefox browser" -Direction Outbound
New-NetFirewallRule -DisplayName "Allow inbound ICMPv4" -Direction Inbound -Protocol ICMPv4 -IcmpType 8 -Action Allow

4. 규칙 수정/활성화/삭제

Get-NetFirewallRule -DisplayName "HTTP-Inbound" | Set-NetFirewallRule -RemoteAddress 192.168.1.10
Disable-NetFirewallRule -DisplayName "HTTP-Inbound"
Enable-NetFirewallRule -Name FPS-ICMP4-ERQ-In
Remove-NetFirewallRule -DisplayName "HTTP-Inbound"

5. 활성 규칙과 세부 정보 보기

Get-NetFirewallRule | Where-Object { $_.Enabled -eq $True -and $_.Direction -eq "Inbound" } | Format-Table
Get-NetFirewallRule -Action Allow -Enabled True -Direction Inbound |
 Format-Table -Property Name,
 @{Name="Protocol";Expression={($_ | Get-NetFirewallPortFilter).Protocol}},
 @{Name="LocalPort";Expression={($_ | Get-NetFirewallPortFilter).LocalPort}},
 @{Name="RemotePort";Expression={($_ | Get-NetFirewallPortFilter).RemotePort}},
 @{Name="RemoteAddress";Expression={($_ | Get-NetFirewallAddressFilter).RemoteAddress}},
 Enabled,Profile,Direction,Action

6. 백업과 복원

netsh advfirewall export "C:\Backup\firewall-config.wfw"
netsh advfirewall import "C:\Backup\firewall-config.wfw"
netsh advfirewall reset

GUI보다 반복 작업에 훨씬 유리하고, 원격 서버도 PowerShell Remoting으로 일괄 관리할 수 있다.

원문: Managing Windows Firewall Rules with PowerShell