Windows事件日志写入SQL Server并PowerBI统计分析

在这里我准备了2台系统,一个Windows Server 2012 R2的域控服务器DC01,一台SQL on CentOS7的SQL数据库服务器

clip_image001

首先我使用SQL Manager Studio连接到SQL数据库服务器创建需要存放Windows转发事件日志的数据库“EventCollections”

CREATE DATABASE EventCollections

GO

USE EventCollections

GO

-- the table name loosely relates to the name of my Win Event Subscription name

CREATE TABLE [dbo].[GeneralEvents](

[Id] [int] NULL,

[LevelDisplayName] [varchar](255) NULL,

[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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值