fail2ban封IP之Http

环境介绍:http是一个tomcat 的js程序 ,设置的路径为/opt/tomcat5/logs/localhost_access_log.txt
OS:centos 5.3  fail2ban版本:fail2ban-0.8.2-3.el5.rf.noarch.rpm
官方网站:http://www.fail2ban.org/wiki/index.php/Main_Page

背景:及时发现别人的恶意请求并禁止

步骤:

1.安装fail2ban
这里我走了弯路,下源码包安装报错,大家可以这个网址下载:http://packages.sw.be/fail2ban/

 

  
  
  
  
  1. #rpm -ivh fail2ban-0.8.23.el5.rf.noarch.rpm 

 

2.配置fail2ban的自定义过滤规则
分析/opt/tomcat5/logs/localhost_access_log.txt  日志的恶意请求如下 :

 

  
  
  
  
  1. 192.168.32.41 - - [10/Sep/2010:18:11:27 +0800] "GET 12345678.txt HTTP/1.1" 404 1063  
  2. 192.168.32.41 - - [10/Sep/2010:18:11:27 +0800] "GET 12345678.txt HTTP/1.1" 404 1063  
  3. 192.168.32.41 - - [10/Sep/2010:18:11:29 +0800] "GET 12345678.txt HTTP/1.1" 404 1063  
  4. 192.168.32.41 - - [10/Sep/2010:18:11:29 +0800] "GET 12345678.txt HTTP/1.1" 404 1063  
  5. 192.168.32.41 - - [10/Sep/2010:18:11:29 +0800] "GET 12345678.txt HTTP/1.1" 404 1063  
  6. 192.168.32.41 - - [10/Sep/2010:18:11:29 +0800] "GET 12345678.txt HTTP/1.1" 404 1063  

从攻击行为特征来看, 这是短时间连续导致服务器发送HTTP 404文件未找到错误码, 下面是用于发现上述攻击的fail2ban filter规则,
在/etc/fail2ban/filter.d/目录下建立tomcat.conf文件保存下面的内容:

 

  
  
  
  
  1. [Definition]  
  2. failregex = <HOST> -.*- .*HTTP/1.* 404 .*$  
  3. ignoreregex = 

3.测试fail2ban的过滤规则

 

  
  
  
  
  1. # fail2ban-regex /opt/tomcat5/logs/localhost_access_log.txt /etc/fail2ban/filter.d/tomcat.conf 

结果如下:

 

  
  
  
  
  1. Running tests 
  2. =============  
  3.  
  4. Use regex file : /etc/fail2ban/filter.d/tomcat.conf  
  5. Use log file   : /opt/tomcat5/logs/localhost_access_log.txt  
  6.  
  7.  
  8. Results 
  9. =======  
  10.  
  11. Failregex  
  12. |- Regular expressions:  
  13. |  [1]  -.*- .*HTTP/1.* 404 .*$  
  14. |  
  15. `- Number of matches:  
  16.    [1] 13 match(es)  
  17.  
  18. Ignoreregex  
  19. |- Regular expressions:  
  20. |  
  21. `- Number of matches:  
  22.  
  23. Summary 
  24. =======  
  25.  
  26. Addresses found:  
  27. [1]  
  28.     192.168.32.41 (Fri Sep 10 18:10:59 2010)  
  29.     192.168.32.41 (Fri Sep 10 18:11:27 2010)  
  30.     192.168.32.41 (Fri Sep 10 18:11:27 2010)  
  31.     192.168.32.41 (Fri Sep 10 18:11:29 2010)  
  32.     192.168.32.41 (Fri Sep 10 18:11:29 2010)  
  33.     192.168.32.41 (Fri Sep 10 18:11:29 2010)  
  34.     192.168.32.41 (Fri Sep 10 18:11:29 2010)  
  35.     192.168.32.41 (Fri Sep 10 18:11:30 2010)  
  36.     192.168.32.41 (Fri Sep 10 18:11:30 2010)  
  37.     192.168.32.41 (Fri Sep 10 18:27:44 2010)  
  38.     192.168.32.41 (Fri Sep 10 18:27:47 2010)  
  39.     192.168.32.41 (Fri Sep 10 18:27:50 2010)  
  40.     192.168.32.41 (Fri Sep 10 18:27:53 2010)  
  41.  
  42. Date template hits:  
  43. 0 hit(s): Month Day Hour:Minute:Second  
  44. 0 hit(s): Weekday Month Day Hour:Minute:Second Year  
  45. 0 hit(s): Weekday Month Day Hour:Minute:Second  
  46. 0 hit(s): Year/Month/Day Hour:Minute:Second  
  47. 130 hit(s): Day/Month/Year:Hour:Minute:Second  
  48. 0 hit(s): Year-Month-Day Hour:Minute:Second  
  49. 0 hit(s): Day-Month-Year Hour:Minute:Second[.Millisecond]  
  50. 0 hit(s): TAI64N  
  51. 0 hit(s): Epoch  
  52.  
  53. Success, the total number of match is 13  
  54.  
  55. However, look at the above section 'Running tests' which could contain important  
  56. information. 

4.激活fail2ban的规则
从测试结果可以看出, 恶意攻击节点的IP地址和攻击时间都能够正确发现, 因此可以进一步修改fail2ban的配置文件激活上述规则.
下面是我的/etc/fail2ban/jail.local配置文件内容:

 

  
  
  
  
  1. [tomcat]  
  2. enabled = true 
  3. port = http,https  
  4. filter = tomcat 
  5. action = iptables[name=tomcatport=8080protocol=tcp]   
  6.          sendmail-whois[name=tomcatdest=abc@mail.com]  
  7. maxretry = 2 
  8. logpath = /opt/tomcat5/logs/localhost_access_log.txt  
  9. bantime  = 1800 

5.测试效果
生成2个错误的链接,查看fail2ban日志 如下:

 

  
  
  
  
  1. 2010-09-10 18:33:30,156 fail2ban.actions.action: INFO   Set actionStart = printf %b "Subject: [Fail2Ban] : started  
  2. From: Fail2Ban <> 
  3. To: \n  
  4. Hi,\n  
  5. The jail  has been started successfully.\n  
  6. Regards,\n  
  7. Fail2Ban" | /usr/sbin/sendmail -f    
  8. 2010-09-10 18:33:30,157 fail2ban.actions.action: INFO   Set actionUnban =   
  9. 2010-09-10 18:33:30,158 fail2ban.actions.action: INFO   Set actionCheck =   
  10. 2010-09-10 18:33:31,546 fail2ban.actions: WARNING [tomcat] Ban 192.168.32.41 

 

并查看管理员邮箱 ,已经收到邮件了,内容大概如下:
 

  
  
  
  
  1. Hi,  
  2.  
  3. The IP 192.168.32.41 has just been banned by Fail2Ban after  
  4. 4 attempts against tomcat.  
  5.  
  6.  
  7. Here are more information about 192.168.32.41:  
  8.  
  9. [Querying whois.arin.net]  
  10. [whois.arin.net]  
  11. #  
  12. # Query terms are ambiguous.  The query is assumed to be:  
  13. #     "n 192.168.32.41"  
  14. #  
  15. # Use "?" to get help.  

 

6.写完收工。
上面只是根据我的需求,写的一点东西,其他很功能大家自己可以去研究 。

你可能感兴趣的:(linux,职场,休闲,fail2ban)