Iptables-Fail2ban bind 불법 공격 처리

14984 단어 bindddos불법 공격
아침에 DNS 데이터가 이상한 것을 발견하고query를 찾아봤어요.로그 로그는 다음과 같습니다.
9-Apr-2013 13:49:33.418 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74)
09-Apr-2013 13:49:33.475 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.487 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.516 queries: info: client 70.39.93.72#49940: view other_user: query: isc.org IN ANY +ED (163.177.24.74) 09-Apr-2013 13:49:33.557 queries: info: client 185.12.119.16#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.588 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.657 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.663 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.758 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.802 queries: info: client 70.39.93.72#49940: view other_user: query: isc.org IN ANY +ED (163.177.24.74) 09-Apr-2013 13:49:33.824 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74) 09-Apr-2013 13:49:33.848 queries: info: client 199.19.213.88#25345: view other_user: query: isc.org IN ANY +ED (183.60.126.74)
처리 방법:
iptables 규칙 추가(connlimit 모듈 추가), 단일 IP 동시 요청 수 제한:

  
  
  
  
  1. -A INPUT -p tcp -m tcp --dport 53 --tcp-flags SYN,RST,ACK SYN -m limit --limit 20/sec --limit-burst 200 -j DROP  
  2. -A INPUT -p udp -m udp --dport 53 -m state --state NEW,RELATED,ESTABLISHED -m connlimit --connlimit-above 5 --connlimit-mask 32 -j DROP  
  3. -A INPUT -p tcp -m tcp --dport 53 -m state --state NEW,RELATED,ESTABLISHED -m connlimit --connlimit-above 5 --connlimit-mask 32 -j DROP  

1. 우선bind 자체의 안전을 검사하고bind를 이용하여 acl을 추가하며 귀속 조회를 제한하고 블랙리스트를 추가한다.

  
  
  
  
  1. acl "mynetwork" { 
  2. 183.61.81.0/25
  3. 119.38.123.0/25
  4. 180.60.116.0/25
  5. 163.172.24.0/25
  6. 127.0.0.1/32
  7. }; 
  8. acl "blackhats" { 
  9. 108.162.204.88
  10. 184.168.72.113
  11. 93.170.127.96
  12. 72.46.155.84
  13. 178.33.217.2
  14. 72.46.132.122
  15. 78.159.111.197
  16. 192.96.200.41
  17. }; 


전역 설정 변경:
allow-query { mynetwork; }; #여기에자신의네트워크를설치한다.zone에는any로 설정할 수 있다.
allow-recursion { mynetwork; }; #귀속 처리 #allow-query-cache {mynetwork;};# 쿼리 캐시 처리
        version                     "hello babay"; #숨겨진 버전
        edns-udp-size 1024; #udp 메시지 크기, 단위 바이트 설정
        max-udp-size 4096; #최대udp 메시지 크기, 단위 바이트 설정
    blackhole {blackhats;};#블랙리스트 설정
