Windows 事件日志 - 电子邮件通知

Mat*_*lls 7 windows windows-event-log alerting

当来自特定服务的特定严重性事件命中 Windows 服务器事件日志时,是否有一种简单的方法来发送电子邮件?这是在 Windows Server 2003 上,如果它有所不同的话。

注意,我们确实在我的工作场所为生产服务器提供了适当的监控和警报,但我们只需要一个开发中的服务的快速解决方案。

spl*_*tne 6

你可以用OSSEC来做到这一点,这是一个多平台的开源软件:

OSSEC 是一个完整的平台,用于监视和控制您的系统。它将 HIDS(基于主机的入侵检测)、日志监控和 SIM/SIEM 的所有方面混合在一个简单、强大和开源的解决方案中。

对于日志监控/警报:

实时和可配置的警报

OSSEC 允许客户配置他们想要收到警报的事件,这让他们可以专注于提高关键事件的优先级,而不是任何系统上的常规噪音。与 smtp、sms 和 syslog 的集成允许客户通过将警报发送到电子邮件和手持设备(如手机和寻呼机)来掌握警报。

[...]

您网络上的每个操作系统、应用程序和设备都会生成日志(事件),让您了解正在发生的事情。OSSEC 收集、分析和关联这些日志,让您知道是否发生了错误(攻击、误用、错误等)。

这是一篇关于 OSSEC on 360° Security文章


专业的商业替代品:EventTracker(Prism Microssystems):

EventTracker 是一种完整的安全信息和事件管理 (SIEM) 解决方案,它将实时日志管理与强大的配置和变更管理结合在一个统包软件包中。


Eva*_*son 5

这是我用其他几个脚本拼凑而成的另一个愚蠢的 VBScript 作品。

Option Explicit

' Main
Dim objShell, objWMIService, objEventSink, dictEventsToMonitor, eventToMonitor

' =====================( Configuration )=====================

' Set to 0 to disable event log reporting of bans / unbans
Const USE_EVENTLOG = 1
Const EVENTLOG_SOURCE = "SimpleEventMonitor"

' SMTP configuration
Const EMAIL_SENDER = "EventLogMonitor@company.com"
Const EMAIL_RECIPIENT = "recipient@company.com"
Const EMAIL_SMTP_SERVER = "smtp-server"
Const EMAIL_SMTP_PORT = 25
Const EMAIL_TIMEOUT = 20

Set dictEventsToMonitor = CreateObject("Scripting.Dictionary")

' Define events that should be monitored. Matches are based on exact matches of all non-NULL fields

' Monitor our own startup and alert based on starting
PushEventToMonitor "100", "Application", EVENTLOG_SOURCE, NULL, NULL, NULL, NULL
PushEventToMonitor "7036", "System", "Service Control Manager", NULL, NULL, NULL, "Telnet service.*(running|stopped).*state"

' ===================( End Configuration )===================


Set objShell = CreateObject("WScript.Shell")

' Create event sink to catchevents
Set objWMIService = GetObject("winmgmts:{(security)}!root/cimv2")
Set objEventSink = WScript.CreateObject("WbemScripting.SWbemSink", "eventSink_")
objWMIService.ExecNotificationQueryAsync objEventSink, "SELECT * FROM __InstanceCreationEvent WHERE TargetInstance ISA 'Win32_NTLogEvent'"

' Loop sleeping for one week, logging an event each week to say we're still alive
While (True)
    LogEvent 100, "INFORMATION", "Simple Event Log Monitor started"
    WScript.Sleep(7 * 24 * 60 * 60 * 1000)
Wend

