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,[email protected],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
[email protected]'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 [email protected]
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
[email protected]'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 [email protected]:/root/.ssh/authorized_keys [email protected]:/root/.ssh/ #將遠程74主機上的文件複製到遠程主機69上
[email protected]'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