SSHD服务搭建管理和防止暴力破解

一、学习环境的搭建-搭建centos7实验环境
1、清空防火墙的规则

[root@xuegod ~]# iptables -F #清空防火墙规则

2、关闭防火墙

[root@xuegod ~]# systemctl stop firewalld #关闭防火墙
[root@xuegod ~]# systemctl disable firewalld #设置开机不启动

3、关闭selinux

[root@xuegod ~]# getenforce #查看状态
Disabled
[root@xuegod ~]# setenforce 0 #临时关闭selinux
[root@xuegod ~]# cat /etc/selinux/config | grep -v “#|$”
SELINUX=disabled #修改为disabled就是永久关闭selinux
SELINUXTYPE=targeted

注释:如果生产环境中,无法重启服务,但是想永久关闭selinux,可以先临时关闭后再修改配置文件永久关闭,达到永久关闭的效果。

4、网卡配置文件
网卡配置文件的路径:/etc/sysconfig/network-scripts/ifcfg-en33
主要修改内容:
BOOTPROTO=“static” #IP地址的类型,一般分为静态(static)、动态获取(DHCP)、无(none)
ONBOOT=“yes” #网卡是否开机启动,如果设置为NO,系统开机不启动网卡
IPADDR=“x.x.x.x” #IP地址
GATEWAY=“x.x.x.x” #网关IP地址
NETMASK=“255.255.255.0” #子网掩码地址
DNS1=“x.x.x.x” #DNS地址,Centos7中一定要写DNS1才行

5、关闭NetworkManager

[root@xuegod ~]# systemctl stop NetworkManager
[root@xuegod ~]# systemctl disable NetworkManager

6、配置主机和IP映射关系

[root@xuegod200 ~]# cat /etc/hosts
192.168.11.200 xuegod200 xuegod200.cn
192.168.11.210 xuegod210 xuegod210.cn

7、修改主机名

[root@xuegod ~]# hostname xuegod200 #临时生效,重启后自动取消
[root@xuegod200 ~]# cat /etc/hostname #修改/etc/hostname配置文件,重启系统生效
xuegod200

8、配置网络yum源

[root@xuegod200 yum.repos.d]# wget -O /etc/yum.repos.d/Centos-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

9、安装epel源(扩展源)

[root@xuegod200 yum.repos.d]# yum -y install epel-release

10、系统必须最小化安装,设置开机init 3启动

[root@xuegod200 ~]# systemctl set-default runlevel3.target

二、sshd服务安装-ssh命令使用方法
1、安装sshd服务

[root@xuegod210 ~]# yum install openssh openssh-clients openssh-server -y

包含三个软件包
openssh #包含openssh服务器及客户端需要的核心文件
openssh-clients #openssh客户端软件包
openssh-server #openssh服务器软件包
openssh-askpass #支持对话框窗口的显示,是一个基于X系统的密码诊断工具

2、确认是否安装

[root@xuegod210 ~]# rpm -qa | grep openssh
openssh-7.4p1-16.el7.x86_64
openssh-server-7.4p1-16.el7.x86_64
openssh-clients-7.4p1-16.el7.x86_64

3、查看软件包的安装路径

[root@xuegod210 ~]# rpm -ql openssh
/etc/ssh
/etc/ssh/moduli

4、ssh服务安装路径下的文件说明

[root@xuegod210 ~]# ll /etc/ssh/
总用量 604
-rw-r–r--. 1 root root 2276 4月 11 2018 ssh_config #客户端配置文件
-rw-------. 1 root root 3907 4月 11 2018 sshd_config #服务端配置文件

5、重启sshd服务

[root@xuegod210 ~]# systemctl start sshd #开启服务
[root@xuegod210 ~]# systemctl enable sshd #开机自启动
[root@xuegod210 ~]# systemctl status sshd #查看服务状态

6、ssh使用方法
1)ssh 192.168.11.210
不加用户名,默认使用当前登录系统的用户
2)ssh root@192.168.11.210
直接增加用户名,指定登录用户进行登录,不一定必须写root用户
3)ssh -l root 192.168.11.210 #-l=login
4) ssh root@192.168.11.210 -p 端口号
如果sshd服务修改过端口,加-p进行登录,后直接跟上端口号
注释:第一次登陆服务器时系统没有保存远程主机的信息,为了确认该主机的身份会提示用户是否继续链接,输入yes后登陆,这是系统会将远程服务器的信息写入到用户的主目录下/$HOME/.ssh/know_hosts文件中,下载进行登录的时候就不会在进行提示了。

