首页 > 解决方案 > 使用 powershell 检查日志文件

问题描述

我有一个特殊的任务,我不知道解决这个任务的最佳主意是什么。

我们的环境中有一个 syslog 服务器,它从服务器收集所有 Windows 日志。

我们可以对 Syslog 服务器进行 API 调用,它返回一个带有属性“Content”的 JSON(包括整个日志条目)。

现在我遇到了以下情况:我们正在 VDA 服务器上记录失败的登录,现在我们想计算在日志系统中有条目的每个用户在一小时内用户登录失败的次数。

例如: 用户 tes_baa 有 3 次失败的登录尝试 用户 aas_eaa 有 2 次失败的登录尝试

但我的问题是,我不能轻易通过 Syslog 服务器返回的 JSON 文件,因为用户名是动态的(看看 JSON 文件的“内容”属性)我的想法是创建一个正则表达式过滤每个 JSON 的用户名并将用户名添加到数组并计算数组。为什么是正则表达式?因为每个用户名都有以下语法:“3 Characters_Name”(在 sqa_augstb 下方的文件中)或者您的想法是什么?

    [20:12:38] [INF] An account failed to log on.

Subject:
    Security ID:        S-1-0-0
    Account Name:       -
    Account Domain:     -
    Logon ID:       0x0

Logon Type:         3

Account For Which Logon Failed:
    Security ID:        S-1-0-0
    Account Name:       sqa_augstb
    Account Domain:     

Failure Information:
    Failure Reason:     Unknown user name or bad password.
    Status:         0xC000006D
    Sub Status:     0xC000006A

Process Information:
    Caller Process ID:  0x0
    Caller Process Name:    -

Network Information:
    Workstation Name:   VDASW2
    Source Network Address: 10.10.2.127
    Source Port:        60973

Detailed Authentication Information:
    Logon Process:      NtLmSsp 
    Authentication Package: NTLM
    Transited Services: -
    Package Name (NTLM only):   -
    Key Length:     0

This event is generated when a logon request fails. It is generated on the computer where access was attempted.

The Subject fields indicate the account on the local system which requested the logon. This is most commonly a service such as the Server service, or a local process such as Winlogon.exe or Services.exe.

The Logon Type field indicates the kind of logon that was requested. The most common types are 2 (interactive) and 3 (network).

The Process Information fields indicate which account and process on the system requested the logon.

The Network Information fields indicate where a remote logon request originated. Workstation name is not always available and may be left blank in some cases.

The authentication information fields provide detailed information about this specific logon request.
    - Transited services indicate which intermediate services have participated in this logon request.
    - Package name indicates which sub-protocol was used among the NTLM protocols.
    - Key length indicates the length of the generated session key. This will be 0 if no session key was requested. 

标签: powershell

解决方案


正如在我的评论中,我不是超级好,regex但假设帐户名称没有.或空格\s,你可以使用这个正则表达式:

假设日志文件保存在变量上$log

PS /> ([regex]'\w{3}_\w+').Matches($log)

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 292
Length   : 10
Value    : sqa_august
  • \w\w 匹配任何单词字符(相当于[a-zA-Z0-9_]
  • {3}与前一个标记完全匹配 3 次
  • __从字面上匹配字符
  • \w+匹配一次到无限次之间的任何单词字符,尽可能多次,根据需要回馈(贪婪)

假设它可以有空格\s.你可以使用这个正则表达式:

PS /> ([regex]'\w{3}_[\w?\.?\s]+[\r\n]{1}').Matches($log)

Groups   : {0}
Success  : True
Name     : 0
Captures : {0}
Index    : 292
Length   : 17
Value    : sqa_ a..u.gu st
  • ?匹配前一个元素零次或一次
  • \.匹配文字点
  • \s匹配空格
  • [\w?\.?\s]+该组将匹配任何可能后跟或不跟在一次\.\s无限次之间的单词
  • [\r\n]{1}匹配一次carriage returnnew line恰好 1 次。我添加了这个,所以它知道在哪里停止,如果我们删除它,它将继续匹配新行......可能有更好的方法:P

.trim()或将.trimEnd()有必要删除由生成的尾随空格[\r\n]{1}


推荐阅读