$ sudo fail2ban-client status sshd Status for the jail: sshd |- Filter ||- Currently failed: 8 ||- Total failed: 4399 | `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd `- Actions |- Currently banned: 101 |- Total banned: 684 `- Banned IP list: ...
从功能上看,主动式方法它与被动式方法没有太大区别,然而,来自有些国家的入侵企图是非常普遍的。如果你的系统既不放在这些国家里,也没有任何源自这些国家的客户,那么为什么不现在就把它们加入黑名单而是等待呢?(LCTT 译注:我的经验是,动辄以国家的范畴而列入黑名单有些过于武断。建议可以将该 IP 所属的 WHOIS 网段放入到黑名单,因为这些网段往往具有相同的使用性质,如都用于用户接入或 IDC 托管,其安全状况也大致相同,因此,如果有来自该网段的某个 IP 的恶意尝试,可以预期该网段内的其它 IP 也可能被利用来做这样的尝试。)
#!/bin/bash # Based on the below article # https://www.linode.com/community/questions/11143/top-tip-firewalld-and-ipset-country-blacklist
# Source the blacklisted countries from the configuration file . /etc/blacklist-by-country
# Create a temporary working directory ipdeny_tmp_dir=$(mktemp -d -t blacklist-XXXXXXXXXX) pushd $ipdeny_tmp_dir
# Download the latest network addresses by country file curl -LO http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz tar xf all-zones.tar.gz
# For updates, remove the ipset blacklist and recreate if firewall-cmd -q --zone=drop--query-source=ipset:blacklist; then firewall-cmd -q --permanent--delete-ipset=blacklist fi
# Create the ipset blacklist which accepts both IP addresses and networks firewall-cmd -q --permanent--new-ipset=blacklist--type=hash:net \ --option=family=inet--option=hashsize=4096--option=maxelem=200000 \ --set-description="An ipset list of networks or ips to be dropped."
# Add the address ranges by country per ipdeny.com to the blacklist for country in $countries; do firewall-cmd -q --permanent--ipset=blacklist \ --add-entries-from-file=./$country.zone && \ echo"Added $country to blacklist ipset." done
# Block individual IPs if the configuration file exists and is not empty if [ -s "/etc/blacklist-by-ip" ]; then echo"Adding IPs blacklists." firewall-cmd -q --permanent--ipset=blacklist \ --add-entries-from-file=/etc/blacklist-by-ip && \ echo"Added IPs to blacklist ipset." fi
# Add the blacklist ipset to the drop zone if not already setup if firewall-cmd -q --zone=drop--query-source=ipset:blacklist; then echo"Blacklist already in firewalld drop zone." else echo"Adding ipset blacklist to firewalld drop zone." firewall-cmd --permanent--zone=drop--add-source=ipset:blacklist fi
# ls | shuf -n 10 | sed "s/\.zone//g" | tr'\n'' ' nl ee ie pk issv na om gp bn
现在只要在配置文件中加入至少一个国家,就可以运行了!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
$ sudo firewalld-blacklist % Total % Received % XferdAverageSpeedTimeTimeTimeCurrent DloadUploadTotalSpentLeftSpeed 1001421001420010140 --:--:-- --:--:-- --:--:--1014 100662k 100662k 00989k 0 --:--:-- --:--:-- --:--:--989k Added nl to blacklist ipset. Added ee to blacklist ipset. Added ie to blacklist ipset. Added pk to blacklist ipset. Added is to blacklist ipset. Added sv to blacklist ipset. Added na to blacklist ipset. Added om to blacklist ipset. Added gp to blacklist ipset. Added bn to blacklist ipset. Adding ipset blacklist to firewalld drop zone. success
要验证 FirewallD 黑名单是否成功,请检查 drop 区和 blacklist ipset。