Linux安全與加密基礎(二)

Linux安全與加密基礎(二)


  • 常見的加密算法

  • SSL: Openssl與CA認證

  • ssh服務

  • dropbear

  • AIDE

  • sudo


gpg

gpg亦可用於對稱加密與文件檢驗。

文件完整性的兩種實施方式

被安裝的文件
    MD5單向散列
    rpm --verify package_name (or -V)
發行的軟件包文件
    GPG公鑰簽名
    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat*
    rpm --checksig pakage_file_name (or -K)

使用gpg實現對稱加密

gpg的對稱加密算法使用的是 CAST5,

對稱加密一個文件

[root@centos7 /testdir]#gpg -c issue 
[root@centos7 /testdir]#ls     # issue.gpg是加密後的文件
issue  issue.gpg

使用gpg -c加密一個文件時,會彈出一個對話框要求你輸入密碼:

wKiom1fyPbCh5aNWAAASOv_H2dw419.png

wKiom1fyPcHRvHl0AAAqXsSCeg0593.png

解密:gpg加密的文件在本機執行解密時會直接解密而不需要輸入密碼,但在其它機子上時會要求輸入密碼。

本機解密:

[root@centos7 /testdir]#gpg -d issue.gpg > issue.2gpg: keyring `/root/.gnupg/secring.gpg' created
gpg: CAST5 encrypted data
gpg: encrypted with 1 passphrase
gpg: WARNING: message was not integrity protected
[root@centos7 /testdir]#lsissue  issue.2  issue.gpg
[root@centos7 /testdir]#cat issue.2\S
Kernel \r on an \m

[root@centos7 /testdir]

非本機解密:

wKioL1fyPdXC7X1qAABG3gVFKnQ869.png

gpg解密時除了重定向方式,還可以如下解密:

pgp -o file2 -d file.gpg   # -o 必須在前

使用gpg實現非對稱加密

如centos 7要加密一個文件傳給centos 6, 這個過程應該是要拿centos 6的公鑰加密,所以要在centos 6上先生成公鑰。

1.在hostA主機上生成公鑰/私鑰對:gpg --gen-key

此操作會用隨機數生成密鑰對兒,需要移動鼠標或敲擊鍵盤,所以此命令最好在本地終端作:

wKiom1fyPerAUfXbAAFvu8p9QuU903.png

wKioL1fyPfuTL5X2AAC_b3-l9VQ534.png

2.在hostA主機上查看公鑰:gpg--list-key

[root@centos6 ~]#gpg --list-key/root/.gnupg/pubring.gpg
------------------------
pub   2048R/79AC0435 2016-09-25uid                  liansir <admin@liansir>
sub   2048R/040E5B00 2016-09-25
[root@centos6 ~]#

wKiom1fyPhLTOiPeAAAt4mvqSak779.png

在hostA主機上導出公鑰到liansir.pubkey

gpg -a --export -o liansir.pubkey-a 表示base64編碼,這一步是轉換成易讀的編碼

gpg生成的密鑰對在家目錄下的/.gnupg
[root@centos6 ~]#gpg -a --export -o liansir.pubkey # 直接導出到當前目錄
[root@centos6 ~]#ls
anaconda-ks.cfg  Documents  f1           install.log.syslog  Music     Public     VideosDesktop          
Downloads  install.log  liansir.pubkey      Pictures  Templates
[root@centos6 ~]#

3.從hostA主機上覆制公鑰文件到需加密的B主機上

[root@centos6 ~]#scp liansir.pubkey 10.1.1.2:
[email protected]'s password: 
liansir.pubkey                                                      100% 1707     1.7KB/s   00:00    
[root@centos6 ~]#

4.在需加密數據的hostB主機上生成公鑰/私鑰對: gpg --gen-key

[root@centos7 ~]#gpg --gen-key[root@centos7 ~]#gpg --list-key/root/.gnupg/pubring.gpg
------------------------
pub   2048R/0FE97934 2016-09-25uid                  zhizhan <[email protected]>
sub   2048R/E43BDE3E 2016-09-25

5.在hostB主機上導入hostA的公鑰: gpg --import

[root@centos7 ~]#gpg --import liansir.pubkey gpg: key 79AC0435: public key "liansir <admin@liansir>" imported
gpg: Total number processed: 1gpg:               imported: 1  (RSA: 1)
[root@centos7 ~]#[root@centos7 ~]#gpg --list-key/root/.gnupg/pubring.gpg
------------------------
pub   2048R/0FE97934 2016-09-25uid                  zhizhan <[email protected]>
sub   2048R/E43BDE3E 2016-09-25pub   2048R/79AC0435 2016-09-25uid                  liansir <admin@liansir>
sub   2048R/040E5B00 2016-09-25
[root@centos7 ~]#