2. fail2ban 필터 로그를 이용하여 iptables를 호출하여 지정한 ip주소를 제한한다.
fail2ban 다운로드:https://github.com/fail2ban
또는http://sourceforge.net/projects/fail2ban/
저는 fail2ban-0.8.4를 사용해서 가방을 풀고...
python setup을 실행합니다.py 설치가 완료되었습니다.
/etc/fail2ban/jail.conf 관련 서비스 프로필 필터링
/etc/fail2ban/fail2ban.conf 주 프로필, sock 및 로그 파일 설정
/etc/fail2ban/filter.d/해당 서비스의 필터 규칙 파일 경로
/etc/fail2ban/action.d/해당 서비스에 대한 작업 구성 파일 경로
jail.conf 지정된 서비스 매개 변수를 추가합니다.

  
  
  
  
  1. [named-refused-udp] 
  2. enabled  = true 
  3. filter   = named-refused  #
  4. action   = iptables-multiport[name=Named, port=53, protocol=udp] 
  5.            sendmail-whois[name=Named, [email protected]
  6. logpath  = /opt/soft/bind/log/query.log  #
  7. bantime  = 3600 # , ,
  8. findtime  = 100  #100 ,
  9. maxretry = 3  #
  10. ignoreip = 127.0.0.1 
  11.  
  12. [named-refused-tcp] 
  13. enabled  = true 
  14. filter   = named-refused 
  15. action   = iptables-multiport[name=Named, port=53, protocol=tcp] 
  16.            sendmail-whois[name=Named, [email protected]
  17. logpath  = /opt/soft/bind/log/query.log 
  18. bantime  = 360000 
  19. findtime  = 100 
  20. maxretry = 3 
  21. ignoreip = 127.0.0.1 

필터 규칙 추가:named-refused.conf

  
  
  
  
  1. # Fail2Ban configuration file for named (bind9). Trying to generalize the 
  2. #          structure which is general to capture general patterns in log 
  3. #          lines to cover different configurations/distributions 
  4. # 
  5. # $Revision: 730 $ 
  6. # 
  7.   
  8. [Definition] 
  9.   
  10. # 
  11. # Daemon name 
  12. _daemon=named 
  13.   
  14. # 
  15. # Shortcuts for easier comprehension of the failregex 
  16. __pid_re=(?:\[\d+\]) 
  17. __daemon_re=\(?%(_daemon)s(?:\(\S+\))?\)?:? 
  18. __daemon_combs_re=(?:%(__pid_re)s?:\s+%(__daemon_re)s|%(__daemon_re)s%(__pid_re)s?:) 
  19. #       hostname       daemon_id         spaces 
  20. # this can be optional (for instance if we match named native log files) 
  21. __line_prefix=(?:\s\S+ %(__daemon_combs_re)s\s+)? 
  22.   
  23. # Option: failregex 
  24. # Notes.: regex to match the password failures messages in the logfile. 
  25. # Values: TEXT 
  26. # 
  27. failregex = %(__line_prefix)sclient <HOST>#.+: query: (baidu.com|isc.org) IN ANY \+ED* 
  28.   
  29. # Option:  ignoreregex 
  30. # Notes.:  regex to ignore. If this regex matches, the line is ignored. 
  31. # Values:  TEXT 
  32. # 
  33. ignoreregex = 


실행 동작 파일 #egrep -v'^#|^$'/etc/fail2ban/action.d/iptables-multiport.conf 

  
  
  
  
  1. [Definition] 
  2. actionstart = iptables -N fail2ban-<name> 
  3.               iptables -A fail2ban-<name> -j RETURN 
  4.               iptables -I INPUT -p <protocol> -m multiport --dports <port> -j fail2ban-<name> 
  5. actionstop = iptables -D INPUT -p <protocol> -m multiport --dports <port> -j fail2ban-<name> 
  6.              iptables -F fail2ban-<name> 
  7.              iptables -X fail2ban-<name> 
  8. actioncheck = iptables -n -L INPUT | grep -q fail2ban-<name> 
  9. actionban = iptables -I fail2ban-<name> 1 -s <ip> -j DROP 
  10. actionunban = iptables -D fail2ban-<name> -s <ip> -j DROP 
  11. [Init] 
  12. name = default 
  13. port = ssh 
  14. protocol = tcp 

fail2ban-client start 또는fail2ban-server 시작 서비스
효과를 봅시다. 이미 차단된 IP가 있습니다.
#fail2ban-client  status named-ddos-tcp Status for the jail: named-ddos-tcp |- filter |  |- File list:/opt/soft/bind/log/query.log | | - Currently failed: 5 |'- Total failed: 299'- action | - Currently banned: 5 |'- IP list: 107.20.206.69 94.75.243.137 61.147.112.29 178.32 61.147.112.32 (외국의 IP는 이미 차단)'- Total banned: 15
Chain OUTPUT(policy ACCEPT 163M packets, 203G bytes) Pkts bytes target prot opt opt in out source destination Iptables에 Chain fail2ban-Named(2 references)가 자동으로 추가되었습니다.  pkts bytes target     prot opt in     out     source               destination              9   576 DROP       all  --  *      *       174.142.207.122      0.0.0.0/0              241 15424 DROP       all  --  *      *       61.147.120.25        0.0.0.0/0               27  1728 DROP       all  --  *      *       61.147.112.32        0.0.0.0/0              115  7360 DROP       all  --  *      *       178.32.244.170       0.0.0.0/0              119  7616 DROP       all  --  *      *       61.147.112.29        0.0.0.0/0               51  3264 DROP       all  --  *      *       94.75.243.137        0.0.0.0/0             2206  141K DROP       all  --  *      *       107.20.206.69        0.0.0.0/0            12829  833K RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0                0     0 RETURN     all  --  *      *       0.0.0.0/0            0.0.0.0/0    
자료 주소:
http://ftp.isc.org/isc/bind9/cur/9.8/doc/arm/Bv9ARM.html
http://www.isc.org/software/bind/documentation
http://www.minihowto.eu/protectio-against-isc-org-any-attack-dns-attack-isc-org-any-query
http://www.bergercity.de/tag/bind/
http://sourceforge.net/projects/fail2ban/
http://www.fail2ban.org/wiki/index.php/HOWTO_fail2ban_0.7.x#Iptables_action_setup
 

좋은 웹페이지 즐겨찾기