在這裏我準備了2臺系統,一個Windows Server 2012 R2的域控服務器DC01,一臺SQL on CentOS7的SQL數據庫服務器 sql
首先我使用SQL Manager Studio鏈接到SQL數據庫服務器建立須要存放Windows轉發事件日誌的數據庫「EventCollections」 shell
CREATE DATABASE EventCollections 數據庫
GO 安全
USE EventCollections 服務器
GO ide
-- the table name loosely relates to the name of my Win Event Subscription name sqlserver
CREATE TABLE [dbo].[GeneralEvents]( ui
[Id] [int] NULL, this
[LevelDisplayName] [varchar](255) NULL, url
[LogName] [varchar](255) NULL,
[MachineName] [varchar](255) NULL,
[Message] [varchar](max) NULL,
[ProviderName] [varchar](255) NULL,
[RecordID] [bigint] NULL,
[TaskDisplayName] [varchar](255) NULL,
[TimeCreated] [smalldatetime] NULL
)
-- Create Unique Clustered Index with IGNORE_DUPE_KEY=ON to avoid duplicates in sqlbulk imports
CREATE UNIQUE CLUSTERED INDEX [ClusteredIndex-EventCombo] ON [dbo].[GeneralEvents]
(
[RecordID] ASC,
[MachineName] ASC,
[LogName] ASC
) WITH (IGNORE_DUP_KEY = ON)
GO
爲了不後面每小時導入一第二天志數據時出現重複,對RecordID,MachineName和LogName使用IGNORE_DUPE_KEY=ON建立惟一的彙集索引
接下來回到DC服務器配置事件服務
首先須要配置WinRM,顯示可用的偵聽器
winrm e winrm/config/listener
執行winrm get winrm/config
檢查
allowremoteAccess = true
在日誌源服務器(咱們只有DC一臺服務器,使用這臺既是源也是收集日誌服務器)把network Service加入到Event Log Readers組裏
而後在日誌源服務器和收集日誌服務器執行以下命令:
wevtutil sl security /ca:O:BAG:SYD:(A;;0xf0005;;;SY)(A;;0x5;;;BA)(A;;0x1;;;S-1-5-32-573)(A;;0x1;;;S-1-5-20)
接下來打開事件查看器,點擊訂閱,這是會出現提示,是否啓用Windows事件收集器服務,點擊「是」
之間轉發使用的HTTP端口是5985
而後建立一個新的訂閱,指定須要收集的計算機,這裏輸入DC01
選擇須要訂閱哪些日誌,這裏我選擇System
選擇收集的事件級別
在高級裏指定收集日誌的賬戶爲域管理員賬戶,而後肯定
點擊用戶名密碼進行輸入
正常:每15分鐘
最小化帶寬:每6小時
最小化延遲:每30秒
肯定
這樣就建立好一個收集系統日誌的訂閱了
按照一樣的方法再建立一個安全日誌的訂閱
若是要執行命令的審計日誌,能夠開啓下面2個位置的組策略,而後經過事件ID4688查看
計算機配置 > 策略 > Windows 設置 > 安全設置 > 高級審覈配置 > 詳細跟蹤>審覈建立進程
管理 模板\系統\審覈建立的進程\在建立事件的過程當中包含命令行
備註:Microsoft不建議永久啓用命令行審覈。啓用此功能後,對Windows安全事件的讀取訪問權限的任何用戶將可以讀取任何成功建立的進程的命令行參數。請記住,命令行命令可能包含機密信息,包括密碼和其餘用戶數據
等待15分鐘後事件查看器的已轉發事件裏就出現了咱們訂閱的安全和系統日誌了
最後我在DC上執行以下PowerShell命令,將已轉發事件的日誌寫入SQL裏
# While this script is intended to run on an hourly basis, the filter is set for going back 65 minutes.
# This allows the script to run for 5 minutes without any missing any events. Because we setup the
# table using the IGNORE_DUPE_KEY = ON, duplicate entries are ignored in the database.
$xml = @'
<QueryList>
<Query Id="0" Path="ForwardedEvents">
<Select Path="ForwardedEvents">*[System[TimeCreated[timediff(@SystemTime) <= 3900000]]]</Select>
</Query>
</QueryList>
'@
$events = Get-WinEvent -FilterXml $xml | Select-Object ID, LevelDisplayName, LogName, MachineName, Message, ProviderName, RecordID, TaskDisplayName, TimeCreated
$connectionString = "Data Source=sqlserver;Integrated Security=true;Initial Catalog=EventCollections;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
$bulkCopy.DestinationTableName = "GeneralEvents"
$dt = New-Object "System.Data.DataTable"
# build the datatable
$cols = $events | select -first 1 | get-member -MemberType NoteProperty | select -Expand Name
foreach ($col in $cols) {$null = $dt.Columns.Add($col)}
foreach ($event in $events)
{
$row = $dt.NewRow()
foreach ($col in $cols) { $row.Item($col) = $event.$col }
$dt.Rows.Add($row)
}
# Write to the database!
$bulkCopy.WriteToServer($dt)
$xml = @'
<QueryList>
<Query Id="0" Path="ForwardedEvents">
<Select Path="ForwardedEvents">*[System[TimeCreated[timediff(@SystemTime) <= 3900000]]]</Select>
</Query>
</QueryList>
'@
$events = Get-WinEvent -FilterXml $xml | Select-Object ID, LevelDisplayName, LogName, MachineName, Message, ProviderName, RecordID, TaskDisplayName, TimeCreated
$connectionString = "Data Source=sqlserver;user id=sa;pwd=password@1;Initial Catalog=EventCollections;"
$bulkCopy = new-object ("Data.SqlClient.SqlBulkCopy") $connectionString
$bulkCopy.DestinationTableName = "GeneralEvents"
$dt = New-Object "System.Data.DataTable"
# build the datatable
$cols = $events | select -first 1 | get-member -MemberType NoteProperty | select -Expand Name
foreach ($col in $cols) {$null = $dt.Columns.Add($col)}
foreach ($event in $events)
{
$row = $dt.NewRow()
foreach ($col in $cols) { $row.Item($col) = $event.$col }
$dt.Rows.Add($row)
}
# Write to the database!
$bulkCopy.WriteToServer($dt)
其中上面這段:
<QueryList>
<Query Id="0" Path="ForwardedEvents">
<Select Path="ForwardedEvents">*[System[TimeCreated[timediff(@SystemTime) <= 3900000]]]</Select>
</Query>
</QueryList>
取自於已轉發事件的「篩選當前日誌」
XML內容
執行完成之後能夠到SQL去檢查日誌是否已經寫進SQL
select * from GeneralEvents
能夠看到日誌成功寫入SQL裏
最後就是作一個Windows計劃任務把上面的Powershell腳本每隔1小時自動執行一次了
把上面成功執行的腳本保存成ps1文件,並把這個文件剪切到C盤根目錄下
打開任務計劃程序,建立一個基本任務
下一步
選擇天天
下一步
啓動程序
在程序裏選擇powershell的啓動路徑C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
參數添加-command ". 'c:\event-into-sql.ps1'"
勾選「但單擊「完成」時,打開此任務屬性的對話框」,而後完成
設置執行該計劃任務的賬戶,以及權限
在觸發器裏修改每日爲以下圖所示
肯定,建立完成
到這裏就大功告成了。既然事件日誌都寫入SQL了,那麼就能夠利用PowerBI Desktop去讀取SQL的數據進行事件日誌統計分析了,以下圖: