fai2ban.sh 13 KB

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