fai2ban 14 KB

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