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
|
'use strict';
// @ts-check
// ==================================================================================
// users.js
// ----------------------------------------------------------------------------------
// Description: System Information - library
// for Node.js
// Copyright: (c) 2014 - 2022
// Author: Sebastian Hildebrandt
// ----------------------------------------------------------------------------------
// License: MIT
// ==================================================================================
// 11. Users/Sessions
// ----------------------------------------------------------------------------------
const exec = require('child_process').exec;
const util = require('./util');
let _platform = process.platform;
const _linux = (_platform === 'linux' || _platform === 'android');
const _darwin = (_platform === 'darwin');
const _windows = (_platform === 'win32');
const _freebsd = (_platform === 'freebsd');
const _openbsd = (_platform === 'openbsd');
const _netbsd = (_platform === 'netbsd');
const _sunos = (_platform === 'sunos');
// let _winDateFormat = {
// dateFormat: '',
// dateSeperator: '',
// timeFormat: '',
// timeSeperator: '',
// amDesignator: '',
// pmDesignator: ''
// };
// --------------------------
// array of users online = sessions
// function getWinCulture() {
// return new Promise((resolve) => {
// process.nextTick(() => {
// if (!_winDateFormat.dateFormat) {
// util.powerShell('(get-culture).DateTimeFormat')
// .then(data => {
// let lines = data.toString().split('\r\n');
// _winDateFormat.dateFormat = util.getValue(lines, 'ShortDatePattern', ':');
// _winDateFormat.dateSeperator = util.getValue(lines, 'DateSeparator', ':');
// _winDateFormat.timeFormat = util.getValue(lines, 'ShortTimePattern', ':');
// _winDateFormat.timeSeperator = util.getValue(lines, 'TimeSeparator', ':');
// _winDateFormat.amDesignator = util.getValue(lines, 'AMDesignator', ':');
// _winDateFormat.pmDesignator = util.getValue(lines, 'PMDesignator', ':');
// resolve(_winDateFormat);
// })
// .catch(() => {
// resolve(_winDateFormat);
// });
// } else {
// resolve(_winDateFormat);
// }
// });
// });
// }
function parseUsersLinux(lines, phase) {
let result = [];
let result_who = [];
let result_w = {};
let w_first = true;
let w_header = [];
let w_pos = [];
let who_line = {};
let is_whopart = true;
lines.forEach(function (line) {
if (line === '---') {
is_whopart = false;
} else {
let l = line.replace(/ +/g, ' ').split(' ');
// who part
if (is_whopart) {
result_who.push({
user: l[0],
tty: l[1],
date: l[2],
time: l[3],
ip: (l && l.length > 4) ? l[4].replace(/\(/g, '').replace(/\)/g, '') : ''
});
} else {
// w part
if (w_first) { // header
w_header = l;
w_header.forEach(function (item) {
w_pos.push(line.indexOf(item));
});
w_first = false;
} else {
// split by w_pos
result_w.user = line.substring(w_pos[0], w_pos[1] - 1).trim();
result_w.tty = line.substring(w_pos[1], w_pos[2] - 1).trim();
result_w.ip = line.substring(w_pos[2], w_pos[3] - 1).replace(/\(/g, '').replace(/\)/g, '').trim();
result_w.command = line.substring(w_pos[7], 1000).trim();
// find corresponding 'who' line
who_line = result_who.filter(function (obj) {
return (obj.user.substring(0, 8).trim() === result_w.user && obj.tty === result_w.tty);
});
if (who_line.length === 1) {
result.push({
user: who_line[0].user,
tty: who_line[0].tty,
date: who_line[0].date,
time: who_line[0].time,
ip: who_line[0].ip,
command: result_w.command
});
}
}
}
}
});
if (result.length === 0 && phase === 2) {
return result_who;
} else {
return result;
}
}
function parseUsersDarwin(lines) {
let result = [];
let result_who = [];
let result_w = {};
let who_line = {};
let is_whopart = true;
lines.forEach(function (line) {
if (line === '---') {
is_whopart = false;
} else {
let l = line.replace(/ +/g, ' ').split(' ');
// who part
if (is_whopart) {
result_who.push({
user: l[0],
tty: l[1],
date: ('' + new Date().getFullYear()) + '-' + ('0' + ('JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.indexOf(l[2].toUpperCase()) / 3 + 1)).slice(-2) + '-' + ('0' + l[3]).slice(-2),
time: l[4],
});
} else {
// w part
// split by w_pos
result_w.user = l[0];
result_w.tty = l[1];
result_w.ip = (l[2] !== '-') ? l[2] : '';
result_w.command = l.slice(5, 1000).join(' ');
// find corresponding 'who' line
who_line = result_who.filter(function (obj) {
return (obj.user === result_w.user && (obj.tty.substring(3, 1000) === result_w.tty || obj.tty === result_w.tty));
});
if (who_line.length === 1) {
result.push({
user: who_line[0].user,
tty: who_line[0].tty,
date: who_line[0].date,
time: who_line[0].time,
ip: result_w.ip,
command: result_w.command
});
}
}
}
});
return result;
}
function users(callback) {
return new Promise((resolve) => {
process.nextTick(() => {
let result = [];
// linux
if (_linux) {
exec('who --ips; echo "---"; w | tail -n +2', function (error, stdout) {
if (!error) {
// lines / split
let lines = stdout.toString().split('\n');
result = parseUsersLinux(lines, 1);
if (result.length === 0) {
exec('who; echo "---"; w | tail -n +2', function (error, stdout) {
if (!error) {
// lines / split
lines = stdout.toString().split('\n');
result = parseUsersLinux(lines, 2);
}
if (callback) { callback(result); }
resolve(result);
});
} else {
if (callback) { callback(result); }
resolve(result);
}
} else {
if (callback) { callback(result); }
resolve(result);
}
});
}
if (_freebsd || _openbsd || _netbsd) {
exec('who; echo "---"; w -ih', function (error, stdout) {
if (!error) {
// lines / split
let lines = stdout.toString().split('\n');
result = parseUsersDarwin(lines);
}
if (callback) { callback(result); }
resolve(result);
});
}
if (_sunos) {
exec('who; echo "---"; w -h', function (error, stdout) {
if (!error) {
// lines / split
let lines = stdout.toString().split('\n');
result = parseUsersDarwin(lines);
}
if (callback) { callback(result); }
resolve(result);
});
}
if (_darwin) {
exec('who; echo "---"; w -ih', function (error, stdout) {
if (!error) {
// lines / split
let lines = stdout.toString().split('\n');
result = parseUsersDarwin(lines);
}
if (callback) { callback(result); }
resolve(result);
});
}
if (_windows) {
try {
// const workload = [];
// // workload.push(util.powerShell('Get-CimInstance -ClassName Win32_Account | fl *'));
// workload.push(util.powerShell('Get-WmiObject Win32_LogonSession | fl *'));
// workload.push(util.powerShell('Get-WmiObject Win32_LoggedOnUser | fl *'));
// workload.push(util.powerShell('Get-WmiObject Win32_Process -Filter "name=\'explorer.exe\'" | Select @{Name="domain";Expression={$_.GetOwner().Domain}}, @{Name="username";Expression={$_.GetOwner().User}} | fl'));
// Promise.all(
// workload
// ).then(data => {
let cmd = 'Get-WmiObject Win32_LogonSession | select LogonId,StartTime | fl' + '; echo \'#-#-#-#\';';
cmd += 'Get-WmiObject Win32_LoggedOnUser | select antecedent,dependent | fl ' + '; echo \'#-#-#-#\';';
cmd += 'Get-WmiObject Win32_Process -Filter "name=\'explorer.exe\'" | Select @{Name="sessionid";Expression={$_.SessionId}}, @{Name="domain";Expression={$_.GetOwner().Domain}}, @{Name="username";Expression={$_.GetOwner().User}} | fl' + '; echo \'#-#-#-#\';';
cmd += 'query user';
util.powerShell(cmd).then(data => {
// controller + vram
// let accounts = parseWinAccounts(data[0].split(/\n\s*\n/));
if (data) {
data = data.split('#-#-#-#');
let sessions = parseWinSessions((data[0] || '').split(/\n\s*\n/));
let loggedons = parseWinLoggedOn((data[1] || '').split(/\n\s*\n/));
let queryUser = parseWinUsersQuery((data[3] || '').split('\r\n'));
let users = parseWinUsers((data[2] || '').split(/\n\s*\n/), queryUser);
for (let id in loggedons) {
if ({}.hasOwnProperty.call(loggedons, id)) {
loggedons[id].dateTime = {}.hasOwnProperty.call(sessions, id) ? sessions[id] : '';
}
}
users.forEach(user => {
let dateTime = '';
for (let id in loggedons) {
if ({}.hasOwnProperty.call(loggedons, id)) {
if (loggedons[id].user === user.user && (!dateTime || dateTime < loggedons[id].dateTime)) {
dateTime = loggedons[id].dateTime;
}
}
}
result.push({
user: user.user,
tty: user.tty,
date: `${dateTime.substr(0, 4)}-${dateTime.substr(4, 2)}-${dateTime.substr(6, 2)}`,
time: `${dateTime.substr(8, 2)}:${dateTime.substr(10, 2)}`,
ip: '',
command: ''
});
});
}
if (callback) { callback(result); }
resolve(result);
});
// util.powerShell('query user').then(stdout => {
// if (stdout) {
// // lines / split
// let lines = stdout.toString().split('\r\n');
// getWinCulture()
// .then(culture => {
// result = parseUsersWin(lines, culture);
// if (callback) { callback(result); }
// resolve(result);
// });
// } else {
// if (callback) { callback(result); }
// resolve(result);
// }
// });
} catch (e) {
if (callback) { callback(result); }
resolve(result);
}
}
});
});
}
// function parseWinAccounts(accountParts) {
// const accounts = [];
// accountParts.forEach(account => {
// const lines = account.split('\r\n');
// const name = util.getValue(lines, 'name', ':', true);
// const domain = util.getValue(lines, 'domain', ':', true);
// accounts.push(`${domain}\${name}`);
// });
// return accounts;
// }
function parseWinSessions(sessionParts) {
const sessions = {};
sessionParts.forEach(session => {
const lines = session.split('\r\n');
const id = util.getValue(lines, 'LogonId');
const starttime = util.getValue(lines, 'starttime');
if (id) {
sessions[id] = starttime;
}
});
return sessions;
}
function fuzzyMatch(name1, name2) {
name1 = name1.toLowerCase();
name2 = name2.toLowerCase();
let eq = 0;
let len = name1.length;
if (name2.length > len) { len = name2.length; }
for (let i = 0; i < len; i++) {
const c1 = name1[i] || '';
const c2 = name2[i] || '';
if (c1 === c2) { eq++; }
}
return (len > 10 ? eq / len > 0.9 : (len > 0 ? eq / len > 0.8 : false));
}
function parseWinUsers(userParts, userQuery) {
const users = [];
userParts.forEach(user => {
const lines = user.split('\r\n');
const domain = util.getValue(lines, 'domain', ':', true);
const username = util.getValue(lines, 'username', ':', true);
const sessionid = util.getValue(lines, 'sessionid', ':', true);
if (username) {
const quser = userQuery.filter(item => fuzzyMatch(item.user, username));
users.push({
domain,
user: username,
tty: quser && quser[0] && quser[0].tty ? quser[0].tty : sessionid
});
}
});
return users;
}
function parseWinLoggedOn(loggedonParts) {
const loggedons = {};
loggedonParts.forEach(loggedon => {
const lines = loggedon.split('\r\n');
const antecendent = util.getValue(lines, 'antecedent', ':', true);
let parts = antecendent.split(',');
const domainParts = parts.length > 1 ? parts[0].split('=') : [];
const nameParts = parts.length > 1 ? parts[1].split('=') : [];
const domain = domainParts.length > 1 ? domainParts[1].replace(/"/g, '') : '';
const name = nameParts.length > 1 ? nameParts[1].replace(/"/g, '') : '';
const dependent = util.getValue(lines, 'dependent', ':', true);
parts = dependent.split('=');
const id = parts.length > 1 ? parts[1].replace(/"/g, '') : '';
if (id) {
loggedons[id] = {
domain,
user: name
};
}
});
return loggedons;
}
function parseWinUsersQuery(lines) {
lines = lines.filter(item => item);
let result = [];
const header = lines[0];
const headerDelimiter = [];
if (header) {
const start = (header[0] === ' ') ? 1 : 0;
headerDelimiter.push(start - 1);
let nextSpace = 0;
for (let i = start + 1; i < header.length; i++) {
if (header[i] === ' ' && ((header[i - 1] === ' ') || (header[i - 1] === '.'))) {
nextSpace = i;
} else {
if (nextSpace) {
headerDelimiter.push(nextSpace);
nextSpace = 0;
}
}
}
for (let i = 1; i < lines.length; i++) {
if (lines[i].trim()) {
const user = lines[i].substring(headerDelimiter[0] + 1, headerDelimiter[1]).trim() || '';
const tty = lines[i].substring(headerDelimiter[1] + 1, headerDelimiter[2] - 2).trim() || '';
// const dateTime = util.parseDateTime(lines[i].substring(headerDelimiter[5] + 1, 2000).trim(), culture) || '';
result.push({
user: user,
tty: tty,
});
}
}
}
return result;
}
exports.users = users;
|