在Linux中使用HTTPS在两个主机之间安全地传输文件

时间:2020-02-23 14:40:26  来源:igfitidea点击:

如何配置Apache HTTP服务器传输文件?
通过TLS/SSL配置apache HTTP服务器以传输文件的步骤。
如何使用TLS/SSL配置Apache服务器以使用curl上载和传输文件。
如何通过https传输文件?
如何使用curl将文件上传到HTTPS服务器。
如何在Linux中使用LIMIT配置HTTP服务器以允许使用curl来下载和上传文件或者目录。

我们可以将HTTPS视为通过网络在多个主机之间安全传输文件(上传和下载)的可能选项之一。

在本文中,将介绍使用TLS/SSL配置apache HTTP服务器,然后使用curl(上传文件)将文件传输到HTTPS服务器的步骤。
我正在使用CentOS 7.4来演示本文中的步骤。
我将配置一个非常基本的HTTP服务器,而无需进行太多自定义,因为如果我们使用所有功能,那么Apache可能会非常复杂。
但是,由于我们专注于配置HTTPS服务器以上传和下载文件(传输文件),因此我们将配置基本的HTTPS服务器。

安装Apache

我们要做的第一件事是在开始配置Web服务器之前安装apache rpm。

[root@node2 ~]# yum -y install httpd

接下来,我们将创建一些虚拟文件和目录,并将其发布到我们的网络服务器上。

[root@node2 ~]# cd /var/www/html/
[root@node2 html]# mkdir secret
[root@node2 html]# chmod 777 secret

我正在创建一个别名/web,它将重定向到/var/www/html/secret
我们也定义了指令。
该指令通过URL限制了随附指令的范围。
它与指令相似,并以该指令开始的子节开始。
在读取节和.htaccess文件之后以及节之后,将按照节在配置文件中出现的顺序处理节。

该指令的目的是将访问控制的影响限制在指定的HTTP方法中。
对于所有其他方法,括号内的访问限制将无效。

[root@node2 ~]# vim /etc/httpd/conf/httpd.conf
Alias /web "/var/www/html/secret"
  <Directory "/var/www/html/secret">
      Options Indexes MultiViews FollowSymLinks Includes ExecCGI
      AllowOverride None
      Allow from all
      Require all granted
  </Directory>
  <Location /web>
     Dav On
     <LimitExcept GET HEAD OPTIONS PUT>
        Order Allow,Deny
        Allow from all
     </LimitExcept>
  </Location>

接下来,重新启动httpd服务以使更改生效。

[root@node2 ~]# systemctl restart httpd

检查服务状态。

[root@node2 ~]# systemctl status httpd
● httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; enabled; vendor preset: disabled)
   Active: active (running) since Sun 2019-04-14 13:52:04 IST; 9min ago
     Docs: man:httpd(8)
           man:apachectl(8)
 Main PID: 3134 (httpd)
   Status: "Total requests: 0; Current requests/sec: 0; Current traffic:   0 B/sec"
    Tasks: 6
   CGroup: /system.slice/httpd.service
           ├─3134 /usr/sbin/httpd -DFOREGROUND
           ├─3255 /usr/sbin/httpd -DFOREGROUND
           ├─3256 /usr/sbin/httpd -DFOREGROUND
           ├─3257 /usr/sbin/httpd -DFOREGROUND
           ├─3259 /usr/sbin/httpd -DFOREGROUND
           └─3260 /usr/sbin/httpd -DFOREGROUND
Apr 14 13:52:04 node2.example.com systemd[1]: Starting The Apache HTTP Server...
Apr 14 13:52:04 node2.example.com systemd[1]: Started The Apache HTTP Server.

启用" httpd"服务以使其重新启动持久。

[root@node2 ~]# systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

创建TLS/SSL证书

我们会将所有密钥和证书存储在/etc/pki/tls/certs /中

让我们从创建2048位SSL密钥开始

[root@node2 html]# cd /etc/pki/tls/certs/
[root@node2 certs]# openssl genpkey -algorithm rsa -pkeyopt rsa_keygen_bits:2048 -out node2.example.com.key
........+++
.....................................................+++

验证创建的密钥node2.example.com.key

[root@node2 certs]# ls -ltr
total 16
-rwxr-xr-x. 1 root root  829 Oct 31 04:12 renew-dummy-cert
-rw-r--r--. 1 root root 2516 Oct 31 04:12 Makefile
-rwxr-xr-x. 1 root root  610 Oct 31 04:12 make-dummy-cert
lrwxrwxrwx. 1 root root   55 Nov 17 17:28 ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
lrwxrwxrwx. 1 root root   49 Nov 17 17:28 ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
-rw-r--r--. 1 root root 1704 Apr 14 11:26 node2.example.com.key

生成密钥后,下一步将生成证书请求(CSR)。
我们可以使用会在最后提示的密码来加密密钥。

