V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
snoopygao
V2EX  ›  信息安全

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

  •  
  •   snoopygao · 2019-01-16 09:36:22 +08:00 · 4116 次点击
    这是一个创建于 1917 天前的主题,其中的信息可能已经有所发展或是发生改变。
    之前用过 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
    21 条回复    2019-02-09 13:45:10 +08:00
    TimePPT
        1
    TimePPT  
       2019-01-16 10:03:55 +08:00 via iPhone
    改 SSH 登录端口,然后仅密钥验证,禁密码登录和 Root 登录,指定有限账户名。
    之后对所有不满足设置的登录尝试一律 ban 了就行。
    siglalala
        2
    siglalala  
       2019-01-16 10:06:49 +08:00
    iptables 搭配 ipset 建表 动态管理更好吧。
    des
        3
    des  
       2019-01-16 10:09:35 +08:00 via Android
    改端口禁 icmp,立马安静
    BOYPT
        4
    BOYPT  
       2019-01-16 10:11:55 +08:00
    ipset 了解一下
    ctro15547
        5
    ctro15547  
       2019-01-16 10:24:49 +08:00
    fail2ban 封的时间不够长,试试错一次封 1 个月、1 年
    snoopygao
        6
    snoopygao  
    OP
       2019-01-16 11:21:38 +08:00
    对于远程登录,直接白名单,用 ipset 大马拉小车了
    @siglalala
    @BOYPT
    AntonChen
        7
    AntonChen  
       2019-01-16 11:43:39 +08:00
    可以用 ICMP 敲门的方式比这个好,ICMP 敲门 = 发送特定 ICMP 包 iptables 收到包之后把 IP 加到 SSH 允许列表中。
    xmlf
        8
    xmlf  
       2019-01-16 11:52:17 +08:00 via Android
    @AntonChen 具体怎么实现?
    est
        9
    est  
       2019-01-16 11:57:10 +08:00   ❤️ 1
    @AntonChen 我这边是 tcp 敲门。要在浏览器地址栏输入

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

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

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

    如果有人说需要在公共电脑上登录之类,那就用 google authenticator 之类的做两步验证,足够安全
    titanium98118
        16
    titanium98118  
       2019-01-16 15:30:37 +08:00
    其实用证书认证登录也会被爆吗?
    GordianZ
        18
    GordianZ  
    MOD
       2019-01-16 15:37:50 +08:00
    @swulling 我也用这个,弱密码走天下……
    AntonChen
        19
    AntonChen  
       2019-01-16 16:14:28 +08:00
    @est 我说的 ping 和 端口复用是两个方案,目前在用端口复用
    @gouchaoer 还得改端口,不改端口流量爆破没了
    xuanhh
        20
    xuanhh  
       2019-01-17 09:42:05 +08:00
    你这个先把默认端口改了就能减少 80%甚至更多的尝试。
    tvirus
        21
    tvirus  
       2019-02-09 13:45:10 +08:00
    hosts.allow 和 hosts.deny
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1086 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:48 · PVG 06:48 · LAX 15:48 · JFK 18:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.