Loading
0

给你的网站穿上外衣 - HTTPS 免费部署指南

前言

随着国内各大网站纷纷开启全站 HTTPS 时代,HTTPS 已不再是支付等敏感操作过程的专属,开启 HTTPS 对于个人网站或者小型网站也不再遥不可及。 今天博主就以自己的网站 www.rapospectre.com 为例叙述一下为自己网站点亮 HTTPS 小绿锁的过程。

HTTP 和 HTTPS

HTTPS( Hypertext Transfer Protocol over Secure Socket Layer ),是以安全为目标的 HTTP 通道,简单讲是 HTTP 的安全版。即 HTTP 下加入 SSL 层,HTTPS 的安全基础是 SSL ,因此加密的详细内容就需要 SSL 。 它是一个 URI scheme( 抽象标识符体系 ),句法类同 http :体系。用于安全的 HTTP 数据传输。 https:URL 表明它使用了 HTTP,但 HTTPS 存在不同于 HTTP 的默认端口及一个加密/身份验证层(在 HTTP 与 TCP 之间)。这个系统的最初研发由网景公司进行,提供了身份验证与加密通讯方法,现在它被广泛用于万维网上安全敏感的通讯,例如交易支付方面。

HTTP 超文本传输协议 ( HTTP-Hypertext transfer protocol ) 是一种详细规定了浏览器和万维网服务器之间互相通信的规则,通过因特网传送万维网文档的数据传送协议。

从概念里可以看到,要开启 HTTPS 至关重要的一点就是 ssl 层的身份验证,而身份验证需要用到 ssl 证书,以前少有免费 ssl 证书,所以小站基本不会选择 https ,而现在网上提供个人免费 ssl 证书的机构越来越多,这使得免费升级站点为 https 成为可能。

1. 申请 SSL 证书

网上已经有不少机构提供个人免费 ssl 证书,有效期几个月到几年不等,博主使用的是 StartSSL, 申请成功后有效期 3 年,到期后可免费续租。 具体申请过程不复杂,注册后根据提示验证网站 + 生成证书即可,如果不清楚可以 Google 一下。

要注意 StartSSL 验证网站拥有者时是给域名所有者的邮箱发验证邮件,如果域名开启了隐私保护请暂时关闭。

然后在自己服务器中生成 SSL 证书的 csr ,记住生成输入的秘密,之后要用到:

openssl req -new -sha256 -key rapospectre.com_secure.key -out rapospectre.com.csr

假设以上文件生成在 /var/tmp 文件夹下

在 StartSSL 填写 csr 文件内容,生成 SSL 证书并下载, 生成成果后如图:

点击 Retrieve 下载证书,解压缩后包含各种服务器的 crt ,博主使用 nginx 做反代,所以选择 nginxserver 解压缩后得到www.rapospectre.com_bundle.crt 将此文件上传到服务器,假设传到 /var/tmp/ 文件夹

2. 配置服务器

