Linux安全和openssl、gpg加密
本章內容:
安全機制
對稱加密
不對稱加密
散列算法
PKI和CA
openssl
證書管理
gpg
加密需要:
不加密的流量易受攻擊性
密碼/數據嗅探
數據操作
驗證操作
相當于郵寄明信片
不安全的傳統協議:
telnet、ftp、pop3、等;不安全密碼
http、smtp、nfs、等;不安全信息
ldap、nis、rsh、等;不安全驗證
安全機制:
NIST(美國國家標準與技術研究院)定義的安全屬性:
保密性:
數據保密性;
隱私性;
完整性:
不可篡改
數據完整性
系統完整性
可用性
可擴展性
安全攻擊:
spoofing(假冒)、tampering(篡改)、repudiation(否認)、informationdisclosure(信息泄露)、denial of service(拒絕服務)、elevation of privilege(提升權限)
安全機制:
加密、數字簽名、訪問控制、數據完整性、認證交換、流量填充、路由控制、公證
安全服務:
認證
訪問控制
數據保密性
鏈接保密性
無連接保密性
選擇域保密性
流量保密性
數據完整性
不可否認性
設計基本原則:
使用成熟的安全系統
以小人之心度輸入數據
外部系統是不安全的
最小授權
減少外部接口
缺省使用外部接口
缺省使用安全模式
安全不是似是而非
從stride思考
在入口處檢查
從管理上保護好你的系統
安全算法:
常用安全技術
認證
授權
安全通信
審計
密碼算法和協議
對稱加密
公鑰加密
單向加密
認證協議
Linux系統:openssl,gpg(gpg協議的實現)
對稱加密算法:
對稱加密:加密和解密使用同一個秘鑰
DES:data encryption standard 56bits
3DES:
AES:advanced(128 192 256bits)
IDEA,RC6,CAST5
特性:
1、加密、解密使用同一個秘鑰,效率高;
2、將原始數據分隔成固定大小的塊,逐個進行加密
缺陷:
1、秘鑰過多
2、秘鑰分發
3、數據來源無法確認
非對稱加密算法:
公鑰加密:秘鑰是成對出現
公鑰:公開所有人:public key
私鑰:自己留存,必須保證其私密性,secret key
特點:
用公鑰加密數據,只能使用與之配對的私鑰解密:反之亦然。
功能:
數字簽名:主要在于讓接受方確認發送方身份;
對稱秘鑰交換:發送方用對方的公鑰加密一個對稱秘鑰后發送給對方;
數據加密:適合加密較小的數據;
缺點:密鑰長,加密解密效率低下;
算法:RSA(加密、數字簽名)DSA(數字簽名)
基于一對公鑰/密鑰對:
用密鑰對中的有個加密,另一個解密
實現加密:
接收者:
生成公鑰/密鑰對:p和s
公開公鑰p,保密密鑰s
發送者:
使用接受者的公鑰來加密消息m
將p(m)發送給接受者
接收者:
使用密碼S來解密:M=S(p(m))
實現數字簽名:
發送者:
生成公鑰/密鑰對:p和s
公開公鑰p,保密密鑰 s
使用密鑰s來加密消息m
發送給接收者s(m)
接收者:
使用發送者的公鑰來解密M=P(s(m))
結合簽名和加密:
分離簽名:
單向散列:
將任意數據縮小成固定大小的“指紋”
任意長度輸入
固定長度輸入
若修改數據,指紋也會改變(不會產生沖突)
無法從指紋中重新生成數據(單向)
功能:數據完整性
常見算式:
md5:128bits 、sha1:160bits、 sha224、sha256、sha384、sha512
檢查MD5加密的文件是否被改過:
[root@centos7 ~]# md5sum f1 > f1.md5
[root@centos7 ~]# md5sum –check f1.md5
f1: OK
[root@centos7 ~]#
常用工具:
MD5sum、sha1sum、openssl 、rpm -V
密鑰交換:IKE(Internet key exchange)
公鑰加密:DH(Deffie-Hellman)
DH:
1、A:a,p協商生成公開的整數a,大素數p
B:a,p
2、A:生成隱私數據:x(x<p),計算的出a^y%p,發送給A
B:生成隱私數據:y,計算得出a^y%p,發送給A
3、A:計算得出(a^y%p)^x=a^xy%p,生成為密鑰
B:計算得出(a^x%p)^y=a^xy%p,生成為密鑰
CA和證書:
PKI:public key infrastructure
簽證機構:CA
注冊機構:RA
證書吊銷列表:CRL
證書取存庫
X.509定義了證書的結構以及認證協議標準:
版本號 主體公鑰
序列號 CRL分發點
簽名算法 擴展信息
頒發者 發行者簽名
有效期限
主體名稱
證書獲取:
證書類型:
證書授權機構的證書
服務器
用戶證書
獲取證書的兩種方法:
使用證書授權機構:
生成簽名請求(csr)
將(csr)發送給CA
從CA處接受簽名
自簽名的證書:
自己簽發自己的公鑰
安全協議:
ssl:secure socket layer
功能:機密性、認證、完整性、重放保護
兩階段協議:分為握手階段和應用階段
握手階段(協商階段):客戶端和服務器端認證對方的身份,(依賴于PKI體系,利用數字證書進行身份認證),并協商通信中使用的安全參數、密碼套件以及主密鑰。后續通信使用的所有密鑰都是通過Mastersecret生成。
應用階段:在握手階段完成后進入,在應用階段通信雙方使用握手階段協商好的密鑰進行安全通信。
SSL/TLS:
SSL protocol(ssl 協議):
Handshake協議:包括協商安全參數和密碼套件、服務器身份驗證(客戶端身份認證可選)、密鑰交換。
ChangCipherspec協議:一條消息表明握手協議已經完成。
Alert協議:對握手協議中的一些異常的錯誤提醒,分為fatal和warning兩個級別,fatal類型錯誤會直接中斷ssl鏈接,而warning級別的錯誤ssl鏈接仍可繼續,只是會給出錯誤警告。
Record協議:包括對消息的分段、壓縮、消息認證和完整性保護、加密等。
HTTPS協議:就是“http”協議和“ssl/tls”協議的組合。http over ssl 或http over tls,對http協議的文本數據進行加密處理后,成為二進制形式傳輸。
openssl開源項目:
三個組件:
openssl:多用途的命令行工具;
libcrypot:加密算法庫
libssl:加密模塊應用庫,實現了ssl及tls
openssl命令:
兩種運行模式:交互模式和批處理模式;
openssl version:程序版本號;
標準命令、消息摘要命令、加密命令
標準命令:enc、ca、req、dgst、passwd
openssl命令:
對稱加密:
enc命令:對稱加密
示例:加密
加密/root/fstab文件,把加密后的文件保存至fstab.des3文件中,刪除原有文件。
[root@centos7 ~]# openssl enc -e -des3 -a -salt -in fstab -out fstab.des3
[root@centos7 ~]#rm -rf /root/fstab
示例:解密
對剛才加密的/root/fstab文件解密,解密成原文件名;
[root@centos7 ~]# openssl enc -d -des3 -a -salt -in fstab.des3 -out fstab
單向加密:
工具: md5:128bits 、sha1:160bits、 sha224、sha256、sha384、sha512
dgst命令: openssl dgst
示例:
1、使用MD5對fstab文件單向加密
[root@centos7 ~]# openssl dgst -md5 fstab
MD5(fstab)= 7821f49dddb994d0156cdc784a8863b6
[root@centos7 ~]#
2、同時還可以使用:MD5sum –check file 將保存到指定文件中的MD5單向加密值,實時檢查文件是否被篡改;
[root@centos7 ~]# openssl dgst -md5 fstab > f1.md5
[root@centos7 ~]# cat f1.md5
MD5(fstab)= 7821f49dddb994d0156cdc784a8863b6
[root@centos7 ~]# md5sum –check f1.md5
fstab: OK
[root@centos7 ~]#
生成用戶加密口令:
passwd命令:man sslpasswd
openssl passwd -1 -salt ##### SALT(最多8位)
示例:
為用戶生成一個加密口令:
[root@centos7 ~]# openssl passwd -1 -salt "123456" centos
$1$123456$3GFsiB8xUJ7EaYT.K6Spz0
[root@centos7 ~]#
生成隨機數:
openssl rand -base64 | -hex number(指定數字)
示例:生成隨機數
[root@centos7 ~]# openssl rand -base64 10
9cqdc7tAaCE5KQ==
[root@centos7 ~]#
生成密鑰對兒(非對稱加密):man genrsa
生成私鑰:
openssl genrsa -out /PATH/TO/PRIVATE.FILE NUM_BITS
des:給私鑰文件設置個密碼;
從私鑰中提取公鑰:
openssl rsa -in PRIVATE.FILE -pubout PUBLICKEY.FIEL
示例:
生成私鑰文件,在子shell中執行需加上():
[root@centos7 ~]# (umask 066;openssl genrsa -out key.private -des 2048 )
生成私鑰文件的權限600:
[root@centos7 ~]# ll key.private
-rw——- 1 root root 1738 oct 2 10:35 key.private
[root@centos7 ~]
從私鑰中提取公鑰:
[root@centos7 ~]# openssl rsa -in key.private -pubout -out key.public
Enter pass phrase for key.private:
writing RSA key
[root@centos7 ~]#
生成公鑰文件的權限644:
[root@centos7 ~]# ll key.public
-rw-r–r– 1 root root 451 oct 2 10:47 key.public
[root@centos7 ~]
隨機數生成器:偽隨機數
鍵盤和鼠標
塊設備中斷
/dev/random:僅從熵池返回隨機數;隨機數用盡,阻塞;
/dev/urando:從熵池返回隨機數,隨機數用盡,利用軟件生成偽隨機數,非阻塞。
openssl:PKI:public key infraStructure
創建私有CA和申請證書:
創建私有CA:
openssl的配置文件:/etc/pki/tls/openssl.cnf
(1)創建所需要的文件:
創建CA數據庫文件;
touch /etc/pki/CA/index.txt
創建CA數據庫索引編號文件;
echo “01” > /etc/pki/CA/serial
(2)CA自簽證書:
生成私鑰;
(umask 066; openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
生成自簽名證書:
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -0ut
/etc/pki/CA/cacert.pem
req:申請證書;
-out:證書的保存路徑;
-new:申請新證書;
-x509:生成專用于CA的自簽證書;
-key:生成請求時用到的私鑰文件;
-days:證書的有效期;
頒發證書:
在需要使用證書的主機生成證書請求:
1、 給web服務器生成私鑰:
(umask 066;openssl genrsa -out /etc/httpd/ssl/httpkey.private 2048)
2、生成證書申請文件:
openssl req -new -key /etc/httpd/ssl/httpkey.private -days 365 -out /etc/httpd/ssl/httpd.csr
3、將證書申請文件傳輸給CA
4、CA簽署證書,并將證書頒發給請求者;
openssl ca -in /root/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
5、查看證書中的信息;
openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
6、吊銷證書;
openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem
7、生成吊銷證書編號(第一次吊銷一個證書時才需要執行)
echo "01" > /etc/pki/CA/crlnumber
8、更新證書吊銷列表;
openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl
9、查看吊銷的crl證書文件;
openssl crl -in /etc/pki/CA/crl/ca.crl -noout -text
示例:搭建私有CA
*根據openssl的配置文件:/etc/pki/tls/openssl.cnf中定義的文件路徑來創建所需文件。
(1)創建所需的文件:
[root@centos7 ~]# touch /etc/pki/CA/index.txt //創建CA數據庫文件
[root@centos7 ~]# echo "01" > /etc/pki/CA/serial //創建CA數據庫索引編號文件
[root@centos7 ~]# cat /etc/pki/CA/serial
01
[root@centos7 ~]#
(2)CA自簽證書:
1)生成私鑰;
[root@centos7 ~]# (umask 066;openssl genrsa -out /etc/pki/CA/private/cakey.pem 2048)
Generating RSA private key, 2048 bit long modulus
…………………………………………………………………………………………………………………………+++
……….+++
e is 65537 (0x10001)
[root@centos7 ~]#
2)生成自簽名證書;
1)[root@centos7 ~]# openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 7300 -out /etc/pki/CA/cacert.pem
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:mage.com
Organizational Unit Name (eg, section) []:m20-1
Common Name (eg, your name or your server's hostname) []:centos7
Email Address []:admin@magedu.com
root@centos7 ~]#
2)查看所生成的證書:
root@centos7 CA]# openssl x509 -in /etc/pki/CA/cacert.pem -noout -text
同樣也可以copy到Windows桌面進行查看,但需要更改證書后綴名為:xxx.cer
(3)頒發證書:
1)在需要使用證書的主機生成證書請求:
例:給www.magedu.com web服務器申請證書。
(實驗環境說明:CA server為:centos7 web server為:centos6)
首先在web服務器中生成一個私鑰文件,用私鑰文件生成證書申請文件;并將證書申請文件傳送給CA。
第一步:在web服務器(centos6)中生成一個私鑰文件:
[root@centos6 Desktop]# mkdir /etc/httpd/ssl
[root@centos6 ]# (umask 066;openssl genrsa -out /etc/httpd/ssl/httpkey.private 2048)
Generating RSA private key, 2048 bit long modulus
………………………+++
……………………………………………………………………………………………………………………+++
e is 65537 (0x10001)
[root@centos6 Desktop]#
第二步:生成證書申請文件:
[root@centos6 Desktop]# openssl req -new -key /etc/httpd/ssl/httpkey.private -days 365 -out /etc/httpd/ssl/httpd.csr
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
—–
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:beijing
Locality Name (eg, city) [Default City]:bj
Organization Name (eg, company) [Default Company Ltd]:magedu.com
Organizational Unit Name (eg, section) []:m20-1
Common Name (eg, your name or your server's hostname) []:www.magedu.com
Email Address []:root@magedu.com
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:
[root@centos6 Desktop]#
第三步:將證書申請文件通過scp傳送給CA;
[root@centos6 Desktop]# scp /etc/httpd/ssl/httpd.csr 192.168.3.11:/root
第四步:CA簽署證書,并將證書頒發給請求者;并將證書保存到/etc/pki/CA/certs/目錄下。
[root@centos7 CA]# openssl ca -in /root/httpd.csr -out /etc/pki/CA/certs/httpd.crt -days 365
如果該證書存在違法行為,我們該如何吊銷此證書呢?
吊銷www.magedu.com的證書:
[root@centos7 newcerts]# openssl ca -revoke /etc/pki/CA/newcerts/01.pem
Using configuration from /etc/pki/tls/openssl.cnf
Revoking Certificate 01.
Data Base Updated
[root@centos7 newcerts]#
生成吊銷證書的編號(第一次吊銷一個證書時才需要執行)
[root@centos7 CA]# echo "01" > /etc/pki/CA/crlnumber
[root@centos7 CA]#
更新證書吊銷列表;
[root@centos7 crl]# openssl ca -gencrl -out /etc/pki/CA/crl/ca.crl
Using configuration from /etc/pki/tls/openssl.cnf
[root@centos7 crl]#
查看吊銷的crl證書文件;
[root@centos7 crl]# openssl crl -in /etc/pki/CA/crl/ca.crl -noout -text
使用gpg實現對稱加密:
對稱加密file文件:
gpg -c file 選項: -c:對稱加密
注:加密后的文件會以file.gpg結尾,如果在同一臺機器上解密是不用輸入密碼的。
在另一臺主機上解密file:
gpg -o file -d file.gpg 選項:-o:重定向 -d:解密
示例:
在centos7主機上使用gpg加密一個文件,在centos6上解密這個文件,這樣可以防止別人偷拷貝你的數據。
[root@centos7 ~]# gpg -c fstab //加密后的文件會以fstab.gpg結尾。
[root@centos6 ~]# gpg -o fstab -d fstab.gpg //把fstab.gpg文件解密到fstab文件中。
使用gpg工具實現公鑰加密:
1)在主機上生成公鑰/私鑰對兒
gpg –gen-key
注意:生成的公鑰私鑰文件放在了/root/.gnupg目錄下。
2)在主機上查看公鑰
gpg –list-key
3)在主機中導出公鑰到指定的文件中
gpg -a –export -o file 選項: -a:表示使用base64機制編碼
注:因為默認的公鑰文件是無法使用命令查看的。
4)從A本機復制公鑰文件到B主機上加密
使用scp命令
5)在B主機中導入A主機的公鑰
gpg —import file
6)用從A主機導入的公鑰,加密B主機的文件file,生成file.gpg
gpg -e -r PUBLIC_UID file
選項:
-e:加密。
-r:指定使用哪個 PUBLIC_UID加密文件。
7)復制加密文件到A主機:
scp file.gpg A主機
8)在A主機中解密文件:
gpg -d file.gpg
gpg -o file -d file.gpg
9)刪除公鑰和私鑰
gpg –delete-keys PUBLIC_UID
gpg –delete-secret-keys PUBLIC_UID (如果私鑰存在要先刪除私鑰)
示例:
使用gpg工具實現公鑰加密:
實驗環境:在centos7上使用gpg工具生成公鑰/私鑰密鑰對兒,然后把公鑰copy到centos6主機上,使用centos7的公鑰加密文件,然后把加密后的文件在copy到centos7上,使用centos7的私鑰進行解密該文件。
1、在centos7上生成公鑰/私鑰密鑰對兒:
[root@centos7 ~]# gpg –gen-key
2、可以使用gpg –list-key進行查看公鑰
[root@centos7 ~]# gpg –list-keys
/root/.gnupg/pubring.gpg
————————
pub 1024R/0379276C 2016-10-03
uid zhengyibo <root@magedu.com>
sub 1024R/CF708D9E 2016-10-03
[root@centos7 ~]#
3、把centos7中的公鑰文件以base64機制導入到一個新文件中:
[root@centos7 ~]# gpg -a –export -o ./zheng.public
4、使用scp命令將zheng.public公鑰文件copy到centos6中。
[root@centos7 ~]# scp ./zheng.public 192.168.3.2:/root
5、在centos6中將/root/zheng.public公鑰導入到/root/gnupg/pubring.gpg文件中。
[root@centos6 .gnupg]# gpg –import /root/zheng.public
gpg: key 0379276C: public key "zhengyibo <root@magedu.com>" imported
gpg: Total number processed: 1
gpg: imported: 1 (RSA: 1)
[root@centos6 .gnupg]#
* 使用gpg –list-key進行查看:
[root@centos6 .gnupg]# gpg –list-key
/root/.gnupg/pubring.gpg
————————
pub 1024R/0379276C 2016-10-03
uid zhengyibo <root@magedu.com>
sub 1024R/CF708D9E 2016-10-03
[root@centos6 .gnupg]#
6、測試,使用centos7中生成的公鑰文件,加密centos6中的文件。
注:建議不要加密過大的文件,因為文件過大,加密速度會很慢。
[root@centos6 ~]# vim testgpg
[root@centos6 ~]# cat testgpg
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
knknkkkkkkkkkkkkkkkkkkkkkkk
jjjjjjjjjjjjjjjjjjj
ccccccccccccccccccccccc
[root@centos6 ~]# gpg -e -r zhengyibo testgpg
gpg: CF708D9E: There is no assurance this key belongs to the named user
pub 1024R/CF708D9E 2016-10-03 zhengyibo <root@magedu.com>
Primary key fingerprint: 7F2D 8D74 E242 DCC9 FFD1 D30D F987 C0DB 0379 276C
Subkey fingerprint: 0E93 FD5A 7B46 2F12 9A61 BA1C D21F BBC5 CF70 8D9E
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@centos6 ~]#
* 在當前目錄使用ls命令查看生成的testgpg.gpg文件。
7、使用scp命令copy testgpg.gpg到centos7主機中。
[root@centos6 ~]# scp ./testgpg.gpg 192.168.3.11:/root
8、在centos7中解密testgpg.gpg文件。
[root@centos7 ~]# ggp -d testgpg.gpg
* 把testgpg.gpg加密文件重定向到指定的文件中。
[root@centos7 ~]# gpg -o test -d testgpg.gpg
[root@centos7 ~]# cat test
kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
knknkkkkkkkkkkkkkkkkkkkkkkk
jjjjjjjjjjjjjjjjjjj
ccccccccccccccccccccccc
[root@centos7 ~]#
9、刪除centos6上的公鑰文件,刪除centos7上的公鑰/私鑰文件
[root@centos6 ~]# gpg –delete-key zhengyibo //centos6上的公鑰文件
[root@centos7 ~]# gpg –delete-secret-key zhengyibo //centos7上的私鑰文件
[root@centos7 ~]# gpg –delete-key zhengyibo //centos7上的公鑰文件
注意:在刪除公鑰時,如果私鑰存在,一定要先刪除私鑰在去刪除公鑰文件。
原創文章,作者:zhengyibo,如若轉載,請注明出處:http://www.www58058.com/50307