使用命令行管理 Windows 防火牆
本文內容
設定設定檔全域預設值
部署基本防火牆規則
遠端管理
部署基本 IPsec 規則設定
使用 IPsec 部署安全防火牆規則
本文提供如何使用PowerShell和 netsh.exe
管理 Windows 防火牆的範例,其可用來自動化 Windows 防火牆的管理。
全域預設值會以每個配置檔為基礎來設定裝置行為。 Windows 防火牆支援網域、私人和公用配置檔。
Windows 防火牆會捨棄未對應至允許未經要求流量的流量,或是因應裝置要求而傳送的流量。 如果您發現未強制執行您建立的規則,您可能需要啟用 Windows 防火牆。 以下說明如何在本機裝置上啟用 Windows 防火牆:
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled True
netsh.exe advfirewall set allprofiles state on
全域預設設定可以透過命令行介面來定義。 這些修改也可透過 Windows 防火牆控制台取得。
下列腳本會設定預設的輸入和輸出動作、指定受保護的網路連線,以及不允許在程式遭到封鎖而無法接收輸入連線時向用戶顯示通知。 它允許多播或廣播網路流量的單播回應,並指定用於疑難解答的記錄設定。
Set-NetFirewallProfile -DefaultInboundAction Block -DefaultOutboundAction Allow -NotifyOnListen False -AllowUnicastResponseToMulticast True -LogFileName %SystemRoot%\System32\LogFiles\Firewall\pfirewall.log
netsh advfirewall set allprofiles firewallpolicy blockinbound,allowoutbound
netsh advfirewall set allprofiles settings inboundusernotification disable
netsh advfirewall set allprofiles settings unicastresponsetomulticast enable
netsh advfirewall set allprofiles logging filename %SystemRoot%\System32\LogFiles\Firewall\pfirewall.log
Microsoft建議您不要停用 Windows 防火牆,因為您遺失其他優點,例如能夠使用因特網通訊協定安全性 (IPsec) 連線安全性規則、網路保護,以防止使用網路指紋攻擊、Windows 服務強化和 開機時間篩選 。 非Microsoft防火牆軟體只能以程序設計方式停用需要停用相容性之 Windows 防火牆的 規則類型 。 基於此目的,您不應該自行停用防火牆。
如果需要停用 Windows 防火牆,請勿藉由停止服務嵌入式管理單元中的 Windows 防火牆服務 (來停用它,顯示名稱是 Windows Defender 防火牆,而服務名稱為 MpsSvc) 。 Microsoft不支援停止 Windows 防火牆服務,而且可能會造成問題,包括:
[開始] 功能表可以停止運作
新式應用程式可能無法安裝或更新
透過手機啟用 Windows 失敗
相依於 Windows 防火牆的應用程式或 OS 不相容
停用 Windows 防火牆的適當方法是停用 Windows 防火牆配置檔,並讓服務繼續執行。
使用下列程式來關閉防火牆,或停用 群組原則 設定計算機組態|系統管理範本 |網路|網路 Connections|Windows 防火牆 |網域 Prolfile|Windows 防火牆:保護所有網路連線 。
如需詳細資訊,請參閱 Windows 防火牆部署指南 。
下列範例會停用所有配置檔的 Windows 防火牆。
Set-NetFirewallProfile -Profile Domain,Public,Private -Enabled False
netsh.exe advfirewall set allprofiles state off
本節提供建立、修改和刪除防火牆規則的腳本範例。
在 Windows PowerShell 中新增防火牆規則看起來與在 Netsh 中類似,但參數和值的指定方式不同。
以下是如何允許 Telnet 應用程式在網路上接聽的範例。 此防火牆規則的範圍是使用 關鍵詞而非IP位址,以限於本機子網。 就像在 Netsh 中一樣,規則會在本機裝置上建立,並立即生效。
New-NetFirewallRule -DisplayName "Allow Inbound Telnet" -Direction Inbound -Program %SystemRoot%\System32\tlntsvr.exe -RemoteAddress LocalSubnet -Action Allow
netsh advfirewall firewall add rule name="Allow Inbound Telnet" dir=in program= %SystemRoot%\System32\tlntsvr.exe remoteip=localsubnet action=allow
下列腳本示範如何新增基本防火牆規則,以封鎖從特定應用程式和本機埠到 Active Directory 中 (GPO) 群組原則 對象的輸出流量。 在 Windows PowerShell 中,原則存放區會指定為 New-NetFirewall Cmdlet 內的參數。 在 Netsh 中,您必須先指定 Netsh 工作階段中的命令應該修改的 GPO。 您輸入的命令會針對 GPO 的內容執行,而且執行會維持有效狀態,直到 Netsh 工作階段結束,或直到執行另一個 set store 命令為止。
在這裡,domain.contoso.com 是您 Active Directory 網域服務 (AD DS) 的名稱,而 gpo_name 是您要修改的 GPO 名稱。 如果 GPO 名稱中有任何空白,則需要引號。
New-NetFirewallRule -DisplayName "Block Outbound Telnet" -Direction Outbound -Program %SystemRoot%\System32\tlntsvr.exe -Protocol TCP -LocalPort 23 -Action Block -PolicyStore domain.contoso.com\gpo_name
netsh advfirewall set store gpo=domain.contoso.com\gpo_name
netsh advfirewall firewall add rule name="Block Outbound Telnet" dir=out program=%SystemRoot%\System32\telnet.exe protocol=tcp localport=23 action=block
若要降低忙碌域控制器的負擔,Windows PowerShell 可讓您將 GPO 載入至本機會話、在該會話中進行所有變更,然後將它全部儲存回來。
下列命令會藉由將 Telnet 規則新增至 GPO) 來執行與先前範例 (相同的動作,但我們是在 PowerShell 中套用 GPO 快取來執行。 Netsh 不支援將 GPO 載入本機會話並使用 -GPOSession 參數來變更 GPO
$gpo = Open-NetGPO -PolicyStore domain.contoso.com\gpo_name
New-NetFirewallRule -DisplayName "Block Outbound Telnet" -Direction Outbound -Program %SystemRoot%\System32\telnet.exe -Protocol TCP -LocalPort 23 -Action Block -GPOSession $gpo
Save-NetGPO -GPOSession $gpo
此命令不會批處理您的個別變更,它會一次載入並儲存整個 GPO。 因此,如果任何其他變更是由其他系統管理員或在不同的 Windows PowerShell 視窗中進行,則儲存 GPO 會覆寫這些變更。
建立規則時,Netsh 和 Windows PowerShell 可讓您變更規則屬性和影響,但規則會在 Windows PowerShell 中維護其唯一標識子 (,此標識符會使用 -Name 參數) 來指定。
例如,您可以有一個規則 [允許 Web 80 ],針對輸入的未經要求的流量啟用 TCP 連接埠 80。 您可以藉由指定人類看得懂的當地語系化規則名稱,來變更規則,以比對 Web 伺服器的不同遠端 IP 位址,該伺服器的流量將會允許。
Set-NetFirewallRule -DisplayName "Allow Web 80" -RemoteAddress 192.168.0.2
netsh advfirewall firewall set rule name="Allow Web 80" new remoteip=192.168.0.2
Netsh 要求您提供要變更的規則名稱,而我們沒有取得防火牆規則的替代方式。 在 Windows PowerShell 中,您可以使用規則的已知屬性來查詢規則。
當您執行 Get-NetFirewallRule
時,您可能會注意到位址和埠等常見條件不會出現。 這些條件會以稱為 Filters 的個別物件表示。 如先前所示,您可以在 New-NetFirewallRule 和 Set-NetFirewallRule 中設定所有條件。 如果您想要根據這些欄位查詢防火牆規則, (埠、位址、安全性、介面、服務) ,則必須自行取得篩選物件。
您可以變更允許 Web 80 規則的遠端端點, (如同先前使用篩選物件) 一樣。 使用 Windows PowerShell,您可以使用埠篩選器依埠進行查詢,然後假設有其他規則會影響本機埠,您可以使用進一步的查詢來建置,直到擷取所需的規則為止。
在下列範例中,我們假設查詢會傳回單一防火牆規則,然後利用 Windows PowerShell的管線輸入能力,透過管道Set-NetFirewallRule
傳送至 Cmdlet。
Get-NetFirewallPortFilter | ?{$_.LocalPort -eq 80} | Get-NetFirewallRule | ?{ $_.Direction -eq "Inbound" -and $_.Action -eq "Allow"} | Set-NetFirewallRule -RemoteAddress 192.168.0.2
您也可以使用通配符查詢規則。 下列範例會傳回與特定程序相關聯的防火牆規則陣列。 數位的專案可以在後續 Set-NetFirewallRule
的 Cmdlet 中修改。
Get-NetFirewallApplicationFilter -Program "*svchost*" | Get-NetFirewallRule
在 Set 命令中指定相關聯的組名時,可以同時修改群組中的多個規則。 您可以將防火牆規則新增至指定的管理群組,以便管理多個共用相同影響的規則。
在下列範例中,我們會將輸入和輸出 Telnet 防火牆規則新增至 Telnet 管理 群組。 在 Windows PowerShell 中,第一次建立規則時會指定群組成員資格,因此我們會重新建立先前的範例規則。 無法在 Netsh 中將規則新增至自訂規則群組。
New-NetFirewallRule -DisplayName "Allow Inbound Telnet" -Direction Inbound -Program %SystemRoot%\System32\tlntsvr.exe -RemoteAddress LocalSubnet -Action Allow -Group "Telnet Management"
New-NetFirewallRule -DisplayName "Block Outbound Telnet" -Direction Outbound -Program %SystemRoot%\System32\tlntsvr.exe -RemoteAddress LocalSubnet -Action Allow -Group "Telnet Management"
如果未在規則建立時指定群組,則可以在 Windows PowerShell 中使用點表示法將規則新增至規則群組。 您無法使用 Set-NetFirewallRule
指定群組,因為命令允許依規則群組進行查詢。
$rule = Get-NetFirewallRule -DisplayName "Allow Inbound Telnet"
$rule.Group = "Telnet Management"
$rule | Set-NetFirewallRule
在命令的協助下 Set
,如果指定規則組名,則不會修改群組成員資格,而是群組的所有規則都會收到指定參數所指出的相同修改。
下列腳本會啟用預先定義群組中的所有規則,其中包含影響防火牆規則的遠端管理。
Set-NetFirewallRule -DisplayGroup "Windows Firewall Remote Management" -Enabled True
netsh advfirewall firewall set rule group="Windows Firewall remote management" new enable=yes
另外還有一個 Enable-NetFirewallRule
Cmdlet 可依群組或規則的其他屬性來啟用規則。
Enable-NetFirewallRule -DisplayGroup "Windows Firewall Remote Management" -Verbose
規則物件可以停用,使其不再作用中。 在 Windows PowerShell 中,Disable-NetFirewallRule Cmdlet 會將規則保留在系統上,但將其置於停用狀態,因此不再套用規則並影響流量。
Enable-NetFirewallRule 可以重新啟用已停用的防火牆規則。 此 Cmdlet 與 Remove-NetFirewallRule 不同,後者會從裝置永久移除規則定義。
下列 Cmdlet 會從本機原則存放區中刪除指定的現有防火牆規則。
Remove-NetFirewallRule -DisplayName "Allow Web 80"
netsh advfirewall firewall delete rule name="Allow Web 80"
如同其他 Cmdlet,您也可以查詢要移除的規則。 在這裡,所有封鎖的防火牆規則都會從裝置中刪除。
Remove-NetFirewallRule -Action Block
使用 Get 命令查詢規則,並將它儲存在變數中,觀察規則會受到影響,然後使用管線將規則傳 送至 Remove 命令可能比較安全,就像我們針對 Set 命令所做的一樣。 下列範例示範如何檢視所有封鎖的防火牆規則,然後刪除前四個規則。
$x = Get-NetFirewallRule -Action Block
$x
$x[0-3] | Remove-NetFirewallRule
默認會啟用使用 WinRM 的遠端管理。 支援 CimSession 參數的 Cmdlet 會使用 WinRM,而且預設可以從遠端管理。
下列範例會傳回名為 RemoteDevice 之裝置上永續性存放區的所有防火牆規則。
Get-NetFirewallRule -CimSession RemoteDevice
我們可以使用 -CimSession 參數,在遠端裝置上執行任何修改或檢視規則。 在這裡,我們會從遠端裝置移除特定的防火牆規則。
$RemoteSession = New-CimSession -ComputerName RemoteDevice
Remove-NetFirewallRule -DisplayName "AllowWeb80" -CimSession $RemoteSession -Confirm
IPsec) 原則 (因特網通訊協定安全性是由判斷 IPsec 行為的規則所組成。 IPsec 支援網路層級對等驗證、數據源驗證、數據完整性、數據機密性 (加密) ,以及重新執行保護。
Windows PowerShell 可以建立功能強大且複雜的 IPsec 原則,例如在 Netsh 和 Windows 防火牆控制台中。 不過,由於 Windows PowerShell 是以對象為基礎,而不是以字串令牌為基礎,因此 Windows PowerShell 中的設定可提供更大的控制和彈性。
在 Netsh 中,驗證和密碼編譯集已指定為特定格式的逗號分隔令牌清單。 在 Windows PowerShell 中,您不需要使用預設設定,而是先建立所需的驗證或密碼編譯提案物件,並以慣用的順序將它們組合成清單。 然後,您會建立一或多個參考這些集合的 IPsec 規則。 此模型的優點是以程序設計方式存取規則中的資訊更為容易。 如需釐清範例,請參閱下列各節。
下列 Cmdlet 會在 群組原則 物件中建立基本的 IPsec 傳輸模式規則。 IPsec 規則很容易建立;只需要顯示名稱,其餘屬性就會使用預設值。 輸入流量會經過驗證,並使用預設的快速模式和主要模式設定來檢查完整性。 您可以在主控台的[自訂 IPsec 預設值] 下找到這些預設設定。
New-NetIPsecRule -DisplayName "Require Inbound Authentication" -PolicyStore domain.contoso.com\gpo_name
netsh advfirewall set store gpo=domain.contoso.com\gpo_name
netsh advfirewall consec add rule name="Require Inbound Authentication" endpoint1=any endpoint2=any action=requireinrequestout
如果您想要建立一組自定義的快速模式提案,其中包含 IPsec 規則物件中的 AH 和 ESP,您可以個別建立相關聯的物件,並連結其關聯。 如需驗證方法的詳細資訊,請參閱 選擇 IPsec 通訊協定 。
然後,您可以在建立 IPsec 規則時,使用新建立的自定義快速模式原則。 密碼編譯集物件會連結至 IPsec 規則物件。
在此範例中,我們會藉由指定自定義的快速模式密碼編譯集,以先前建立的 IPsec 規則為基礎。 最終的 IPsec 規則需要由指定的密碼編譯方法驗證輸出流量。
$AHandESPQM = New-NetIPsecQuickModeCryptoProposal -Encapsulation AH,ESP -AHHash SHA1 -ESPHash SHA1 -Encryption DES3
$QMCryptoSet = New-NetIPsecQuickModeCryptoSet -DisplayName "ah:sha1+esp:sha1-des3" -Proposal $AHandESPQM -PolicyStore domain.contoso.com\gpo_name
New-NetIPsecRule -DisplayName "Require Inbound Authentication" -InboundSecurity Require -OutboundSecurity Request -QuickModeCryptoSet $QMCryptoSet.Name -PolicyStore domain.contoso.com\gpo_name
netsh advfirewall set store gpo=domain.contoso.com\gpo_name
netsh advfirewall consec add rule name="Require Outbound Authentication" endpoint1=any endpoint2=any action=requireinrequestout qmsecmethods=ah:sha1+esp:sha1-3des
公司網路可能需要保護與其他機構的通訊。 但是,您發現代理程式會執行非 Windows 作業系統,而且需要使用因特網密鑰交換版本 2 (IKEv2) 標準。
您可以將 IKEv2 指定為 IPsec 規則中的關鍵模組,以在 Windows Server 2012 中套用 IKEv2 功能。 此功能規格只能使用計算機憑證驗證來完成,且無法與階段 2 驗證搭配使用。
New-NetIPsecRule -DisplayName "Require Inbound Authentication" -InboundSecurity Require -OutboundSecurity Request -Phase1AuthSet MyCertAuthSet -KeyModule IKEv2 -RemoteAddress $nonWindowsGateway
如需 IKEv2 的詳細資訊,包括案例,請參閱使用 IKEv2 保護端對端 IPsec Connections 。
具有相同規則屬性的防火牆和 IPsec 規則可以重複,以簡化在不同原則存放區中重新建立它們的工作。
若要將先前建立的規則從一個原則存放區複製到另一個原則存放區,也必須個別複製相關聯的物件。 不需要複製相關聯的防火牆篩選。 您可以使用與其他 Cmdlet 相同的方式來查詢要複製的規則。
複製個別規則是無法透過 Netsh 介面執行的工作。 以下是您可以使用 Windows PowerShell 完成此作業的作法。
$Rule = Get-NetIPsecRule -DisplayName "Require Inbound Authentication"
$Rule | Copy-NetIPsecRule -NewPolicyStore domain.costoso.com\new_gpo_name
$Rule | Copy-NetPhase1AuthSet -NewPolicyStore domain.costoso.com\new_gpo_name
若要處理 Windows PowerShell 文稿中的錯誤,您可以使用 -ErrorAction 參數。 此參數在 Remove Cmdlet 中特別有用。 如果您想要移除特定規則,您會發現如果找不到規則,它就會失敗。 拿掉規則時,如果規則尚未存在,則可接受忽略該錯誤。 在此情況下,您可以執行下列動作,在移除作業期間隱藏任何「找不到規則」錯誤。
Remove-NetFirewallRule -DisplayName "Contoso Messenger 98" -ErrorAction SilentlyContinue
使用通配符也可以隱藏錯誤,但可能會符合您不想要移除的規則。 這些通配符可以是實用的快捷方式,但只有在您知道沒有任何額外的規則會意外刪除時,才應該使用。 因此,下列 Cmdlet 也會移除規則,以隱藏任何「找不到」錯誤。
Remove-NetFirewallRule -DisplayName "Contoso Messenger 98*"
使用通配符時,如果您想要再次檢查相符的規則集合,您可以使用 -WhatIf 參數。
Remove-NetFirewallRule -DisplayName "Contoso Messenger 98*" -WhatIf
如果您只想要刪除某些相符的規則,您可以使用 -Confirm 參數來取得依規則的規則確認提示。
Remove-NetFirewallRule -DisplayName "Contoso Messenger 98*" -Confirm
您也可以只執行整個作業,在執行作業時顯示每個規則的名稱。
Remove-NetFirewallRule -DisplayName "Contoso Messenger 98*" -Verbose
下列 Windows PowerShell 命令在部署階段的更新週期中很有用。
若要讓您檢視特定存放區中的所有 IPsec 規則,您可以使用下列命令。 在 Netsh 中,此命令不會顯示 profile=domain,public 或 profile=domain,private 的規則。 它只會顯示規則中包含單一輸入網域的規則。 下列命令範例會顯示所有設定檔中的 IPsec 規則。
Show-NetIPsecRule -PolicyStore ActiveStore
netsh advfirewall consec show rule name=all
您可以監視主要模式安全性關聯,以取得資訊,例如哪些對等目前已連線到裝置,以及哪些保護套件用來形成安全性關聯。
使用下列 Cmdlet 來檢視現有的主要模式規則及其安全性關聯:
netsh advfirewall monitor show mmsa all
若要檢視特定規則或規則群組的屬性,您可以查詢規則。 當查詢傳回指定為 NotConfigured 的欄位時,您可以判斷規則的來源原則存放區。
針對來自 GPO (-PolicyStoreSourceType 參數在 Show 命令) 中指定為 GroupPolicy 的物件,如果傳遞 -TracePolicyStore ,則會在 PolicyStoreSource 字段中找到並傳回 GPO 的名稱。
Get-NetIPsecRule -DisplayName "Require Inbound Authentication" -TracePolicyStore
請務必注意,顯示的來源不包含功能變數名稱。
IPsec 可用來隔離網域成員與非網域成員。 網域隔離會使用 IPsec 驗證,要求已加入網域的裝置積極建立通訊裝置的身分識別,以改善組織的安全性。 IPsec 的一或多個功能可用來使用 IPsec 規則對象來保護流量。
若要在網路上實作網域隔離,網域中的裝置會收到IPsec規則,以封鎖不受IPsec保護的未經要求的輸入網路流量。 在這裡,我們會建立需要網域成員驗證的 IPsec 規則。 透過此驗證,您可以將已加入網域的裝置與未加入網域的裝置隔離。 在下列範例中,輸入流量需要 Kerberos 驗證,並針對輸出流量要求 Kerberos 驗證。
$kerbprop = New-NetIPsecAuthProposal -Machine -Kerberos
$Phase1AuthSet = New-NetIPsecPhase1AuthSet -DisplayName "Kerberos Auth Phase1" -Proposal $kerbprop -PolicyStore domain.contoso.com\domain_isolation
New-NetIPsecRule -DisplayName "Basic Domain Isolation Policy" -Profile Domain -Phase1AuthSet $Phase1AuthSet.Name -InboundSecurity Require -OutboundSecurity Request -PolicyStore domain.contoso.com\domain_isolation
netsh advfirewall set store gpo=domain.contoso.com\domain_isolation
netsh advfirewall consec add rule name="Basic Domain Isolation Policy" profile=domain endpoint1="any" endpoint2="any" action=requireinrequestout auth1="computerkerb"
下列命令會建立 IPsec 信道,透過本機裝置上的介面 (1.1.1.1) ,從專用網 (192.168.0.0/16) 路由傳送流量透過其公用介面 (2.2.2.2.2 連接到第二個裝置的公用網路,) 另一個專用網 (192.157.0.0/16) 。 透過通道的所有流量都會使用 ESP/SHA1 來檢查其完整性,並使用 ESP/DES3 進行加密。
$QMProposal = New-NetIPsecQuickModeCryptoProposal -Encapsulation ESP -ESPHash SHA1 -Encryption DES3
$QMCryptoSet = New-NetIPsecQuickModeCryptoSet -DisplayName "esp:sha1-des3" -Proposal $QMProposal
New-NetIPSecRule -DisplayName "Tunnel from HQ to Dallas Branch" -Mode Tunnel -LocalAddress 192.168.0.0/16 -RemoteAddress 192.157.0.0/16 -LocalTunnelEndpoint 1.1.1.1 -RemoteTunnelEndpoint 2.2.2.2 -InboundSecurity Require -OutboundSecurity Require -QuickModeCryptoSet $QMCryptoSet.Name
netsh advfirewall consec add rule name="Tunnel from 192.168.0.0/16 to 192.157.0.0/16" mode=tunnel endpoint1=192.168.0.0/16 endpoint2=192.157.0.0/16 localtunnelendpoint=1.1.1.1 remotetunnelendpoint=2.2.2.2 action=requireinrequireout qmsecmethods=esp:sha1-3des
在只能透過 Windows 防火牆允許安全流量的情況下,必須結合手動設定的防火牆和 IPsec 規則。 防火牆規則會決定允許封包的安全性層級,而基礎 IPsec 規則會保護流量。 這些案例可在 Windows PowerShell 和 Netsh 中完成,在部署中有許多相似之處。
設定防火牆規則以在連線安全時允許連線,需要驗證對應的流量並保護完整性,然後選擇性地由 IPsec 加密。
下列範例會建立需要驗證流量的防火牆規則。 只有在使用個別的 IPsec 規則驗證來自遠端裝置的連線時,此命令才允許輸入 Telnet 網路流量。
New-NetFirewallRule -DisplayName "Allow Authenticated Telnet" -Direction Inbound -Program %SystemRoot%\System32\tlntsvr.exe -Authentication Required -Action Allow
netsh advfirewall firewall add rule name="Allow Authenticated Telnet" dir=in program=%SystemRoot%\System32\tlntsvr.exe security=authenticate action=allow
下列命令會建立需要第一個 (計算機) 驗證的 IPsec 規則,然後嘗試選擇性的第二個 (使用者) 驗證。 建立此規則可保護並允許流量通過 Messenger 程式的防火牆規則需求。
$mkerbauthprop = New-NetIPsecAuthProposal -Machine -Kerberos
$mntlmauthprop = New-NetIPsecAuthProposal -Machine -NTLM
$P1Auth = New-NetIPsecPhase1AuthSet -DisplayName "Machine Auth" -Proposal $mkerbauthprop,$mntlmauthprop
$ukerbauthprop = New-NetIPsecAuthProposal -User -Kerberos
$unentlmauthprop = New-NetIPsecAuthProposal -User -NTLM
$anonyauthprop = New-NetIPsecAuthProposal -Anonymous
$P2Auth = New-NetIPsecPhase2AuthSet -DisplayName "User Auth" -Proposal $ukerbauthprop,$unentlmauthprop,$anonyauthprop
New-NetIPSecRule -DisplayName "Authenticate Both Computer and User" -InboundSecurity Require -OutboundSecurity Require -Phase1AuthSet $P1Auth.Name -Phase2AuthSet $P2Auth.Name
netsh advfirewall consec add rule name="Authenticate Both Computer and User" endpoint1=any endpoint2=any action=requireinrequireout auth1=computerkerb,computerntlm auth2=userkerb,userntlm,anonymous
若要改善組織中裝置的安全性,您可以部署限制網域成員的網域隔離。 它們在彼此通訊時需要驗證,並拒絕未驗證的輸入連線。 若要改善具有敏感數據的伺服器安全性,此數據必須藉由只允許存取企業網域內的裝置子集來保護。
IPsec 可以藉由隔離伺服器來提供這一層額外的保護。 在伺服器隔離中,敏感數據存取僅限於具有合法商務需求的使用者和裝置,而且會額外加密數據以防止竊聽。
為了部署伺服器隔離,我們會分層防火牆規則,以限制IPsec規則上強制執行驗證之授權使用者或裝置的流量。
下列防火牆規則允許來自用戶帳戶的 Telnet 流量,這些用戶帳戶是名為「授權存取伺服器」自定義群組的成員。您可以藉由指定限制參數,根據裝置、使用者或兩者來進一步限制此存取。
藉由將使用者或群組的安全標識符擴充 (SID) ,建立安全描述符定義語言 (SDDL) 字串。 如需尋找群組 SID 的詳細資訊,請參閱: 尋找組帳戶的 SID 。
限制群組的存取權可讓系統管理員透過 Windows 防火牆和/或 IPsec 原則擴充強身份驗證支援。
下列範例示範如何建立代表安全組的 SDDL 字串。
$user = new-object System.Security.Principal.NTAccount ("corp.contoso.com\Administrators")
$SIDofSecureUserGroup = $user.Translate([System.Security.Principal.SecurityIdentifier]).Value
$secureUserGroup = "D:(A;;CC;;;$SIDofSecureUserGroup)"
使用先前的 Scriptlet,您也可以取得安全計算機群組的 SDDL 字串,如下所示:
$secureMachineGroup = "D:(A;;CC;;;$SIDofSecureMachineGroup)"
如需如何建立安全組或如何判斷 SDDL 字串的詳細資訊,請參閱 使用 SID 。
Telnet 是不提供加密的應用程式。 此應用程式可以透過網路傳送資料,例如名稱和密碼。 惡意使用者可以攔截此數據。 如果系統管理員想要允許使用 Telnet,但保護流量,則可以建立需要 IPsec 加密的防火牆規則。 此防火牆規則是必要的,讓系統管理員可以確定使用此應用程式時,此埠傳送或接收的所有流量都會加密。 如果 IPsec 無法授權連線,則不允許來自此應用程式的流量。
在此範例中,我們只允許透過建立下列防火牆規則,來自指定安全使用者群組的已驗證和加密輸入 Telnet 流量。
New-NetFirewallRule -DisplayName "Allow Encrypted Inbound Telnet to Group Members Only" -Program %SystemRoot%\System32\tlntsvr.exe -Protocol TCP -Direction Inbound -Action Allow -LocalPort 23 -Authentication Required -Encryption Required -RemoteUser $secureUserGroup -PolicyStore domain.contoso.com\Server_Isolation
netsh advfirewall set store gpo=domain.contoso.com\Server_Isolation
netsh advfirewall firewall add rule name="Allow Encrypted Inbound Telnet to Group Members Only" program=%SystemRoot%\System32\tlntsvr.exe protocol=TCP dir=in action=allow localport=23 security=authenc rmtusrgrp ="D:(A;;CC;;; S-1-5-21-2329867823-2610410949-1491576313-1735)"
上一個範例顯示特定應用程式的端對端安全性。 在許多應用程式都需要端點安全性的情況下,每個應用程式都擁有防火牆規則可能很麻煩且難以管理。 授權可以覆寫每個規則的基礎,並在 IPsec 層完成。
在此範例中,我們會將全域 IPsec 設定設為只允許傳輸模式流量來自具有下列 Cmdlet 的授權使用者群組。 請參閱先前使用安全組的範例。
Set-NetFirewallSetting -RemoteMachineTransportAuthorizationList $secureMachineGroup
建立防火牆規則,允許 IPsec 保護的網路流量 (經過驗證的略過)
已驗證的略過允許來自指定信任裝置或使用者的流量覆寫防火牆封鎖規則。 當系統管理員想要使用掃描伺服器來監視和更新裝置,而不需要使用埠層級例外狀況時,此覆寫會很有説明。 如需詳細資訊,請 參閱如何啟用已驗證的防火牆略過 。
在此範例中,我們假設存在封鎖防火牆規則。 如果流量是以來自指定裝置或使用者安全組成員的裝置或使用者帳戶進行驗證,則此範例允許任何埠上任何埠上的任何網路流量覆寫封鎖規則。
New-NetFirewallRule -DisplayName "Inbound Secure Bypass Rule" -Direction Inbound -Authentication Required -OverrideBlockRules $true -RemoteMachine $secureMachineGroup -RemoteUser $secureUserGroup -PolicyStore domain.contoso.com\domain_isolation
netsh advfirewall set store gpo=domain.contoso.com\domain_isolation
netsh advfirewall firewall add rule name="Inbound Secure Bypass Rule" dir=in security=authenticate action="bypass" rmtcomputergrp="D:(A;;CC;;;S-1-5-21-2329867823-2610410949-1491576313-1114)" rmtusrgrp="D:(A;;CC;;; S-1-5-21-2329867823-2610410949-1491576313-1735)"