' Fires each time new events are generated
Sub eventSink_OnObjectReady(objEvent, objWbemAsyncContext)
    Dim evt, field, boolAlert, regexpMessage

    For Each evt In dictEventsToMonitor.Keys
        boolAlert = True

        For Each field In dictEventsToMonitor.Item(evt).Keys
            If UCase(Field) = "MESSAGE" Then 
                Set regexpMessage = new Regexp
                regexpMessage.Pattern = dictEventsToMonitor.Item(evt).Item(Field)
                regexpMessage.IgnoreCase = True
                If NOT regexpMessage.Test(objEvent.TargetInstance.Properties_(Field)) then boolAlert = False
            Else
                If UCase(objEvent.TargetInstance.Properties_(Field)) <> UCase(dictEventsToMonitor.Item(evt).Item(field)) Then boolAlert = False
            End If
        Next ' field

    if boolAlert = True Then
        SendMessage "Simple Event Log Monitor notification from " & objEvent.TargetInstance.ComputerName, _
            "Event ID:       " & objEvent.TargetInstance.EventCode & VbCrLf _
            & "Date/Time:      " & Mid(objEvent.TargetInstance.TimeGenerated, 5, 2) & "/" & Mid(objEvent.TargetInstance.TimeGenerated, 7, 2) & "/" & Mid(objEvent.TargetInstance.TimeGenerated, 1, 4) & " " & Mid(objEvent.TargetInstance.TimeGenerated, 9, 2) & ":" & Mid(objEvent.TargetInstance.TimeGenerated, 11, 2) & ":" & Mid(objEvent.TargetInstance.TimeGenerated, 13, 2)   & VbCrLf _
            & "Computer:       " & objEvent.TargetInstance.ComputerName & vbCrLf _
            & "Event Log:      " & objEvent.TargetInstance.LogFile & vbCrLf _
            & "Event Source:   " & objEvent.TargetInstance.SourceName & vbCrLf _
            & "Event Category: " & objEvent.TargetInstance.CategoryString & vbCrLf _
            & "Event Type:     " & objEvent.TargetInstance.Type & vbCrLf _
            & "User Name:      " & objEvent.TargetInstance.User & vbCrLf _
            & "Message:" & vbCrLf & vbCrLF _
            & objEvent.TargetInstance.Message
        Exit Sub
    End If

    Next ' evt
End Sub

Sub LogEvent(ID, EventType, Message)
    ' Log an event to the Windows event log
    If USE_EVENTLOG Then objShell.Exec "EVENTCREATE /L APPLICATION /SO " & EVENTLOG_SOURCE & " /ID " & ID & " /T " & EventType & " /D """ & Message & """"
End Sub

Sub SendMessage(strSubject, strBody)
    Dim objCDOMessage
    Set objCDOMessage = CreateObject("CDO.Message")

    objCDOMessage.From = EMAIL_SENDER
    objCDOMessage.To = EMAIL_RECIPIENT
    objCDOMessage.Subject = strSubject
    objCDOMessage.Textbody = strBody
    objCDOMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = EMAIL_SMTP_SERVER
    objCDOMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = EMAIL_SMTP_PORT
    objCDOMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = EMAIL_TIMEOUT
    objCDOMessage.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
    objCDOMessage.Configuration.Fields.Update
    objCDOMessage.send
End Sub

Sub PushEventToMonitor(strID, strLog, strSource, strCategory, strType, strUser, strMessagePattern)
    Dim x

    x = dictEventsToMonitor.Count
    Set dictEventsToMonitor.Item(x) = CreateObject("Scripting.Dictionary")
    If NOT IsNull(strID) Then dictEventsToMonitor.Item(x).Add "EventCode", strID
    If NOT IsNull(strLog) Then dictEventsToMonitor.Item(x).Add "LogFile", strLog
    If NOT IsNull(strSource) Then dictEventsToMonitor.Item(x).Add "SourceName", strSource
    If NOT IsNull(strCategory) Then dictEventsToMonitor.Item(x).Add "CategoryString", strCategory
    If NOT IsNull(strType) Then dictEventsToMonitor.Item(x).Add "Type", strType
    If NOT IsNull(strType) Then dictEventsToMonitor.Item(x).Add "User", strUser
    If NOT IsNull(strMessagePattern) Then dictEventsToMonitor.Item(x).Add "Message", strMessagePattern
End Sub
Run Code Online (Sandbox Code Playgroud)

如果您使用Non-Sucking Service Manager或 SRVANY 等工具来安装它,则可以将其作为 Windows 服务运行。使用 NSSM,命令行将是:

nssm install SimpleEventLogMonitor %SystemRoot%\System32\cscript.exe "\"Pull_path_and_filename_of_script\""
Run Code Online (Sandbox Code Playgroud)

请务必替换您的电子邮件收件人、发件人和 SMTP 服务器名称。

您可以通过“PushEventToMonitor”调用来定义要收到警报的事件。参数包括:事件 ID、事件日志名称、源、类别、类型、用户以及可与日志消息匹配的正则表达式。我在那里有一个与 TELNET 服务的启动/停止相匹配的示例,以及一个与脚本本身的启动相匹配的示例(将事件记录到应用程序日志中)。

这是初稿,因为我为客户写的实际上“正在生产”的草稿是用他们的钱写的并且“属于”他们。因此,我重新编码了这个代码(实际上与客户使用的代码有很大不同),并且它很可能潜伏着愚蠢的错误。今晚我已经在我的一些系统上运行了一段时间,没有发现任何问题。

也许我最终会让这变得更好一点。如果它将其配置从注册表中提取出来(这样就可以通过组策略进行控制)并且将其打包为 MSI 以便轻松部署到服务器组,那就太好了。那好吧。