1
0

fai2ban.sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. #!/bin/bash
  2. #27/01/2021
  3. declare -A cor=( [0]="\033[1;37m" [1]="\033[1;34m" [2]="\033[1;31m" [3]="\033[1;33m" [4]="\033[1;32m" )
  4. pid_fail=$(dpkg -l | grep fail2ban | grep ii)
  5. apache=$(dpkg -l | grep apache2 | grep ii)
  6. squid=$(dpkg -l | grep squid | grep ii)
  7. dropbear=$(dpkg -l | grep dropbear | grep ii)
  8. openssh=$(dpkg -l | grep openssh | grep ii)
  9. stunnel4=$(dpkg -l | grep stunnel4 | grep ii)
  10. [[ "$openssh" != "" ]] && s1="ssh"
  11. [[ "$squid" != "" ]] && s2="squid"
  12. [[ "$dropbear" != "" ]] && s3="dropbear"
  13. [[ "$apache" != "" ]] && s4="apache"
  14. [[ "$stunnel4" != "" ]] && s5="stunnel4"
  15. clear
  16. clear
  17. msg -bar
  18. msg -tit
  19. echo -e "\e[93m -- Fail2ban Protection v0.11.2 -- "
  20. echo -e "\e[97m Anti ataques DDOS y spoofing SPAM"
  21. msg -bar
  22. if [[ ! -z "$pid_fail" ]]; then
  23. echo -e "${cor[2]} [1] >${cor[5]} $(fun_trans "Desinstalar Fail2ban")"
  24. echo -e "${cor[2]} [2] >\e[92m $(fun_trans "Mirar el registro")"
  25. msg -bar
  26. while [[ -z ${logxyz} || ${logxyz} != @(1|2) ]]; do
  27. echo -ne "\033[1;37m$(fun_trans "Seleccione una Opcion"): " && read logxyz
  28. tput cuu1 && tput dl1
  29. done
  30. case ${logxyz} in
  31. 1)apt-get remove fail2ban -y &> /dev/null;;
  32. 2)cat /var/log/fail2ban.log
  33. msg -bar;;
  34. esac
  35. exit 0
  36. fi
  37. echo -e "${cor[5]} Desea Instalar Fail2ban?"
  38. msg -bar
  39. while [[ -z ${fail2ban} || ${fail2ban} != @(s|S|n|N|y|Y) ]]; do
  40. echo -ne "\033[1;37m$(fun_trans "Seleccione una Opcion") [S/N]: " && read fail2ban
  41. tput cuu1 && tput dl1
  42. done
  43. if [[ "$fail2ban" = @(s|S|y|Y) ]]; then
  44. apt-get install fail2ban -y &> /dev/null
  45. wget -O $HOME/fail2ban https://github.com/fail2ban/fail2ban/archive/0.11.2.tar.gz &> /dev/null
  46. tar -xf $HOME/fail2ban &> /dev/null
  47. cd $HOME/fail2ban-0.11.2 &> /dev/null
  48. python ./setup.py install &> /dev/null
  49. echo '[INCLUDES]
  50. before = paths-debian.conf
  51. [DEFAULT]
  52. ignoreip = 127.0.0.1/8
  53. # ignorecommand = /path/to/command <ip>
  54. ignorecommand =
  55. bantime = 1036800
  56. findtime = 3600
  57. maxretry = 5
  58. backend = auto
  59. usedns = warn
  60. logencoding = auto
  61. enabled = false
  62. filter = %(__name__)s
  63. destemail = root@localhost
  64. sender = root@localhost
  65. mta = sendmail
  66. protocol = tcp
  67. chain = INPUT
  68. port = 0:65535
  69. fail2ban_agent = Fail2Ban/%(fail2ban_version)s
  70. banaction = iptables-multiport
  71. banaction_allports = iptables-allports
  72. action_ = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  73. action_mw = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  74. %(mta)s-whois[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", protocol="%(protocol)s", chain="%(chain)s"]
  75. action_mwl = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  76. %(mta)s-whois-lines[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
  77. action_xarf = %(banaction)s[name=%(__name__)s, bantime="%(bantime)s", port="%(port)s", protocol="%(protocol)s", chain="%(chain)s"]
  78. xarf-login-attack[service=%(__name__)s, sender="%(sender)s", logpath=%(logpath)s, port="%(port)s"]
  79. action_cf_mwl = cloudflare[cfuser="%(cfemail)s", cftoken="%(cfapikey)s"]
  80. %(mta)s-whois-lines[name=%(__name__)s, sender="%(sender)s", dest="%(destemail)s", logpath=%(logpath)s, chain="%(chain)s"]
  81. action_blocklist_de = blocklist_de[email="%(sender)s", service=%(filter)s, apikey="%(blocklist_de_apikey)s", agent="%(fail2ban_agent)s"]
  82. action_badips = badips.py[category="%(__name__)s", banaction="%(banaction)s", agent="%(fail2ban_agent)s"]
  83. action_badips_report = badips[category="%(__name__)s", agent="%(fail2ban_agent)s"]
  84. action = %(action_)s' > /etc/fail2ban/jail.local
  85. echo -ne "${cor[5]} $(fun_trans "Fail2ban sera activo en los Siguientes\n Puertos y Servicos"):"
  86. echo ""
  87. msg -bar
  88. echo -ne "\n"
  89. [ "$s1" != "" ] && echo -ne " $s1"
  90. [ "$s2" != "" ] && echo -ne " $s2"
  91. [ "$s3" != "" ] && echo -ne " $s3"
  92. [ "$s4" != "" ] && echo -ne " $s4"
  93. [ "$s5" != "" ] && echo -ne " $s5"
  94. echo -ne "\n"
  95. echo -ne "\n"
  96. msg -bar
  97. sleep 1
  98. if [[ "$s1" != "" ]]; then
  99. echo '[sshd]
  100. enabled = true
  101. port = ssh
  102. logpath = %(sshd_log)s
  103. backend = %(sshd_backend)s
  104. [sshd-ddos]
  105. enabled = true
  106. port = ssh
  107. logpath = %(sshd_log)s
  108. backend = %(sshd_backend)s' >> /etc/fail2ban/jail.local
  109. else
  110. echo '[sshd]
  111. port = ssh
  112. logpath = %(sshd_log)s
  113. backend = %(sshd_backend)s
  114. [sshd-ddos]
  115. port = ssh
  116. logpath = %(sshd_log)s
  117. backend = %(sshd_backend)s' >> /etc/fail2ban/jail.local
  118. fi
  119. if [[ "$s2" != "" ]]; then
  120. echo '[squid]
  121. enabled = true
  122. port = 80,443,3128,8080
  123. logpath = /var/log/squid/access.log' >> /etc/fail2ban/jail.local
  124. else
  125. echo '[squid]
  126. port = 80,443,3128,8080
  127. logpath = /var/log/squid/access.log' >> /etc/fail2ban/jail.local
  128. fi
  129. if [[ "$s3" != "" ]]; then
  130. echo '[dropbear]
  131. enabled = true
  132. port = ssh
  133. logpath = %(dropbear_log)s
  134. backend = %(dropbear_backend)s' >> /etc/fail2ban/jail.local
  135. else
  136. echo '[dropbear]
  137. port = ssh
  138. logpath = %(dropbear_log)s
  139. backend = %(dropbear_backend)s' >> /etc/fail2ban/jail.local
  140. fi
  141. if [[ "$s4" != "" ]]; then
  142. echo '[apache-auth]
  143. enabled = true
  144. port = http,https
  145. logpath = %(apache_error_log)s' >> /etc/fail2ban/jail.local
  146. else
  147. echo '[apache-auth]
  148. port = http,https
  149. logpath = %(apache_error_log)s' >> /etc/fail2ban/jail.local
  150. fi
  151. echo '[selinux-ssh]
  152. port = ssh
  153. logpath = %(auditd_log)s
  154. [apache-badbots]
  155. port = http,https
  156. logpath = %(apache_access_log)s
  157. bantime = 172800
  158. maxretry = 1
  159. [apache-noscript]
  160. port = http,https
  161. logpath = %(apache_error_log)s
  162. [apache-overflows]
  163. port = http,https
  164. logpath = %(apache_error_log)s
  165. maxretry = 2
  166. [apache-nohome]
  167. port = http,https
  168. logpath = %(apache_error_log)s
  169. maxretry = 2
  170. [apache-botsearch]
  171. port = http,https
  172. logpath = %(apache_error_log)s
  173. maxretry = 2
  174. [apache-fakegooglebot]
  175. port = http,https
  176. logpath = %(apache_access_log)s
  177. maxretry = 1
  178. ignorecommand = %(ignorecommands_dir)s/apache-fakegooglebot <ip>
  179. [apache-modsecurity]
  180. port = http,https
  181. logpath = %(apache_error_log)s
  182. maxretry = 2
  183. [apache-shellshock]
  184. port = http,https
  185. logpath = %(apache_error_log)s
  186. maxretry = 1
  187. [openhab-auth]
  188. filter = openhab
  189. action = iptables-allports[name=NoAuthFailures]
  190. logpath = /opt/openhab/logs/request.log
  191. [nginx-http-auth]
  192. port = http,https
  193. logpath = %(nginx_error_log)s
  194. [nginx-limit-req]
  195. port = http,https
  196. logpath = %(nginx_error_log)s
  197. [nginx-botsearch]
  198. port = http,https
  199. logpath = %(nginx_error_log)s
  200. maxretry = 2
  201. [php-url-fopen]
  202. port = http,https
  203. logpath = %(nginx_access_log)s
  204. %(apache_access_log)s
  205. [suhosin]
  206. port = http,https
  207. logpath = %(suhosin_log)s
  208. [lighttpd-auth]
  209. port = http,https
  210. logpath = %(lighttpd_error_log)s
  211. [roundcube-auth]
  212. port = http,https
  213. logpath = %(roundcube_errors_log)s
  214. [openwebmail]
  215. port = http,https
  216. logpath = /var/log/openwebmail.log
  217. [horde]
  218. port = http,https
  219. logpath = /var/log/horde/horde.log
  220. [groupoffice]
  221. port = http,https
  222. logpath = /home/groupoffice/log/info.log
  223. [sogo-auth]
  224. port = http,https
  225. logpath = /var/log/sogo/sogo.log
  226. [tine20]
  227. logpath = /var/log/tine20/tine20.log
  228. port = http,https
  229. [drupal-auth]
  230. port = http,https
  231. logpath = %(syslog_daemon)s
  232. backend = %(syslog_backend)s
  233. [guacamole]
  234. port = http,https
  235. logpath = /var/log/tomcat*/catalina.out
  236. [monit]
  237. #Ban clients brute-forcing the monit gui login
  238. port = 2812
  239. logpath = /var/log/monit
  240. [webmin-auth]
  241. port = 10000
  242. logpath = %(syslog_authpriv)s
  243. backend = %(syslog_backend)s
  244. [froxlor-auth]
  245. port = http,https
  246. logpath = %(syslog_authpriv)s
  247. backend = %(syslog_backend)s
  248. [3proxy]
  249. port = 3128
  250. logpath = /var/log/3proxy.log
  251. [proftpd]
  252. port = ftp,ftp-data,ftps,ftps-data
  253. logpath = %(proftpd_log)s
  254. backend = %(proftpd_backend)s
  255. [pure-ftpd]
  256. port = ftp,ftp-data,ftps,ftps-data
  257. logpath = %(pureftpd_log)s
  258. backend = %(pureftpd_backend)s
  259. [gssftpd]
  260. port = ftp,ftp-data,ftps,ftps-data
  261. logpath = %(syslog_daemon)s
  262. backend = %(syslog_backend)s
  263. [wuftpd]
  264. port = ftp,ftp-data,ftps,ftps-data
  265. logpath = %(wuftpd_log)s
  266. backend = %(wuftpd_backend)s
  267. [vsftpd]
  268. port = ftp,ftp-data,ftps,ftps-data
  269. logpath = %(vsftpd_log)s
  270. [assp]
  271. port = smtp,465,submission
  272. logpath = /root/path/to/assp/logs/maillog.txt
  273. [courier-smtp]
  274. port = smtp,465,submission
  275. logpath = %(syslog_mail)s
  276. backend = %(syslog_backend)s
  277. [postfix]
  278. port = smtp,465,submission
  279. logpath = %(postfix_log)s
  280. backend = %(postfix_backend)s
  281. [postfix-rbl]
  282. port = smtp,465,submission
  283. logpath = %(postfix_log)s
  284. backend = %(postfix_backend)s
  285. maxretry = 1
  286. [sendmail-auth]
  287. port = submission,465,smtp
  288. logpath = %(syslog_mail)s
  289. backend = %(syslog_backend)s
  290. [sendmail-reject]
  291. port = smtp,465,submission
  292. logpath = %(syslog_mail)s
  293. backend = %(syslog_backend)s
  294. [qmail-rbl]
  295. filter = qmail
  296. port = smtp,465,submission
  297. logpath = /service/qmail/log/main/current
  298. [dovecot]
  299. port = pop3,pop3s,imap,imaps,submission,465,sieve
  300. logpath = %(dovecot_log)s
  301. backend = %(dovecot_backend)s
  302. [sieve]
  303. port = smtp,465,submission
  304. logpath = %(dovecot_log)s
  305. backend = %(dovecot_backend)s
  306. [solid-pop3d]
  307. port = pop3,pop3s
  308. logpath = %(solidpop3d_log)s
  309. [exim]
  310. port = smtp,465,submission
  311. logpath = %(exim_main_log)s
  312. [exim-spam]
  313. port = smtp,465,submission
  314. logpath = %(exim_main_log)s
  315. [kerio]
  316. port = imap,smtp,imaps,465
  317. logpath = /opt/kerio/mailserver/store/logs/security.log
  318. [courier-auth]
  319. port = smtp,465,submission,imap3,imaps,pop3,pop3s
  320. logpath = %(syslog_mail)s
  321. backend = %(syslog_backend)s
  322. [postfix-sasl]
  323. port = smtp,465,submission,imap3,imaps,pop3,pop3s
  324. logpath = %(postfix_log)s
  325. backend = %(postfix_backend)s
  326. [perdition]
  327. port = imap3,imaps,pop3,pop3s
  328. logpath = %(syslog_mail)s
  329. backend = %(syslog_backend)s
  330. [squirrelmail]
  331. port = smtp,465,submission,imap2,imap3,imaps,pop3,pop3s,http,https,socks
  332. logpath = /var/lib/squirrelmail/prefs/squirrelmail_access_log
  333. [cyrus-imap]
  334. port = imap3,imaps
  335. logpath = %(syslog_mail)s
  336. backend = %(syslog_backend)s
  337. [uwimap-auth]
  338. port = imap3,imaps
  339. logpath = %(syslog_mail)s
  340. backend = %(syslog_backend)s
  341. [named-refused]
  342. port = domain,953
  343. logpath = /var/log/named/security.log
  344. [nsd]
  345. port = 53
  346. action = %(banaction)s[name=%(__name__)s-tcp, port="%(port)s", protocol="tcp", chain="%(chain)s", actname=%(banaction)s-tcp]
  347. %(banaction)s[name=%(__name__)s-udp, port="%(port)s", protocol="udp", chain="%(chain)s", actname=%(banaction)s-udp]
  348. logpath = /var/log/nsd.log
  349. [asterisk]
  350. port = 5060,5061
  351. action = %(banaction)s[name=%(__name__)s-tcp, port="%(port)s", protocol="tcp", chain="%(chain)s", actname=%(banaction)s-tcp]
  352. %(banaction)s[name=%(__name__)s-udp, port="%(port)s", protocol="udp", chain="%(chain)s", actname=%(banaction)s-udp]
  353. %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s"]
  354. logpath = /var/log/asterisk/messages
  355. maxretry = 10
  356. [freeswitch]
  357. port = 5060,5061
  358. action = %(banaction)s[name=%(__name__)s-tcp, port="%(port)s", protocol="tcp", chain="%(chain)s", actname=%(banaction)s-tcp]
  359. %(banaction)s[name=%(__name__)s-udp, port="%(port)s", protocol="udp", chain="%(chain)s", actname=%(banaction)s-udp]
  360. %(mta)s-whois[name=%(__name__)s, dest="%(destemail)s"]
  361. logpath = /var/log/freeswitch.log
  362. maxretry = 10
  363. [mysqld-auth]
  364. port = 3306
  365. logpath = %(mysql_log)s
  366. backend = %(mysql_backend)s
  367. [recidive]
  368. logpath = /var/log/fail2ban.log
  369. banaction = %(banaction_allports)s
  370. bantime = 604800 ; 1 week
  371. findtime = 86400 ; 1 day
  372. [pam-generic]
  373. banaction = %(banaction_allports)s
  374. logpath = %(syslog_authpriv)s
  375. backend = %(syslog_backend)s
  376. [xinetd-fail]
  377. banaction = iptables-multiport-log
  378. logpath = %(syslog_daemon)s
  379. backend = %(syslog_backend)s
  380. maxretry = 2
  381. [stunnel]
  382. logpath = /var/log/stunnel4/stunnel.log
  383. [ejabberd-auth]
  384. port = 5222
  385. logpath = /var/log/ejabberd/ejabberd.log
  386. [counter-strike]
  387. logpath = /opt/cstrike/logs/L[0-9]*.log
  388. # Firewall: http://www.cstrike-planet.com/faq/6
  389. tcpport = 27030,27031,27032,27033,27034,27035,27036,27037,27038,27039
  390. udpport = 1200,27000,27001,27002,27003,27004,27005,27006,27007,27008,27009,27010,27011,27012,27013,27014,27015
  391. action = %(banaction)s[name=%(__name__)s-tcp, port="%(tcpport)s", protocol="tcp", chain="%(chain)s", actname=%(banaction)s-tcp]
  392. %(banaction)s[name=%(__name__)s-udp, port="%(udpport)s", protocol="udp", chain="%(chain)s", actname=%(banaction)s-udp]
  393. [nagios]
  394. logpath = %(syslog_daemon)s ; nrpe.cfg may define a different log_facility
  395. backend = %(syslog_backend)s
  396. maxretry = 1
  397. [directadmin]
  398. logpath = /var/log/directadmin/login.log
  399. port = 2222
  400. [portsentry]
  401. logpath = /var/lib/portsentry/portsentry.history
  402. maxretry = 1
  403. [pass2allow-ftp]
  404. # this pass2allow example allows FTP traffic after successful HTTP authentication
  405. port = ftp,ftp-data,ftps,ftps-data
  406. # knocking_url variable must be overridden to some secret value in filter.d/apache-pass.local
  407. filter = apache-pass
  408. # access log of the website with HTTP auth
  409. logpath = %(apache_access_log)s
  410. blocktype = RETURN
  411. returntype = DROP
  412. bantime = 3600
  413. maxretry = 1
  414. findtime = 1
  415. [murmur]
  416. port = 64738
  417. action = %(banaction)s[name=%(__name__)s-tcp, port="%(port)s", protocol=tcp, chain="%(chain)s", actname=%(banaction)s-tcp]
  418. %(banaction)s[name=%(__name__)s-udp, port="%(port)s", protocol=udp, chain="%(chain)s", actname=%(banaction)s-udp]
  419. logpath = /var/log/mumble-server/mumble-server.log
  420. [screensharingd]
  421. logpath = /var/log/system.log
  422. logencoding = utf-8
  423. [haproxy-http-auth]
  424. logpath = /var/log/haproxy.log' >> /etc/fail2ban/jail.local
  425. [[ -e $HOME/fail2ban ]] && rm $HOME/fail2ban
  426. [[ -d $HOME/fail2ban-0.11.2 ]] && rm -rf $HOME/fail2ban-0.11.2
  427. cd
  428. service fail2ban restart
  429. fi