Linux之加密和安全
加密和安全
安全機制
信息安全防護的目標
保密性 Confidentiality
完整性 Integrity
可用性 Usability
可控制性 Controlability
不可否認性 Non-repudiation
安全防護環節
物理安全:各種設備/主機、機房環境
系統安全:主機或設備的操作系統
應用安全:各種網絡服務、應用程序
網絡安全:對網絡訪問的控制、防火墻規則
數據安全:信息的備份與恢復、加密解密
管理安全:各種保障性的規范、流程、方法
安全
Spoofing 假冒
Tampering 篡改
Repudiation 否認
Information Disclosure 信息泄漏
Denial of Service 拒絕服務
Elevation of Privilege 提升權限
安全設計基本原則
使用成熟的安全系統
以小人之心度輸入數據
外部系統是不安全的
最小授權
減少外部接口
缺省使用安全模式
安全不是似是而非
從STRIDE思考
在入口處檢查
從管理上保護好你的系統
安全算法
常用安全技術
認證
授權
審計
安全通信
加密算法和協議
對稱加密
公鑰加密
單向加密
認證協議
對稱加密算法:
加密和解密使用同一個密鑰
特性:
1、加密、解密使用同一個密鑰,效率高
2、將原始數據分割成固定大小的塊,逐個進行加密
缺陷:
1、密鑰過多
2、密鑰分發
3、數據來源無法確認
非對稱加密算法
公鑰加密:密鑰是成對出現
公鑰:公開給所有人;public key
私鑰:自己留存,必須保證其私密性;secret key
特點:用公鑰加密數據,只能使用與之配對的私鑰解密;反之亦然
功能:
數字簽名:主要在于讓接收方確認發送方身份
對稱密鑰交換:發送方用對方的公鑰加密一個對稱密鑰后發送給對方
數據加密:適合加密較小數據
缺點:密鑰長,加密解密效率低下
算法:
RSA(加密,數字簽名)
DSA(數字簽名)
ELGamal
數字簽名
Shell中可以使用
sha256sum 計算文件的哈希值,從而和網上下載提供的哈希值對比,如果相同證明該文件未被篡改過。
使用gpg實現對稱加密
對稱加密file文件需輸入兩次密碼
gpg -c file
ls file.gpg
在另一臺主機上解密file 指定解密后文件名并解密
gpg -o file -d file.gpg
使用gpg工具實現公鑰加密密鑰生成后在家目錄下.gnupg目錄中
在hostB主機上用公鑰加密,在hostA主機上解密
在hostA主機上生成公鑰/私鑰對
gpg --gen-key
在hostA主機上查看公鑰
gpg --list-keys
在hostA主機上導出公鑰到martinhe.pubkey
gpg -a --export -o martinhe.pubkey
從hostA主機上復制公鑰文件到需加密的B主機上
scp marinthe.pubkey hostB
在需加密數據的hostB主機上生成公鑰/私鑰對
gpg --list-keys
gpg --gen-key
在hostB主機上導入公鑰
gpg --import martinhe.pubkey
gpg --list-keys
用從hostA主機導入的公鑰,加密hostB主機的文件file,生成file.gpg
gpg -e -r martinhe file
file file.gpg
復制加密文件到hostA主機
scp fstab.gpg hostA:
在hostA主機解密文件
gpg -d file.gpg
gpg -o file -d file.gpg
刪除公鑰和私鑰:先刪除導入的A公鑰,再刪除本機的私鑰,最后刪除本機的公鑰
gpg --delete-keys martinhe
gpg --delete-secret-keys chengge
gpg --delete-keys chengge
CA和證書
PKI:Public Key Infrastructure
簽證機構:CA(Certificate Authority)
注冊機構:RA
證書吊銷列表:CRL
證書存取庫:
X.509:定義了證書的結構以及認證協議標準
版本號
序列號
簽名算法
頒發者
有效期限
主體名稱
主體公鑰
CRL分發點
擴展信息
發行者簽名
證書獲取
證書類型:
證書授權機構的證書
服務器
用戶證書
獲取證書兩種方法:
? 使用證書授權機構
生成證書請求(csr)
將證書請求csr發送給CA
CA簽名頒發證書
? 自簽名的證書
自已簽發自己的公鑰
SSL/TLS
HTTPS結構
HTTPS工作過程
OpenSSL
OpenSSL:開源項目
三個組件:
openssl:多用途的命令行工具,包openssl
libcrypto:加密算法庫,包openssl-libs
libssl:加密模塊應用庫,實現了ssl及tls,包nss
openssl命令:
兩種運行模式:交互模式和批處理模式
openssl version:程序版本號
標準命令、消息摘要命令、加密命令
標準命令:enc, ca, req, ...
openssl命令
對稱加密:
工具:openssl enc, gpg
算法:3des, aes, blowfish, twofish
enc命令:
幫助:man enc
加密:-a 指定加密基于base64編碼(不指定生成機密文件內為亂碼):每6位分割,不足6位后面補零
openssl enc -e -des3 -a -salt -in testfile -out testfile.cipher
解密:解密時如果不指定輸出文件,則在屏幕打印解密文件內容
openssl enc -d -des3 -a -salt –in testfile.cipher -out testfile
openssl ?
用于校驗本地文件是否被篡改,可提前生成校驗文件然后保存下來
md5sum sohu.txt > sohu.txt.md5
md5sum -c|--check sohu.txt
單向加密:
工具:md5sum, sha1sum, sha224sum,sha256sum…
openssl dgst
dgst命令:
幫助:man dgst
openssl dgst -md5 [-hex默認] /PATH/SOMEFILE
openssl dgst -md5 testfile
md5sum /PATH/TO/SOMEFILE
MAC: Message Authentication Code,單向加密的一種延伸應用,用于實現
網絡通信中保證所傳輸數據的完整性機制
CBC-MAC
HMAC:使用md5或sha1算法
生成用戶密碼:
passwd命令:
幫助:man sslpasswd
openssl passwd -1 -salt SALT(最多8位)
openssl passwd -1 –salt centos
生成隨機數:
幫助:man sslrand
openssl rand -base64|-hex NUM
NUM: 表示字節數,使用-hex,每個字符為十六進制,相當于4位二進制,
出現的字符數為NUM*2
openssl rand -base64 12 | grep -o "[[:alnum:]]{9}"
可用于生成9位隨機字母數字的字串
公鑰加密:
算法:RSA, ELGamal
工具:gpg, openssl rsautl(man rsautl)
數字簽名:
算法:RSA, DSA, ELGamal
密鑰交換:
算法:dh
DSA:Digital Signature Algorithm
DSS:Digital Signature Standard
RSA:
生成密鑰對兒:man genrsa
生成私鑰
openssl genrsa -out /PATH/TO/PRIVATEKEY.FILE NUM_BITS
(umask 077; openssl genrsa –out test.key –des3 ) 生成私鑰的同時使用des3對稱口令加密-平時不加口令,前提確保密鑰安全
openssl rsa -in test.key –out test2.key 將加密key解密
從私鑰中提取出公鑰
openssl rsa -in PRIVATEKEYFILE –pubout –out PUBLICKEYFILE
openssl rsa –in test.key –pubout –out test.key.pub
隨機數生成器:偽隨機數字
鍵盤和鼠標,塊設備中斷
/dev/random:僅從熵池返回隨機數;隨機數用盡,阻塞
/dev/urandom:從熵池返回隨機數;隨機數用盡,會利用軟件生成偽隨機
數,非阻塞
PKI:Public Key Infrastructure
CA
RA
CRL
證書存取庫
建立私有CA:
OpenCA
openssl
證書申請及簽署步驟:
1、生成申請請求
2、RA核驗
3、CA簽署
4、獲取證書
創建CA和申請證書
創建私有CA:
openssl的配置文件:/etc/pki/tls/openssl.cnf
三種策略:match匹配、optional可選、supplied提供
match:要求申請填寫的信息跟CA設置信息必須一致
optional:可有可無,跟CA設置信息可不一致
supplied:必須填寫這項申請信息,
1、創建所需要的文件
touch /etc/pki/CA/index.txt 生成證書索引數據庫文件
echo 01 > /etc/pki/CA/serial 指定第一個頒發證書的序列號
2、 CA自簽證書
生成私鑰
cd /etc/pki/CA/
(umask 066; openssl genrsa -out private/cakey.pem 2048)
生成自簽名證書
openssl req -new -x509 -key /etc/pki/CA/private/cakey.pem -days 3650 -out /etc/pki/CA/cacert.pem
選項說明:
-new:生成新證書簽署請求
-x509:專用于CA生成自簽證書
-key:生成請求時用到的私鑰文件
-days n:證書的有效期限
-out /PATH/TO/SOMECERTFILE: 證書的保存路徑
3、頒發證書
在需要使用證書的主機生成證書請求
給web服務器生成私鑰
(umask 066; openssl genrsa –out /data/test.key 2048)
生成證書申請文件
openssl req -new -key /data/test.key -out /data/test.csr
將證書請求文件傳輸給CA
CA簽署證書,并將證書頒發給請求者
openssl ca -in /tmp/test.csr –out /etc/pki/CA/certs/test.crt -days 100
注意:默認要求 國家,省,公司名稱三項必須和CA一致
查看證書中的信息:
openssl x509 -in /PATH/FROM/CERT_FILE -noout -text|issuer|subject|serial|dates
openssl ca -status SERIAL 查看指定編號的證書狀態
4、吊銷證書
在客戶端獲取要吊銷的證書的serial
openssl x509 -in /PATH/FROM/CERT_FILE -noout -serial -subject
在CA上,根據客戶提交的serial與subject信息,對比檢驗是否與index.txt文件中的信息一致,
吊銷證書:
openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem
指定第一個吊銷證書的編號,注意:第一次更新證書吊銷列表前,才需要執行
echo 01 > /etc/pki/CA/crlnumber
更新證書吊銷列表
openssl ca -gencrl -out /etc/pki/CA/crl.pem
查看crl文件:
openssl crl -in /etc/pki/CA/crl.pem -noout -text
實驗:私有CA建立和證書申請,寫成腳本
1 創建CA /etc/pki/tls/openssl.cnf 此文件為CA配置文件
cd /etc/pki/CA
touch index.txt 數據庫文件
echo 0F > serial 證書序列號-16進制數
(umask 066;openssl genrsa -out private/cakey.pem 4096 ) 創建根CA私鑰 長度4096 保證權限為600
openssl req -new -x509 -key private/cakey.pem -out cacert.pem -days 3650
req:證書申請
-new:新的證書
-x509:自簽名
-key:提交自己的私鑰
-out:生成證書文件
-days:有效期3650天 私鑰自簽名證書
此三項必填(填寫國家、省會,公司名稱),城市,部門名稱,主機名稱,郵箱可寫可不寫。
openssl x509 -in cacert.pem -noout -text 查看證書內容
2 在客戶端生成證書申請
(umask 066;openssl genrsa -out test.key 1024) 生成私鑰
openssl req -new -key test.key -out test.csr 利用私鑰生成證書申請csr文件,其中填寫三項必須和上面一致。
scp test.csr 192.168.36.7:/data
3 在根CA給客戶端頒發證書
openssl ca -in /data/test.csr -out certs/test.crt -days 100 頒發證書
同一個證書申請csr如果向重復頒發證書文件,需要把index.txt.attr文件里的字段唯一性檢查改為no
4 吊銷證書
在客戶端獲取要吊銷的證書的serial
openssl x509 -in /PATH/FROM/CERT_FILE -noout -serial -subject
在CA上,根據客戶提交的serial與subject信息,對比檢驗是否與index.txt文件中的信息一致,吊銷證書:
openssl ca -revoke /etc/pki/CA/newcerts/SERIAL.pem
指定第一個吊銷證書的編號,注意:第一次更新證書吊銷列表前,才需要執行
echo 01 > /etc/pki/CA/crlnumber
更新證書吊銷列表
openssl ca -gencrl -out /etc/pki/CA/crl.pem
查看crl文件:
openssl crl -in /etc/pki/CA/crl.pem -noout -text
#!/bin/bash # #--------------------------------------- #Author: MartinHe #Date: 2019-04-19 #FileName: CA_create.sh #URL: https://blog.51cto.com/5033330 #Desciption: The test Scirpt #Copyright(c) 2019 All rights reserved #---------------------------------------# 根CA創建函數,內部定義自行修改 creatCA () {expect<<EOF spawn openssl req -new -x509 -key $DIR/private/cakey.pem -out $DIR/cacert.pem -days 3650 expect { "Country Name" { send "CN\n";exp_continue } "State or Province Name" { send "beijing\n";exp_continue } "Locality Name" { send "beijing\n";exp_continue } "Organization Name" { send "martinhe\n";exp_continue }"Organizational Unit Name" { send "m36\n";exp_continue} "Common Name" { send "ca.martinhe.com\n";exp_continue } "Email Address" { send "admin@martinhe.com\n" } }expect eof EOF}# 使用私鑰生成證書申請csr文件,內部定義自行修改 applyCA () {expect<<EOF spawn openssl req -new -key ./${KEY}.key -out ./${KEY}.csr expect { "Country Name" { send "CN\n";exp_continue } "State or Province Name" { send "beijing\n";exp_continue } "Locality Name" { send "haidian\n";exp_continue } "Organization Name" { send "martinhe\n";exp_continue } "Organizational Unit Name" { send "m36_devops\n";exp_continue} "Common Name" { send "m36.martinhe.com\n";exp_continue } "Email Address" { send "m36@martinhe.com\n" ;exp_continue } "A challenge password" { send "\n";exp_continue } "An optional company name" { send "\n" } } expect eof EOF}BC="\e[1;32m" EC="\e[0m"PS3="Please choose a num which one you want to do: "DIR="/etc/pki/CA"select menu in createCA applyCA awardCA quit;docase $menu increateCA) touch $DIR/{index.txt,serial} echo 01 > $DIR/serial (umask 066;openssl genrsa -out $DIR/private/cakey.pem 4096) &> /dev/nullcreatCA;;applyCA)echo -e "${BC}please input a private key name: ${EC}\c";read KEY(umask 066;openssl genrsa -out ${KEY}.key 1024)applyCAecho -e "${BC}please input the CA server IP: ${EC}\c"; read IPscp ${KEY}.csr ${IP}:;;awardCA)echols /root/*.csrechoecho -e "${BC}please input the csr name to ward(such as test): ${EC}\c";read CSRopenssl ca -in /root/${CSR}.csr -out $DIR/certs/$CSR.crt -days 100;;quit)echo you input is $REPLYbreak;;*)echo -e "${BC}Wrong choose,stupid!!!${EC}"esac done轉載于:https://blog.51cto.com/5033330/2380672
總結
以上是生活随笔為你收集整理的Linux之加密和安全的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 一些常用软件的网络端口协议分类介绍
- 下一篇: AWS EKS 介绍