三、日常工作中,关于sshd服务需要我们做哪些工作?
1、sshd配置文件 /etc/ssh/sshd_config

  1. #后面有空格表示注释内容
    14 # SELinux about this change.
    2)#后面没有空格表示缺省值,默认缺省值是生效的
    17 #Port 22
    修改第17行的默认端口号,修改默认缺省值,需要取消#号后才可以生效
    修改默认端口号,需要重启sshd服务才生效
    3)第19号:修改监听地址,默认修改为内网地址
    19 #ListenAddress 0.0.0.0
    4)第65行:密码认证项。
    65 PasswordAuthentication yes
    如果不是使用密码登录,就将这一项修改为NO
  2. 第37行:等待登录时间(输入密码的等待登录时间,默认2分钟)
    37 #LoginGraceTime 2m
  3. 第38行:是否允许root用户登录(默认是允许,yes)
    38 #PermitRootLogin yes
  4. 第64行:不允许空密码登录
    64 #PermitEmptyPasswords no
  5. 第105行:登录后打印登录信息(PrintMotd yes)
    105 #PrintMotd yes
  6. 第115行:是否使用DNS服务器解释(默认是开启的,yes)
    115 #UseDNS yes
    实例讲解:
    1.1 设置sshd监听端口号
    1)ssh预设使用22这个port,可以使用多个port,即重复使用port这个设定项
    2)例如想要开放ssh端口为22和222,则可以多加一行内容:Port 222 即可
    3)然后重启sshd服务就生效了。
    注意:这里建议修改port端口号,防止别人暴力破解
    例1:修改sshd服务默认监听的端口号为222

    [root@xuegod200 ~]# grep Port /etc/ssh/sshd_config
    Port 222
    [root@xuegod200 ~]# systemctl restart sshd #重启sshd服务
    [root@xuegod200 fail2ban]# ssh root@xuegod200 #不加端口,默认使用22端口登录失败
    ssh: connect to host xuegod200 port 22: Connection refused
    [root@xuegod200 fail2ban]# ssh root@xuegod200 -p 222 #加端口登录
    root@xuegod200’s password:
    Last login: Sat May 11 22:26:59 2019 from xuegod210

1.2 ListenAddress 0.0.0.0
1)设置sshd服务器绑定的IP地址,0.0.0.0表示侦听所有的地址
2)如果主机不需要从公网ssh访问,可以把监听地址改为内网地址
注意:这里的监听地址,服务器必须可以进行通信,即网络是可达的状态,否则服务启动失败。
1.3 Protocol 2
1)选择的ssh协议版本,可以使1也可以是2,Cnetos 5.x预设仅支持V2版本,出于安全考虑,设置为最新的协议版本
1.4 #HostKey /etc/ssh/ssh_host_rsa_key
1)设置包含计算机私人密钥的文件
1.5 SyslogFacility AUTHPRIV
当有人使用ssh登入系统的时候,ssh会记录信息,这个信息要记录的类型为AUTHPRIV,ssh服务日志存放在:/var/log/secure
注释:查看/etc/rssylog.conf文件,可以看出ssh的日志为什么会存放到/var/log/secure文件中

[root@xuegod200 ~]# grep -i authpriv /etc/rsyslog.conf
authpriv.* /var/log/secure

1.6 #LogLevel INFO
定义登录级别的等级
1.7 打印提示信息
将需要显示的信息加入到/etc/motd文件中
例:

[root@xuegod200 ~]# echo “hello world” >> /etc/motd

 效果:
  [root@xuegod210 ~]# ssh root@192.168.11.200
  root@192.168.11.200's password: 
  Last login: Sat May 11 22:04:33 2019 from xuegod210
  hello world

2、 安全调优的重点
2.1 LoginGraceTime 2m
1)grace意思是系统给与多少秒进行登录
2)当使用者连上SSH server之后,会出现输入密码的画面,在画面中等待的时间
3)在多久时间内没有成功连接上SSH server 就强迫断线!若无单位则默认时间为秒
2.2 #PermitRootLogin yes
是否允许root用户登录,默认是允许的,但是建议修改为NO,真实的生产环境服务器,是不允许root账号直接登录的,仅允许普通用户登录,需要使用root用户再切换到root用户。
2.3 PasswordAuthentication yes
密码验证当然是需要的,这里写yes,也可以写no,在真是的服务器中,根据不同的安全级别要求,有的是需要设置不需要密码登录,通过认证的秘钥来登录。修改后需要重启服务器才生效。
2.4 # PermitEmptyRpasswords no
是否允许空密码的用户登录,默认为no,不允许空密码用户登录系统
2.5 # PrintLastLog yes
显示上次登录的信息,默认为yes

[root@xuegod200 ~]# ssh xuegod210
root@xuegod210’s password:
Last login: Sun May 12 20:56:21 2019 from xuegod200 #显示上一次登录的信息

2.6 # UserDNS yes
一般来说,为了要判断客户端来源是否正常合法的,因此会使用DNS去反查客户端的主机名,但通常在内网互联时,该设置为no,因此使联网速度会快些。

