-
Notifications
You must be signed in to change notification settings - Fork 78
Expand file tree
/
Copy pathindex.js
More file actions
302 lines (249 loc) · 7.76 KB
/
index.js
File metadata and controls
302 lines (249 loc) · 7.76 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
/* eslint-disable global-require */
const fs = require('fs'); // for fs.existsSync
const os = require('os');
const { join } = require('path');
const async = require('async');
const remember = require('memorize');
const cp = require('child_process');
const os_name = process.platform.replace('darwin', 'mac').replace('win32', 'windows');
const system = require(join(__dirname, os_name));
const { version } = require(join('..', '..', 'package'));
/// ///////////////////////////////////////////////////
// helpers
const clean_string = function (str) {
return str.replace(/[^A-Za-z0-9\s]/g, '_').trim();
};
const capitalize = function (str) {
return str.charAt(0).toUpperCase() + str.slice(1);
};
/// ///////////////////////////////////////////////////
// exports, base functions
module.exports = system;
system.os_name = os_name;
system.get_os_version((err, os_version) => {
if (!err) {
system.os_release = os_version;
system.user_agent = `Prey/${version} (Node ${process.version} ${system.os_name[0].toUpperCase()}${system.os_name.slice(1)} ${os_version})`;
}
});
system.paths = require('./paths');
// bin/scripts for as_logged_user
const node_bin = join(system.paths.package, 'bin', 'node');
const safexec_bin = join(__dirname, 'windows', 'bin', 'safexec.exe');
const runner_script = join(__dirname, 'utils', 'runner.js');
system.get_device_name = function (cb) {
return os.hostname().replace(/\.local$/, ''); // remove trailing '.local'
};
system.tempfile_path = function (filename) {
return join(system.paths.temp, filename);
};
system.get_os_info = remember((cb) => {
let done;
const data = {};
data.arch = process.arch == 'x64' ? 'x64' : 'x86';
async.parallel([
system.get_os_name,
system.get_os_version,
], (err, results) => {
if (done) return;
data.name = results[0] ? capitalize(results[0]) : os_name;
if (results[1]) data.version = results[1];
cb(err, data);
done = true;
});
});
system.get_lang((lang) => {
system.lang = lang;
});
system.get_env = system.get_env;
system.get_ubuntu_session_type = system.get_session_type;
system.get_ubuntu_session_display = system.get_session_display;
system.get_current_hostname = system.get_current_hostname;
if (system.os_name == 'windows') {
system.get_os_edition = system.get_os_edition;
system.get_winsvc_version = system.get_winsvc_version;
system.updateAsAdminUser = system.updateAsAdminUser;
system.get_as_admin = system.get_as_admin;
system.run_as_admin = system.run_as_admin;
system.check_service = system.check_service;
}
/// ///////////////////////////////////////////////////
// logger user, running user
// Cache the last logged user to avoid logging repeatedly
let lastLoggedUser = null;
system.get_logged_user = (cb) => {
system.find_logged_user((e, user) => {
if (e || !user || user.trim() == '') {
const err = new Error('No logged user detected.');
if (e) err.message += ` ${e.message}`;
err.code = 'NO_LOGGED_USER';
return cb(err);
}
// Only log when user changes or first time
if (lastLoggedUser !== user) {
const { logger } = require('../agent/common');
logger.info(`Logged user: ${user}`);
lastLoggedUser = user;
}
cb(null, user);
});
};
system.get_admin_user = remember((cb) => {
system.find_admin_user((e, user) => {
if (e || !user || user.trim() == '') {
const err = new Error('No admin user detected.');
if (e) err.message += ` ${e.message}`;
err.code = 'NO_ADMIN_USER';
return cb(err);
}
cb(null, user);
});
});
system.get_running_user = function () {
const s = process.env.LOGNAME
|| process.env.USER
|| process.env.USERNAME || 'System';
return clean_string(s);
};
const get_user = {
logged_user: system.get_logged_user,
admin_user: system.get_admin_user,
};
function get(type, cb) {
get_user[type]((err, user) => cb(err, user));
}
/// ///////////////////////////////////////////////////
// impersonation
system.spawn_as_logged_user = function (command, args, opts, cb, user = null) {
as('logged_user', 'spawn', command, args, opts, cb, user);
};
system.updateAsAdminUser = (what, cb) => {
system.check_service(what, (err, data) => {
if (err) return cb(err);
system.updateAsAdmin(data, cb);
});
};
system.get_as_admin_user = function (what, cb) {
system.check_service(what, (err, data) => {
if (err) return cb(err);
system.get_as_admin(data, cb);
});
};
system.spawn_as_admin_user = function (command, args, opts, cb) {
const options = {
command, args, opts, cb,
};
if (system.os_name == 'windows') {
system.check_service(options, (err, data) => { // An error means the new service isn't available
if (err) {
if (options.args && Array.isArray(options.args) && options.args[0].includes('wipe')) // For wipe only when service is not available
{ return as('logged_user', 'spawn', data.command, data.args, data.opts, data.cb); }
var cb = data.opts;
return cb();
}
var { cb } = data;
if (typeof data.opts === 'function') cb = data.opts;
return cb(null, system.run_as_admin);
});
} else as('admin_user', 'spawn', command, args, opts, cb);
};
system.run_as_logged_user = function (command, args, opts, cb, user = null) {
as('logged_user', 'exec', command, args, opts, cb, user);
};
system.kill_as_logged_user = function (pid, cb) {
var cb = cb || function () { /* boo-hoo */ };
as('logged_user', 'exec', 'kill', [pid], {}, cb);
};
/**
* run_as_user options have the same signature as as_user.
*/
system.run_as_user = as_user;
function as(user_type, type, bin, args, opts, cb, user = null) {
if (typeof opts === 'function') {
var cb = opts;
var opts = {};
} else if (!opts) {
var opts = {};
}
if (user) {
const options = {
user,
type,
bin,
args,
opts,
};
as_user(options, cb);
} else {
get(user_type, (err, userz) => {
if (err) return cb(err);
const options = {
user: userz,
type,
bin,
args,
opts,
};
as_user(options, cb);
});
}
}
/**
* as_user runs a command as the specified user.
* An opts example is as follows:
* {
* user: 'someuser'
* type: 'exec' | 'spawn'
* bin: '/usr/sbin/screencapture'
* args: '/tmp/dest.jpg'
* opts: {}
* }
*/
function as_user(options, cb) {
const { user } = options;
const { bin } = options;
const { type } = options;
const { opts } = options;
const { args } = options;
let finished = false;
let runner = options.bin;
let command = options.args;
if (!(user === system.get_running_user())) {
if (os_name == 'windows') {
runner = safexec_bin;
if (type == 'exec') runner = `"${runner}"`;
command = [`"${bin}"`].concat(args.map((x) => `"${x}"`));
} else {
runner = runner_script;
command = [user, bin].concat(args);
if (fs.existsSync(node_bin)) {
command = [runner].concat(command);
runner = node_bin;
}
}
}
if (!opts.env) opts.env = process.env;
if (process.platform == 'linux' && !opts.env.DISPLAY) {
opts.env.DISPLAY = ':0'; // ensure active display is used
}
if (type == 'exec') {
const cmd = `${runner} ${command ? command.join(' ') : ''}`;
var child = cp.exec(cmd, opts, cb);
child.impersonating = user;
return child;
}
const done = function (e) {
if (finished) return;
finished = true;
cb(e, child);
};
// ok, so they want spawn mode. let's fire up the command.
var child = cp.spawn(runner, command, opts);
child.impersonating = user;
// set a listener on error, so if it fails the app doesn't crash!
child.on('error', (err) => {
if (err.code == 'ENOENT') { err.message = `ENOENT - Executable not found: ${runner}`; }
done(err);
});
process.nextTick(done);
}