This repository has been archived by the owner on Apr 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ParserWMI.py
280 lines (255 loc) · 11.8 KB
/
ParserWMI.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env python
#
# License:
#
# Copyright (c) 2003-2006 ossim.net
# Copyright (c) 2007-2014 AlienVault
# All rights reserved.
#
# This package is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 dated June, 1991.
# You may not use, modify or distribute this program under any other version
# of the GNU General Public License.
#
# This package is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
# MA 02110-1301 USA
#
#
# On Debian GNU/Linux systems, the complete text of the GNU General
# Public License can be found in `/usr/share/common-licenses/GPL-2'.
#
# Otherwise you can read it here: http://www.gnu.org/licenses/gpl-2.0.txt
#
#
# GLOBAL IMPORTS
#
import os
import sys
import time
import re
import socket
import commands
from time import sleep
#
# LOCAL IMPORTS
#
from Detector import Detector
from Event import Event
from Logger import Lazyformat
class ParserWMI(Detector):
'''
WMI EventLog Parser, adapted from Database Parser
TODO:
TRANSLATIONS support
Testing
Make sure it works with more wmi stuff, not just windows log files
Test with different languages / windows versions
'''
LAST_RECORD_FILE_TMP = "/etc/ossim/agent/wmi_%s_%s"
VALID_SECTIONS = ["Application", "Security","System"]
CMD_CHECK_SECTION = "wmic -U %s%%%s //%s \"SELECT LogfileName FROM Win32_NTEventLogFile where LogfileName='%s'\""
# To get las record by time: "Select TimeWritten from Win32_NTLogEvent Where Logfile = 'Application' and TimeWritten >\"20110803103502.000000-000\""
CMD_GET_LAST_RECORD = "wmic -U %s%%%s //%s \"Select LogFile,RecordNumber from Win32_NTLogEvent Where Logfile = '%s'\" | head -n 3 | tail -n 1 | cut -f 2 -d \|"
CMD = "wmic -U %s%%%s //%s %s"
def __init__(self, conf, plugin, conn, hostname, username, password):
self.__conf = conf
self.__plugin = plugin
self.__rules = [] # list of RuleMatch objects
self.__conn = conn
self.__hostname = hostname
self.__username = username
self.__password = password.strip()
self.__section = self.__plugin.get("config", "section")
self.__last_record_time = ""
if self.__section == "":
#search into the command to find the section
rules = self.__plugin.rules()
cmd_str = rules['cmd']['cmd']
for sec in ParserWMI.VALID_SECTIONS:
if cmd_str.find(sec)>=0:
self.__section = sec
self.logwarn(Lazyformat(
"The section was not found in [config]. Section deduced: {}",
self.__section
))
break
if self.__section == "":
self.__section = "Security"
self.logwarn(Lazyformat("The section was not found in [config]. It can't be deduced: applying default value: {}", self.__section))
self.__pluginID = self.__plugin.get("DEFAULT", "plugin_id")
self.__stop_processing = False
self.__sectionExists = False
Detector.__init__(self, conf, plugin, conn)
def existsLogForSeciton(self):
"""
Checks whether the specified section exists.
"""
returnValue = False
"""
Example query output:
CLASS: Win32_NTEventlogFile
LogfileName|Name
Application|C:\WINDOWS\system32\config\AppEvent.Evt
Security|C:\WINDOWS\System32\config\SecEvent.Evt
System|C:\WINDOWS\system32\config\SysEvent.Evt
"""
self.__plugin.get("config", "section")
query= ParserWMI.CMD_CHECK_SECTION % (self.__username, self.__password, self.__hostname,self.__section)
status,output = commands.getstatusoutput(query)
if status != 0 or output =="":
self.logwarn(Lazyformat("An error occurred while trying to get logs from: {} - status:{} --output: {}", self.__hostname, status, output))
else:
returnValue = True
return returnValue
def getLastRecordTimeString(self):
"""
Gets the last record time
# To get las record by time: "Select TimeWritten from Win32_NTLogEvent Where Logfile = 'Application' and TimeWritten >\"20110803103502.000000-000\""
"""
last_record_file = ParserWMI.LAST_RECORD_FILE_TMP % (self.__hostname,self.__section)
self.__last_record_time = ""
if os.path.exists(last_record_file):
file = open(last_record_file,'r')
data = file.readline()
self.logdebug(Lazyformat("Last record time: {}", data.rstrip()))
self.__last_record_time = data.rstrip()
file.close()
if self.__last_record_time != "":
return
#cmd_run = "wmic -U %s%%%s //%s \"Select TimeWritten from Win32_NTLogEvent Where Logfile = '%s'\" | sort | head -n 1 | tr \"|\" \" \" |awk '{print$3;}'" % (self.__username, self.__password, self.__hostname, self.__section)
cmd_run = "wmic -U %s%%%s //%s \"Select TimeWritten from Win32_NTLogEvent Where Logfile = '%s'\" | grep %s | tr \"\|\" \" \" |awk '{print$3;}' | sort -r | head -n 1" % (self.__username, self.__password, self.__hostname, self.__section,self.__section)
status, output = commands.getstatusoutput(cmd_run)
if status != 0 or output == "":
self.logwarn(Lazyformat(
"[GET_LAST_RECORD] An error occurred while trying to get logs from: {}, section: {}",
self.__hostname,
self.__section
))
self.__last_record_time = ""
else:
self.logdebug(Lazyformat("[GET_LAST_RECORD] Last record time: {}", output))
self.__last_record_time = output
self.updateLastRecordTimeString()
def getLastRecord(self):
"""
Gets the last record.
"""
last_record = 0
query = ParserWMI.CMD_GET_LAST_RECORD % (self.__username, self.__password, self.__hostname, self.__section)
status, output = commands.getstatusoutput(query)
if status != 0:
self.logwarn(Lazyformat(
"[GET_LAST_RECORD] An error occurred while trying to get logs from: {}, section: {}",
self.__hostname,
self.__section
))
elif output =="":
last_record = 0
else:
last_record = output
return last_record
def updateLastRecordTimeString(self):
last_record_file = ParserWMI.LAST_RECORD_FILE_TMP % (self.__hostname,self.__section)
thefile = open(last_record_file,'w')
thefile.write(self.__last_record_time + "\n")
self.logdebug(Lazyformat("Updating last_record_file: {}", self.__last_record_time))
thefile.close()
def process(self):
if self.__section not in ParserWMI.VALID_SECTIONS:
self.logerror(Lazyformat("Unable to process invalid section: '{}'. Exiting...", self.__section))
return
rules = self.__plugin.rules()
sleep_time = self.__plugin.get("config", "sleep")
cmd = rules['cmd']['cmd']
real_cmd = rules['cmd']['cmd']
if cmd == "":
self.logerror("Uanble to process - command is not specified. Exiting...")
return
old_mode_active = True
last_record = 0
if cmd.find("OSS_TIME") > 0:
old_mode_active = False
self.getLastRecordTimeString()
else:
last_record = self.getLastRecord()
cmd = cmd.replace("OSS_WMI_USER", self.__username)
cmd = cmd.replace("OSS_WMI_PASS", self.__password)
cmd = cmd.replace("OSS_WMI_HOST", self.__hostname)
cmd = cmd.replace("OSS_COUNTER", str(last_record))
cmd = cmd.replace("OSS_TIME", self.__last_record_time)
regex = rules['cmd']['regexp']
start_regexp = rules['cmd']['start_regexp']
splitter = re.compile('(?<!\r)\n') # Split on \n unless it's preceded by \r
cregexp = re.compile(regex)
while not self.__stop_processing:
"""
CLASS: Win32_NTLogEvent
ComputerName|EventCode|Logfile|Message|RecordNumber|SourceName|TimeWritten|User
<domainName>|15|Application|La inscripcion de certificados automotica para Sistema local no puede ponerse en contacto con el directorio activo (0x8007054b) El dominio especificado no existe o no se pudo establecer conexion con el.
. La inscripcion no se efectuar.
|68|AutoEnrollment|20110708052118.000000+120|(null)
"""
self.logdebug(Lazyformat("Fetching WMI data, section: {}", self.__section))
status,output = commands.getstatusoutput(cmd)
if output != "" and len(output) > 1:
data = splitter.split(output)
cval_helper = 1
for log in data:
log = log.encode('string_escape')
log = log.replace("\r\n"," ")
result = cregexp.search(log)
if result is None:
continue
else:
if cval_helper == 1:
# Only calculate cVal for first row since logs come out reversed
if old_mode_active:
last_record = str(int(result.groups()[4]))
else:
self.__last_record_time = result.groups()[6]
cval_helper = 0
groups =[]
for group in result.groups():
groups.append(group.decode('utf-8'))
self.generate(groups,log)
else:
self.logdebug(Lazyformat("Fetching WMI data, section:{} - No data", self.__section))
if not self.__stop_processing:
if not old_mode_active:
self.updateLastRecordTimeString()
time.sleep(int(sleep_time))
cmd = real_cmd
cmd = cmd.replace("OSS_WMI_USER", self.__username)
cmd = cmd.replace("OSS_WMI_PASS", self.__password)
cmd = cmd.replace("OSS_WMI_HOST", self.__hostname)
cmd = cmd.replace("OSS_COUNTER", str(last_record))
cmd = cmd.replace("OSS_TIME", self.__last_record_time)
self.updateLastRecordTimeString()
self.loginfo("Processing finished")
def generate(self, groups,log):
event = Event()
rules = self._plugin.rules()
for key, value in rules['cmd'].iteritems():
if key != "cmd" and key != "regexp" and key != "ref" and key != "start_regexp":
event[key] = self._plugin.get_replace_array_value(value.encode('utf-8'), groups)
#event[key] = self.get_replace_value(value, groups)
#self.plugin.get_replace_value
if log and not event['log'] and "log" in event.EVENT_ATTRS:
event['log'] = log.encode('utf-8')
if event is not None:
self.send_message(event)
def stop(self):
self.loginfo("Scheduling plugin stop")
self.__stop_processing = True
try:
self.join()
except RuntimeError:
self.logwarn("Stopping thread that likely hasn't started.")