HTTPS

HTTPS

1.什么是HTTPS

HTTPS ,是以安全为目标的 HTTP 通道,在HTTP的基础上通过传输加密和身份认证保证了传输过程的安全性 。HTTPS 在HTTP 的基础下加入SSL,HTTPS 的安全基础是 SSL,因此加密的详细内容就需要 SSL。 HTTPS 存在不同于 HTTP 的默认端口及一个加密/身份验证层(在 HTTP与 TCP之间)。这个系统提供了身份验证与加密通讯方法。它被广泛用于安全敏感的通讯,例如交易支付等方面。

2.为什么要用HTTPS

为什么需要使用HTTPS,因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。

那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。

那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密

3.模拟网站劫持

# web01 ip为192.168.15.7
[root@web01 conf.d]# vim http.conf
server {
    listen 80;
    server_name _;
    location / {
        root /opt/code;
        index index.html;
    }
}

[root@web01 ~]# vim /code/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生信息注册页面</title>
</head>
<body>
<h3 align="center">学生信息注册</h3>
<form  name="stu"action="">
<table>
  <tr><td>姓名:</td><td><input type="text"name="stuName"/></td></tr>
  <tr><td>性别:</td>
      <td><input type="radio"name="stuSex"checked="checked">男
          <input type="radio"name="stuSex">女
          </td>
          </tr>
   <tr><td>出生日期</td>
       <td><input type="text"name="stuBirthday"></td>
       <td>按格式yyyy-mm-dd</td>
       </tr>
       <tr><td>学校:</td><td><input type="text"name="stuSchool"></td></tr>
       <tr><td>专业:</td>
           <td><select name="stuSelect2">
               <option selected>计算机科学与技术</option>
               <option>网络工程</option>
               <option>物联网工程</option>
               <option>应用数学</option>
               </select>
               </td>
               </tr>
               <tr><td>体育特长:</td>
                   <td colspan="2">
                      <input type="checkbox"name="stuCheck" >篮球
                      <input type="checkbox"name="stuCheck" >足球
                      <input type="checkbox"name="stuCheck" >排球
                      <input type="checkbox"name="stuCheck" >游泳
                   </td> 
               </tr>
               <tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>
               <tr><td>密码:</td><td><input type="password"name="stuPwd" ></td></tr>
               <tr><td>个人介绍:</td>
                   <td colspan="2"><textarea name="Letter"rows="4"cols="40"></textarea></td>
               </tr>
               <tr>
                 <td><input type="submit"value="提交" ><input type="reset"value="取消" ></td>
                 </tr>
                 </table>
                 </form>
</body>
</html>
 
[root@web01 ~]# chown -R www.www /code/index.html

访问192.168.15.7

image

4.网站劫持

在lb01(代理web01)

