iptables 相关技巧
禁止主机连接
禁止指定 IP
~# iptables -I INPUT -s 10.0.28.15 -j DROP
禁止指定 IP 段
~# iptables -I INPUT -s 10.0.28.15/24 -j DROP
禁止指定 IP 和端口
~# iptables -I INPUT -s 10.0.28.15 -ptcp --dport 22 -j DROP
例如先获取正在攻击主机的 IP 列表:
awk '{if (index($0,"Invalid user ")>0 && index($0," from ")>0) {gsub("^.* from ",""); print $1;}}' /var/log/secure | sort | uniq | xargs -I {} sh -c "grep -Fxq '{}' blklist || echo '{}' >> blklist"
然后批量加入黑名单:
while read T_BLK; do
iptables -I INPUT -s $T_BLK -j DROP
done < blklist