Windows事件日誌寫入SQL Server並PowerBI統計分析

在這裏我準備了2臺系統,一個Windows Server 2012 R2的域控服務器DC01,一臺SQL on CentOS7的SQL數據庫服務器 sql

clip_image001

首先我使用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

clip_image002

爲了不後面每小時導入一第二天志數據時出現重複,對RecordID,MachineName和LogName使用IGNORE_DUPE_KEY=ON建立惟一的彙集索引

接下來回到DC服務器配置事件服務

首先須要配置WinRM,顯示可用的偵聽器

winrm e winrm/config/listener

clip_image003

執行winrm get winrm/config

檢查

allowremoteAccess = true

clip_image004

在日誌源服務器(咱們只有DC一臺服務器,使用這臺既是源也是收集日誌服務器)把network Service加入到Event Log Readers組裏

clip_image005

而後在日誌源服務器和收集日誌服務器執行以下命令:

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)

clip_image006

接下來打開事件查看器,點擊訂閱,這是會出現提示,是否啓用Windows事件收集器服務,點擊「是」

之間轉發使用的HTTP端口是5985

而後建立一個新的訂閱,指定須要收集的計算機,這裏輸入DC01

clip_image007

選擇須要訂閱哪些日誌,這裏我選擇System

clip_image008

選擇收集的事件級別

clip_image009

在高級裏指定收集日誌的賬戶爲域管理員賬戶,而後肯定

clip_image010

點擊用戶名密碼進行輸入

clip_image011

正常:每15分鐘

最小化帶寬:每6小時

最小化延遲:每30秒

肯定

clip_image012

這樣就建立好一個收集系統日誌的訂閱了

clip_image013

按照一樣的方法再建立一個安全日誌的訂閱

clip_image014

若是要執行命令的審計日誌,能夠開啓下面2個位置的組策略,而後經過事件ID4688查看

計算機配置 > 策略 > Windows 設置 > 安全設置 > 高級審覈配置 > 詳細跟蹤>審覈建立進程

clip_image015

管理 模板\系統\審覈建立的進程\在建立事件的過程當中包含命令行

clip_image016

備註:Microsoft不建議永久啓用命令行審覈。啓用此功能後,對Windows安全事件的讀取訪問權限的任何用戶將可以讀取任何成功建立的進程的命令行參數。請記住,命令行命令可能包含機密信息,包括密碼和其餘用戶數據

等待15分鐘後事件查看器的已轉發事件裏就出現了咱們訂閱的安全和系統日誌了

clip_image017

最後我在DC上執行以下PowerShell命令,將已轉發事件的日誌寫入SQL裏

  • 若是SQL是臺Windows而且加域,那麼能夠採用集成身份驗證方式登錄,執行下面腳本

# 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) &lt;= 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)

  • 若是是採用sa賬戶登錄就執行以下:

$xml = @'

<QueryList>

<Query Id="0" Path="ForwardedEvents">

<Select Path="ForwardedEvents">*[System[TimeCreated[timediff(@SystemTime) &lt;= 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) &lt;= 3900000]]]</Select>

</Query>

</QueryList>

取自於已轉發事件的「篩選當前日誌」

clip_image018

XML內容

clip_image019

執行完成之後能夠到SQL去檢查日誌是否已經寫進SQL

select * from GeneralEvents

能夠看到日誌成功寫入SQL裏

clip_image020

最後就是作一個Windows計劃任務把上面的Powershell腳本每隔1小時自動執行一次了

把上面成功執行的腳本保存成ps1文件,並把這個文件剪切到C盤根目錄下

clip_image021

打開任務計劃程序,建立一個基本任務

clip_image022

下一步

clip_image023

選擇天天

clip_image024

下一步

clip_image025

啓動程序

clip_image026

在程序裏選擇powershell的啓動路徑C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe

參數添加-command ". 'c:\event-into-sql.ps1'"

clip_image027

勾選「但單擊「完成」時,打開此任務屬性的對話框」,而後完成

clip_image028

設置執行該計劃任務的賬戶,以及權限

clip_image029

在觸發器裏修改每日爲以下圖所示

clip_image030

肯定,建立完成

clip_image031

到這裏就大功告成了。既然事件日誌都寫入SQL了,那麼就能夠利用PowerBI Desktop去讀取SQL的數據進行事件日誌統計分析了,以下圖:

clip_image032

相關文章
相關標籤/搜索