Monday, January 29, 2018

root password using Nodejs mscdex/ssh2

Leave a Comment

I'm trying to login as root on a remote linux machine using mscdex/ssh2, the steps I'm trying to achieve are :

  1. connect via ssh to the remot machine
  2. execute command as root user

but I'm failing in the second part, I can't get to put the password right, 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's 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 option set to true, but I'm only getting error messages on the 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've managed to perform the several commands part in a much cleaner way, I even raised an issue on the library github page .shell command "su" loses interaction , but now what it's happening it's kinda weird, I can run as many commands as I what, but when I run a "su" command nothing seems to happen, does somebody step into this before?, or what I'm I doing wrong ? Sorry if I couldn't explain myself right.

Regards.

1 Answers

Answers 1

Consider using node-ssh. I've used it with no problem before and it is more modern as well because it has a promise-based asynchronous interface.

You also should connect directly to the root user if you need to perform root actions, unless you have a user that does not ask for a password when performing sudo, for example with EC2 instances that use the Ubuntu AMI which comes by default with an ubuntu user without password (you authenticate via ssh keypair).

Trying to log in by command and inputting the password in plain text is not a good idea. Also consider adding an ssh key to the root user to authenticate 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')) 
If You Enjoyed This, Take 5 Seconds To Share It

0 comments:

Post a Comment