首页 > 【CentOS实用篇】之SSH的安全登录

PF2-ssh,【CentOS实用篇】之SSH的安全登录

互联网 2021-01-28 20:02:45
在线算命,八字测算命理

SSH是安全的远程登录,使用SSH v2的协议版本,基于DH算法做密钥交换,基于RSA或DSA实现身份认证。SSH的用户登录认证是基于password和基于key

准备工作

本次实验用到的虚拟机有(1)c6 --------- IP:172.18.252.84(2)c7 --------- IP:172.18.253.104(3)Mini6 ----- IP:172.18.251.89(4)Mini7 ----- IP:172.18.250.11

客户端工具包:openssh-clients.x86_64服务器端工具包:openssh-server.x86_64客户端配置文件:/etc/ssh/ssh_config服务器端配置文件:/etc/ssh/sshd_config登录记录保存文件:/root/.ssh/known_hosts

一、基于password的SSH连接

基于用户和口令登录验证的登录流程1 客户端发起ssh请求,服务器会把自己的公钥发送给用户2 用户会根据服务器发来的公钥对密码进行加密3 加密后的信息回传给服务器,服务器用自己的私钥解密,如果密码正确,则用户登录成功

SSH的安全性

在首次远程登录某个主机时,会保存对方主机的公钥储存于/root/.ssh/known_hosts文件里,方便于再次连接时的身份确认,不能被假冒或替代。由以下实验证实SSH的安全性

第一步:利用c6首次登录Mini6主机的ip地址172.18.251.89

[root@c6 ~]#ssh 172.18.251.89The authenticity of host '172.18.251.89 (172.18.251.89)' can't be established.RSA key fingerprint is 3d:25:b4:84:db:de:ef:e8:57:e9:79:10:cb:57:a6:a2.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.18.251.89' (RSA) to the list of known hosts.root@172.18.251.89's password: Last login: Mon Sep 11 15:03:36 2017 from 192.168.10.1[root@Mini6 ~]#

第二步:查看c6主机的连接记录文件known_hosts

[root@c6 ~]#cat .ssh/known_hosts 172.18.251.89 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8edaJKzACgj4cnfyGmjCy2p2vn87KAcm+i/5q5RadFUknS7ALyUxoMBMWz1VThdjoW4nII32qiRV+zoEYEn+ua5fc3ZtBmNZ+saeGeDApE6u0OsfHbBiR6lp97GbB1r1yi4ERgbZsRtlNpJPaHXoRKLx2VtiVQ5WHdBEPMoqXnoMbv54r7F3TvWIOGvDzzm9Xq0SCGUbWtz3M0PpxVVPTSb9RMgCLLt+qtWkkBL6G7WWQhMrc8fbKqzXZO26OUSW9nVowlDqkXXzHaGeNJvNyhkDSRXzCDV3GxSM7TksgCizaCY/ilTO77gZVzDZZyB870/j+uHaiPogdqcV1eLer

第三步:down掉Mini6主机IP地址172.18.251.89使用的网卡

[root@Mini6 ~]#ifconfig eth1 down[root@Mini6 ~]#ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host loinet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:27:99:0c brd ff:ff:ff:ff:ff:ffinet 192.168.10.129/24 brd 192.168.10.255 scope global eth0inet6 fe80::20c:29ff:fe27:990c/64 scope link valid_lft forever preferred_lft forever3: eth1:mtu 1500 qdisc pfifo_fast state DOWN qlen 1000link/ether 00:0c:29:27:99:16 brd ff:ff:ff:ff:ff:ffinet 172.18.251.89/16 brd 172.18.255.255 scope global eth1

第四步:修改Mini7主机的ip为172.18.251.89,实现冒充

