2.1 SSH客户端与OpenSSH

SSH

    SSH 为 Secure Shell 的缩写,由 IETF 的网络小组(Network Working Group)所制定;SSH 为建立在应用层基础上的安全协议。SSH 是目前较可靠,专为远程登录会话和其他网络服务提供安全性的协议。利用 SSH 协议可以有效防止远程管理过程中的信息泄露问题。SSH最初是UNIX系统上的一个程序,后来又迅速扩展到其他操作平台。SSH在正确使用时可弥补网络中的漏洞。

    SSH的功能

    传统的网络服务程序,如:ftp、pop和telnet在本质上都是不安全的,因为它们在网络上用明文传送口令和数据,别有用心的人非常容易就可以截获这些口令和数据。而且,这些服务程序的安全验证方式也是有其弱点的, 就是很容易受到“中间人”(man-in-the-middle)这种方式的攻击。所谓“中间人”的攻击方式, 就是“中间人”冒充真正的服务器接收你传给服务器的数据,然后再冒充你把数据传给真正的服务器。服务器和你之间的数据传送被“中间人”一转手做了手脚之后,就会出现很严重的问题。通过使用SSH,你可以把所有传输的数据进行加密,这样"中间人"这种攻击方式就不可能实现了,而且也能够防止DNS欺骗和IP欺骗。使用SSH,还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的速度。SSH有很多功能,它既可以代替Telnet,又可以为FTP、PoP、甚至为PPP提供一个安全的"通道"。

    SSH的验证方式

第一种级别:基于口令的安全验证

    只要你知道自己帐号和口令,就可以登录到远程主机。所有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的攻击。

第二种级别:基于密匙的安全验证

    需要依靠密匙,也就是你必须为自己创建一对密匙,并把公用密匙放在需要访问的服务器上。如果你要连接到SSH服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。服务器收到请求之后,先在该服务器上你的主目录下寻找你的公用密匙,然后把它和你发送过来的公用密匙进行比较。如果两个密匙一致,服务器就用公用密匙加密“质询”(challenge)并把它发送给客户端软件。客户端软件收到“质询”之后就可以用你的私人密匙解密再把它发送给服务器。
用这种方式,你必须知道自己密匙的口令。但是,与第一种级别相比,第二种级别不需要在网络上传送口令。

第二种级别不仅加密所有传送的数据,而且“中间人”这种攻击方式也是不可能的(因为他没有你的私人密匙)。但是整个登录的过程可能需要10秒。

    SSH的结构

传输层协议 [SSH-TRANS]

    提供了服务器认证,保密性及完整性。此外它有时还提供压缩功能。 SSH-TRANS 通常运行在TCP/IP连接上,也可能用于其它可靠数据流上。 SSH-TRANS 提供了强力的加密技术、密码主机认证及完整性保护。该协议中的认证基于主机,并且该协议不执行用户认证。更高层的用户认证协议可以设计为在此协议之上。

用户认证协议 [SSH-USERAUTH]

    用于向服务器提供客户端用户鉴别功能。它运行在传输层协议 SSH-TRANS 上面。当SSH-USERAUTH 开始后,它从低层协议那里接收会话标识符(从第一次密钥交换中的交换哈希H )。会话标识符唯一标识此会话并且适用于标记以证明私钥的所有权。 SSH-USERAUTH 也需要知道低层协议是否提供保密性保护。

连接协议 [SSH-CONNECT]

    将多个加密隧道分成逻辑通道。它运行在用户认证协议上。它提供了交互式登录话路、远程命令执行、转发 TCP/IP 连接和转发 X11 连接。

OpenSSH

SSH 基于 C/S 结构

    Server端使用sshd提供服务,默认监听在22端口,并且后台运行

[root@CentOS7 ~]# ss -ntlp | grep sshd
LISTEN     0      128          *:22                       *:*                   users:(("sshd",pid=1046,fd=3))
LISTEN     0      128         :::22                      :::*                   users:(("sshd",pid=1046,fd=4))

查看SSH客户端的配置文件

