Cyrus Blog

FLAG{S0_H4PPY_C_U_H3R3} (>.<)

使用 acme.sh 为全站更新 HTTPS

本文共 918 字,预计阅读时间 4 分钟。

0x00 acme 基本使用技巧

Step1:获取acme.sh脚本:

1
curl  https://get.acme.sh | sh

本文没有配置反代的情形,也不涉及泛解析。Hexo 的 blog 是直接在生成的静态目录下通过 Apache 调用的。

Step2:需要手动建立给apache存放凭据的目录,任意目录均可,需要和下文一致:

1
mkdir /etc/apache2/ssl/

Step3:启用相关模块:

1
2
3
ln -s mods-available/ssl.load mods-enabled
ln -s mods-available/ssl.conf mods-enabled
ln -s mods-available/socache_shmcb.load mods-enabled

Step4:过以下命令配置:

1
2
3
acme.sh --issue -d cyru1s.com --webroot /var/www/html/root/of/cyru1s/homepage
acme.sh --install-cert -d cyru1s.com --key-file /etc/apache2/ssl/cyru1s.com.key --fullchain-file /etc/apache2/ssl/fullchain.cer --reloadcmd "service apache2 force-reload"
acme.sh --install-cert -d cyru1s.com --cert-file /etc/apache2/ssl/cyru1s.com-cert.pem --key-file /etc/apache2/ssl/cyru1s.com-key.pem --fullchain-file /etc/apache2/ssl/letsencrypt.pem --reloadcmd "service apache2 force-reload"

Step5:最后在 Apache sites-enabled中的配置文件中增加:

1
2
3
4
5
6
7
8
9
10
11
12
13
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/cyru1s.com-cert.pem
SSLCertificateKeyFile /etc/apache2/ssl/cyru1s.com-key.pem
SSLCertificateChainFile "/etc/apache2/ssl/letsencrypt.pem"
SSLCACertificatePath "/etc/apache2/ssl/"
SSLCACertificateFile "/etc/apache2/ssl/letsencrypt.pem"
<Directory cyru1s.com>
AllowOverride All
</Directory>
DocumentRoot /var/www/html/root/of/cyru1s/homepage
ServerName cyru1s.com
</VirtualHost>

即可完成为主页配置 HTTPS。

0x01 一个批量脚本

辣么多二级域名,当然是写一个脚本生成命令啦。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
ssl_path = "/etc/apache2/ssl"

site = [
("cyru1s.com","/var/www/fuck"),
("v1.cyru1s.com","/var/www/html/the"),
("v2.cyru1s.com","/var/www/html/holy"),
("v3.cyru1s.com","/var/www/html/shit"),
("v4.cyru1s.com","/var/www/html/world"),
]

for i in site:
print('acme.sh --issue -d %s --webroot %s'%(i[0],i[1]))
print("\n"*5)
for i in site:
print('acme.sh --install-cert -d %s --key-file %s/%s.key --fullchain-file %s/fullchain.cer --reloadcmd "service apache2 force-reload"'%(i[0],ssl_path,i[0],ssl_path))
print("\n"*5)
for i in site:
print('acme.sh --install-cert -d %s --cert-file %s/%s-cert.pem --key-file %s/%s-key.pem --fullchain-file %s/letsencrypt.pem --reloadcmd "service apache2 force-reload"'%(i[0],ssl_path,i[0],ssl_path,i[0],ssl_path))
print("\n"*32)

for i in site:
print("""
<VirtualHost *:443>
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/%s-cert.pem
SSLCertificateKeyFile /etc/apache2/ssl/%s-key.pem
SSLCertificateChainFile "/etc/apache2/ssl/letsencrypt.pem"
SSLCACertificatePath "/etc/apache2/ssl/"
SSLCACertificateFile "/etc/apache2/ssl/letsencrypt.pem"
<Directory %s>
AllowOverride All
</Directory>
DocumentRoot %s
ServerName %s
</VirtualHost>"""%(i[0],i[0],i[0],i[1],i[0]))

可以快速生成以上命令。

值得注意的是:如果有问题请不要反复尝试,有可能会触发 LetsEncrypt 每小时最高失败次数超过 5 次就 ban 账户的惩罚。

有一种在批量命令执行到某一句 Step4 的第一条命令时失败的情况(报错信息为 Verify error:Invalid response from http://xxxx.com)之类的,可能是不知道什么时候在 Apache 根目录的配置文件中有这样的语句,注释掉可以解决:

1
2
3
4
5
# Alias /.well-known/acme-challenge  /home/.acme

#<Directory /home/.acme >
#Require all granted
#</Directory>

0x02 全站 HTTP 跳转 HTTPS(Apache)

这里不是太复杂,两步即可完成。

Step1:启用相关模块:

1
ln -s mods-available/rewrite.load mods-enabled

在 Apache 根目录下的配置文件中,找到<Directory />所在块,进行修改。如果根目录文件没有,可以在根目录配置文件所包含的目录中寻找(通常<Directory />都是在根目录的配置文件中的)。

Step2:修改配置文件,增加 Rewrite 相关的三行代码:

1
2
3
4
5
6
7
8
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
RewriteEngine on
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)?$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
</Directory>

重启整个 Apache 即可。

0x03 可能的小问题

  • 配置第一条 SSL 配置时,Apache 在 reload/restart 时报错,经过journalctl -xe查看错误为:SSLSessionCache: ‘shmcb’ session cache not supported (known names: ). Maybe you need to load the appropriate socache module (mod_socache_shmcb?) 应当载入socache_shmcb.load模块。
  • 最后全站 HTTP 跳转 HTTPS 需要 restart 而不是 reload。