[root@Mini7 network-scripts]#ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens33:mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:fc:73:b3 brd ff:ff:ff:ff:ff:ffinet 192.168.10.128/24 brd 192.168.10.255 scope global dynamic ens33 valid_lft 1797sec preferred_lft 1797secinet6 fe80::dc7d:7e:6cb9:fcd5/64 scope link valid_lft forever preferred_lft forever3: ens34:mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:fc:73:bd brd ff:ff:ff:ff:ff:ffinet 172.18.251.89/16 brd 172.18.255.255 scope global ens34 valid_lft forever preferred_lft foreverinet6 fe80::d46f:ec3b:310f:2e3a/64 scope link valid_lft forever preferred_lft forever

第五步:用c6主机重新登录172.18.251.89,会有报错提示

[root@c6 ~]#ssh 172.18.251.89@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!Someone could be eavesdropping on you right now (man-in-the-middle attack)!It is also possible that the RSA host key has just been changed.The fingerprint for the RSA key sent by the remote host is17:f2:11:d1:ca:b1:42:3e:72:bc:29:26:0d:ba:69:67.Please contact your system administrator.Add correct host key in /root/.ssh/known_hosts to get rid of this message.Offending key in /root/.ssh/known_hosts:1RSA host key for 172.18.251.89 has changed and you have requested strict checking.Host key verification failed.

第六步:重新恢复主机Mini6的IP地址,再次用主机c6登录Mini6,即可实现登录

[root@c6 ~]#ssh 172.18.251.89root@172.18.251.89's password: Last login: Mon Sep 11 16:27:20 2017 from 172.18.252.84[root@Mini6 ~]#

第八步:查看主机Mini6的公钥,与主机c6登录时保存的文件known_hosts内的公钥一致

[root@Mini6 ssh]#cat ssh_host_rsa_key.pub 172.18.251.89 ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8edaJKzACgj4cnfyGmjCy2p2vn87KAcm+i/5q5RadFUknS7ALyUxoMBMWz1VThdjoW4nII32qiRV+zoEYEn+ua5fc3ZtBmNZ+saeGeDApE6u0OsfHbBiR6lp97GbB1r1yi4ERgbZsRtlNpJPaHXoRKLx2VtiVQ5WHdBEPMoqXnoMbv54r7F3TvWIOGvDzzm9Xq0SCGUbWtz3M0PpxVVPTSb9RMgCLLt+qtWkkBL6G7WWQhMrc8fbKqzXZO26OUSW9nVowlDqkXXzHaGeNJvNyhkDSRXzCDV3GxSM7TksgCizaCY/ilTO77gZVzDZZyB870/j+uHaiPogdqcV1eLer模拟IP地址的冒充

在SSH的登录过程中,因为会保存对方的公钥文件做为对主机的确认,也就是利用对方的公钥验证对方的私钥检验对方主机,所以如果要实现冒充对方主机,除了冒充对方的IP地址之外,还要拿到对方主机的私钥。在现实中很难拿到对方的私钥,以下实验仅做证实和参考。

第一步:复制主机Mini6的私钥文件到主机Mini7对应的私钥文件目录,替换掉主机Mini7自带的私钥文件

[root@Mini6 ~]#scp ssh_host_rsa_key 172.18.253.104:/etc/ssh/The authenticity of host '172.18.253.104 (172.18.253.104)' can't be established.RSA key fingerprint is 4a:3c:ce:1a:3f:e1:76:9a:89:9e:b0:c8:ff:ac:9e:0d.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.18.253.104' (RSA) to the list of known hosts.root@172.18.253.104's password: ssh_host_rsa_key 100% 1675 1.6KB/s 00:00

第二步:down掉主机Mini6对应IP地址的网卡

[root@Mini6 ~]#ifconfig eth1 down[root@Mini6 ~]#ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host loinet6 ::1/128 scope host valid_lft forever preferred_lft forever2: eth0:mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:27:99:0c brd ff:ff:ff:ff:ff:ffinet 192.168.10.129/24 brd 192.168.10.255 scope global eth0inet6 fe80::20c:29ff:fe27:990c/64 scope link valid_lft forever preferred_lft forever3: eth1:mtu 1500 qdisc pfifo_fast state DOWN qlen 1000link/ether 00:0c:29:27:99:16 brd ff:ff:ff:ff:ff:ffinet 172.18.251.89/16 brd 172.18.255.255 scope global eth1

