如何设置 HTTPS(SSL 证书)以保护 PhpMyAdmin 登录
为了介绍这个技巧,让我们嗅探客户端计算机和 Debian 8 服务器之间的 HTTP 流量,我们在上一篇文章中犯了一个无辜的错误,即使用数据库 root 用户的凭据登录:更改和安全默认 PhpMyAdmin 登录 URL
正如我们在上一篇技巧中提到的,如果您不想公开您的凭据,请不要尝试这样做。为了开始嗅探流量,我们输入以下命令并按 Enter:
tcpdump port http -l -A | egrep -i 'pass=|pwd=|log=|login=|user=|username=|pw=|passw=|passwd=|password=|pass:|user:|username:|password:|login:|pass |user ' --line-buffered -B20
很快我们就会意识到用户名和密码已经以纯文本格式通过网络发送,正如您在 tcpdump 的截断输出中看到的那样下图。
请注意,我们用蓝色标记隐藏了部分root密码:
为了避免这种情况,我们使用证书来保护登录页面。为此,请在基于 CentOS 的发行版上安装 mod_ssl 软件包。
yum install mod_ssl
尽管我们将使用 Debian/Ubuntu 路径和名称,但如果您替换下面的命令和路径,则相同的过程对于 CentOS 和 RHEL 也有效与 CentOS 等效版本。
创建一个目录来存储密钥和证书:
mkdir /etc/apache2/ssl [On Debian/Ubuntu based systems]
mkdir /etc/httpd/ssl [On CentOS based systems]
创建密钥和证书:
----------- On Debian/Ubuntu based systems -----------
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/apache2/ssl/apache.key -out /etc/apache2/ssl/apache.crt
----------- On CentOS based systems -----------
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout /etc/httpd/ssl/apache.key -out /etc/httpd/ssl/apache.crt
样本输出
........................+++
.....................................................+++
writing new private key to '/etc/httpd/ssl/apache.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]:IN
State or Province Name (full name) []:Maharashtra
Locality Name (eg, city) [Default City]:Mumbai
Organization Name (eg, company) [Default Company Ltd]:TecMint
Organizational Unit Name (eg, section) []:TecMint
Common Name (eg, your name or your server's hostname) []:TecMint
Email Address []:[email
接下来,验证密钥和证书。
cd /etc/apache2/ssl/ [On Debian/Ubuntu based systems]
cd /etc/httpd/ssl/ [On CentOS based systems]
ls -l
total 8
-rw-r--r--. 1 root root 1424 Sep 7 15:19 apache.crt
-rw-r--r--. 1 root root 1704 Sep 7 15:19 apache.key
在 Debian/Ubuntu 中,确保 Apache 正在侦听默认站点 (/etc/apache2/sites- available/000-default.conf)并在 VirtualHost 声明中添加 3 行与 SSL 相关的行:
SSLEngine on
SSLCertificateFile /etc/apache2/ssl/apache.crt
SSLCertificateKeyFile /etc/apache2/ssl/apache.key
在基于 CentOS 的发行版中,告诉 Apache 监听端口 443 并在 /etc/httpd/conf/ 中查找 Listen 指令httpd.conf 并在其下方添加以上行。
SSLEngine on
SSLCertificateFile /etc/httpd/ssl/apache.crt
SSLCertificateKeyFile /etc/httpd/ssl/apache.key
保存更改,在 Debian/Ubuntu 发行版上加载 SSL Apache 模块(在 CentOS 中,当您安装 mod_ssl 时会自动加载该模块)强>早先):
a2enmod ssl
强制phpmyadmin使用SSL,确保以下行存在于/etc/phpmyadmin/config.inc.php或/中etc/phpMyAdmin/config.inc.php 文件:
$cfg['ForceSSL'] = true;
并重新启动网络服务器:
systemctl restart apache2 [On Debian/Ubuntu based systems]
systemctl restart httpd [On Debian/Ubuntu based systems]
接下来,启动网络浏览器并输入 https://
(了解如何更改 PhpMyAdmin 登录 URL),如下所示。
重要:请注意,这只是说连接不安全,因为我们使用的是自签名证书。点击高级并确认安全例外:
确认安全异常后,在登录之前,让我们开始嗅探 HTTP 和 HTTPS 流量:
tcpdump port http or port https -l -A | egrep -i 'pass=|pwd=|log=|login=|user=|username=|pw=|passw=|passwd=|password=|pass:|user:|username:|password:|login:|pass |user ' --line-buffered -B20
然后使用与之前相同的凭据登录。流量嗅探器最多只能捕获乱码:
现在就这样,在下一篇文章中我们将分享您如何使用用户名/密码限制 PhpMyAdmin 访问,在此之前请继续关注 Tecmint。