目录

防止暴力破解ssh的四种方法

../../images/weixin_public.png

防止暴力破解ssh的四种方法

  • 密码要写的足够的复杂,通常建议将密码写16位,并且无连贯的数字或者字母;当然也可以固定一个时间修改一次密码,推荐是一个月修改一次会稳妥一些

  • 修改ssh的端口号,给对方一些迷惑性,因为远程linux服务器默认端口是22,修改成其他的端口,三位数,四位数的都行,这样能避免大部分的暴力破解的可能性

  • 通常我们远程登录都是使用root用户进行登录的,我们将root用户设置成系统用户,并且不允许root账号直接登录,添加一个普通用户,给它赋予root用户的权限,这样也能极大的避免对方破解成功的可能性。

  • 使用秘钥认证的方式登录,在客户端上生成公钥和私钥,将公钥发送给需要远程的服务端,在输入一次正确的密码之后,后续再次远程,则不需要用到密码登录。

参考实例

1
2
#ssh配置文件
vim /etc/ssh/sshd_config

修改的是第17行的端口信息,这里有个方法,ssh默认端口是22,可以将17行的信息复制一行,在18行进行修改,这里把端口修改成2222

修改完之后一定要记得重启服务

1
systemctl restart sshd

可以使用扫描端口的工具看下

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@localhost ~]# nmap 127.0.0.1