第三步:修改主机Mini7的IP地址为172.18.251.89

[root@Mini7 ~]#ip a1: lo:mtu 65536 qdisc noqueue state UNKNOWN qlen 1link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft foreverinet6 ::1/128 scope host valid_lft forever preferred_lft forever2: ens33:mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:32:35:10 brd ff:ff:ff:ff:ff:ffinet 192.168.10.134/24 brd 192.168.10.255 scope global dynamic ens33 valid_lft 951sec preferred_lft 951secinet6 fe80::997a:4280:f508:a15b/64 scope link valid_lft forever preferred_lft forever3: ens34:mtu 1500 qdisc pfifo_fast state UP qlen 1000link/ether 00:0c:29:32:35:1a brd ff:ff:ff:ff:ff:ffinet 172.18.251.89/16 brd 172.18.255.255 scope global ens34 valid_lft forever preferred_lft foreverinet6 fe80::eaa:3e5a:d418:b3c3/64 scope link valid_lft forever preferred_lft forever

第四步:重新用主机c6登录172.18.251.89,此时连接到主机Mini7,而不再是之前的主机Mini6

[root@c6 ~]#ssh 172.18.251.89root@172.18.251.89's password: Last login: Mon Sep 11 17:05:19 2017 from 192.168.10.1[root@Mini7 ~]#更改SSH的监听端口

第一步:修改SSH服务器端的配置文件/etc/ssh/sshd_config,找到Port 22(默认端口22),修改Port后的端口号

[root@Mini6 ~]#vim /etc/ssh/sshd_config # $OpenBSD: sshd_config,v 1.80 2008/07/02 02:24:18 djm Exp $# This is the sshd server system-wide configuration file.See# sshd_config(5) for more information.# This sshd was compiled with PATH=/usr/local/bin:/bin:/usr/bin# The strategy used for options in the default sshd_config shipped with# OpenSSH is to specify options with their default value where# possible, but leave them commented.Uncommented options change a# default value.Port 2234 # 端口号修改为2234

第二步:因为端口改为非标准端口,需要关闭SELinux和防火墙iptables

[root@Mini6 ~]#vim /etc/selinux/config # This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=disabled# SELINUXTYPE= can take one of these two values:# targeted - Targeted processes are protected,# mls - Multi Level Security protection.SELINUXTYPE=targeted[root@Mini6 ~]#service iptables stopiptables: Setting chains to policy ACCEPT: filter[OK]iptables: Flushing firewall rules: [OK]iptables: Unloading modules: [OK][root@Mini6 ~]#iptables -vnLChain INPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination

第三步:重启sshd服务,并查看服务端口

[root@Mini6 ~]#service sshd restartStopping sshd: [OK]Starting sshd: [OK][root@Mini6 ~]#ss -nlState Recv-Q Send-QLocal Address:PortPeer Address:Port LISTEN0100 ::1:25:::* LISTEN0100 127.0.0.1:25 *:* LISTEN0128:::2234:::* LISTEN0128 *:2234 *:*

第四步:用主机c6重新登录172.18.251.89因为sshd端口的改变,此时用最基本的ssh命令重新登录172.18.251.89,系统会提示目标主机不可到达

[root@c6 ~]#ssh 172.18.251.89ssh: connect to host 172.18.251.89 port 22: Connection refused

在远程登录时用-p属性指定端口2234即可连接

[root@c6 ~]#ssh 172.18.251.89 -p 2234root@172.18.251.89's password: Last login: Mon Sep 11 16:27:45 2017 from 172.18.252.84[root@Mini6 ~]#

