基于 iptables 设置只有自己的 IP(段)能 ssh 登录 vps

2019-01-16 09:36:22 +08:00
 snoopygao
之前用过 fail2ban,后来发现还是有问题,每天 last 看有大量的尝试,看着就烦,后来研究了一下 iptables,具体如下


新建自己 IP 库 链
iptables -N myip

所有访问 22 端口的先检查 myip 链
iptables -A INPUT -p tcp --dport 22 -j myip
检查后返回来的都扔掉
iptables -A INPUT -p tcp --dport 22 -j DROP


在 myip 链上加入自己的 IP (段),允许通过
iptables -A myip -s 222.222.222.222/32 -j ACCEPT
iptables -A myip -s 111.224.0.0/16 -j ACCEPT
其它 ip 返回去
iptables -A myip -j RETURN


整体效果

Chain INPUT (policy ACCEPT 1566 packets, 551K bytes)
pkts bytes target prot opt in out source destination
1362 136K myip tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
6 328 DROP tcp -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:22
Chain FORWARD (policy ACCEPT 1086 packets, 435K bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 2006 packets, 595K bytes)
pkts bytes target prot opt in out source destination
Chain myip (1 references)
pkts bytes target prot opt in out source destination
676 78901 ACCEPT all -- * * 222.223.222.222/32 0.0.0.0/0
678 56618 ACCEPT all -- * * 111.224.0.0/16 0.0.0.0/0
8 408 RETURN all -- * * 0.0.0.0/0 0.0.0.0/0

保存
iptables-save > /etc/iptables.rules

在网卡中生效
vi /etc/network/interfaces

pre-up iptables-restore < /etc/iptables.rules
4143 次点击
所在节点    信息安全
21 条回复
TimePPT
2019-01-16 10:03:55 +08:00
改 SSH 登录端口,然后仅密钥验证,禁密码登录和 Root 登录,指定有限账户名。
之后对所有不满足设置的登录尝试一律 ban 了就行。
siglalala
2019-01-16 10:06:49 +08:00
iptables 搭配 ipset 建表 动态管理更好吧。
des
2019-01-16 10:09:35 +08:00
改端口禁 icmp,立马安静
BOYPT
2019-01-16 10:11:55 +08:00
ipset 了解一下
ctro15547
2019-01-16 10:24:49 +08:00
fail2ban 封的时间不够长,试试错一次封 1 个月、1 年
snoopygao
2019-01-16 11:21:38 +08:00
对于远程登录,直接白名单,用 ipset 大马拉小车了
@siglalala
@BOYPT
AntonChen
2019-01-16 11:43:39 +08:00
可以用 ICMP 敲门的方式比这个好,ICMP 敲门 = 发送特定 ICMP 包 iptables 收到包之后把 IP 加到 SSH 允许列表中。
xmlf
2019-01-16 11:52:17 +08:00
@AntonChen 具体怎么实现?
est
2019-01-16 11:57:10 +08:00
@AntonChen 我这边是 tcp 敲门。要在浏览器地址栏输入

http://IP:端口 1
http://IP:端口 2
http://IP:端口 3

三个端口才能用 ssh。当然这三个端口是没响应的,但是服务器会识别 SYN 包。
AntonChen
2019-01-16 14:16:09 +08:00
AntonChen
2019-01-16 14:17:50 +08:00
@est 其实端口复用更方便些,反正 nginx 现在也支持了
digimoon
2019-01-16 14:25:28 +08:00
设置 ddns,vps 解析 ddns 后将 ip 加到允许
或者将 ip 放到某个在线网盘 vps 用 api 读
实时性没有敲门的方式好
est
2019-01-16 14:57:53 +08:00
@AntonChen 比如一个新的 iOS 或者 安卓手机,你如何 ping ?

但是浏览器随处都有。这里是端口敲门,如何复用端口?
gouchaoer
2019-01-16 15:14:28 +08:00
你改个复杂点的密码不就完了?脸滚出来的超过 10 位无法爆破,纠结这个干嘛。。。
swulling
2019-01-16 15:29:36 +08:00
密钥就完了,哪那么复杂,多设备就搞多个密钥就完了

如果有人说需要在公共电脑上登录之类,那就用 google authenticator 之类的做两步验证,足够安全
titanium98118
2019-01-16 15:30:37 +08:00
其实用证书认证登录也会被爆吗?
swulling
2019-01-16 15:33:33 +08:00
GordianZ
2019-01-16 15:37:50 +08:00
@swulling 我也用这个,弱密码走天下……
AntonChen
2019-01-16 16:14:28 +08:00
@est 我说的 ping 和 端口复用是两个方案,目前在用端口复用
@gouchaoer 还得改端口,不改端口流量爆破没了
xuanhh
2019-01-17 09:42:05 +08:00
你这个先把默认端口改了就能减少 80%甚至更多的尝试。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/527455

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX