-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathNtUtils.WinStation.pas
338 lines (271 loc) · 8.1 KB
/
NtUtils.WinStation.pas
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
unit NtUtils.WinStation;
{
The module provides access to the Window Station (aka Terminal Server) API.
}
interface
uses
Ntapi.WinNt, Ntapi.winsta, Ntapi.WinUser, NtUtils, NtUtils.Objects;
type
TSessionIdW = Ntapi.winsta.TSessionIdW;
TWinStationInformation = Ntapi.winsta.TWinStationInformation;
TWinStaHandle = Ntapi.winsta.TWinStaHandle;
IWinStaHandle = NtUtils.IHandle;
// Connect to a remote computer
function WsxOpenServer(
out hxServer: IWinStaHandle;
const Name: String
): TNtxStatus;
// Enumerate all session on the server for which we have Query access
function WsxEnumerateSessions(
out Sessions: TArray<TSessionIdW>;
[opt] hServer: TWinStaHandle = SERVER_CURRENT
): TNtxStatus;
// Find the ID of the active session
function WsxFindActiveSessionId(
out SessionId: TSessionId;
[opt] hServer: TWinStaHandle = SERVER_CURRENT
): TNtxStatus;
type
WsxWinStation = class abstract
// Query fixed-size information
class function Query<T>(
SessionId: TSessionId;
InfoClass: TWinStationInfoClass;
out Buffer: T;
[opt] hServer: TWinStaHandle = SERVER_CURRENT
): TNtxStatus; static;
end;
// Query variable-size information
function WsxQuery(
SessionId: TSessionId;
InfoClass: TWinStationInfoClass;
out xMemory: IMemory;
hServer: TWinStaHandle = SERVER_CURRENT;
InitialBuffer: Cardinal = 0;
[opt] GrowthMethod: TBufferGrowthMethod = nil
): TNtxStatus;
// Open session token
function WsxQueryToken(
out hxToken: IHandle;
SessionId: TSessionId;
[opt] hServer: TWinStaHandle = SERVER_CURRENT
): TNtxStatus;
// Send a message to a session
function WsxSendMessage(
SessionId: TSessionId;
const Title: String;
const MessageStr: String;
Style: TMessageStyle;
TimeoutSeconds: Cardinal = 0;
WaitForResponse: Boolean = False;
[out, opt] pResponse: PMessageResponse = nil;
[opt] ServerHandle: TWinStaHandle = SERVER_CURRENT
): TNtxStatus;
// Connect one session to another
function WsxConnect(
SessionId: TSessionId;
TargetSessionId: TSessionId = LOGONID_CURRENT;
[in, opt] Password: PWideChar = nil;
Wait: Boolean = True;
hServer: TWinStaHandle = SERVER_CURRENT
): TNtxStatus;
// Disconnect a session
function WsxDisconnect(
SessionId: TSessionId;
Wait: Boolean;
hServer: TWinStaHandle = SERVER_CURRENT
): TNtxStatus;
// Remote control (shadow) an active remote session
function WsxRemoteControl(
TargetSessionId: TSessionId;
HotKeyVk: Byte;
HotkeyModifiers: Word;
hServer: TWinStaHandle = SERVER_CURRENT;
[opt] const TargetServer: String = ''
): TNtxStatus;
// Stop controlling (shadowing) a session
function WsxRemoteControlStop(
hServer: TWinStaHandle;
SessionId: TSessionId;
Wait: Boolean
): TNtxStatus;
implementation
uses
Ntapi.ntstatus, NtUtils.SysUtils, DelphiUtils.AutoObjects, NtUtils.Ldr;
{$BOOLEVAL OFF}
{$IFOPT R+}{$DEFINE R+}{$ENDIF}
{$IFOPT Q+}{$DEFINE Q+}{$ENDIF}
type
TWinStaAutoHandle = class(TCustomAutoHandle, IWinStaHandle, IAutoReleasable)
procedure Release; override;
end;
procedure TWinStaAutoHandle.Release;
begin
if (FHandle <> 0) and LdrxCheckDelayedImport(
delayed_WinStationCloseServer).IsSuccess then
WinStationCloseServer(FHandle);
FHandle := 0;
inherited;
end;
function WsxDelayFreeMemory(
[in] Buffer: Pointer
): IAutoReleasable;
begin
Result := Auto.Delay(
procedure
begin
if LdrxCheckDelayedImport(delayed_WinStationFreeMemory).IsSuccess then
WinStationFreeMemory(Buffer);
end
);
end;
function WsxOpenServer;
var
hServer: TWinStaHandle;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationOpenServerW);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationOpenServerW';
hServer := WinStationOpenServerW(PWideChar(Name));
Result.Win32Result := hServer <> 0;
if Result.IsSuccess then
hxServer := Auto.CaptureHandle(hServer);
end;
function WsxEnumerateSessions;
var
Buffer: PSessionIdArrayW;
BufferDeallocator: IAutoReleasable;
Count, i: Integer;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationEnumerateW);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationEnumerateW';
Result.Win32Result := WinStationEnumerateW(hServer, Buffer, Count);
if not Result.IsSuccess then
Exit;
BufferDeallocator := WsxDelayFreeMemory(Buffer);
SetLength(Sessions, Count);
for i := 0 to High(Sessions) do
Sessions[i] := Buffer{$R-}[i]{$IFDEF R+}{$IFDEF R+}{$R+}{$ENDIF}{$ENDIF};
end;
function WsxFindActiveSessionId;
var
Sessions: TArray<TSessionIdW>;
i: Integer;
begin
Result := WsxEnumerateSessions(Sessions);
if not Result.IsSuccess then
Exit;
for i := 0 to High(Sessions) do
if Sessions[i].State = State_Active then
begin
SessionId := Sessions[i].SessionID;
Exit;
end;
Result.Location := 'WsxFindActiveSessionId';
Result.Status := STATUS_NOT_FOUND;
end;
class function WsxWinStation.Query<T>;
var
Returned: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationQueryInformationW);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationQueryInformationW';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
Result.Win32Result := WinStationQueryInformationW(hServer, SessionId,
InfoClass, @Buffer, SizeOf(Buffer), Returned);
end;
function GrowWxsDefault(
const Memory: IMemory;
Required: NativeUInt
): NativeUInt;
begin
Result := Memory.Size + (Memory.Size shr 2) + 64; // + 25% + 64 B
end;
function WsxQuery;
var
Required: Cardinal;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationQueryInformationW);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationQueryInformationW';
Result.LastCall.UsesInfoClass(InfoClass, icQuery);
// WinStationQueryInformationW might not return the required buffer size,
// we need to guess it
if not Assigned(GrowthMethod) then
GrowthMethod := GrowWxsDefault;
xMemory := Auto.AllocateDynamic(InitialBuffer);
repeat
Required := 0;
Result.Win32Result := WinStationQueryInformationW(hServer, SessionId,
InfoClass, xMemory.Data, xMemory.Size, Required);
until not NtxExpandBufferEx(Result, xMemory, Required, GrowthMethod);
end;
function WsxQueryToken;
var
UserToken: TWinStationUserToken;
begin
UserToken := Default(TWinStationUserToken);
Result := WsxWinStation.Query(SessionId, WinStationUserToken, UserToken,
hServer);
if Result.IsSuccess then
hxToken := Auto.CaptureHandle(UserToken.UserToken);
end;
function WsxSendMessage;
var
Response: TMessageResponse;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationSendMessageW);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationSendMessageW';
Result.Win32Result := WinStationSendMessageW(ServerHandle, SessionId,
PWideChar(Title), StringSizeNoZero(Title), PWideChar(MessageStr),
StringSizeNoZero(MessageStr), Style, TimeoutSeconds, Response,
not WaitForResponse);
if Result.IsSuccess and Assigned(pResponse) then
pResponse^ := Response;
end;
function WsxConnect;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationConnectW);
if not Result.IsSuccess then
Exit;
// It fails with null pointer
if not Assigned(Password) then
Password := '';
Result.Location := 'WinStationConnectW';
Result.Win32Result := WinStationConnectW(hServer, SessionId, TargetSessionId,
Password, Wait);
end;
function WsxDisconnect;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationDisconnect);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationDisconnect';
Result.Win32Result := WinStationDisconnect(hServer, SessionId, Wait);
end;
function WsxRemoteControl;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationShadow);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationShadow';
Result.Win32Result := WinStationShadow(hServer, RefStrOrNil(TargetServer),
TargetSessionId, HotKeyVk, HotkeyModifiers);
end;
function WsxRemoteControlStop;
begin
Result := LdrxCheckDelayedImport(delayed_WinStationShadowStop);
if not Result.IsSuccess then
Exit;
Result.Location := 'WinStationShadowStop';
Result.Win32Result := WinStationShadowStop(hServer, SessionId, Wait);
end;
end.