四、sshd防止暴力破解
1、配置安全的sshd服务的要求
1)密码足够复杂,长度大于8位最好大于20位。复杂度要有数字、大小字母和特殊符号混合组成。
2)修改默认登录端口,不要和22端口类似。一般都改5位数。范围:0-65535
3)不允许root用户直接登录到用户,添加普通用户,使用普通用户登录系统,授予root的权限,必要时在从普通用户切换到root用户
4)不允许空密码登录,只能通过认证的密钥来登录系统

1.1 通过密钥认证来实现sshd认证

[root@xuegod210 ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): #提示输入密钥保存的路径
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:mk4r77QCqlNScBem2HXJ3ZEX83OFA5HEBC/Qc7hKC8w root@xuegod210
The key’s randomart image is:
±–[RSA 2048]----+
| +o.o.ooOB= …|
|.o.+…o ….=oo .|
|.oo. o .=. o…|
| . E . … o |
| . oSo |
|. o oo |
| + . = |
|o o+ o |
|o. =
|
±—[SHA256]-----+

[root@xuegod210 ~]# cd /root/.ssh/
[root@xuegod210 .ssh]# ll
总用量 12
-rw------- 1 root root 1679 5月 11 22:25 id_rsa #私钥文件
-rw-r–r-- 1 root root 396 5月 11 22:25 id_rsa.pub #public key公钥(pub)
-rw-r–r-- 1 root root 176 5月 11 22:03 known_hosts #所有访问的公钥都保存在这个文件内,如果清空,下次登录还是需要提示输入密码,保存后,再次登录就不会提示

注释:解释known_hosts文件的作用,如下例子

[root@xuegod210 .ssh]# ssh xuegod210
The authenticity of host ‘xuegod210 (192.168.11.210)’ can’t be established.
ECDSA key fingerprint is SHA256:WDL0gvyyWLvrv05VooVstGHR/CREgUK3SyJGCsdy2Rk.
ECDSA key fingerprint is MD5:fb:d9:7d:48:37:52:a9:a2:2e:57:0a:61:86:c3:f7:2d.
Are you sure you want to continue connecting (yes/no)? yes #需要输入yes
Warning: Permanently added ‘xuegod210,192.168.11.210’ (ECDSA) to th e list of known hosts. #将确认文件保存到known_hosts文件

再次输入如下:

[root@xuegod210 ~]# ssh xuegod210
root@xuegod210’s password: #直接提示输入密码,没有提示输入yes或no
Last login: Sun May 12 21:49:43 2019 from xuegod210

[root@xuegod210 .ssh]# ssh-copy-id -i root@xuegod200 #将公钥文件传到远程服务器,实现免密登录
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: “/root/.ssh/id_rsa.pub”
The authenticity of host ‘xuegod200 (192.168.11.200)’ can’t be established.
ECDSA key fingerprint is SHA256:WDL0gvyyWLvrv05VooVstGHR/CREgUK3SyJGCsdy2Rk.
ECDSA key fingerprint is MD5:fb:d9:7d:48:37:52:a9:a2:2e:57:0a:61:86:c3:f7:2d.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempti ng to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed – if you are prompted now it is to install the new keys
root@xuegod200’s password: #输入远程主机的密码
Number of key(s) added: 1
Now try logging into the machine, with: “ssh ‘root@xuegod200’”
and check to make sure that only the key(s) you wanted were added.
[root@xuegod210 .ssh]# ssh root@xuegod200
Last login: Sat May 11 22:14:14 2019 from xuegod210
hello world

注意:如果对端的登录端口默认不是22端口,需要加端口号进行登录

[root@xuegod200 ~]# ssh -p 2233 root@xuegod220

2、通过开源的防护工具来防止暴力破解
2.1 下载软件包
地址:http://www.fail2ban.org
2.2 使用epel扩展源安装

[root@xuegod200 ~]# yum -y install fail2ban

2.3 配置文件的存放路径

[root@xuegod200 ~]# cd /etc/fail2ban/
[root@xuegod200 fail2ban]# ls
action.d filter.d paths-common.conf paths-freebsd.conf
fail2ban.conf jail.conf paths-debian.conf paths-opensuse.conf
fail2ban.d jail.d paths-fedora.conf paths-osx.conf

2.4 相关配置文件作用说明
/etc/fail2ban/action.d #动作文件,内含默认文件。iptables以及mail等动作配置
/etc/fail2ban/fail2ban.conf #定义了fail2ban日志级别、日志位置和sock文件位置
/etc/fail2ban/filter.d #条件文件夹,内含默认文件。过滤日志关键内容设置
/etc/fail2ban/jail.conf #主要配置文件,模块化。主要设置启动ban动作的服务及动作阀值
2.5 启动fail2ban服务

