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加密一個文件時,會彈出一個對話框要求你輸入密碼:
解密: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]
非本機解密:
gpg解密時除了重定向方式,還可以如下解密:
pgp -o file2 -d file.gpg # -o 必須在前
使用gpg實現非對稱加密
如centos 7要加密一個文件傳給centos 6, 這個過程應該是要拿centos 6的公鑰加密,所以要在centos 6上先生成公鑰。
1.在hostA主機上生成公鑰/私鑰對:gpg –gen-key
此操作會用隨機數生成密鑰對兒,需要移動鼠標或敲擊鍵盤,所以此命令最好在本地終端作:
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 ~]#
在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: root@10.1.1.2'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 <admin@zhizhan.com> 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 <admin@zhizhan.com> 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 root@10.1.1.1'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>"
至此,我們利用gpg工具就完成了一個完整的非對稱加密過程,如果要刪除公鑰與私鑰,如使用如下命令:
gpg --delete-secret-keys liansir gpg --delete-keys liansir
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
sshd_config:
在centos 7上修改ssh的默認端口為2222,并重啟服務
[root@centos7 ~]#semanage port -a -t ssh_port_t -p tcp 2222 [root@centos7 ~]#systemctl restart sshd
現在讓其它主機ssh連接時則必須要加相應的端口:-p portnum
[root@centos6 ~]#ssh 10.1.1.2 -p 2222root@10.1.1.2's password: Last login: Sun Sep 25 03:48:36 2016 from 10.1.252.68[root@centos7 ~]#
在ssh遠程連接其它主機時,在當前用戶的家目錄下的.ssh目錄里面,會生成一個known_hosts文件,這個文件記錄了ssh的登錄信息,遠程主機的公鑰,即遠程主機的/etc/ssh/ssh_host_rsa_key.pub文件。
此處記錄公鑰的目的是:在客戶機在下次連接服務器時,因為服務器的公鑰已經被復制到客戶機的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
如果我們想在通過遠程連接工具,直接打開遠程主機的桌面,Xmanager Enterprise提供了一個xstart的工具就可以做到。
點擊運行會提示輸入密碼:接著您就會看到遠程主機的桌面了!
退出:system–> logout
繞過防火墻
ssh -t 是一個有意思的工具,可實現防火墻的跳轉,一步到位跳轉,強制分配“偽終端”。
場景:
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!
[root@rhel5.4 ~]#iptables -A INPUT -s 10.1.1.2 -j REJECT[root@rhel5.4 ~]#
[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 ~]#
其實我們可“一步到位”:
[root@centos7 ~]#ssh -t 10.1.1.1 ssh 10.1.1.3root@10.1.1.1's password: root@10.1.1.3's password: Last login: Sun Sep 25 18:36:22 2016 from 10.1.250.37[root@rhel5.4 ~]#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 keysroot@10.1.1.1'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的認證,即使對方更改了口令,照樣能夠連接。
測試:
不輸入任何密碼又不太安全,假如有人正在運用你的機器。。。那就給重設私鑰口令吧!
重設私鑰口令:#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 ~]#
測試:
如此一來,您就不必記一大堆服務器的密碼了,只須記住自己的私鑰口令就OK了!但每次還是得輸入,不爽!我們可以將口令托管于bash!
口令托管
基于key認證中,私鑰口令的托管只需ssh-agent bash即可,然后用ssh-add 將口令添加給代理。
以上是在Linux中實現的,如果我們是在Windows中用Xshell或SecureCRT類似的工具應該如何設置呢?
Xshell:
保存到Windows桌面之后,就可導入到linux主機中:
[root@centos6 ~]#cat id_rsa_2048.pub >> .ssh/authorized_keys [root@centos6 ~]#
接著設置:
SecureCRT:
然后就一直下一步就行!CRT會同步生成公鑰(Identity.pub)與私鑰(Identity),最后只需導入公鑰給 ssh 服務器就行。
注意:轉化為openssh兼容格式(適合SecureCRT,Xshell不需要轉化格式),并復制到需登錄主機上相應文件authorized_keys中,注意權限必須為600。
轉換: ssh-keygen-i-f Identity.pub >> .ssh/authorized_keys
可見二者的格式略有不同!
[root@centos6 ~]#ssh-keygen -i -f Identity.pub >> .ssh/authorized_keys [root@centos6 ~]#
先到這兒吧,本篇前文主要用gpg工具進行了一個完整的非對稱加密,后文主要講解了ssh的一個應用,如x11協議轉發,強制分配tty一步到位實現繞過防火墻,最后就是基于key認證的內容。
原創文章,作者:Liansir,如若轉載,請注明出處:http://www.www58058.com/49997