[关闭]
@mrz1 2018-01-07T12:38:21.000000Z 字数 7426 阅读 1233

2018-1-3课程笔记(证书 ssh)

笔记


CA和证书

PKI(Public Key Infrastructure) 简写
签证机构 CA (Certificate Authority)
注册机构 RA
证书吊销列表 CRL
证书存取库 --

X.509:定义了证书的结构以及认证协议标准

  1. 版本号 序列号 签名算法 颁发者
  2. 有效期限 主体名称 主体公钥 CRL分发点
  3. 扩展信息 发行者签名

证书获取

证书类型:
证书授权机构的证书
服务器
用户证书

获取证书两种方法:

1.使用证书授权机构
生成签名请求(csr)
将csr发送给CA
从CA处接收签名
2.自签名的证书:
自已签发自己的公钥

安全协议

SSL Secure Socket Layer
TLS Transport Layer Security
1995 SSL 2.0 Netscape
1996 SSL 3.0
1999 TLS 1.0
2006 TLS 1.1 IETF(Internet工程任务组) RFC 4346
2008 TLS 1.2 当前使用
2015 TLS 1.3
功能 机密性,认证,完整性,重放保护

两阶段协议,分为握手阶段和应用阶段

SSL/TLS

SSL/TLS

SSL/TLS

SSL/TLS

HTTPS工作原理

  1. client hello---> server
  2. server cert:Sca(Pserver)--->client
  3. client Pca[Sca(Pserver)]=Pserver
  4. client key(客户端随机生成的key)
  5. client Pserver(key)--->server
  6. server Sserver[Pserver(key)]=key
  7. client<---key(data)--->server

cert:Sca(Pserver)==证书:私钥(证书)
Pca[Sca(Pserver)]==公钥[私钥(证书)]

OpenSSL

OpenSSL:开源项目

  1. 三个组件:
  2. openssl: 多用途的命令行工具,包openssl
  3. libcrypto: 加密算法库,包openssl-libs
  4. libssl:加密模块应用库,实现了ssltls,包nss

openssl命令:

  1. 两种运行模式:交互模式和批处理模式
  2. openssl version:程序版本号
  3. 标准命令、消息摘要命令、加密命令
  4. 标准命令:enc, ca, req, ...

openssl命令

  1. 对称加密:
  2. 工具:opensslenc, gpg
  3. 算法:3des, aes, blowfish, twofish
  4. enc命令:
  5. 帮助:man enc
  6. 加密:openssl enc -e -des3 -a -salt -in file -out file.s
  7. 注释:-e -des3(加密算法) -a -salt(加颜料) -in(那个文件) file -out(输出文件的名字) file.s
  8. 解密:openssl enc -d -des3 -a -salt in file.s -out file
  9. 单向加密:
  10. 工具:md5sum, sha1sum, sha224sum,sha256sum...openssl dgst
  11. dgst命令:
  12. 帮助:man dgst
  13. openssl dgst -md5 [-hex默认] /PATH/SOMEFILE
  14. openssl dgst -md5 testfile
  15. md5sum /PATH/TO/SOMEFILE
  16. MAC: Message Authentication Code,单向加密的一种延伸应用,用于实现网络通信中保证所传输数据的完整性机制
  17. CBC-MAC
  18. HMAC:使用md5sha1算法
  19. 生成用户密码:
  20. passwd命令:
  21. 帮助:man sslpasswd
  22. openssl passwd -1 -salt SALT(最多8位)
  23. openssl passwd -1 salt centos(把centos加密)
  24. 生成随机数:
  25. 帮助:man sslrand
  26. openssl rand -base64|-hex NUM
  27. NUM: 表示字节数;-hex时,每个字符为十六进制,相当于4位二进制,出现的字符数为NUM*2
  28. 公钥加密:
  29. 算法:RSA, ELGamal
  30. 工具:gpg, opensslrsautlman rsautl
  31. 数字签名:
  32. 算法:RSA, DSA, ELGamal
  33. 密钥交换:
  34. 算法:dh
  35. DSA: Digital Signature Algorithm
  36. DSSDigital Signature Standard
  37. RSA

