自 Windows XP SP2 引入「Windows 防火牆」取代舊有 ICF (Internet Connection Firewall),後續版本持續增強:
| 方法 | 指令/路徑 | 適用版本 |
|---|---|---|
| 控制台 | 控制台 → 系統與安全性 → Windows Defender 防火牆 | Vista/7/8/10/11 |
| 執行指令 | firewall.cpl | 所有版本 |
| 進階安全性 | wf.msc | Vista/7/8/10/11 |
| PowerShell | Get-NetFirewallProfile, Get-NetFirewallRule | Win 8/Server 2012+ |
| 設定 App (Win10/11) | 設定 → 隱私權與安全性 → Windows 安全性 → 防火牆與網路保護 | Win 10/11 |
注意: 若群組原則強制設定,本頁選項會變灰無法修改,需檢查 GPO。
C:\Program Files\MyApp\server.exe)。%systemroot%\system32\LogFiles\Firewall\pfirewall.log) 與大小上限 (預設 4 MB)。預設全阻擋。常用啟用項目:
點選「還原預設原則」→ 確認。將防火牆恢復安裝初始狀態:所有設定檔啟用、預設例外啟用、日誌關閉、ICMP 全阻擋、所有介面受保護。
# 查看目前設定檔狀態
netsh advfirewall show allprofiles
# 啟用/關閉特定設定檔
netsh advfirewall set domainprofile state on
netsh advfirewall set privateprofile state on
netsh advfirewall set publicprofile state on
# 新增輸入規則允許 TCP 8080 (僅限子網)
netsh advfirewall firewall add rule name="Allow TCP 8080 LAN" dir=in action=allow protocol=TCP localport=8080 remoteip=192.168.1.0/24 profile=private
# 新增輸出規則阻擋特定程式連網
netsh advfirewall firewall add rule name="Block Chrome Update" dir=out action=block program="C:\Program Files\Google\Chrome\Application\chrome.exe"
# 匯出/匯入原則 (含所有規則)
netsh advfirewall export "C:\Backup\FW_Policy.wfw"
netsh advfirewall import "C:\Backup\FW_Policy.wfw"
# 重設為預設
netsh advfirewall reset
# 取得所有規則
Get-NetFirewallRule | Format-Table Name, Enabled, Direction, Action, Profile -AutoSize
# 依程式路徑尋找規則
Get-NetFirewallRule -Program "C:\Program Files\MyApp\server.exe"
# 新增規則 (物件管線)
New-NetFirewallRule -DisplayName "Allow MyApp In" -Direction Inbound -Action Allow -Program "C:\Program Files\MyApp\server.exe" -Profile Private -LocalPort 8080 -Protocol TCP -RemoteAddress 192.168.1.0/24
# 批次啟用/停用
Get-NetFirewallRule -DisplayGroup "File and Printer Sharing" | Enable-NetFirewallRule
Get-NetFirewallRule -DisplayGroup "Remote Desktop" | Disable-NetFirewallRule
gpedit.msc (本機) 或 GPMC (域控) 編輯 GPO。%windir%\system32\LogFiles\Firewall\pfirewall.log。可用記事本/Excel/Log Parser Studio 分析。欄位說明見 Microsoft 文件「Windows Firewall Log File Format」。netsh advfirewall set allprofiles state off (測試完畢立即 on),或控制台介面關閉。切勿長期關閉。