Starting Nmap 6.40 ( http://nmap.org ) at 2023-03-05 11:44 CST
Nmap scan report for VM-12-17-centos (127.0.0.1)
Host is up (0.0000050s latency).
Not shown: 996 closed ports
PORT     STATE SERVICE
21/tcp   open  ftp
2222/tcp open  EtherNet/IP-1
3306/tcp open  mysql
9050/tcp open  tor-socks

端口扫描并没有显示到 ssh的服务信息,若多开放几个端口,就能起到迷惑性,让对方不知道是使用什么端口远程的。

赋予其他用户超级权限

1
vim /etc/passwd

先将root用户设置成系统用户,并且不能进行远程登录

../f9a245c10efe49fb9096acc97f52d473.png 这里直接新增一个用户,再给一个普通用户超级权限(修改UID和GID)

1
2
3
[root@localhost ~]# useradd -s /bin/bash test 
[root@localhost ~]# vim /etc/passwd
[root@localhost ~]# echo "GUANzhu123//" | passwd --stdin test

这里可以测试下修改完后是否具有root用户的权限,可以尝试去打开shadow文件。

1
2
3
4
5
6
7
8
[root@localhost ~]# su - test
上一次登录:日 3月  5 11:41:13 CST 2023:0 上
ABRT has detected 1 problem(s). For more info run: abrt-cli list
[root@localhost ~]# pwd
/home/test
[root@localhost ~]# tail -2 /etc/shadow
mysql:!!:19420::::::
test:!!:19421:0:99999:7:::

小提示:千万千万要记得给要赋予超级权限的普通用户设置一个登录密码,让它可以远程登录,否则会出现上不去服务器的情况。

使用秘钥认证

先在本地使用ssh-keygen命令生成公钥和私钥文件,-t表示选择秘钥的类型,-b表示指定长度,这里选择长度是4096。

 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
[root@localhost ~]# ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:Mq/1yY0jDdMsw9DNQIUgS0mDKXxkPpxSXXg24inkl44 root@localhost.localdomain
The key's randomart image is:
+---[RSA 4096]----+
|. .B*.++.o.      |
|..B+oB +o        |
| o+=o *..+       |
|  .o.=. . o      |
|    = ooSo       |
|   E . +* o      |
|        o*       |
|       o.oo+     |
|      .  .=..    |
+----[SHA256]-----+
[root@localhost ~]# ll /root/.ssh/
总用量 12
-rw-------. 1 root root 3243 3月   5 12:17 id_rsa
-rw-r--r--. 1 root root  752 3月   5 12:17 id_rsa.pub

../39d92c97774644f4b88a22aef1987eb2.png

这里有几个信息分别是: 输入保存秘钥的文件,默认是放在/root/.ssh_rsa,如果不指定其他位置,直接按回车即可。 创建目录,默认是/root/.ssh这个目录,不指定的话也是直接按enter。 输入秘钥的密码,这里默认也不设置,按回车,毕竟都使用秘钥了,就不想每次远程服务器又要输入一次密码了。 再次输入相同的密码,这里也直接按回车吧 您的身份信息保证在/root/.ssh/id_rsa中 公钥已保存在/root/.ssh/id_rsa.pub中 秘钥的指纹为: RSA 4096那张图就是这个秘钥的指纹了,可以看出来是很复杂的。

以上都是在客户端上设置的,下一步是将公钥发送到服务器上面 这里又要使用一个命令ssh-copy-id

1
2
#ip填写要远程服务器的IP
[root@localhost ~]# ssh-copy-id root@192.168.196.23 

../66140042471446cc9aff283bccc3338c.png

公钥发送过去后,就可以直接使用ssh远程登录;

1
2
[root@localhost ~]# ssh 192.168.196.23
Last login: Sun Mar  5 12:24:28 2023 from 192.168.196.166

无须输入密码即可远程登录,这个就是秘钥登录的优势了。

使用Fail2ban软件

fail2ban是一款安全保护工具,触发限制后会创建防火墙规则封锁IP,诸如对ssh暴力破解、ftp/http密码穷举等场景提供强有力的保护。 1.这里会使用到两台虚拟机做测试分别是 192.168.196.166、192.168.196.23 2.需要用到iptables和ssh服务

1
2
3
4
使用Fail2ban软件
fail2ban是一款安全保护工具,触发限制后会创建防火墙规则封锁IP,诸如对ssh暴力破解、ftp/http密码穷举等场景提供强有力的保护。
1.这里会使用到两台虚拟机做测试分别是 192.168.196.166、192.168.196.23
2.需要用到iptables和ssh服务
1
2
yum install epel-release -y
yum install fail2ban -y

修改配置文件(服务端进行)

配置文件的位置在:

1
/etc/fail2ban/jail.conf

这里填写如下信息

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
[root@localhost fail2ban]# vim jail.conf
[root@localhost fail2ban]# tail -10 jail.conf 
[ssh-iptables]    #用到的服务 
enabled = true     #开机自动启用服务
filter = sshd      #添加动作是sshd
action = iptables[name=SSH,port=ssh,protocol=tcp] 
logpath = /var/log/secure     #要监控的站点日志文件
#这三个代表的是 将5分钟内频繁访问失败3次的IP屏蔽3600秒
maxretry = 3            #设定失败次数       
findtime = 300 			#一定时间内			
bantime = 3600    		#屏蔽多长时间

重启服务

1
systemctl restart fail2ban.service systemctl enable fail2ban.service

以上的都是在服务端进行的

测试远程登录(客户端)

这里测试登录失败次数超过三次后会提示什么

1
2
3
4
5
6
7
[root@node1 ~]# ssh 192.168.196.166 
root@192.168.196.166's password: Permission denied, please try again. 
root@192.168.196.166's password: Permission denied, please try again. 
root@192.168.196.166's password: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password). 
[root@node1 ~]# ssh 192.168.196.166 ssh: connect to host 192.168.196.166 port 22: Connection refused 
[root@node1 ~]# 
[root@node1 ~]# ssh 192.168.196.166 ssh: connect to host 192.168.196.166 port 22: Connection refused [root@node1 ~]# ssh 192.168.196.166 ssh: connect to host 192.168.196.166 port 22: Connection refused 

测试过后显示连接失败,以上配置是已经成功了

查看有哪些IP被拉入黑名单(服务端)

 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
1 [root@localhost fail2ban]# iptables -L -n | tail
2 Chain IN_public_log (1 references)
3 target prot opt source destination 
4
5 Chain OUTPUT_direct (1 references)
6 target prot opt source destination 
7
8 Chain f2b-SSH (1 references)
9 target prot opt source destination 
REJECT all -- 192.168.196.23 0.0.0.0/0 reject-with icmp-portunreachable
10
11 RETURN all -- 0.0.0.0/0 0.0.0.0/0 
12
13 [root@localhost fail2ban]# fail2ban-client status 
14 Status
|- Number of jail: 1 15
`- Jail list: ssh-iptables 16
17 [root@localhost fail2ban]# fail2ban-client status ssh-iptables
18 Status for the jail: ssh-iptables
19 |- Filter
20 | |- Currently failed: 0
| |- Total failed: 6 21
| `- File list: /var/log/secure 22
23 `- Actions
24 |- Currently banned: 1
 |- Total banned: 2 25
 `- Banned IP list: 192.168.196.23

如果不慎是其他认识的人登录失败了,可以使用以下方式将他移除黑名单

1
[root@localhost fail2ban]# fail2ban-client set ssh-iptables unbanip 192.168.196.23