关于AS5 SSH支持key认证的完全解决方案

 

      SSH 为 Secure Shell 的缩写, 专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。透过 SSH 可以对所有传输的数据进行加密,也能够防止 DNS欺骗和IP欺骗.还有一个优点就是为其传输的数据是经过压缩的,所以可以加快传输的速度.
    
  在客户端来看,SSH提供两种级别的安全验证。
 
第一种级别(基于密码的安全验证),知道帐号密码,就可登录到远程主机,并且所有传输的数据都会被加密。但是,可能会有别的服务器在冒充真正的服务器,无法避免被“中间人”攻击。 
 
第二种级别(基于密匙的安全验证),需要依靠密匙,也就是你必须为自己创建一对密匙,并把公有密匙放在需要访问的服务器上。客户端软件会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在你在该服务器的用户根目录下寻找你的公有密匙,然后把它和你发送过来的公有密匙进行比较。如果两个密匙一致,服务器就用公有密匙加密“质询”(challenge)并把它发送给客户端软件。从而避免被“中间人”攻击。
 
在服务器端,SSH也提供两种安全验证。
 
第一种方案,主机将自己的公用密钥分发给相关的客户端,客户端在访问主机时则使用该主机的公开密钥来加密数据,主机则使用自己的私有密钥来解密数据,从而实现主机密钥认证,确定客户端的可靠身份。
 
第二种方案,存在一个密钥认证中心,所有提供服务的主机都将自己的公开密钥提交给认证中心,而任何作为客户端的主机则只要保存一份认证中心的公开密钥就可以了。在这种模式下,客户端必须访问认证中心然后才能访问服务器主机。
 
 
实验环境:
 

解决方案:
可以通过客户端或者服务器端提供ssh安全认证
 
1.客户端提供ssh安全认证,也就是上面提到第二种级别
1.客户端
利用客户端linux系统提供ssh安全认证,如客户端是XP系统,可以选择用SecureCRT自动生成ssh2的公钥与私钥.
由于本地系统是windows 7 ,这里使用SecureCRT生成ssh认证。
SecureCRT---Options---Global Options----SSH2
 

完成后会在C:\Users\Administrator\AppData\Roaming\VanDyke\目录自动生成以Identity命名公钥与私钥.
2)服务器端
为了server安全性考虑,禁止空密码与密码登录系统。
需修改/etc/ssh/sshd.conf
 
#PermitEmptyPasswords no      修改为         PermitEmptyPasswords no
PasswordAuthentication yes   =========     PasswordAuthentication no
 
 [root@viong .ssh]# service sshd restart
需要重启 ssh 才能使配置生效
Stopping sshd:                                             [ OK ]
Starting sshd:                                              [ OK ]
[root@viong ~]# ssh localhost
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password: 输入系统密码
Last login: Tue Feb 23 00:32:06 2010 from 192.168.15.253
[root@viong ~]# cd ~/.ssh
 
利用RZ小工具上传刚刚客户端生成的Identity.pub到~/.ssh目录下
关于RZ 与SZ的安装请看 http://viong.blog.51cto.com/844766/260081
 
[root@viong .ssh]# rz 
 
上传 Identity.pub  
 
[root@viong .ssh]# cat Identity.pub >>authorized_keys
 
把公钥的内容输出到 authorized_keys
 
[root@viong .ssh]# rm -rf Identity.pub
 
删除公钥
 
[root@viong .ssh]# chmod 400 authorized_keys
 
设置 authorized_keys 的权限为 400
 
[root@viong .ssh]# ll
total 12
-r-------- 1 root root 608 Feb 23 02:38 authorized_keys
-rw-r--r-- 1 root root 391 Feb 23 02:34 known_hosts
 
重新连接服务器,提示密钥输入密码,说明设置成功了.
 
======================================================================
客户端与服务器端都是linux系统基于RSA认证无私钥保护密码情况下
Client:192.168.15.50    server:192.168.15.64
Clinet
[root@client .ssh]# pwd
/root/.ssh   如没这个目录,可以手动添加
[root@client .ssh]# ssh-keygen –t rsa
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 viong.
Your public key has been saved in id_rsa  .pub.
The key fingerprint is:
f9:14:16:7f:a8:68:f2:90:46:2e:28:b8:45:cc:aa:e9 root@client
[root@client .ssh]# ll
total 8
-rw------- 1 root root 1675 Feb 23 22:21  id_rsa
-rw-r--r-- 1 root root 393 Feb 23 22:21  id_rsa  .pub
 [root@client .ssh]# ssh-copy-id -i /root/.ssh/ id_rsa .pub   [email protected]
发布公匙到服务器端,会在服务器端自动生成 .ssh/authorized_keys 这个文件
20
reverse mapping checking getaddrinfo for macbook_air failed - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password: 输入 192.168.15.64 root 密码
Now try logging into the machine, with "ssh '192.168.15.64'", and check in:
 .ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