6.用從hostA主機導入的公鑰,加密hostB主機的文件FILE,生成FILE.gpg

gpg -e -r liansir FILE
[root@centos7 /testdir]#gpg -e -r liansir fstab
gpg: 040E5B00: There is no assurance this key belongs to the named user

pub  2048R/040E5B00 2016-09-25 liansir <admin@liansir>
 Primary key fingerprint: F042 2A48 096B C16A D953  3D2C 8657 6F32 79AC 0435
      Subkey fingerprint: CC15 C943 A52A AF8A 1BE6  8B05 0C9D D197 040E 5B00

It is NOT certain that the key belongs to the person named
in the user ID.  If you *really* know what you are doing,
you may answer the next question with yes.

Use this key anyway? (y/N) y

[root@centos7 /testdir]#ls
fstab  fstab.gpg
[root@centos7 /testdir]#file fstab.gpg 
fstab.gpg: data
[root@centos7 /testdir]#

7.複製加密文件到hostA主機

[root@centos7 /testdir]#scp fstab.gpg 10.1.1.1:/testdir
[email protected]'s password: 
fstab.gpg                                                           100%  812     0.8KB/s   00:00    
[root@centos7 /testdir]#

8.在hostA主機解密文件: gpg -o file -d file.gpg

[root@centos6 /testdir]#gpg -o fstab -d fstab.gpg 
gpg: encrypted with 2048-bit RSA key, ID 040E5B00, created 2016-09-25
      "liansir <admin@liansir>"

wKiom1fyQ4vDudbuAACDDjHW7ss051.png

至此,我們利用gpg工具就完成了一個完整的非對稱加密過程,如果要刪除公鑰與私鑰,如使用如下命令:

gpg --delete-secret-keys liansir
gpg --delete-keys liansir

wKiom1fyQ5-SEOyiAAClPtM4E1c796.png

ssh 服務

SSH 爲 Secure Shell 的縮寫,由 IETF 的網絡小組(Network Working Group)所制定;SSH 爲建立在應用層基礎上的安全協議。SSH是目前較可靠,專爲遠程登錄會話和其他網絡服務提供安全性的協議。利用 SSH 協議可以有效防止遠程管理過程中的信息泄露問題。

ssh的功能

傳統的網絡服務程序,如:ftp、smtp、telnet在本質上都是不安全的,因爲它們在網絡上用明文傳送口令和數據,就是很容易受到“中間人”(man-in-the-middle)的***。所謂“中間人”的***方式,就是“中間人”冒充真正的服務器接收你傳給服務器的數據,然後再冒充你把數據傳給真正的服務器;通過使用SSH,你可以把所有傳輸的數據進行加密,這樣"中間人"這種***方式就很難實現了,而且也能夠防止DNS欺騙和IP欺騙。使用SSH,其傳輸的數據是經過壓縮的,所以可以加快傳輸的速度。故SSH可以爲這些不安全的服務提供一個安全的ssh隧道。

相關知識點:

ssh: secure shell, protocol, 22/tcp, 安全的遠程登錄
    OpenSSH: ssh協議的開源實現
    dropbear:另一個開源實現
SSH協議版本
    v1: 基於CRC-32做MAC,不安全;man-in-middle
    v2:雙方主機協議選擇安全的MAC方式
    基於DH算法做密鑰交換,基於RSA或DSA實現身份認證
兩種方式的用戶登錄認證:
    基於password
    基於key

ssh客戶端

ssh的配置文件在/etc/ssh目錄下,ssh_config爲其客戶端配置文件,sshd_config爲其服務端配置文件。

ssh的使用格式:

格式:ssh[user@]host [COMMAND]
ssh[-l user] host [COMMAND]
-p port:遠程服務器監聽的端口-b:指定連接的源IP
-v:調試模式
-C:壓縮方式
-X: 支持x11轉發
-Y:支持信任x11轉發ForwardX11Trusted yes
-t: 強制僞tty分配ssh-t remoteserver1 sshremoteserver2

修改ssh的默認端口與登陸

ssh服務的默認端口是tcp/22,可在ssh的配置文件下修改其默認端口,而centos 7還得修改selinux的相關配置:

# semanage port -a -t ssh_port_t -p tcp 2222

wKioL1fySFfzsju3AAAmQImwIY4435.png

