2017-05-25 15:57:40 +08:00
|
|
|
#!/usr/bin/env python2
|
|
|
|
|
|
|
|
import re
|
|
|
|
import time
|
|
|
|
import atexit
|
|
|
|
import signal
|
|
|
|
import ipaddress
|
|
|
|
import subprocess
|
|
|
|
from threading import Thread
|
|
|
|
import docker
|
2017-06-24 06:07:18 +08:00
|
|
|
import redis
|
|
|
|
import time
|
|
|
|
import json
|
2017-05-25 15:57:40 +08:00
|
|
|
|
2017-06-24 06:07:18 +08:00
|
|
|
r = redis.StrictRedis(host='172.22.1.249', port=6379, db=0)
|
2017-05-25 15:57:40 +08:00
|
|
|
RULES = {
|
|
|
|
'mailcowdockerized_postfix-mailcow_1': 'warning: .*\[([0-9a-f\.:]+)\]: SASL .* authentication failed',
|
|
|
|
'mailcowdockerized_dovecot-mailcow_1': '-login: Disconnected \(auth failed, .*\): user=.*, method=.*, rip=([0-9a-f\.:]+),',
|
|
|
|
'mailcowdockerized_sogo-mailcow_1': 'SOGo.* Login from \'([0-9a-f\.:]+)\' for user .* might not have worked',
|
2017-05-25 19:57:50 +08:00
|
|
|
'mailcowdockerized_php-fpm-mailcow_1': 'Mailcow UI: Invalid password for .* by ([0-9a-f\.:]+)',
|
2017-05-25 15:57:40 +08:00
|
|
|
}
|
2017-06-24 06:07:18 +08:00
|
|
|
|
|
|
|
r.setnx("F2B_BAN_TIME", "1800")
|
|
|
|
r.setnx("F2B_MAX_ATTEMPTS", "10")
|
2017-06-25 02:04:12 +08:00
|
|
|
r.setnx("F2B_RETRY_WINDOW", "600")
|
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):
|
2017-06-24 06:07:18 +08:00
|
|
|
BAN_TIME = int(r.get("F2B_BAN_TIME"))
|
|
|
|
MAX_ATTEMPTS = int(r.get("F2B_MAX_ATTEMPTS"))
|
2017-06-25 02:04:12 +08:00
|
|
|
RETRY_WINDOW = int(r.get("F2B_RETRY_WINDOW"))
|
|
|
|
|
2017-05-25 15:57:40 +08:00
|
|
|
ip = ipaddress.ip_address(address.decode('ascii'))
|
2017-05-25 19:57:50 +08:00
|
|
|
if type(ip) is ipaddress.IPv6Address and ip.ipv4_mapped:
|
|
|
|
ip = ip.ipv4_mapped
|
|
|
|
address = str(ip)
|
2017-05-25 15:57:40 +08:00
|
|
|
if ip.is_private or ip.is_loopback:
|
|
|
|
return
|
|
|
|
|
|
|
|
net = ipaddress.ip_network((address + ('/24' if type(ip) is ipaddress.IPv4Address else '/64')).decode('ascii'), strict=False)
|
|
|
|
net = str(net)
|
2017-06-25 02:04:12 +08:00
|
|
|
|
|
|
|
if not net in bans or time.time() - bans[net]['last_attempt'] > RETRY_WINDOW:
|
2017-05-25 15:57:40 +08:00
|
|
|
bans[net] = { 'attempts': 0 }
|
2017-06-25 02:04:12 +08:00
|
|
|
active_window = RETRY_WINDOW
|
|
|
|
else:
|
|
|
|
active_window = time.time() - bans[net]['last_attempt']
|
2017-05-25 15:57:40 +08:00
|
|
|
|
|
|
|
bans[net]['attempts'] += 1
|
|
|
|
bans[net]['last_attempt'] = time.time()
|
|
|
|
|
2017-06-25 02:04:12 +08:00
|
|
|
active_window = time.time() - bans[net]['last_attempt']
|
|
|
|
|
2017-05-25 15:57:40 +08:00
|
|
|
if bans[net]['attempts'] >= MAX_ATTEMPTS:
|
2017-06-24 06:07:18 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2017-06-25 02:04:12 +08:00
|
|
|
log['priority'] = "crit"
|
2017-06-24 06:07:18 +08:00
|
|
|
log['message'] = "Banning %s" % net
|
|
|
|
r.lpush("F2B_LOG", json.dumps(log, ensure_ascii=False))
|
2017-06-25 02:04:12 +08:00
|
|
|
print "Banning %s for %d minutes" % (net, BAN_TIME / 60)
|
2017-05-25 15:57:40 +08:00
|
|
|
if type(ip) is ipaddress.IPv4Address:
|
|
|
|
subprocess.call(["iptables", "-I", "INPUT", "-s", net, "-j", "REJECT"])
|
|
|
|
subprocess.call(["iptables", "-I", "FORWARD", "-s", net, "-j", "REJECT"])
|
|
|
|
else:
|
|
|
|
subprocess.call(["ip6tables", "-I", "INPUT", "-s", net, "-j", "REJECT"])
|
|
|
|
subprocess.call(["ip6tables", "-I", "FORWARD", "-s", net, "-j", "REJECT"])
|
|
|
|
else:
|
2017-06-24 06:07:18 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2017-06-25 02:04:12 +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)
|
2017-06-24 06:07:18 +08:00
|
|
|
r.lpush("F2B_LOG", json.dumps(log, ensure_ascii=False))
|
2017-06-25 02:04:12 +08:00
|
|
|
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-06-24 06:07:18 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2017-06-25 02:04:12 +08:00
|
|
|
log['priority'] = "info"
|
2017-06-24 06:07:18 +08:00
|
|
|
log['message'] = "Unbanning %s" % net
|
|
|
|
r.lpush("F2B_LOG", json.dumps(log, ensure_ascii=False))
|
2017-05-25 15:57:40 +08:00
|
|
|
print "Unbanning %s" % net
|
|
|
|
if type(ipaddress.ip_network(net.decode('ascii'))) is ipaddress.IPv4Network:
|
|
|
|
subprocess.call(["iptables", "-D", "INPUT", "-s", net, "-j", "REJECT"])
|
|
|
|
subprocess.call(["iptables", "-D", "FORWARD", "-s", net, "-j", "REJECT"])
|
|
|
|
else:
|
|
|
|
subprocess.call(["ip6tables", "-D", "INPUT", "-s", net, "-j", "REJECT"])
|
|
|
|
subprocess.call(["ip6tables", "-D", "FORWARD", "-s", net, "-j", "REJECT"])
|
|
|
|
del bans[net]
|
|
|
|
|
|
|
|
def quit(signum, frame):
|
|
|
|
global quit_now
|
|
|
|
quit_now = True
|
|
|
|
|
|
|
|
def clear():
|
2017-06-24 06:07:18 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2017-06-25 02:04:12 +08:00
|
|
|
log['priority'] = "info"
|
2017-06-24 06:07:18 +08:00
|
|
|
log['message'] = "Clearing all bans"
|
|
|
|
r.lpush("F2B_LOG", json.dumps(log, ensure_ascii=False))
|
2017-05-25 15:57:40 +08:00
|
|
|
print "Clearing all bans"
|
|
|
|
for net in bans.copy():
|
|
|
|
unban(net)
|
|
|
|
|
|
|
|
def watch(container):
|
2017-06-24 06:07:18 +08:00
|
|
|
log['time'] = int(round(time.time()))
|
2017-06-25 02:04:12 +08:00
|
|
|
log['priority'] = "info"
|
2017-06-24 06:07:18 +08:00
|
|
|
log['message'] = "Watching %s" % container
|
|
|
|
r.lpush("F2B_LOG", json.dumps(log, ensure_ascii=False))
|
2017-05-25 15:57:40 +08:00
|
|
|
print "Watching", container
|
|
|
|
client = docker.from_env()
|
|
|
|
for msg in client.containers.get(container).attach(stream=True, logs=False):
|
|
|
|
result = re.search(RULES[container], msg)
|
|
|
|
if result:
|
|
|
|
addr = result.group(1)
|
|
|
|
ban(addr)
|
|
|
|
|
|
|
|
def autopurge():
|
|
|
|
while not quit_now:
|
2017-06-24 06:07:18 +08:00
|
|
|
BAN_TIME = int(r.get("F2B_BAN_TIME"))
|
2017-06-25 02:04:12 +08:00
|
|
|
MAX_ATTEMPTS = int(r.get("F2B_MAX_ATTEMPTS"))
|
2017-05-25 15:57:40 +08:00
|
|
|
for net in bans.copy():
|
2017-06-25 02:04:12 +08:00
|
|
|
if bans[net]['attempts'] >= MAX_ATTEMPTS:
|
|
|
|
if time.time() - bans[net]['last_attempt'] > BAN_TIME:
|
|
|
|
unban(net)
|
2017-05-25 15:57:40 +08:00
|
|
|
time.sleep(60)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
threads = []
|
|
|
|
for container in RULES:
|
|
|
|
threads.append(Thread(target=watch, args=(container,)))
|
|
|
|
threads[-1].daemon = True
|
|
|
|
threads[-1].start()
|
|
|
|
|
|
|
|
autopurge_thread = Thread(target=autopurge)
|
|
|
|
autopurge_thread.daemon = True
|
|
|
|
autopurge_thread.start()
|
|
|
|
|
|
|
|
signal.signal(signal.SIGTERM, quit)
|
|
|
|
atexit.register(clear)
|
|
|
|
|
|
|
|
while not quit_now:
|
|
|
|
for thread in threads:
|
|
|
|
if not thread.isAlive():
|
|
|
|
break
|
|
|
|
time.sleep(0.1)
|
|
|
|
|
|
|
|
clear()
|