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
/
Detector.py
464 lines (385 loc) · 16.3 KB
/
Detector.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
#
# 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 commands
import re
import socket
import threading
import time
from pytz import all_timezones
#
# LOCAL IMPORTS
#
import Config
from Logger import Logger, Lazyformat
from Output import Output
from Stats import Stats
from Threshold import EventConsolidation
import Utils
#
# GLOBAL VARIABLES
#
logger = Logger.logger
DEFAULT_PLUGIN_SID = 20000000
class Detector(threading.Thread):
def __init__(self, conf, plugin, conn):
self._conf = conf
self._plugin = plugin
self.os_hash = {}
self.conn = conn
self.consolidation = EventConsolidation(self._conf)
self.systemtzone = None
self.__plugin_id = self._plugin.get("DEFAULT", "plugin_id")
self.__plugin_name = self._plugin.get("config", "name")
if "tzone" in self._plugin.hitems("DEFAULT"):
self._timezone = self._plugin.get("DEFAULT", "tzone")
else:
self._timezone = self._conf.get("plugin-defaults", "tzone")
self._sensorID = None
if "sensor_id" in self._conf.hitems("plugin-defaults"):
self._sensorID = self._conf.get("plugin-defaults", "sensor_id")
self.loginfo(Lazyformat("Starting plugin with the following tzone: {}", self._timezone))
threading.Thread.__init__(self)
self._agenttimezone = self._conf.get("plugin-defaults", "tzone")
self._EventTimeZone = None
if self._conf.has_option("plugin-defaults", "override_sensor"):
self.override_sensor = self._conf.getboolean("plugin-defaults", "override_sensor")
else:
self.override_sensor = False
# 2011-02-01 17:00:16
self.patterndate = re.compile('(\d{10})')
self.patternISO_date = re.compile(
'(?P<year>\d+)[\s-](?P<month>\d+)[\s-](?P<day>\d+)\s+(?P<hour>\d+):(?P<minute>\d+):(?P<second>\d+)')
self.set_system_tzone()
self.checkTimeZone()
def __isDigit(self, value):
"""Checks if value is a digit
@returns true if value is a number, otherwise false
"""
rt = True
try:
int(value)
except:
rt = False
return rt
def set_system_tzone(self):
"""Sets the system timezone by reading the timezone """
try:
# read local timezone information.
f = open('/etc/timezone', 'r')
used_tzone = f.readline().rstrip()
f.close()
if used_tzone not in all_timezones:
self.loginfo("Failed to get valid timezone. Falling back to the default one: GMT")
used_tzone = 'GMT'
except Exception, e:
used_tzone = 'GMT'
self.loginfo("Failed to get the timezone. Falling back to the default one: GMT")
self.systemtzone = used_tzone
def checkTimeZone(self):
if self._timezone in all_timezones:
used_tzone = self._timezone
self.logdebug(Lazyformat("Using custom plugin tzone data: {}", used_tzone))
elif self._agenttimezone in all_timezones:
used_tzone = self._agenttimezone
self.loginfo(Lazyformat(
"Failed to find the {} timezone in database. Falling back to agent timezone: {}",
self._timezone,
used_tzone
))
else:
self.set_system_tzone()
used_tzone = self.systemtzone
self._EventTimeZone = used_tzone
def _event_os_cached(self, event):
return False
def _exclude_event(self, event):
if self._plugin.has_option("config", "exclude_sids"):
exclude_sids = self._plugin.get("config", "exclude_sids")
if event["plugin_sid"] in Config.split_sids(exclude_sids):
self.logdebug(Lazyformat(
"Excluding event with plugin_id={} and plugin_sid={}",
event["plugin_id"],
event["plugin_sid"]
))
return True
return False
def _thresholding(self):
"""
This section should contain:
- Absolute thresholding by plugin, src, etc...
- Time based thresholding
- Consolidation
"""
self.consolidation.process()
def _getLocalIP(self):
if self.override_sensor:
self.loginfo("override_sensor detected")
if self._conf.has_section("plugin-defaults"):
mylocalip = self._conf.get("plugin-defaults", "sensor")
return mylocalip
hostname, aliaslist, ipaddrlist = socket.gethostbyname_ex(socket.gethostname())
for ip in ipaddrlist:
if not ip.startswith("127"):
return ip
# In this case we try to parse the output of ip a
lines = commands.getoutput("ip a | grep inet | grep -v inet6 | awk '{print $2}'| grep -v \"127.0.0.1\" | awk -F '/' '{print $1}'").split("\n")
if len(lines) > 0:
self.loginfo(Lazyformat("Using sensor ip: {}", lines[0]))
return lines[0]
def isIPV6(self, string_ip):
ipv6 = True
try:
socket.inet_pton(socket.AF_INET6, string_ip)
except:
ipv6 = False
return ipv6
def _plugin_defaults(self, event):
ipv4_reg = "^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$"
default_date_format = '%Y-%m-%d %H:%M:%S'
default_ip = '0.0.0.0'
# get default values from config
if self._sensorID is not None:
event['sensor_id'] = self._sensorID
if 'plugin_sid' in event.EVENT_ATTRS:
if event['plugin_sid'] == "0":
event['plugin_sid'] = DEFAULT_PLUGIN_SID
if not self.__isDigit(event['plugin_sid']) and not str(event).startswith("idm-event"):
self.logwarn(Lazyformat("Event discarded [{!s}] Plugin sid not a number...", event))
return ""
if not self.__isDigit(event['plugin_id']) and not str(event).startswith("idm-event"):
self.logwarn(Lazyformat("Event discarded [{!s}] Plugin id not a number... ", event))
return ""
# 1) date
if self._conf.has_section("plugin-defaults"):
default_date_format = self._conf.get("plugin-defaults",
"date_format")
if event["date"] is None and default_date_format and 'date' in event.EVENT_ATTRS:
event["date"] = time.strftime(default_date_format,
time.localtime(time.time()))
# 2) sensor
default_sensor = self._conf.get("plugin-defaults", "sensor")
if event["sensor"] is None and default_sensor and \
'sensor' in event.EVENT_ATTRS and not self.override_sensor:
event["sensor"] = default_sensor
# 3) interface
default_iface = self._conf.get("plugin-defaults", "interface")
if event["interface"] is None and default_iface and 'interface' in event.EVENT_ATTRS:
event["interface"] = default_iface
# 4) source ip
if event["src_ip"] is None and 'src_ip' in event.EVENT_ATTRS:
event["src_ip"] = event["sensor"]
# 5) Time zone
if 'tzone' in event.EVENT_ATTRS:
Utils.normalizeToUTCDate(event, self._EventTimeZone)
# 6) sensor,source ip and dest != localhost
if event["sensor"] in ('127.0.0.1', '127.0.1.1', '::1') and not self.override_sensor:
event["sensor"] = default_sensor
if event["dst_ip"] in ('127.0.0.1', '127.0.1.1', '::1') and 'dst_ip' in event.EVENT_ATTRS:
event["dst_ip"] = event['device']
if event["src_ip"] in ('127.0.0.1', '127.0.1.1', '::1', 'localhost') and 'src_ip' in event.EVENT_ATTRS:
event["src_ip"] = event['device']
# Check if valid ip, if not we put 0.0.0.0 in sensor field
if event['src_ip'] is not None and 'src_ip' in event.EVENT_ATTRS:
# check for ipv6
if not re.match(ipv4_reg, event['src_ip']) and not self.isIPV6(event['src_ip']):
data = event['src_ip']
event['src_ip'] = default_ip
self.logwarn(Lazyformat(
"Event's field src_ip {} is not a valid IP.v4/IP.v6 address, falling back to default: 0.0.0.0",
data
))
elif 'src_ip' in event.EVENT_ATTRS:
event['src_ip'] = default_ip
# Check if valid ip, if not we put 0.0.0.0 in sensor field
if event['dst_ip'] is not None and 'dst_ip' in event.EVENT_ATTRS:
if not re.match(ipv4_reg, event['dst_ip']) and not self.isIPV6(event['dst_ip']):
data = event['dst_ip']
self.logwarn(Lazyformat(
"Event's field dst_ip {} is not a valid IP.v4/IP.v6 address, falling back to default: 0.0.0.0",
data
))
event['dst_ip'] = default_ip
elif 'dst_ip' in event.EVENT_ATTRS:
event['dst_ip'] = default_ip
# Check if valid ip, if not we put 0.0.0.0 in sensor field
if event['sensor'] is not None and not self.override_sensor:
if not re.match(ipv4_reg, event['sensor']) and not self.isIPV6(event['sensor']):
data = event['sensor']
self.logwarn(Lazyformat(
"Event's field sensor {} is not a valid IP.v4/IP.v6 address, falling back to default local",
data
))
event['sensor'] = self._getLocalIP()
else:
event['sensor'] = self._getLocalIP()
# Check for device valid IP
if event['device'] is not None:
if not re.match(ipv4_reg, event['device']) and not self.isIPV6(event['device']):
data = event['device']
self.logwarn(Lazyformat(
"Event's device field {} is not a valid IP.v4/IP.v6 address, falling back to default local.",
data)
)
if event['sensor'] is not None:
event['device'] = event['sensor']
else:
event['device'] = self._getLocalIP()
else:
if event['sensor'] is not None:
event['device'] = event['sensor']
else:
event['device'] = self._getLocalIP()
# check port
if event['dst_port'] is not None and 'dst_port' in event.EVENT_ATTRS:
try:
port = int(event['dst_port'])
if port < 0 or port > 65535:
event['dst_port'] = None
self.logwarn(Lazyformat(
"Event's field dst_port is out of range: {} (PID:{} SID:{})",
port,
event["plugin_id"],
event["plugin_sid"]
))
except ValueError:
# Do not show warnings on empty values
if event['dst_port'] != '':
self.logwarn(Lazyformat(
"Event's field dst_port is not a number: {} (PID:{} SID:{})",
event['dst_port'],
event['plugin_id'],
event['plugin_sid']
))
event['dst_port'] = None
if event['src_port'] is not None and 'src_port' in event.EVENT_ATTRS:
try:
port = int(event['src_port'])
if port < 0 or port > 65535:
event['src_port'] = None
self.logwarn(Lazyformat(
"Event's field src_port is out of range: {} (PID:{} SID:{})",
port,
event['plugin_id'],
event['plugin_sid']
))
except ValueError:
# Do not show warnings on empty values
if event['src_port'] != '':
self.logwarn(Lazyformat(
"Event's field src_port is not a number: {} (PID:{} SID:{})",
event['src_port'],
event['plugin_id'],
event['plugin_sid']
))
event['src_port'] = None
if event['tzone'] is None:
event['tzone'] = 0
# the type of this event should always be 'detector'
if event["type"] is None and 'type' in event.EVENT_ATTRS:
event["type"] = 'detector'
if not str(event).startswith("idm-event") and self.should_set_system_date(event["date"], event["fdate"]):
self.logwarn("Invalid plugin date... using system date...")
event["date"] = time.strftime(default_date_format, time.localtime(time.time()))
Utils.normalizeToUTCDate(event, self.systemtzone)
return event
def should_set_system_date(self, event_date, event_fdate):
"""Checks whether the system date should be set"""
if event_date is None or event_fdate is None:
return True
if not self.patterndate.match(str(event_date)) or not self.patternISO_date.match(event_fdate):
return True
return False
def send_message(self, event):
if self._event_os_cached(event):
return
if self._exclude_event(event):
return
# use default values for some empty attributes
event = self._plugin_defaults(event)
Output.event(event)
Stats.new_event(event)
return
# check for consolidation
# if self.conn is not None:
# try:
# self.conn.send(str(event))
# except:
# id = self._plugin.get("config", "plugin_id")
# c = ServerConnPro(self._conf, id)
# self.conn = c.connect(0, 10)
# try:
# self.conn.send(str(event))
# except:
# return
#
# self.loginfo(str(event).rstrip())
#
# elif not self.consolidation.insert(event):
# Output.event(event)
#
# Stats.new_event(event)
def stop(self):
# self.consolidation.clear()
pass
def process(self):
"""Process method placeholder.
NOTE: Must be overridden in child classes.
"""
pass
def run(self):
self.process()
def loginfo(self, message):
logger.info(self.label(message))
def logdebug(self, message):
logger.debug(self.label(message))
def logwarn(self, message):
logger.warning(self.label(message))
def logerror(self, message):
logger.error(self.label(message))
def label(self, message):
return Lazyformat(
"{}[{}] {}",
self.__plugin_name,
self.__plugin_id,
message
)
class ParserSocket(Detector):
def process(self):
self.process()
class ParserDatabase(Detector):
def process(self):
self.process()
class ParserWMI(Detector):
def process(self):
self.process()
# vim:ts=4 sts=4 tw=79 expandtab: