实战案例:Openvpn部署及相关实现

702 阅读8分钟

1.实验目标

  1. 搭建Openvpn
  2. 实现用户密码验证
  3. 证书的私钥加密码
  4. 给新用户颁发新证书:用脚本实现
  5. 给离职的用户吊销证书:用脚本实现

2.openvpn部署

购买并且配置云服务器

image.png

image.png

server服务器的公网地址:118.195.181.27,server私网地址为172.30.0.10。

内部网络的web1私网地址:172.30.0.13,web2私网地址:172.30.0.4

2.1安装 OpenVPN 软件包

首先,查看OpenVPN 版本

[root@centos8 ~]#yum list easy-rsa

通过阿里云镜像平台,配置epel源来下载easy-rs

image.png

yum -y install easy-rsa.noarch
#生成服务器配置文件
[root@centos8 ~]#cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf/etc/openvpn/#准备证书颁发相关文件
[root@centos8 ~]#mkdir -p /data/easy-rsa;cp -r /usr/share/easy-rsa/3/* /data/easy-rsa#准备颁发证书相关变量的配置文件
[root@centos8 ~]#cp /usr/share/doc/easy-rsa/vars.example /data/easy-rsa/vars#建议修改给CA和OpenVPN服务器颁发的证书的有效期,可适当加长
[root@centos8 ~]#vim /etc/openvpn/easy-rsa/vars#CA的证书默认有效期为10年,可以适当延长,比如:36500天
#set_var EASYRSA_CA_EXPIRE     3650
#将上面行修改为下面
set_var EASYRSA_CA_EXPIRE      36500#服务器证书默为为825天,可适当加长,比如:3650天
#set_var EASYRSA_CERT_EXPIRE   825 
#将上面行修改为下面
set_var EASYRSA_CERT_EXPIRE    3650 

2.2初始化PKI生成PKI相关目录和文件

[root@centos8 ~]#cd /data/easy-rsa/
[root@centos8 3]#pwd
/etc/openvpn/easy-rsa-server/3
[root@centos8 3]#ls
easyrsa openssl-easyrsa.cnf vars x509-types

image.png

#初始化数据,在当前目录下生成pki目录及相关文件
[root@centos8 3]#./easyrsa init-pki
Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa-server/3.0.7/vars
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /data/easy-rsa/pki
[root@centos8 3]#tree 
.
├── easyrsa
├── openssl-easyrsa.cnf
├── pki          #生成一个新目录及相关文件
│   ├── openssl-easyrsa.cnf
│   ├── private
│   ├── reqs
│   └── safessl-easyrsa.cnf
├── vars
└── x509-types
   ├── ca
   ├── client
   ├── code-signing
   ├── COMMON
   ├── email
   ├── kdc
   ├── server
   └── serverClient
4 directories, 13 files

自己搭建的证书

2.3准备证书相关文件

2.3.1创建 CA 机构环境

[root@centos8 3]#./easyrsa build-ca nopass 
sz pki/ca.crt   #将自签名搭建的证书,传到window上查看

image.png

2.3.1.1创建服务端证书申请

[root@centos8 ~]#cd /etc/openvpn/easy-rsa-server/3
[root@centos8 3]#pwd
/etc/openvpn/easy-rsa-server/3
#创建服务器证书申请文件,其中server是文件前缀
[root@centos8 3]#./easyrsa gen-req server nopass

2.3.1.2颁发服务端证书

[root@centos8 ~]#cd /data/easy-rsa/
[root@centos8 easy-rsa]#./easyrsa sign server server
Type the word 'yes' to continue, or any other input to abort.
 Confirm request details: yes #输入yes回车  

配置服务端证书时需要diff-hellman密钥,下面开始创建密钥

2.3.2创建 Diffie-Hellman 密钥

创建 Diffie-Hellman 密钥

#生成diffie-hellman密钥;
[root@centos8 3]#./easyrsa gen-dh

至此服务器端证书配置完成

2.3.3准备客户端证书环境

#生成客户端用户的证书申请
[root@centos8 easy-rsa]#./easyrsa gen-req feifan nopass

image.png

2.3.4颁发客户端证书

#修改给客户端颁发的证书的有效期
[root@centos8 3]#vim vars #建议修改给客户端颁发证书的有效期,可适当减少,比如:90天
#set_var EASYRSA_CERT_EXPIRE   825 
#将上面行修改为下面
set_var EASYRSA_CERT_EXPIRE 180
#颁发客户端证书
[root@centos8 3]#./easyrsa sign client feifan
Type the word 'yes' to continue, or any other input to abort.
 Confirm request details: yes                      #输入yes后回车

至此客户端证书颁发完成;

2.3.5将CA和服务器证书相关文件复制到服务器相应的目录

[root@centos8 ~]#mkdir /etc/openvpn/certs
[root@centos8 ~]#cp /etc/openvpn/easy-rsa-server/3/pki/ca.crt  /etc/openvpn/certs/
[root@centos8 ~]#cp /etc/openvpn/easy-rsa-server/3/pki/issued/server.crt          /etc/openvpn/certs/ 
​
[root@centos8 ~]#cp /etc/openvpn/easy-rsa-server/3/pki/private/server.key 
/etc/openvpn/certs/         
[root@centos8 ~]#cp /etc/openvpn/easy-rsa-server/3/pki/dh.pem 
/etc/openvpn/certs/
[root@centos8 ~]#ll /etc/openvpn/certs/
total 20
-rw------- 1 root root 1204 Aug  3 20:34 ca.crt
-rw------- 1 root root  424 Aug  3 20:35 dh.pem
-rw------- 1 root root 4608 Aug  3 20:34 server.crt
-rw------- 1 root root 1704 Aug  3 20:35 server.key

2.3.6将客户端私钥与证书相关文件复制到服务器相关的目录

[root@VM-0-10-rockylinux easy-rsa]# mkdir /etc/openvpn/client/feifan
​
[root@VM-0-10-rockylinux easy-rsa]# cp /etc/openvpn/certs/ca.crt /etc/openvpn/client/feifan/
[root@VM-0-10-rockylinux easy-rsa]# cp pki/issued/feifan.crt /etc/openvpn/client/feifan/
[root@VM-0-10-rockylinux easy-rsa]# cp pki/private/feifan.key /etc/openvpn/client/feifan/

2.4准备 OpenVPN 服务器配置文件

2.4.1 服务器端配置文件说明

#将OpenVPN 服务器配置文件拷贝到/etc/openvpn/目录下
cp /usr/share/doc/openvpn/sample/sample-config-files/server.conf /etc/openvpn/

2.4.2 修改服务器端配置文件

[root@centos8 ~]#vim /etc/openvpn/server.conf
​
[root@centos8 ~]#grep '^[a-Z].*' /etc/openvpn/server.conf
port 1194
proto tcp
dev tun
ca /etc/openvpn/certs/ca.crt
cert /etc/openvpn/certs/server.crt
key /etc/openvpn/certs/server.key  # This file should be kept secret
dh /etc/openvpn/certs/dh.pem
server 10.8.0.0 255.255.255.0
push "route 172.30.0.0 255.255.255.0"
keepalive 10 120
cipher AES-256-CBC
compress lz4-v2
push "compress lz4-v2"
max-clients 2048
user openvpn
group openvpn
status /var/log/openvpn/openvpn-status.log
log-append /var/log/openvpn/openvpn.log
verb 3
mute 20
#准备目志相关目录
[root@centos8 ~]#getent passwd openvpn
openvpn:x:993:990:OpenVPN:/etc/openvpn:/sbin/nologin
[root@centos8 ~]#mkdir /var/log/openvpn
[root@centos8 ~]#chown openvpn.openvpn /var/log/openvpn
[root@centos8 ~]#ll -d /var/log/openvpn
drwxr-xr-x 2 openvpn openvpn 6 Aug  3 23:07 /var/log/openvpn

2.5启动 OpenVPN 服务

2.5.1启动 OpenVPN 服务

[root@centos8 ~]#vim /usr/lib/systemd/system/openvpn@.service
[Unit]
Description=OpenVPN Robust And Highly Flexible Tunneling Application On %I
After=network.target
[Service]
Type=notify
PrivateTmp=true
ExecStart=/usr/sbin/openvpn --cd /etc/openvpn/ --config %i.conf
[Install]
WantedBy=multi-user.target
​
#启动OpenVPN服务,注意service名称和文件名不一致
[root@centos8 openvpn]#systemctl daemon-reload 
[root@centos8 openvpn]#systemctl enable --now openvpn@server

成功启动服务之后,通过本地Xshell会话,使用telnet 连接

image.png

2.6 准备 OpenVPN 客户端配置文件

2.6.1生成客户端用户的配置文件

#修改配置文件,内容如下
[root@centos8 ~]#vim /etc/openvpn/client/feifan/client.ovpn
client
dev tun
proto tcp
remote  118.195.181.27  1194              #生产中为OpenVPN公网IP或者FQDN
resolv-retry infinite
nobind
#persist-key
#persist-tun
ca ca.crt
cert feifan.crt 
key feifan.key
remote-cert-tls server
#tls-auth ta.key 1
cipher AES-256-CBC
verb 3 #此值不能随意指定,否则无法通信
compress lz4-v2 #此项在OpenVPN2.4.X版本使用,需要和服务器端保持一致,如不指定,默认使用comp-lz压缩

2.7 实现 OpenVPN 客户端

2.7.1 Windows 配置部署 OpenVPN 客户端

2.7.1.2 Windows 客户端配置准备

保存证书到openvpn 客户端安装目录

#在服务器打包证书并下载发送给windows客户端
[root@centos8 ~]#cd /etc/openvpn/client/feifan/
[root@centos8 feifan]#pwd
/etc/openvpn/client/feifan
[root@centos8 feifan]#tar cf feifan.tar ./
tar: ./feifan.tar: file is the archive; not dumped
[root@centos8 feifan]#ll
total 40
-rw------- 1 root root  1204 Aug  3 21:05 ca.crt
-rw-r--r-- 1 root root   231 Aug  3 23:31 client.ovpn
-rw------- 1 root root  4506 Aug  3 21:05 feifan.crt
-rw------- 1 root root  1704 Aug  3 21:05 feifan.key
-rw-r--r-- 1 root root 20480 Aug  4 10:48 feifan.tar
[root@centos8 wangxiaochun]#tar tf feifan.tar 
./
./feifan.key
./feifan.crt
./ca.crt
./client.ovpn
​

安装完openvpn后找到指定的文件路径,在conf文件夹中,将在openvpn服务器中生成的指定证书打包到此文件夹下并解压。

image.png

image.png

2.7.1.4 在Windows 客户端测试访问OpenVPN后端服务器

image.png

image.png

2.8实现访问VPN服务器的内网主机

2.8.1 OpenVPN服务器打开 ip_forward功能

#在服务器开启ip_forward转发功能
[root@centos8 ~]#echo net.ipv4.ip_forward = 1 >> /etc/sysctl.conf 
[root@centos8 ~]#sysctl -p
net.ipv4.ip_forward = 1

2.8.2 配置实现内网服务器回应外网的请求的路由

2.8.2.1 在内网每个主机上添加路由(本次实验暂未使用)

#阿里云服务器不支持修改路由
[root@rocky8 ~]#route add -net 10.8.0.0/24 gw 172.30.0.1

2.8.2.2 在内网主机指定的路由器上添加路由(本次实验暂未使用)

[root@router ~]#route add -net 10.8.0.0/24 gw 172.30.0.1

2.8.2.3 在OpenVPN服务器配置 iptables 规则

#本次实验使用的方法为
[root@centos8 ~]#iptables -t nat -A POSTROUTING -s 10.8.0.0/24 ! -d 10.8.0.0/24 -j SNAT --to 172.30.0.10
​
[root@centos8 ~]#iptables -vnL -t na

image.png

然后windows客户端就可以通过openvpn来访问内网

image.png

image.png

共享客户端证书,可以使得多个客户端使用一个证书。

image.png

3 OpenVPN 管理

3.1 创建新用户,生成对应的有密码的私钥和证书申请

[root@centos8 3]#./easyrsa gen-req magedu 
Note: using Easy-RSA configuration from: /etc/openvpn/easy-rsa-client/3.0.7/vars
Using SSL: openssl OpenSSL 1.1.1c FIPS  28 May 2019
Generating a RSA private key
...............................................................+++++
................+++++
writing new private key to '/etc/openvpn/easy-rsa-client/3/pki/easy-rsa-
35371.iJfHbs/tmp.9lGUMy'
Enter PEM pass phrase:   
#输入两遍密码

3.2 导入用户证书申请并颁发证书

3.3 实现用户密码认证

基于证书验证的基础上再加上用户名密码验证可以实现更高的安全性

3.3.1 修改服务端配置

[root@centos8 ~]# vim /etc/openvpn/server.conf
# 添加三行,实现服务端支持密码认证方式
script-security 3       # 允许使用自定义脚本
auth-user-pass-verify /etc/openvpn/checkpsw.sh via-env #指定自定义脚本路径
username-as-common-name #开启用户密码验证

3.3.2创建用户密码文件

# 创建用户和密码认证文件,每行是一个用户和密码
[root@centos8 ~]# cat > /etc/openvpn/psw-file <<EOF
test 123456
test2 654321
EOF

[root@centos8 ~]# systemctl restart openvpn@server

3.2.3 修改客户端配置

修改客户端配置文件client.ovpn文件,增加下面一行,使其支持用户名/密码与服务器进行身份验证.

[root@centos8 ~]#vim /etc/openvpn/client/wangxiaochun/client.ovpn
#加下面一行,可以支持用户密码认证
auth-user-pass 

重新登录用test 123456即可