標題 遠程訪問及控制
*學會構建SSH遠程登錄服務
*學會使用SSH客戶端工具
*學會撰寫TCP Wrappers訪問策略
實驗環境:
開啟2臺linux虛擬機
- linux-1 openssh服務器
僅主機 192.168.10.1
nat 200.1.1.1
2.linux-2 外網客戶端
nat 200.1.1.10
3.windows 主機 內網客戶機
僅主機 192.168.10.10
OpenSSH服務器
1.SSH協議(安全通道協議)
*為客戶機提供安全的Shell環境,用于遠程管理(生產環境多用),遠程登錄、遠程雙向復制scp,遠程檔案傳輸sftp;安全性高,
*默認埠:TCP 22
2.OpenSSH
*服務名稱:sshd
*服務端主程式:/usr/sbin/sshd
*服務端組態檔:/etc/ssh/sshd_config
為了提高安全性,可做下列配置
3,服務監聽選項
*埠號、協議版本、監聽IP地址
*禁用反向決議
[root@localhost ~]# cp /etc/ssh/sshd_config /etc/ssh/sshd_config.bak
[root@localhost ~]# vi /etc/ssh/sshd_config
……
17 Port 22 ***可更改埠
18 Protocol 2 ***版本2比版本1安全性高
20 ListenAddress 192.168.10.1 ***偵聽地址
116 UseDNS no ***禁用反向決議,提高服務器回應速度(添加)
[root@localhost ~]# systemctl restart sshd
驗證:外網客戶機 200.1.1.2
[root@localhost ~]# ssh root@192.168.10.1
[root@localhost ~]# ssh root@200.1.1.1
ssh: connect to host 200.1.1.1 port 22: Connection refused
如果要實作可訪問,openssh服務器添加偵聽地址
ListenAddress 200.1.1.1
4.用戶登錄控制
*禁止root用戶、空密碼用戶—(普通用戶登錄–>切換到root)
*登錄時間、重試次數
*AllowUsers、DenyUsers
[root@localhost ~]# useradd tom
[root@localhost ~]# useradd jack
[root@localhost ~]# echo “123” |passwd --stdin tom
更改用戶 tom 的密碼 ,
passwd:所有的身份驗證令牌已經成功更新,
[root@localhost ~]# echo “123” |passwd --stdin jack
更改用戶 jack 的密碼 ,
passwd:所有的身份驗證令牌已經成功更新,
[root@localhost ~]# vi /etc/ssh/sshd_config
……
40 PermitRootLogin no *** 禁止root用戶
66 PermitEmptyPasswords no ***禁止空密碼用戶
39 LoginGraceTime 2m ***登錄驗證時間2分鐘
42 MaxAuthTries 6 ***最大重試次數
……
末尾添加:
AllowUsers tom jack@200.1.1.10 ***允許tom登錄;允許jack只能在此地址登錄
*可設定AllowUsers和DenyUsers,【不要與DenyUsers同時用】
[root@localhost ~]# systemctl restart sshd
驗證:允許tom登錄;允許jack只能在此地址登錄
5.登錄驗證物件
*服務器中的本地用戶賬號—例:tom
6.登錄驗證方式
*密碼驗證:核對用戶名、密碼是否匹配—安全性弱,易被攻擊
*密鑰對驗證:核對客戶的私鑰、服務端公鑰是否匹配
— 客戶機創建秘鑰對(公鑰、私鑰)–>公鑰存放到服務器
— 密碼、秘鑰同時啟用,優先使用密鑰
—公鑰庫檔案可保存多個公鑰
[root@localhost ~]# vi /etc/ssh/sshd_config
……
【啟用密碼驗證、密鑰對驗證、指定公鑰庫位置】
PasswordAuthentication yes ***密碼驗證
PubkeyAuthentication yes ** 秘鑰對驗證
AuthorizedKeysFile .ssh/authorized_keys 指定公鑰庫資料檔案位置
使用SSH客戶端程式
1.ssh命令 —— 遠程安全登錄 【埠選項:-p 22】
*格式:ssh user@host
2.scp命令 —— 遠程安全復制
*格式1:scp user@host:file1 file2
*格式2:scp file1 user@host:file2
3.sftp命令 —— 安全FTP上下載
格式:sftp user@host
4.Xshell
*Windows下一款功能非常強大的安全終端模擬軟體
linux-1:
[root@localhost ~]# cp /etc/ssh/sshd_config.bak /etc/ssh/sshd_config
cp:是否覆寫"/etc/ssh/sshd_config"? y
[root@localhost ~]# systemctl restart sshd
su - tom
$ touch tom.txt
linux-2:
[root@localhost ~]# useradd zhangsan
[root@localhost ~]# useradd lisi
[root@localhost ~]# echo “123” |passwd --stdin zhangsan
[root@localhost ~]# echo “123” |passwd --stdin lisi
[root@localhost ~]# su - lisi
[lisi@localhost ~]$ touch lisi.txt
[lisi@localhost ~]$ ssh tom@192.168.10.1
The authenticity of host ‘192.168.10.1 (192.168.10.1)’ can’t be established.
ECDSA key fingerprint is SHA256:T+Kaqay4bTpIR3BBJ1x8EXaE3ukPjDMGEqMLPAaqaQg.
ECDSA key fingerprint is MD5:3e:1d:ef:74:ae:15:bb:3f:f1:cc:53:07:64:e2:ef:f9.
—根據服務端發送過來的RSA密鑰輸入‘yes’驗證
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.10.1’ (ECDSA) to the list of known hosts.
—輸入tom密碼
tom@192.168.10.1’s password:
Last login: Thu Oct 25 14:34:00 2018
[tom@localhost ~]$ pwd
/home/tom
[tom@localhost ~]$ ls
tom.txt
[tom@localhost ~]$ exit
登出
Connection to 192.168.10.1 closed.
[lisi@localhost ~]$ scp tom@192.168.10.1:tom.txt tom.txt
tom@192.168.10.1’s password:
tom.txt 100% 0 0.0KB/s 00:00
[lisi@localhost ~]$ scp lisi.txt tom@192.168.10.1:lisi.txt
tom@192.168.10.1’s password:
lisi.txt 100% 0 0.0KB/s 00:00
[lisi@localhost ~]$ ssh tom@192.168.10.1 ls
tom@192.168.10.1’s password:
lisi.txt
tom.txt
[lisi@localhost ~]$ touch test1.txt
[lisi@localhost ~]$ sftp tom@192.168.10.1
tom@192.168.10.1’s password:
Connected to 192.168.10.1.
sftp> lls
lisi.txt test1.txt tom.txt
sftp> ls
lisi.txt tom.txt
sftp> put test1.txt
Uploading test1.txt to /home/tom/test1.txt
test1.txt 100% 0 0.0KB/s 00:00
sftp> ls
lisi.txt test1.txt tom.txt
sftp> bye
構建密鑰對驗證的SSH體系(ssh信任)
1.整體實作程序
原理;由客戶端的用戶zhangsan、lisi在本地創建密鑰對
匯入到服務端用戶tom的公鑰資料庫(然后就可以在客戶端zhangsan、lisi登錄的情況下ssh到服務器tom用戶了)
第一步:創建密鑰對(zhangsan的密鑰對,在Linux客戶機的…/.ssh里面)
私鑰檔案:id_rsa
公鑰檔案:id_rsa.pub
第二步:上傳公鑰檔案 id_rsa.pub
第三步:匯入公鑰資訊()
公鑰庫檔案:~/.ssh/authorized_keys
第四步:使用密鑰對驗證方式【以服務端的用戶tom的身份進行登錄】
linux-1
[tom@localhost ~]$ su -
[root@localhost ~]# vim /etc/ssh/sshd_config
43 PubkeyAuthentication yes
47 AuthorizedKeysFile .ssh/authorized_keys
[root@localhost ~]# systemctl restart sshd
linux-2 客戶機
- 在客戶機中創建密鑰對
*ssh-keygen命令
*可用的加密演算法:escda或DSA
[lisi@localhost ~]$ su -
[root@localhost ~]# su - zhangsan
[zhangsan@localhost ~]$ ll .ssh
ls: 無法訪問.ssh: 沒有那個檔案或目錄
[zhangsan@localhost ~]$ ssh-keygen -t ecdsa
Generating public/private ecdsa key pair.
Enter file in which to save the key (/home/zhangsan/.ssh/id_ecdsa): 默認位置
Created directory ‘/home/zhangsan/.ssh’.
Enter passphrase (empty for no passphrase): 默認回車
Enter same passphrase again: 默認回車
Your identification has been saved in /home/zhangsan/.ssh/id_ecdsa.
Your public key has been saved in /home/zhangsan/.ssh/id_ecdsa.pub.
The key fingerprint is:
SHA256:byF6T/5YXG/9D9I5xqcfiI8isugc6z5BNyHQkTohaoI zhangsan@localhost.localdomain
The key’s randomart image is:
±–[ECDSA 256]—+
| .o.o |
|… + . |
|+ o . . |
|E+ . o |
|o o . . S . . |
| . . o o.+.o.|
| … . . +.+.B.=|
| …+. o * ooo *o|
| +B…o . =o…=|
±—[SHA256]-----+
[zhangsan@localhost ~]$ ls -lh .ssh/id_ecdsa*
-rw------- 1 zhangsan zhangsan 227 10月 25 14:45 .ssh/id_ecdsa
-rw-r–r-- 1 zhangsan zhangsan 192 10月 25 14:45 .ssh/id_ecdsa.pub
方法1:
?2. 將公鑰檔案上傳至服務器
*任何方式均可(共享、FTP、Email、SCP、……)
[zhangsan@localhost ~]$ scp .ssh/id_ecdsa.pub tom@192.168.10.1:zhangsan.key
- 在服務器中匯入公鑰文本
*將公鑰文本添加至目標用戶的公鑰庫
*默認公鑰庫位置:~/.ssh/authorized_keys
SSH對公鑰、私鑰的權限和所有權的要求是非常嚴格的
— .ssh 目錄權限 700
— .ssh/authorized_keys 權限644
— .ssh/id_rsa 私鑰檔案權限必須是600
[zhangsan@localhost ~]$ ssh tom@192.168.10.1
[tom@localhost ~]$ ll .ssh
【tom的家目錄本來沒有authorized_keys】
[tom@localhost ~]$ mkdir .ssh
[tom@localhost ~]$ touch .ssh/authorized_keys
[tom@localhost ~]$ chmod 700 .ssh
[tom@localhost ~]$ chmod 644 .ssh/authorized_keys
[tom@localhost ~]$ cat zhangsan.key >> .ssh/authorized_keys
---選擇‘>>’,公鑰庫可存放多個用戶的公鑰
[tom@localhost ~]$ cat .ssh/authorized_keys
—庫中==后為宣告
[zhangsan@localhost ~]$ ls -lh .ssh/
總用量 12K
-rw------- 1 zhangsan zhangsan 227 10月 25 14:45 id_ecdsa
-rw-r–r-- 1 zhangsan zhangsan 192 10月 25 14:45 id_ecdsa.pub
-rw-r–r-- 1 zhangsan zhangsan 174 10月 25 14:49 known_hosts
[zhangsan@localhost ~]$ ls -ldh .ssh/
drwx------ 2 zhangsan zhangsan 61 10月 25 14:49 .ssh/
[zhangsan@localhost ~]$ ssh tom@192.168.10.1
方法2:*驗證密碼后,會將公鑰自動添加到目標主機user宿主目錄下的.ssh/authorized_keys檔案結尾
*ssh-copy-id -i 公鑰檔案 user@host
[zhangsan@localhost ~]$ exit
登出
[root@localhost ~]# su - lisi
[lisi@localhost ~]$ ssh-keygen -t ecdsa
[lisi@localhost ~]$ ssh-copy-id -i .ssh/id_ecdsa.pub tom@192.168.10.1
— -i 指定公鑰檔案
[lisi@localhost ~]$ ssh tom@192.168.10.1
[tom@localhost ~]$ cat .ssh/authorized_keys
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBAZydP4XKVX6GGR9DLcKIW/nYZY+sLenMYnIaOrPrRhZ14tfGg1c+7JOWkd2yEL+VaXrhkRpjPCdOJfXoHT4vuU= zhangsan@localhost.localdomain
ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE/gMz/L6FSUJC7fSy/0/cW/8+DB4QSSvxLHhmdpIspy4EvQ5hzvCI1Q4N0He6kIf82U0rv3SJEofdsRcD7/8Yg= lisi@localhost.localdomain
TCP Wrappers概述
1.TCP Wrappers機制
【客戶機的網路訪問請求】
1)對訪問請求進行過濾控制
代為監聽埠21
代為監聽埠23
代為監聽埠110
代為監聽埠143
2)呼叫相應的網路程式
vsftpd
telnet
ipop3
imap
2.保護機制的實作方式
方式1:通過tcpd主程式對其他服務程式進行包裝
方式2:由其他服務程式呼叫libwrap.so.*鏈接庫
3.訪問控制策略的組態檔
/etc/hosts.allow
/etc/hosts.deny
TCP Wrappers(tcp封套)策略應用
在 wrappers 下進行訪問控制的通常有 telnet、ssh、sendmail、ftp 包、pop3 和 stunnel,
主組態檔:
/etc/hosts.allow
/etc/hosts.deny
*執行程式: tcpd
共享鏈接庫檔案: libwrap.so.
–這種方式更廣泛
查看程式的共享庫 ldd
ldd /usr/sbin/sshd |grep “libwrap”
1.設定訪問控制策略
*策略格式:服務串列:客戶機地址串列
2.服務串列 ——
*多個服務以逗號分隔,ALL 表示所有服務
3.客戶機地址串列
*多個地址以逗號分隔,ALL表示所有地址
*允許使用通配符 ? 和 *
*網段地址,如 192.168.4. 或者 192.168.4.0/255.255.255.0
*區域地址,如 .benet.com
—匹配域中所有主機
4.策略的應用順序
*先檢查hosts.allow,找到匹配則允許訪問
*否則再檢查hosts.deny,找到則拒絕訪問
*若兩個檔案中均無匹配策略,則默認允許訪問
5.策略應用示例
*僅允許從以下地址訪問sshd服務
&主機192.168.10.3
&網段192.168.2.0/24
*禁止其他所有地址訪問受保護的服務
[root@localhost ~]# ll /etc/hosts.*
-rw-r–r--. 1 root root 370 6月 7 2013 /etc/hosts.allow
-rw-r–r--. 1 root root 460 6月 7 2013 /etc/hosts.deny
[root@localhost ~]# vim /etc/hosts.allow
sshd:192.168.10.*,200.1.1.3
[root@localhost ~]# vi /etc/hosts.deny
sshd:ALL
驗證:在客戶機ssh登錄驗證
[root@localhost ~]# ssh root@200.1.1.1
更改hosts.allow為sshd:192.168.10.*,200.1.1.2繼續驗證
總結:配置策略要2個檔案都做配置
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/241448.html
標籤:其他
