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

推荐订阅源

V
Vulnerabilities – Threatpost
U
Unit 42
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
F
Full Disclosure
月光博客
月光博客
Engineering at Meta
Engineering at Meta
博客园_首页
The Register - Security
The Register - Security
G
Google Developers Blog
The Cloudflare Blog
博客园 - Franky
K
Kaspersky official blog
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cisco Blogs
Hugging Face - Blog
Hugging Face - Blog
C
Check Point Blog
NISL@THU
NISL@THU
AI
AI
D
DataBreaches.Net
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Stack Overflow Blog
Stack Overflow Blog
Project Zero
Project Zero
The GitHub Blog
The GitHub Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
Vercel News
Vercel News
T
Tor Project blog
P
Privacy International News Feed
D
Docker
I
Intezer
L
LangChain Blog
P
Proofpoint News Feed
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Threatpost
博客园 - 聂微东
AWS News Blog
AWS News Blog
Martin Fowler
Martin Fowler
P
Privacy & Cybersecurity Law Blog
V
V2EX
Last Week in AI
Last Week in AI
C
Cybersecurity and Infrastructure Security Agency CISA
The Hacker News
The Hacker News
T
Tenable Blog
Blog — PlanetScale
Blog — PlanetScale
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
T
Tailwind CSS 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,可以在文章下面评论留言。