共用方式為


設定 Windows 防火牆記錄

若要將 Windows 防火牆設定為記錄已卸除的封包或成功連線,您可以使用:

  • 使用 Microsoft Intune 等 MDM 解決方案設定服務提供者 (CSP)
  • 組策略 (GPO)

下列指示提供如何設定裝置的詳細數據。 選取最符合您需求的選項。

  1. 登入 Microsoft Intune 系統管理中心
  2. 移至 端點安全>性防火牆>建立原則>Windows 10、Windows 11 和 Windows Server>Windows 防火牆>建立
  3. 輸入名稱,並選擇性地輸入描述 >下一步
  4. 在 [ 組態設定] 底下,針對 [網 ]、[ 私人]、[ 用) ] (每個網络位置類型,設定:
    • 記錄檔路徑
    • 啟用記錄卸除封包
    • 啟用記錄成功連線
    • 記錄檔大小上限
  5. 選取 [下一步>]
  6. 將原則指派給包含為您要設定下一個>建立之裝置或用戶成員的>群組

提示

如果您想要的話,也可以使用 [設定] 目錄原則 來設定 Windows 防火牆記錄。

或者,您可以使用 自定義 原則搭配 防火牆 CSP 來設定裝置。

網路配置檔 設定
網域 設定名稱: EnableLogDroppedPackets
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/DomainProfile/EnableLogDroppedPackets
網域 設定名稱: LogFilePath
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/DomainProfile/LogFilePath
網域 設定名稱: EnableLogSuccessConnections
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/DomainProfile/EnableLogSuccessConnections
網域 設定名稱: LogMaxFileSize
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/DomainProfile/LogMaxFileSize
Private 設定名稱: EnableLogDroppedPackets
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PrivateProfile/EnableLogDroppedPackets
Private 設定名稱: LogFilePath
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PrivateProfile/LogFilePath
Private 設定名稱: EnableLogSuccessConnections
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PrivateProfile/EnableLogSuccessConnections
Private 設定名稱: LogMaxFileSize
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PrivateProfile/LogMaxFileSize
Public 設定名稱: EnableLogDroppedPackets
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PublicProfile/EnableLogDroppedPackets
Public 設定名稱: LogFilePath
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PublicProfile/LogFilePath
Public 設定名稱: EnableLogSuccessConnections
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PublicProfile/EnableLogSuccessConnections
Public 設定名稱: LogMaxFileSize
OMA-URI: ./Vendor/MSFT/Firewall/MdmStore/PublicProfile/LogMaxFileSize

重要

您指定的位置必須獲指派許可權,以允許 Windows 防火牆服務寫入記錄檔。

建議

以下是設定 Windows 防火牆記錄的一些建議:

  • 將記錄大小變更為至少 20,480 KB (20 MB) ,以確保記錄檔不會太快填滿。 記錄檔大小上限為 32,767 KB (32 MB)
  • 針對網域、私人和公用 (每個配置檔,) 將默認記錄檔名稱從 %windir%\system32\logfiles\firewall\pfirewall.log 變更為:
    • %windir%\system32\logfiles\firewall\pfirewall_Domain.log
    • %windir%\system32\logfiles\firewall\pfirewall_Private.log
    • %windir%\system32\logfiles\firewall\pfirewall_Public.log
  • 將卸除的封包記錄到 [是]
  • 將成功連線記錄到 [是]

在單一系統上,您可以使用下列命令來設定記錄:

netsh advfirewall>set allprofiles logging allowedconnections enable
netsh advfirewall>set allprofiles logging droppedconnections enable

剖析方法

有數種方法可以剖析 Windows 防火牆記錄檔。 例如:

提示

如果記錄在 SIEM 解決方案中顯示的速度很慢,您可以減少記錄檔大小。 請注意,縮減會導致因記錄輪替增加而產生更多資源使用量。

針對記錄檔是否未建立或修改進行疑難解答

有時不會建立 Windows 防火牆記錄檔,或事件不會寫入記錄檔。 可能發生此情況的一些範例包括:

  • 資料夾或記錄檔上遺漏 Windows Defender 防火牆服務 (mpssvc) 的許可權
  • 您想要將記錄檔儲存在不同的資料夾中,但許可權遺失或未自動設定
  • 如果防火牆記錄是透過原則設定來設定,就可能發生這種情況
    • 預設位置 %windir%\System32\LogFiles\firewall 中的記錄檔資料夾不存在
    • 自訂路徑中的記錄檔資料夾不存在

在這兩種情況下,您都必須手動或透過腳本建立資料夾,並新增的 mpssvc許可權。

New-Item -ItemType Directory -Path $env:windir\System32\LogFiles\Firewall

確認資料夾和檔案上是否有 mpssvcFullControl 。 從提升許可權的 PowerShell 工作階段中,使用下列命令,確保使用正確的路徑:

$LogPath = Join-Path -path $env:windir -ChildPath "System32\LogFiles\Firewall"
(Get-ACL -Path $LogPath).Access | Format-Table IdentityReference,FileSystemRights,AccessControlType,IsInherited,InheritanceFlags -AutoSize

輸出應該會顯示 NT SERVICE\mpssvcFullControl

IdentityReference      FileSystemRights AccessControlType IsInherited InheritanceFlags
-----------------      ---------------- ----------------- ----------- ----------------
NT AUTHORITY\SYSTEM         FullControl             Allow       False    ObjectInherit
BUILTIN\Administrators      FullControl             Allow       False    ObjectInherit
NT SERVICE\mpssvc           FullControl             Allow       False    ObjectInherit

如果沒有,請將 的 FullControl 權mpssvc 新增至資料夾、子資料夾和檔案。 請務必使用正確的路徑。

$LogPath = Join-Path -path $env:windir -ChildPath "System32\LogFiles\Firewall"
$NewAcl = Get-Acl -Path $LogPath

$identity = "NT SERVICE\mpssvc"
$fileSystemRights = "FullControl"
$inheritanceFlags = "ContainerInherit,ObjectInherit"
$propagationFlags = "None"
$type = "Allow"

$fileSystemAccessRuleArgumentList = $identity, $fileSystemRights, $inheritanceFlags, $propagationFlags, $type
$fileSystemAccessRule = New-Object -TypeName System.Security.AccessControl.FileSystemAccessRule -ArgumentList $fileSystemAccessRuleArgumentList

$NewAcl.SetAccessRule($fileSystemAccessRule)
Set-Acl -Path $LogPath -AclObject $NewAcl

重新啟動裝置以重新啟動 Windows Defender 防火牆 服務。