/usr/share/pyshared/PreludeCorrelator/context.py is in prelude-correlator 1.0.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | # Copyright (C) 2009 PreludeIDS Technologies. All Rights Reserved.
# Author: Yoann Vandoorselaere <yoann.v@prelude-ids.com>
#
# This file is part of the Prelude-Correlator program.
#
# This program 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; either version 2, or (at your option)
# any later version.
#
# This program 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 program; see the file COPYING. If not, write to
# the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
import os, time, StringIO, pickle
from PreludeCorrelator.idmef import IDMEF
from PreludeCorrelator import require
_TIMER_LIST = [ ]
_CONTEXT_TABLE = { }
class Timer:
def __setstate__(self, dict):
self.__dict__.update(dict)
if self._timer_start:
_TIMER_LIST.append(self)
def __init__(self, expire, cb_func=None):
self._timer_start = None
self._timer_expire = expire
self._timer_cb = cb_func
def _timerExpireCallback(self):
self.stop()
try:
self._timer_cb(self)
except:
pass
def hasExpired(self, now=time.time()):
return self.elapsed(now) >= self._timer_expire
def check(self, now=time.time()):
if self.hasExpired(now):
self._timerExpireCallback()
def elapsed(self, now=time.time()):
return now - self._timer_start
def running(self):
return self._timer_start != None
def setExpire(self, expire):
self._timer_expire = expire
def start(self):
if not self._timer_start and self._timer_expire > 0:
self._timer_start = time.time()
_TIMER_LIST.append(self)
def stop(self):
if self._timer_start:
_TIMER_LIST.remove(self)
self._timer_start = None
def reset(self):
self.stop()
self.start()
class Context(IDMEF, Timer):
FORMAT_VERSION = 0.1
def __setstate__(self, dict):
Timer.__setstate__(self, dict)
IDMEF.__setstate__(self, dict)
def __init__(self, name, options={}, overwrite=True, update=False, idmef=None):
already_initialized = (update or (overwrite is False)) and hasattr(self, "_name")
if already_initialized is True:
return
self._version = self.FORMAT_VERSION
self._options = { "threshold": -1, "expire": 0, "alert_on_expire": False }
IDMEF.__init__(self)
Timer.__init__(self, 0)
name = getName(name)
self._name = name
self._update_count = 0
if _CONTEXT_TABLE.has_key(name): # Make sure any timer is deleted on overwrite
_CONTEXT_TABLE[name].destroy()
_CONTEXT_TABLE[name] = self
self._options.update(options)
self.setOptions(self._options)
if idmef:
self.addAlertReference(idmef)
def __new__(cls, name, options={}, overwrite=True, update=False, idmef=None):
if update or (overwrite is False):
ctx = search(name)
if ctx:
if update:
ctx.update(options, idmef)
return ctx
if overwrite is False:
return ctx
return super(Context, cls).__new__(cls)
def _timerExpireCallback(self):
threshold = self._options["threshold"]
alert_on_expire = self._options["alert_on_expire"]
if alert_on_expire:
if threshold == -1 or (self._update_count + 1) >= threshold:
if callable(alert_on_expire):
alert_on_expire(self)
return
else:
self.alert()
self.destroy()
def isVersionCompatible(self):
version = self.__dict__.get("_version", None)
return self.FORMAT_VERSION == version
def update(self, options={}, idmef=None):
self._update_count += 1
if idmef:
self.addAlertReference(idmef)
if self.running():
self.reset()
self._options.update(options)
self.setOptions(self._options)
def stats(self, log_func, now=time.time()):
str = ""
if self._options["threshold"] != -1:
str += " threshold=%d/%d" % (self._update_count + 1, self._options["threshold"])
if self._timer_start:
str += " expire=%d/%d" % (self.elapsed(now), self._options["expire"])
log_func("[%s]: update=%d%s" % (self._name, self._update_count, str))
def getOptions(self):
return self._options
def setOptions(self, options={}):
self._options = options
Timer.setExpire(self, self._options.get("expire", 0))
Timer.start(self) # will only start the timer if not already running
def getUpdateCount(self):
return self._update_count
def destroy(self):
if isinstance(self, Timer):
self.stop()
del(_CONTEXT_TABLE[self._name])
def getName(arg):
def escape(s):
return s.replace("_", "\\_")
if type(arg) is str:
return escape(arg)
cnt = 0
name = ""
for i in arg:
if cnt > 0:
name += "_"
name += escape(str(i))
cnt += 1
return name
def search(name):
name = getName(name)
if _CONTEXT_TABLE.has_key(name):
return _CONTEXT_TABLE[name]
return None
_ctxt_filename = require.get_data_filename(None, "context.dat")
def save():
fd = open(_ctxt_filename, "w")
pickle.dump(_CONTEXT_TABLE, fd)
fd.close()
def load():
if os.path.exists(_ctxt_filename):
fd = open(_ctxt_filename, "r")
try:
_CONTEXT_TABLE.update(pickle.load(fd))
except EOFError:
return
for ctx in _CONTEXT_TABLE.values():
if not ctx.isVersionCompatible():
ctx.destroy()
def wakeup(now):
for timer in _TIMER_LIST:
timer.check(now)
def stats(logger):
now = time.time()
with_threshold = []
for ctx in _CONTEXT_TABLE.values():
if ctx._options["threshold"] == -1:
ctx.stats(logger.info, now)
else:
with_threshold.append(ctx)
with_threshold.sort(lambda x, y: x.getUpdateCount() - y.getUpdateCount())
for ctx in with_threshold:
ctx.stats(logger.info, now)
|