远程密令临时开启ssh端口

linux服务器,我们一般是通过ssh通道远程管理,这就需要我们开启ssh端口,如22。但开启端口有被暴力破解的风险,你会说可以设置复杂的密码或使用证书避免。就算破解不了密码,但openssh也可能会有漏洞,你会说可以更改ssh端口,但还是有可能被扫描出来。还有一种选择,我们可以只允许指定IP访问ssh,通过vpn登录管理服务器,但局限很明显,万一紧急情况vpn登录不上去了怎么办。下面给出一种个人觉得比较满意的解决方案,即使用iptables的recent模块,通过密令临时开启ssh端口。当然,密令需要保管好,防止外泄。
1、iptables规则设定

  1. #指定78字节的icmp数据包(包含IP头部20字节,ICMP头部8字节)通过被加入sshopen列表。
  2. iptables -A INPUT -p icmp --icmp-type 8 -m length --length 78 -m recent --set --name sshopen --rsource -j ACCEPT
  3. #检查sshopen列表是否存在你的来源IP,如果存在,即从第一次使用密令开始15秒钟内开启ssh端口22,超过15秒端口自动关闭,不再允许新连接,已连接的不会断开。
  4. iptables -A INPUT -p tcp --dport 22 --syn -m recent --rcheck --seconds 15 --name sshopen --rsource -j ACCEPT

2、临时开启ssh端口密令

  1. linux下:ping -s 50 host
  2. windows下:ping -l 50 host

3、我目前使用的iptables规则

  1. -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  2. -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT
  3. -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT
  4. -A INPUT -p tcp -m tcp --dport 123 -j ACCEPT
  5. -A INPUT -p icmp -m icmp --icmp-type 8 -m length --length 50 -m recent --set --name sshopen --rsource -j ACCEPT
  6. -A INPUT -p tcp -m tcp --dport 22 --syn -m recent --rcheck --seconds 15 --name sshopen --rsource -j ACCEPT
  7. -A INPUT -i lo -j ACCEPT
  8. -A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  9. -A INPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT
  10. -A OUTPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
  11. -A OUTPUT -o lo -j ACCEPT
  12. -A OUTPUT -p tcp -m tcp --dport 80 -j ACCEPT
  13. -A OUTPUT -p tcp -m tcp --dport 443  -j ACCEPT
  14. -A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
  15. -A OUTPUT -p icmp -m icmp --icmp-type 11 -j ACCEPT

参考:http://blog.onovps.com/archives/iptables-recent.html

标签:SSH 发布于:2019-11-22 19:26:34