fail2ban 常用命令:
    1. fail2ban-client status freeswitch #查看当前freeswitch 被禁止的服务器的状态

    1. fail2ban-client reload #重新加载fail2ban配置

    2. fail2ban-client set freeswitch unbanip 1.2.3.4 #设置freeswitch策略,解禁IP 1.2.3.4

    fail2ban的配置文件可以通过下面命令校验:
    fail2ban-regex /opt/utry/ccfs/log/freeswitch.log /etc/fail2ban/filter.d/freeswitch.conf
    fail2ban-regex freeswitch.log “failregex” 测试了这个正则表达式
    修改 /etc/fail2ban/jail.d/freeswitch.local 用 fail2ban-client reload 生效
    修改 /etc/fail2ban/filter.d/freeswitch.conf 用 systemctl restart fail2ban 生效

    从CentOS7开始,官方的标准防火墙设置软件从iptables变更为firewalld。 为了使Fail2ban与iptables联动,需禁用自带的firewalld服务,同时安装iptables服务。因此,在进行Fail2ban的安装与使用前需根据博客CentOS7安装和配置iptables防火墙进行环境配置。

    一. 安装 fail2ban
    centos: sudo yum install fail2ban

    ubuntu: sudo apt install fail2ban

    设置开机自启动 systemctl enable fail2ban

    启动: systemctl start fail2ban

    二. 配置文件简单介绍
    1. fail2ban.conf

    fail2ban的基本配置;日志路径,日志级别等

    1. jail.conf

    jail官方提供的,一些常用服务器的配置默认都是没有enable的;建议不要直接改动, 里面也有个freeswitch的配置,可以参考。

    可以在jail.d目录下,就单独的服务在重新配置

    官方的文档写到:在配置时,我们应该避免修改由fail2ban安装创建的文件,我们应该去编写具有.local扩展名的新文件。在.local新文件里配置的内容会覆盖jail.conf内容里相同的值

    1. filter.d 目录;有各个服务给出的默认过滤配置;其中也有freeswitch.conf 文件;过滤规则可参考

    2. jail.d 目录

    针对具体过滤的服务的配置。配置文件以.local后缀结尾

    注意:centos系统,该目录下有个默认的配置文件,把它删除

    三. 增加freeswitch的策略文件
    在jail.d 目录下新建freeswitch.local 文件,添加下面内容;也可以根据实际情况进行配置,

    这个配置文件是 3600秒(1个小时) 中有5次匹配到 failregex 中的内容,则进行 action 操作;禁止时间为 259200(3天)

    [freeswitch]
    enabled = true
    maxretry = 4 ; for a total of five failures
    findtime = 3600 ; based on empirical testing
    bantime = 259200 ; ban for 3day (which lets us pick up repeat offenders)
    # We do not use 5061 and it is not open to the outside world, so act on 5060 only
    port = 7060
    failregex = ^.\d+ [WARNING] sofiareg.c:\d+ SIP auth (failure) ((REGISTER|INVITE)) on sofia profile \’[^’]+\’ for [.*] from ip $
    ^.\d+ [WARNING] sofiareg.c:\d+ Can’t find user [\d+@\d+.\d+.\d+.\d+] from $
    # we do not want mail, so remove that action from standard jail.conf freeswitch config
    action = %(banaction)s[name=%(name)s-tcp, port=”%(port)s”, protocol=”tcp”, chain=”%(chain)s”, actname=%(banaction)s-tcp]
    %(banaction)s[name=%(__name
    )s-udp, port=”%(port)s”, protocol=”udp”, chain=”%(chain)s”, actname=%(banaction)s-udp]
    # Add intrusion log, which contains special string for host (see freeswitch.local filter)
    logpath = /usr/local/freeswitch/log/freeswitch.log

    四. 重启 或者重新加载fail2ban的配置

    五:freeswitch 对应的网关文件打开对应配置
    我这里是默认的5060端口配置文件 internal.xml;打开