summaryrefslogtreecommitdiffstats
path: root/LogActio/ThresholdWatch.py
blob: d5a5ee47e3da614efe32df3ee88615bcdc83e8c6 (plain)
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
#
#   logactio  -  simple framework for doing configured action on certain
#                log file events
#
#   Copyright 2012 - 2015   David Sommerseth <dazo@eurephia.org>
#
#   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, version 2
#   of the License.
#
#   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; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#   For the avoidance of doubt the "preferred form" of this code is one which
#   is in an open unpatent encumbered format. Where cryptographic key signing
#   forms part of the process of creating an executable the information
#   including keys needed to generate an equivalently functional executable
#   are deemed to be part of the source code.
#

import hashlib, time


class ThresholdType_Rule(object):
    def __init__(self, params):
        if "threshold" not in params:
            raise ValueError("Missing required 'threshold' parameter")

        self.__threshold = int(params["threshold"])
        self.__timeframe = params["timeframe"] and int(params["timeframe"]) or None
        self.__ratelimit = params["ratelimit"] and int(params["ratelimit"]) or None
        self.__lastseen = 0
        self.__lastsent = 0
        self.__currentcount = 0

    def CheckThreshold(self, alert, notused_regexmatch):
        now = int(time.time())
        self.__currentcount += 1
        ret = (self.__threshold == 0
                or ((self.__currentcount % self.__threshold == 0)
                    and (self.__timeframe is None
                         or now <= (self.__lastseen + self.__timeframe)))
                and (self.__ratelimit is None or now > (self.__lastsent + self.__ratelimit)))

        if (self.__timeframe and (self.__lastseen > 0)
            and (now >= (self.__lastseen + self.__timeframe))):
            # If the time-frame have timed out, reset it
            self.__lastseen = 0
        else:
            self.__lastseen = now

        return ret


    def ClearTimeTrackers(self, notused_regexmatch):
        self.__lastseen = 0
        self.__lastsent = 0


    def GetThreshold(self):
        return self.__threshold


    def GetCurrentCount(self, rgmatch):
        return self.__currentcount



class ThresholdType_Exact(object):
    def __init__(self, params):
        self.__matchedgroups = {}

        self.__threshold = int(params["threshold"])
        self.__timeframe = params["timeframe"] and int(params["timeframe"]) or None
        self.__ratelimit = params["ratelimit"] and int(params["ratelimit"]) or None

    def __gen_hash(self, regexmatch):
        v = "|".join(regexmatch)
        return hashlib.sha384(v.encode('utf-8')).hexdigest()


    def CheckThreshold(self, alert, regexmatch):
        now = int(time.time())
        ret = False

        # If threshold is 0 or 1, then we process all records
        if self.__threshold < 2:
            return True

        # Check if we have a regexmatch on this from earlier checks
        rghash = self.__gen_hash(regexmatch)
        if rghash in self.__matchedgroups:
            lastevent = self.__matchedgroups[rghash]
            lastevent["count"] += 1

            ret = ((lastevent["count"] % self.__threshold == 0)
                   and (self.__timeframe is None
                        or now <= (lastevent["lastseen"] + self.__timeframe))
                   and (self.__ratelimit is None or now > (lastevent["lastsent"] + self.__ratelimit)))

            try:
                if (self.__timeframe and 'lastseen' in lastevent
                    and (lastevent["lastseen"] > 0)
                    and (now >= (lastevent["lastseen"] + self.__timeframe))):
                    # If the time-frame have timed out, reset it
                    self.__lastseen = 0
                else:
                    self.__lastseen = now
            except KeyError as e:
                self.__lastseen = now

        else:
            # Not seen before, register it as a new one
            self.__matchedgroups[rghash] = {
                "count": 1,
                "lastseen": now,
                "lastsent": 0
                }
            ret = (1 % self.__threshold == 0)

        return ret


    def ClearTimeTrackers(self, regexmatch):
        rghash = self.__gen_hash(regexmatch)
        if rghash in self.__matchedgroups:
            self.__matchedgroups[rghash]["lasteen"] = 0
            self.__matchedgroups[rghash]["lastsent"] = 0


    def GetThreshold(self):
        return self.__threshold


    def GetCurrentCount(self, regexmatch):
        rghash = self.__gen_hash(regexmatch)
        if rghash in self.__matchedgroups:
            return self.__matchedgroups[rghash]["count"]
        else:
            return 0



class ThresholdWatch(object):
    WATCHTYPE_RULE = 1
    WATCHTYPE_EXACT = 2

    def __init__(self, watchtype, params):
        self.__watchtype = watchtype
        if watchtype == self.WATCHTYPE_RULE:
            self.__thresholdtype = ThresholdType_Rule(params)
        elif watchtype == self.WATCHTYPE_EXACT:
            self.__thresholdtype = ThresholdType_Exact(params)
        else:
            raise ValueError("Invalid watchtype parameter")


    def CheckThreshold(self, alert, regexmatch):
        return self.__thresholdtype.CheckThreshold(alert, regexmatch)


    def ClearTimeTrackers(self, regexmatch):
        self.__thresholdtype.ClearTimeTrackers(regexmatch)


    def GetThreshold(self):
        return self.__thresholdtype.GetThreshold()


    def GetCurrentCount(self, regexmatch):
        return self.__thresholdtype.GetCurrentCount(regexmatch)