« 在 Windows 系統掛載 UNIX NFS 分享目錄 | 首頁 | 在 Linux NAT 監看 MSN 聊天內容 »

2005年11月30日

使用 Linux FreeS/WAN 構連 IPSEC LAN-to-LAN VPN 網路

目的:

以 Fedora Core 1 Linux NAT + FreeS/WAN 為中心, 分別向外構連 IPSEC LAN-to-LAN VPN

1. Linux NAT 與市售硬體 VPN Route 構連
2. Linux NAT 與另一台 Linux NAT 構連

環境:

‧ Fedora Core 1 Linux NAT + FreeS/WAN 2.06
‧ Trustix 2.2 Linux NAT + Openswan 2.2.1
‧ DrayTek Vigor 2900 VPN Router (Firmware v2.5.5)
‧ ifNetworking SVR8104 VPN Router (Firmware v1.40.1)

Linux NAT 與市售硬體 VPN Route 構連

環境: 直接連接 Internet 的 Fedora Core 1 Linux NAT (kernel: 2.4.22)

Linux NAT 的設立方式請參考本站: Building Linux NAT with MRTG network monitoring

‧下載 FreeS/WAN source tarball, 並在 /usr/src 解包:

cd /usr/src
wget ftp://ftp.xs4all.nl/pub/crypto/freeswan/freeswan-2.06.tar.gz
tar -zxf freeswan-2.06.tar.gz

‧下載 Linux Kernel Source (以下三種方式皆可):

yum install kernel-source

wget ftp://ftp.isu.edu.tw/Linux/Fedora/linux/core/1/i386/os/Fedora/RPMS/kernel-source-2.4.22-1.2115.nptl.i386.rpm

wget ftp://ftp.isu.edu.tw/Linux/Fedora/linux/core/updates/1/i386/kernel-source-2.4.22-1.2199.nptl.i386.rpm

wget 下載之 kernel source 版本須與系統 kernel 版本一致 (可由 uname -r 查詢得知)
rpm 檔下載後直接安裝即可: rpm -ivh kernel-source*

‧安裝 FreeS/WAN:

cd /usr/src/linux-2.4
make oldconfig
cd ../freeswan-2.06
make menumod
(出現 menu 畫面時, 直接 Exit -> Save)
make minstall
cd /lib/modules/2.4.22-1.2199.nptl/kernel
cp ../../2.4.22-1.2199-nptlcustom/kernel/ipsec.o ./
ln -s ../../2.4.22-1.2199-nptlcustom/kernel/ipsec.o ./ipsec.o
depmod -a

最後可執行 modprobe ipsec && lsmod | grep ipsec 確定 ipsec 模組是否已正確安裝
以及 service ipsec start && ipsec verify 啟動 ipsec 服務後, 檢查是否符合 ipsec 需求環境

‧修改相關設定檔:

vi /etc/sysctl.conf

net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter =
0

vi /etc/ipsec.conf

config setup
  interfaces=%defaultroute
  klipsdebug=none
  plutodebug=none

conn %default
  auto=start
  auth=esp
  authby=secret
  keyingtries=%forever

conn vigor
  left=%defaultroute
  leftsubnet=192.168.0.0/24
  right=www.your.com.tw
  rightsubnet=10.0.0.0/24
  pfs=no

conn svr8104
  left=%defaultroute
  leftsubnet=192.168.0.0/24
  right=61.61.61.61
  rightnexthop=61.61.61.254
  rightsubnet=10.0.1.0/24
  pfs=yes

說明:

config setup 是 ipsec service 基本設定
config 和 conn 都是設定區塊的起始行
其下的設定參數都要用空格或 Tab 內縮編排
否則 service ipsec start 時會出現錯誤訊息

conn %default 是各通道 (tunnel) 的共通設定
若不設定 conn %default 可將本區塊裡的設定值
分別寫入各通道的區塊中

conn vigor 在本例是指連結到 Vigor VPN Router 的設定
其中 vigor 為通道名稱

left 為本地端網路 public ip; right 為遠端網路 public ip
leftnexthop / rightnexthop 分別為本地 / 遠端 public gw
leftsubnet / rightsubnet 分別為本地 / 遠端 LAN network
pfs 指是否啟用 Perfect Forward Secrecy
auto = start 表示 ipsec 服務啟動後自動連線 VPN 通道
auth=esp 表示以 IPSEC ESP 方式傳送驗證資料
authby=secret 表示驗證碼置於 ipsec.secrets 檔案中
keyingtries=%forever 表示永遠嘗試連線

ps. 預設 ESP: 3DES-MD5-96 (96 為 Group 1, 768bits)

vi /etc/ipsec.secrets (將以下兩行加到最後)

linux.nat.public.ip vigor.public.ip: PSK "your_preshared_key_string"
linux.nat.public.ip svr8104.public.ip: PSK "your_preshared_key_string"

‧設定 VPN Router:

Vigor 2900 > Advanced Setup > VPN and Remote Access Setup > LAN-to-LAN Profile Setup

