Freeswitch 集成fail2ban配置
    1. 总体说明
    使用fail2ban+iptables相结合,防范voip攻击
    2. fail2ban的安装与配置
    2.1 安装fail2ban
    CentOS 5
    yum install http://dl.fedoraproject.org/pub/epel/5/i386/epel-release-5-4.noarch.rpm
    yum install fail2ban
    CentOS 6
    yum install http://dl.fedoraproject.org/pub/epel/6/i386/epel-release-6-8.noarch.rpm
    yum install fail2ban
    CentOS 7
    yum install fail2ban

    2.2 配置fail2ban
    2.2.1 配置日志级别及路径
    文件/etc/fail2ban/fail2ban.conf
    默认fail2ban.conf里面就三个参数,而且都有注释。
    #默认日志的级别
    # CRITICAL, ERROR, WARNING, NOTICE, INFO, DEBUG,缺省值是ERROR
    loglevel = INFO
    #日志的存放路径
    logtarget = /var/log/fail2ban.log
    2.2.2 Fail2ban全局配置
    文件/etc/fail2ban/jail.conf
    [DEFAULT]
    #忽略IP的范围,如果有二组以上用空白做为间隔,可以是 IP 地址、CIDR 前缀或#者 DNS 主机名,用于指定哪些地址可以忽略 fail2ban 防御
    ignoreip = 127.0.0.1/8 10.10.0.0/24 192.168.0.0/24
    #设定IP 被封锁的时间(秒),如果值为 -1,代表永远封锁
    bantime = 600
    #设定在多少时间内(秒)达到maxretry的次数就封锁
    findtime = 600
    #客户端允许尝试的次数
    maxretry = 3
    #是否开启fail2ban 防御,false不开启,true开启
    enabled = false
    #action为iptables-multiport,可以配置为(e.g. iptables, iptables-new,
    # iptables-multiport, shorewall, etc)
    banaction = iptables-multiport
    3. FreeSWITCH配置
    使用fail2ban时,需要开启freeswitch的注册错误日志,以便进行跟踪:
    文件:/usr/local/freeswitch/conf/sbcprofiles/ / externaltointernalusers.xml将下面的属性设置为true,如下所示:

    4. 防火墙iptables配置
    4.1 开启防火墙
    使用以下命令开启防火墙:
    chkconfig iptables on
    #注:如果需要关闭防火墙的话,使用命令chkconfig iptables off即可。
    4.2 防火墙配置
    A、 修改防火墙配置
    文件: /etc/sysconfig/iptables-config 将下面两个圈出来的属性修改为yes,如下所示:
    # Save current firewall rules on stop.
    # Value: yes|no, default: no
    # Saves all firewall rules to /etc/sysconfig/iptables if firewall gets stopped
    # (e.g. on system shutdown).
    IPTABLES_SAVE_ON_STOP=”yes
    # Save current firewall rules on restart.
    # Value: yes|no, default: no
    # Saves all firewall rules to /etc/sysconfig/iptables if firewall gets
    # restarted.
    IPTABLES_SAVE_ON_RESTART=”yes
    B、 重启防火墙
    service iptables restart
    5. Fail2ban防范攻击配置
    5.1 防范SIP注册失败攻击
    该攻击是由攻击方短时间内大量发起SIP注册消息,导致服务器瘫痪。
    5.1.1 增加规则匹配文件
    新增文件/etc/fail2ban/filter.d/freeswitch-all.conf
    在/etc/fail2ban/filter.d下新建freeswitch-all.conf文件,内容如下:
    [Definition]
    failregex = [WARNING] sofia_reg.c:\d+ SIP auth challenge (REGISTER) on sofia profile \’[^’]+\’ for [.] from ip $
    [WARNING] sofia_reg.c:\d+ SIP auth challenge (INVITE) on sofia profile \’[^’]+\’ for [.
    ] from ip $
    [WARNING] sofia.c:\d+ IP Rejected by acl .
    ignoreregex =
    注意failregex格式
    Freeswitch 集成fail2ban配置 - 图1
    *5.1.2 修改jail.conf文件

    路径/etc/fail2ban/jail.conf,在文件的最后添加上如下的内容:
    [freeswitch]
    enabled = true
    port = 5070,35080
    filter = freeswitch-all
    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]
    logpath = /usr/local/freeswitch/log/freeswitch.log
    maxretry = 4
    bantime = 60
    findtime = 600
    注意action格式:
    Freeswitch 集成fail2ban配置 - 图2
    5.1.3 重启fail2ban
    /etc/init.d/fail2ban restart
    5.1.4 检查规则是否被加载进iptables中
    运行iptables –L查看所有规则,将有两条规则加入iptables中:Chain f2b-freeswitch-tcp和Chain f2b-freeswitch-udp。
    [root@iZ23vslqjlcZ ~]# iptables -L f2b-freeswitch-udp
    Chain f2b-freeswitch-udp (1 references)
    target prot opt source destination
    RETURN all — anywhere anywhere
    [root@iZ23vslqjlcZ ~]# iptables -L f2b-freeswitch-tcp
    Chain f2b-freeswitch-tcp (1 references)
    target prot opt source destination
    RETURN all — anywhere anywhere
    5.1.5 测试
    使用eyeBeam,连续多次用错误的密码进行登录。
    超过三次后就会提示服务器无法连接,说明该IP已被服务器封锁。
    查看iptables列表:
    [root@iZ23vslqjlcZ ~]# iptables -L f2b-freeswitch-udp
    Chain f2b-freeswitch-udp (1 references)
    target prot opt source destination
    REJECT all — 183.129.201.58 anywhere reject-with icmp-port-unreachable
    RETURN all — anywhere anywhere
    [root@iZ23vslqjlcZ ~]# iptables -L f2b-freeswitch-tcp
    Chain f2b-freeswitch-tcp (1 references)
    target prot opt source destination
    REJECT all — 183.129.201.58 anywhere reject-with icmp-port-unreachable
    RETURN all — anywhere anywhere