3

使用IPSET封掉某个国家整个的访问

 2 years ago
source link: https://zhangrr.github.io/posts/20211018-ipset_block_cn/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

现在国内都禁止挖币,什么币安、火币、cnspark之类的都不允许国内 IP 访问了。

如何实现的呢?

首先需要得到国家IP段,下载地址:http://www.ipdeny.com/ipblocks/。这里以我们国家 cn 为例

步骤如下:

一、安装ipset

#Debian/Ubuntu系统
apt-get -y install ipset

#CentOS系统
yum -y install ipset
shell

二、清空iptable规则

#防止设置不生效,清空之前的防火墙规则
iptables -P INPUT ACCEPT
iptables -F
shell

三、创建ipset规则集

#创建一个名为cnip的规则
ipset -N cnip hash:net

#下载国家IP段,这里以中国为例
wget -P . http://www.ipdeny.com/ipblocks/data/countries/cn.zone

#将IP段添加到cnip规则集中
for i in $(cat /root/cn.zone ); do ipset -A cnip $i; done
shell

四、创建iptable黑名单

#扔黑名单
iptables -A INPUT -p tcp --dport 80 -m set --match-set cnip src -j DROP
iptables -A INPUT -p tcp --dport 443 -m set --match-set cnip src -j DROP

#然后放行其他的
iptables -P INPUT ACCEPT
shell

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK