powershell脚本 - 在安全日志中生成的时间

时间:2015-11-20 17:59:55

标签: powershell

我需要一些关于这段代码的帮助,因为我是powershell的超级初学者,但是试图向我的经理报告,他希望看到外部尝试远程进入我们系统的失败。

尝试从安全日志中提取4625个事件,并将以下字段输入csv文件:用户名(如果是内部用户),事件日期,原始IP。到目前为止,我根据我能找到的(a.k.a。leech)在线和定制了一些代码。除了日期(timegenerated)之外,此时一切都是正确的。我相信这是因为列出的替换字符串数量。它正在从日志中提取SubjectUserSid。我不太清楚我是否理解如何找到替换字符串编号,所以如果有人可以向我解释,那会有所帮助。 感谢

$Date = [DateTime]::Now.AddDays(-1)
$Server = "SERVER"
$logName = '{0}{1}_security4625_log.csv' -f "C:\temp\",
 $Date.tostring("MM-dd-yyyy_HH,mm,ss")

Get-EventLog -LogName 'Security' -Computer $Server `
 -InstanceId 4625 `
 -After $Date |
 Select-Object @{
  Name='TargetUserName'
  Expression={$_.ReplacementStrings[5]}
 },
 @{
  Name='WorkstationName'
  Expression={$_.ReplacementStrings[1] -replace '\$$'}
 },
 @{
  Name='IpAddress'
  Expression={$_.ReplacementStrings[-2]}
 },
 @{
  Name='TimeGenerated'
  Expression={$_.ReplacementStrings[0]}
 } |
 Export-Csv -Path $logName -NoTypeInformation

1 个答案:

答案 0 :(得分:0)

@{Name='TimeGenerated';Expression={$_.ReplacementStrings[0]}更改为TimeGenerated,您应该全部设置。

ReplacementStringsMessage字段中的变量。例如,以下日志条目:

EventID            : 4656
MachineName        : AmazingLaptop.ChinchillaFarm.com
Data               : {}
Index              : 23277285
Category           : (12804)
CategoryNumber     : 12804
EntryType          : FailureAudit
Message            : A handle to an object was requested.

                     Subject:
                         Security ID:        S-1-5-21-2127521184-6397854128-1234567890-12345678
                         Account Name:        TMTech
                         Account Domain:        ChinchillaFarm
                         Logon ID:        0xb8f705b

                     Object:
                         Object Server:        SC Manager
                         Object Type:        SERVICE OBJECT
                         Object Name:        Schedule
                         Handle ID:        0x0
                         Resource Attributes:    -

                     Process Information:
                         Process ID:        0x2b4
                         Process Name:        C:\Windows\System32\services.exe

                     Access Request Information:
                         Transaction ID:        {00000000-0000-0000-0000-000000000000}
                         Accesses:        %%7186
                                     %%7188

                         Access Reasons:        -
                         Access Mask:        0x14
                         Privileges Used for Access Check:    -
                         Restricted SID Count:    0
Source             : Microsoft-Windows-Security-Auditing
ReplacementStrings : {S-1-5-21-2127521184-6397854128-1234567890-12345678, TMTech, ChinchillaFarm, 0xb8f705b...}
InstanceId         : 4656
TimeGenerated      : 11/20/2015 11:06:39 AM
TimeWritten        : 11/20/2015 11:06:39 AM
UserName           : 
Site               : 
Container          : 

ReplacementStrings是Message属性中“安全ID”,“帐户名”和“帐户域”等所有字段的值。而是使用其中一个作为日期/时间,您可以使用TimeGenerated属性,它也可以用于您的CSV。

更新了脚本:

$Date = [DateTime]::Now.AddDays(-1)
$Server = "SERVER"
$logName = '{0}{1}_security4625_log.csv' -f "C:\temp\",
 $Date.tostring("MM-dd-yyyy_HH,mm,ss")

Get-EventLog -LogName 'Security' -Computer $Server `
 -InstanceId 4625 `
 -After $Date |
 Select-Object @{
  Name='TargetUserName'
  Expression={$_.ReplacementStrings[5]}
 },
 @{
  Name='WorkstationName'
  Expression={$_.ReplacementStrings[1] -replace '\$$'}
 },
 @{
  Name='IpAddress'
  Expression={$_.ReplacementStrings[-2]}
 },
 TimeGenerated |
 Export-Csv -Path $logName -NoTypeInformation