Vigor 2900 Web 操作介面在 Firefox 瀏覽器下的相容性較好, 建議以 Firefox 進行操作
IKE Pre-Shared Key 須與 /etc/ipsec.secrets 裡的設定值一樣
Advance 裡的設定值保持預設值即可

  

  

  

  

SVR8104 > VPN 設定值

This Tunnel: Enable

Tunnel Name: fc1

Local Secure Group: Subnet - 依 SVR8104 LAN 環境設定

Remote Secure Group: Subnet - 依 Linux NAT LAN 環境設定

Remote Secure Gateway: IP Addr. - 設定為 Linux NAT Public IP

Encryption: 3DES

Authentication: MD5

Key Management: Auto. (IKE)

PFS (Perfect Forward Secrecy): 若打勾, 須在 /etc/ipsec.conf 設定 pfs=yes, 反之: pfs=no

Pre-Shared Key: 須與 /etc/ipsec.secrets 裡的設定值一樣

Advanced: Proposal 1: Encryption: 3DES; Authentication: MD5; Group: 768-bit

(以上未提到的部份保持預設值即可)

‧啟動 ipsec service 並測試 VPN 連線

service ipsec start

ping -I linux.nat.lan.ip vigor.lan.ip

ping -I linux.nat.lan.ip svr8104.lan.ip

使用 Linux NAT 自己的 LAN IP 去 ping 遠端 LAN IP
若可 ping 到, 表示 VPN 連線已成功建立
測試時請注意防火牆設定是否會防礙兩地網路傳輸

‧修改 iptables nat MASQUERADE 設定, 讓 Linux LAN User 可透過 VPN 與遠端電腦連線

iptables -t nat -A POSTROUTING -s linux.nat.lan/24 -d ! vpn.remote.lan/24 -j MASQUERADE

iptables -t nat -D POSTROUTING 1 (刪除原先的 MASQUERADE 設定)

Linux NAT 與另一台 Linux NAT 進行 VPN 構連

環境:

  • 直接連接 Internet 的 Fedora Core 1 Linux NAT (kernel: 2.4.22) + FreeS/WAN 2.06
  • 直接連接 Internet 的 Trutix Linux 2.2 (kernel: 2.4.31) + Openswan 2.2.1

Fedora Core 1 安裝 FreeS/WAN 的方法請參考前述
Trustix 2.2 已內含 Openswan 2.2.1 (若無可 swup --install openswan)

前置動作:

分別將自己的 public 存到 /root

ipsec showhostkey --left > /root/myrsakey.public

若沒有 public key 則自行產生, 再進行上一動

ipsec newhostkey -bits 2896 --output /etc/ipsec.secrets

將 /root/myrsakey.public 匯入本機 /etc/ipsec.conf
並傳到另一部 Linux NAT, 將其匯入 /etc/ipsec.conf
記得將 "對方" 的 "leftrsasigkey" 改成 "rightrsasigkey"

因 FreeS/WAN 2.06 裝好後, 預設 RSA Key 的長度是 2896 bits
所以在 Openswan 也建立一個相同長度的 RSA Key

Fedora Core 1

vi /etc/ipsec.conf

conn trutix
  left=fc1.public.ip
  leftsubnet=192.168.0.0/24
  leftnexthop=fc1.public.gw.ip
  right=trutix.public.ip
  rightsubnet=10.0.2.0/24
  rightnexthop=trutix.public.gw.ip
  auto=start
  authby=rsasig
  pfs=no
  keyingtries=%forever
  leftrsasigkey=xxxxxx.....==
(自己的 RSA public key)
  rightrsasigkey=xxxxxx.....== (對方的 RSA public key)

Trustix 2.2

vi /etc/ipsec.conf

同上, 將 left 和 right 內容對調即可

啟動 ipsec service 和修改 iptables nat MASQUERADE 設定請參考前述


參考資料:

相關網頁:

本站 Linux NAT 相關文章:


--- 2006/05/15 補充:

FreeS/WAN 基本設置:

conn mytunnel

left=%defaultroute
leftsubnet=my.lan.net.0/24
right=remote.ip.addr
rightnexthop=remote.gw.ip.addr
rightsubnet=remote.lan.net.0/24
auth=esp
pfs=yes

預設值如下:

IKE Proposal:

  • 協商模式 (Negotiation mode): Main
  • 加密演算法 (Encryption): 3DES
  • 驗證演算法 (Authentication): SHA1
  • SA 壽命 (秒): 3600
  • 金鑰群組 (Key Group): DH2 (1024bit)

IPSec Proposal:

  • 通訊協定 (IPSec Protocol): ESP
  • 加密演算法 (Encryption): 3DES
  • 驗證演算法 (Authentication): MD5
  • SA 壽命 (秒): 28800
  • 完美保密 (PFS): DH2 (1024bit)

Posted by Jamyy at 2005年11月30日 12:38

Trackback Pings

TrackBack URL for this entry:
http://cha.homeip.net/cgi-bin/mt/mt-tb.cgi/145

Comments

你好,看了你的有关VPN的文章很受启发,多谢了~~~!

