Linux自带的pfkey可以使用两种方式操作搭建VPN环境:一种是使用setkey手动设置SA,这种方式不常用,SA中的SPI,加密密钥、认证密钥等都是要手动填的,比如md5需要128bit,3des 192bits,可想而知有多大不便;另一种由racoon自动协商生成IPsec SA,但它在协商之前需要setkey先设置SP。搭建环境:CentOS release 5.4,内核2.6.18-164.el5xen

拓扑如下:

在每一个VPN网关需要增加默认路由,否则无法使用ping触发协商。

一、使用setkey手动设置方式。
如果之前使用过openswan klips,需要:1,执行service ipsec stop;2,进入/lib/modules/uname -r/kernel/net/key/,执行insmod af_key.ko;3,加载内核模块xfrmuser af_key esp4 ah4 ipcomp xfrm4_tunnel。如果之前没有用过klips,那么直接执行service ipsec restart即可。可以执行如下命令看是否正确加载netkey:

[root@localhost ipsec]# ipsec version

Linux Openswan U2.4.7/K2.6.18-164.el5xen (netkey)

See `ipsec --copyright’ for copyright information.

     在确保Linux是使用netkey的情况下,就可以往下继续操作了。先配置VPN1,VPN2只要把配置里的IP换个位置就可以了,加黑的表示执行的命令行。

[root@localhost racoon]# cd /etc/racoon/

[root@localhost racoon]# vim setkey.conf

[root@localhost racoon]# cat setkey.conf

#!/usr/sbin/setkey-f

#flushSAD and SPD

flush;

spdflush;

add 192.168.95.162 192.168.95.230 esp 0x201 -m tunnel -E 3des-cbc 0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831-A hmac-md5 0x16b844ab6136507eadc4765d782af8d0;

add 192.168.95.230 192.168.95.162 esp 0x301 -m tunnel -E 3des-cbc0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831 -A hmac-md50x16b844ab6136507eadc4765d782af8d0;

spdadd 40.0.0.0/24 50.0.0.0/24 any -P out ipsecesp/tunnel/192.168.95.162-192.168.95.230/use;

spdadd 50.0.0.0/24 40.0.0.0/24 any -P in ipsecesp/tunnel/192.168.95.230-192.168.95.162/use;

[root@localhost racoon]# setkey -f/etc/racoon/setkey.conf

此时VPN1网关的SA、SP已经添加完毕,下面使用一个应用程序倾泻出已添加完的SA。

[root@localhost racoon]# cat dump.c (相关代码可参考UNPV13e,也可从网上直接下载源码)

因为dump.c要使用UNPV13e里的一些库和封装的函数,所以这个.c文件无法直接使用简单的gcc命令执行。我在源码目录已经编译好了dump可执行程序。

[root@localhost racoon]# ./dump (如果使用klips是无法倾泻的,因为klips没有实现倾泻安全联盟相关代码)

SADB_DUMP:10

Sendingdump message:

SADBMessage Dump, errno 0, satype Unspecified, seq 0, pid 27408

Messagesreturned:

SADBMessage Dump, errno 0, satype IPsec ESP, seq 1, pid 27408

SA: SPI=16973824 Replay Window=0 State=Mature

Authentication Algorithm: HMAC-MD5

Encryption Algorithm: 3DES-CBC

Hard lifetime:

0 allocations, 0 bytes0 addtime, 0 usetime

Soft lifetime:

0 allocations, 0 bytes0 addtime, 0 usetime

Current lifetime:

0 allocations, 0 bytes

added at Tue Jan 29 11:11:58 2013, never used

Source address: 192.168.95.230/32

Dest address: 192.168.95.162/32

Proxy address: 0.0.0.0 (IP proto 255)

Authentication key,128 bits: 0x16b844ab6136507eadc4765d782af8d0

Encryption key, 192bits: 0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831

[unknown extension 19]

SADBMessage Dump, errno 0, satype IPsec ESP, seq 0, pid 27408

SA: SPI=16908288 Replay Window=0 State=Mature

Authentication Algorithm: HMAC-MD5

Encryption Algorithm: 3DES-CBC

Hard lifetime:

0 allocations, 0 bytes0 addtime, 0 usetime

Soft lifetime:

0 allocations, 0 bytes0 addtime, 0 usetime

Current lifetime:

0 allocations, 0 bytes

added at Tue Jan 29 11:11:58 2013, never used

Source address: 192.168.95.162/32

Dest address: 192.168.95.230/32

Proxy address: 0.0.0.0 (IP proto 255)

Authentication key,128 bits: 0x16b844ab6136507eadc4765d782af8d0

Encryption key, 192bits: 0x7aeaca3f87d060a12f4a4487d5a5c3355920fae70a96c831

[unknown extension 19]

可以很清楚的看到,两个方向都包含对应的SA,只不过它的SPI显示的不太好看,可以用以下命令查看SPI,即我们在 setkey.conf里指定的。

[root@localhost racoon]# setkey -D

192.168.95.230192.168.95.162

    esp mode=tunnel spi=769(0x00000301) reqid=0(0x00000000)E: 3des-cbc  7aeaca3f 87d060a1 2f4a4487 d5a5c335 5920fae70a96c831A: hmac-md5  16b844ab 6136507e adc4765d 782af8d0seq=0x00000000 replay=0flags=0x00000000 state=maturecreated: Jan 29 11:11:58 2013   current: Jan 29 11:18:14 2013diff: 376(s)    hard: 0(s)      soft: 0(s)last:                           hard: 0(s)     soft: 0(s)current: 0(bytes)       hard: 0(bytes)  soft: 0(bytes)allocated: 0    hard: 0 soft: 0sadb_seq=1 pid=27417 refcnt=0

192.168.95.162192.168.95.230

    esp mode=tunnel spi=513(0x00000201) reqid=0(0x00000000)E: 3des-cbc  7aeaca3f 87d060a1 2f4a4487 d5a5c335 5920fae70a96c831A: hmac-md5  16b844ab 6136507e adc4765d 782af8d0seq=0x00000000 replay=0flags=0x00000000 state=maturecreated: Jan 29 11:11:58 2013   current: Jan 29 11:18:14 2013diff: 376(s)    hard: 0(s)      soft: 0(s)last:                           hard: 0(s)      soft: 0(s)current: 0(bytes)       hard: 0(bytes)  soft: 0(bytes)allocated: 0    hard: 0 soft: 0sadb_seq=0 pid=27417 refcnt=0

二、使用racoon自动协调方式。
也仅配置VPN1。VPN2类似。

[root@localhost racoon]# cd /etc/racoon/

[root@localhost racoon]# vim setkey.conf

[root@localhost racoon]# cat setkey.conf

#!/usr/sbin/setkey-f

#flushSAD and SPD

flush;

spdflush

spdadd 40.0.0.0/24 50.0.0.0/24 any -P out ipsecesp/tunnel/192.168.95.162-192.168.95.230/use;

spdadd 50.0.0.0/24 40.0.0.0/24 any -P in ipsecesp/tunnel/192.168.95.230-192.168.95.162/use;

[root@localhost racoon]# vim racoon.conf

[root@localhost racoon]# cat racoon.conf

RacoonIKE daemon configuration file.

See’man racoon.conf’ for a description of the format and entries.

pathinclude “/etc/racoon”;

pathpre_shared_key “/etc/racoon/psk”;

#pathcertificate “/etc/racoon/certs”;

remote192.168.95.230 {

    exchange_mode main;lifetime time 2 hour;proposal {encryption_algorithm 3des;hash_algorithm sha1;authentication_methodpre_shared_key;dh_group 2;}

}

sainfoanonymous

{

    pfs_group modp768;lifetime time 1 hour ;encryption_algorithm 3des, blowfish448, rijndael ;authentication_algorithm hmac_sha1,hmac_md5 ;compression_algorithm deflate ;

}

[root@localhost racoon]# cat psk

192.168.95.2300x123456

[root@localhost racoon]# chmod 700 psk

[root@localhost racoon]# ls -alh psk

-rwx------1 root root 24 01-28 17:27 psk

注意psk文件的属性,一定不能被除所有者以外的人可读(我把它设置为700),否则racoon协商时会报以下错误:

/etc/racoon/pskhas weak file permission

在配置完VPN1和VPN2后,执行:

[root@localhost racoon]# setkey -f/etc/racoon/setkey.conf

[root@localhost racoon]# racoon -d -F -f/etc/racoon/racoon.conf (-d表示打开详细的调试信息,-F表示前台运行,这样可以直接看到调试信息了)

Foregroundmode.

2013-01-2911:27:54: INFO: @(#)ipsec-tools 0.6.5 (http://ipsec-tools.sourceforge.net)

2013-01-2911:27:54: INFO: @(#)This product linked OpenSSL 0.9.8e-fips-rhel5 01 Jul 2008(http://www.openssl.org/)

2013-01-2911:27:54: INFO: 127.0.0.1[500] used as isakmp port (fd=7)

2013-01-2911:27:54: INFO: 127.0.0.1[500] used for NAT-T

2013-01-2911:27:54: INFO: 192.168.95.230[500] used as isakmp port (fd=8)

2013-01-2911:27:54: INFO: 192.168.95.230[500] used for NAT-T

2013-01-2911:27:54: INFO: 50.0.0.1[500] used as isakmp port (fd=9)

2013-01-2911:27:54: INFO: 50.0.0.1[500] used for NAT-T

2013-01-2911:27:54: INFO: ::1[500] used as isakmp port (fd=10)

2013-01-2911:27:54: INFO: fe80::20c:29ff:fe38:49f8%eth0[500] used as isakmp port (fd=11)

2013-01-2911:27:54: INFO: fe80::20c:29ff:fe38:4902%eth1[500] used as isakmp port (fd=12)

此时两边都是就绪状态,这时可以通过从一方ping对端保护子网地址,就可以触发协商,这个类似于cisco方式。

附几个常用的命令:
setkey使用

setkey –D:查看SAD信息

setkey –DP:查看SPD信息

setkey –F:清除SAD信息

setkey –FP:清除SPD信息

racoon

     racoon –F :任务前台运行racoon –d: debug

racoon和setkey源码在ipsec-toos包中。

常遇错误
1,/etc/racoon/pskhas weak file permission

这是因为psk这个文件的安全性要求很高,不能不能被除所有者以外的人可读(这个可以从源码的注释中看到)。

2,pfkey X_SPDDUMP failed: No such file ordirectory

     没有使用setkey设置SP。

3,ping的时候无法触发racoon协调,增加默认路由。

使用racoon setkey搭建IPsec VPN环境相关推荐

  1. strongswan 搭建 IPSec 实验环境

    使用两个CentOS7虚拟机,基于strongswan搭建IPSec VPN实验环境,通过是否配置加密算法,达到产生正常和非正常ESP数据包的目的.本篇为自己填坑记录. 目录 1.准备两个CentOS ...

  2. Docker部署 IPsec VPN 服务器

    官网手册: 搭建服务端:https://git.io/vpnnotes2 配置客户端: https://git.io/vpnclients 一. 搭建 IPsec VPN 服务端 1. 创建 IPse ...

  3. 防火墙—IPSec VPN(NAT 穿透-单侧 NAT)

    两个防火墙之间通过 IKE 建立 IPSec VPN 隧道(NAT 穿透-单侧 NAT) 组网需求:            某公司总部和分部之间要通过 Internet 进行通信,为保证信息安全,计划 ...

  4. 防火墙—IPSec VPN(NAT 穿透-双侧 NAT)

    两个防火墙之间通过 IKE 建立 IPSec VPN 隧道( NAT 穿透 - 双侧 NAT ) 组网需求:某公司总部和分部之间要通过 Internet 进行通信,为保证信息安全,计划搭建 IPSec ...

  5. IPSEC实验环境搭建

    实验名称: 第三章 IP Sec VPN(一) VPN基本配置 实验要求的环境: 实验环境: 要求描述: 需要的软件工具.软件安装包:小凡 实验目的: 1)掌握IKE阶段1和阶段2的协商过程 2)在c ...

  6. IPSEC VPN详解

    目录 IPSEC的技术协议族架构 IPESC建立安全通信工作步骤: IPSEC架构 IKE 工作阶段 IPSEC的NAT问题 IPSEC的多VPN问题 DSVPN IPSEC 是一种基于网络层,应用密 ...

  7. 搭建Android开发环境

    最近由于工作中要负责开发一款Android的App,之前都是做JavaWeb的开发,Android开发虽然有所了解,但是一直没有搭建开发环境去学习,Android的更新速度比较快了,Android1. ...

  8. 实用分享|多台4G工业路由器与华为USG6300搭建IPsec

    USG6300是华为推出的针对中小企业用户推出的企业级下一代防火墙,集传统防火墙.VPN.入侵防御.防病毒.数据防泄漏.带宽管理.上网行为管理等多种安全功能于一身,为企业提供全面.简单.高效的下一代网 ...

  9. 安全防御 IPsec VPN

    目录 1.什么是数据认证,有什么用,有哪些实现的技术手段? 2.什么是身份认证,有什么用,有哪些实现的技术手段? 3.什么是VPN技术? 4.VPN技术有哪些分类? 5.IPsec技术能够提供哪些安全 ...

  10. 【安全防御】IPsec VPN

    目录 1.什么是数据认证,有什么用,有哪些实现的技术手段? 2.什么是身份认证,有什么用,有哪些实现的技术手段? 3.什么是VPN技术? 5.IPsec技术能够提供哪些安全服务? 6.IPsec的技术 ...

最新文章

  1. 【面向对象编程】(1) 类实例化的基本方法
  2. (十二)企业级java springcloud b2bc商城系统开源源码二次开发-断路器监控(Hystrix Dashboard)...
  3. 学习 慕课网 PHP工程师学习计划--我的笔记汇总
  4. TFboys:使用Tensorflow搭建深层网络分类器
  5. 内存泄漏的常见应用领域
  6. SQL实战篇:SQL行列转换及真题
  7. sar图像去噪matlab,一种基于总曲率的SAR图像变分去噪方法与流程
  8. c语言结构体 单科成绩排序,【查错】怎样让结构体其他信息(姓名)对应关键值(成绩)排序输出...
  9. onu光功率多少是正常_电信宽带升级为200M光纤,为什么网速没有提升多少?
  10. linux下简单的邮件配置
  11. c语言爱心代码(c语言画爱心的代码)
  12. 跟踪AppInfo代码
  13. 浅谈 Spring 中的设计模式
  14. 在Azure上搭建SQL云数据库
  15. 抖音作品别人保存不了,这个方法可快速将抖音短视频下载到本地
  16. 微软云Blob存储账号使用——Java使用
  17. 微信更新值得注意的几点
  18. JS 如何调用高德地图
  19. 关于nose的简单用法
  20. 2022电大国家开放大学网上形考任务-桥梁工程技术非免费(非答案)

热门文章

  1. 营业执照如何完整的转为Word格式?
  2. html5请假页面,请假模版。.html
  3. ios闹钟铃声实现代码
  4. 如何搭建IPv6测试环境
  5. 最大连续子数组和python_连续子数组的最大和(python)
  6. 统计元音(函数专题)
  7. 电子邮件客户端java实现_java电子邮件客户端软件
  8. C语言之i++,++i,i--,--i
  9. 数据库与数据库管理系统之间的关系
  10. .Microsoft Visual Studio 2010 Service Pack 1