修改本机ssh的配置文件/etc/ssh/ssh_config,修改默认端口号Pork为2234,可以直接登录端口为2234的Mini6主机

[root@c6 ~]#vim /etc/ssh/ssh_config# GSSAPITrustDNS no# BatchMode no# CheckHostIP yes# AddressFamily any# ConnectTimeout 0# StrictHostKeyChecking ask# IdentityFile ~/.ssh/identity# IdentityFile ~/.ssh/id_rsa# IdentityFile ~/.ssh/id_dsa Port 2234[root@c6 ~]#ssh 172.18.251.89root@172.18.251.89's password: Last login: Mon Sep 11 18:42:43 2017 from 172.18.252.84[root@Mini6 ~]#SSH的跳转连接

生产环境中,某些服务器只循序特定的主机才能登录,隐藏其他机器需要利用这个特定的主机去跳转到服务器主机。在ssh命令中,使用-t选项可以实现ssh的跳转登录,以下实现模拟跳转登录第一步:在主机Mini6中禁止来自于172.18.252.84的流量

[root@Mini6 ~]#iptables -A INPUT -s 172.18.252.84 -j REJECT

第二步:利用主机c7进行跳转,实现主机c6对主机Mini6的远程登录,登录时需要输入跳板机和对方主机的登录口令

[root@c6 ~]#ssh -t 172.18.253.104 ssh 172.18.251.89root@172.18.253.104's password: root@172.18.251.89's password: Last login: Mon Sep 11 21:06:38 2017 from 172.18.253.104[root@Mini6 ~]#基于key的远程连接

在生产环境中,一个工作人员需要维护很多设备,需要经常登录不同的设备,每次登录都需要输入口令会大大降低工作的效率,基于key的验证可以解决这个问题

基于密钥的登录方式1 首先在客户端生成一对密钥(ssh-keygen)2 并将客户端的公钥ssh-copy-id 拷贝到服务端3 当客户端再次发送一个连接请求,包括ip、用户名4 服务端得到客户端的请求后,会到authorized_keys中查找,如果有响应的IP和用户,就会随机生成一个字符串,例如:acdf5 服务端将使用客户端拷贝过来的公钥进行加密,然后发送给客户端6 得到服务端发来的消息后,客户端会使用私钥进行解密,然后将解密后的字符串发送给服务端7 服务端接受到客户端发来的字符串后,跟之前的字符串进行对比,如果一致,就允许免密码登录

以下实验实现主机c6对Mini6基于key的登录

第一步:生成c6的密钥对,-t指定算法为rsa,-P指定私钥的加密口令为空,默认保存的文件夹为/root/.ssh/,私钥文件位为id_rsa,公钥文件为id_rsa.pub

[root@c6 ~]#ssh-keygen -t rsa -P ''Generating public/private rsa key pair.Enter file in which to save the key (/root/.ssh/id_rsa): 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:f9:fc:db:08:05:6e:a2:58:6f:43:06:0b:b0:3d:a4:d0 root@c6The key's randomart image is:+--[ RSA 2048]----+|... .|| .E* ||o + . .|| o o o . ||o S o .|| o = = . ||. . + +|| . . o o ||+..|+-----------------+[root@c6 ~]#ls /root/.ssh/id_rsaid_rsa.pubknown_hosts

第二步:从主机c6复制公钥文件到需要连接的主机Mini6-i指定要复制的公钥文件名,利用ssh-copy-id命令复制公钥文件时,即使指定的文件是私钥文件,也会复制公钥文件到对方主机,相对于用scp复制到对方主机,避免了修改路径、文件名和错误复制私钥。

[root@c6 ~]#ssh-copy-id -i /root/.ssh/id_rsa.pub root@172.18.251.89root@172.18.251.89's password: Now try logging into the machine, with "ssh 'root@172.18.251.89'", and check in:.ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.[root@Mini6 ~]#ls .ssh/authorized_keysknown_hosts

