遠程訪問及控制

標題 遠程訪問及控制

*學會構建SSH遠程登陸服務
*學會使用SSH客戶端工具
*學會編寫TCP Wrappers訪問策略

linux

實驗環境:
開啓2臺linux虛擬機
算法

  1. linux-1 openssh服務器
    僅主機 192.168.10.1
    nat 200.1.1.1

2.linux-2 外網客戶端
nat 200.1.1.10
shell

3.windows 主機 內網客戶機
僅主機 192.168.10.10
數據庫

OpenSSH服務器

1.SSH協議(安全通道協議)
*爲客戶機提供安全的Shell環境,用於遠程管理(生產環境多用),遠程登陸、遠程雙向複製scp,遠程文件傳輸sftp;安全性高。
*默認端口:TCP 22

vim

2.OpenSSH
*服務名稱:sshd
*服務端主程序:/usr/sbin/sshd
*服務端配置文件:/etc/ssh/sshd_config


windows

爲了提升安全性,可作下列配置

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 ***禁用反向解析,提升服務器響應速度(添加)



app

[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 客戶機

  1. 在客戶機中建立密鑰對
    *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

  1. 在服務器中導入公鑰文本
    *將公鑰文本添加至目標用戶的公鑰庫
    *默認公鑰庫位置:~/.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個文件都作配置

相關文章
相關標籤/搜索