以 nginx 为例,打开 /etc/nginx/nginx.conf,加入配置:

 server {
        listen       443 ssl;
        ssl_certificate /var/tmp/www.rapospectre.com_bundle.crt;
        ssl_certificate_key /var/tmp/rapospectre.com_secure.key;
        ssl_prefer_server_ciphers on;
        ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
        #选择特定的加密方式, 避免已知的漏洞
        ssl_ciphers 'kEECDH+ECDSA+AES128 kEECDH+ECDSA+AES256 kEECDH+AES128 kEECDH+AES256 kEDH+AES128 kEDH+AES256 DES-CBC3-SHA +SHA !aNULL !eNULL !LOW !MD5 !EXP !DSS !PSK !SRP !kECDH !CAMELLIA !RC4 !SEED';
        #让浏览器记住直接访问 https 的网址, 不再去 http 重定向。
        add_header Strict-Transport-Security 'max-age=31536000; preload';
        add_header X-Frame-Options DENY;
        ssl_session_cache   shared:SSL:10m;
        ssl_session_timeout 10m;
        keepalive_timeout 70;
        ssl_dhparam /var/tmp/dhparam2048.pem;
        #禁止服务器自动解析资源类型
        add_header X-Content-Type-Options nosniff;
        #防XSS攻擊
        add_header X-Xss-Protection 1;
        server_name  www.rapospectre.com rapospectre.com;

在之前的 80 端口进行重定向配置:

server {
    listen 80;
    server_name rapospectre.com www.rapospectre.com;
    return 301 https://www.rapospectre.com$request_uri;
}

3. HTTP 替换

将网站所有以 http 方式获取的资源全部改为 https 方式或自动方式获取, eg:

<script src="http://xx.cdn.com/jquery.js"></script>
改为
<script src="https://xx.cdn.com/jquery.js"></script>
或
<script src="//xx.cdn.com/jquery.js"></script>

重启服务器,提示输入之前生成 csr 的密码,输入密码,重启成功,访问 https://www.rapospectre.com 可以看到 HTTPS 已经正常工作!

顺手来一发 SSLLABS测试,wtf 只有 F?

看图发现因为

This server is vulnerable to the OpenSSL Padding Oracle vulunerability ( CVE-2016-2107 )

原来是 OpenSSL 漏洞的锅,升级 OpenSSL 到 1.0.2h 版 ( 后续版本应该也可以,博主一开始升级到了最新的 1.1.0a 结果服务器挂了 ) 即可修复漏洞:

Fix OpenSSL Padding Oracle vulnerability (CVE-2016-2107) - Ubuntu 14.04

# Based on http://fearby.com/Article/update-openssl-on-a-digital-ocean-vm/

$ apt-get update
$ apt-get dist-upgrade

$ wget ftp://ftp.openssl.org/source/old/1.0.2/openssl-1.0.2h.tar.gz
$ tar -xvzf openssl-1.0.2h.tar.gz
$ cd openssl-1.0.2h
$ ./config --prefix=/usr/
$ make depend
$ sudo make install
$ openssl version
# OpenSSL 1.0.2h  3 May 2016

# now restart your nginx or other server
$ nginx -s reload

4. HTTP2

开启 http2 ,nginx 在 1.9.5 以后的版本才开始支持 http2 ,之前一直使用的是 spdy 而 ubuntu 自带的 nginx 是 1.4.6 的古董, 所以需要重新编译安装新版的 nginx ,博主选择了安装最新的 nginx 1.11.4:

1. 下载 nginx 到 /var/tmp/nginx:

wget http://nginx.org/download/nginx-1.11.4.tar.gz

2. 解压nginx-1.11.4.tar.gz文件

tar zxvf nginx-1.11.4.tar.gz

3. 进入ngixn-1.11.4文件夹

cd nginx-1.2.5

4. 查看nginx原来的配置

nginx -V

上面的命令将输出类似如下信息:

--with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module

我们在后面加上 http2 模块与 上一步中 openssl 源码( 是源码路径不是安装 )路径:

--with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h

注意,如果以上信息内包含 --with-spdy_module 请去除,nginx 1.9.5 之后已弃用 spdy

5. 执行configure命令,后面跟上原来nginx的配置

./configure --with-cc-opt='-g -O2 -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro' --prefix=/usr/share/nginx --conf-path=/etc/nginx/nginx.conf --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --lock-path=/var/lock/nginx.lock --pid-path=/run/nginx.pid --http-client-body-temp-path=/var/lib/nginx/body --http-fastcgi-temp-path=/var/lib/nginx/fastcgi --http-proxy-temp-path=/var/lib/nginx/proxy --http-scgi-temp-path=/var/lib/nginx/scgi --http-uwsgi-temp-path=/var/lib/nginx/uwsgi --with-debug --with-pcre-jit --with-ipv6 --with-http_ssl_module --with-http_stub_status_module --with-http_realip_module --with-http_addition_module --with-http_dav_module --with-http_geoip_module --with-http_gzip_static_module --with-http_image_filter_module --with-http_sub_module --with-http_xslt_module --with-mail --with-mail_ssl_module --with-http_v2_module --with-openssl=/var/tmp/ssl/openssl-1.0.2h

configure时可能遇到的几个错误:

  1. --with-http_xslt_module 时提示 the HTTP XSLT module requires the libxml2/libxslt libraries
    apt-get install libxml2 libxml2-dev libxslt-dev
  2. --with-http_image_filter_module 时提示 the HTTP image filter module requires the GD library.
    apt-get install libgd2-xpm-dev
  3. --with-http_geoip_module 时提示 the GeoIP module requires the GeoIP library.
    apt-get install geoip-database libgeoip-dev
  4. ./configure: error: the HTTP rewrite module requires the PCRE library.
    apt-get install libpcre3 libpcre3-dev
  5. 再次执行 configure 命令, 然后make && make install。 编译好以后objs目录下多出一个nginx文件,用它替换旧的 nginx 文件:
    mv /usr/sbin/nginx /usr/sbin/nginx-backup
    cp objs/nginx /usr/sbin/nginx

    执行/usr/sbin/nginx -t 命令检查配置文件返回下面的信息:

    nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    nginx: configuration file /etc/nginx/nginx.conf test is successful

    表示 nginx 升级成功,修改 nginx 配置,加入 http2 支持:

    listen 443 ssl http2 fastopen=3 reuseport;

    重启 nginx 访问正常后再测一发:

    搞定,个人网站加入 HTTPS 并且 SSLABS 评分 A+ 。 快来试试吧~

    ( 博主网站图片上传到七牛,而七牛免费似乎账户不支持 https 链接,所以有些文章比如说这篇会提示网页内有不安全的内容 )

【声明】:8090安全小组门户(http://www.8090-sec.com)登载此文出于传递更多信息之目的,并不代表本站赞同其观点和对其真实性负责,仅适于网络安全技术爱好者学习研究使用,学习中请遵循国家相关法律法规。如有问题请联系我们:邮箱hack@ddos.kim,我们会在最短的时间内进行处理。