在 UFW 中按国家/地区禁止 IP

Vah*_*eni 7 firewall ufw 12.10

我想在我的服务器中屏蔽一个国家,但我不知道我必须在 UFW(或 iptables)中使用哪个规则。例如,来自美国和其他国家/地区的人可以看到该网站,而来自俄罗斯的访问者则不能。有人可以向我解释如何在 UFW 中按国家/地区禁止吗?

我的服务器详情
• Ubuntu 12.10
• Nginx
• phpfpm
• Mysql

Rad*_*anu 3

我发现这个很好的脚本会自动在 Iptables 中按国家/地区阻止 IP:

#!/bin/bash
# Purpose: Block all traffic from RUSSIA (ru) and BELARUS (by). Use ISO code. #
# See url for more info - http://www.cyberciti.biz/faq/?p=3402
# Author: nixCraft <www.cyberciti.biz> under GPL v.2.0+
# -------------------------------------------------------------------------------
ISO="ru by"

### Set PATH ###
IPT=/sbin/iptables
WGET=/usr/bin/wget
EGREP=/bin/egrep

### No editing below ###
SPAMLIST="countrydrop"
ZONEROOT="/root/iptables"
DLROOT="http://www.ipdeny.com/ipblocks/data/countries"

cleanOldRules(){
$IPT -F
$IPT -X
$IPT -t nat -F
$IPT -t nat -X
$IPT -t mangle -F
$IPT -t mangle -X
$IPT -P INPUT ACCEPT
$IPT -P OUTPUT ACCEPT
$IPT -P FORWARD ACCEPT
}

# create a dir
[ ! -d $ZONEROOT ] && /bin/mkdir -p $ZONEROOT

# clean old rules
cleanOldRules

# create a new iptables list
$IPT -N $SPAMLIST

for c  in $ISO
do
    # local zone file
    tDB=$ZONEROOT/$c.zone

    # get fresh zone file
    $WGET -O $tDB $DLROOT/$c.zone

    # country specific log message
    SPAMDROPMSG="$c Country Drop"

    # get 
    BADIPS=$(egrep -v "^#|^$" $tDB)
    for ipblock in $BADIPS
    do
       $IPT -A $SPAMLIST -s $ipblock -j LOG --log-prefix "$SPAMDROPMSG"
       $IPT -A $SPAMLIST -s $ipblock -j DROP
    done
done

# Drop everything 
$IPT -I INPUT -j $SPAMLIST
$IPT -I OUTPUT -j $SPAMLIST
$IPT -I FORWARD -j $SPAMLIST

# call your other iptable script
# /path/to/other/iptables.sh

exit 0
Run Code Online (Sandbox Code Playgroud)

来源和更多信息:Linux Iptables Just Block By Country

  • 您应该看看[此使用的数据](http://www.ipdeny.com/ipblocks/data/countries/)...英国有一个 IP 块,而许多国家没有。它有缺陷,无法使用。 (2认同)