[root@node2 certs]# openssl req -new -key node2.example.com.key -out node2.example.com.csr
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) []:KARNATAKA
Locality Name (eg, city) [Default City]:BANGALORE
Organization Name (eg, company) [Default Company Ltd]:theitroad
Organizational Unit Name (eg, section) []:TEST
Common Name (eg, your name or your server's hostname) []:node2.example.com
Email Address []:[email protected]
Please enter the following 'extra' attributes
to be sent with your certificate request
A challenge password []:
An optional company name []:

验证CSR文件

[root@node2 certs]# ls -ltr
total 20
-rwxr-xr-x. 1 root root  829 Oct 31 04:12 renew-dummy-cert
-rw-r--r--. 1 root root 2516 Oct 31 04:12 Makefile
-rwxr-xr-x. 1 root root  610 Oct 31 04:12 make-dummy-cert
lrwxrwxrwx. 1 root root   55 Nov 17 17:28 ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
lrwxrwxrwx. 1 root root   49 Nov 17 17:28 ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
-rw-r--r--. 1 root root 1704 Apr 14 11:26 node2.example.com.key
-rw-r--r--. 1 root root 1078 Apr 14 11:28 node2.example.com.csr

最后签署证书并创建一个CRT文件

[root@node2 certs]# openssl x509 -req -days 365 -signkey node2.example.com.key -in node2.example.com.csr -out node2.exam                                                                                                                     ple.com.crt
Signature ok
subject=/C=IN/ST=KARNATAKA/L=BANGALORE/O=theitroad/OU=TEST/CN=node2.example.com/[email protected]
Getting Private key

验证CRT文件

[root@node2 certs]# ls -ltr
total 24
-rwxr-xr-x. 1 root root  829 Oct 31 04:12 renew-dummy-cert
-rw-r--r--. 1 root root 2516 Oct 31 04:12 Makefile
-rwxr-xr-x. 1 root root  610 Oct 31 04:12 make-dummy-cert
lrwxrwxrwx. 1 root root   55 Nov 17 17:28 ca-bundle.trust.crt -> /etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
lrwxrwxrwx. 1 root root   49 Nov 17 17:28 ca-bundle.crt -> /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem
-rw-r--r--. 1 root root 1704 Apr 14 11:26 node2.example.com.key
-rw-r--r--. 1 root root 1078 Apr 14 11:28 node2.example.com.csr
-rw-r--r--. 1 root root 1354 Apr 14 11:29 node2.example.com.crt

因此,现在我们有了使用SSL配置apache服务器所需的所有密钥。

模块管理(mod_ssl)

Apache Web服务器包含许多模块化功能。
例如,如果没有包含mod_ssl.so模块和ssl.conf配置文件的mod_ssl软件包,则无法建立SSL保护的。

模块中还组织了许多其他类似的系统。
带有LoadModule指令的标准Apache配置文件中包含了已加载的模块。
可用模块的完整列表位于/usr/lib64/httpd/modules目录中,但是除非使用在/etc/httpd/conf.modules.d目录中的相应Apache配置文件中的LoadModule指令加载了可用模块,否则不使用可用模块。

因此,我们将使用'yum'命令安装mod_ssl

[root@node2 certs]# yum install openssl mod_ssl -y

配置SSL

接下来,我们将使用SSL密钥配置apache。

在/etc/httpd/conf.d/ssl.conf文件的末尾添加以下内容。

[root@node2 ~]# vim /etc/httpd/conf.d/ssl.conf
<VirtualHost *:443>
        DocumentRoot "/var/www/html"
        ServerName node2.example.com:443
        SSLCertificateFile /etc/pki/tls/certs/node2.example.com.crt
        SSLCertificateKeyFile   /etc/pki/tls/certs/node2.example.com.key
</VirtualHost>

重新启动httpd服务

[root@node2 ~]# systemctl restart httpd

接下来,验证Web服务器。
使用https连接到" node2.example.com"。

以下是我们生成的证书

因此,我们的apache服务器已使用SSL正确配置。

禁用SELinux

为了本文的方便,我们已禁用SELinux策略。

如下所示,将"/etc/selinux/config"中的SELINUX更改为" Disabled"。

SELINUX=disabled

并重新启动节点。

节点启动后,检查selinux状态

[root@node2 ~]# getenforce
Disabled

在HTTPS上使用curl传输文件

接下来,我们讨论本文的主题。
我们将创建一个虚拟文件,尝试使用curl将其上传到HTTPS服务器。

[root@node2 ~]# touch /tmp/secret_file

使用以下命令,我们将/tmp/secret_file上传到提供的Web服务器位置

说明:

由于我们没有认证的CA,因此我们在此处使用--insecure参数。

[root@node2 ~]# curl --insecure  -T /tmp/secret_file https://node2.example.com/web/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /web/secret_file has been created.</p>
</body></html>
[root@node2 ~]# ls -l /var/www/html/secret/
total 0
-rw-r--r-- 1 apache apache 0 Apr 14 14:03 secret_file

我们也可以使用特定用户上载文件,以增强安全性。
要使用用户,必须将Apache配置为允许特定用户访问。

我写了另一篇文章,其中介绍了对用户进行身份验证以使用apache服务器的步骤。

将用户放置到位后,请尝试以下命令,并将root:redhat替换为user:password
其中我们将"/tmp"位置下的" root_file"上传到我们的Web服务器。

[root@node2 ~]# touch /tmp/root_file
[root@node2 ~]# curl --insecure -u root:redhat -T /tmp/root_file https://node2.example.com:443/web/
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>201 Created</title>
</head><body>
<h1>Created</h1>
<p>Resource /web/root_file has been created.</p>
</body></html>
[root@node2 ~]# ls -l /var/www/html/secret/
total 0
-rw-r--r-- 1 apache apache 0 Apr 14 14:04 root_file
-rw-r--r-- 1 apache apache 0 Apr 14 14:04 secret_file