Root password using Nodejs mscdex / ssh2

I am trying to log in as root on a remote Linux machine using mscdex / ssh2 , the steps I am trying to achieve are:

  • connect via ssh to a remote device
  • run the command as root user

but I can’t in the second part, I can’t enter the password correctly, here is my code.

conn.on('ready', function() { conn.exec('sudo -s ls /', { pty: true }, (err, stream) => { if (err) { res.send(err); } stream.on('exit', (code, signal) => { console.log(`Stream :: close :: code: ${code}, signal: ${signal}`); }); stream.on('data', data => { // here it where supposedly the password should be given stream.write('r00tpa$$word' + '\n'); console.log(data); }); }); }).connect({ host: '192.168.100.100', username: 'fakeAdmin', password: 'fakePassword' }); 

I already have the pty parameter set to true, but I get error messages on promt.

Update:

here is my new code snippet:

 const Client = require('ssh2').Client; const conn = new Client(); const encode = 'utf8'; conn.on('ready', () => { conn.shell(false, { pty: true }, (err, stream) => { if (err) { console.log(err) } stream.on('data', (data) => { process.stdout.write(data.toString(encode)); }); stream.write('ls -a\n'); stream.write('uptime\n'); stream.write('su\n'); // here nothing seems to happen stream.write('rootPassword\n'); // here also stream.write('cd /tmp && ls\n'); }); }) .connect({ host: "192.168.100.100", username: "username", password: "usernamePassword" }); 

I managed to execute several commands in a purer form, I even raised the problem on the github page of the library . The “su” shell command is losing interaction , but now that this is happening, it’s strange, I can run as many commands as I do, but when I run the “su” command, nothing happens, does anyone come into this earlier ?, or what am I doing wrong? Sorry if I can’t explain correctly.

Sincerely.

+7
npm express
source share
2 answers

Consider using node-ssh . I used it without problems, and it is more modern because it has an asynchronous promise-based interface.

You must also connect directly to the root user if you need to perform root actions, if you do not have a user who does not request a password when executing sudo , for example, with EC2 instances that use Ubuntu AMI, which comes by default with the ubuntu user without a password (you authenticate through ssh keypair).

Trying to enter the system at the command and enter the password in plain text is not a good idea. Also consider adding an ssh key to the root user for authentication with a private key instead of a password.

 const SSH = require('node-ssh') const ssh = new SSH() ssh.connect({ host: '<host>', username: 'root', password: '<password>' }).then(() => ssh.exec('your command')) 
0
source share

I found a way to execute what I was looking for, here is the code:

 const Client = require('ssh2').Client; const conn = new Client(); const encode = 'utf8'; const connection = { host: '100.10.101.11', username: 'admin', password: 'c0mm0nU53rP455' } conn.on('ready', function() { // avoid the use of console.log due to it adds a new line at the end of // the message process.stdout.write('Connection :: ready'); let password = 'y0urR007p455w0rd'; let command = ''; let pwSent = false; let su = false; let commands = [ `su`, `ls /root`, `touch /media/delete_me.txt`, `rm /media/delete_me.txt` // be carefull with this type of commands ]; conn.shell((err, stream) => { if (err) { console.log(err); } stream.on('exit', function (code) { process.stdout.write('Connection :: exit'); conn.end(); }); stream.on('data', function(data) { process.stdout.write(data.toString(encode)); // handle su password prompt if (command.indexOf('su') !== -1 && !pwSent) { /* * if su has been sent a data event is triggered but the * first event is not the password prompt, this will ignore the * first event and only respond when the prompt is asking * for the password */ if (command.indexOf('su') > -1) { su = true; } if (data.indexOf(':') >= data.length - 2) { pwSent = true; stream.write(password + '\n'); } } else { // detect the right condition to send the next command let dataLength = data.length > 2; let commonCommand = data.indexOf('$') >= data.length - 2; let suCommand = data.indexOf('#') >= data.length - 2; if (dataLength && (commonCommand || suCommand )) { if (commands.length > 0) { command = commands.shift(); stream.write(command + '\n'); } else { // su requires two exit commands to close the session if (su) { su = false; stream.write('exit\n'); } else { stream.end('exit\n'); } } } } }); // first command command = commands.shift(); stream.write(command + '\n'); }); }).connect(connection); 
0
source share

All Articles