File: packets_mod.py

package info (click to toggle)
epylog 1.0.7-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 648 kB
  • sloc: python: 4,442; makefile: 491; perl: 194; sh: 178; xml: 78
file content (303 lines) | stat: -rw-r--r-- 12,120 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/python -tt
"""
Description will eventually go here.
"""
##
# Copyright (C) 2003 by Duke University
#
# 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
# of the License, 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; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.
#
# $Id$
#
# @Author Konstantin Ryabitsev <icon@linux.duke.edu>
# @version $Date$
#

import sys
import re

##
# This is for testing purposes, so you can invoke this from the
# modules directory. See also the testing notes at the end of the
# file.
#
sys.path.insert(0, '../py/')
from epylog import Result, InternalModule

class packets_mod(InternalModule):
    def __init__(self, opts, logger):
        InternalModule.__init__(self)
        self.logger = logger
        rc = re.compile
        iptables_map = {
            rc('IN=\S*\sOUT=\S*\sMAC=\S*\sSRC=\S*\sDST=\S*\s'): self.iptables
            }
        ipchains_map = {
            rc('Packet\slog:\s.*PROTO.*'): self.ipchains
            }
        ipfilter_map = {
            rc('ipmon\[\d+\]:'): self.ipfilter
            }

        self.regex_map = {}
        if opts.get('enable_iptables', '1') == '1':
            self.regex_map.update(iptables_map)
        if opts.get('enable_ipchains', '0') == '1':
            self.regex_map.update(ipchains_map)
        if opts.get('enable_ipfilter', '0') == '1':
            self.regex_map.update(ipfilter_map)
        self.sortby = opts.get('sortby', 'packets')

        self.comment_line_re = rc('^\s*#')
        self.empty_line_re = rc('^\s*$')
        self.iptables_logtype_re = rc(':\s.*?(\S+?):*\sIN=')
        self.iptables_re = rc('SRC=(\S*)\s.*PROTO=(\S*)\s.*DPT=(\S*)')
        self.ipchains_re = rc('\slog:\s\S+\s(\S*).*\sPROTO=(\d+)\s(\S*):\d*\s\S*:(\d+)')
        self.ipfilter_re = rc('ipmon\[\d+\]:.*\s(\S+),\d+\s->\s\S+,(\d+)\sPR\s(\S+)')
        self.etc_services_re = rc('^(\S*)\s+(\S*)')
        self.trojan_list_re = rc('^(\S*)\s+(.*)')
        self.etc_protocols_re = rc('^(\S*)\s+(\S*)')

        svcdict = self._parse_etc_services()

        trojans = opts.get('trojan_list', '')
        self.systems_collapse = int(opts.get('systems_collapse', '10'))
        self.ports_collapse = int(opts.get('ports_collapse', '10'))

        self.trojan_warning_wrap = '<font color="red">%s</font>'
        if trojans: svcdict = self._parse_trojan_list(trojans, svcdict)
        self.svcdict = svcdict

        self.protodict = self._parse_etc_protocols()

        self.collapsed_ports_rep = '<font color="red">[%d&nbsp;ports]</font>'
        self.collapsed_hosts_rep = '<font color="red">[%d&nbsp;hosts]</font>'

        self.report_wrap = '<table width="100%%" rules="cols" cellpadding="2">%s</table>'
        self.subreport_wrap = '<tr><th align="left" colspan="5"><h3><font color="red">%s</font></h3></th></tr>\n%s\n'

        self.line_rep = '<tr%s><td valign="top" width="5%%">%d</td><td valign="top" width="50%%">%s</td><td valign="top" width="15%%">%s</td><td valign="top" width="15%%">%s</td><td valign="top" width="15%%">%s</td></tr>\n'
        self.flip = ' bgcolor="#dddddd"'

    def _parse_etc_protocols(self):
        try: fh = open('/etc/protocols', 'r')
        except:
            self.logger.put(0, 'Could not open /etc/protocols for reading!')
            return {}
        protodict = {}
        while 1:
            line = fh.readline()
            if not line: break
            if (self.comment_line_re.search(line)
                or self.empty_line_re.search(line)): continue
            try: proto, num = self.etc_protocols_re.search(line).groups()
            except: continue
            protodict[num] = proto
        return protodict
        
    def _parse_etc_services(self):
        try: fh = open('/etc/services', 'r')
        except:
            self.logger.put(0, 'Could not open /etc/services for reading!')
            return {}
        svcdict = {}
        while 1:
            line = fh.readline()
            if not line: break
            if (self.comment_line_re.search(line)
                or self.empty_line_re.search(line)): continue
            try: service, pproto = self.etc_services_re.search(line).groups()
            except: continue
            svcdict[pproto] = service
        return svcdict

    def _parse_trojan_list(self, fileloc, svcdict):
        try: fh = open(fileloc, 'r')
        except:
            self.logger.put(0, 'Could not open %s for reading!' % fileloc)
            return svcdict
        while 1:
            line = fh.readline()
            if not line: break
            if (self.comment_line_re.search(line)
                or self.empty_line_re.search(line)): continue
            try: pproto, trojan = self.trojan_list_re.search(line).groups()
            except: continue
            if pproto not in svcdict:
                svcdict[pproto] = self.trojan_warning_wrap % trojan
        return svcdict

    ##
    # Line-matching routines
    #
    def iptables(self, linemap):
        sys, msg, mult = self.get_smm(linemap)
        ##
        # See if it's prepended with a logtype string of sorts.
        #
        try: logtype = self.iptables_logtype_re.search(msg).group(1)
        except: logtype = 'LOGGED'
        try: src, proto, dpt = self.iptables_re.search(msg).groups()
        except:
            self.logger.put(3, 'Unknown iptables entry: %s' % msg)
            return None
        source = self.gethost(src)
        dpt = int(dpt)
        proto = proto.lower()
        return {(source, sys, dpt, proto, logtype): mult}

    def ipchains(self, linemap):
        sys, msg, mult = self.get_smm(linemap)
        try: logtype, proto, src, dpt = self.ipchains_re.search(msg).groups()
        except:
            self.logger.put(3, 'Unknown ipchains entry: %s' % msg)
            return None
        source = self.gethost(src)
        dpt = int(dpt)
        proto = self.protodict.get(proto, '??')
        return {(source, sys, dpt, proto, logtype): mult}

    def ipfilter(self, linemap):
        sys, msg, mult = self.get_smm(linemap)
        try: src, dpt, proto = self.ipfilter_re.search(msg).groups()
        except:
            self.logger.put(3, 'Unknown ipfilter entry: %s' % msg)
            return None
        source = self.gethost(src)
        dpt = int(dpt)
        proto = proto.lower()
        return {(source, sys, dpt, proto, 'LOGGED'): mult}

    def _mk_port(self, port):
        try: desc = '%s&nbsp;(%s)' % (self.svcdict[port], port)
        except KeyError: desc = port
        return desc

    def _addfin(self, fin, packets, source, system, port, logtype):
        if self.sortby == 'source':
            fin.append((source, packets, system, port, logtype))
        elif self.sortby == 'system':
            fin.append((system, packets, source, port, logtype))
        elif self.sortby == 'port':
            fin.append((port, packets, source, system, logtype))
        else:
            fin.append((packets, source, system, port, logtype))
            
    
    ##
    # Finalize!
    #
    def finalize(self, rs):
        logger = self.logger
        fin = []
        for source in rs.get_distinct(()):
            dstrs = Result(rs.get_submap((source,)))
            systems = dstrs.get_distinct(())
            if len(systems) >= self.systems_collapse:
                ##
                # result will look like so:
                # 655 | source | [ 25 systems ] | [ 2 ] | [ 2 ports ] | lst
                # or
                # 655 | source | [ 25 systems ] | DROP | 22/tcp | ssh
                #
                ports = []
                logtypes = []
                packets = 0
                for system in systems:
                    submap = dstrs.get_submap((system,))
                    while 1:
                        try: entry, mult = submap.popitem()
                        except KeyError: break
                        dpt, proto, logtype = entry
                        if (dpt, proto) not in ports:
                            ports.append((dpt, proto))
                        if logtype not in logtypes: logtypes.append(logtype)
                        packets += mult
                if len(ports) > 1: port = (-1, len(ports))
                else: port = ports[0]                    
                if len(logtypes) > 1: logtype = '[%d]' % len(logtypes)
                else: logtype = logtypes[0]
                system = self.collapsed_hosts_rep % len(systems)
                self._addfin(fin, packets, source, system, port, logtype)
            else:
                for system in systems:
                    logger.put(2, 'Processing system %s' % system)
                    dpts = dstrs.get_distinct((system,))
                    if len(dpts) > self.ports_collapse:
                        ##
                        # Result will look like so:
                        # 655 | source | system | DROP | [ 5 ports ] | lst
                        #
                        logtypes = []
                        packets = 0
                        sysrs = Result(dstrs.get_submap((system,)))
                        portmap = dstrs.get_submap((system,))
                        while 1:
                            try: entry, mult = portmap.popitem()
                            except KeyError: break
                            dpt, proto, logtype = entry
                            logger.put(2, 'Processing port %s' % dpt)
                            if logtype not in logtypes:
                                logtypes.append(logtype)
                            packets += mult
                        if len(logtypes) > 1:
                            logtype = '[%d]' % len(logtypes)
                        else: logtype = logtypes[0]
                        port = (-1, len(dpts))
                        self._addfin(fin, packets, source, system, port,
                                     logtype)
                    else:
                        for dpt in dpts:
                            submap = dstrs.get_submap((system, dpt))
                            while 1:
                                try: entry, packets = submap.popitem()
                                except KeyError: break
                                proto, logtype = entry
                                port = (dpt, proto)
                                self._addfin(fin, packets, source, system,
                                             port, logtype)
        report = ''
        flipper = ''
        fin.sort()
        if self.sortby == 'packets':
            fin.reverse()
        for entry in fin:
            if flipper: flipper = ''
            else: flipper = self.flip
            if self.sortby == 'source':
                source, packets, system, port, logtype = entry
            elif self.sortby == 'system':
                system, packets, source, port, logtype = entry
            elif self.sortby == 'port':
                port, packets, source, system, logtype = entry
            else:
                packets, source, system, port, logtype = entry

            if port[0] == -1:
                    port = self.collapsed_ports_rep % port[1]                
            else:
                port = self._mk_port("%s/%s" % port)
            report += self.line_rep % (flipper, packets, source, system,
                                       logtype, port)

        report = self.subreport_wrap % ('Firewall Violations', report)
        report = self.report_wrap % report
                
        return report

if __name__ == '__main__':
    from epylog.helpers import ModuleTest
    ModuleTest(packets_mod, sys.argv)