SSH 防止别人爆破方法
方法1. 使用证书密钥登陆,我就是这样做,主要是最简单而且有效,而且使用简单
- 证书登陆的脚本.sh
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| #!/bin/bash mkdir -p ~/.ssh && cd ~/.ssh
authorized_keys(){
cat <<EOF >> authorized_keys ssh-rsa 把你的公钥放这行 root ##################################
EOF }
no_use_passwd(){ sed -i "s/PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config sed -i "s/#PasswordAuthentication.*/PasswordAuthentication no/g" /etc/ssh/sshd_config
sed -i '/Protocol/d' /etc/ssh/sshd_config echo "Protocol 2" >> /etc/ssh/sshd_config
systemctl restart ssh }
authorized_keys no_use_passwd
|
方法2. 改端口 密钥登陆 关闭密码登陆, 操作起来麻烦一些,自己使用也稍微不方便点
方法3. 改端口+用key登陆,常年root用户登录,外加iptable伺候
1 2 3 4
| iptables -I INPUT -p tcp --dport 端口 -m state --state NEW -m recent --name ssh --rcheck --seconds 600 --hitcount 3 -j DROP iptables -I INPUT -p tcp --dport 端口 -m state --state NEW -m recent --name ssh --set -j ACCEPT
|
方法4. ssh爆破10次失败就拉黑
1 2 3 4 5 6 7 8 9 10 11 12
| #! /bin/bash cat /var/log/secure|awk '/Failed/{print $(NF-3)}'|sort|uniq -c|awk '{print $2"="$1;}' > /usr/local/bin/black.txt for i in `cat /usr/local/bin/black.txt` do IP=`echo $i |awk -F= '{print $1}'` NUM=`echo $i|awk -F= '{print $2}'` result=$(cat /etc/hosts.deny | grep $IP) if [[ $NUM -gt 10 ]];then if [[ $result = "" ]];then echo "sshd: $IP" >> /etc/hosts.deny fi fi
|
定时任务:10分钟执行一次,crontab -e
1
| */10 * * * * bash /usr/local/bin/secure_ssh.sh
|
方法5. 很多人使用的 fail2ban
1 2 3 4 5
| apt -y update apt install -y fail2ban cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local sed -i 's/^bantime = 600$/bantime = 3600/g' /etc/fail2ban/jail.local /etc/init.d/fail2ban start
|
方法6. 关门放狗 iptables 法,这个也是不错的辅助方法,可以把跳板机的IP放里面
1 2
| iptables -A INPUT -p tcp -s 0.0.0.0/0 --dport 22 -j DROP iptables -I INPUT -p tcp -s 你的ip --dport 22 -j ACCEPT
|
自己忘了 直接重启小鸡就好了
关门重启就好了,重启自动失效
方法7. 使用云主机的防火墙机制,SSH端口只对本地市级IP网段开放
- 方法是监测自己上网动态IP,然后使用 ipip.net 查询网段,添加到防火墙规则