Posted by: lspzw at 2006年11月14日 11:56

請問一下freeswan要使用iptables必須額外安裝什麼樣的patch嗎?

Posted by: peter at 2007年06月04日 16:25

不需要

Posted by: Jamyy at 2007年06月04日 16:54

站長您好,不好意思又要麻煩您了
因為小弟的公司對外的連線是adsl
是由Linux NAT 架構出來的

目前我只是用iptables 來實現nat (如下)
我用2個網卡來隔離內外網路,但是這樣安全嗎?
我想問的是,我這一台Linx NAT 主機,可能要在做些什麼防護
的動作會比較好呢?
#!/bin/bash
/sbin/iptables -F -t filter
/sbin/iptables -F -t nat
/sbin/iptables -X
/sbin/iptables -t nat -X

/bin/echo "1" > /proc/sys/net/ipv4/ip_forward
/bin/echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_all
/bin/echo "1" > /proc/sys/net/ipv4/icmp_echo_ignore_broadcasts
/bin/echo "0" > /proc/sys/net/ipv4/conf/all/accept_source_route

/sbin/modprobe ip_tables
/sbin/modprobe ip_nat_ftp
/sbin/modprobe ip_conntrack
/sbin/modprobe ip_conntrack_ftp
/sbin/modprobe ip_conntrack_irc
/sbin/iptables -t nat -A POSTROUTING -o ppp0 -x.x.x.x/24 -j MASQUERADE

Posted by: 阿榮 at 2007年06月20日 12:44

阿榮您好
以下幾個方向供您參考:

1. 留意每天的 system log report, 觀察是否有不尋常的記錄

2. 特別注意 sshd 埠口的安全, 看您是要改埠口, 還是用 tcpwrappers 限制來源 IP 都可以

3. 設置 iptables rules, 僅對外開放需要的埠口, 如: tcp 25, 80, 110; udp 53, 500

4. 利用工具程式提昇系統安全性, 如:

以 Fail2ban 封鎖嘗試侵入的 IP
/blog/archives/2007/06/_fail2ban_ip.html

Portsentry & Logcheck
/blog/archives/2006/03/portsentry_logc.html

Cfengine 自動化管理工具
/blog/archives/2006/03/cfengine.html

5. 提昇系統安全性

iptables limit 參數備忘 (防治 SYN-Flood 碎片攻擊)
/blog/archives/2006/03/iptables_limit.html

ProFTPD 虛擬帳號 (避免被刺探系統帳號)
/blog/archives/2006/12/proftpd.html

在 FC1 建置可 SSL 安全連線的 Mail Server
/blog/archives/2006/07/_fc1_ssl_mail_s.html

Securing Sendmail
/blog/archives/2006/03/mail_server.html

Netfilter Extensions: Patch-O-Matic (iptables 擴充套件)
/blog/archives/2006/01/netfilter_exten.html

L7-filter 安裝實錄 (iptables 擴充套件)
/blog/archives/2005/12/l7filter.html

解決 Linux NAT ip_conntrack: table full 的方法
/blog/archives/2005/06/_linux_nat_ip_c.html

Posted by: Jamyy at 2007年06月20日 14:17

請問站長:

是否可以將Linux OpensWan與另一台的Linux OpensWan,做到Lan to Lan呢?請賜教,謝謝!

Posted by: ann at 2007年06月28日 16:28

可以, 請參考本文 "Linux NAT 與另一台 Linux NAT 進行 VPN 構連"

Posted by: Jamyy at 2007年06月28日 16:58

站長您好:

請問有沒有fedora code 6 安裝OpensWan的過程,我在網路上查詢許多,都沒有完整的安裝過程,所以想請站長您支援,謝謝!

Posted by: vpn at 2007年07月09日 10:58

FC6 的話
yum install openswan 就可以了

Posted by: Jamyy at 2007年07月09日 11:46

請問站長:

我已經架設好OpensWan,並且XP可以撥通VPN,但是如果想用另一台的Linux該如何撥上OpnesWan呢?謝謝

Posted by: AA at 2007年07月12日 15:31

不好意思, 我只研究到 openswan 的 LAN to LAN 而已
我用 google 找到這篇
http://www.natecarlson.com/linux/ipsec-x509.php#clientopenswan
希望能解決您的問題 :)

Posted by: Jamyy at 2007年07月12日 15:44

謝謝站長,那我再自行研究看看了^^

Posted by: AA at 2007年07月12日 15:49

Jamyy's 站長您好:
小弟最近測試openswan(2.4.6)site to site vpn,但是其中ipsec.conf的compress=yes 參數讓我很confusing。

原因如下:
使用compress=yes後,經過測試不論任何檔案種類
(.jpg,.exe,.txt) 都比沒開啟壓縮(compress=no)
來得"慢"。不知道站長有測過相關的功能嗎? 懇請指點小弟.

E-Mail為小弟的MSN,若站長願意可加入我進而向您討教。
小弟也是linux的愛好者之一:)

Posted by: Wesley1981 at 2007年12月14日 16:57