Interactive terminal without python
TotallyNotElite committed
Dec 22, 2019 at 13:59 UTC
e33fab40c3c01454c991ab5408954ea9facafb4a
2 files changed
+52
-22
agents/meshcore.js
+26
-11
@@ -1184,8 +1184,7 @@ function createMeshCore(agent) {
1184
}
1185
});
1186
}
1187
- } catch (e)
1188
- {
1187
+ } catch (e) {
1188
MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1189
this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1190
this.end();
@@ -1202,17 +1201,33 @@ function createMeshCore(agent) {
1201
{
1202
try
1203
{
1205
- var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } };
1204
+ var bash = fs.existsSync('/bin/bash') ? '/bin/bash' : false;
1205
+ var sh = fs.existsSync('/bin/sh') ? '/bin/sh' : false;
1206
+ var script = fs.existsSync('/usr/bin/script') ? '/usr/bin/script' : false;
1207
+ var python = fs.existsSync('/usr/bin/python') ? '/usr/bin/python' : false;
1208
+ var shell = bash || sh;
1209
1207
- if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) {
1208
- this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], options); // Start as active user
1209
- if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1210
- } else if (fs.existsSync('/bin/bash')) {
1211
- this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], options); // Start as active user
1212
- if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1210
+ var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } };
1211
+ var setupcommands = " alias ls='ls --color=auto'\n";
1212
+ if (shell == sh) setupcommands += "stty erase ^H\n"
1213
+
1214
+ if (script && shell) {
1215
+ this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user
1216
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1217
+ } else if (python && shell) {
1218
+ this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user
1219
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1220
+ } else if (bash) {
1221
+ options.type = childProcess.SpawnTypes.TERM;
1222
+ this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user
1223
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1224
+ } else if (sh) {
1225
+ options.type = childProcess.SpawnTypes.TERM;
1226
+ this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user
1227
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); }
1228
} else {
1214
- this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], options); // Start as active user
1215
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\n"); }
1229
+ MeshServerLog("Failed to start remote terminal session, no shell found");
1230
+ return;
1231
}
1232
} catch (e) {
1233
MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
agents/meshcore.min.js
+26
-11
@@ -1184,8 +1184,7 @@ function createMeshCore(agent) {
1184
}
1185
});
1186
}
1187
- } catch (e)
1188
- {
1187
+ } catch (e) {
1188
MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);
1189
this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() }));
1190
this.end();
@@ -1202,17 +1201,33 @@ function createMeshCore(agent) {
1201
{
1202
try
1203
{
1205
- var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } };
1204
+ var bash = fs.existsSync('/bin/bash') ? '/bin/bash' : false;
1205
+ var sh = fs.existsSync('/bin/sh') ? '/bin/sh' : false;
1206
+ var script = fs.existsSync('/usr/bin/script') ? '/usr/bin/script' : false;
1207
+ var python = fs.existsSync('/usr/bin/python') ? '/usr/bin/python' : false;
1208
+ var shell = bash || sh;
1209
1207
- if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) {
1208
- this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], options); // Start as active user
1209
- if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1210
- } else if (fs.existsSync('/bin/bash')) {
1211
- this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], options); // Start as active user
1212
- if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); }
1210
+ var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } };
1211
+ var setupcommands = " alias ls='ls --color=auto'\n";
1212
+ if (shell == sh) setupcommands += "stty erase ^H\n"
1213
+
1214
+ if (script && shell) {
1215
+ this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user
1216
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1217
+ } else if (python && shell) {
1218
+ this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user
1219
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1220
+ } else if (bash) {
1221
+ options.type = childProcess.SpawnTypes.TERM;
1222
+ this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user
1223
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); }
1224
+ } else if (sh) {
1225
+ options.type = childProcess.SpawnTypes.TERM;
1226
+ this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user
1227
+ if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); }
1228
} else {
1214
- this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], options); // Start as active user
1215
- if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\n"); }
1229
+ MeshServerLog("Failed to start remote terminal session, no shell found");
1230
+ return;
1231
}
1232
} catch (e) {
1233
MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);