OpenVPN结合OpenLDAP认证

目录

安装OpenVPN服务器

安装openvpn及easyrsa

yum install epel-release -y
yum install openvpn easy-rsa -y

配置CA变量:

## 复制easyrsa脚本到openvpn
cd /etc/openvpn/
cp -r /usr/share/easy-rsa /etc/openvpn/
## 准备变量用于配置CA机构
cd /etc/openvpn/easy-rsa/3/
vim vars
set_var EASYRSA                 "$PWD"
set_var EASYRSA_PKI             "$EASYRSA/pki"
set_var EASYRSA_DN              "cn_only"
set_var EASYRSA_REQ_COUNTRY     "ID"
set_var EASYRSA_REQ_PROVINCE    "Shanghai"
set_var EASYRSA_REQ_CITY        "Shanghai"
set_var EASYRSA_REQ_ORG         "Tars VPN CERTIFICATE AUTHORITY"
set_var EASYRSA_REQ_EMAIL       "it@tars.com"
set_var EASYRSA_REQ_OU          "mgt-vpn-server1 EASY CA"
set_var EASYRSA_KEY_SIZE        4096      ## 用于生成Diffie-Hellman密钥,位数越高生成时间越久
set_var EASYRSA_ALGO            rsa
set_var EASYRSA_CA_EXPIRE       7500
set_var EASYRSA_CERT_EXPIRE     365       ## 客户端证书有效期,建议长一些
set_var EASYRSA_NS_SUPPORT      "no"
set_var EASYRSA_NS_COMMENT      "Tars VPN CERTIFICATE AUTHORITY"
set_var EASYRSA_EXT_DIR         "$EASYRSA/x509-types"
set_var EASYRSA_SSL_CONF        "$EASYRSA/openssl-1.0.cnf"
set_var EASYRSA_DIGEST          "sha256"
## 添加可执行权限
chmod +x vars

建立CA颁发机构

cd /etc/openvpn/easy-rsa/3/
./easyrsa init-pki
./easyrsa build-ca		## 执行过程中需要设置CA机构密码,最少6位,其余选项回车即可

构建OpenVPN服务器证书

./easyrsa gen-req tars-vpn-server nopass
./easyrsa sign-req server tars-vpn-server  ##系统将要求您输入"CA"密码,输入密码,然后按Enter。您将在“ pki/issued/”目录下获得"tars-vpn-server.crt"证书文件。
openssl verify -CAfile pki/ca.crt pki/issued/tars-vpn-server.crt   ##验证生成的证书文件

## 已创建所有服务器证书密钥。服务器私钥位于"pki/private/tars-vpn-server.key",服务器证书位于"pki/issued/tars-vpn-server.crt"。

构建OpenVPN客户端证书

./easyrsa gen-req yexinlei nopass   ##这里可以为每个人创建一个证书也可以创建一个公共证书
./easyrsa sign-req client yexinlei	## ca签发yexinlei的证书,先yes,后输入密码
openssl verify -CAfile pki/ca.crt pki/issued/client01.crt ## 验证客户端证书

生成Diffie-Hellman密钥

./easyrsa gen-dh    ## 生成时间取决于我们机器配置以及vars文件中定义的长度,当然越长越安全。

配置OpenVPN证书归纳

整理证书到OpenVPN规定目录

## 服务器证书
cp pki/ca.crt /etc/openvpn/server/
cp pki/issued/tars-vpn-server.crt /etc/openvpn/server/
cp pki/private/tars-vpn-server.key /etc/openvpn/server/

## 客户端证书
cp pki/ca.crt /etc/openvpn/client/
cp pki/issued/yexinlei.crt /etc/openvpn/client/
cp pki/private/yexinlei.key /etc/openvpn/client/

复制Diffie-Hellman密钥和CRL密钥

cp pki/dh.pem /etc/openvpn/server/
cp pki/crl.pem /etc/openvpn/server/

配置OpenVPN服务器

cd /etc/openvpn/
mkdir -p /var/log/openvpn	## 创建日志文件
vim server.conf		## 编辑配置文件

# OpenVPN Port, Protocol and the Tun
port 54911

proto udp
dev tun

# OpenVPN Server Certificate - CA, server key and certificate
ca /data1/openvpn/server/ca.crt
cert /data1/openvpn/server/mgt-vpn-server1.crt
key /data1/openvpn/server/mgt-vpn-server1.key

# DH and CRL key
dh /data1/openvpn/server/dh.pem

# Revoked certificates if we create the list
# crl-verify /data1/openvpn/server/crl.pem


# push "redirect-gateway def1"      ## 所有客户端的默认网关都将重定向到VPN


server 10.254.254.0 255.255.255.0       ## vpn地址池
push "route 10.3.0.0 255.255.0.0"       ## 推送路由表
push "route 10.10.0.0 255.255.0.0"


# DNS configuration
push "dhcp-option DNS 10.3.2.20"        ## 推送DNS信息
#push "dhcp-option DNS 10.3.2.10"



# Domain search configuration. 
# The latest OpenVPN client versions for Windows do not recognize option DOMAIN-SEARCH correctly, and work with DOMAIN
#push "dhcp-options DOMAIN mgt.tarscorp.com"
push "dhcp-option DOMAIN-SEARCH mgt.tarscorp.com"