提示: ssh-copy-id 这个工具是针对 22 号端口,如果对方 ssh 端口是 2520 ,那么就要修改为:
ssh-copy-id -i /root/.ssh/ id_rsa   .pub    -p 2520 192.168.15.64
 
Server
 
[root@server .ssh]# ll
查看是否有 authorized_keys 生成
total 16
-rw- ------ 1 root root 393 Feb 23 22:28 authorized_keys
-rw-r--r-- 1 root root 395 Feb 23 21:49 known_hosts
[root@server .ssh]# cat authorized_keys 确认是否发布成功
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAtUOq6yotBEkldfBFZalh4FrFtX6wERChbLn7igQI4aofBNThWgIlBPfaThncfhs2Vu9DSBxExyGje/evcu/TyQRVBTuejEQnF4RPB+5fNMS+Qq48pdS33Rd/yt4m+Np/eGYDYW/ch5r/MRm5nek/WCW2MPAQbqrSZ7prj++SnMlavZDlAVsgHbY3GLYDD/7UbFJvewbY099zjSrW7yqIPdF9nACIg0dN+aMRtezJwr+NvOFZwrUuuz/ek1iCl2Q6soxRAkOTWbkE5bnF5LJJWtOox+ZxDu/mAry0dSv3eH0NMs6NPfBkwE5pyE9HG47r7k6kp7qKsuKCysk+MASNaQ== root@client

在客户端输入
[root@client .ssh]# ssh [email protected]   
Last login: Wed Feb 24 00:13:09 2010 from 192.168.15.50
 
注意: Enter file in which to save the key (/root/.ssh/id_rsa):  直接回车---这个地方不能输入名称,不然无密码登陆会报以下错误:
Permission denied (publickey,gssapi-with-mic
 
可以等生成后再做修改
===================================================================
 
[root@server ~]#     连接成功
 
提示 :由于生成密钥的口令的密码为空,可以利用 scp 远程加密备份, scp 是有 Security 的文件 copy ,基于 ssh 登录。操作起来比较方便,比如要把当前一个文件 copy 到远程另外一台主机上,可以如下命令。
[root@clinet .ssh]# scp id_dsa.pub [email protected]:/home/  
reverse mapping checking getaddrinfo for macbook_air failed - POSSIBLE BREAK-IN ATTEMPT!
id_dsa.pub                                    100% 601     0.6KB/s   00:00
[root@server .ssh]# ll /home/
total 4
-rw-r--r-- 1 root root 601 Feb 24 00:25 id_dsa.pub    说明拷贝成功
    如果想反过来操作,把文件从远程主机 copy 到当前系统,也很简单。
[root@ server home]# scp id_dsa.pub [email protected]:/home/
The authenticity of host '192.168.15.50 (192.168.15.50)' can't be established.
RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.15.50' (RSA) to the list of known hosts.
Address 192.168.15.50 maps to ysgame2-pc, but this does not map back to the address - POSSIBLE BREAK-IN ATTEMPT!
[email protected]'s password: 输入 client 密码
id_dsa.pub                    100% 601     0.6KB/s   00:00 
 
[root@ clinet .ssh]# ll /home/
total 4
-rw-r--r-- 1 root root 601 Feb 24 00:29 id_dsa.pub     说明拷贝成功
 
2.服务器端提供ssh安全认证,也就上面提到第一种方案
1)服务器端(AS5)
同样修改/etc/ssh/sshd.conf
 
#PermitEmptyPasswords no      修改为         PermitEmptyPasswords no
PasswordAuthentication yes   =========      PasswordAuthentication no
 
 [root@viong .ssh]# service sshd restart
需要重启 ssh 才能使配置生效
Stopping sshd:                                             [ OK ]
Starting sshd:                                              [ OK ]
 
[root@viong ~]# ssh localhost
第一次通过 ssh 命令登入服务器时,要输入 “yes” ,再输入本地服务器的密码,登入信息记入到用户主目录下 .ssh known_host 文件中。
The authenticity of host 'localhost (127.0.0.1)' can't be established.
RSA key fingerprint is 05:de:f8:d8:7b:db:36:0a:1e:01:cc:06:64:0e:7e:0a.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'localhost' (RSA) to the list of known hosts.
root@localhost's password:     输入系统密码
Last login: Tue Feb 23 00:32:06 2010 from 192.168.1.253
[root@viong ~]# cat known_hosts
localhost ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAy1OKMCYaKdT544qv0hRBBC6rFZviEJ6ECwnBV/mNkwahgkPp5TTHyHDUeBPtYmmKVdcxgjItUtTBHFPQjN2aKwNmPfikqf0WoaN7yf2F6IVAyAIaIi5jXwkJzadW61izmCEkdi/33r4oj5uoNfTjMBfghZZzrMwGZHEaEg2nWbbhMJrzuMU6v31WhjPm6MlZ+xBoR7T2iqiDyUczzSJZ2KG2rPMTG6Zdxm+WLrioOoeIn25wY+4gsrpPUGHxQsxVNIpUP6UtGZA/Y9uksgN5OSIkSxRwGPDTCp65mYQ7pDo9Pvbxg1yQFs54+zA2XcsKz55mbn0MMucPrOxbfGtIEQ==
 
