【问题标题】:powershell script - timegenerated in security logpowershell 脚本 - 在安全日志中生成的时间
【发布时间】:2015-11-21 03:51:20
【问题描述】:

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

试图从安全日志中提取 4625 个事件并将以下字段放入 csv 文件:用户名(如果是内部用户)、事件日期、源 IP。到目前为止,我有这个代码是基于我在网上可以找到的(又名水蛭)并进行了一些定制。除了日期(生成的时间)之外,此时一切都是正确的。我相信这是因为列出的替换字符串编号。它正在从日志中提取 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

【问题讨论】:

    标签: powershell


    【解决方案1】:

    @{Name='TimeGenerated';Expression={$_.ReplacementStrings[0]} 更改为简单的TimeGenerated,您应该一切就绪。

    ReplacementStrings 是来自Message 字段的变量。比如,下面的日志条目:

    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          : 
    

    替换字符串是消息属性中所有字段的值,例如“安全 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
    

    【讨论】:

    • 你的意思是像这样@TheMadTechnician @{Name='TimeGenerated'}
    • 哦,简直就是 TimeGenerated 这个词!好的,让我试一试。
    • 效果很好,非常感谢。在询问之前为此工作了 2 天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-10-05
    • 1970-01-01
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    相关资源
    最近更新 更多