利用powershell进行windows日志分析

0x00 前言html

  Windows 中提供了 2 个分析事件日志的 PowerShell cmdlet:一个是Get-WinEvent,超级强大,但使用起来比较麻烦;另外一个是Get-EventLog,使得起来至关简单,能够实时筛选,接下来,咱们利用PowerShell 来自动筛选 Windows 事件日志。shell

0x01 Get-WinEventwindows

A、XML编写微信

假设有这样一个需求:windows server2008 R2环境,须要统计一下近7天用户登陆次数。网络

咱们能够直接利用事件查看器里面筛选日志,以下图:spa

经过查看登陆日志,发如今真正的登陆时间,是这条日志,去其余不一样的是,此条日志记录的进程名是winlogon.exe 要实现比较精确的筛选,须要从这里入手,日志

进一步筛选 点击“事件属性”里面的“详细信息”中,能够看见一条信息,后面会用到:code

 

在“筛选当前日志”中,选择“XML”,勾选“手动编辑查询”,并确认,在手动编辑中加入如下设置 *[EventData[Data[@Name='ProcessName'] and (Data='c:\windows\system32\winlogon.exe')]] and 如图(里面的PrcessName和winlogon.exe就是前面在“事件属性”里面的“详细信息”中看到的):server

*[EventData[Data[@Name='ProcessName'] and (Data='c:\windows\system32\winlogon.exe')]] and

点击确认,能够精确的筛选用户登陆事件。xml

windows server 2012的登陆筛选 在windows server2012中,可能会有一些小变化,可是也不要紧,按照以前的解决思路便可。下面可作参考:

*[EventData[Data[@Name='ProcessName'] and (Data='c:\windows\system32\winlogon.exe')]] and

*[EventData[Data[@Name='LogonType'] and (Data='10')]] and

B、Get-WinEvent使用

Get-WinEvent -FilterHashtable @{Logname='system';Id='6006','6005'}

0x02 Get-EventLog

 Get-EventLog Security  -InstanceId  4624,4625

 

$xml='<QueryList>
  <Query Id="0" Path="Security">
    <Select Path="Security">*[System[(EventID=4624)]]</Select>
  </Query>
</QueryList>'
$events = Get-WinEvent -FilterXml $xml
$i=0
#Write-Host '登陆时间','登陆类型','登陆帐号','登陆IP地址'
while ($i -lt $events.length) {
    $time=$events[$i].TimeCreated
    $type=[regex]::matches($events[$i].Message, '登陆类型:(.+)') | %{$_.Groups[1].Value.Trim()}
    $user=([regex]::matches($events[$i].Message, '账户名:(.+)') | %{$_.Groups[1].Value.Trim()})[1]
    $IP=[regex]::matches($events[$i].Message, '源网络地址:(.+)') | %{$_.Groups[1].Value.Trim()}
    Write-Host $time,$user,$type,$IP
    $i++
}

脚本运行:

查看用户登陆事件、登陆用户名、登陆类型、登陆IP地址

 

最后

欢迎关注我的微信公众号:Bypass--,每周原创一篇技术干货。 

 

 

参考连接:

Get-EventLog 使用

https://www.cnblogs.com/brooks-dotnet/archive/2010/08/24/1807615.html

https://www.cnblogs.com/fuhj02/archive/2011/01/03/1924339.html 

Get-EventLog使用

https://docs.microsoft.com/zh-cn/powershell/module/Microsoft.PowerShell.Management/Get-EventLog?view=powershell-5.1

精准的筛选windows用户登陆事件

https://www.iyunv.com/thread-525384-1-1.html

相关文章
相关标签/搜索