生成密钥

  1. 生成密钥对儿:man genrsa
  2. 生成私钥
  3. openssl genrsa -out /PATH/TO/PRIVATEKEY.FILE NUM_BITS
  4. openssl genrsa -out centos6.key 2048 //文件生成后权限不安全
  5. (umask 066; openssl genrsa out test.key des3 2048) //安全
  6. openssl rsa -in test.key out test2.key 将加密key解密
  7. 从私钥中提取出公钥
  8. openssl rsa -in PRIVATEKEYFILE puboutout PUBLICKEYFILE
  9. Openssl rsa in test.key pubout out test.key.pub
  10. 例子:
  11. 生成私钥
  12. (umask 066; openssl genrsa out centos6.key des3 2048)
  13. 注意:-des3设置口令
  14. 从私钥中提取出公钥
  15. openssl rsa -in centos6.key -pubout -out centos6.key.pub
  16. 注意:提取公钥会输入口令
  17. 将加密key解密
  18. openssl rsa -in centos6.key out centos6.key1
  19. 随机数生成器:伪随机数字
  20. 键盘和鼠标,块设备中断
  21. /dev/random:仅从熵池返回随机数;随机数用尽,阻塞
  22. /dev/urandom:从熵池返回随机数;随机数用尽,会利用软件生成伪随机数,非阻塞

OpenSSL

创建CA和申请证书

SSH

SSH介绍

Openssh软件组成

ssh客户端

客户端组件:

  1. /etc/ssh/ssh_host*key.pubCentOS7默认是ssh_host_ecdsa_key.pub)文件中的公钥到客户机的~./ssh/know_hosts中。下次连接时,会自动匹配相应私钥,不能匹配,将拒绝连接
  1. # port 22 //修改这一行 端口自己指定
  2. systemctl reload sshd //修改文件后生效

ssh服务登录验证

基于key认证

基于密钥的认证:

  1. SecureCRT工具—>创建公钥—>生成Identity.pub文件转化为openssh兼容格式(适合SecureCRTXshell不需要转化格式),并复制到需登录主机上相应文件authorized_keys中,注意权限必须为600,在需登录的ssh主机上执行:ssh-keygen-i-f Identity.pub >> .ssh/authorized_keys

免密登录

  1. [root@centos6 ~]#ssh-keygen -p //在客户端生成密钥对
  2. Enter file in which the key is (/root/.ssh/id_rsa):
  3. Key has comment '/root/.ssh/id_rsa'
  4. Enter new passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved with the new passphrase.
  7. You have new mail in /var/spool/mail/root
  1. [root@centos6 ~]#ssh-copy-id -i .ssh/id_rsa root@172.18.101.118 //把公钥文件传输至远程服务器对应用户的家目录
  2. root@172.18.101.118's password:
  3. Now try logging into the machine, with "ssh 'root@172.18.101.118'", and check in:
  4. .ssh/authorized_keys
  5. to make sure we haven't added extra keys that you weren't expecting.
  1. [root@centos6 ~]#ssh 172.18.101.118 //连接远程服务器
  2. Enter passphrase for key '/root/.ssh/id_rsa': //这里输入的密码是自己私钥的密码
  3. Last login: Sun Jan 7 19:48:09 2018 from 172.18.101.93
  1. [root@centos6 ~]#ssh-agent bash
  2. [root@centos6 ~]#ssh-add
  3. Enter passphrase for /root/.ssh/id_rsa: //输入自己私钥的密码
  4. Identity added: /root/.ssh/id_rsa (/root/.ssh/id_rsa)
  5. [root@centos6 ~]#ssh 172.18.101.118
  6. Last login: Sun Jan 7 19:49:50 2018 from 172.18.101.93
  7. 登陆成功
  8. [root@centos7 ~]#
  9. 这里需要注意的的每次登录需要输入下密码 退出登录失效
  1. #!/bin/bash
  2. rpm -q expect &> /dev/null || yum -y -q install expect
  3. ssh-kengen -P "" -f ~/.ssh/id_rsa &> /dev/null
  4. while read ip passwd;do
  5. user=root
  6. expect <<EOF
  7. set timeout 10
  8. spawn ssh-copy-id $ip@$passwd
  9. expect{
  10. "yes/no" { send "yes\n";exp_continue }
  11. "password" { send "$password\n" }
  12. }
  13. expect eof
  14. EOF
  15. done < host.txt

解决ssh连接慢问题

  1. [root@centos6 ~]#vim /etc/ssh/sshd_config //找到下面两项改为nook
  2. GSSAPIAuthentication no
  3. UsePAM no
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注