[root@CentOS7 pki]#⮀cat /etc/ssh/ssh_config
#	$OpenBSD: ssh_config,v 1.30 2016/02/20 23:06:23 sobrado Exp $

# This is the ssh client system-wide configuration file.  See
# ssh_config(5) for more information.  This file provides defaults for
# users, and the values can be changed in per-user configuration files
# or on the command line.

# Configuration data is parsed as follows:
#  1. command line options
#  2. user-specific file
#  3. system-wide file
# Any configuration value is only changed the first time it is set.
# Thus, host-specific definitions should be at the beginning of the
# configuration file, and defaults at the end.

# Site-wide defaults for some commonly used options.  For a comprehensive
# list of available options, their meanings and defaults, please see the
# ssh_config(5) man page.

# Host *
#   ForwardAgent no
#   ForwardX11 no
#   RhostsRSAAuthentication no
#   RSAAuthentication yes
#   PasswordAuthentication yes
#   HostbasedAuthentication no
#   GSSAPIAuthentication no
#   GSSAPIDelegateCredentials no
#   GSSAPIKeyExchange no
#   GSSAPITrustDNS no
#   BatchMode no
#   CheckHostIP yes
#   AddressFamily any
#   ConnectTimeout 0
#   StrictHostKeyChecking ask   #第一次是否询问主机
#   IdentityFile ~/.ssh/identity
#   IdentityFile ~/.ssh/id_rsa
#   IdentityFile ~/.ssh/id_dsa
#   IdentityFile ~/.ssh/id_ecdsa
#   IdentityFile ~/.ssh/id_ed25519
#   Port 22              #默认链接端口
#   Protocol 2
#   Cipher 3des
#   Ciphers aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc
#   MACs hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160
#   EscapeChar ~
#   Tunnel no
#   TunnelDevice any:any
#   PermitLocalCommand no
#   VisualHostKey no
#   ProxyCommand ssh -q -W %h:%p gateway.example.com
#   RekeyLimit 1G 1h
#
# Uncomment this if you want to use .local domain
# Host *.local
#   CheckHostIP no         #是否检查主机IP可信

Host *
	GSSAPIAuthentication yes
# If this option is set to yes then remote X11 clients will have full access
# to the original X11 display. As virtually no X11 client supports the untrusted
# mode correctly we set this to yes.
	ForwardX11Trusted yes
# Send locale-related environment variables
	SendEnv LANG LC_CTYPE LC_NUMERIC LC_TIME LC_COLLATE LC_MONETARY LC_MESSAGES
	SendEnv LC_PAPER LC_NAME LC_ADDRESS LC_TELEPHONE LC_MEASUREMENT
	SendEnv LC_IDENTIFICATION LC_ALL LANGUAGE
	SendEnv XMODIFIERS

    ssh客户端

    语法:ssh [选项] [用户@] host [命令]

[root@CentOS7 pki]# ssh -b 192.168.30.6 192.168.30.8
root@192.168.30.8's password: 
Last login: Tue May 22 09:38:31 2018 from 192.168.30.1
[root@CentOS6 ~]# exit 
logout
Connection to 192.168.30.8 closed.

    选项

-p:远程服务器监听的端口
-b:指定连接的源IP
-v:调试模式
-C:压缩方式
-X:支持x11转发
-t:强制伪tty分配

ssh -t remoteserver1 ssh remoteserver2

    当用户远程连接ssh服务器时,会复制ssh服务器 /etc/ssh/ssh_host*key.pub (CentOS7 默认是ssh_host_ecdsa_key.pub)文件中的公钥到客户机的 ~./ssh/know_hosts 中。下次连接时,会自动匹配相应私钥,不能匹配,将拒绝连接。

基于用户口令的验证方式


基于密钥的验证方式


实现基于key的验证

1、在客户端生成密钥对

