-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWinSystemShell.py
More file actions
465 lines (385 loc) · 14.4 KB
/
WinSystemShell.py
File metadata and controls
465 lines (385 loc) · 14.4 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
###################
# A local privilege escalation utility that allows elevating from an
# administrator context to the SYSTEM account on Windows to perform
# high-privilege operations.
# Copyright (C) 2026 WinSystemShell
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
###################
'''
A local privilege escalation utility that allows elevating from an
administrator context to the SYSTEM account on Windows to perform
high-privilege operations.
'''
__version__ = "0.0.1"
__author__ = "Maurice Lambert"
__author_email__ = "mauricelambert434@gmail.com"
__maintainer__ = "Maurice Lambert"
__maintainer_email__ = "mauricelambert434@gmail.com"
__description__ = '''
A local privilege escalation utility that allows elevating from an
administrator context to the SYSTEM account on Windows to perform
high-privilege operations.
'''
__url__ = "https://github.com/mauricelambert/WinSystemShell"
# __all__ = []
__license__ = "GPL-3.0 License"
__copyright__ = '''
WinSystemShell Copyright (C) 2026 Maurice Lambert
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions.
'''
copyright = __copyright__
license = __license__
from ctypes import (
Structure, Union, WinDLL, c_void_p, c_uint32, c_ushort, c_int, c_wchar,
create_string_buffer, byref
)
from argparse import ArgumentParser, ArgumentTypeError, Namespace
from os.path import dirname, join, abspath, isfile
from string import ascii_letters, digits
from sys import executable, stdout, exit
from random import randrange, choices
from ctypes.wintypes import HANDLE
from urllib.request import urlopen
from shutil import copyfileobj
from subprocess import Popen
from typing import Tuple
from time import sleep
from re import match
PIPEDIR: str = "\\\\.\\pipe\\"
SERVER_PATH: str = abspath(join(dirname(__file__), "SystemShellServer.py"))
CMD: str = r"C:\Windows\System32\cmd.exe"
class OVERLAPPED(Structure):
"""
Represents the Windows OVERLAPPED structure for asynchronous I/O.
"""
_fields_ = [
("Internal", c_void_p),
("InternalHigh", c_void_p),
("Offset", c_uint32),
("OffsetHigh", c_uint32),
("hEvent", c_void_p),
]
class KEY_EVENT_RECORD(Structure):
"""
Represents a key event in the Windows console input.
"""
_fields_ = [
("bKeyDown", c_int),
("wRepeatCount", c_ushort),
("wVirtualKeyCode", c_ushort),
("wVirtualScanCode", c_ushort),
("uChar", c_wchar),
("dwControlKeyState", c_uint32),
]
class INPUT_RECORD(Structure):
"""
Represents a console input record.
"""
class _U(Union):
_fields_ = [("KeyEvent", KEY_EVENT_RECORD)]
_fields_ = [
("EventType", c_ushort),
("Event", _U),
]
class PipeClient:
"""
Client for communicating with a server via named pipes.
"""
GENERIC_READ = 0x80000000
GENERIC_WRITE = 0x40000000
OPEN_EXISTING = 3
FILE_FLAG_OVERLAPPED = 0x40000000
STD_OUTPUT_HANDLE = -11
STD_INPUT_HANDLE = -10
WAIT_OBJECT_0 = 0
ERROR_IO_PENDING = 997
ERROR_BROKEN_PIPE = 109
KEY_EVENT = 0x0001
def __init__(
self,
executable: str = CMD,
server_path: str = SERVER_PATH,
schtasks: str = None,
pipein: str = None,
pipeout: str = None,
):
"""
Initialize pipes and console handles, and start the server process.
"""
self.executable = executable
self.server_path = server_path
self.schtasks = schtasks
self.pipe_in = PIPEDIR + (pipein or self._gen_random_name())
self.pipe_out = PIPEDIR + (pipeout or self._gen_random_name())
self.kernel32 = WinDLL("kernel32", use_last_error=True)
self.source_cp = self.kernel32.GetACP()
self.destination_cp = self.kernel32.GetOEMCP()
self._start_server()
sleep(5)
self.h_in = self._open_pipe(self.pipe_in, self.GENERIC_WRITE)
self.h_out = self._open_pipe(self.pipe_out, self.GENERIC_READ)
self.h_console_out = self.kernel32.GetStdHandle(self.STD_OUTPUT_HANDLE)
self.h_console_in = self.kernel32.GetStdHandle(self.STD_INPUT_HANDLE)
self.ov, self.buf = self.start_pipe_read()
self.running = True
self._input_buffer = ""
@staticmethod
def _gen_random_name() -> str:
"""
Generate a random name.
"""
return "".join(choices(ascii_letters + digits, k=randrange(1, 10)))
def _start_server(self) -> None:
"""
Launch the server process with pipe arguments.
"""
server_cmd = f'"\"{executable}\" \"{self.server_path}\" \"{self.executable}\" \"{self.pipe_in}\" \"{self.pipe_out}\"'
if self.schtasks is None:
schtasks = r"C:\Windows\System32\schtasks.exe"
task_cmd = (
f'"{CMD}" /c '
+ server_cmd +
f' & \"{schtasks}\" /delete /tn RunOnceSystem /f"'
)
if len(task_cmd) > 255:
task_cmd = server_cmd
Popen(
[
schtasks,
"/create",
"/tn", "RunOnceSystem",
"/tr", task_cmd,
"/sc", "once",
"/st", "00:00",
"/ru", "SYSTEM",
"/rl", "HIGHEST",
"/f"
],
close_fds=True
).communicate()
Popen([schtasks, "/run", "/tn", "RunOnceSystem"]).communicate()
else:
schtasks = self.schtasks
task_cmd = server_cmd
Popen([schtasks, self._gen_random_name(), self._gen_random_name(), CMD, "/c " + task_cmd]).communicate()
# Popen(
# [executable,
# join(dirname(__file__), "server_test1.py"),
# # str(self.source_cp),
# self.executable,
# self.pipe_in,
# self.pipe_out],
# stdin=None, stdout=None, stderr=None, close_fds=True
# )
def _open_pipe(self, name: str, access: int) -> HANDLE:
"""
Open a named pipe for reading or writing.
"""
handle = self.kernel32.CreateFileW(
name, access, 0, None, self.OPEN_EXISTING,
self.FILE_FLAG_OVERLAPPED, None
)
if not handle:
raise OSError(self.kernel32.GetLastError())
return handle
def start_pipe_read(self) -> Tuple[OVERLAPPED, bytes]:
"""
Start an asynchronous read from the output pipe.
"""
ov = OVERLAPPED()
ov.hEvent = self.kernel32.CreateEventW(None, True, False, None)
buf = create_string_buffer(4096)
ok = self.kernel32.ReadFile(self.h_out, buf, 4096, None, byref(ov))
last_error = self.kernel32.GetLastError()
if last_error == self.ERROR_BROKEN_PIPE:
self.running = False
return ov, buf
if not ok and last_error != self.ERROR_IO_PENDING:
raise OSError(self.kernel32.GetLastError())
return ov, buf
def run(self) -> int:
"""
Run the main loop reading from the pipe and console.
"""
while self.running:
result = self.kernel32.WaitForSingleObject(self.ov.hEvent, 50)
if result == self.WAIT_OBJECT_0:
self._process_pipe_output()
self.ov, self.buf = self.start_pipe_read()
if not self._process_console_input():
break
return 0
def _process_pipe_output(self) -> None:
"""
Read data from the pipe and write it to the console.
"""
read = c_uint32()
self.kernel32.GetOverlappedResult(self.h_out, byref(self.ov), byref(read), False)
if read.value == 0:
return
needed = self.kernel32.MultiByteToWideChar(
self.destination_cp, 0, self.buf, read.value, None, 0
)
if needed:
wbuf = (c_wchar * needed)()
self.kernel32.MultiByteToWideChar(self.destination_cp, 0, self.buf,
read.value, wbuf, needed)
written = c_uint32()
self.kernel32.WriteConsoleW(self.h_console_out, wbuf, needed, byref(written), None)
# written = c_uint32()
# self.kernel32.WriteConsoleW(self.h_console_out, self.buf.value.decode("oem"), read.value, byref(written), None)
self.kernel32.ResetEvent(self.ov.hEvent)
def _process_console_input(self) -> int:
"""
Read console input and send key presses to the server.
"""
def remove_line():
stdout.write("\x08" * len(self._input_buffer) + " " * len(self._input_buffer) + "\x08" * len(self._input_buffer))
self._input_buffer = ""
stdout.flush()
events = c_uint32()
self.kernel32.GetNumberOfConsoleInputEvents(self.h_console_in, byref(events))
if not events.value:
return 4
rec = INPUT_RECORD()
read_ev = c_uint32()
self.kernel32.ReadConsoleInputW(self.h_console_in, byref(rec), 1, byref(read_ev))
if rec.EventType == self.KEY_EVENT and rec.Event.KeyEvent.bKeyDown:
ch = rec.Event.KeyEvent.uChar
if ch == '\r':
line = self._input_buffer + '\r\n'
data = line.encode("oem")
# data = line.encode("mbcs")
ov_in = OVERLAPPED()
ov_in.hEvent = self.kernel32.CreateEventW(None, True, False, None)
remove_line()
self.kernel32.WriteFile(self.h_in, data, len(data), None, byref(ov_in))
return 2
elif ch == '\x08':
if not self._input_buffer:
return 5
self._input_buffer = self._input_buffer[:-1]
stdout.write(ch + " ")
elif ch == "\x1b":
remove_line()
return 3
elif ch == "\x1A":
return 0
elif ch and ch != "\0" and ch != "\7" and ch != "\x09":
self._input_buffer += ch
stdout.write(ch)
stdout.flush()
return 1
def existing_file(path: str) -> str:
"""
Validate that the given path points to an existing file.
Args:
path: Path to the file.
Returns:
The validated file path.
Raises:
ArgumentTypeError: If the file does not exist.
"""
if not isfile(path):
raise ArgumentTypeError(f"File does not exist: {path}")
return path
def valid_windows_path(path: str) -> str:
"""
Validate that the provided string looks like a valid Windows file path.
This performs a basic validation by checking that the path starts
with a drive letter followed by a backslash (e.g., C:\\).
Args:
path: Windows file path.
Returns:
The validated path.
Raises:
ArgumentTypeError: If the path does not match the expected format.
"""
path = abspath(path)
if not match("^[a-zA-Z]:\\\\", path):
raise ArgumentTypeError(f"Invalid Windows path: {path}")
return path
def valid_pipe_name(name: str) -> str:
"""
Validate a Windows named pipe name.
Only allows alphanumeric characters, dots, underscores,
and hyphens. The full pipe path (\\\\.\\pipe\\) is not required.
Args:
name: Pipe name.
Returns:
The validated pipe name.
Raises:
ArgumentTypeError: If the pipe name contains invalid characters.
"""
if not match(r"^[a-zA-Z0-9._-]+$", name):
raise ArgumentTypeError(f"Invalid pipe name: {name}")
return name
def parse_args() -> Namespace:
r"""
Parse and validate command-line arguments.
python WinSystemShell.py --executable C:\Windows\System32\cmd.exe --schtasks "SystemRunOnce.exe" --server-path C:\temp\server.py --pipein shellpipein --pipeout shellpipeout
Returns:
Namespace containing parsed arguments:
executable: Path to an existing executable file.
schtasks: Path to SystemRunOnce.exe, if not defined the default schtasks was used.
server_path: Windows path to the server executable.
pipein: Name of the input named pipe.
pipeout: Name of the output named pipe.
"""
parser = ArgumentParser(description="Spawn an interactive shell as the Windows SYSTEM account.")
parser.add_argument(
"--executable",
default="C:\\Windows\\System32\\cmd.exe",
type=existing_file,
help="Path to the executable file (must exist)."
)
parser.add_argument(
"--schtasks",
help=(
"Path to SystemRunOnce.exe. If not exists this script download it."
" If not used this script use the lolbin schtasks."
)
)
parser.add_argument(
"--server-path",
default=SERVER_PATH,
type=valid_windows_path,
help="Valid Windows path where the server will be stored."
)
parser.add_argument(
"--pipein",
type=valid_pipe_name,
help="Name of the input named pipe."
)
parser.add_argument(
"--pipeout",
type=valid_pipe_name,
help="Name of the output named pipe."
)
return parser.parse_args()
def main() -> int:
"""
The main function to start the script from the command line.
"""
print(copyright)
arguments = parse_args()
if arguments.schtasks and not isfile(arguments.schtasks):
with open(arguments.schtasks, "wb") as file:
copyfileobj(urlopen("https://github.com/mauricelambert/WinSystemShell/releases/download/v0.0.1/SystemRunOnce.exe"), file)
PipeClient(**vars(arguments)).run()
return 0
if __name__ == "__main__":
exit(main())