滲透技巧——Windows Token九種權限的利用
0x00 前言
普通用戶(或者LocalService用戶)的特殊Token有哪些可利用方法呢?能否提權?如何判斷?
本文將要結合自己的經驗,參考多個開源工具和資料,嘗試對這個技巧做總結,分享學習心得。
參考的開源工具和資料:
- Hot Potato: https://github.com/foxglovesec/Potato
- powershell版本Hot Potato: https://github.com/Kevin-Robertson/Tater
- Rotten Potato: https://github.com/breenmachine/RottenPotatoNG
- lonelypotato: https://github.com/decoder-it/lonelypotato
- Juicy Potato: https://github.com/ohpe/juicy-potato
- https://github.com/hatRiot/token-priv
- https://foxglovesecurity.com/2017/08/25/abusing-token-privileges-for-windows-local-privilege-escalation/
- https://foxglovesecurity.com/2016/01/16/hot-potato/
- https://foxglovesecurity.com/2016/09/26/rotten-potato-privilege-escalation-from-service-accounts-to-system/
- https://foxglovesecurity.com/2017/08/25/abusing-token-privileges-for-windows-local-privilege-escalation/
0x01 簡介
本文將要介紹以下內容:
- 簡要利用思路
- SeImpersonatePrivilege權限對應的利用思路和開源代碼
- SeAssignPrimaryPrivilege權限對應的利用思路和開源代碼
- SeTcbPrivilege權限對應的利用思路和開源代碼
- SeBackupPrivilege權限對應的利用思路和開源代碼
- SeRestorePrivilege權限對應的利用思路和開源代碼
- SeCreateTokenPrivilege權限對應的利用思路和開源代碼
- SeLoadDriverPrivilege權限對應的利用思路和開源代碼
- SeTakeOwnershipPrivilege權限對應的利用思路和開源代碼
- SeDebugPrivilege權限對應的利用思路和開源代碼
0x02 簡要利用思路
1、取得了目標的訪問權限后,查看可用權限
whoami /priv
例如,普通用戶具有的權限如下圖。

管理員用戶具有的權限如下圖。

iis用戶具有的權限如下圖。