[root@CentOS7 .ssh]# ssh-keygen -t rsa   使用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 /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:xrg3GeMda+MI1InqgZwb/ok5x/h+Y5ZJ4A07MYwTjrs root@CentOS7.miriam
The key's randomart image is:
+---[RSA 2048]----+
|                 |
|  .              |
| o +             |
|. + *  = .       |
| . o B+ S .      |
|.. o+oo+ = o     |
| .=ooooo= =      |
|E.o*ooBo = .     |
|  =**+ .. .      |
+----[SHA256]-----+
[root@CentOS7 .ssh]# ll
total 12
-rw------- 1 root root 1675 May 22 17:00 id_rsa       #私钥
-rw-r--r-- 1 root root  401 May 22 17:00 id_rsa.pub   #公钥
-rw-r--r-- 1 root root  790 May 19 15:50 known_hosts

2、将公钥发送给服务器端

[root@CentOS7 .ssh]# ssh-copy-id 192.168.30.8   #ssh-copy-id会自动复制本机的ssh公钥到对端主机
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
/usr/bin/ssh-copy-id: INFO: attempting 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@192.168.30.8's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh '192.168.30.8'"
and check to make sure that only the key(s) you wanted were added.

3、验证结果
[root@CentOS7 .ssh]# ssh 192.168.30.8
Last login: Tue May 22 15:57:36 2018 from 172.20.111.54
[root@CentOS6 ~]#               #不需要输入用户口令便可以连接

4、修改私钥口令

[root@CentOS7 .ssh]#?ssh-keygen -p
Enter file in which the key is (/root/.ssh/id_rsa):       
Enter new passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved with the new passphrase.

实现批量基于Key验证

[root@CentOS7 bin]#⮀cat sshKey.sh copyKey.exp    #脚本实现意见生成密钥并复制到目标主机
#!/bin/bash
#
#********************************************************************
#Author:		M30_Miriam
#Tel:                   13803833736
#Date: 			2018-05-22
#FileName:		sshKey.sh
#Copyright (C): 	2018 All rights reserved
#********************************************************************
PassWord=dengniaiwo123
read -p "Please input a host: " HOST
read -p "put a user: " USER
if [ $USER == root ] ;then
	USER=/root
else
	USER=/home/$USER
fi
IP="192.168.30.$HOST"
if [ -e $USER/.ssh/id_rsa.pub ] ;then
	./copyKey.exp $IP $PassWord
	echo "wancheng"
else
	if [ ! -e $USER/.ssh ] ;then
		mkdir $USER/.ssh
	fi
	ssh-keygen -f $USER/.ssh/id_rsa -P ""
	./copyKey.exp $IP $PassWord
	echo "wancheng "
fi
#!/usr/bin/expect
set IP [lindex $argv 0]
set PassWord [lindex $argv 1]
spawn ssh-copy-id $IP
expect {
	"yes/no" { send "yes\n"; exp_continue }
	"password" { send "$PassWord\n" }
}
interact

想要完成多主机基于Key验证,方法有二:

 方法一:将所有主机包括本主机的公钥复制到一台主机上,然后再将这台主机上的 authorized_keys 文件复制到其他所有主机上,推荐。

 方法二:所有主机使用同一对密钥,因为基于 Key 验证,破解一台主机后所有主机也可以免密登陆,所以破解难度与方法一相同。

SSH应用

    scp命令用于基于ssh登陆进行安全的远程文件全量拷贝。

scp root@192.168.30.74:/root/.ssh/authorized_keys root@192.168.30.69:/root/.ssh/   #将远程74主机上的文件复制到远程主机69上
root@192.168.30.69's password: 
authorized_keys                                                                                                                                            100% 1203     2.0MB/s   00:00    
Connection to 192.168.30.74 closed.

    语法:scp [选项] [用户@]网络地址:/远程文件 本地目录
              scp [选项] 本地文件 [用户@]网络地址:/远程目录 
              scp [选项] [用户@]网络地址:/远程文件 [用户@]网络地址:/远程目录

    选项

-C: 压缩数据流
-r: 递归复制
-p: 保持原文件的属性信息
-q: 静默模式
-P PORT: 指明remote host的监听的端口

    rsync命令用于基于ssh登陆进行安全的远程文件拷贝。

    语法:rsync [选项] 本地文件 网络地址:远程文件

              rsync [选项] 网络地址:远程文件 本地文件

              rsync [选项] 本地文件/网络地址:远程文件