以上连接是基于口令认证,相对telnet 还是很安全. 但还是会受到“中间人”攻击
 
[root@viong ~]# cd ~/.ssh
[root@viong ~]# ssh-keygen -t dsa 生成公钥与私钥
Generating public/private dsa key pair.
Enter file in which to save the key (/root/.ssh/id_dsa): viong 密钥文件名
Enter passphrase (empty for no passphrase): 输入口令
Enter same passphrase again: 再次输入口令
Your identification has been saved in viong.
Your public key has been saved in viong.pub.
The key fingerprint is:
cf:fd:14:c1:7c:eb:de:e4:5e:bb:91:6a:70:57:1c:8e root@viong
 
[root@viong .ssh]# ll
total 12
-rw-r--r-- 1 root root 391 Feb 23 02:34 known_hosts
-rw------- 1 root root 736 Feb 23 03:08 viong
-rw-r--r-- 1 root root 600 Feb 23 03:08 viong.pub
 
[root@viong .ssh]# cat viong.pub >>authorized_keys
把公钥的内容输出到 authorized_keys
[root@viong .ssh]# chmod 400 authorized_keys
设置 authorized_keys 的权限为 400
2)客户端(WIN 7-SecureCRT)
把viong 私钥和viong.pub 公钥用sz 小工具下载到本地同一个目录
[root@viong .ssh]# sz viong
[root@viong .ssh]# sz viong.pub
[root@viong .ssh]# rm -rf viong.pub
[root@viong .ssh]# rm -rf viong
删除公钥与密钥
 
用SecureCRT测试连接,如下图,说明连接成功。

 
关于大批量服务器更改key的脚本,后面会更新上去.
 
备注: 关于 /etc/ssh/sshd.conf详解,转载了鸟哥的文章给大家参考。
http://203.208.39.132/search?q=cache:MGSOnAay0xoJ:blog.chinaunix.net/u3/99067/showart_2022385.html+&cd=3&hl=zh-CN&ct=clnk&gl=cn&st_usg=ALhdy2_BDztvJ6iX9NPcUkXym9Ohx0SclQ
 
# 1. 关于 SSH Server 的整体设定,包含使用的 port 啦,以及使用的密码演算方式
Port 22            # SSH 预设使用 22 这个 port ,您也可以使用多的 port
              # 亦即重复使用 port 这个设定项目即可!
Protocol 2,1         # 选择的 SSH 协议版本,可以是 1 也可以是 2
              # 如果要同时支持两者,就必须要使用 2,1 这个分隔了!
#ListenAddress 0.0.0.0    # 监听的主机适配卡!举个例子来说,如果您有两个 IP
              # 分别是 192.168.0.100 192.168.2.20 ,那么只想要
              # 开放 192.168.0.100 时,就可以写如同下面的样式:
ListenAddress 192.168.0.100          # 只监听来自 192.168.0.100 这个 IP SSH 联机。
                   # 如果不使用设定的话,则预设所有接口均接受 SSH
PidFile /var/run/sshd.pid         # 可以放置 SSHD 这个 PID 的档案!左列为默认值
LoginGraceTime 600      # 当使用者连上 SSH server 之后,会出现输入密码的画面,
              # 在该画面中,在多久时间内没有成功连上 SSH server
              # 就断线!时间为秒!
Compression yes         # 是否可以使用压缩指令?当然可以��!
 
# 2. 说明主机的 Private Key 放置的档案,预设使用下面的档案即可!
HostKey /etc/ssh/ssh_host_key       # SSH version 1 使用的私钥
HostKey /etc/ssh/ssh_host_rsa_key     # SSH version 2 使用的 RSA 私钥
HostKey /etc/ssh/ssh_host_dsa_key     # SSH version 2 使用的 DSA 私钥
# 2.1 关于 version 1 的一些设定!
KeyRegenerationInterval 3600       # 由前面联机的说明可以知道, version 1 会使用  
                   # server Public Key ,那么如果这个 Public 
                   # Key 被偷的话,岂不完蛋?所以需要每隔一段时间
                   # 来重新建立一次!这里的时间为秒!