Privilege Name項表示具有的權限,State表示權限的狀態,我們可以通過WinAPI AdjustTokenPrivileges將權限設置為Disabled或者Enabled
可供參考的實現代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnablePrivilegeandGetTokenInformation.cpp
代碼實現了開啟指定權限(SeDebugPrivilege),并且查看當前用戶名稱和具有的權限
2、如果包含以下九個權限,我們就可以對其進一步利用
- SeImpersonatePrivilege
- SeAssignPrimaryPrivilege
- SeTcbPrivilege
- SeBackupPrivilege
- SeRestorePrivilege
- SeCreateTokenPrivilege
- SeLoadDriverPrivilege
- SeTakeOwnershipPrivilege
- SeDebugPrivilege
注:
- iis或者sqlserver的用戶通常具有SeImpersonatePrivilege和SeAssignPrimaryPrivilege權限
- Backup service用戶通常具有SeBackupPrivilege和SeRestorePrivilege權限
0x03 SeImpersonatePrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L327
SeImpersonatePrivilege
身份驗證后模擬客戶端(Impersonatea client after authentication)
擁有該權限的進程能夠模擬已有的token,但不能創建新的token
以下用戶具有該權限:
- 本地管理員組成員和本地服務帳戶
- 由服務控制管理器啟動的服務
- 由組件對象模型 (COM) 基礎結構啟動的并配置為在特定帳戶下運行的COM服務器
通常,iis或者sqlserver用戶具有該權限
利用思路
- 利用NTLM Relay to Local Negotiation獲得System用戶的Token 可使用開源工具Rotten Potato、lonelypotato或者Juicy Potato
- 通過WinAPI CreateProcessWithToken創建新進程,傳入System用戶的Token 具有SeImpersonatePrivilege權限才能創建成功
- 該Token具有System權限
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeImpersonatePrivilege.cpp
代碼實現了開啟當前進程的SeImpersonatePrivilege權限,調用CreateProcessWithToken,傳入當前進程的Token,創建一個進程,配合RottenPotato,可用來從LocalService提權至System權限。
0x04 SeAssignPrimaryPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L359
SeAssignPrimaryPrivilege
向進程(新創建或者掛起的進程)分配token
通常,iis或者sqlserver用戶具有該權限
利用思路1
· 利用NTLM Relay to Local Negotiation獲得System用戶的Token
· 通過WinAPI CreateProcessAsUser創建新進程,傳入System用戶的Token
· 該Token具有System權限
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeAssignPrimaryTokenPrivilege.cpp
代碼實現了開啟當前進程的SeAssignPrimaryTokenPrivilege權限,調用CreateProcessAsUser,傳入當前進程的Token,創建一個進程,配合RottenPotato,可用來從LocalService提權至System權限。
利用思路2
- 利用NTLM Relay to Local Negotiation獲得System用戶的Token
- 通過WinAPI CreateProcess創建一個掛起的新進程,參數設置為CREATE_SUSPENDED
- 通過WinAPI NtSetInformationProcess將新進程的Token替換為System用戶的Token
- 該Token具有System權限
0x05 SeTcbPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L418
SeTcbPrivilege
等同于獲得了系統的***權限
利用思路
· 調用LsaLogonUser獲得Token
- 將該Token添加至Local System account組
- 該Token具有System權限
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeTcbPrivilege.cpp
代碼實現了開啟當前進程的SeTcbPrivilege權限,登錄用戶test1,將其添加至Local System account組,獲得System權限,創建注冊表項HKEY_LOCAL_MACHINE\SOFTWARE\testtcb
0x06 SeBackupPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L495
SeBackupPrivilege
用來實現備份操作,對當前系統任意文件具有讀權限
利用思路
· 讀取注冊表HKEY_LOCAL_MACHINE\SAM、HKEY_LOCAL_MACHINE\SECURITY和HKEY_LOCAL_MACHINE\SYSTEM
· 導出當前系統的所有用戶hash mimikatz的命令如下:
- lsadump::sam /sam:SamBkup.hiv /system:SystemBkup.hiv
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeBackupPrivilege.cpp
代碼實現了開啟當前進程的SeBackupPrivilege權限,讀取注冊表,將其保存成文件C:\\test\\SAM、C:\\test\\SECURITY和C:\\test\\SYSTEM
0x07 SeRestorePrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L528
SeRestorePrivilege
用來實現恢復操作,對當前系統任意文件具有寫權限
利用思路1
- 獲得SeRestorePrivilege權限,修改注冊表HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
- 劫持exe文件的啟動
- 實現提權或是作為后門
利用思路2
- 獲得SeRestorePrivilege權限,向任意路徑寫入dll文件
- 實現dll劫持
- 實現提權或是作為后門
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeRestorePrivilege.cpp
代碼實現了開啟當前進程的SeRestorePrivilege權限,創建注冊表項HKEY_LOCAL_MACHINE\SOFTWARE\testrestore
0x08 SeCreateTokenPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L577
SeCreateTokenPrivilege
用來創建Primary Token
利用思路
· 通過WinAPI ZwCreateToken創建Primary Token
- 將Token添加至local administrator組
- 該Token具有System權限
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeCreateTokenPrivilege.cpp
代碼實現了開啟當前進程的SeCreateTokenPrivilege權限,創建Primary Token,將其添加至local administrator組,開啟SeDebugPrivilege和SeTcbPrivilege權限
0x09 SeLoadDriverPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L626
SeLoadDriverPrivilege
用來加載驅動文件
利用思路
- 創建驅動文件的注冊表
- reg add hkcu\System\CurrentControlSet\CAPCOM /v ImagePath /t REG_SZ /d "\??\C:\test\Capcom.sys"
- reg add hkcu\System\CurrentControlSet\CAPCOM /v Type /t REG_DWORD /d 1
- 加載驅動文件Capcom.sys
- Capcom.sys存在漏洞,系統加載后,可從普通用戶權限提升至System權限,利用代碼可參考:https://github.com/tandasat/ExploitCapcom
- 獲得System權限
可供參考的測試代碼: https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeLoadDriverPrivilege.cpp
代碼實現了開啟當前進程的SeLoadDriverPrivilege權限,讀取注冊表項hkcu\System\CurrentControlSet\CAPCOM,加載驅動文件Capcom.sys
0x0A SeTakeOwnershipPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L688
SeTakeOwnershipPrivilege
同SeRestorePrivilege類似,對當前系統任意文件具有寫權限
利用思路1
- 獲得SeTakeOwnershipPrivilege權限,修改注冊表HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options
- 劫持exe文件的啟動
- 實現提權或是作為后門
利用思路2
- 獲得SeTakeOwnershipPrivilege權限,向任意路徑寫入dll文件
- 實現dll劫持
- 實現提權或是作為后門
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeTakeOwnershipPrivilege.cpp
代碼實現了開啟當前進程的SeTakeOwnershipPrivilege權限,修改注冊表項hklm\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options的權限,普通用戶權限對其具有完整操作權限
后續的寫操作:
reg add "hklm\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options" /v takeownership /t REG_SZ /d "C:\\Windows\\System32\\calc.exe"
0x0B SeDebugPrivilege權限的利用思路
參考資料:
https://github.com/hatRiot/token-priv/blob/master/abusing_token_eop_1.0.txt#L736
SeDebugPrivilege
用來調試指定進程,包括讀寫內存,常用作實現dll注入
利用思路
- 找到System權限的進程
- dll注入
- 獲得System權限
可供參考的測試代碼:
https://github.com/3gstudent/Homework-of-C-Language/blob/master/EnableSeDebugPrivilege.cpp
代碼實現了開啟當前進程的SeDebugPrivilege權限,向指定進程注入dll
0x0C 小結
本文總結了普通用戶(或者LocalService用戶)Token中九種權限的利用方法,分析利用思路,完善實現代碼。