/
matchers.py
496 lines (475 loc) · 15.8 KB
/
matchers.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#
# This module supplies all of the basic matching of various
# characteristics of a hinfo object, via a collection of classes.
# Characteristics are generally expressed in a way broadly similar
# to tcpwrappers, but include things such as regexps based on the
# (remote) host name, DNSBl lookups, and some information on previous
# connections from the same IP address (via hinfo supplied services).
#
# Matchers may take a required argument, in which case their name (as
# shown in MatchInfo.terminals; see rdparse) has a ':' at the end, or
# they may take no arguments. The simplest no-argument matcher is 'ALL'.
import re, string
import netblock
import util
# Utility bits
unitytrans = string.maketrans('', '')
class BadArg(Exception):
pass
# 'Memoize' certain expensive computations across multiple generations
# of the rules file (we don't expect to see the same thing multiple
# times in the same rules file; that it helps this out is a nice side
# effect). After each 'generation', memoized computations not used in
# the current generation are discarded. End-of-generation is signalled
# by calling .age(); .discard() discards the whole memoization, and is
# used on errors on the principle that on errors *everything* is dead.
#
# AbstractMemo must be subclassed by supplying a .generate() method
# that does the actual computation process and returns the result.
class AbstractMemo:
def __init__(self):
self.discard()
def discard(self):
self.oldmem = {}
self.newmem = {}
def age(self):
self.oldmem = self.newmem
self.newmem = {}
def generate(self, what):
raise TypeError, "must be overridden in subclass"
def compile(self, what):
if what in self.newmem:
pass
elif what in self.oldmem:
self.newmem[what] = self.oldmem[what]
else:
self.newmem[what] = self.generate(what)
return self.newmem[what]
# Compiling regexps is surprisingly expensive, so they are our first
# memoization target.
class REMemo(AbstractMemo):
def generate(self, regstr):
try:
return re.compile(regstr, re.IGNORECASE)
except re.error, e:
raise BadArg, "bad regexp '%s': %s" % (regstr, str(e))
# This converts a prefix IP address (with a . on the end) to a CIDR
# in string form. Since the string passed validation earlier, we know
# it is in a very predictable format and we can be simple.
def ippreftocidrs(ipS):
return "%s/%d" % (ipS[:-1], 8 * ipS.count('.'))
class IAdMemo(AbstractMemo):
# We generate CIDR netblocks based on the text strings, which
# may be either something that can be fed to IPRanges.add()
# straight or may be a prefix match, which must be converted
# to a CIDR string first.
def generate(self, rangel):
nb = netblock.IPRanges()
for re in rangel:
try:
if re[-1] == '.':
nb.add(ippreftocidrs(re))
else:
nb.add(re)
except netblock.NBError, e:
raise BadArg, "bad CIDR netblock %s: %s" % (re, str(e))
return nb
rememo = REMemo()
ipadmemo = IAdMemo()
# agememos() is called after the rules file is loaded.
# errormemos() is called if there is an error during rules file load.
# In both cases, they just apply the particular operation to each of
# our memoization proxies.
def agememos():
rememo.age()
ipadmemo.age()
def discardmemos():
rememo.discard()
ipadmemo.discard()
# ALL: matches everything.
class AllMatch:
def __init__(self, name, val):
__pychecker__ = "no-argsused"
def __str__(self):
return "ALL"
def eval(self, hi):
__pychecker__ = "no-argsused"
return 1
# Match against identd data.
# As 'identd:', we get an argument. As 'IDENTD', we do not.
# We need to distinguish those cases in the eval routine.
class IdentdMatch:
def __init__(self, name, val):
__pychecker__ = 'no-argsused'
self.desid = val
def __str__(self):
if self.desid:
return "identd: "+self.desid
else:
return "IDENTD"
def eval(self, hi):
r = hi.getidentd()
if not r:
return 0
if self.desid:
return r == self.desid
else:
return 1
# Match against the local host and port.
class LocalMatch:
def __init__(self, name, val):
__pychecker__ = 'no-argsused'
r = util.gethostport(val)
if r == None:
raise BadArg, "bad local: values"
self.host, self.port = r
def __str__(self):
return "local: %s@%s" % (self.port, self.host)
def eval(self, hi):
if self.port and self.port != hi.getlport():
return 0
if self.host and self.host != hi.getlip():
return 0
return 1
# Match against the hostname status. This takes either tcpwrappers
# style arguments (KNOWN, UNKNOWN, PARANOID) or our specific status
# ones.
hnmapdict = {
'KNOWN': ('good',), 'UNKNOWN': ('unknown',),
'PARANOID': ('noforward', 'addrmismatch'),
'good': ('good',), 'unknown': ('unknown',),
'addrmismatch': ('addrmismatch',), 'noforward': ('noforward',),
}
class HNStatusMatch:
def __init__(self, name, val):
if val == None:
val = name
try:
self.wstates = hnmapdict[val]
except KeyError:
raise BadArg, "unrecognized hostname state"
self.name = intern(val)
def __str__(self):
return "hnstatus: "+self.name
def eval(self, hi):
return hi.gethnstate() in self.wstates
# IP addresses are complicated because we support three different
# matches: a) literal IP addresses or b) CIDR netblocks through the
# netblock code or c) tcpwrappers style shortened forms.
# validateipprefix knows it is being called on a string that ends in a '.'
# and contains only digits and dots.
def validateipprefix(val):
"Is this a good partial IP address, tcpwrappers style?"
n = val.split('.')[:-1]
if len(n) == 0 or len(n) > 3:
raise BadArg, "bad IP address specifier"
for octet in n:
if not octet:
raise BadArg, "empty IP octet"
# we know that val contains only numbers and '.'s. We split
# on '.', so the octet cannot contain anything but numbers.
# So try/except is pointless; we can just use int straight.
i = int(octet)
if not (0 <= i <= 255):
raise BadArg, "bad IP octet"
# valid characters in the IP address forms we accept.
ipAddrChars = string.digits + "./-"
def validipaddr(val):
return not (val[0] == '.' or
val.translate(unitytrans, ipAddrChars))
class IPAddrMatch(object):
__slots__ = "cidr", "cname", "ip", "name"
# 'docheck' is a hack and a private interface; it avoids doing
# validity checks twice if we're coming through the handledefault()
# internal path.
def __init__(self, name, val, docheck = 1):
if docheck and not validipaddr(val):
raise BadArg, "bad characters in IP address match "+val
self.cidr = None
self.ip = None
if '/' in val or '-' in val or val[-1] != '.':
# validation will happen in finalization.
pass
else:
validateipprefix(val)
self.ip = val
self.name = [val]
self.cname = intern(name)
def __str__(self):
# EVIL. Note that we know too much here in order to
# reproduce ourselves acceptably; we are essentially
# regenerating a fake orlist.
return " ".join(["%s %s" % (self.cname, x) for x in self.name])
# EVIL HACK. This supports the attempt of orlist parsing in rdparse
# to merge subsequent entries into a past one. This is possible for
# IP address checks, because we can glom them all into one big
# IPRanges object.
def finalize(self):
# if we are a length-one thing of an IP prefix, we're done.
if len(self.name) == 1 and self.ip:
return
# Otherwise, we need to compile to IPAddrRange form. First
# we need to tuple-ize self.name, so it can be hashed on.
# Then we compile/memoize.
self.name = tuple(self.name)
self.cidr = ipadmemo.compile(self.name)
self.ip = None
# TESTING USE ONLY: definalizes a finalized situation.
def _definalize(self):
if len(self.name) == 1 and self.ip:
return
self.name = list(self.name)
self.cidr = None
def merge(self, other):
# isinstance(other, IPAddrMatch) is still true if the
# other is LIPAddrMatch (or we are), which is why we
# have to check .cname too.
if not (isinstance(other, IPAddrMatch) and \
other.cname == self.cname):
return False
# We don't do anything much here; all the real work
# happens in finalization. We drive the finalization
# work off the self.name list, since we have to maintain
# it anyways and it better be parseable.
self.ip = None
self.name.extend(other.name)
return True
# This returns both the string and the numeric form of the IP
# address, because we may need both.
def _getipS(self, hi):
return hi.getip()
def _getipN(self, hi):
return hi.getipn()
def eval(self, hi):
# if self.ip is set, it ends with a dot and is safe to
# match against the IP address we want to check.
# Otherwise, CIDR match.
if self.ip:
return self._getipS(hi).startswith(self.ip)
else:
return self._getipN(hi) in self.cidr
# This matches against the local IP address instead of the remote one.
class LIPAddrMatch(IPAddrMatch):
def _getipS(self, hi):
return hi.getlip()
def _getipN(self, hi):
return hi.getlipn()
# Match against tcpwrappers style hostnames, which may be either full
# hostnames or '.' with a hostname portion. '.foobar' matches either
# 'nnn.foobar' or 'foobar' itself, and we have to check separately.
# Under some mental protest, we accept '_' as a valid character in
# hostnames. (Strictly speaking it isn't, but it's in common usage.)
hostNameChars = string.lowercase + string.digits + ".-_"
def validhostname(hn):
return not (hn.translate(unitytrans, hostNameChars) or
hn == '.')
class HostnameMatch(object):
__slots__ = "cname", "host", "hoste"
def __init__(self, name, val):
val = val.lower()
if not validhostname(val):
raise BadArg, "bad hostname: "+val
self.cname = intern(name)
# 'host' is matched against the literal hostname.
# 'hoste' is matched against the end of the hostname.
# 'hoste' is set only if the value starts with a '.',
# in which case 'host' is the value minus the dot.
# This avoids repeatedly slicing the front off during
# evaluation, at the expense of more memory.
if val[0] == '.':
self.hoste = val
self.host = val[1:]
else:
self.hoste = None
self.host = val
def __str__(self):
# Our small optimization above requires us to be clever
# here in order to get the original version out, since
# that is either hoste or host, depending.
if self.hoste:
return '%s %s' % (self.cname, self.hoste)
else:
return "%s %s" % (self.cname, self.host)
def _gethostname(self, hi):
return hi.gethostname_l()
def eval(self, hi):
hn = self._gethostname(hi)
if not hn:
return False
elif self.hoste:
return hn.endswith(self.hoste) or hn == self.host
else:
return hn == self.host
# This is *dangerous*: it matches against not the hostname, but the
# claimed hostname.
class ClaimedHNMatch(HostnameMatch):
def _gethostname(self, hi):
return hi.getclaimedhn_l()
class ClassMatch:
def __init__(self, name, val):
__pychecker__ = 'no-argsused'
self.cls = val
def __str__(self):
return "class: "+self.cls
def eval(self, hi):
return self.cls in hi.getclasses()
# Regular expressions turn out to be pretty easy.
class REMatch(object):
__slots__ = "cname", "rexp"
def __init__(self, name, val):
self.rexp = rememo.compile(val)
self.cname = intern(name)
def __str__(self):
return "%s '%s'" % (self.cname, self.rexp.pattern)
def _gethostname(self, hi):
return hi.gethostname()
def eval(self, hi):
hn = self._gethostname(hi)
if not hn:
return 0
# We cannot just return the search result directly because
# it isn't a boolean. So we make it one. (Go team.)
return bool(self.rexp.search(hn))
class ClaimedREMatch(REMatch):
def _gethostname(self, hi):
return hi.getclaimedhn()
# This matches based on the *forward* hostname to IP address mapping
# information, as opposed to the reverse. 'forwhn: foobar' will match
# if the connection is coming from one of the IP addresses 'foobar'
# resolves to, irregardless of their reverse mappings.
class ForwhnMatch:
def __init__(self, name, val):
__pychecker__ = 'no-argsused'
val = val.lower()
if not validhostname(val):
raise BadArg, "bad forwhn hostname: "+val
self.forwhn = val
def __str__(self):
return "forwhn: "+self.forwhn
def eval(self, hi):
ips = hi.gethostips(self.forwhn)
ip = hi.getip()
for i in ips:
if ip == i:
return 1
return 0
# Check a IP-based DNS blocklist. The optional /<IP> makes things only
# match if the DNSBl specifically returns that IP address on lookups.
# All lookups are IP address lookups, not TXT-based.
class DNSBlMatch:
def __init__(self, name, val):
__pychecker__ = 'no-argsused'
pos = val.find('/')
if val[0] == '/' or val[-1] == '/':
# very funny: -10 points.
raise BadArg, "bad position of / in dnsbl: argument"
if pos >= 0:
self.dnsbl = "." + val[:pos]
self.ipval = val[pos+1:]
if not util.isipaddr(self.ipval):
raise BadArg, "dnsbl: IP address portion isn't an IP address"
else:
self.dnsbl = "." + val
self.ipval = None
# We have to remember we glued the period on the front.
def __str__(self):
if self.ipval:
return "dnsbl: %s/%s" % (self.dnsbl[1:], self.ipval)
else:
return "dnsbl: "+self.dnsbl[1:]
def eval(self, hi):
# We have to reverse the IP address in order to perform
# DNS blacklist lookups. Fortunately the HostInfo data
# caches that for us.
revip = hi.getrevip()
ips = hi.gethostips(revip+self.dnsbl)
# Do we have to check the IP value? If not, we're done.
if not self.ipval:
return len(ips) > 0
for i in ips:
if i == self.ipval:
return 1
return 0
class AnswersOnMatch:
def __init__(self, name, val):
__pychecker__ = "no-argsused"
self.port = util.int_or_raise(val, BadArg)
if not (0 <= self.port <= 65536):
raise BadArg, "port number outside of OK range"
def __str__(self):
return "answerson: %d" % self.port
def eval(self, hi):
return hi.answerson(self.port)
# These matchers operate based on the time of the first or the most recent
# connection.
class TimedMatch:
def __init__(self, name, val):
self.name = name
self.secsold = util.getsecs_or_raise(val, BadArg)
def __str__(self):
return "%s %ds" % (self.name, self.secsold)
class WaitedMatch(TimedMatch):
def eval(self, hi):
return hi.getfirsttime() > self.secsold
class StallMatch(TimedMatch):
def eval(self, hi):
return hi.getfirsttime() <= self.secsold
# If this is the first connection, we have by definition not seen them
# for an infinite time.
class LastSeenMatch(TimedMatch):
def eval(self, hi):
r = hi.getlasttime()
if r == None:
return 0
return r <= self.secsold
# Similarly, if this is the first connection we have not seen them for
# an infinite amount of time.
class NotSeenForMatch(TimedMatch):
def eval(self, hi):
r = hi.getlasttime()
if r == None:
return 1
return hi.getlasttime() > self.secsold
class FirstTimeMatch:
def __init__(self, name, val):
__pychecker__ = 'no-argsused'
def __str__(self):
return "firsttime"
def eval(self, hi):
return hi.getlasttime() == None
# This structure is what the rdparse module uses to match up matchers
# with arguments.
class MatchInfo:
terminals = {
'ALL': AllMatch,
'local:': LocalMatch,
# Hostname state and its aliases,
'hnstatus:': HNStatusMatch, 'PARANOID': HNStatusMatch,
'KNOWN': HNStatusMatch, 'UNKNOWN': HNStatusMatch,
# General stuff.
"ip:": IPAddrMatch, "localip:": LIPAddrMatch,
"identd:": IdentdMatch, 'IDENTD': IdentdMatch,
"hostname:": HostnameMatch,
're:': REMatch,
'forwhn:': ForwhnMatch,
'dnsbl:': DNSBlMatch,
'answerson:': AnswersOnMatch,
# These are based on the age of the first or the most recent
# connection from the IP address.
'stallfor:': StallMatch, 'waited:': WaitedMatch,
'seenwithin:': LastSeenMatch,'notseenfor:': NotSeenForMatch,
'firsttime': FirstTimeMatch,
# This sort of doesn't belong here, but.
'class:': ClassMatch,
# DANGER WILL ROBINSON. Use of these is dangerous and can
# blow up in your face.
"claimedhn:": ClaimedHNMatch,
"claimedre:": ClaimedREMatch,
}
error = BadArg
def defaultterm(self, val):
#__pychecker__ = "no-returnvalues"
if validipaddr(val):
return self.terminals["ip:"]("ip:", val, 1)
return self.terminals["hostname:"]("hostname:", val)
matchinfo = MatchInfo()