天天看點

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加密一個檔案時,會彈出一個對話框要求你輸入密碼:

Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

解密: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]      

非本機解密:

Linux安全與加密基礎(二)

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

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

使用gpg實作非對稱加密

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

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

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

Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

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 ~]#      
Linux安全與加密基礎(二)

在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>"      
Linux安全與加密基礎(二)

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

gpg --delete-secret-keys liansir
gpg --delete-keys liansir      
Linux安全與加密基礎(二)

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      
Linux安全與加密基礎(二)

sshd_config:

Linux安全與加密基礎(二)

在centos 7上修改ssh的預設端口為2222,并重新開機服務

[root@centos7 ~]#semanage port -a -t ssh_port_t -p tcp 2222
[root@centos7 ~]#systemctl restart sshd      
Linux安全與加密基礎(二)

現在讓其它主機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 ~]#      
Linux安全與加密基礎(二)

在ssh遠端連接配接其它主機時,在目前使用者的家目錄下的.ssh目錄裡面,會生成一個known_hosts檔案,這個檔案記錄了ssh的登入資訊,遠端主機的公鑰,即遠端主機的/etc/ssh/ssh_host_rsa_key.pub檔案。

Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

此處記錄公鑰的目的是:在客戶機在下次連接配接伺服器時,因為伺服器的公鑰已經被複制到客戶機的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      
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

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

Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

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

Linux安全與加密基礎(二)

退出:system--> logout

繞過防火牆

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

場景:

Linux安全與加密基礎(二)
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 ~]#      
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

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

Linux安全與加密基礎(二)
[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 p_w_picpath 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的認證,即使對方更改了密碼,照樣能夠連接配接。

Linux安全與加密基礎(二)

測試:

Linux安全與加密基礎(二)

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

重設私鑰密碼:#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 ~]#      
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

如此一來,您就不必記一大堆伺服器的密碼了,隻須記住自己的私鑰密碼就OK了!但每次還是得輸入,不爽!我們可以将密碼托管于bash!

密碼托管

基于key認證中,私鑰密碼的托管隻需ssh-agent bash即可,然後用ssh-add 将密碼添加給代理。

Linux安全與加密基礎(二)

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

Xshell:

Linux安全與加密基礎(二)
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

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

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

接着設定:

Linux安全與加密基礎(二)
Linux安全與加密基礎(二)
Linux安全與加密基礎(二)

SecureCRT:

Linux安全與加密基礎(二)

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

注意:轉化為openssh相容格式(适合SecureCRT,Xshell不需要轉化格式),并複制到需登入主機上相應檔案authorized_keys中,注意權限必須為600。

轉換:
ssh-keygen-i-f Identity.pub >> .ssh/authorized_keys      
Linux安全與加密基礎(二)

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

[root@centos6 ~]#ssh-keygen -i -f Identity.pub >> .ssh/authorized_keys 
[root@centos6 ~]#      
Linux安全與加密基礎(二)

繼續閱讀