# lb01 ip为192.168.15.5
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.jc.com.conf 
server {
    listen 80;
    server_name _;
 
    location / {
        proxy_pass http://192.168.15.7:80;
        include proxy_params;
[root@lb01 ~]# systemctl restart nginx

访问192.168.15.5

image

修改配置文件

[root@lb01 conf.d]# cat https.conf 
server {
    listen 80;
    server_name _;

    location / {
	proxy_pass http://192.168.15.7:80;
	sub_filter '<title>学生信息注册页面</title>' '<title>澳门首家线上赌场</title>';
        sub_filter '<h3 align="center">学生信息注册</h3>' '<h3 align="center">VIP用户信息注册</h3>';
        sub_filter '<tr><td>性别:</td>' '<tr><td>爱好:</td>';
        sub_filter '<option selected>计算机科学与技术</option>' '<option selected>按摩</option>';
        sub_filter '<option>网络工程</option>' '<option>抽烟</option>';
        sub_filter '<option>物联网工程</option>' '<option>喝酒</option>';
        sub_filter '<option>应用数学</option>' '<option>烫头</option>';
        sub_filter '<tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>' '<img src="https://blog.driverzeng.com/zenglaoshi/xingganheguan.gif">';
    }

}

再次访问192.168.15.5

image

5.加密流程

1、浏览器发起往服务器的443端口发起请求,请求携带了浏览器支持的加密算法和哈希算法。 
2、服务器收到请求,选择浏览器支持的加密算法和哈希算法。 
3、服务器下将数字证书返回给浏览器,这里的数字证书可以是向某个可靠机构申请的,也可以是自制的。 
4、浏览器进入数字证书认证环节,这一部分是浏览器内置的TLS完成的:
    4.1 首先浏览器会从内置的证书列表中索引,找到服务器下发证书对应的机构,如果没有找到,此时就会提示用户该证书是不是由权威机构颁发,是不可信任的。如果查到了对应的机构,则取出该机构颁发的公钥。 
    4.2 用机构的证书公钥解密得到证书的内容和证书签名,内容包括网站的网址、网站的公钥、证书的有效期等。浏览器会先验证证书签名的合法性(验证过程类似上面Bob和Susan的通信)。签名通过后,浏览器验证证书记录的网址是否和当前网址是一致的,不一致会提示用户。如果网址一致会检查证书有效期,证书过期了也会提示用户。这些都通过认证时,浏览器就可以安全使用证书中的网站公钥了。 
    4.3 浏览器生成一个随机数R,并使用网站公钥对R进行加密。
5、浏览器将加密的R传送给服务器。 
6、服务器用自己的私钥解密得到R。 
7、服务器以R为密钥使用了对称加密算法加密网页内容并传输给浏览器。 
8、浏览器以R为密钥使用之前约定好的解密算法获取网页内容。

证书对比

对比 域名型 DV 企业型 OV 增强型 EV
绿色地址栏 img小锁标记+https img小锁标记+https img小锁标记+企业名称+https
一般用途 个人站点和应用; 简单的https加密需求 电子商务站点和应用; 中小型企业站点 大型金融平台; 大型企业和政府机构站点
审核内容 域名所有权验证 全面的企业身份验证; 域名所有权验证 最高等级的企业身份验证; 域名所有权验证
颁发时长 10分钟-24小时 3-5个工作日 5-7个工作日
单次申请年限 1年 1-2年 1-2年
赔付保障金 —— 125-175万美金 150-175万美金

自签证书

#使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)
[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
...............................+++
........+++
e is 65537 (0x10001)
Enter pass phrase for server.key: 123456
Verifying - Enter pass phrase for server.key: 123456
 
[root@web01 ssl_key]# ll
total 4
-rw-r--r--. 1 root root 1739 Dec  9 11:27 server.key

#生成自签证书(公钥),同时去掉私钥的密码
[root@web01 ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit RSA private key
.....................................+++ 
............+++
writing new private key to 'server.key'
-----
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]:china       
string is too long, it needs to be less than  2 bytes long
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:meiguo
Locality Name (eg, city) [Default City]:riben
Organization Name (eg, company) [Default Company Ltd]:heishoudang
Organizational Unit Name (eg, section) []:oldboy
Common Name (eg, your name or your server's hostname) []:oldboy
Email Address []:123@qq.com
 
# req  --> 用于创建新的证书
# new  --> 表示创建的是新证书    
# x509 --> 表示定义证书的格式为标准格式
# key  --> 表示调用的私钥文件信息
# out  --> 表示输出证书文件信息
# days --> 表示证书的有效期
# sha256 --> 加密方式

#1.开启证书
Syntax: ssl on | off;
Default:    ssl off;
Context:    http, server
 
#2.指定证书文件
Syntax: ssl_certificate file;
Default:    —
Context:    http, server
 
#3.指定私钥文件
Syntax: ssl_certificate_key file;
Default:    —
Context:    http, server

效果是这样的
image

6.负载均衡加密

# 在lb01里
[root@lb01 conf.d]# vim https.conf 
upstream ssl {
    server 172.16.1.7;
    server 172.16.1.8;
    server 172.16.1.9;
}

server {
    listen 443 ssl;
    server_name _;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    location / {
        proxy_pass http://ssl;
        include /etc/nginx/proxy_params;
    }
}

访问https://192.168.15.5(web01里是学生注册,web02、03里是超级玛丽)

image

但是现在不能访问80端口了,所以要再追加一个server

 [root@lb01 conf.d]# vim https.conf 
upstream ssl {
    server 172.16.1.7;
    server 172.16.1.8;
    server 172.16.1.9;
}

server {
    listen 443 ssl;
    server_name _;

    ssl_certificate /etc/nginx/ssl/server.crt;
    ssl_certificate_key /etc/nginx/ssl/server.key;

    location / {
        proxy_pass http://ssl;
        include /etc/nginx/proxy_params;
    }
}
server {
    listen 80;
    server_name _;
    rewrite (.*) https://192.168.15.5 permanent;  # 默认访问就转发到https://192.168.15.5
}

所以实现了只需要输入192.168.15.5就可以,不再需要输入https://

image

posted @ 2022-01-08 09:43  zong涵  阅读(165)  评论(0编辑  收藏  举报