duplicate-cn        ## 允许多个客户端使用相同的证书密钥进行连接

# TLS Security
cipher AES-256-CBC
tls-version-min 1.2
tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256
auth SHA512
auth-nocache

# Other Configuration
keepalive 20 60
persist-key
persist-tun
comp-lzo yes
daemon
user nobody
group nobody

# LDAP authentication
auth-user-pass-verify auth_ldap.sh via-env
script-security 3
reneg-sec 0     ## 禁用TSL重协商

# OpenVPN Log
log-append /var/log/openvpn/vpn.log
# management 10.254.1.1 7656        ## 管理接口,可以搭配 https://github.com/AuspeXeu/openvpn-status 进行使用
status /var/log/openvpn/status.log  ## 记录当前在线vpn用户状态
verb 3

配置OpenVPN结合OpenLDAP认证脚本

yum install openvpn-auth-ldap openldap-clients -y

vim auth_ldap.sh
#!/bin/bash
# :mode=shellscript
#
# Gets environment from OpenVPN and Auth to LDAP Server

set >> /tmp/auth-user

ldap_base="ou=Users,dc=tars,dc=com"
ldap_host="mgt-ldap-server1"

#
# Bind As and Search For Self
ldapsearch \
    -h ${ldap_host} \
    -b "uid=${username},${ldap_base}" \
    -D "uid=${username},${ldap_base}" \
    -w "${password}" \
    "uid=${username}" >/dev/null 2>&1

# Return Success (0) or Not Success
R="$?"
echo "$R" >> /tmp/auth-user
exit $R

chmod +x auth_ldap.sh

配置系统支持OpenVPN

## 配置内核
echo 'net.ipv4.ip_forward = 1' >> /etc/sysctl.conf
sysctl -p

## 配置iptables
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -A FORWARD -i eth0 -o tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT

## 启动及开机自启
systemctl start openvpn@server
systemctl enable openvpn@server


## 若使用云环境,请开放OpenVPN的安全组权限

配置OpenVPN客户端

## 层级关系
openvpn
└── 10.10.10.1-dev
    ├── 10.10.10.1-dev.ovpn
    └── certs
        ├── ca.crt
        ├── yexinlei.crt
        └── yexinlei.key

## 客户端配置文件
vim 10.10.10.1-dev.ovpn

client
dev tun
proto udp

remote 10.10.10.1 54911

ca certs/ca.crt
cert certs/yexinlei.crt
key certs/yexinlei.key

reneg-sec 0
auth-user-pass


cipher AES-256-CBC
auth SHA512
auth-nocache
tls-version-min 1.2
tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-256-CBC-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-CBC-SHA256

resolv-retry infinite
compress lzo
nobind
persist-key
persist-tun
mute-replay-warnings
verb 3

常见问题:

使用openVPN登录后所有流量全部走openvpn?

1、openvpn的配置及客户端文件是否指定如下字段

push "redirect-gateway def1 bypass-dhcp" ##该策略会修改计算机的默认网关为openVPN

2、若不存在检查openVPN客户端是否有重定向流量开关定义。这种问题主要发生在Linux系统中

deepin 系统在openvpn下包含“仅应用于相对应的网络上的资源”选项,勾选该选项意味着只会增加openVPN推送的路由,而不会重定向所有流量。具体Ubuntu及debian等系统各位仔细查看响应的配置

3、如果是在找不到问题可以尝试使用openvpn命令连接,借助expect自动化登录,而不借助系统或第三方openVPN工具

#!/usr/bin/expect
spawn sudo openvpn --config 10.10.10.1-dev.ovpn
expect {
        "*Username"        {send "xxxx\r";exp_continue}
        "*Password"     {send "xxxx\r"}
}
expect eof

如何多实例openVPN server?

场景是我们印度一台服务器做openVPN,但由于不可抗原因端口国内经常不可用,国外访问正常,所以满足国内需要经常变更端口,那么每次修改端口给国外的小伙伴并不友好。所以可以在一台服务器上部署两组vpn实例,分别给国内与国外使用,而修改端口也不影响印度同事正常使用。

分别配置不同实例的启动加载配置文件,以下为参考

[Unit]
Description=OpenVPN Robust And Highly Flexible Tunneling Application On %I
After=network.target

[Service]
Type=notify
PrivateTmp=true
ExecStart=/usr/sbin/openvpn --config /data1/openvpn/server-india.conf   ## 这里指向不同配置文件

[Install]
WantedBy=multi-user.target

客户端如何同时连接两个openvpn ?

默认Windows安装只会有一个“TAP-Windows Adapter V9”的虚拟网卡,只能满足一个openVPN连接,所以使用自带工具添加

Windows主机:
以管理员身份运行该文件--> "C:\Program Files\TAP-Windows\bin\addtap.bat" --> 提示“Drivers installed successfully.”按任意键退出
重新打开vpn尝试同时连接两个vpn

OpenVPN无法与其他vpn工具共存?

理论上只要路由不冲突,多个vpn共存完全不是问题,所以从路由表上开始排查。分别单独登录vpn工具,并记录每个vpn的路由表,然后作对比,排查是否存在路由冲突网关重定向,这两样都会导致无法共存

Windows:
	route print
Linux:
    route -n