-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathindex.js
More file actions
260 lines (245 loc) · 8.42 KB
/
index.js
File metadata and controls
260 lines (245 loc) · 8.42 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
/* eslint-disable consistent-return */
/* eslint-disable global-require */
/// ///////////////////////////////////////
// Prey Node.js Windows Client Functions
// (c) 2011 - Fork Ltd.
// by Tomas Pollak - http://forkhq.com
// GPLv3 Licensed
/// ///////////////////////////////////////
const path = require('path');
const os = require('os');
const needle = require('needle');
const cp = require('child_process');
const { osInfo } = require('../../agent/utils/utilinformation');
const paths = require('../paths');
const { exec } = cp;
const { spawn } = cp;
const osName = os.platform().replace('win32', 'windows');
const LOCALHOST_ACTION = 'http://127.0.0.1:7739/action';
const LOCALHOST_PROVIDER = 'http://127.0.0.1:7739/provider';
const LOCALHOST_UPDATE = 'http://127.0.0.1:7739/update';
exports.monitoring_service_go = false;
// add windows bin path to env
process.env.PATH = `${process.env.PATH};${path.join(__dirname, 'bin')}`;
const binPath = (executable) => path.join(__dirname, 'bin', executable);
const cleanString = (str) => str.replace(/[^A-Za-z0-9\s]/g, '_').trim();
exports.process_running = function (processName, callback) {
const cmd = `tasklist /fi "imagename eq ${processName}"`;
exec(cmd, (err, stdout) => {
const bool = stdout && stdout.toString().indexOf(processName) !== -1;
if (typeof callback !== 'function') return;
callback(!!bool);
});
};
exports.get_os_name = (callback) => {
if (typeof callback !== 'function') return;
callback(null, osName);
};
exports.get_os_version = (cb) => {
const release = os.release();
if (!release || release.trim() === '') {
if (typeof cb !== 'function') return;
cb(new Error('Unable to determine Windows version.'));
} else {
if (typeof cb !== 'function') return;
cb(null, release.trim());
}
};
exports.find_logged_user = (callback) => {
const common = require('../../agent/common');
const gte = common.helpers.is_greater_or_equal;
const done = (err, stdout) => {
if (err) {
if (typeof callback !== 'function') return;
return callback(err);
}
const out = stdout.toString().split('\\');
const user = cleanString(out[out.length - 1]);
if (!user || user === '' || user === 'undefined') {
if (typeof callback !== 'function') return;
return callback(err || new Error('No logged user found.'));
}
callback(null, user);
};
// Get current logged user
exec('powershell -Command "(Get-WmiObject -Class win32_computersystem).UserName"', { timeout: 10000 }, (err, psout) => {
if (err || psout.toString().trim() === '') return callback(new Error('No logged user found.'));
const username = psout.split('\\').pop().trim();
let targetSessionID;
// On Windows versions lower than 10 just return the username
if (!gte(common.os_release, '10.0.0')) return done(null, psout);
// Get the session ID from the current logged user
exec(
`powershell -Command "Get-WmiObject Win32_Process | Where-Object { $_.Name -eq 'explorer.exe' -and $_.GetOwner().User -eq '${username}' } | Select-Object -ExpandProperty SessionId"`,
{ timeout: 10000 },
(errPower, sidout) => {
if (errPower) return done(null, psout);
targetSessionID = parseInt(sidout.trim(), 10);
if (Number.isNaN(targetSessionID)) return done(null, psout);
// Get locked session id's and compare with the current one
exec('powershell -Command "(Get-Process -Name LogonUI -ErrorAction SilentlyContinue).SessionId"', { timeout: 10000 }, (errPowerGet, lockedSid) => {
if (errPowerGet) return done(null, psout);
const sessionIds = lockedSid.split('\n').filter((sessionId) => sessionId.trim() !== '').map((id) => parseInt(id.trim(), 10));
if (sessionIds.includes(targetSessionID)) {
return callback(new Error(`${psout} - System on Windows Lock Screen state.`));
}
return done(null, psout);
});
},
);
});
};
exports.get_os_edition = (callback) => {
if (osName !== 'windows') {
if (typeof callback !== 'function') return;
return callback(new Error('Only for Windows'));
}
osInfo((stdoutsi) => {
if (!stdoutsi || !stdoutsi.distro || stdoutsi.distro.toString().trim() === '') {
if (typeof callback !== 'function') return;
return callback(new Error('No edition found.'));
}
let edition = stdoutsi.distro.split(' ').splice(3)[0];
if (edition === 'Business') edition = 'Pro';
if (typeof callback !== 'function') return;
callback(null, edition);
});
};
exports.get_winsvc_version = (callback) => {
const common = require('../../agent/common');
const gte = common.helpers.is_greater_or_equal;
if (osName !== 'windows' || !gte(common.os_release, '10.0.0')) {
if (typeof callback !== 'function') return;
return callback(null, null);
}
exec(`${path.join(paths.install, 'wpxsvc.exe')} -winsvc=version`, (err, stdout) => {
if (err) {
if (typeof callback !== 'function') return;
return callback(null, null);
}
const serviceVersion = stdout.split('\n')[0];
callback(null, serviceVersion);
});
};
exports.scan_networks = function (cb) {
const cmdPath = binPath('wlanscan.exe');
try {
const child = spawn(cmdPath, ['/triggerscan'], {});
child.on('exit', () => {
if (typeof cb !== 'function') return;
cb();
});
} catch (e) {
return cb();
}
};
exports.check_service = (data, cb) => {
if (exports.monitoring_service_go) {
if (typeof cb !== 'function') return;
return cb(null, data);
}
needle.get(LOCALHOST_ACTION, (err) => {
if (err) {
if (typeof cb !== 'function') return;
return cb(new Error('Admin service not available'), data);
}
exports.monitoring_service_go = true;
if (typeof cb !== 'function') return;
return cb(null, data);
});
};
exports.updateAsAdmin = (data, cb) => {
const opts = {
timeout: 90000,
json: true,
};
needle.post(`${LOCALHOST_UPDATE}?target=${data}`, null, opts, (err, resp) => {
if (err) {
if (typeof cb !== 'function') return;
return cb(err);
}
if (resp?.statusCode !== 200) return cb(new Error('Unable to update provider'));
cb();
});
};
exports.get_as_admin = function (provider, cb) {
const body = {
provider,
};
const opts = {
timeout: 90000,
json: true,
};
needle.post(LOCALHOST_PROVIDER, body, opts, (err, resp, bodyResp) => {
if (err) {
if (typeof cb !== 'function') return;
return cb(err);
}
let data;
try {
data = JSON.parse(bodyResp);
} catch (e) {
return cb(new Error('Unable to parse provider data'));
}
const out = data && data.output ? data.output : null;
return cb(null, out);
});
};
exports.run_as_admin = (command, opts, cb) => {
const body = {
action: command,
key: opts.key,
token: opts.token,
opts: opts.dirs,
optsKeep: opts.dir_keep,
};
needle.post(LOCALHOST_ACTION, body, { json: true, timeout: 120000 }, (err, _resp, bodyResp) => {
if (err) {
if (typeof cb !== 'function') return;
return cb(err);
}
let data;
try {
data = JSON.parse(bodyResp);
} catch (e) {
if (typeof cb !== 'function') return;
return cb(new Error('Unable to parse action data'));
}
const out = data && data.output ? data.output : null;
if (typeof cb !== 'function') return;
return cb(null, out);
});
};
exports.get_lang = function (cb) {
let lang = 'en';
const regPath = path.join('hklm', 'system', 'controlset001', 'control', 'nls', 'language');
const cmd = `reg query ${regPath} /v Installlanguage`;
try {
exec(cmd, (err, stdout) => {
if (!err && stdout.includes('0C0A')) lang = 'es';
if (typeof cb !== 'function') return;
cb(lang);
});
} catch (e) {
return cb(lang);
}
};
exports.get_current_hostname = (callback) => {
exec('hostname', (err, stdout) => {
if (err) {
if (typeof callback !== 'function') return;
return callback(err);
}
if (typeof callback !== 'function') return;
callback(null, stdout.split('\r\n')[0]);
});
};
exports.compatible_with_module_tpm = function (data) {
const editions = ['Pro', 'Education', 'Enterprise'];
const common = require('../../agent/common');
const gte = common.helpers.is_greater_or_equal;
if (data.os_name === 'windows' && gte(os.release().trim(), '10.0.0')
&& data.os_edition && editions.includes(data.os_edition)
&& data.winsvc_version && gte(data.winsvc_version, '2.0.0')) return true;
return false;
};