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 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563
|
#!/usr/bin/env python2
from __future__ import with_statement
# RADIUS comparison tee v1.0
# Sniffs local RADIUS traffic, replays incoming requests to a test
# server, and compares the sniffed responses with the responses
# generated by the test server.
#
# Copyright (c) 2009, Frontier Communications
# Copyright (c) 2010, John Morrissey <jwm@horde.net>
#
# 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.
# Requires
# ========
# - python 2.4 or newer
# - impacket
# - pcapy
# - pyrad, ideally 1.2 or newer
# Output
# ======
# - .: 50 successful, matching responses processed.
# - C=x.x.x.x: Ignored packet sniffed from unknown client.
# - D: Dropped sniffed packet due to processing bottleneck. Consider
# increasing THREADS.
# - I: Invalid/unparseable packet sniffed.
# - Mreq: Response was sniffed without sniffing a corresponding request.
# - Mresp: Request was sniffed without sniffing a corresponding response.
# - T: Request to test server timed out.
import fcntl
from getopt import gnu_getopt, GetoptError
import os
import Queue
import re
import signal
import socket
import struct
import sys
import thread
from threading import Thread
from time import sleep, time
from impacket.ImpactDecoder import EthDecoder
import pcapy
from pyrad.client import Client
from pyrad.dictionary import Dictionary
from pyrad import packet
TEST_DEST = 'server.example.com'
TEST_SECRET = 'examplesecret'
# Dictionary to use when decoding RADIUS packets. pyrad earlier than
# v1.2 can't parse $INCLUDE directives, so you must combine FreeRADIUS'
# dictionary manually, with something like this:
#
# import re
# import sys
#
# def combine(file):
# for line in open(file):
# matches = re.search(r'^\$INCLUDE\s+(.*)$', line)
# if not matches:
# sys.stdout.write(line)
# continue
#
# combine(matches.group(1))
#
# combine('/etc/freeradius/dictionary')
DICTIONARY = '/etc/freeradius/dictionary'
# Number of worker threads to run.
THREADS = 32
# Mapping of RADIUS request source addresses to shared secrets,
# so we can decode incoming RADIUS requests.
#
# For example:
# '127.0.0.1': 'test',
CLIENTS = {
}
# Ignore any sniffed requests from these IP addresses.
IGNORE_CLIENTS = [
]
# Expected mismatches to ignore and consider the packet matching.
# Only the differences are compared to these items, so only the
# differing attrs need be listed in the attrs array.
#
# Examples:
# - Ignore mismatched AccessRejects whose sole difference is a
# Reply-Message attribute with the values given.
# {
# 'sniffed': {
# 'code': packet.AccessReject,
# 'attrs': [
# 'Reply-Message=Request Denied',
# ],
# },
# 'test': {
# 'code': packet.AccessReject,
# 'attrs': [
# 'Reply-Message=Account is disabled.',
# ],
# }
# },
#
# - Ignore mismatched AccessRejects with Reply-Message=Request Denied
# and arbitrary Cisco dns-servers in the sniffed packet, and
# no Reply-Message and Cisco-AVPair attrs in the response from the
# test RADIUS server.
# {
# 'sniffed': {
# 'code': packet.AccessReject,
# 'attrs': [
# 'Reply-Message=Request Denied',
# 'regex:^Cisco-AVPair=ip:dns-servers=.*$',
# ],
# },
# 'test': {
# 'code': packet.AccessReject,
# 'attrs': [
# ],
# }
# },
#
# - Only apply this stanza to sniffed requests with
# 'User-Name= user@example.com' (note the leading whitespace).
# {
# 'check': [
# 'User-Name= user@example.com',
# ],
# 'sniffed': {
# 'code': packet.AccessReject,
# 'attrs': [
# 'Reply-Message=Request Denied',
# ],
# },
# 'test': {
# 'code': packet.AccessAccept,
# 'attrs': [
# 'Service-Type=Framed-User',
# 'Framed-Protocol=PPP',
# 'Framed-IP-Address=255.255.255.255',
# 'Framed-MTU=1500',
# 'Framed-Compression=Van-Jacobson-TCP-IP',
# ],
# }
# },
IGNORE = [
]
QUEUE = Queue.Queue(maxsize=25000)
DICT = Dictionary(DICTIONARY)
def code2str(code):
if code == packet.AccessRequest:
return "Access-Request"
elif code == packet.AccessAccept:
return "Access-Accept"
elif code == packet.AccessReject:
return "Access-Reject"
elif code == packet.AccountingRequest:
return "Accounting-Request"
elif code == packet.AccountingResponse:
return "Accounting-Response"
elif code == packet.AccessChallenge:
return "Access-Challenge"
elif code == packet.StatusServer:
return "Status-Server"
elif code == packet.StatusClient:
return "Status-Client"
elif code == packet.DisconnectRequest:
return "Disconnect-Request"
elif code == packet.DisconnectACK:
return "Disconnect-ACK"
elif code == packet.DisconnectNAK:
return "Disconnect-NAK"
elif code == packet.CoARequest:
return "CoA-Request"
elif code == packet.CoAACK:
return "CoA-ACK"
elif code == packet.CoANAK:
return "CoA-NAK"
def handlePacket(header, data):
"""Place captured packets in the queue to be picked up
by worker threads."""
global QUEUE
try:
QUEUE.put_nowait(data)
except Queue.Full:
sys.stdout.write('D')
sys.stdout.flush()
def ignore_applies(pkt, ignore):
"""Determine whether an ignore stanza (based on its check
items) applies to a packet."""
# All check items must match for this ignore stanza to apply.
stanza_applies = True
for pair in ignore.get('check', []):
attr, value = pair.split('=')
if attr not in pkt:
return False
if value.startswith('regex:'):
if not re.search(value.replace('regex:', '', 1), value):
return False
elif pkt[attr] != value:
return False
return True
def ignores_match(pkt, mismatched, ignore):
"""Determine whether mismatched AV pairs remain after accounting
for ignored differences."""
non_regex_ignore = [
q
for q
in ignore['attrs']
if not q.startswith('regex:')
]
regex_ignore = [
q
for q
in ignore['attrs']
if q.startswith('regex:')
]
unmatched_av = mismatched[:]
unmatched_rules = ignore['attrs'][:]
for av in mismatched:
if av in non_regex_ignore:
unmatched_av.remove(av)
unmatched_rules.remove(av)
continue
for regex in regex_ignore:
if re.search(regex.replace('regex:', '', 1), av):
unmatched_av.remove(av)
if regex in unmatched_rules:
unmatched_rules.remove(regex)
break
if unmatched_av or unmatched_rules:
return False
return True
def matches(req, sniffed_pkt, test_pkt):
"""Determine whether a response from the test server matches
the response sniffed from the wire, accounting for ignored
differences."""
global IGNORE
mis_attrs_sniffed = []
for k in sniffed_pkt.keys():
if sorted(sniffed_pkt[k]) == sorted(test_pkt.get(k, [])):
continue
mis_attrs_sniffed.append('%s=%s' % (
k, ', '.join([str(v) for v in sorted(sniffed_pkt[k])])))
mis_attrs_test = []
for k in test_pkt.keys():
if sorted(test_pkt[k]) == sorted(sniffed_pkt.get(k, [])):
continue
mis_attrs_test.append('%s=%s' % (
k, ', '.join([str(v) for v in sorted(test_pkt[k])])))
# The packets match without having to consider any ignores.
if sniffed_pkt.code == test_pkt.code and \
not mis_attrs_sniffed and not mis_attrs_test:
return True
for ignore in IGNORE:
if not ignore_applies(req, ignore):
continue
if ignore['sniffed']['code'] != sniffed_pkt.code or \
ignore['test']['code'] != test_pkt.code:
continue
if ignores_match(sniffed_pkt, mis_attrs_sniffed, i['sniffed']):
return True
if ignores_match(test_pkt, mis_attrs_test, i['test']):
return True
return False
def log_mismatch(nas, req, passwd, expected, got):
"""Emit notification that the test server has returned a response
that differs from the sniffed response."""
print 'Mismatch: %s' % nas
print 'Request: %s' % code2str(req.code)
for key in req.keys():
if key == 'User-Password':
print '\t%s: %s' % (key, passwd)
continue
print '\t%s: %s' % (
key, ', '.join([str(v) for v in req[key]]))
print 'Expected: %s' % code2str(expected.code)
for key in expected.keys():
print '\t%s: %s' % (
key, ', '.join([str(v) for v in expected[key]]))
print 'Got: %s' % code2str(got.code)
for key in got.keys():
print '\t%s: %s' % (
key, ', '.join([str(v) for v in got[key]]))
print
REQUESTS = {}
REQUESTS_LOCK = thread.allocate_lock()
NUM_SUCCESSFUL = 0
def check_for_match(key, req_resp):
"""Send a copy of the original request to the test server and
determine whether the response matches the response sniffed from
the wire."""
global DICT, NUM_SUCCESSFUL, TEST_DEST, TEST_SECRET
global REQUESTS, REQUESTS_LOCK
client = Client(server=TEST_DEST,
secret=TEST_SECRET, dict=DICT)
fwd_req = client.CreateAuthPacket(code=packet.AccessRequest)
fwd_req.authenticator = req_resp['req']['pkt'].authenticator
keys = req_resp['req']['pkt'].keys()
for k in keys:
for value in req_resp['req']['pkt'][k]:
fwd_req.AddAttribute(k, value)
if 'User-Password' in keys:
fwd_req['User-Password'] = fwd_req.PwCrypt(req_resp['req']['passwd'])
if 'NAS-IP-Address' in fwd_req:
del fwd_req['NAS-IP-Address']
fwd_req.AddAttribute('NAS-IP-Address', req_resp['req']['ip'])
try:
test_reply = client.SendPacket(fwd_req)
except:
# Request to test server timed out.
sys.stdout.write('T')
sys.stdout.flush()
with REQUESTS_LOCK:
del REQUESTS[key]
return
if not matches(req_resp['req']['pkt'],
req_resp['response']['pkt'], test_reply):
print
log_mismatch(req_resp['req']['ip'],
req_resp['req']['pkt'],
req_resp['req']['passwd'],
req_resp['response']['pkt'], test_reply)
with REQUESTS_LOCK:
# Occasionally, this key isn't present. Maybe retransmissions
# due to a short timeout on the remote RADIUS client's end
# and a subsequent race?
if key in REQUESTS:
del REQUESTS[key]
NUM_SUCCESSFUL += 1
if NUM_SUCCESSFUL % 50 == 0:
sys.stdout.write('.')
sys.stdout.flush()
class RadiusComparer(Thread):
def run(self):
global DICT, IGNORE_CLIENTS, QUEUE, REQUESTS, REQUESTS_LOCK
while True:
data = QUEUE.get()
if not data:
return
frame = EthDecoder().decode(data)
ip = frame.child()
udp = ip.child()
rad_raw = udp.child().get_buffer_as_string()
try:
pkt = packet.Packet(dict=DICT, packet=rad_raw)
except packet.PacketError:
sys.stdout.write('I')
sys.stdout.flush()
continue
if ip.get_ip_src() in IGNORE_CLIENTS:
continue
if pkt.code == packet.AccessRequest:
auth = packet.AuthPacket(data[42:])
auth.authenticator = pkt.authenticator
auth.secret = clients.CLIENTS.get(ip.get_ip_src(), None)
if not auth.secret:
# No configuration for this client.
sys.stdout.write('C=%s' % ip.get_ip_src())
sys.stdout.flush()
continue
passwd = None
if 'User-Password' in pkt.keys():
passwd = auth.PwDecrypt(pkt['User-Password'][0])
key = '%s:%d:%d' % (ip.get_ip_src(),
udp.get_uh_sport(), pkt.id)
do_compare = None
with REQUESTS_LOCK:
if key not in REQUESTS:
REQUESTS[key] = {}
REQUESTS[key]['req'] = {
'ip': ip.get_ip_src(),
'port': udp.get_uh_sport(),
'pkt': pkt,
'passwd': passwd,
}
REQUESTS[key]['tstamp'] = time()
if 'response' in REQUESTS[key]:
do_compare = REQUESTS[key]
if do_compare:
check_for_match(key, do_compare)
elif pkt.code in [packet.AccessAccept, packet.AccessReject]:
key = '%s:%d:%d' % (ip.get_ip_dst(),
udp.get_uh_dport(), pkt.id)
do_compare = None
with REQUESTS_LOCK:
if key not in REQUESTS:
REQUESTS[key] = {}
REQUESTS[key]['response'] = {
'ip': ip.get_ip_src(),
'port': udp.get_uh_sport(),
'pkt': pkt,
}
REQUESTS[key]['tstamp'] = time()
if 'req' in REQUESTS[key]:
do_compare = REQUESTS[key]
if do_compare:
check_for_match(key, do_compare)
else:
print >>sys.stderr, \
'Unsupported packet type received: %d' % pkt.code
class RequestsPruner(Thread):
"""Prune stale request state periodically."""
def run(self):
global REQUESTS, REQUESTS_LOCK
while True:
sleep(30)
now = time()
with REQUESTS_LOCK:
keys = REQUESTS.keys()
for key in keys:
if REQUESTS[key]['tstamp'] + 60 >= now:
continue
if 'req' not in REQUESTS[key]:
sys.stdout.write('Mreq')
sys.stdout.flush()
if 'response' not in REQUESTS[key]:
sys.stdout.write('Mresp')
sys.stdout.flush()
del REQUESTS[key]
def usage():
print 'Usage: %s INTERFACE' % os.path.basename(sys.argv[0])
print ''
print ' -h, --help display this help and exit'
if __name__ == '__main__':
global PID_FILE
progname = os.path.basename(sys.argv[0])
try:
options, iface = gnu_getopt(sys.argv[1:], 'h', ['help'])
except GetoptError, e:
print '%s: %s' % (progname, str(e))
usage()
sys.exit(1)
for option in options:
if option[0] == '-h' or option[0] == '--help':
usage()
sys.exit(0)
if len(iface) != 1:
usage()
sys.exit(1)
iface = iface[0]
if os.geteuid() != 0:
print >>sys.stderr, '%s: must be run as root.' % progname
sys.exit(1)
for i in range(0, THREADS):
RadiusComparer().start()
RequestsPruner().start()
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# This is Linux-specific, and there's no tenable way to make
# it portable.
#
# Unfortunately, we need the interface's IP address to filter out
# only RADIUS traffic destined for this host (avoiding traffic sent
# *by* this host, such as proxied requests or our own traffic) to
# avoid replaying requests not directed to the local radiusd.
#
# Furthermore, this only obtains the interface's *first* IP address,
# so we won't notice traffic sent to additional IP addresses on
# the given interface.
#
# This is Good Enough For Me given the effort I care to invest.
# Of course, patches enhancing this are welcome.
if os.uname()[0] == 'Linux':
local_ipaddr = socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', iface[:15])
)[20:24])
else:
raise Exception('Only the Linux operating system is currently supported.')
p = pcapy.open_live(iface, 1600, 0, 100)
p.setfilter('''
(dst host %s and udp and dst port 1812) or
(src host %s and udp and src port 1812)''' % \
(local_ipaddr, local_ipaddr))
while True:
try:
p.dispatch(1, handlePacket)
except KeyboardInterrupt:
sys.exit(0)
|