sshd_config:

wKiom1fySHiS52kUAAA2mzgZqnU397.png

在centos 7上修改ssh的默認端口爲2222,並重啓服務

[root@centos7 ~]#semanage port -a -t ssh_port_t -p tcp 2222
[root@centos7 ~]#systemctl restart sshd

wKioL1fySI3gt4v5AABodwxfKvA628.png

現在讓其它主機ssh連接時則必須要加相應的端口:-p portnum

[root@centos6 ~]#ssh 10.1.1.2 -p [email protected]'s password: 
Last login: Sun Sep 25 03:48:36 2016 from 10.1.252.68[root@centos7 ~]#

wKioL1fySJ7i-SJ3AAAtPlCvNig650.png

在ssh遠程連接其它主機時,在當前用戶的家目錄下的.ssh目錄裏面,會生成一個known_hosts文件,這個文件記錄了ssh的登錄信息,遠程主機的公鑰,即遠程主機的/etc/ssh/ssh_host_rsa_key.pub文件。

wKiom1fySK3gw7o9AABmmoBB_t0557.png

wKioL1fySMvTpZijAABwe--Any0705.png

此處記錄公鑰的目的是:在客戶機在下次連接服務器時,因爲服務器的公鑰已經被複制到客戶機的known_hosts文件中了,故在下次連接服務器時,服務器會用其私鑰作一個數字簽名發到客戶機,而客戶機則用之前保存的對方公鑰解密,從而確定對方的身份!

小結:

允許實現對遠程系統經驗證地加密安全訪問
當用戶遠程連接ssh服務器時,會複製ssh服務器/etc/ssh/ssh_host*key.pub
(centos7.0默認是ssh_host_ecdsa_key.pub)文件中的公鑰到客戶機的~./ssh/kno
w_hosts中。下次連接時,會比較兩處是否有不同。

X協議轉發

所有圖形化應用程序都是X客戶程序,x11能夠通過tcp/ip連接遠程X服務器,數據沒有加密機,但是它通過ssh連接隧道安全進行。由於涉及到了圖形界面程序,我們需要在本機上進行X協議轉發。如:

ssh-X user@remotehostgeditsystem-config-users

wKiom1fySOTST8DDAACPcc7EEtE207.png

wKioL1fySPKARVJKAAFi2BLEUro069.png

如果我們想在通過遠程連接工具,直接打開遠程主機的桌面,Xmanager Enterprise提供了一個xstart的工具就可以做到。

wKiom1fySQLzgy2AAACaJDR3mh8041.png

wKioL1fySRjAbrOgAABla9SbJ3E864.png

點擊運行會提示輸入密碼:接着您就會看到遠程主機的桌面了!

wKiom1fySSjhTLjCAALRDx0vhh4032.png-wh_50

退出:system--> logout

繞過防火牆

ssh -t 是一個有意思的工具,可實現防火牆的跳轉,一步到位跳轉,強制分配“僞終端”。

場景:

wKioL1fySTmg_kKPAAAiPPYqX4I367.png

A: 互聯網主機:10.1.1.2,centos 7
B,C: 局域網主機;10.1.1.1,cetntos 6 	10.1.1.3,centos 5防火牆:

A 無法訪問C, 但A可以訪問B,
C: iptables -A INPUT -s 10.1.1.2 -j REJECT

如果我們要使A成功訪問C, 可如下操作:

操作:A ssh 到B, 在B上再ssh到C, 從而A成功訪問C!

[[email protected] ~]#iptables -A INPUT -s 10.1.1.2 -j REJECT[[email protected] ~]#
[root@centos7 ~]#ssh 10.1.1.3 -b 10.1.1.2
ssh: connect to host 10.1.1.3 port 22: Connection refused
[root@centos7 ~]#

wKiom1fySV6S5AuMAABSCwfcXGg997.png

wKiom1fySXDx3-DlAAA5m3HKrDY826.png

其實我們可“一步到位”:

wKioL1fySYKyYpH9AACZPrgG7mE968.png

[root@centos7 ~]#ssh -t 10.1.1.1 ssh [email protected]'s password: 
[email protected]'s password: 
Last login: Sun Sep 25 18:36:22 2016 from 10.1.250.37[[email protected] ~]#exit
logout
Connection to 10.1.1.3 closed.
Connection to 10.1.1.1 closed.[root@centos7 ~]#

在centos 5上依然還是顯示centos 6連接的它,而非centos 7.

基於key認證

