Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for non-interactive shell to nasl_ssh_shell_open(). #744

Merged
merged 4 commits into from
Jun 2, 2021

Conversation

jjnicola
Copy link
Member

@jjnicola jjnicola commented May 20, 2021

What:
Add support for non-interactive shell to nasl_ssh_shell_open().

Why:
Currently there are two options:

  • an interactive shell with pty enabled, which responses are quite verbose (cmds, prompt, etc).
  • one shot cmd exec which close the channel, with the response of the command only.

The new option allows to run multiple commands and only get the answer of this command, whitout the extra data like prompts and commands sent to the target.

How:

## This script uses a non-interactive (non pty) shell to elevate privileges
## in an ssh session.

include("ssh_func.inc");
include("misc_func.inc");

port = 22;
user = 'some_user';
pass = 'some_pass';
priv_pass = 'root_pass';

function clean_buffer(sess) {
  while( TRUE ) {
    c = ssh_shell_read(sess);
    if( strlen( c ) <= 0 ) break;
  }
};


#socket
soc = open_sock_tcp( port );
if( ! soc ) exit( 0 );
display (soc); 

#session
display("Open connection");
sess = ssh_connect( socket:soc );
display("User Auth");
prompt = ssh_userauth(sess, login:user, password:pass);

display("Open shell");
sess = ssh_shell_open (sess, pty:0);
sleep(2);
clean_buffer (sess);


cmd1 = "whoami" + '\n';
sh_wr = ssh_shell_write(sess, cmd:cmd1);
 
sleep(1);
ret = "";
while( TRUE ) {
   c = ssh_shell_read(sess);
   if( strlen( c ) <= 0 ) break;
   else {
     ret = ret + c;
     display("Return whoami before priv login: ", ret);
   }
}

display("Changing to root");
cmd1 = 'su -' + '\n';
sh_wr = ssh_shell_write(sess, cmd:cmd1);
 
sleep(2);
ret = "";
# This is optional, to print the promt and clean the buffer
while( TRUE ) {
   c = ssh_shell_read(sess);
   if( strlen( c ) <= 0 ) break;
   else {
   ret = ret + c;
   display(ret);
   }
}

if (strstr(ret, "Password")){
   cmd1 = priv_pass + '\n';
   sh_wr = ssh_shell_write(sess, cmd:cmd1);
   clean_buffer(sess);
}

cmd1 = "whoami" + '\n';
sh_wr = ssh_shell_write(sess, cmd:cmd1);
 
sleep(1);
ret = "";
while( TRUE ) {
   c = ssh_shell_read(sess);
   if( strlen( c ) <= 0 ) break;
   else {
     ret = ret + c;
     display("Return whoami after priv login: ", ret);
   }
}
 
ssh_shell_close(sess);
ssh_disconnect(sess);
display("Finished, close, disconnect script 1");

Checklist:

@jjnicola jjnicola requested a review from a team as a code owner May 20, 2021 08:50
@jjnicola jjnicola force-pushed the non-interactive-shell branch from 9a031f2 to 1d829a1 Compare May 20, 2021 08:57
jjnicola added 3 commits June 1, 2021 06:41
…led).

Currently there are two options:

- an interactive shell with pty enabled, which responses are quite verbose (cmds, prompt, etc).
- one shot cmd exec which close the channel, with the response of the command only.

The new option allows to run multiple commands and only get the answer of this command, whitout the extra data like prompts and commands sent to the target.
@jjnicola jjnicola force-pushed the non-interactive-shell branch from 1d829a1 to 9cf482e Compare June 1, 2021 11:41
@ArnoStiefvater ArnoStiefvater merged commit 50183b4 into greenbone:master Jun 2, 2021
ArnoStiefvater added a commit that referenced this pull request Jun 2, 2021
Add support for non-interactive shell to nasl_ssh_shell_open(). (backport #744)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants