惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

S
Secure Thoughts
罗磊的独立博客
T
The Blog of Author Tim Ferriss
人人都是产品经理
人人都是产品经理
博客园 - 叶小钗
Last Week in AI
Last Week in AI
美团技术团队
Google Online Security Blog
Google Online Security Blog
Application and Cybersecurity Blog
Application and Cybersecurity Blog
D
Docker
G
Google Developers Blog
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
小众软件
小众软件
月光博客
月光博客
L
LINUX DO - 最新话题
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
W
WeLiveSecurity
H
Heimdal Security Blog
Vercel News
Vercel News
SecWiki News
SecWiki News
Forbes - Security
Forbes - Security
Blog — PlanetScale
Blog — PlanetScale
Google DeepMind News
Google DeepMind News
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
TaoSecurity Blog
TaoSecurity Blog
T
Troy Hunt's Blog
A
About on SuperTechFans
C
Check Point Blog
S
Security Affairs
Hacker News - Newest:
Hacker News - Newest: "LLM"
AI
AI
WordPress大学
WordPress大学
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Help Net Security
Help Net Security
博客园_首页
The Last Watchdog
The Last Watchdog
S
SegmentFault 最新的问题
Hugging Face - Blog
Hugging Face - Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
Engineering at Meta
Engineering at Meta
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
I
Intezer
K
Kaspersky official blog
M
MIT News - Artificial intelligence
J
Java Code Geeks
G
GRAHAM CLULEY
P
Palo Alto Networks Blog

Rat's Blog - iptables

Linux VPS一键屏蔽指定国家所有的IP访问 - Rat's Blog Linux VPS使用ipset快速屏蔽指定国家的IP访问 - Rat's Blog iptables封禁BT/PT/SPAM(垃圾邮件)和自定义端口/关键词一键脚本 - Rat's Blog CentOS 7使用iptables开放设置端口 - Rat's Blog 使用iptables封掉所有邮件端口 - Rat's Blog 使用iptables进行端口转发 - Rat's Blog Linux使用iptables封IP或IP段教程 - Rat's Blog iptables之禁ping和ddos向外发包 - Rat's Blog
使用ipset设置防火墙端口白名单,只让指定国家访问 - Rat's Blog
博主: Rat's · 2018-07-25 · via Rat's Blog - iptables

说明:博主很早前发过VPS一键屏蔽指定国家IP的教程,查看:Linux VPS一键屏蔽指定国家所有的IP访问,这对于我们阻止某个国家访问网站和CC攻击还是很有用的,不过鉴于很多人需要白名单设置方法,博主研究了下,发现也可以用ipset来完成,这里就说下,目前测试是没问题的。

方法

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

1、安装ipset

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

#CentOS系统
yum -y install ipset

CentOS 7还需要关闭firewall防火墙:

systemctl stop firewalld.service
systemctl disable firewalld.service

2、清空之前的规则

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

3、创建新规则

#创建一个名为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

4、设置IP段白名单

#放行IP段
iptables -A INPUT -p tcp -m set --match-set cnip src -j ACCEPT
#关掉所有端口
iptables -P INPUT DROP

这时候就只有指定国家的IP能访问服务器了。

如果你在国内,网站不允许被国内人访问,建议别关所有端口,这样你的SSH会上不去,我们可以只关闭80/443端口。

#关闭指定端口,比如80/443
iptables -A INPUT -p tcp --dport 80 -j DROP
iptables -A INPUT -p tcp --dport 443 -j DROP

这时候其他国家的IP是无法访问你服务器的80/443端口,等于无法访问你的网站,其它端口还是可以访问的。

5、删除规则

#将参数里的-A改成-D就是删除规则了,如
iptables -D INPUT -p tcp -m set --match-set cnip src -j ACCEPT
iptables -D INPUT -p tcp --dport 443 -j DROP

说明

设置防火墙后,可能有些服务器重启系统后会清空防火墙规则,导致设置的失效,所以我们设置规则后,需要使用iptables命令保存下,保存命令可能在很多系统中都不通用,这里就不说了,需要各位自行搜索解决了,有耐心的也可以每次重启的时候都重新设置一下防火墙。


版权声明:本文为原创文章,版权归 Rat's Blog 所有,转载请注明出处!

本文链接:https://www.moerats.com/archives/684/

如教程需要更新,或者相关链接出现404,可以在文章下面评论留言。