sshguard
sshguard 是一個用於保護 SSH 以及其他服務免受暴力破解攻擊的守護進程,類似於 fail2ban。
sshguard 與後者的不同之處在於,它是使用 C 編寫的,使用起來更輕量、簡單,功能更少,但同時也能出色地執行核心功能。
sshguard 不會受到大多數(或任何)日誌分析漏洞的影響,而這些漏洞曾導致同類型工具出現問題。
安裝[編輯 | 編輯原始碼]
設置[編輯 | 編輯原始碼]
sshguard 通過監控 /var/log/auth.log
,syslog-ng 或 systemd 日誌來獲得失敗的登錄嘗試。對於每次失敗的嘗試,違規的主機都會在有限時間內被禁止,無法進行通信。對於違規主機,被禁止的默認時間長度從 120 秒開始,之後每次登錄失敗都會增加 1.5 倍。sshguard 可以被配置為永久禁止一個失敗嘗試次數過多的主機。
臨時和永久禁止都是通過在 iptables 的 "sshguard" 鏈中添加條目完成的,該條目會刪除來自違規主機的所有數據包。隨後該封禁會被記錄到 syslog 並保存到 /var/log/auth.log
,如果使用 systemd 日誌,則會保存到 systemd 日誌中。
您必須配置下列防火牆之一來讓 sshguard 的屏蔽發揮作用。
firewalld[編輯 | 編輯原始碼]
sshguard 可以與 firewalld 一同工作。確保您已安裝、配置並啟用 firewalld。為了讓 sshguard 寫入設置區域,請執行以下命令:
# firewall-cmd --permanent --zone=public --add-rich-rule="rule source ipset=sshguard4 drop"
如果您使用 ipv6,您可以執行相同的命令,但需要把 sshguard4 替換為 sshguard6。完成後,執行以下命令:
# firewall-cmd --reload
您可以通過以下命令確認上述內容:
# firewall-cmd --info-ipset=sshguard4
最後,在 /etc/sshguard.conf 中,找到 BACKEND 行並變更如下:
BACKEND="/usr/lib/sshguard/sshg-fw-firewalld"
UFW[編輯 | 編輯原始碼]
如果安裝並啟用了 UFW,則必須賦予它將 DROP 控制傳遞給 sshguard 的能力。可以通過在 /etc/ufw/before.rules
中包含以下行來實現。這些行應該被插入到迴環設備部分之後。
/etc/ufw/before.rules
# 允許全部迴環 -A ufw-before-input -i lo -j ACCEPT -A ufw-before-output -o lo -j ACCEPT # 將 sshd 的控制權交給 sshguard :sshguard - [0:0] -A ufw-before-input -p tcp --dport 22 -j sshguard
在進行上述變更後需要 重新啟動 ufw。
iptables[編輯 | 編輯原始碼]
必要的設置是在 iptables 上創建一個名為 sshguard
的鏈。sshguard 將會在這個鏈上自動插入規則以丟棄來自壞主機的數據包:
# iptables -N sshguard
接下來添加一條從 INPUT
鏈跳轉到 sshguard
鏈的規則。這條規則必須被添加在任何其他處理 sshguard 保護端口的規則之前。使用下列行來保護 FTP 和 SSH,或查看 [1] 了解更多實例。
# iptables -A INPUT -m multiport -p tcp --destination-ports 21,22 -j sshguard
若要保存這些規則:
# iptables-save > /etc/iptables/iptables.rules
/etc/iptables/ip6tables.rules
.nftables[編輯 | 編輯原始碼]
編輯 /etc/sshguard.conf
並將 BACKEND
的值更改如下:
/etc/sshguard.conf
BACKEND="/usr/lib/sshguard/sshg-fw-nft-sets"
When you start/enable the sshguard.service
, two new tables named sshguard
in the ip
and ip6
address families are added which filter incoming traffic through sshguard's list of IP addresses. The chains in the sshguard
table have a priority of -10 and will be processed before other rules of lower priority. See sshguard-setup(7) and nftables for more information.
用法[編輯 | 編輯原始碼]
systemd[編輯 | 編輯原始碼]
syslog-ng[編輯 | 編輯原始碼]
如果您安裝了 syslog-ng包,您可以直接從命令行啟動 sshguard。
/usr/sbin/sshguard -l /var/log/auth.log -b /var/db/sshguard/blacklist.db
配置文件[編輯 | 編輯原始碼]
Configuration is done in /etc/sshguard.conf
which is required for sshguard to start. A commented example is located at /usr/share/doc/sshguard/sshguard.conf.sample
or can also be found on Bitbucket sshguard.conf.sample.
Blacklisting threshold[編輯 | 編輯原始碼]
By default in the Arch-provided configuration file, offenders become permanently banned once they reach a "danger" level of 120 (or 12 failed logins; see attack dangerousness for more details). This behavior can be modified by prepending a danger level to the blacklist file.
BLACKLIST_FILE=200:/var/db/sshguard/blacklist.db
The 200:
in this example tells sshguard to permanently ban a host after achieving a danger level of 200.
Finally restart sshguard.service
Moderate banning example[編輯 | 編輯原始碼]
A slightly more aggressive banning rule than the default one is proposed here to illustrate various options:
- It monitors sshd and vsftpd via logs from the systemd/日誌
- It blocks attackers after 2 attempts (each having a cost of 10, explaining the
20
value of theTHRESHOLD
parameter) for 180 seconds with subsequent block time longer by a factor of 1.5. Note that this 1.5 multiplicative delay is internal and not controlled in the settings - Attackers are permanently blacklisted after 10 attempts (10 attempts having each a cost of 10, explaining the
100
value in theBLACKLIST_FILE
parameter) - It blocks not only the attacker's IP but all the IPv4 subnet 24 (CIDR notation)
/etc/sshguard.conf
# Full path to backend executable (required, no default) BACKEND="/usr/lib/sshguard/sshg-fw-iptables" # Log reader command (optional, no default) LOGREADER="LANG=C.UTF-8 /usr/bin/journalctl -afb -p info -n1 -t sshd -t vsftpd -o cat" # How many problematic attempts trigger a block THRESHOLD=20 # Blocks last at least 180 seconds BLOCK_TIME=180 # The attackers are remembered for up to 3600 seconds DETECTION_TIME=3600 # Blacklist threshold and file name BLACKLIST_FILE=100:/var/db/sshguard/blacklist.db # IPv6 subnet size to block. Defaults to a single address, CIDR notation. (optional, default to 128) IPV6_SUBNET=64 # IPv4 subnet size to block. Defaults to a single address, CIDR notation. (optional, default to 32) IPV4_SUBNET=24
Aggressive banning[編輯 | 編輯原始碼]
For some users under constant attack, a more aggressive banning policy can be adopted. If you are confident that accidental failed logins are unlikely, you can instruct SSHGuard to permanently ban hosts after a single failed login. Modify the parameters in the configuration file in the following way:
THRESHOLD=10 BLACKLIST_FILE=10:/var/db/sshguard/blacklist.db
最後 重新啟動 sshguard.service
。
另外,為了防止在單個連接期間進行多次身份驗證嘗試,您需要在 /etc/ssh/sshd_config
中定義如下:
MaxAuthTries 1
重新啟動 sshd.service
來使此更改生效。
提示與技巧[編輯 | 編輯原始碼]
解除封禁[編輯 | 編輯原始碼]
如果您封禁了自己,你可以等待自動解封,或者使用 iptables 或 nftables 來解除封禁。
要永久解除您的封禁,您還需要從 /var/db/sshguard/blacklist.db
移除您的 IP 地址。
iptables[編輯 | 編輯原始碼]
首先檢查您的 IP 是否被 sshguard 阻止:
# iptables --list sshguard --line-numbers --numeric
然後使用下列命令來解禁,使用前一個命令中標識的行號:
# iptables --delete sshguard line-number
nftables[編輯 | 編輯原始碼]
從 attackers
中移除您的 IP 地址:
# nft delete element family sshguard attackers { ip_address }
其中 family
可以為 ip
或 ip6
。
日誌記錄[編輯 | 編輯原始碼]
要查看傳遞給 sshguard 的內容,請檢查 /usr/lib/systemd/scripts/sshguard-journalctl
中的腳本和 systemd 服務 sshguard.service
。一個在終端中查看日誌的等效命令:
# journalctl -afb -p info SYSLOG_FACILITY=4 SYSLOG_FACILITY=10