控制面板 → 系統與安全性 → Windows 防火牆 → 左側「進階設定」或執行 wf.msc。
三個設定檔:網域、私人、公用。對每個設定檔可獨立設定:
80,443,8080-8090、允許連線、套用至「私人、網域」。C:\Program Files\App\server.exe、允許、僅限安全連線(需 IPsec)。192.168.1.0/24)。應用程式與服務記錄 → Microsoft → Windows → Windows Firewall with Advanced Security。netsh advfirewall show allprofiles
netsh advfirewall set allprofiles state on
netsh advfirewall set currentprofile state off
# 允許 TCP 80 入站
netsh advfirewall firewall add rule name="Allow HTTP" dir=in action=allow protocol=TCP localport=80 profile=domain,private
# 允許特定程式出站
netsh advfirewall firewall add rule name="MyApp Out" dir=out action=allow program="C:\App\myapp.exe"
# 封鎖特定 IP 入站
netsh advfirewall firewall add rule name="Block BadIP" dir=in action=block remoteip=203.0.113.50
# 刪除規則
netsh advfirewall firewall delete rule name="Allow HTTP"
# 匯出/匯入原則
netsh advfirewall export "C:\fw-policy.wfw"
netsh advfirewall import "C:\fw-policy.wfw"
# 允許 IPv4 Ping 回應
netsh advfirewall firewall add rule name="Allow ICMPv4 Echo" protocol=icmpv4:8,any dir=in action=allow
# 允許 IPv6 Ping
netsh advfirewall firewall add rule name="Allow ICMPv6 Echo" protocol=icmpv6:128,any dir=in action=allow
# 要求驗證的伺服器到伺服器規則
netsh advfirewall consec add rule name="IPsec Auth" endpoint1=192.168.1.0/24 endpoint2=10.0.0.0/8 auth1=computerpsk psk=SharedSecret123 action=requireauth
# 動態加密:先驗證再加密
netsh advfirewall consec add rule name="Dynamic Encrypt" endpoint1=any endpoint2=any auth1=computerkerb action=authdynenc
路徑:電腦設定 → 原則 → Windows 設定 → 安全性設定 → 進階安全性的 Windows 防火牆
| 情境 | GUI 操作 | netsh 指令 |
|---|---|---|
| 開放遠端桌面 (TCP 3389) 僅限內網 | 新增輸入規則 → 連接埠 3389 → 範圍:遠端 IP 192.168.0.0/16 | netsh advfirewall firewall add rule name="RDP LAN" dir=in action=allow protocol=TCP localport=3389 remoteip=192.168.0.0/16 |
| 阻擋某程式上網 | 新增輸出規則 → 程式路徑 → 封鎖連線 | netsh advfirewall firewall add rule name="Block App" dir=out action=block program="C:\Bad\app.exe" |
| 啟用 Ping 回應 | 預定義規則「檔案和印表機共用 (回應要求 - ICMPv4-In)」啟用 | netsh advfirewall firewall set rule group="File and Printer Sharing" new enable=yes |
Windows 7 防火牆提供 GUI 與命令列雙軌管理,滿足從單機設定到企業級 GPO 部署的各種需求。熟練 netsh advfirewall 腳本化操作,配合事件日誌監控,能構建可稽核、可自動化的主機防護基線。