내가 받고있는 특정 유형의 악성 트래픽을 모니터링하고 관련 IP 주소를 금지하도록 fail2ban을 구성했습니다.
정규식이 패턴을 적절하게 일치시키고 문제의 IP 주소가 iptables에 추가되고 있습니다.
그러나 Apache 로그를 확인하면 여전히 금지 된 IP 주소에서 히트가 발생합니다. iptables가 전혀 실행되지 않는 것처럼 보입니다.
모든 것이 올바르게 구성되었는지 확인하기 위해 몇 가지 세부 사항을 공유하겠습니다.
먼저 iptables 규칙을 지우고 다시로드하겠습니다.
$ sudo iptables -F
$ cat /etc/iptables.firewall.rules
*filter
# Allow all loopback (lo0) traffic and drop all traffic to 127/8 that doesn't use lo0
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT
# Accept all established inbound connections
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow all outbound traffic - you can modify this to only allow certain traffic
-A OUTPUT -j ACCEPT
# Allow HTTP and HTTPS connections from anywhere (the normal ports for websites and SSL).
-A INPUT -p tcp --dport 80 -j ACCEPT
-A INPUT -p tcp --dport 443 -j ACCEPT
# Allow SSH connections
#
# The -dport number should be the same port number you set in sshd_config
#
-A INPUT -p tcp -m state --state NEW --dport 22 -j ACCEPT
# Allow ping
-A INPUT -p icmp -j ACCEPT
# Log iptables denied calls
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
# Drop all other inbound - default deny unless explicitly allowed policy
-A INPUT -j DROP
-A FORWARD -j DROP
COMMIT
$ sudo iptables-restore < /etc/iptables.firewall.rules
$ sudo iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 REJECT all -- * * 0.0.0.0/0 127.0.0.0/8 reject-with icmp-port-unreachable
14 1432 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
1 60 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
0 0 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables denied: "
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
11 1638 ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
이제 fail2ban 구성은 다음과 같습니다.
$ cat /etc/fail2ban/filter.d/apache-xmlrpc.conf
[Definition]
failregex = .*:80 <HOST> .*POST .*xmlrpc\.php.*
ignoreregex =
$ cat /etc/fail2ban/jail.local
[apache-xmlrpc]
enabled = true
port = http,https
filter = apache-xmlrpc
logpath = /var/log/apache2/other_vhosts_access.log
maxretry = 6
$ fail2ban-regex /var/log/apache2/other_vhosts_access.log /etc/fail2ban/filter.d/apache-xmlrpc.conf
Running tests
=============
Use regex file : /etc/fail2ban/filter.d/apache-xmlrpc.conf
Use log file : /var/log/apache2/other_vhosts_access.log
Results
=======
Failregex
|- Regular expressions:
| [1] .*:80 <HOST> .*POST .*xmlrpc\.php.*
|
`- Number of matches:
[1] 29 match(es)
Ignoreregex
|- Regular expressions:
|
`- Number of matches:
Summary
=======
Addresses found:
[1]
80.82.70.239 (Sat Jul 13 02:41:52 2013)
80.82.70.239 (Sat Jul 13 02:41:53 2013)
80.82.70.239 (Sat Jul 13 02:41:55 2013)
80.82.70.239 (Sat Jul 13 02:41:56 2013)
80.82.70.239 (Sat Jul 13 02:41:57 2013)
80.82.70.239 (Sat Jul 13 02:41:58 2013)
80.82.70.239 (Sat Jul 13 02:41:59 2013)
80.82.70.239 (Sat Jul 13 02:42:00 2013)
80.82.70.239 (Sat Jul 13 02:42:02 2013)
80.82.70.239 (Sat Jul 13 02:42:03 2013)
80.82.70.239 (Sat Jul 13 02:42:04 2013)
80.82.70.239 (Sat Jul 13 02:42:05 2013)
80.82.70.239 (Sat Jul 13 02:42:06 2013)
80.82.70.239 (Sat Jul 13 02:42:07 2013)
80.82.70.239 (Sat Jul 13 02:42:09 2013)
80.82.70.239 (Sat Jul 13 02:42:10 2013)
80.82.70.239 (Sat Jul 13 02:42:11 2013)
80.82.70.239 (Sat Jul 13 02:42:12 2013)
80.82.70.239 (Sat Jul 13 02:42:13 2013)
80.82.70.239 (Sat Jul 13 02:42:15 2013)
80.82.70.239 (Sat Jul 13 02:42:16 2013)
80.82.70.239 (Sat Jul 13 02:42:17 2013)
80.82.70.239 (Sat Jul 13 02:42:18 2013)
80.82.70.239 (Sat Jul 13 02:42:19 2013)
80.82.70.239 (Sat Jul 13 02:42:20 2013)
80.82.70.239 (Sat Jul 13 02:42:22 2013)
80.82.70.239 (Sat Jul 13 02:42:23 2013)
80.82.70.239 (Sat Jul 13 02:42:24 2013)
80.82.70.239 (Sat Jul 13 02:42:25 2013)
Date template hits:
0 hit(s): MONTH Day Hour:Minute:Second
0 hit(s): WEEKDAY MONTH Day Hour:Minute:Second Year
0 hit(s): WEEKDAY MONTH Day Hour:Minute:Second
0 hit(s): Year/Month/Day Hour:Minute:Second
0 hit(s): Day/Month/Year Hour:Minute:Second
0 hit(s): Day/Month/Year Hour:Minute:Second
70 hit(s): Day/MONTH/Year:Hour:Minute:Second
0 hit(s): Month/Day/Year:Hour:Minute:Second
0 hit(s): Year-Month-Day Hour:Minute:Second
0 hit(s): Year.Month.Day Hour:Minute:Second
0 hit(s): Day-MONTH-Year Hour:Minute:Second[.Millisecond]
0 hit(s): Day-Month-Year Hour:Minute:Second
0 hit(s): TAI64N
0 hit(s): Epoch
0 hit(s): ISO 8601
0 hit(s): Hour:Minute:Second
0 hit(s): <Month/Day/Year@Hour:Minute:Second>
Success, the total number of match is 29
However, look at the above section 'Running tests' which could contain important
information.
보시다시피, 필터에 failregex가 설정되어 있고 필터가 활성화되어 있습니다. fail2ban-regex를 사용하면 필터가 모니터링중인 로그 파일에서 일치하는 항목을 찾습니다. (현재 문제가있는 IP 주소에 적극적으로 타격을 받고있어 테스트가 매우 쉬워졌습니다.)
이제 fail2ban을 다시 시작하고 규칙이 적용되는지 확인하십시오.
$ sudo service fail2ban restart
* Restarting authentication failure monitor fail2ban [ OK ]
$ tail /var/log/fail2ban.log -n 50
2013-07-13 02:42:58,014 fail2ban.server : INFO Stopping all jails
2013-07-13 02:42:58,745 fail2ban.jail : INFO Jail 'apache-xmlrpc' stopped
2013-07-13 02:42:59,439 fail2ban.jail : INFO Jail 'ssh' stopped
2013-07-13 02:42:59,440 fail2ban.server : INFO Exiting Fail2ban
2013-07-13 02:43:08,055 fail2ban.server : INFO Changed logging target to /var/log/fail2ban.log for Fail2ban v0.8.6
2013-07-13 02:43:08,057 fail2ban.jail : INFO Creating new jail 'ssh'
2013-07-13 02:43:08,111 fail2ban.jail : INFO Jail 'ssh' uses Gamin
2013-07-13 02:43:08,397 fail2ban.filter : INFO Added logfile = /var/log/auth.log
2013-07-13 02:43:08,404 fail2ban.filter : INFO Set maxRetry = 6
2013-07-13 02:43:08,414 fail2ban.filter : INFO Set findtime = 600
2013-07-13 02:43:08,435 fail2ban.actions: INFO Set banTime = 600
2013-07-13 02:43:09,277 fail2ban.jail : INFO Creating new jail 'apache-xmlrpc'
2013-07-13 02:43:09,277 fail2ban.jail : INFO Jail 'apache-xmlrpc' uses Gamin
2013-07-13 02:43:09,283 fail2ban.filter : INFO Added logfile = /var/log/apache2/other_vhosts_access.log
2013-07-13 02:43:09,286 fail2ban.filter : INFO Set maxRetry = 6
2013-07-13 02:43:09,289 fail2ban.filter : INFO Set findtime = 600
2013-07-13 02:43:09,292 fail2ban.actions: INFO Set banTime = 600
2013-07-13 02:43:09,458 fail2ban.jail : INFO Jail 'ssh' started
2013-07-13 02:43:09,792 fail2ban.jail : INFO Jail 'apache-xmlrpc' started
2013-07-13 02:43:11,361 fail2ban.actions: WARNING [apache-xmlrpc] Ban 80.82.70.239
$ sudo iptables -nvL
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
244 39277 fail2ban-apache-xmlrpc tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 80,443
101 7716 fail2ban-ssh tcp -- * * 0.0.0.0/0 0.0.0.0/0 multiport dports 22
0 0 ACCEPT all -- lo * 0.0.0.0/0 0.0.0.0/0
0 0 REJECT all -- * * 0.0.0.0/0 127.0.0.0/8 reject-with icmp-port-unreachable
3404 582K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
349 20900 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:80
0 0 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:443
12 720 ACCEPT tcp -- * * 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
0 0 ACCEPT icmp -- * * 0.0.0.0/0 0.0.0.0/0
2 80 LOG all -- * * 0.0.0.0/0 0.0.0.0/0 limit: avg 5/min burst 5 LOG flags 0 level 7 prefix "iptables denied: "
2 80 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain FORWARD (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 0.0.0.0/0 0.0.0.0/0
Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
3331 4393K ACCEPT all -- * * 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-apache-xmlrpc (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 80.82.70.239 0.0.0.0/0
244 39277 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
Chain fail2ban-ssh (1 references)
pkts bytes target prot opt in out source destination
0 0 DROP all -- * * 223.4.147.8 0.0.0.0/0
101 7716 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0
fail2ban 로그에 표시된대로 규칙 세트가 올바르게 구성된 것으로 나타납니다. 문제가있는 IP 주소가 바로 잡히고 금지되고 있음을 이미 알 수 있습니다. iptables의 출력은 실제로 삭제되고 있음을 보여줍니다.
그러나 이미 fail2ban-apache-xmlrpc 체인에서 일치하는 해당 IP 주소에 대해 손실 된 패킷이 없음을 관찰하고 있습니다. 물론 아파치 로그를 확인합니다.
$ tail /var/log/apache2/other_vhosts_access.log
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:43:53 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:43:54 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:43:56 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:43:57 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:43:58 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:43:59 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:44:00 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
www.--SNIP--.com:80 80.82.70.239 - - [13/Jul/2013:02:44:02 +0000] "POST /xmlrpc.php HTTP/1.1" 403 474 "-" "-"
아니요, 차단되지 않습니다! 또한 fail2ban 로그에서이를 확인할 수 있습니다.
$ tail /var/log/fail2ban.log
2013-07-13 02:52:30,757 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:52:37,767 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:52:44,783 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:52:51,814 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:52:58,830 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:53:05,842 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:53:11,858 fail2ban.actions: WARNING [apache-xmlrpc] Unban 80.82.70.239
2013-07-13 02:53:12,910 fail2ban.actions: WARNING [apache-xmlrpc] Ban 80.82.70.239
2013-07-13 02:53:20,118 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
2013-07-13 02:53:27,129 fail2ban.actions: WARNING [apache-xmlrpc] 80.82.70.239 already banned
아파치 로그에 계속 다시 나타나므로 fail2ban이 계속 금지하려고합니다!
iptables가 왜이 IP 주소에서 트래픽을 삭제하지 않는지 저의 삶을 솔직히 알 수 없습니다. 규칙 순서는 나에게 맞는 것 같습니다. DROP이 다른 것보다 우선합니다.
나는 사람들이 비슷한 문제를 겪고있는 많은 결과를 Google에 보냈지 만 항상 비표준 포트에있는 SSH 트래픽을 금지하는 문제로 되돌아 오는 것 같습니다. 제 경우에는 표준 http 포트 80에서 IP 주소를 차단하려고합니다.
나는 정말 단순한 것을 간과하고 있기를 바랍니다. Linode에서 Ubuntu 12.04를 실행하는 VPS입니다. 누구든지 아이디어가 있으면 알려주십시오. 많은 감사합니다 ...
편집 : 여기에 출력이 있습니다iptables -S
$ sudo iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-N fail2ban-apache-xmlrpc
-N fail2ban-ssh
-A INPUT -p tcp -m multiport --dports 80,443 -j fail2ban-apache-xmlrpc
-A INPUT -p tcp -m multiport --dports 22 -j fail2ban-ssh
-A INPUT -i lo -j ACCEPT
-A INPUT -d 127.0.0.0/8 -j REJECT --reject-with icmp-port-unreachable
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
-A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
-A INPUT -j DROP
-A FORWARD -j DROP
-A OUTPUT -j ACCEPT
-A fail2ban-apache-xmlrpc -s 80.82.70.239/32 -j DROP
-A fail2ban-apache-xmlrpc -j RETURN
-A fail2ban-ssh -s 223.4.147.8/32 -j DROP
-A fail2ban-ssh -j RETURN
iptables -S
당신 을 위해 출력을 보여주기 위해 내 질문을 편집했습니다 . 그것이 당신에게 더 많은 통찰력을 제공하는지 알려주세요.
So now I restart fail2ban and observe the rules taking effect:
차단 은 블록 에서 발생
iptables -s
다음 형식보다 우리에게 더 도움 이 될 수 있습니다.iptables -L