當我們需要管理一大堆機器時,遠程連接需要記住每個機器的密碼,而且每次都得輸入,不方便效率不高,基於key認證也幫助我們解決此問題!

1.在客戶端生成密鑰對

[root@centos7 ~]#ssh-keygen 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:
eb:00:81:3d:6f:c2:90:3b:8d:22:42:66:0c:26:44:33 root@liansirThe key's randomart image is:
+--[ RSA 2048]----+
|=E               |
|= o+             |
| =+ +            |
|+  * +           |
|+ + = o S        |
|o. . +   .       |
|      . .        |
|       o         |
|        .        |
+-----------------+
[root@centos7 ~]#[root@centos7 ~]#cd .ssh[root@centos7 ~/.ssh]#lsid_rsa  id_rsa.pub  known_hosts
[root@centos7 ~/.ssh]#

注意:

ssh-keygen -t rsa -p 爲了安全,可以考慮給私鑰加密,選項-p,從而在連接遠程主機時需要輸入私鑰的密碼

2.把公鑰文件傳輸至遠程服務器對應用戶的家目錄

[root@centos7 ~]#ssh-copy-id -i .ssh/id_rsa.pub 10.1.1.1/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 [email protected]'s password: 

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

[root@centos7 ~]#

公鑰複製到遠程服務器默認位置了,並會被改名爲authorized_keys,且權限必須爲600,基於key的認證,即使對方更改了口令,照樣能夠連接。

wKioL1fySaLx88yoAAA4HNSE6LA884.png

測試:

wKioL1fySbrgKfMiAAAwME8nGf0025.png

不輸入任何密碼又不太安全,假如有人正在運用你的機器。。。那就給重設私鑰口令吧!

重設私鑰口令:#ssh-keygen–p, 口令須大於4bites,否則失敗

[root@centos7 ~]#ssh-keygen -pEnter file in which the key is (/root/.ssh/id_rsa): 
Key has comment '/root/.ssh/id_rsa'Enter new passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved with the new passphrase.
[root@centos7 ~]#

wKiom1fyScigccGRAAByNlX69a8701.png

測試:

wKiom1fySdejfcunAAA_rzrMxmE826.png

如此一來,您就不必記一大堆服務器的密碼了,只須記住自己的私鑰口令就OK了!但每次還是得輸入,不爽!我們可以將口令託管於bash!

口令託管

基於key認證中,私鑰口令的託管只需ssh-agent bash即可,然後用ssh-add 將口令添加給代理。

wKioL1fySeaibv87AABp1GbVQdc063.png

以上是在Linux中實現的,如果我們是在Windows中用Xshell或SecureCRT類似的工具應該如何設置呢?

Xshell:

wKiom1fySfmBo1saAACLzxvhaqI862.png

wKioL1fySgiBD0__AACPWu0PwWM040.png

wKiom1fyShWQiq3fAACHXnFsZHs712.png

wKioL1fySiTg767aAACR63Toaic167.png

wKioL1fySjTguGKxAACxDp5gOss456.png

保存到Windows桌面之後,就可導入到linux主機中:

[root@centos6 ~]#cat id_rsa_2048.pub >> .ssh/authorized_keys [root@centos6 ~]#

接着設置:

wKiom1fySkOzkuS3AABtQ2s3iX4313.png

wKiom1fySlTSnKXsAABio2OLX5c114.png

wKioL1fySmDhbhCGAABMoc0iie0700.png

SecureCRT:

wKiom1fySm_RNeJoAAA2isNgFas799.png

然後就一直下一步就行!CRT會同步生成公鑰(Identity.pub)與私鑰(Identity),最後只需導入公鑰給 ssh 服務器就行。

注意:轉化爲openssh兼容格式(適合SecureCRT,Xshell不需要轉化格式),並複製到需登錄主機上相應文件authorized_keys中,注意權限必須爲600。

轉換:
ssh-keygen-i-f Identity.pub >> .ssh/authorized_keys

wKioL1fySoHDsv7SAAEn_pyaMt8014.png

可見二者的格式略有不同!

[root@centos6 ~]#ssh-keygen -i -f Identity.pub >> .ssh/authorized_keys 
[root@centos6 ~]#

wKioL1fySq_DM7q-AABvOMze1Ak879.png

wKioL1fySsTxPITdAABHYJ_V470862.png

先到這兒吧,本篇前文主要用gpg工具進行了一個完整的非對稱加密,後文主要講解了ssh的一個應用,如x11協議轉發,強制分配tty一步到位實現繞過防火牆,最後就是基於key認證的內容。


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章