ssh安全加固

每一台服务器基本都会配置ssh服务,但是如果我们不对ssh进行安全防护,就相当于给入侵者提供了一个入侵途径。所以对ssh进行一些安全配置十分重要。

0x00 前言

ssh服务的配置文件:/etc/ssh/sshd_config

ssh日志文件:/var/log/secure

0x01 更换ssh服务的端口

ssh服务默认是22端口,修改的时候如果系统的SELinux是开启的话是不会成功的,会报以下的端口错误。

因为SELinux开启时ssh不能在另一个端口运行

1).检查允许监听哪些端口sshd

$ semanage port -l | grep ssh
ssh_port_t                     tcp      22

2).关闭SELinux或者添加一条规则

a.关闭SELinux

$ sudo vi /etc/selinux/config
#SELINUX=enforcing #注释掉
#SELINUXTYPE=targeted #注释掉
SELINUX=disabled #增加
#保存退出

执行命令使配置生效

$ sudo setenforce 0

b.添加规则

$ sudo semanage port -a -t ssh_port_t -p tcp 30102

semanage命令不可以,使用命令yum install -y policycoreutils-python安装policycoreutils-python

3).修改端口号(17行)

$ sudo vi /etc/ssh/sshd_config
 17  Port 30102

4).重启ssh服务

$ systemctl restart sshd

5).测试30102是否可用

$ ssh -p 30102 localhost

6).在防火墙把22端口关闭

$ sudo firewall-cmd --remove-port=22/tcp --permanent

–permanent表示永久生效,重启不会丢失配置

7).重新加载配置

$ sudo firewall-cmd --reload

8).查看22端口的开放情况

$ sudo firewall-cmd --query-port=22/tcp
no

9).防火墙开放30102端口

$ sudo firewall-cmd --add-port=30102/tcp --permanent
$ sudo firewall-cmd --reload
$ systemctl restart sshd

10).其他主机即可通过30102端口远程此电脑

#查看开放的所有端口
$ sudo firewall-cmd --zone=public --list-ports

0x02 改变ssh版本

目前存在两种SSH(版本1和版本2)。Red Hat Enterprise Linux下的OpenSSH套件使用SSH版本2,该版本具有增强的密钥交换算法,不易受到版本1中的攻击。修改配置文件第23行。

23 Protocol 2

0x03 禁止root登录

使用其他用户登录,这样既较低了用户权限,同时其他用户的账号攻击者还得猜解,这大大增加了攻击难度。修改配置文件49行

49 PermitRootLogin no

0x04 通过密钥登录

不允许密码登录,利用密钥生成器制作一对密钥——一只公钥和一只私钥。将公钥添加到服务器的某个账户上,然后在客户端利用私钥即可完成认证并登录。可以有效防止SSH暴力破解。如果将公钥复制到其他账户甚至主机,利用私钥也可以登录。或者采用双因子认证

#修改配置文件77行,先允许密码登录,后面的步骤需要远程下载私钥文件
$ sudo vi /etc/ssh/sshd_config
77 PasswordAuthentication yes
#允许密钥登录,54、55行
54 RSAAuthentication yes
55 PubkeyAuthentication yes
#保存退出

1).制作密钥对

密码登录到打算使用密钥登录的账户,建立密钥对

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/admin/.ssh/id_rsa):   回车
Enter passphrase (empty for no passphrase):    输入密钥锁码,或直接按 Enter 留空
Enter same passphrase again:    确认密钥锁码
Your identification has been saved in /home/admin/.ssh/id_rsa.    <== 私钥
Your public key has been saved in /home/admin/.ssh/id_rsa.pub.    <== 公钥
The key fingerprint is:
b7:af:ce:02:0d:d0:8d:64:80:11:13:1f:ed:c9:4f:5e admin@localhost.localdomain
The key's randomart image is:
+--[ RSA 2048]----+
|  ==o=oo         |
|  .o.o+ .        |
|    .+ .         |
|      = . E      |
|       *S..      |
|      . +. .     |
|       .  .      |
|        .. .     |
|         o+..    |
+-----------------+

密钥锁码在使用私钥时必须输入,这样就可以保护私钥不被盗用。当然,也可以留空,实现无密码登录。

在admin用户的家目录中生成了一个 .ssh 的隐藏目录,内含两个密钥文件。id_rsa 为私钥,id_rsa.pub 为公钥

2).在服务器配置公钥

$ cd .ssh
$ cat id_rsa.pub >> authorized_keys
$ chmod 600 authorized_keys
$ chmod 700 ~/.ssh
$systemctl restart sshd

3).下载密钥(id_rsa)文件到本地

4).修改配置文件,不允许密码登录

$ sudo vi /etc/ssh/sshd_config
77 PasswordAuthentication yes
#保存退出
$ systemctl restart sshd

5).使用Xshell远程连接

浏览->文件->选择id_rsa->输入密钥锁码->确认即可连接

0x05 设置黑白名单

通过设置黑白名单可以限制访问,这样可以大大提高安全性

限制用户

1).设置登录用户白名单

/etc/ssh/sshd_config配置文件中设置AllowUsers选项

# 允许testadmin和从192.168.0.1 登录的test帐户通过SSH登录系统。
AllowUsers    testadmin   test@192.168.0.1

2).设置登录用户黑名单

/etc/ssh/sshd_config配置文件中设置DenyUsers选项

#不允许testadmin用户登录系统
DenyUsers testadmin

限制IP

1).设置IP白名单

/etc/hosts.allow中添加IP

$ sudo vi /etc/hosts.allow
#
# hosts.allow   This file contains access rules which are used to
#               allow or deny connections to network services that
#               either use the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
sshd:192.168.0.1:allow
sshd:192.168.0.1/24:allow

在上面文件中添加了两个内容

sshd:192.168.0.1:allow      #允许IP192.168.0.1登录ssh
sshd:192.168.0.1/24:allow   #允许192.168.0.1/24这段IP的用户登录ssh

2).设置IP黑名单

$ sudo vi /etc/hosts.deny

#
# hosts.deny    This file contains access rules which are used to
#               deny connections to network services that either use
#               the tcp_wrappers library or that have been
#               started through a tcp_wrappers-enabled xinetd.
#
#               The rules in this file can also be set up in
#               /etc/hosts.allow with a 'deny' option instead.
#
#               See 'man 5 hosts_options' and 'man 5 hosts_access'
#               for information on rule syntax.
#               See 'man tcpd' for information on tcp_wrappers
sshd:ALL    #拒绝全部的ssh登录

3).hosts.allow 和hosts.deny 两个文件同时设置规则的时候,hosts.allow 文件中的规则优先级高

0x06 设置多长时间没有成功连接上就断线

默认的等待时间为 2 分钟,如果没有单位将以秒作为单位,可用的单位分别为 h,m、s,时间越短越安全。

$ sudo vi /etc/ssh/sshd_config
48 LoginGraceTime 30s
#保存退出
$systemctl restart sshd

0x07 配置Fail2ban、denyhosts等

当因为某些需要不得不使用密码登录时,使用Fail2ban可以缓解ssh服务器的暴力破解,通过配置Fail2ban来自动屏蔽针对SSH,HTTP等各服务的暴力密码猜解或者DDOS攻击等,降低暴力破解对服务器造成的威胁。

-------------本文结束感谢您的阅读-------------

本文标题:ssh安全加固

文章作者:Peithon

发布时间:2018年05月31日 - 15:05

最后更新:2018年10月13日 - 08:10

原始链接:https://peithon.github.io/2018/05/31/ssh-safety/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。