用 iptables 屏蔽来自某个国家的 IP
2012年2月21日
DDOS经常会让人头痛只有先屏蔽攻击源我们才能有更多的时间来检查VPS的安全.
如何屏蔽来自某个特定国家的 IP 呢?
方法很容易,先到 IPdeny 下载以国家代码编制好的 IP 地址列表,比如下载 cn.zone:
1 |
wget http://www.ipdeny.com/ipblocks/data/countries/cn.zone |
有了国家的所有 IP 地址,要想屏蔽这些 IP 就很容易了,直接写个脚本逐行读取 cn.zone 文件并加入到 iptables 中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
#!/bin/bash # Block traffic from a specific country # written by vpsee.com COUNTRY="cn" IPTABLES=/sbin/iptables EGREP=/bin/egrep if [ "$(id -u)" != "0" ]; then echo "you must be root" 1>&2 exit 1 fi resetrules() { $IPTABLES -F $IPTABLES -t nat -F $IPTABLES -t mangle -F $IPTABLES -X } resetrules for c in $COUNTRY do country_file=$c.zone IPS=$($EGREP -v "^#|^$" $country_file) for ip in $IPS do echo "blocking $ip" $IPTABLES -A INPUT -s $ip -j DROP done done exit 0 |
转载自VPSEE [ http://www.vpsee.com ]
声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: 用 iptables 屏蔽来自某个国家的 IP