第三步:利用主机c6直接连接Mini6

[root@c6 ~]#ssh 172.18.251.89Last login: Mon Sep 11 21:06:50 2017 from 172.18.253.104[root@Mini6 ~]#

复制公钥到所有需要直连的主机可实现对多主机的直接操作

在基于key的连接以后,大大的方便了对远程主机的操作,可以直接在本机执行远程主机的命令[root@c6 ~]#ssh 172.18.251.89 'hostname'Mini6[root@c6 ~]#[root@c6 ~]#ssh 172.18.251.89 'ifconfig eth1'eth1Link encap:EthernetHWaddr 00:0C:29:27:99:16inet addr:172.18.251.89Bcast:172.18.255.255Mask:255.255.0.0inet6 addr: fe80::20c:29ff:fe27:9916/64 Scope:LinkUP BROADCAST RUNNING MULTICASTMTU:1500Metric:1RX packets:432507 errors:0 dropped:0 overruns:0 frame:0TX packets:87108 errors:0 dropped:0 overruns:0 carrier:0collisions:0 txqueuelen:1000 RX bytes:286056641 (272.8 MiB)TX bytes:6116023 (5.8 MiB)私钥文件很重要!复制私钥到其他主机进行直接登录

第一步:从c6机器中复制私钥到c7

[root@c6 ~]#scp .ssh/id_rsa root@172.18.253.104:/root/.ssh/root@172.18.253.104's password: id_rsa100% 1671 1.6KB/s 00:00

第二步:利用主机c7直接登录主机Mini6

[root@c7 ~]#ssh 172.18.251.89Last login: Mon Sep 11 22:02:35 2017 from 192.168.10.1[root@Mini6 ~]#拥有私钥的主机可以无视直连主机的密码修改,为了私钥的安全,通常要对私钥进行加密

利用ssh-keygen -p可以直接找到私钥文件并进行加密

[root@c6 ~]#ssh-keygen -pEnter file in which the key is (/root/.ssh/id_rsa): Key has comment '/root/.ssh/id_rsa'Enter new passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved with the new passphrase.

私钥加密后利用设备直接登录远程主机需要输入一次私钥的加密口令,仅此一次

[root@c6 ~]#ssh 172.18.251.89Enter passphrase for key '/root/.ssh/id_rsa': Last login: Mon Sep 11 22:23:18 2017 from 172.18.252.84[root@Mini6 ~]#启用代理,托管口令

利用命令ssh-agent bash启用代理,把私钥的密码进行托管,方便下次把公钥传给其他主机时,不用输入私钥加密口令即可直接登录。以下实验把c6的公钥复制到Mini7,第一次登录时跳过输入私钥密码

[root@c6 ~]#ssh-agent bash[root@c6 ~]#ssh-addEnter passphrase for /root/.ssh/id_rsa: Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)[root@c6 ~]#ssh-copy-id -i /root/.ssh/id_rsa root@172.18.250.11The authenticity of host '172.18.250.11 (172.18.250.11)' can't be established.RSA key fingerprint is 17:f2:11:d1:ca:b1:42:3e:72:bc:29:26:0d:ba:69:67.Are you sure you want to continue connecting (yes/no)? yesWarning: Permanently added '172.18.250.11' (RSA) to the list of known hosts.root@172.18.250.11's password: Now try logging into the machine, with "ssh 'root@172.18.250.11'", and check in:.ssh/authorized_keysto make sure we haven't added extra keys that you weren't expecting.[root@c6 ~]#ssh 172.18.250.11Last login: Mon Sep 11 18:17:12 2017 from 192.168.10.1[root@Mini7 ~]#
免责声明:非本网注明原创的信息,皆为程序自动获取自互联网,目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责;如此页面有侵犯到您的权益,请给站长发送邮件,并提供相关证明(版权证明、身份证正反面、侵权链接),站长将在收到邮件24小时内删除。

相关阅读