From e33fab40c3c01454c991ab5408954ea9facafb4a Mon Sep 17 00:00:00 2001 From: TotallyNotElite <1yourexperiment@protonmail.com> Date: Sun, 22 Dec 2019 13:59:24 +0100 Subject: [PATCH] Interactive terminal without python --- agents/meshcore.js | 37 ++++++++++++++++++++++++++----------- agents/meshcore.min.js | 37 ++++++++++++++++++++++++++----------- 2 files changed, 52 insertions(+), 22 deletions(-) diff --git a/agents/meshcore.js b/agents/meshcore.js index 1fcd19e9..c6873489 100644 --- a/agents/meshcore.js +++ b/agents/meshcore.js @@ -1184,8 +1184,7 @@ function createMeshCore(agent) { } }); } - } catch (e) - { + } catch (e) { MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest); this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() })); this.end(); @@ -1202,17 +1201,33 @@ function createMeshCore(agent) { { try { - var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } }; + var bash = fs.existsSync('/bin/bash') ? '/bin/bash' : false; + var sh = fs.existsSync('/bin/sh') ? '/bin/sh' : false; + var script = fs.existsSync('/usr/bin/script') ? '/usr/bin/script' : false; + var python = fs.existsSync('/usr/bin/python') ? '/usr/bin/python' : false; + var shell = bash || sh; - if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) { - this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], options); // Start as active user - if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); } - } else if (fs.existsSync('/bin/bash')) { - this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], options); // Start as active user - if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); } + var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } }; + var setupcommands = " alias ls='ls --color=auto'\n"; + if (shell == sh) setupcommands += "stty erase ^H\n" + + if (script && shell) { + this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } + } else if (python && shell) { + this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } + } else if (bash) { + options.type = childProcess.SpawnTypes.TERM; + this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } + } else if (sh) { + options.type = childProcess.SpawnTypes.TERM; + this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); } } else { - this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], options); // Start as active user - if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\n"); } + MeshServerLog("Failed to start remote terminal session, no shell found"); + return; } } catch (e) { MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest); diff --git a/agents/meshcore.min.js b/agents/meshcore.min.js index 1fcd19e9..c6873489 100644 --- a/agents/meshcore.min.js +++ b/agents/meshcore.min.js @@ -1184,8 +1184,7 @@ function createMeshCore(agent) { } }); } - } catch (e) - { + } catch (e) { MeshServerLog('Failed to start remote terminal session, ' + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest); this.write(JSON.stringify({ ctrlChannel: '102938', type: 'console', msg: e.toString() })); this.end(); @@ -1202,17 +1201,33 @@ function createMeshCore(agent) { { try { - var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } }; + var bash = fs.existsSync('/bin/bash') ? '/bin/bash' : false; + var sh = fs.existsSync('/bin/sh') ? '/bin/sh' : false; + var script = fs.existsSync('/usr/bin/script') ? '/usr/bin/script' : false; + var python = fs.existsSync('/usr/bin/python') ? '/usr/bin/python' : false; + var shell = bash || sh; - if (fs.existsSync('/usr/bin/python') && fs.existsSync('/bin/bash')) { - this.httprequest.process = childProcess.execFile('/usr/bin/python', ['python', '-c', "import pty; pty.spawn([\"/bin/bash\"])"], options); // Start as active user - if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); } - } else if (fs.existsSync('/bin/bash')) { - this.httprequest.process = childProcess.execFile('/bin/bash', ['bash', '-i'], options); // Start as active user - if (process.platform == 'linux') { this.httprequest.process.stdin.write(" alias ls='ls --color=auto'\n"); } + var options = { uid: (this.httprequest.protocol == 8) ? require('user-sessions').consoleUid() : null, env: { HISTCONTROL: "ignoreboth", TERM: "xterm" } }; + var setupcommands = " alias ls='ls --color=auto'\n"; + if (shell == sh) setupcommands += "stty erase ^H\n" + + if (script && shell) { + this.httprequest.process = childProcess.execFile(script, ['script', '--return', '--quiet', '-c', '"' + shell + '"', '/dev/null'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } + } else if (python && shell) { + this.httprequest.process = childProcess.execFile(python, ['python', '-c', 'import pty; pty.spawn(["' + shell + '"])'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } + } else if (bash) { + options.type = childProcess.SpawnTypes.TERM; + this.httprequest.process = childProcess.execFile(bash, ['bash', '-i'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands); } + } else if (sh) { + options.type = childProcess.SpawnTypes.TERM; + this.httprequest.process = childProcess.execFile(sh, ['sh'], options); // Start as active user + if (process.platform == 'linux') { this.httprequest.process.stdin.write(setupcommands + "PS1='$ '\n"); } } else { - this.httprequest.process = childProcess.execFile('/bin/sh', ['sh'], options); // Start as active user - if (process.platform == 'linux') { this.httprequest.process.stdin.write("stty erase ^H\nalias ls='ls --color=auto'\nPS1='\\u@\\h:\\w\\$ '\n"); } + MeshServerLog("Failed to start remote terminal session, no shell found"); + return; } } catch (e) { MeshServerLog("Failed to start remote terminal session, " + e.toString() + ' (' + this.httprequest.remoteaddr + ')', this.httprequest);