Thuban
2017-03-29 3a37e640ba952bbaf726b38077f8d0adf6850fec
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
#!/usr/bin/env python3
# -*- coding:Utf-8 -*- 
 
 
"""
Author :      thuban <thuban@yeuxdelibad.net>  
Licence :     MIT
Require : python >= 3.5
 
Description : Mimic fail2ban with pf for OpenBSD.
              Inspired from http://www.vincentdelft.be/post/post_20161106
 
              In pf.conf, add : 
                    table <vilain_bruteforce> persist
                    block quick from <vilain_bruteforce> 
 
              You might want to add a cron task to remove old banned IP. As example, to ban for one day max : 
                    pfctl -t vilain_bruteforce -T expire 86400
 
              To see banned IP : 
                    pfctl -t vilain_bruteforce -T show
"""
 
import sys
import os
import configparser
import re
import logging
import subprocess
import asyncio
import time
from multiprocessing import Process
 
configfile = "/etc/vilain.conf"
version = "0.3"
vilain_table = "vilain_bruteforce"
logfile = "/var/log/daemon"
sleeptime = 0.5
 
if os.geteuid() != 0:
    print("Only root can use this tool")
    sys.exit()
 
# Configure logging
logger = logging.getLogger(__name__)
logging.basicConfig(filename=logfile,
                    format='%(asctime)s %(module)s:%(funcName)s:%(message)s',
                    datefmt='%H:%M:%S')
logger.setLevel(logging.DEBUG)
ch = logging.StreamHandler(sys.stdout)
logger.addHandler(ch)
 
# functions
def readconfig():
    if not os.path.isfile(configfile):
        logging.error("Can't read config file, exiting...")
        sys.exit(1)
 
    config = configparser.ConfigParser()
    config.read(configfile)
    return(config)
 
def load_config():
    c = readconfig()
    d = c.defaults()
    watch_while = int(d['watch_while'])
    maxtries = int(d['maxtries'])
    vilain_table = d['vilain_table']
 
    if c.has_section('ignoreip'):
        ignoreip = [ i[1] for i in c.items('ignoreip') if i[0] not in c.defaults()]
    else:
        ignoreip = []
    return(watch_while, maxtries, vilain_table, ignoreip)
 
def load_sections():
    c = readconfig()
    for s in c.sections():
        if c.has_option(s,'logfile'):
            logfile = c.get(s,'logfile')
            regex = c.get(s,'regex')
            d = {'name' : s, 'logfile':logfile, 'regex':regex}
            yield d
 
class Vilain():
    def __init__(self):
        self.loop = asyncio.get_event_loop()
        self.watch_while, self.maxtries, self.vilain_table, self.ignore_ip = load_config()
        #self.bad_ip_queue = []
        self.bad_ip_queue = asyncio.Queue(loop=self.loop)
 
        for entry in load_sections():
            logger.info("Start vilain for {}".format(entry['name']))
            asyncio.ensure_future(self.check_logs(entry['logfile'], entry['regex'], entry['name']))
 
        asyncio.ensure_future(self.ban_ips())
 
    def start(self):
        try:
            self.loop.run_forever()
        except KeyboardInterrupt:
            self.loop.close()
        finally:
            self.loop.close()
 
    async def check_logs(self, logfile, regex, reason):
        """
        worker who put in bad_ip_queue bruteforce IP
        """
        if not os.path.isfile(logfile) :
            logger.warning("{} doesn't exist".format(logfile))
        else :
            # Watch the file for changes
            stat = os.stat(logfile)
            size = stat.st_size
            mtime = stat.st_mtime
            RE = re.compile(regex)
            while True:
                await asyncio.sleep(sleeptime)
                stat = os.stat(logfile)
                if mtime < stat.st_mtime:
                    mtime = stat.st_mtime
                    with open(logfile, "rb") as f:
                        f.seek(size)
                        lines = f.readlines()
                        ul = [ u.decode() for u in lines ]
                        line = "".join(ul).strip()
 
                        ret = RE.match(line)
                        if ret:
                            bad_ip = ret.groups()[0]
                            if bad_ip not in self.ignore_ip :
                                #self.bad_ip_queue.append({'ip' : bad_ip, 'reason' : reason})
                                await self.bad_ip_queue.put({'ip' : bad_ip, 'reason' : reason})
                    size = stat.st_size
 
    async def ban_ips(self):
        """
        worker who ban IP on bad_ip_queue
        add IP in bad_ips_list 
        record time when this IP has been seen in ip_seen_at = { ip:time }
 
        check number of occurence of the same ip in bad_ips_list
        if more than 3 : ban and clean of list
 
        check old ip in ip_seen_at : remove older than watch_while
        """
 
        bad_ips_list = []
        ip_seen_at = {}
        while True:
            await asyncio.sleep(sleeptime)
            #if not len(s#elf.bad_ip_queue) > 0:
            #    continue
            ip_item = await self.bad_ip_queue.get()
            #ip_item = self.bad_ip_queue.pop()
            ip = ip_item['ip']
            reason = ip_item['reason']
            logger.info("{} detected, reason {}".format(ip, reason))
            bad_ips_list.append(ip)
            ip_seen_at[ip] = time.time()
            n_ip = bad_ips_list.count(ip)
            if n_ip >= self.maxtries:
                logger.info("Blacklisting {}".format(ip))
                subprocess.call(["pfctl", "-t", self.vilain_table, "-T", "add", ip])
                ip_seen_at.pop(ip)
                while ip in bad_ips_list:
                    bad_ips_list.remove(ip)
 
            to_remove = []
            for recorded_ip, last_seen in ip_seen_at.items():
                if time.time() - last_seen >= self.watch_while:
                    logger.info("{} not seen since a long time, forgetting...".format(recorded_ip))
                    to_remove.append(recorded_ip)
            for i in to_remove:
                ip_seen_at.pop(i)
 
 
 
 
 
def main():
    os.chdir(os.path.dirname(os.path.abspath(__file__)))
    v = Vilain()
    p = Process(target=v.start())
    p.start()
    return 0
 
if __name__ == '__main__':
    main()
 
 
# vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4