资源描述
OpenVPN HOWTO 中文版
# Loopback 地址
LOOP=127.0.0.1
# 删除旧的 iptables 规则
# 并且临时阻塞网络通信
iptables -P OUTPUT DROP
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -F
# 设置缺省策略
iptables -P OUTPUT ACCEPT
iptables -P INPUT DROP
iptables -P FORWARD DROP
# 阻止外部数据包使用 loopback 地址
iptables -A INPUT -i eth0 -s $LOOP -j DROP
iptables -A FORWARD -i eth0 -s $LOOP -j DROP
iptables -A INPUT -i eth0 -d $LOOP -j DROP
iptables -A FORWARD -i eth0 -d $LOOP -j DROP
# 任何从互联网流入的数据包都必须使用真实互联网地址
iptables -A FORWARD -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A FORWARD -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A FORWARD -i eth0 -s 10.0.0.0/8 -j DROP
iptables -A INPUT -i eth0 -s 192.168.0.0/16 -j DROP
iptables -A INPUT -i eth0 -s 172.16.0.0/12 -j DROP
iptables -A INPUT -i eth0 -s 10.0.0.0/8 -j DROP
# 阻塞 NetBios 数据包流出(如果内网有 windows 机器)。
# 这不会影响 VPN 隧道上的NetBios通信,
# 但它会阻止本地 windows 机器向互联网广播自己。
iptables -A FORWARD -p tcp --sport 137:139 -o eth0 -j DROP
iptables -A FORWARD -p udp --sport 137:139 -o eth0 -j DROP
iptables -A OUTPUT -p tcp --sport 137:139 -o eth0 -j DROP
iptables -A OUTPUT -p udp --sport 137:139 -o eth0 -j DROP
# 检查流向互联网的数据包中源地址的合法性
iptables -A FORWARD -s ! $PRIVATE -i eth1 -j DROP
# 允许本地 loopback
iptables -A INPUT -s $LOOP -j ACCEPT
iptables -A INPUT -d $LOOP -j ACCEPT
# 允许向内的(incoming) ping 操作(可以禁止)
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
# 允许 www 和 ssh 服务(可以禁止)
iptables -A INPUT -p tcp --dport http -j ACCEPT
iptables -A INPUT -p tcp --dport ssh -j ACCEPT
# 允许向内的(incoming) OpenVPN 数据包
# 对于每条 OpenVPN 隧道要将以下每行重复,
# 改变 --dport n 为 OpenVPN UDP 实际端口。
#
# 在 OpenVPN中,端口号由 --port n 选项控制。
# 如果你将这个选型写入配置文件,
# 你可以去掉 '--'后这一串字符。
#
# If you taking the stateful firewall
# approach (参看 OpenVPN HOWTO),
# 那么注释掉下面这一行。
iptables -A INPUT -p udp --dport 5000 -j ACCEPT
# 允许来自 TUN/TAP 设备的数据包。
# 当 OpenVPN 运行于安全模式时,
# 他会对 tun 或 tap 接口上的数据包到达前进行验证。
# 也就是说,这里添加的任何过滤不是必需的,
# 除非你想对有可能溢出隧道的数据包类型进行严格约束。
iptables -A INPUT -i tun+ -j ACCEPT
iptables -A FORWARD -i tun+ -j ACCEPT
iptables -A INPUT -i tap+ -j ACCEPT
iptables -A FORWARD -i tap+ -j ACCEPT
# 允许来自内网的数据包
iptables -A INPUT -i eth1 -j ACCEPT
iptables -A FORWARD -i eth1 -j ACCEPT
# 保持来自本机和内网数据包的连接状态
iptables -A OUTPUT -m state --state NEW -o eth0 -j ACCEPT
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A FORWARD -m state --state NEW -o eth0 -j ACCEPT
iptables -A FORWARD -m state --state ESTABLISHED,RELATED -j ACCEPT
# 伪装本地子网
iptables -t nat -A POSTROUTING -s $PRIVATE -o eth0 -j MASQUERADE
OpenVPN 在防火墙设置中提供少量的附加选项:
If both OpenVPN peers reference the other with an explicit --remote option, and stateful firewalls that provide UDP connection tracking (such as iptables) exist between the peers, it is possible to run OpenVPN without any explicit firewall rules, if both peers originate regular pings to each other to keep the connection alive. To do this, simply run OpenVPN with the --remote peer option, and specify --ping 15 to ensure that packets flow over the tunnel at least once every 15 seconds.
上面的选项在隧道一端(peer)频繁变更 IP 地址比如说DHCP或拨号时,显得不够方便。在这种情况下,以上简单的防火墙配置将允许任何IP地址通过UDP端口5000(OpenVPN 的缺省UDP端口) 流入数据包。在 OpenVPN 的安全模式下,所有流入隧道的数据或者通过安全验证或者被丢弃,所以它通常被认为是安全的。
如果你选择完全开放 OpenVPN 的 incoming UDP 端口就像上面简单防火墙中的配置一样,你可能会想利用 --tls-auth 选项在 TLS 控制通道上作双倍的验证,同时使用 RSA 密钥和预先分享的密码短语(passphrase)来作为防御 DoS 或 active 攻击的第二道防线。关于--tls-auth 的更多信息,参考 openvpn man page。
创建 RSA 证书和密钥
OpenVPN 有两种安全模式,一种基于使用 RSA 证书和密钥的 SSL/TLS,一种使用预先分享的静态密钥。SSL/TLS + RSA 密钥被证明是一种最安全的选择,静态密钥优势则在于简洁。如果你想使用 RSA 密钥,继续往下读。要使用静态密钥,向前跳到 创建预分享静态密钥 一节.
我们将使用 openssl 命令创建 RSA 证书和密钥,该命令包含在 OpenSSL 库的发布程序中。
RSA 证书是一种公开密钥,在其中还含有其他安全域,比如说证书持有者的 Common Name 或 email 地址。OpenVPN 有能力在进行认证前对这些域进行测试。更多信息参考 openvpn man page 中的 --tls-verify 选项。
在我们的例子中遵从 apache 惯例使用 .crt 扩展名表示证书文件,.key 扩展名表示私钥。私钥文件必须安全保管。证书文件可以自由发布共享。
选择一台机器比如 Office 作为密钥管理主机。
首先编辑文件 /usr/share/ssl/f (这个文件也许在其他地方,可以用 locate f 命令找到它)。
你或许会对它作一些修改:
建立一个目录作为密钥的工作目录,将 dir 指向它。
考虑增加有限期限 default_days 以免你的 VPN 在工作整一年后莫名其妙的终止。
设定 certificate 和 private_key 指向你的根证书 (master certificate authority certificate)和私钥文件(我们马上要生成它)。在下面的例子中,我们假定你的证书文件名为 my-ca.crt,你的私钥文件名为 my-ca.key 。
注意文件 index.txt 和 serial。将 index.txt 清空,serial 初始化为包含一个数字序列比如 01.
如果你狂热的追求密钥长度,那可以将 default_bits 增加到2048。 对于打开 pthread 支持(可以后台处理 RSA 密钥)的 OpenVPN 处理2048位的 RSA 密钥是毫无问题的。甚至没有打开 pthread 支持时也可以使用更长的密钥,但你会在隧道中作 SSL/TLS 密钥协商时感觉到响应时间的延迟。这里有一份选择 RSA 密钥长度的很好的文章,参见 April 2002 issue of Bruce Schneier's Crypto-Gram Newsletter.
f 编辑完后,创建根证书/私钥对(master certificate authority certificate/private-key pair):
openssl req -nodes -new -x509 -keyout my-ca.key -out my-ca.crt -days 3650
这将会创建一对有效期为十年的根证书/私钥对。
现在为 Home 和 Office 创建 证书/私钥对:
openssl req -nodes -new -keyout office.key -out office.csr
openssl ca -out office.crt -in office.csr
openssl req -nodes -new -keyout home.key -out home.csr
openssl ca -out home.crt -in home.csr
现在将 home.crt, home.key, 和 my-ca.crt 文件通过安全途径拷贝到 Home 机器上,尽管实际上只有.key 才被认为是应该保密的。
现在在 Office 机器上创建 Diffie Hellman 参数,使用下列命令:
openssl dhparam -out dh1024.pem 1024
如果在 f 中增大了密钥位数,相应的这里也要把 1024 改为 2048。
对于偏执狂可以考虑去掉上面 openssl 命令中的-nodes 选项。其结果是对每一个私钥进行口令加密,从而保证当有人攻入你的主机盗走你的私钥时仍有一定的安全性。麻烦的是每次当你使用 OpenVPN 时都需要输入口令。更多信息参考 openvpn man page 中的 --askpass 选项。
如果你对手工管理 RSA 密钥觉得困惑,那要指出一点 OpenVPN 可以很好的同任何 X509 证书管理工具或服务包括商业化的 CAs 如 Thawte or Verisign等协同工作。检索 OpenCA 项目可以查看有哪些同证书/密钥管理工具 配合良好的开源项目。
另外,OpenVPN 发布版中包含了一组脚本能够简化 RSA 证书和密钥管理。
在 OpenVPN 上使用商业证书(CAs)的重要提示
It should be noted that OpenVPN's security model in SSL/TLS mode is oriented toward users who will generate their own root certificate, and hence be their own CA. In SSL/TLS mode, OpenVPN authenticates its peer by checking that the peer-supplied certificate was signed by the CA certificate specified in the --ca option. Like the SSL-based secure web, the security of OpenVPN's SSL/TLS mode rests on the infeasibility of forging a root certificate signature.
This authentication procedure works perfectly well if you have generated your own root certificate, but presents a problem if you wish to use the root certificate of a commercial CA such as Thawte. If, for example, you specified Thawte's root certificate in the --ca option, any certificate signed by Thawte would now be able to authenticate with your OpenVPN peer -- certainly not what you would want.
Luckily there is a solution to this problem in the --tls-verify option. This option will allow you to execute a command to check the contents of a certificate, to fine-tune the selection of which certificate is allowed, and which is not. See the script verify-cn in the sample-scripts subdirectory for an example of how to do this, and also see the man page for the --tls-verify option.
使用 SSL/TLS 模式和 RSA 证书/密钥的配置文件
在我们的例子中,我们使用 OpenVPN 配置文件。OpenVPN 允许由命令行或一个或多个配置文件指定参数。在命令行中由"--"引导的参数在配置文件中是可以忽略"--"的,命令行中则不行。
设置下列配置文件:
--------------------------------------------------------------------------------
sample-config-files/tls-office.conf
#
# Sample OpenVPN configuration file for
# office using SSL/TLS mode and RSA certificates/keys.
#
# '#' 或 ';' 用于表明注释.
# 使用 dynamic tun device.
# For Linux 2.2 or non-Linux OSes,
# 你可以使用显式指明的设备号,比如"tun1"
# OpenVPN 也支持虚拟网络 "tap" devices;
dev tun
# 10.1.0.1 is our local VPN endpoint (office).
# 10.1.0.2 is our remote VPN endpoint (home).
ifconfig 10.1.0.1 10.1.0.2
# up 脚本将在 VPN 激活时建立路由
up ./office.up
# 在 SSL/TLS 密钥交换时,Office 作为 server,Home 作为 client
tls-server
# Diffie-Hellman Parameters (tls-server only)
dh dh1024.pem
# Certificate Authority file
ca my-ca.crt
# Our certificate/public key
cert office.crt
# Our private key
key office.key
# OpenVPN 缺省使用 UDP 端口 5000 .
# 每一个 OpenVPN tunnel 必须使用一个不同的端口.
# lport 和 rport 被用于指明本地或远程的不同端口
; port 5000
# 出于安全考虑,初始化后UID 和GID 权限将降为 "nobody"
; user nobody
; group nobody
# 如果你的OpenVPN支持LZO 压缩,去掉这一行的注释
; comp-lzo
#
# 每15秒向远端主机发送一个 UDP ping
# stateful firewall connection
# alive. Uncomment this
# out if you are using a stateful
# firewall.
; ping 15
# Uncomment this section for a more reliable detection when a system
# loses its connection. For example, dial-ups or laptops that
# travel to other locations.
; ping 15
; ping-restart 45
; ping-timer-rem
; persist-tun
; persist-key
# 信息细节等级
# 0 -- 除非发生致命错误,否则保持安静。
# 1 -- 非常安静,但会显示一些非致命网络错误。
# 3 -- 中等输出,通常情况下的很好选择。
# 9 -- 非常详细,用于诊断错误。
verb 3
--------------------------------------------------------------------------------
sample-config-files/office.up
#!/bin/bash
route add -net 10.0.1.0 netmask 255.255.255.0 gw $5
--------------------------------------------------------------------------------
sample-config-files/tls-home.conf
#
# Sample OpenVPN configuration file for
# home using SSL/TLS mode and RSA certificates/keys.
#
# '#' or ';' may be used to delimit comments.
# Use a dynamic tun device.
# For Linux 2.2 or non-Linux OSes,
# you may want to use an explicit
# unit number such as "tun1".
# OpenVPN also supports virtual
# ethernet "tap" devices.
dev tun
# Our OpenVPN peer is the office gateway.
remote 1.2.3.4
# 10.1.0.2 is our local VPN endpoint (home).
# 10.1.0.1 is our remote VPN endpoint (office).
ifconfig 10.1.0.2 10.1.0.1
# Our up script will establish routes
# once the VPN is alive.
up ./home.up
# 在 SSL/TLS 密钥交换时,Office 作为 server,Home 作为 client
tls-client
# Certificate Authority file
ca my-ca.crt
# Our certificate/public key
cert home.crt
# Our private key
key home.key
# OpenVPN 缺省使用 UDP 端口 5000 .
# 每一个 OpenVPN tunnel 必须使用一个不同的端口.
# lport 和 rport 被用于指明本地或远程的不同端口
; port 5000
# 出于安全考虑,初始化后UID 和GID 权限将降为 "nobody"
; user nobody
; group nobody
# 如果你的OpenVPN支持LZO 压缩,去掉这一行的注释
; comp-lzo
# 每15秒向远端主机发送一个 UDP ping
# stateful firewall connection
# alive. Uncomment this
# out if you are using a stateful
# firewall.
; ping 15
# Uncomment this section for a more reliable detection when a system
# loses its connection. For example, dial-ups or laptops that
# travel to other locations.
; ping 15
; ping-restart 45
; ping-timer-rem
; persist-tun
; persist-key
# 信息细节等级
# 0 -- 除非发生致命错误,否则保持安静。
# 1 -- 非常安静,但会显示一些非致命网络错误。
# 3 -- 中等输出,通常情况下的很好选择。
# 9 -- 非常详细,用于诊断错误。
verb 3
--------------------------------------------------------------------------------
sample-config-files/home.up
#!/bin/bash
route add -net 10.0.0.0 netmask 255.255.255.0 gw $5
创建一个预分享静态密钥(Pre-Shared Static Key)
同 RSA 密钥管理极为不同的是,使用预分享静态密钥是十分简单。The major downside of using static keys is that you give up the notion of perfect forward secrecy, meaning that if an attacker steals your static key, everything that was ever encrypted with it is compromised.
使用如下命令生成静态密钥:
openvpn --genkey --secret static.key
静态密钥文件由ascii组成,就像下面这样:
-----BEGIN OpenVPN Static key V1-----
e5e4d6af39289d53
171ecc237a8f996a
97743d146661405e
c724d5913c550a0c
30a48e52dfbeceb6
e2e7bd4a8357df78
4609fe35bbe99c32
bdf974952ade8fb9
71c204aaf4f256ba
eeda7aed4822ff98
fd66da2efa9bf8c5
e70996353e0f96a9
c94c9f9afb17637b
283da25cc99b37bf
6f7e15b38aedc3e8
e6adb40fca5c5463
-----END OpenVPN Static key V1-----
一个 OpenVPN 静态密钥包含有足够的熵值,其中512位作为加密键,512位作为身份验证的 HMAC。(An OpenVPN static key file contains enough entropy to key both a 512 bit cipher key and a 512 bit HMAC key for authentication.)
通过安全的媒介将 static.key 拷贝到另一端(peer),比如使用scp 或 ssh 中的拷贝、粘贴。
使用预共享静态密钥(Pre-Shared Static Key)的配置文件
在我们的例子中,我们使用 OpenVPN 配置文件。OpenVPN 允许由命令行或一个或多个配置文件指定参数。在命令行中由"--"引导的参数在配置文件中是可以忽略"--"的,命令行中则不行。
Set up the following configuration files:
--------------------------------------------------------------------------------
sample-config-files/static-office.conf
#
# Sample OpenVPN configuration file for
# office using a pre-shared static key.
#
# '#' or ';' may be used to delimit comments.
# Use a dynamic tun device.
# For Linux 2.2 or non-Linux OSes,
# you may want to use an explicit
# unit number such as "tun1".
# OpenVPN also supports virtual
# ethernet "tap" devices.
dev tun
# 10.1.0.1 is our local VPN endpoint (office).
# 10.1.0.2 is our remote VPN endpoint (home).
ifconfig 10.1.0.1 10.1.0.2
# Our up script will establish routes
# once the VPN is alive.
up ./office.up
# Our pre-shared static key
secret static.key
# OpenVPN uses UDP port 5000 by default.
# Each OpenVPN tunnel must use
# a different port number.
# lport or rport can be used
# to denote different ports
# for local and remote.
; port 5000
# Downgrade UID and GID to
# "nobody" after initialization
# for extra security.
; user nobody
; group nobody
# If you built OpenVPN with
# LZO compression, uncomment
# out the following line.
; comp-lzo
# Send a UDP ping to remote once
# every 15 seconds to keep
# stateful firewall connection
# alive. Uncomment this
# out if you are using a stateful
# firewall.
; ping 15
# Uncomment this section for a more reliable detection when a system
# loses its connection. For example, dial-ups or laptops that
# travel to other locations.
; ping 15
; ping-restart 45
; ping-timer-rem
; persist-tun
; persist-key
# 信息细节等级
# 0 -- 除非发生致命错误,否则保持安静。
# 1 -- 非常安静,但会显示一些非致命网络错误。
# 3 -- 中等输出,通常情况下的很好选择。
# 9 -- 非常详细,用于诊断错误。
verb 3
--------------------------------------------------------------------------------
sample-config-files/office.up
#!/bin/bash
route add -net 10.0.1.0 netmask 255.255.255.0 gw $5
--------------------------------------------------------------------------------
sample-config-files/static-home.conf
#
# Sample OpenVPN configuration file for
# home using a pre-shared static key.
#
# '#' or ';' may be used to delimit comments.
# Use a dynamic tun device.
# For Linux 2.2 or non-Linux OSes,
# you may want to use an explicit
# unit number such as "tun1".
# OpenVPN also supports virtual
# ethernet "tap" devices.
dev tun
# Our OpenVPN peer is the office gateway.
remote 1.2.3.4
# 10.1.0.2 is our local VPN endpoint (home).
# 10.1.0.1 is our remote VPN endpoint (office).
ifconfig 10.1.0.2 10.1.0.1
# Our up script will establish routes
# once the VPN is alive.
up ./home.up
# Our pre-shared static key
secret static.key
# OpenVPN uses UDP port 5000 by default.
# Each OpenVPN tunnel must use
# a different port number.
# lport or rport can be used
# to denote different ports
# for local and remote.
; port 5000
# Downgrade UID and GID to
# "nobody" after initialization
# for extra security.
; user nobody
; group nobody
# If you built OpenVPN with
# LZO compression, uncomment
# out the following line.
; comp-lzo
# Send a UDP ping to remote once
# every 15 seconds to keep
# stateful firewall connection
# alive. Uncomment this
# out if you are using a stateful
# firewall.
; ping 15
# Uncomment this section for a more reliable detection when a system
# loses its connection. For example, dial-ups or laptops that
# travel to other locations.
; ping 15
; ping-restart 45
; ping
展开阅读全文