[root@xuegod200 fail2ban]# systemctl start fail2ban.service
[root@xuegod200 fail2ban]# systemctl enable fail2ban.service
[root@xuegod200 fail2ban]# systemctl status fail2ban.service

3、实战实例:ssh远程登录5分钟内3次密码验证失败,禁止用户IP访问主机1小时,1小时后自动取消限制,用户可自动登录
1)修改jail.conf文件
在第224行的SSHD模块下添加如下内容,即在231行开始添加如下:
231 enabled = true #是否激活此项(true/flase)修改成true
232 filter = sshd #过滤规则filter的名字,对应filterr.d目录下的sshd.conf
233 action = iptables[name=SSH, port=ssh, protocol=tcp] #动作相关的参数,对应action.d/iptables.conf文件。port=ssh,这里默认没有修改端口可以写ssh,如果端口修改,需要些修改后的端口
234 sendmail-whois[name=SSH, dest=you@example.com, sender=fail2ban@example.com, send ername=“Fail2Ban”] #触发报警的收件人。dest是收件人,sender是发件人,可以指定外部邮箱。
235 logpath = /var/log/secure #检测的系统登录日志文件,这里写sshd服务日志文件
236 bantime = 3600 #禁止用户IP访问主机1小时
237 findtime = 300 #5分钟内出现规定的次数就开始工作
238 maxretry = 3 #3次密码验证失败
2)启动服务

[root@xuegod200 fail2ban]# systemctl start fail2ban
[root@xuegod200 fail2ban]# systemctl status fail2ban
[root@xuegod200 fail2ban]# systemctl enable fail2ban

3)清空日志、重启服务、查看防火墙规则链

[root@xuegod200 fail2ban]# > /var/log/secure
[root@xuegod200 fail2ban]# systemctl restart fail2ban
[root@xuegod200 fail2ban]# iptables -L -n #在fail2ban服务启动后,iptables会多生成一个规则链,如下
Chain f2b-SSH (1 references)
target prot opt source destination
RETURN all – 0.0.0.0/0 0.0.0.0/0

4)测试结果,故意输错3次密码,在进行登录,会提示拒绝登录

[root@xuegod210 ~]# ssh root@xuegod200 #三次故意输错
root@xuegod200’s password:
Permission denied, please try again.
root@xuegod200’s password:
Permission denied, please try again.
root@xuegod200’s password: #强制结束
^C
[root@xuegod210 ~]# ssh root@xuegod200 #再次登录,提示Connection refused
ssh: connect to host xuegod200 port 22: Connection refused

5)查看防火墙规则

[root@xuegod200 ~]# iptables -L -n | tail -4 #拒绝了11.200所有的访问
target prot opt source destination
REJECT all – 192.168.11.210 0.0.0.0/0 reject-with icmp-port-unreachable
RETURN all – 0.0.0.0/0 0.0.0.0/0

6)检查fail2ban客户端是否工作

[root@xuegod200 ~]# fail2ban-client status #具体看某一项的状态也行,如果显示被ban的ip和数目表示成功了,如果都是0,说明没有成功
Status
|- Number of jail: 1
`- Jail list: sshd

7)查看详细被ban的数量和IP地址

[root@xuegod200 ~]# fail2ban-client status sshd
Status for the jail: sshd
|- Filter
| |- Currently failed: 0 #当前失败的
| |- Total failed: 6 #失败的总数
| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd #日志的匹配
`- Actions
|- Currently banned: 2 #当前禁止的数量
|- Total banned: 2 #禁止的总量
`- Banned IP list: 192.168.11.200 192.168.11.210 #被ban的ip地址列表

8)查看fail2ban的日志能看到相关信息

[root@xuegod200 ~]# cat /var/log/fail2ban.log
2019-05-12 23:24:22,158 fail2ban.actions [2077]: NOTICE [sshd] Ban 192.168.11.210

4、需要注意的点:
(1) 另外如果后期需要把iptables清空后或iptables重启后,也需要把fail2ban重启一下

(2) 如果修改ssh默认端口22为2015后,配置fail2ban来监控SSHD服务需要修改配置文件

例:

[root@xuegod200 ~]# vim /etc/ssh/sshd_config
改 17 #Port 22
为 17 Port 2015

[root@xuegod200 ~]# systemctl restart sshd

[root@xuegod200 ~]# vim /etc/fail2ban/jail.conf
#修改iptables动作中的端口号,默认为SSH,如图 1-11 所示。
改:port=ssh
为 port=2015

图 1-11 修改fail2ban监听SSH端口
重启服务即可

[root@xuegod200 ~]# systemctl restart fail2ban

fail2ban从黑名单中移除IP的方法:

[root@xuegod200 ~]# fail2ban-client set sshd unbanip 192.168.1.64

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值