-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsshell.js
328 lines (291 loc) · 10.7 KB
/
sshell.js
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
const PEM = {
JUST_ROOT: 'justRoot',
SUDO_NO_PASSWD: 'sudoNoPasswd',
SUDO_PASSWD: 'sudoPasswd',
SU: 'su'
};
exports.runCmd = (con, command) => exports.runCmdInteractive(con, command);
exports.runCmdAsRootMethod = (con, command, method, forceBreak) => {
var ctrlC = '';
if (forceBreak) {
ctrlC = '\x03';
}
switch (method) {
case PEM.JUST_ROOT:
return exports.runCmd(con, command);
case PEM.SUDO_NO_PASSWD:
return exports.runCmd(con, 'sudo -H -i -- sh -c \'' + command.replace(/'/g, '\'') + '\'');
case PEM.SUDO_PASSWD:
return exports.runCmdInteractive(con, 'sudo -H -i -- sh -c \'' + command.replace(/'/g, '\'') + '\'', [{
regex: /:\s*$/,
answer: con.config.password + '\n'
},
{
regex: /:\s*$/,
answer: ctrlC //Ctrl-C for try again
}
]);
case PEM.SU:
return exports.runCmdInteractive(con, 'su - -c \'' + command.replace(/'/g, '\'') + '\'', [{
regex: /:\s*$/,
answer: con.config.rootPassword + '\n'
},
{
regex: /:\s*$/,
answer: ctrlC //Ctrl-C for try again
}
]);
default:
return new Promise((rs, rj) => rj);
}
};
exports.runCmdAsRoot = (con, command) => {
return detectPrivilegeEscalationMethod(con)
.then(method =>
exports.runCmdAsRootMethod(con, command, method)
)
.catch((e) => Promise.reject(e));
};
exports.runBashScriptAsRoot = (con, script, args) => {
var script64 = Buffer.from(script, 'binary').toString('base64');
if (!args) {
args = '';
}
var command = 'echo ' + script64 + ' | base64 -d | /bin/bash /dev/stdin ' + args;
return rushPackage(con, 'bash')
.then(() => rushPackage(con, 'base64'))
.then(() => detectPrivilegeEscalationMethod(con))
.then(method => exports.runCmdAsRootMethod(con, command, method))
.catch((e) => Promise.reject(e));
};
exports.runCmdInteractive = (con, command, reactions) => {
var stdout = '';
var stderr = '';
var reactionN = 0;
return new Promise((resolve, reject) => {
var makeTry = () => {
var isSuccessful = con.exec(command, {
pty: true
}, (err, stream) => {
if (err) {
if (/open fail/i.test(err)) {
log.error('Exec failed with ' + err);
con.end();
}
logCmd(con, command, undefined, undefined, err);
reject(err);
return;
}
stream.on('close', code => {
logCmd(con, command, code, stdout, stderr);
if (code === undefined || code === 0) { // Bugfix: on shutdown, code === undefined
resolve(stdout);
} else {
if ((!stderr || stderr.length < 1) && stdout && stdout.length > 0) {
reject('Exit code: ' + code + '\n' + stdout);
}
reject('Exit code: ' + code + '\n' + stderr);
}
})
.on('data', data => {
stdout += data;
if (reactions && reactionN < reactions.length) {
if (reactions[reactionN].regex.test(data)) {
stream.write(reactions[reactionN].answer);
reactionN++;
} else if (reactions[reactionN].optional) {
// If current answer optional -> find first required
for (i = reactionN + 1; i < reactions.length; i++) {
if (reactions[i].regex.test(data)) {
stream.write(reactions[i].answer);
reactionN = i + 1;
break;
} else if (reactions[i].optional) {
continue;
} else {
break;
}
}
}
}
}).stderr.on('data', data => {
stderr += data;
});
});
if (!isSuccessful) {
con.on('continue', makeTry);
}
};
makeTry();
});
};
var detectPrivilegeEscalationMethod = con => {
return new Promise((resolve, reject) => {
if (con.privilegeEscalationMethod) {
resolve(con.privilegeEscalationMethod);
return;
}
//Already root
var checkJustRoot = () => {
exports.runCmdAsRootMethod(con, 'whoami', PEM.JUST_ROOT)
.then(data => {
if (/\s*root\s*/.test(data)) {
con.privilegeEscalationMethod = PEM.JUST_ROOT;
resolve(con.privilegeEscalationMethod);
} else {
checkSudoNoPasswd();
}
}, checkSudoNoPasswd);
};
//Sudo without password
var checkSudoNoPasswd = () => {
exports.runCmdAsRootMethod(con, 'whoami', PEM.SUDO_NO_PASSWD)
.then(data => {
if (/\s*root\s*/.test(data)) {
con.privilegeEscalationMethod = PEM.SUDO_NO_PASSWD;
resolve(con.privilegeEscalationMethod);
} else {
checkSudoPasswd();
}
}, checkSudoPasswd);
};
//Sudo with password
var checkSudoPasswd = () => {
exports.runCmdAsRootMethod(con, 'whoami', PEM.SUDO_PASSWD, true)
.then(data => {
if (/\s*root\s*/.test(data)) {
con.privilegeEscalationMethod = PEM.SUDO_PASSWD;
resolve(con.privilegeEscalationMethod);
} else {
checkSu();
}
}, checkSu);
};
//Su
var checkSu = () => {
exports.runCmdAsRootMethod(con, 'whoami', PEM.SU, true)
.then(data => {
if (/\s*root\s*/.test(data)) {
con.privilegeEscalationMethod = PEM.SU;
resolve(con.privilegeEscalationMethod);
} else {
reject({
message: 'Can not get root'
});
}
}, () => reject({
message: 'Can not get root'
}));
};
checkJustRoot();
});
};
var rushPackage = (con, package) => {
var command = 'which ' + package +
' || ( apt-get update && apt-get install -y ' + package + ' )' +
' || yum install -y ' + package +
' || zypper install -y ' + package +
' || urpmi --auto ' + package +
' || pacman --noconfirm -Sy ' + package +
' || upgradepkg --install-new ' + package +
' || apk --no-cache add ' + package;
return exports.runCmdAsRoot(con, command);
};
var logCmd = (con, command, code, out, err) => {
log.debug('com: %s; ext: %s out: %s; err: %s',
command, code, out, err);
};
var makeTempPassword = con => {
con.origPassword = con.config.password;
con.tempPassword = con.config.password.charAt(0) + 'ssheller' + con.config.password.slice(1);
/* //WORKAROUD FOR LINUX CHECKS:
if (palindrome(oldmono, newmono)) {
msg = _("Bad: new password cannot be a palindrome");
} else if (strcmp(oldmono, newmono) == 0) {
msg = _("Bad: new and old password must differ by more than just case");
} else if (similar(oldmono, newmono)) {
msg = _("Bad: new and old password are too similar");
} else if (simple(old, new)) {
msg = _("Bad: new password is too simple");
} else if (strstr(wrapped, newmono)) {
msg = _("Bad: new password is just a wrapped version of the old one");
}
// And "The password is too similar to the old one" on CentOS
*/
};
exports.setTempPassword = con => {
log.warning("Changing password to temporary");
makeTempPassword(con);
var answers = [{
regex: /current/i,
answer: con.origPassword + '\n',
optional: true
}, {
regex: /new/i,
answer: con.tempPassword + '\n'
}, {
regex: /new/i,
answer: con.tempPassword + '\n'
}, {
regex: /error|bad|fail/i,
answer: '\x03'
}];
return exports.runCmdInteractive(con, 'true', answers);
};
exports.setBackPassword = con => {
log.warning("Changing password back");
var answers = [{
regex: /current/i,
answer: con.tempPassword + '\n',
optional: true
}, {
regex: /new/i,
answer: con.origPassword + '\n'
}, {
regex: /new/i,
answer: con.origPassword + '\n'
}, {
regex: /error|bad|fail/i,
answer: '\x03'
}];
return exports.runCmdInteractive(con, 'passwd', answers);
};
exports.checkPasswordExpire = con => {
if (con.config.password === con.tempPassword) {
//Step 3: Set orig password back
return exports.setBackPassword(con)
.then(() => {
delete(con.tempPassword);
con.reconnect();
log.warning('Password changed back to orig');
return Promise.reject('On orig password');
});
}
// Step 1: Check for password expired
var answers = [{
regex: /:/,
answer: '\x03'
}, {
regex: /:/,
answer: '\x03'
}, {
regex: /:/,
answer: '\x03'
}];
return exports.runCmdInteractive(con, 'true', answers)
.then((e) => {
log.info('Not needed to change password');
}, (e) => {
if (/password/i.test(e)) {
//Step 2: Set temp password
return exports.setTempPassword(con)
.then(() => {
con.reconnect();
log.warning('Password changed to temp');
return Promise.reject('On temp password');
});
} else {
log.error("Connection broken: %s", e);
}
});
};