root@CentOS7 rsync]# tree    #源主机上的文件
.
├── dir1
│   ├── 9x9table.sh     #目标主机上有删除修改
│   ├── chessboard.sh   #目标主机没有此文件
│   ├── copycmd.sh      #目标主机有增加修改
│   ├── Ddos.sh
│   └── dir3            #目标主机没有这个目录
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── isosceles_triangle.sh
    ├── MageAwk.sh      #目标主机没有此文件
    ├── matrix.sh
    └── md5break.sh

3 directories, 10 files
[root@CentOS7 rsync]# ssh 192.168.30.75 "tree /data/rsync/"   #目标主机上的文件
/data/rsync/
├── dir1
│   ├── 9x9table.sh
│   ├── copycmd.sh
│   └── Ddos.sh
└── dir2
    ├── isosceles_triangle.sh
    ├── matrix.sh
    └── md5break.sh

2 directories, 6 files    #实验环境搭建如上

    注意:本地文件如果是文件夹的话,目录后有没有"/"是不同的。例如: 

rsync -av /etc/ 192.168.30.74:/data   #会只复制etc下的文件,类似于cp -r
rsync -av /etc 192.168.30.74:/data    #会连同etc目录一同复制,类似于cp --parents

    选项

-n 模拟复制过程
-v 显示详细过程
-r 递归复制目录树
-p 保留权限
-t 保留时间戳
-g 保留组信息
-o 保留所有者信息
-l 将软链接文件本身进行复制(默认)
-L 将软链接文件指向的文件复制
-a 存档,相当于–rlptgoD,但不保留ACL(-A)和SELinux属性(-X)

--existing 只同步目标端已有的文件    --ignore-existing 只复制目标端不存在的文件

[root@CentOS7 rsync]# rsync -av --existing /data/rsync/ 192.168.30.75:/data/rsync
sending incremental file list
./
dir1/    #同步dir1
dir1/9x9table.sh   #复制到与源主机同步
dir1/Ddos.sh
dir1/copycmd.sh
dir2/    #同步dir2
dir2/isosceles_triangle.sh
dir2/matrix.sh
dir2/md5break.sh
#并没有同步dir1/chessboard.sh,dir2/MageAwk.sh,dir3/*
sent 1656 bytes  received 180 bytes  1224.00 bytes/sec
total size is 8120  speedup is 4.42
[root@CentOS7 rsync]# rsync -av --ignore-existing /data/rsync/ 192.168.30.75:/data/rsync
sending incremental file list   #并没有同步目标端存在且被修改的文件
dir1/chessboard.sh
dir1/dir3/
dir1/dir3/Hanoi_Tower.sh
dir1/dir3/guess.sh
dir2/MageAwk.sh

sent 5074 bytes  received 94 bytes  3445.33 bytes/sec
total size is 8120  speedup is 1.57
[root@CentOS7 rsync]# ssh 192.168.30.75 "tree /data/rsync"  
/data/rsync
├── dir1
│   ├── 9x9table.sh
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 10 files
--backup 备份文件  --backup-dir= 备份至指定目录
[root@CentOS7 data]# rsync -av --backup --backup-dir=/data/backup/ /data/rsync/ 192.168.30.74:/data/backup/
sending incremental file list
./
dir1/
dir1/9x9table.sh
dir1/Ddos.sh
dir1/chessboard.sh
dir1/copycmd.sh
dir1/dir3/
dir1/dir3/Hanoi_Tower.sh
dir1/dir3/guess.sh
dir2/
dir2/MageAwk.sh
dir2/isosceles_triangle.sh
dir2/matrix.sh
dir2/md5break.sh

