-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathNtUtils.Console.pas
More file actions
607 lines (491 loc) · 16 KB
/
NtUtils.Console.pas
File metadata and controls
607 lines (491 loc) · 16 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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
unit NtUtils.Console;
{
This module includes helper functions for low-level console support.
}
interface
uses
Ntapi.WinNt, Ntapi.ConsoleApi, NtUtils, DelphiApi.Reflection, Ntapi.Versions;
const
// An access mask for annotations
PROCESS_ATTACH_CONSOLE_DIRECT = PROCESS_DUP_HANDLE or PROCESS_VM_READ or
PROCESS_QUERY_LIMITED_INFORMATION;
type
TRtlxConsoleOwnershipOperation = (coPreserve, coUse, coReset);
{ Helpers }
// Helpers for accessing PEB console handles
function RtlxConsoleConnection: IHandle;
function RtlxConsoleInput: IHandle;
function RtlxConsoleOutput: IHandle;
function RtlxConsoleError: IHandle;
// Get or open a reference handle to the console
[MinOSVersion(OsWin8)]
function RtlxGetConsoleReference(
out hxReference: IHandle
): TNtxStatus;
{ Win32 function }
// Create a new console
function AdvxAllocConsole: TNtxStatus;
// Attach to an existing console
function AdvxAttachConsole(
ProcessId: TProcessId
): TNtxStatus;
// Release the current console
function AdvxFreeConsole: TNtxStatus;
// Enumerate processes that are using the current console
function AdvxEnumerateConsoleProcesses(
out ProcessIDs: TArray<TProcessId32>
): TNtxStatus;
{ Native functions }
// Query the process ID of the associated console host
[MinOSVersion(OsWin8)]
function RtlxQueryConhostPid(
out ProcessId: TProcessId;
const hxConnection: IHandle = nil
): TNtxStatus;
// Prepare parameters for creating a console
[MinOSVersion(OsWin8)]
function RtlxPrepareConsoleLaunchData(
const ConsoleTitle: String;
InheritFromPeb: Boolean = True
): TConsoleServerMsg;
// Create a new console
[MinOSVersion(OsWin8)]
function RtlxLaunchConsoleServer(
const LaunchParameters: TConsoleServerMsg;
out hxConnect: IHandle
): TNtxStatus;
// Attach to an existing console
[MinOSVersion(OsWin8)]
function RtlxAttachConsoleById(
ProcessId: TProcessId;
out hxConnect: IHandle
): TNtxStatus;
// Attach to an existing console by duplicating its connection handle
function RtlxAttachConsoleDirect(
[Access(PROCESS_ATTACH_CONSOLE_DIRECT)] const hxProcess: IHandle;
out hxConnect: IHandle
): TNtxStatus;
// Allows the console to terminate the current process
[MinOSVersion(OsWin8)]
function RtlxSelectConsoleAsOwner(
[opt] const hxConnect: IHandle
): TNtxStatus;
// Create an console connection handle from an already launched console reference
[MinOSVersion(OsWin8)]
function RtlxDeriveConsoleConnect(
const hxReference: IHandle;
out hxConnect: IHandle
): TNtxStatus;
// Create an console reference handle from a connection
[MinOSVersion(OsWin8)]
function RtlxDeriveConsoleReference(
const hxConnect: IHandle;
out hxReference: IHandle
): TNtxStatus;
// Create an console input handle from a connection
[MinOSVersion(OsWin8)]
function RtlxDeriveConsoleInput(
const hxConnect: IHandle;
out hxInput: IHandle
): TNtxStatus;
// Create an console output handle from a connection
[MinOSVersion(OsWin8)]
function RtlxDeriveConsoleOutput(
const hxConnect: IHandle;
out hxOutput: IHandle
): TNtxStatus;
// Create I/O handles from a connection and set them as the current console
[MinOSVersion(OsWin8)]
function RtlxSelectDeriveConsole(
[opt] const hxConnect: IHandle;
Ownership: TRtlxConsoleOwnershipOperation = coUse
): TNtxStatus;
// Set console handles as the current
procedure RtlxSelectConsole(
[opt] const hxConnect: IHandle;
[opt] const hxInput: IHandle;
[opt] const hxOutput: IHandle;
[opt] const hxError: IHandle
);
implementation
uses
Ntapi.ntpebteb, Ntapi.ntstatus, Ntapi.ntrtl, Ntapi.ntwow64, Ntapi.ntpsapi,
Ntapi.ProcessThreadsApi, Ntapi.WinUser, NtUtils.Ldr, NtUtils.Files,
NtUtils.Files.Open, NtUtils.Files.Control, NtUtils.Processes.Info,
NtUtils.Processes.Create, NtUtils.Processes.Create.Native, NtUtils.Objects,
NtUtils.Memory;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
{ Helpers }
function RtlxConsoleConnection;
begin
Result := Auto.RefHandle(RtlGetCurrentPeb.ProcessParameters.ConsoleHandle);
end;
function RtlxConsoleInput;
begin
Result := Auto.RefHandle(RtlGetCurrentPeb.ProcessParameters.StandardInput);
end;
function RtlxConsoleOutput;
begin
Result := Auto.RefHandle(RtlGetCurrentPeb.ProcessParameters.StandardOutput);
end;
function RtlxConsoleError;
begin
Result := Auto.RefHandle(RtlGetCurrentPeb.ProcessParameters.StandardError);
end;
var
// Cache for manual console creation handles, see RtlxSelectConsole below
hxCurrentConnect, hxCurrentInput, hxCurrentOutput, hxCurrentError: IHandle;
function RtlxGetConsoleReference;
var
hConnection: THandle;
begin
// RS3+ exposes the handle and makes things easy. Although, only if the
// console was created by kernelbase and not our custom code.
if not Assigned(hxCurrentConnect) and
LdrxCheckDelayedImport(delayed_BaseGetConsoleReference).IsSuccess then
begin
hxReference := Auto.RefHandle(BaseGetConsoleReference);
Result := NtxSuccess;
Exit;
end;
// On earlier versions, we only have the connection handle from PEB
hConnection := RtlGetCurrentPeb.ProcessParameters.ConsoleHandle;
if (hConnection = 0) or (hConnection > MAX_HANDLE) then
begin
// No console
hxReference := Auto.RefHandle(0);
Result := NtxSuccess;
Exit;
end;
// Open a new reference from the connection
Result := NtxOpenFile(hxReference, FileParameters
.UseFileName(CD_REFERENCE_NAME)
.UseRoot(Auto.RefHandle(hConnection))
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
{ Win32 }
function AdvxAllocConsole;
begin
Result.Location := 'AllocConsole';
Result.Win32Result := AllocConsole;
end;
function AdvxAttachConsole;
begin
Result.Location := 'AttachConsole';
Result.Win32Result := AttachConsole(TProcessId32(ProcessId));
end;
function AdvxFreeConsole;
begin
Result.Location := 'FreeConsole';
Result.Win32Result := FreeConsole;
end;
function AdvxEnumerateConsoleProcesses;
var
Required: Integer;
begin
Result.Location := 'GetConsoleProcessList';
SetLength(ProcessIDs, 1);
repeat
Required := GetConsoleProcessList(@ProcessIDs[0], Length(ProcessIDs));
Result.Win32Result := Required <> 0;
if not Result.IsSuccess then
Exit;
if Required > Length(ProcessIDs) then
SetLength(ProcessIDs, Required)
else
Break;
until False;
// Trim if necessary
if Length(ProcessIDs) > Required then
SetLength(ProcessIDs, Required);
end;
{ Native }
function RtlxQueryConhostPid;
var
hConsole: THandle;
begin
if Assigned(hxConnection) then
hConsole := hxConnection.Handle
else
hConsole := RtlGetCurrentPeb.ProcessParameters.ConsoleHandle;
if hConsole = 0 then
begin
Result.Location := 'RtlxQueryConhostPid';
Result.Status := STATUS_NOT_SUPPORTED;
Exit;
end;
Result := NtxFileControl.IoControlOut(Auto.RefHandle(hConsole),
IOCTL_CONDRV_GET_SERVER_PID, ProcessId);
end;
procedure MarshalConsoleString(
[in, ReadsFrom] Source: PWideChar;
[in, NumberOfBytes] SourceSize: NativeUInt;
[out, WritesTo] Destination: Pointer;
[in, NumberOfBytes] DestinationSize: Word;
[out, NumberOfBytes] out WrittenSize: Word
);
begin
if SourceSize > DestinationSize then
WrittenSize := DestinationSize
else
WrittenSize := Word(SourceSize);
Move(Source^, Destination^, SourceSize);
end;
function RtlxPrepareConsoleLaunchData;
var
ProcessParameters: PRtlUserProcessParameters;
begin
FillChar(Result, SizeOf(Result), 0);
Result.WindowVisible := True;
MarshalConsoleString(PWideChar(ConsoleTitle), StringSizeNoZero(ConsoleTitle),
@Result.Title, SizeOf(Result.Title), Result.TitleLength);
if InheritFromPeb then
begin
ProcessParameters := RtlGetCurrentPeb.ProcessParameters;
Result.StartupFlags := ProcessParameters.WindowFlags;
if BitTest(ProcessParameters.WindowFlags and STARTF_USEFILLATTRIBUTE) then
Result.FillAttribute := Word(ProcessParameters.FillAttribute);
if BitTest(ProcessParameters.WindowFlags and STARTF_USESHOWWINDOW) then
Result.ShowWindow := TShowMode16(ProcessParameters.ShowWindowFlags);
if BitTest(ProcessParameters.WindowFlags and STARTF_USECOUNTCHARS) then
begin
Result.ScreenBufferSize.X := Int16(ProcessParameters.CountCharsX);
Result.ScreenBufferSize.Y := Int16(ProcessParameters.CountCharsY);
end;
if BitTest(ProcessParameters.WindowFlags and STARTF_USESIZE) then
begin
Result.WindowSize.X := Int16(ProcessParameters.CountX);
Result.WindowSize.Y := Int16(ProcessParameters.CountY);
end;
if BitTest(ProcessParameters.WindowFlags and STARTF_USEPOSITION) then
begin
Result.WindowOrigin.X := Int16(ProcessParameters.StartingX);
Result.WindowOrigin.Y := Int16(ProcessParameters.StartingY);
end;
if RtlOsVersionAtLeast(OsWin8) then
Result.ProcessGroupId := ProcessParameters.ProcessGroupID;
if Result.ProcessGroupId = 0 then
Result.ProcessGroupId := Cardinal(NtCurrentTeb.ClientID.UniqueProcess);
Result.WindowVisible := ProcessParameters.ConsoleHandle <> THandle(-3);
MarshalConsoleString(ProcessParameters.ImagePathName.Buffer,
ProcessParameters.ImagePathName.Length, @Result.ApplicationName,
SizeOf(Result.ApplicationName), Result.ApplicationNameLength);
MarshalConsoleString(ProcessParameters.CurrentDirectory.DosPath.Buffer,
ProcessParameters.CurrentDirectory.DosPath.Length,
@Result.CurrentDirectory, SizeOf(Result.CurrentDirectory),
Result.CurrentDirectoryLength);
end;
end;
function RtlxLaunchConsoleServer;
var
Options: TCreateProcessOptions;
ProcessParameters: IRtlUserProcessParameters;
hxServer, hxReference: IHandle;
begin
Options := Default(TCreateProcessOptions);
Options.Application := CD_SERVER_LAUNCH_APPLICATION;
Options.Parameters := CD_SERVER_LAUNCH_ARGUMENTS;
// Allocate process parameters for the conhost process
Result := RtlxCreateProcessParameters(Options, ProcessParameters);
if not Result.IsSuccess then
Exit;
// Create a ConDrv server handle
Result := NtxCreateFile(hxServer, FileParameters
.UseFileName(CD_DEVICE_PATH + CD_SERVER_NAME)
.UseSyncMode(fsAsynchronous)
.UseAccess(GENERIC_ALL)
);
if not Result.IsSuccess then
Exit;
// Start conhost
Result := NtxDeviceIoControlFile(hxServer, IOCTL_CONDRV_LAUNCH_SERVER,
ProcessParameters.Data, ProcessParameters.Size);
if not Result.IsSuccess then
Exit;
// Create a reference handle
Result := NtxCreateFile(hxReference, FileParameters
.UseFileName(CD_REFERENCE_NAME)
.UseRoot(hxServer)
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
if not Result.IsSuccess then
Exit;
// Connect to conhost and make it create the console window
Result := NtxCreateFile(hxConnect, FileParameters
.UseFileName(CD_CONNECT_NAME)
.UseRoot(hxReference)
.UseEAs(RtlxAllocateEA(CD_SERVER_EA_NAME,
Auto.RefBuffer(LaunchParameters)))
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
function RtlxAttachConsoleById;
begin
Result := NtxCreateFile(hxConnect, FileParameters
.UseFileName(CD_DEVICE_PATH + CD_CONNECT_NAME)
.UseEAs(RtlxAllocateEA(CD_ATTACH_EA_NAME, Auto.RefBuffer(ProcessId)))
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
function RtlxAttachConsoleDirect;
var
BasicInfo: TProcessBasicInformation;
Wow64Peb: PPeb32;
RemoteParameters32: Wow64Pointer<PRtlUserProcessParameters32>;
ConsoleHandle32: Wow64Handle;
RemoteParameters: PRtlUserProcessParameters;
ConsoleHandle: THandle;
hxConnectionCopy, hxReference: IHandle;
begin
// Determine target's bitness and prevent WoW64 -> Native access
Result := RtlxAssertWoW64CompatiblePeb(hxProcess, Wow64Peb);
if not Result.IsSuccess then
Exit;
if Assigned(Wow64Peb) then
begin
// Read the WoW64 process parameters base
Result := NtxMemory.Read(hxProcess, @Wow64Peb.ProcessParameters,
RemoteParameters32);
if not Result.IsSuccess then
Exit;
// Read the console handle value
Result := NtxMemory.Read(hxProcess, @RemoteParameters32.Self.ConsoleHandle,
ConsoleHandle32);
if not Result.IsSuccess then
Exit;
ConsoleHandle := ConsoleHandle32;
end
else
begin
// Locate the native PEB
Result := NtxProcess.Query(hxProcess, ProcessBasicInformation, BasicInfo);
if not Result.IsSuccess then
Exit;
// Read the process parameters base
Result := NtxMemory.Read(hxProcess,
@BasicInfo.PebBaseAddress.ProcessParameters, RemoteParameters);
if not Result.IsSuccess then
Exit;
// Read the console handle value
Result := NtxMemory.Read(hxProcess, @RemoteParameters.ConsoleHandle,
ConsoleHandle);
if not Result.IsSuccess then
Exit;
end;
if (ConsoleHandle = 0) or (ConsoleHandle > MAX_HANDLE) then
begin
Result.Location := 'RtlxAttachConsoleViaDuplication';
Result.Status := STATUS_NOT_FOUND;
Exit;
end;
// Duplicate the connection handle. Note that the handle is still tied to
// another process, so we cannot use it directly.
Result := NtxDuplicateHandleFrom(hxProcess, ConsoleHandle, hxConnectionCopy);
if not Result.IsSuccess then
Exit;
// We can, however, convert it to a reference
Result := RtlxDeriveConsoleReference(hxConnectionCopy, hxReference);
if not Result.IsSuccess then
Exit;
// And then convert back to a connection, this time, relative to our process
Result := RtlxDeriveConsoleConnect(hxReference, hxConnect);
end;
function RtlxSelectConsoleAsOwner;
var
ConhostPID: TProcessId;
begin
// Determine the connected conhost PID
if Assigned(hxConnect) then
begin
Result := RtlxQueryConhostPid(ConhostPID, hxConnect);
if not Result.IsSuccess then
Exit;
end
else
ConhostPID := 0;
// Adjust the lower bits to indicate console rather than creator ownership
ConhostPID := (ConhostPID and not 3) or 1;
// Set it the current process owner
Result := NtxProcess.Set(NtxCurrentProcess, ProcessConsoleHostProcess,
ConhostPID);
end;
function RtlxDeriveConsoleConnect;
begin
Result := NtxCreateFile(hxConnect, FileParameters
.UseFileName(CD_CONNECT_NAME)
.UseRoot(hxReference)
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
function RtlxDeriveConsoleReference;
begin
Result := NtxOpenFile(hxReference, FileParameters
.UseFileName(CD_REFERENCE_NAME)
.UseRoot(hxConnect)
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
function RtlxDeriveConsoleInput;
begin
Result := NtxCreateFile(hxInput, FileParameters
.UseFileName(CD_INPUT_NAME)
.UseRoot(hxConnect)
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
function RtlxDeriveConsoleOutput;
begin
Result := NtxCreateFile(hxOutput, FileParameters
.UseFileName(CD_OUTPUT_NAME)
.UseRoot(hxConnect)
.UseAccess(GENERIC_READ or GENERIC_WRITE)
);
end;
function RtlxSelectDeriveConsole;
var
hxInput, hxOutput: IHandle;
begin
if not Assigned(hxConnect) then
begin
RtlxSelectConsole(nil, nil, nil, nil);
// Reset process ownership, if necassary, but don't fail on error
if Ownership in [coUse, coReset] then
RtlxSelectConsoleAsOwner(nil);
Result := NtxSuccess;
Exit;
end;
// Make standard input
Result := RtlxDeriveConsoleInput(hxConnect, hxInput);
if not Result.IsSuccess then
Exit;
// Make standard output & error
Result := RtlxDeriveConsoleOutput(hxConnect, hxOutput);
if not Result.IsSuccess then
Exit;
// Adjust process ownership, if necessary
case Ownership of
coUse: Result := RtlxSelectConsoleAsOwner(hxConnect);
coReset: Result := RtlxSelectConsoleAsOwner(nil);
end;
if not Result.IsSuccess then
Exit;
RtlxSelectConsole(hxConnect, hxInput, hxOutput, hxOutput);
end;
procedure RtlxSelectConsole;
var
ProcessParameters: PRtlUserProcessParameters;
begin
ProcessParameters := RtlGetCurrentPeb.ProcessParameters;
ProcessParameters.ConsoleHandle := HandleOrDefault(hxConnect);
ProcessParameters.StandardInput := HandleOrDefault(hxInput);
ProcessParameters.StandardOutput := HandleOrDefault(hxOutput);
ProcessParameters.StandardError := HandleOrDefault(hxError);
hxCurrentConnect := hxConnect;
hxCurrentInput := hxInput;
hxCurrentOutput := hxOutput;
hxCurrentError := hxError;
end;
end.