使用 fail2ban 和 FirewallD 黑名单保护你的系统( 二 )


$ sudo fail2ban-client statusStatus|- Number of jail:      1`- Jail list:   sshdsshd “监狱”的上级状态也会显示出来 。如果启用了多个“监狱”,它们会在这里显示出来 。
要查看一个“监狱”的详细状态,只需在前面的命令中添加“监狱”名称 。下面是我的系统的输出,它已经运行了一段时间 。我已经从输出中删除了被禁止的 IP:
$ sudo fail2ban-client status sshdStatus 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:   ...监控 fail2ban 日志文件是否有入侵尝试,可以通过“尾随”日志来实现:
$ sudo tail -f /var/log/fail2ban.logtail 是一个很好的命令行工具,默认情况下,它可以显示一个文件的最后 10 行 。添加 -f 告诉它尾随文件,这是个观察一个仍在被写入的文件的很好方式 。
由于输出的内容中有真实的 IP,所以这里不会提供样本,但它的可读性很高 。INFO 行通常是登录的尝试 。如果从一个特定的 IP 地址进行了足够多的尝试,你会看到一个 NOTICE 行显示一个 IP 地址被禁止 。在达到禁止时间后,你会看到一个 NOTICE 解禁行 。
注意几个警告行 。最常见的情况是,当添加了一个禁止后,fail2ban 发现该 IP 地址已经在其禁止数据库中,这意味着禁止可能无法正常工作 。如果是最近安装的 fail2ban 包,它应该被设置为 FirewallD 的富规则 。这个包在 fail2ban-0.11.1-6 版本时从 ipset 方式切换到了富规则方式,所以如果你的 fail2ban 安装时间较早,它可能还在尝试使用 ipset 方式,这种方式使用的是传统的 iptables,不是很可靠 。
FirewallD 配置 被动还是主动?
有两种策略可以分开或一起使用:被动地将单个 IP 地址或主动地根据来源国将子网永久列入黑名单 。
对于被动方式,一旦 fail2ban 运行了一段时间,最好再运行 sudo fail2ban-client status sshd 来看看有哪些坏蛋 。很可能会有很多被禁止的 IP 地址 。选择一个,然后试着对它运行 whois 。在输出结果中可能会有很多有趣的信息,但是对于这个方法来说,只有来源国是重要的 。为了保持简单,让我们过滤掉除了国家以外的所有信息 。
在这个例子中,我们将使用一些著名的域名:
$ whois google.com | grep -i countryRegistrant Country: USAdmin Country: USTech Country: US$ whois rpmfusion.org | grep -i countryRegistrant Country: FR$ whois aliexpress.com | grep -i countryRegistrant Country: CN使用 grep -i 的原因是为了使 grep 不区分大小写,而大多数条目都使用的是 “Country”,而有些条目则是全小写的 “country”,所以这种方法无论如何都能匹配 。
现在知道了尝试入侵的来源国,问题是,“是否有来自这个国家的人有合法的理由连接到这台计算机?”如果答案是否定的,那么封锁整个国家应该是可以接受的 。
从功能上看,主动式方法它与被动式方法没有太大区别,然而,来自有些国家的入侵企图是非常普遍的 。如果你的系统既不放在这些国家里,也没有任何源自这些国家的客户,那么为什么不现在就把它们加入黑名单而是等待呢?(LCTT 译注:我的经验是,动辄以国家的范畴而列入黑名单有些过于武断 。建议可以将该 IP 所属的 WHOIS 网段放入到黑名单,因为这些网段往往具有相同的使用性质,如都用于用户接入或 IDC 托管,其安全状况也大致相同,因此,如果有来自该网段的某个 IP 的恶意尝试,可以预期该网段内的其它 IP 也可能被利用来做这样的尝试 。)
黑名单脚本和配置
那么如何做到这一点呢?用 FirewallD ipset 。我开发了下面的脚本来尽可能地自动化这个过程:
#!/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 directoryipdeny_tmp_dir=$(mktemp -d -t blacklist-XXXXXXXXXX)pushd $ipdeny_tmp_dir# Download the latest network addresses by country filecurl -LO http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gztar xf all-zones.tar.gz# For updates, remove the ipset blacklist and recreateif firewall-cmd -q --zone=drop --query-source=ipset:blacklist; then    firewall-cmd -q --permanent --delete-ipset=blacklistfi# Create the ipset blacklist which accepts both IP addresses and networksfirewall-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 blacklistfor 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 emptyif [ -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 setupif 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:blacklistfifirewall-cmd -q --reloadpopdrm -rf $ipdeny_tmp_dir


推荐阅读