-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathUser.cs
More file actions
254 lines (235 loc) · 9.29 KB
/
User.cs
File metadata and controls
254 lines (235 loc) · 9.29 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
using System;
using System.Collections.Generic;
using System.Data;
using System.Runtime.InteropServices;
using System.Text;
namespace WinDump
{
class User
{
internal static DataTable GetUser()
{
return Utils.Query("SELECT Name,SID,Status,Domain,LocalAccount FROM Win32_Account Where SIDType=1");
}
}
class QUser
{
[DllImport("wtsapi32.dll")]
static extern IntPtr WTSOpenServer([MarshalAs(UnmanagedType.LPStr)] string pServerName);
[DllImport("wtsapi32.dll")]
static extern void WTSCloseServer(IntPtr hServer);
[DllImport("wtsapi32.dll")]
static extern Int32 WTSEnumerateSessions(IntPtr hServer,
[MarshalAs(UnmanagedType.U4)] Int32 Reserved,
[MarshalAs(UnmanagedType.U4)] Int32 Version,
ref IntPtr ppSessionInfo,
[MarshalAs(UnmanagedType.U4)] ref Int32 pCount);
[DllImport("wtsapi32.dll")]
static extern void WTSFreeMemory(IntPtr pMemory);
[DllImport("wtsapi32.dll")]
static extern bool WTSQuerySessionInformation(IntPtr hServer,
int sessionId,
WTS_INFO_CLASS wtsInfoClass,
out IntPtr ppBuffer,
out uint pBytesReturned);
[StructLayout(LayoutKind.Sequential)]
private struct WTS_SESSION_INFO
{
public Int32 SessionID;
[MarshalAs(UnmanagedType.LPStr)]
public string pWinStationName;
public WTS_CONNECTSTATE_CLASS State;
}
//https://social.technet.microsoft.com/Forums/windowsserver/en-US/cbfd802c-5add-49f3-b020-c901f1a8d3f4/retrieve-user-logontime-on-terminal-service-with-remote-desktop-services-api
//https://docs.microsoft.com/en-us/windows/win32/api/wtsapi32/ns-wtsapi32-wtsinfoa
[StructLayout(LayoutKind.Sequential)]
public struct WTSINFOA
{
public const int WINSTATIONNAME_LENGTH = 32;
public const int DOMAIN_LENGTH = 17;
public const int USERNAME_LENGTH = 20;
WTS_CONNECTSTATE_CLASS State;
public int SessionId;
public int IncomingBytes;
public int OutgoingBytes;
public int IncomingFrames;
public int OutgoingFrames;
public int IncomingCompressedBytes;
public int OutgoingCompressedBytes;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = WINSTATIONNAME_LENGTH)]
byte[] WinStationNameRaw;
public string WinStationName
{
get
{
return Encoding.ASCII.GetString(WinStationNameRaw);
}
}
[MarshalAs(UnmanagedType.ByValArray, SizeConst = DOMAIN_LENGTH)]
public byte[] DomainRaw;
public string Domain
{
get
{
return Encoding.ASCII.GetString(DomainRaw);
}
}
[MarshalAs(UnmanagedType.ByValArray, SizeConst = USERNAME_LENGTH + 1)]
public byte[] UserNameRaw;
public string UserName
{
get
{
return Encoding.ASCII.GetString(UserNameRaw);
}
}
public long ConnectTimeUTC;
public DateTime ConnectTime
{
get
{
return DateTime.FromFileTimeUtc(ConnectTimeUTC);
}
}
public long DisconnectTimeUTC;
public DateTime DisconnectTime
{
get
{
return DateTime.FromFileTimeUtc(DisconnectTimeUTC);
}
}
public long LastInputTimeUTC;
public DateTime LastInputTime
{
get
{
return DateTime.FromFileTimeUtc(LastInputTimeUTC);
}
}
public long LogonTimeUTC;
public DateTime LogonTime
{
get
{
return DateTime.FromFileTimeUtc(LogonTimeUTC);
}
}
public long CurrentTimeUTC;
public DateTime CurrentTime
{
get
{
return DateTime.FromFileTimeUtc(CurrentTimeUTC);
}
}
}
public enum WTS_INFO_CLASS
{
WTSInitialProgram,
WTSApplicationName,
WTSWorkingDirectory,
WTSOEMId,
WTSSessionId,
WTSUserName,
WTSWinStationName,
WTSDomainName,
WTSConnectState,
WTSClientBuildNumber,
WTSClientName,
WTSClientDirectory,
WTSClientProductId,
WTSClientHardwareId,
WTSClientAddress,
WTSClientDisplay,
WTSClientProtocolType,
WTSIdleTime,
WTSLogonTime,
WTSIncomingBytes,
WTSOutgoingBytes,
WTSIncomingFrames,
WTSOutgoingFrames,
WTSClientInfo,
WTSSessionInfo
}
public enum WTS_CONNECTSTATE_CLASS
{
Active,
Connected,
ConnectQuery,
Shadow,
Disconnected,
Idle,
Listen,
Reset,
Down,
Init
}
//https://stackoverflow.com/questions/32522545/retrieve-user-logontime-on-terminal-service-with-remote-desktop-services-api
//https://social.technet.microsoft.com/Forums/windowsserver/en-US/cbfd802c-5add-49f3-b020-c901f1a8d3f4/retrieve-user-logontime-on-terminal-service-with-remote-desktop-services-api
internal static DataTable GetQUser()
{
IntPtr serverHandle = WTSOpenServer("");
var dt = new DataTable();
dt.Columns.Add("UserName");
dt.Columns.Add("Domain");
dt.Columns.Add("WinStationName");
dt.Columns.Add("SessionID");
dt.Columns.Add("State");
dt.Columns.Add("Idle");
dt.Columns.Add("LoginTime");
try
{
IntPtr sessionInfoPtr = IntPtr.Zero;
IntPtr userPtr = IntPtr.Zero;
IntPtr domainPtr = IntPtr.Zero;
IntPtr wtsinfoPtr = IntPtr.Zero;
Int32 sessionCount = 0;
//https://docs.microsoft.com/en-us/windows/win32/api/wtsapi32/nf-wtsapi32-wtsenumeratesessionsa
//Retrieves a list of sessions on a Remote Desktop Session Host (RD Session Host) server.
Int32 retVal = WTSEnumerateSessions(serverHandle, 0, 1, ref sessionInfoPtr, ref sessionCount);
Int32 dataSize = Marshal.SizeOf(typeof(WTS_SESSION_INFO));
IntPtr currentSession = sessionInfoPtr;
uint bytes = 0;
if (retVal != 0)
{
//collect sessions - may contain duplicates
for (int i = 0; i < sessionCount; i++)
{
WTS_SESSION_INFO si = (WTS_SESSION_INFO)Marshal.PtrToStructure(currentSession, typeof(WTS_SESSION_INFO));
currentSession = new IntPtr(currentSession.ToInt64() + dataSize);
WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSUserName, out userPtr, out bytes);
WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSDomainName, out domainPtr, out bytes);
WTSQuerySessionInformation(serverHandle, si.SessionID, WTS_INFO_CLASS.WTSSessionInfo, out wtsinfoPtr, out bytes);
string domain = Marshal.PtrToStringAnsi(domainPtr);
string username = Marshal.PtrToStringAnsi(userPtr);
var wtsinfo = (WTSINFOA)Marshal.PtrToStructure(wtsinfoPtr, typeof(WTSINFOA));
//if username is not null
if (!String.IsNullOrEmpty(Marshal.PtrToStringAnsi(userPtr)))
{
var idle = wtsinfo.LastInputTimeUTC == 0 ? "none" : wtsinfo.LastInputTime.ToString();
dt.Rows.Add(
username,
domain,
si.pWinStationName,
si.SessionID,
si.State.ToString(),
idle,
wtsinfo.LogonTime
);
}
WTSFreeMemory(userPtr);
WTSFreeMemory(domainPtr);
WTSFreeMemory(wtsinfoPtr);
}
WTSFreeMemory(sessionInfoPtr);
}
}
finally
{
WTSCloseServer(serverHandle);
}
return dt;
}
}
}