fai2ban 15 KB

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