centos配置Fail2Ban防止sip攻击`FreeSWITCH`

服务器   发布日期:2025年07月16日   浏览次数:230
  1. # 使用`Fail2Ban`防止sip攻击`FreeSWITCH`
  2. `FreeSWITCH`在公网运行容易遭受sip攻击,解决的办法有很多种,而`Fail2Ban`安装配置以及调试比较简单,不失为一种好的选择。
  3. `Fail2Ban`版本很多,配置方式略有差别。
  4. 本次测试是基于`Fail2Ban 0.9.`版本,其他相关信息如下:
  5. - Debian9
  6. - FreeSWITCH 1.10.,`base_dir``/usr/local/freeswitch`
  7. ## 安装`Fail2Ban`
  8. ```shell
  9. cd /usr/src; git clone https://github.com/fail2ban/fail2ban.git -b 0.9.6
  10. cd /usr/src/fail2ban; python3 setup.py install
  11. #配置成服务
  12. cp files/debian-initd /etc/init.d/fail2ban
    #centos

    cp files/redhat-initd /etc/init.d/fail2ban
  1. https://github.com/fail2ban/fail2ban/blob/0.11/files/redhat-initd
  2. update-rc.d fail2ban defaults
  3. service fail2ban start
  4. ```
  5. ## 配置`iptables`
  6. ```shell
  7. iptables -A INPUT -i lo -j ACCEPT
  8. iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
  9. iptables -A INPUT -p tcp --dport -j ACCEPT
  10. iptables -A INPUT -p tcp --dport -j ACCEPT
  11. iptables -A INPUT -p tcp --dport -j ACCEPT
  12. iptables -A INPUT -p tcp --dport -j ACCEPT
  13. iptables -A INPUT -p tcp --dport -j ACCEPT
  14. iptables -A INPUT -p tcp --dport : -j ACCEPT
  15. iptables -A INPUT -p udp --dport -j ACCEPT
  16. iptables -A INPUT -p tcp --dport -j ACCEPT
  17. iptables -A INPUT -p udp --dport -j ACCEPT
  18. iptables -A INPUT -p udp --dport : -j ACCEPT
  19. iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
  20. iptables -P INPUT DROP
  21. iptables -P FORWARD DROP
  22. iptables -P OUTPUT ACCEPT
  23. ```
  24. ## 配置 `FreeSWITCH`
  25. . sip_profiles/internal.xml
  26. ```
  27. <param name="log-auth-failures" value="true"/>
  28. ```
  29. . autoload_configs/switch.conf.xml 要修改一个配置项目
  30. ```
  31. <param name="threaded-system-exec" value="true"/>
  32. ```
  33. ## 配置 fail2ban
  34. ### 配置 freeswitch jail
  35. 找到 `/etc/fail2ban/jail.conf` 的 freeswitch 段,修改成下面这样:
  36. ```
  37. [freeswitch]
  38. enabled = true
  39. port = ,,
  40. action = iptables-allports[name=freeswitch, protocol=all]
  41. logpath = /usr/local/freeswitch/log/freeswitch.log
  42. filter = freeswitch
  43. maxretry =
  44. bantime = -
  45. findtime =
  46. ignoreip = 127.0.0.1/ 192.168.0.0/ 10.0.0.0/ 172.16.0.0/
  47. ```
  48. 其中:
  49. * port = ,, # sip profile 的端口
  50. * action = iptables-allports[name=freeswitch, protocol=all] # 这里不用改动
  51. * logpath = /usr/local/freeswitch/log/freeswitch.log # freeswitch.log的全路径
  52. * filter = freeswitch #这里不用改动
  53. * maxretry = # 尝试次数
  54. * bantime = - # - 永久 ban(禁止)
  55. * findtime = # 发现的时间,这几个参数合起来的意思就是,如果 小时内检查到 哪个 IP 地址,做了 次尝试,那么永久禁止他
  56. * ignoreip = 127.0.0.1/ 192.168.0.0/ 10.0.0.0/ 172.16.0.0/ # ip 白名单
  57. ### 配置 freeswitch filter
  58. 修改`/etc/fail2ban/filter.d/freeswitch.conf`,改成下面这样:
  59. ```
  60. # Fail2Ban configuration file
  61. #
  62. # Enable "log-auth-failures" on each Sofia profile to monitor
  63. # <param name="log-auth-failures" value="true"/>
  64. # -- this requires a high enough loglevel on your logs to save these messages.
  65. #
  66. # In the fail2ban jail.local file for this filter set ignoreip to the internal
  67. # IP addresses on your LAN.
  68. #
  69. [Definition]
  70. #failregex = ^\.\d+ \[WARNING\] sofia_reg\.c:\d+ SIP auth (failure|challenge) \((REGISTER|INVITE)\) on sofia profile \'[^']+\' for \[.*\] from ip <HOST>$
  71. # ^\.\d+ \[WARNING\] sofia_reg\.c:\d+ Can't find user \[\d+@\d+\.\d+\.\d+\.\d+\] from <HOST>$
  72. failregex = ^\.\d+ \[WARNING\] sofia_reg\.c:\d+ SIP auth failure \((REGISTER|INVITE)\) on sofia profile \'[^']+\' for \[.*\] from ip <HOST>$
  73. ^\.\d+ \[WARNING\] sofia_reg\.c:\d+ Can't find user \[\d+@\d+\.\d+\.\d+\.\d+\] from <HOST>$
  74. ignoreregex =
  75. # Author: Rupa SChomaker, soapee01, Daniel Black
  76. # https://freeswitch.org/confluence/display/FREESWITCH/Fail2Ban
  77. # Thanks to Jim on mailing list of samples and guidance
  78. #
  79. # No need to match the following. Its a duplicate of the SIP auth regex.
  80. # ^\.\d+ \[DEBUG\] sofia\.c:\d+ IP <HOST> Rejected by acl "\S+"\. Falling back to Digest auth\.$
  81. ```
  82. 现在运行`systemctl restart fail2ban`重启服务
  83. 再运行 `fail2ban-client status`,输出如下:
  84. ```
  85. Status
  86. |- Number of jail:
  87. `- Jail list: freeswitch
  88. ```
  89. 运行`fail2ban-client status freeswitch`,输出如下:
  90. ```
  91. Status for the jail: freeswitch
  92. |- Filter
  93. | |- Currently failed:
  94. | |- Total failed:
  95. | `- File list: /usr/local/freeswitch/log/freeswitch.log
  96. `- Actions
  97. |- Currently banned:
  98. |- Total banned:
  99. `- Banned IP list:
  100. ```
  101. 现在试着ban一个ip,执行这个命令:
  102. ```
  103. fail2ban-client set freeswitch banip 113.113.113.113
  104. ```
  105. 然后用`fail2ban-client status freeswitch`查看
  106. ```
  107. Status for the jail: freeswitch
  108. |- Filter
  109. | |- Currently failed:
  110. | |- Total failed:
  111. | `- File list: /usr/local/freeswitch/log/freeswitch.log
  112. `- Actions
  113. |- Currently banned:
  114. |- Total banned:
  115. `- Banned IP list: 113.113.113.113
  116. ```
  117. 可以看到, `113.113.113.113` 这个地址已经被ban
  118. 执行这个命令`iptables -nvL --line-numbers`
  119. 输出如下:
  120. ```
  121. Chain INPUT (policy DROP packets, bytes)
  122. num pkts bytes target prot opt in out source destination
  123. 132K f2b-freeswitch all -- * * 0.0.0.0/ 0.0.0.0/
  124. ACCEPT all -- lo * 0.0.0.0/ 0.0.0.0/
  125. ACCEPT all -- * * 0.0.0.0/ 0.0.0.0/ state RELATED,ESTABLISHED
  126. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpt:
  127. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpt:
  128. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpt:
  129. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpt:
  130. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpt:
  131. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpts::
  132. ACCEPT udp -- * * 0.0.0.0/ 0.0.0.0/ udp dpt:
  133. ACCEPT tcp -- * * 0.0.0.0/ 0.0.0.0/ tcp dpt:
  134. ACCEPT udp -- * * 0.0.0.0/ 0.0.0.0/ udp dpt:
  135. ACCEPT udp -- * * 0.0.0.0/ 0.0.0.0/ udp dpts::
  136. ACCEPT icmp -- * * 0.0.0.0/ 0.0.0.0/ icmptype
  137. Chain FORWARD (policy DROP packets, bytes)
  138. num pkts bytes target prot opt in out source destination
  139. Chain OUTPUT (policy ACCEPT packets, bytes)
  140. num pkts bytes target prot opt in out source destination
  141. Chain f2b-freeswitch ( references)
  142. num pkts bytes target prot opt in out source destination
  143. REJECT all -- * * 113.113.113.113 0.0.0.0/ reject-with icmp-port-unreachable
  144. 132K RETURN all -- * * 0.0.0.0/ 0.0.0.0/
  145. ```
  146. 用这个命令解除,`fail2ban-client set freeswitch unbanip 113.113.113.113`
  147. ## 把 FreeSWITCH 运行起来,运行`tail -f /var/log/fail2ban.log`进行观察,再结合`/usr/local/freeswitch/log/freeswitch.log`的日志内容,进行调试,不断优化。
  148. ## fail2ban.lua
  149. 一般情况下`Fail2Ban`工作的很好,但还是有特殊的呼叫流程`Fail2Ban`抓不到。为此,笔者写了个`fail2ban.lua`,弥补`Fail2Ban`的不足
  150. . 修改`lua.conf.xml`,增加下面俩个配置项目:
  151. ```
  152. <hook event="CUSTOM" subclass="sofia::wrong_call_state" script="fail2ban.lua"/>
  153. <hook event="CUSTOM" subclass="sofia::register_failure" script="fail2ban.lua"/>
  154. ```
  155. 下面是`fail2ban.lua`的内容(代码比较简单,不再解释了):
  156. ```
  157. function OnEvent(e)
  158. local subclass = e:getHeader("Event-Subclass") or ""
  159. if string.find(subclass, "sofia::") ~= then return end
  160. local ip = e:getHeader("network_ip") or e:getHeader("network-ip")
  161. if not ip then return end
  162. local ua = e:getHeader("user-agent") or ""
  163. local to_user = e:getHeader("to-user") or ""
  164. local from_user = e:getHeader("from-user") or ""
  165. local auth_result = e:getHeader("auth-result") or ""
  166. local registration_type = e:getHeader("registration-type") or ""
  167. local s = string.format("*** %s, ip = %s, ua = %s, to = %s, from = %s, result = %s, type = %s\n", subclass, ip, ua, to_user, from_user, auth_result, registration_type)
  168. freeswitch.consoleLog("NOTICE", s)
  169. if subclass == "sofia::wrong_call_state" or subclass == "sofia::register_failure" then
  170. local cmd = "fail2ban-client set freeswitch banip " .. ip
  171. freeswitch.consoleLog("ERR", cmd .. "\n")
  172. os.execute(cmd)
  173. end
  174. end
  175. freeswitch.consoleLog("INFO", "fail2ban.lua, ===\n" .. event:serialize() .. "===\n")
  176. OnEvent(event)
  177. ```
  178. ## 参考资料:
  179. <https://docs.fusionpbx.com/en/latest/firewall/fail2ban.html>
  180. <https://docs.fusionpbx.com/en/latest/firewall/iptables.html>
  181. <https://freeswitch.org/confluence/display/FREESWITCH/Fail2Ban>



  1. https://blog.csdn.net/weixin_43103905/article/details/95060220
  1. 注意:
    如果遇到报错“no directory /var/run/fail2ban to contain the socket file /var/run/fail2ban/fail2ban.sock”, 请手动创建相关文件夹:
    mkdir /var/run/fail2ban

#开机启动 

$ systemctl enable fail2ban
$ systemctl start fail2ban

  1. redhat-initd



以上就是centos配置Fail2Ban防止sip攻击`FreeSWITCH`的详细内容,更多关于centos配置Fail2Ban防止sip攻击`FreeSWITCH`的资料请关注九品源码其它相关文章!