2017-05-25 15:57:40 +08:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
|
|
|
|
import re
|
2017-06-27 16:26:48 +08:00
|
|
|
import os
|
2017-05-25 15:57:40 +08:00
|
|
|
import time
|
|
|
|
import atexit
|
|
|
|
import signal
|
|
|
|
import ipaddress
|
|
|
|
import subprocess
|
|
|
|
from threading import Thread
|
2017-06-24 06:07:18 +08:00
|
|
|
import redis
|
|
|
|
import time
|
|
|
|
import json
|
2017-05-25 15:57:40 +08:00
|
|
|
|
2017-06-27 16:26:48 +08:00
|
|
|
yes_regex = re.compile(r'([yY][eE][sS]|[yY])+$')
|
|
|
|
if re.search(yes_regex, os.getenv('SKIP_FAIL2BAN', 0)):
|
2018-01-24 16:11:33 +08:00
|
|
|
print 'SKIP_FAIL2BAN=y, Skipping Fail2ban container...'
|
2017-11-15 02:50:14 +08:00
|
|
|
time.sleep(31536000)
|
|
|
|
raise SystemExit
|
2017-06-27 16:26:48 +08:00
|
|
|
|
2018-01-24 16:11:33 +08:00
|
|
|
r = redis.StrictRedis(host=os.getenv('IPV4_NETWORK', '172.22.1') + '.249', decode_responses=True, port=6379, db=0)
|
2017-07-05 00:08:20 +08:00
|
|
|
pubsub = r.pubsub()
|
2017-06-29 17:30:14 +08:00
|
|
|
|
|
|
|
RULES = {}
|
2017-07-05 00:08:20 +08:00
|
|
|
RULES[1] = 'warning: .*\[([0-9a-f\.:]+)\]: SASL .+ authentication failed'
|
|
|
|
RULES[2] = '-login: Disconnected \(auth failed, .+\): user=.*, method=.+, rip=([0-9a-f\.:]+),'
|
2017-08-31 04:27:33 +08:00
|
|
|
RULES[3] = '-login: Aborted login \(no auth .+\): user=.+, rip=([0-9a-f\.:]+), lip.+'
|
|
|
|
RULES[4] = '-login: Aborted login \(tried to use disallowed .+\): user=.+, rip=([0-9a-f\.:]+), lip.+'
|
|
|
|
RULES[5] = 'SOGo.+ Login from \'([0-9a-f\.:]+)\' for user .+ might not have worked'
|
|
|
|
RULES[6] = 'mailcow UI: Invalid password for .+ by ([0-9a-f\.:]+)'
|
2017-06-24 06:07:18 +08:00
|
|
|
|
2018-01-24 16:11:33 +08:00
|
|
|
r.setnx('F2B_BAN_TIME', '1800')
|
|
|
|
r.setnx('F2B_MAX_ATTEMPTS', '10')
|
|
|
|
r.setnx('F2B_RETRY_WINDOW', '600')
|
|
|
|
r.setnx('F2B_NETBAN_IPV6', '64')
|
|
|
|
r.setnx('F2B_NETBAN_IPV4', '24')
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
bans = {}
|
2017-06-24 06:07:18 +08:00
|
|
|
log = {}
|
2017-05-25 15:57:40 +08:00
|
|
|
quit_now = False
|
|
|
|
|
|
|
|
def ban(address):
|
2018-01-24 16:11:33 +08:00
|
|
|
BAN_TIME = int(r.get('F2B_BAN_TIME'))
|
|
|
|
MAX_ATTEMPTS = int(r.get('F2B_MAX_ATTEMPTS'))
|
|
|
|
RETRY_WINDOW = int(r.get('F2B_RETRY_WINDOW'))
|
|
|
|
WHITELIST = r.hgetall('F2B_WHITELIST')
|
|
|
|
NETBAN_IPV6 = '/' + str(r.get('F2B_NETBAN_IPV6'))
|
|
|
|
NETBAN_IPV4 = '/' + str(r.get('F2B_NETBAN_IPV4'))
|
2017-11-15 02:50:14 +08:00
|
|
|
|
|
|
|
ip = ipaddress.ip_address(address.decode('ascii'))
|
|
|
|
if type(ip) is ipaddress.IPv6Address and ip.ipv4_mapped:
|
|
|
|
ip = ip.ipv4_mapped
|
|
|
|
address = str(ip)
|
|
|
|
if ip.is_private or ip.is_loopback:
|
|
|
|
return
|
|
|
|
|
|
|
|
self_network = ipaddress.ip_network(address.decode('ascii'))
|
|
|
|
if WHITELIST:
|
|
|
|
for wl_key in WHITELIST:
|
|
|
|
wl_net = ipaddress.ip_network(wl_key.decode('ascii'), False)
|
|
|
|
if wl_net.overlaps(self_network):
|
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'info'
|
|
|
|
log['message'] = 'Address %s is whitelisted by rule %s' % (self_network, wl_net)
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
print 'Address %s is whitelisted by rule %s' % (self_network, wl_net)
|
2017-11-15 02:50:14 +08:00
|
|
|
return
|
|
|
|
|
2018-01-24 16:11:33 +08:00
|
|
|
net = ipaddress.ip_network((address + (NETBAN_IPV4 if type(ip) is ipaddress.IPv4Address else NETBAN_IPV6)).decode('ascii'), strict=False)
|
2017-11-15 02:50:14 +08:00
|
|
|
net = str(net)
|
|
|
|
|
|
|
|
if not net in bans or time.time() - bans[net]['last_attempt'] > RETRY_WINDOW:
|
|
|
|
bans[net] = { 'attempts': 0 }
|
|
|
|
active_window = RETRY_WINDOW
|
|
|
|
else:
|
|
|
|
active_window = time.time() - bans[net]['last_attempt']
|
|
|
|
|
|
|
|
bans[net]['attempts'] += 1
|
|
|
|
bans[net]['last_attempt'] = time.time()
|
|
|
|
|
|
|
|
active_window = time.time() - bans[net]['last_attempt']
|
|
|
|
|
|
|
|
if bans[net]['attempts'] >= MAX_ATTEMPTS:
|
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'crit'
|
|
|
|
log['message'] = 'Banning %s' % net
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
print 'Banning %s for %d minutes' % (net, BAN_TIME / 60)
|
2017-11-15 02:50:14 +08:00
|
|
|
if type(ip) is ipaddress.IPv4Address:
|
2018-01-24 16:11:33 +08:00
|
|
|
subprocess.call(['iptables', '-I', 'INPUT', '-s', net, '-j', 'REJECT'])
|
|
|
|
subprocess.call(['iptables', '-I', 'FORWARD', '-s', net, '-j', 'REJECT'])
|
2017-11-15 02:50:14 +08:00
|
|
|
else:
|
2018-01-24 16:11:33 +08:00
|
|
|
subprocess.call(['ip6tables', '-I', 'INPUT', '-s', net, '-j', 'REJECT'])
|
|
|
|
subprocess.call(['ip6tables', '-I', 'FORWARD', '-s', net, '-j', 'REJECT'])
|
|
|
|
r.hset('F2B_ACTIVE_BANS', '%s' % net, log['time'] + BAN_TIME)
|
2017-11-15 02:50:14 +08:00
|
|
|
else:
|
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'warn'
|
|
|
|
log['message'] = '%d more attempts in the next %d seconds until %s is banned' % (MAX_ATTEMPTS - bans[net]['attempts'], RETRY_WINDOW, net)
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
print '%d more attempts in the next %d seconds until %s is banned' % (MAX_ATTEMPTS - bans[net]['attempts'], RETRY_WINDOW, net)
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
def unban(net):
|
2017-11-15 02:50:14 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'info'
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
2017-11-15 02:50:14 +08:00
|
|
|
if not net in bans:
|
2018-01-24 16:11:33 +08:00
|
|
|
log['message'] = '%s is not banned, skipping unban and deleting from queue (if any)' % net
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
print '%s is not banned, skipping unban and deleting from queue (if any)' % net
|
|
|
|
r.hdel('F2B_QUEUE_UNBAN', '%s' % net)
|
2017-11-15 02:50:14 +08:00
|
|
|
return
|
2018-01-24 16:11:33 +08:00
|
|
|
log['message'] = 'Unbanning %s' % net
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
print 'Unbanning %s' % net
|
2017-11-15 02:50:14 +08:00
|
|
|
if type(ipaddress.ip_network(net.decode('ascii'))) is ipaddress.IPv4Network:
|
2018-01-24 16:11:33 +08:00
|
|
|
subprocess.call(['iptables', '-D', 'INPUT', '-s', net, '-j', 'REJECT'])
|
|
|
|
subprocess.call(['iptables', '-D', 'FORWARD', '-s', net, '-j', 'REJECT'])
|
2017-11-15 02:50:14 +08:00
|
|
|
else:
|
2018-01-24 16:11:33 +08:00
|
|
|
subprocess.call(['ip6tables', '-D', 'INPUT', '-s', net, '-j', 'REJECT'])
|
|
|
|
subprocess.call(['ip6tables', '-D', 'FORWARD', '-s', net, '-j', 'REJECT'])
|
|
|
|
r.hdel('F2B_ACTIVE_BANS', '%s' % net)
|
|
|
|
r.hdel('F2B_QUEUE_UNBAN', '%s' % net)
|
2017-11-15 02:50:14 +08:00
|
|
|
del bans[net]
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
def quit(signum, frame):
|
2017-11-15 02:50:14 +08:00
|
|
|
global quit_now
|
|
|
|
quit_now = True
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
def clear():
|
2017-11-15 02:50:14 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'info'
|
|
|
|
log['message'] = 'Clearing all bans'
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
print 'Clearing all bans'
|
2017-11-15 02:50:14 +08:00
|
|
|
for net in bans.copy():
|
|
|
|
unban(net)
|
|
|
|
pubsub.unsubscribe()
|
2017-05-25 15:57:40 +08:00
|
|
|
|
2017-07-05 00:08:20 +08:00
|
|
|
def watch():
|
2017-11-15 02:50:14 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'info'
|
|
|
|
log['message'] = 'Watching Redis channel F2B_CHANNEL'
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
|
|
|
pubsub.subscribe('F2B_CHANNEL')
|
|
|
|
print 'Subscribing to Redis channel F2B_CHANNEL'
|
2017-11-15 02:50:14 +08:00
|
|
|
while True:
|
|
|
|
for item in pubsub.listen():
|
|
|
|
for rule_id, rule_regex in RULES.iteritems():
|
|
|
|
if item['data'] and item['type'] == 'message':
|
|
|
|
result = re.search(rule_regex, item['data'])
|
|
|
|
if result:
|
|
|
|
addr = result.group(1)
|
2017-11-14 17:44:00 +08:00
|
|
|
ip = ipaddress.ip_address(addr.decode('ascii'))
|
|
|
|
if ip.is_private or ip.is_loopback:
|
|
|
|
continue
|
2018-01-24 16:11:33 +08:00
|
|
|
print '%s matched rule id %d' % (addr, rule_id)
|
2017-11-15 02:50:14 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2018-01-24 16:11:33 +08:00
|
|
|
log['priority'] = 'warn'
|
|
|
|
log['message'] = '%s matched rule id %d' % (addr, rule_id)
|
|
|
|
r.lpush('F2B_LOG', json.dumps(log, ensure_ascii=False))
|
2017-11-15 02:50:14 +08:00
|
|
|
ban(addr)
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
def autopurge():
|
2017-11-15 02:50:14 +08:00
|
|
|
while not quit_now:
|
2018-01-24 16:11:33 +08:00
|
|
|
BAN_TIME = int(r.get('F2B_BAN_TIME'))
|
|
|
|
MAX_ATTEMPTS = int(r.get('F2B_MAX_ATTEMPTS'))
|
|
|
|
QUEUE_UNBAN = r.hgetall('F2B_QUEUE_UNBAN')
|
2017-11-15 02:50:14 +08:00
|
|
|
if QUEUE_UNBAN:
|
|
|
|
for net in QUEUE_UNBAN:
|
|
|
|
unban(str(net))
|
|
|
|
for net in bans.copy():
|
|
|
|
if bans[net]['attempts'] >= MAX_ATTEMPTS:
|
|
|
|
if time.time() - bans[net]['last_attempt'] > BAN_TIME:
|
|
|
|
unban(net)
|
|
|
|
time.sleep(10)
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2017-07-05 00:08:20 +08:00
|
|
|
|
2017-11-15 02:50:14 +08:00
|
|
|
watch_thread = Thread(target=watch)
|
|
|
|
watch_thread.daemon = True
|
|
|
|
watch_thread.start()
|
2017-05-25 15:57:40 +08:00
|
|
|
|
2017-11-15 02:50:14 +08:00
|
|
|
autopurge_thread = Thread(target=autopurge)
|
|
|
|
autopurge_thread.daemon = True
|
|
|
|
autopurge_thread.start()
|
2017-05-25 15:57:40 +08:00
|
|
|
|
2017-11-15 02:50:14 +08:00
|
|
|
signal.signal(signal.SIGTERM, quit)
|
|
|
|
atexit.register(clear)
|
2017-05-25 15:57:40 +08:00
|
|
|
|
2017-11-15 02:50:14 +08:00
|
|
|
while not quit_now:
|
|
|
|
time.sleep(0.5)
|