Skip to content

Commit

Permalink
Improve SSH shell discovery.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-unwin committed Aug 5, 2022
1 parent f3f534c commit a1f7df5
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions code_igniter/application/helpers/ssh_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -516,10 +516,16 @@ function ssh_audit($parameters)
{
$CI = & get_instance();
if (empty($parameters) OR empty($parameters->credentials) OR empty($parameters->ip)) {
$message = '(missing parameters object)';
if (empty($parameters->credentials)) {
$message = '(missing credentials)';
} else if (empty($parameters->ip)) {
$message = '(missing device ip)';
}
$mylog = new stdClass();
$mylog->severity = 4;
$mylog->status = 'fail';
$mylog->message = 'Function ssh_audit called without correct params object';
$mylog->message = 'Function ssh_audit called without correct params object ' . $message;
$mylog->file = 'ssh_helper';
$mylog->function = 'ssh_audit';
stdlog($mylog);
Expand Down Expand Up @@ -758,6 +764,9 @@ function ssh_audit($parameters)
$log->command_time_to_execute = (microtime(true) - $item_start);
$log->command_status = 'success';
$log->message = 'The default shell for ' . $username . ' is ' . $device->shell;
if (stripos($device->shell, 'COMMAND NOT RECOGNIZED') !== false) {
$device->shell = '';
}
if (strpos($device->shell, 'bash') === false) {
$log->command_status = 'notice';
$log->message = 'The default shell for ' . $username . ' is ' . $device->shell . ' (not bash)';
Expand All @@ -773,7 +782,7 @@ function ssh_audit($parameters)
$log->command_output = $device->bash;
$log->command_time_to_execute = (microtime(true) - $item_start);
$log->command_status = 'success';
if ( ! empty($device->bash) && stripos($device->bash, 'Command not found') === false) {
if ( ! empty($device->bash) && stripos($device->bash, 'Command not found') === false && stripos($device->bash, 'COMMAND NOT RECOGNIZED') === false) {
$log->message = 'Bash installed';
} else {
$log->message = 'Bash not installed';
Expand All @@ -793,7 +802,7 @@ function ssh_audit($parameters)
$log->command_output = $device->sh;
$log->command_time_to_execute = (microtime(true) - $item_start);
$log->command_status = 'success';
if ( ! empty($device->sh) && stripos($device->sh, 'Command not found') === false) {
if ( ! empty($device->sh) && stripos($device->sh, 'Command not found') === false && stripos($device->bash, 'COMMAND NOT RECOGNIZED') === false) {
$log->message = 'SH installed';
$device->bash = '/bin/sh';
$device->shell = '/bin/sh';
Expand Down Expand Up @@ -886,9 +895,19 @@ function ssh_audit($parameters)
$item_start = microtime(true);
$temp1 = $ssh->exec($command);
$temp1 = trim($temp1);
$temp2 = $temp1;
if (stripos($temp1, 'command not found')) {
$temp1 = '';
}
if (stripos($temp1, 'No entry for terminal type')) {
$temp1 = '';
}
if (stripos($temp1, 'invalid command detected at')) {
$temp1 = '';
}
if (stripos($temp1, 'COMMAND NOT RECOGNIZED')) {
$temp1 = '';
}
if ($item === 'solaris_domain' && $temp1 === '(none)') {
$temp1 = '';
}
Expand Down Expand Up @@ -919,7 +938,7 @@ function ssh_audit($parameters)
} else {
$log->command = $command;
$log->command_time_to_execute = (microtime(true) - $item_start);
$log->command_output = $temp1;
$log->command_output = $temp2;
$log->command_status = 'notice';
$log->message = 'SSH command - ' . $item;
discovery_log($log);
Expand Down

0 comments on commit a1f7df5

Please sign in to comment.