sent 8,888 bytes  received 244 bytes  18,264.00 bytes/sec
total size is 8,120  speedup is 0.89
[root@CentOS7 data]#⮀ssh 192.168.30.74 "tree /data/backup"
/data/backup
├── dir1
│   ├── 9x9table.sh
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 10 files
--exclude 排除匹配的文件
[root@CentOS7 rsync]# rsync -av --exclude=*.{txt,bak,log} /data/rsync/ 192.168.30.75:/data/rsync
sending incremental file list    #排除以txt,log,bak为后缀的文件
./
dir1/
dir1/9x9table.sh
dir1/Ddos.sh
dir1/chessboard.sh
dir1/copycmd.sh
dir1/dir3/
dir1/dir3/Hanoi_Tower.sh
dir1/dir3/guess.sh
dir2/
dir2/MageAwk.sh
dir2/isosceles_triangle.sh
dir2/matrix.sh
dir2/md5break.sh

sent 8855 bytes  received 217 bytes  18144.00 bytes/sec
total size is 8120  speedup is 0.90
[root@CentOS7 rsync]# ssh 192.168.30.75 "tree /data/rsync"
/data/rsync
├── dir1
│   ├── 9x9table.sh
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 10 files
--remove-source-files 删除已经同步的源文件
[root@CentOS7 rsync]# rsync -av --exclude=*.{txt,bak,log} --remove-source-files /data/rsync/ 192.168.30.75:/data/rsync
sending incremental file list
./
dir1/
dir1/9x9table.sh
dir1/Ddos.sh
dir1/chessboard.sh
dir1/copycmd.sh
dir1/dir3/
dir1/dir3/Hanoi_Tower.sh
dir1/dir3/guess.sh
dir2/
dir2/MageAwk.sh
dir2/isosceles_triangle.sh
dir2/matrix.sh
dir2/md5break.sh

sent 8855 bytes  received 217 bytes  18144.00 bytes/sec
total size is 8120  speedup is 0.90
[root@CentOS7 rsync]# tree   #已经同步成功的文件已经被删除
.
├── dir1
│   ├── aaa.txt
│   └── dir3
│       └── ccc.bak
└── dir2
    └── bbb.log

3 directories, 3 files
[root@CentOS7 rsync]#⮀ssh 192.168.30.75 "tree /data/rsync"
/data/rsync
├── dir1
│   ├── 9x9table.sh
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 10 files
--delete 删除目标端的不同步文件(多删,少补)
[root@CentOS7 rsync]# tree      #源文件与目标文件都有*.sh,bbb.*
.
├── dir1
│   ├── 9x9table.sh
│   ├── aaa.txt
│   ├── bbb.txt
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── aaa.bak
│       ├── bbb.bak
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── aaa.log
    ├── bbb.log
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 16 files
[root@CentOS7 rsync]# ssh 192.168.30.75 "tree /data/rsync"
/data/rsync                    #目标端少aaa.*
├── dir1                       #     多ccc.*
│   ├── 9x9table.sh
│   ├── bbb.txt
│   ├── ccc.txt
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── bbb.bak
│       ├── ccc.bak
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── bbb.log
    ├── ccc.log
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 16 files
[root@CentOS7 rsync]# rsync -av --delete /data/rsync/ 192.168.30.75:/data/rsync
sending incremental file list    #使用delete选项进行同步
deleting dir1/ccc.txt
deleting dir1/dir3/ccc.bak
deleting dir2/ccc.log
dir1/
dir1/aaa.txt
dir1/bbb.txt
dir1/dir3/
dir1/dir3/aaa.bak
dir1/dir3/bbb.bak
dir2/
dir2/aaa.log
dir2/bbb.log

sent 670 bytes  received 138 bytes  538.67 bytes/sec
total size is 8120  speedup is 10.05
[root@CentOS7 rsync]# ssh 192.168.30.75 "tree /data/rsync"   #目标端与源文件是实现同步
/data/rsync
├── dir1
│   ├── 9x9table.sh
│   ├── aaa.txt
│   ├── bbb.txt
│   ├── chessboard.sh
│   ├── copycmd.sh
│   ├── Ddos.sh
│   └── dir3
│       ├── aaa.bak
│       ├── bbb.bak
│       ├── guess.sh
│       └── Hanoi_Tower.sh
└── dir2
    ├── aaa.log
    ├── bbb.log
    ├── isosceles_triangle.sh
    ├── MageAwk.sh
    ├── matrix.sh
    └── md5break.sh

3 directories, 16 files



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值