#!/usr/bin/perl
#######################################
# Filename: login.saint               #
#######################################


#Added so that Telnet module code be used
use lib 'perllib';
use Net::Telnet ();
require 'config/paths.pl';
require 'perl/misc.pl';
require 'perllib/getopts.pl';

print "BEGIN\n";

$usage = "Usage: login.saint [-bfor] -u <login> -p <passwd> <service> target \n";
if ($#ARGV < 0) {
   die $usage;
   
}

$password = "";
$username = "";
$timeout = 15;
$rootkit = 0;
$openaccount = 0;
$ftplogin = 0;

&Getopts("bfrou:p:") || die $usage;
$rootkit = 1 if defined($opt_r);
$openaccount = 1 if defined($opt_o);
$ftplogin = 1 if defined($opt_f);
$username = $opt_u if defined($opt_u);
$password = $opt_p if defined($opt_p);

# -b option means backwards password
if (defined($opt_b)) {
    $newpassword = "";
    while ($password) {
	$newpassword .= chop $password;
    }
    $password = $newpassword;
}

$target = "$ARGV[1]";
$service = "$ARGV[0]";

if($ARGV[0] =~ /\:/) {
   $port =~ s/\:TCP//;
} elsif ($ARGV[0] =~ /X-(\d+)/) {
   $port = 6000 + $1;
} else {
   open(SERVICES, $SERVICES) or die "$0: cannot open $SERVICES: $!\n";
   while(<SERVICES>) {
      $port_name{$1} = $2 if /(\S+)\s+([0-9]+)\/tcp/;
   }
   $port = $port_name{$ARGV[0]} if $port_name{$ARGV[0]};
}

if (!$port) {
   $port = getservbyname($ARGV[0], "tcp");
}

#Create Telnet Object
$telnet = new Net::Telnet();
$telnet->errmode("return");

#Setup fact line
$status = "a";
$severity="us";
$trustee = "ANY@$target";
$trusted = "ANY@$target";

#Now want to try to login in using RootKit logins/passwords
if ($rootkit) {
 $open_ok = $telnet->open(Host => $target, Port => $port, Timeout =>$timeout);

 if ($open_ok) {

  $login_ok = $telnet->login($username, $password);

  if ($login_ok) {
   $telnet->close;
   $service_output = "rootkits";
   $text = "System may have a RootKit installed";
   &saint_print(); 
  }
 }
}

#Now want to try to login in using Open Accounts logins/passwords
if ($openaccount) {
 $open_ok = $telnet->open(Host => $target, Port => $port, Timeout =>$timeout);

 if ($open_ok) {

  $login_ok = $telnet->login($username, $password);

  if ($login_ok) {
   $telnet->close;
   $service_output = "guessed account password";
   if ($password) {
     $text = "Guessed password to account ($username:$password)";
   }
   else {
     $text = "Account $username has no password";
   }
   &saint_print(); 
  }
 }
}

if ($ftplogin) {
    $args = "<<EOF
open $target
quote USER $username
quote PASS $password
quit
EOF
";
    $recv230 = 0;
    open(FTP, "$FTP -nv $args |") || die "cannot run $FTP";
    while(<FTP>) {
	$recv230 = 1 if /^230/;
    }
    close FTP;
    if ($recv230) {
	if ($password eq "1234") {
	    $service_output = "default router password";
	    $text = "default password on ZyXEL Prestige";
	}
	else {
	    $service_output = "guessed account password";
	    if ($password) {
		$text = "Guessed password to account ($username:$password)";
	    }
	    else {
		$text = "Account $username has no password";
	    }
	}
	&saint_print(); 
    }
}
