2024/10/24

以 PowerShell 處理 Windows Server 防火牆規則

修改防火牆規則可執行【wf.msc】開啟圖形介面來修改 Windows Server 防火牆,那如果想透過程式或 PowerShell 方式來處理該怎麼做呢?

底下簡列幾種常用的命令
 
列出所有目前已啟用的規則
 Get-NetFirewallRule | Where-Object {$_.Enabled -eq "True"} | Format-Table -Property DisplayName, Direction, Action, Enabled

啟用 https 開啟443 port,底下的規則名稱為 Allow HTTPS
 New-NetFirewallRule -DisplayName "Allow HTTPS" -Direction Inbound -LocalPort 443 -Protocol TCP -Action Allow

停用規則

  • 停用檔案分享規則
  • Disable-NetFirewallRule -DisplayName "File and Printer Sharing (SMB-In)"
  • 停用遠端桌面規則,名稱改為【Remote Desktop - User Mode (TCP-In)Remote Desktop - User Mode (UDP-In)
  • 停用遠端管理Windows Management Instrumentation (WMI) 規則
  • 停用NetBIOS規則,名稱改為【NetBIOS Name Service (NB-Name-In)】
  • 停用LLMNR規則,名稱改為【Link-Local Multicast Name Resolution (LLMNR-In)】

 

封鎖所有入站連線,除了允許的例外規則

 Set-NetFirewallProfile -Profile Domain,Public,Private -DefaultInboundAction Block

Windows Server 預設允許所有出站連線,如果你需要更嚴格的控制,也可以選擇僅允許 HTTPS 相關的出站流量

開啟必要的出站規則
Set-NetFirewallProfile -Profile Domain,Public,Private -DefaultOutboundAction Block

允許 HTTPS 出站流量

New-NetFirewallRule -DisplayName "Allow HTTPS Outbound" -Direction Outbound -LocalPort 443 -Protocol TCP -Action Allow

~END

 

沒有留言:

張貼留言

Ubuntu 如何查看及變更時區 TimeZone

使用指令 timedatectl 查看目前 ubuntu 的時區設定 $ timedatectl 列出所有 Asia 的時區 $ timedatectl list-timezones | grep Asia 變更時區為 Asia/Taipei $ sudo timedatectl...