ServerKeyBits 768           # 没错!这个就是 Server key 的长度!
# 3. 关于登录文件的讯息数据放置与 daemon 的名称!
SyslogFacility AUTH           # 当有人使用 SSH 登入系统的时候, SSH 会记录资
                   # 讯,这个信息要记录在什么 daemon name 底下?
                   # 预设是以 AUTH 来设定的,即是 /var/log/secure
                   # 里面!什么?忘记了!回到 Linux 基础 去翻一下
                   # 其它可用的 daemon name 为: DAEMON,USER,AUTH,
                   # LOCAL0,LOCAL1,LOCAL2,LOCAL3,LOCAL4,LOCAL5,
LogLevel INFO               # 登录记录的等级!嘿嘿!任何讯息!
                   # 同样的,忘记了就回去参考!
# 4. 安全设定项目!极重要!
# 4.1 登入设定部分
PermitRootLogin no        # 是否允许 root 登入!预设是允许的,但是建议设定成 no
UserLogin no          # SSH 底下本来就不接受 login 这个程序的登入!
StrictModes yes         # 当使用者的 host key 改变之后, Server 就不接受联机,
              # 可以抵挡部分的木马程序!
#RSAAuthentication yes     # 是否使用纯的 RSA 认证!?仅针对 version 1
PubkeyAuthentication yes   # 是否允许 Public Key ?当然允许啦!只有 version 2
AuthorizedKeysFile      .ssh/authorized_keys
              # 上面这个在设定若要使用不需要密码登入的账号时,那么那个
              # 账号的存放档案所在档名!
# 4.2 认证部分
RhostsAuthentication no    # 本机系统不止使用 .rhosts ,因为仅使用 .rhosts
              # 不安全了,所以这里一定要设定为 no
IgnoreRhosts yes       # 是否取消使用 ~/.ssh/.rhosts 来做为认证!当然是!
RhostsRSAAuthentication no # 这个选项是专门给 version 1 用的,使用 rhosts 档案在
              # /etc/hosts.equiv 配合 RSA 演算方式来进行认证!不要使用
HostbasedAuthentication no # 这个项目与上面的项目类似,不过是给 version 2 使用的!
IgnoreUserKnownHosts no    # 是否忽略家目录内的 ~/.ssh/known_hosts 这个档案所记录
              # 的主机内容?当然不要忽略,所以这里就是 no 啦!
PasswordAuthentication yes # 密码验证当然是需要的!所以这里写 yes ��!
PermitEmptyPasswords no    # 若上面那一项如果设定为 yes 的话,这一项就最好设定
              # no ,这个项目在是否允许以空的密码登入!当然不许!
ChallengeResponseAuthentication yes  # 挑战任何的密码认证!所以,任何 login.conf 
                   # 规定的认证方式,均可适用!
#PAMAuthenticationViaKbdInt yes      # 是否启用其它的 PAM 模块!启用这个模块将会
                   # 导致 PasswordAuthentication 设定失效!
 
# 4.3 Kerberos 有关的参数设定!因为我们没有 Kerberos 主机,所以底下不用设定!
#KerberosAuthentication no
#KerberosOrLocalPasswd yes
#KerberosTicketCleanup yes
#KerberosTgtPassing no
 
# 4.4 底下是有关在 X-Window 底下使用的相关设定!
X11Forwarding yes
#X11DisplayOffset 10
#X11UseLocalhost yes
# 4.5 登入后的项目:
PrintMotd no              # 登入后是否显示出一些信息呢?例如上次登入的时间、地点等
              # 等,预设是 yes ,但是,如果为了安全,可以考虑改为 no
PrintLastLog yes        # 显示上次登入的信息!可以啊!预设也是 yes
KeepAlive yes        # 一般而言,如果设定这项目的话,那么 SSH Server 会传送
              # KeepAlive 的讯息给 Client 端,以确保两者的联机正常!
              # 在这个情况下,任何一端死掉后, SSH 可以立刻知道!而不会
              # 有僵尸程序的发生!
UsePrivilegeSeparation yes # 使用者的权限设定项目!就设定为 yes 吧!
MaxStartups 10        # 同时允许几个尚未登入的联机画面?当我们连上 SSH
              # 但是尚未输入密码时,这个时候就是我们所谓的联机画面啦!
              # 在这个联机画面中,为了保护主机,所以需要设定最大值,
              # 预设最多十个联机画面,而已经建立联机的不计算在这十个当中
# 4.6 关于使用者抵挡的设定项目:
DenyUsers *         # 设定受抵挡的使用者名称,如果是全部的使用者,那就是全部
              # 挡吧!若是部分使用者,可以将该账号填入!例如下列!
DenyUsers test
DenyGroups test        # DenyUsers 相同!仅抵挡几个群组而已!
# 5. 关于 SFTP 服务的设定项目!
Subsystem       sftp    /usr/lib/ssh/sftp-server
 

 

 

你可能感兴趣的:(sshkey,viong)