#!/usr/bin/perl -w
#
# Copyright (C) 2006 Joshua D. Abraham (jabra@spl0it.org)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
# banners.pl - perl banner grabber
#
#
use strict;
use IO::Socket::SSL;
use IO::Socket;

my $raw = 0;
my @ports = (22, 25, 631);
#my @ports = (80, 443);
my $target;
if ( @ARGV == 0 ) {
    print "Usage $0 [ip]\n";
    exit;
}
else {
    $target = $ARGV[0];
}
print "target is $target\n";
foreach(@ports){
    print "\nport is $_\n";
    print "banner is: ";
    if ($_ eq 22 or $_ eq 25 or $_ eq 80 or $_ eq 631){
        my $out1;
        my $remote2 = IO::Socket::INET->new(
                    Proto    => "tcp",
                    PeerAddr => $target,
                    PeerPort => $_,
                )
                or die "cannot connect to port $_ port at $target";
        print $remote2 "GET \n";
        while ( <$remote2> ) { 
            if (defined($_)){
                $out1 .= $_;
                next;
            }
            last;
        }
        close $remote2;
        print $out1;
    }   
    elsif ($_ eq 443){
        print "port is $_\n";
        print "banner is: ";
        my $out2;
        my $client = IO::Socket::SSL->new("$target:https");
        if ($client) {
            print $client "GET / HTTP/1.0\r\n\r\n";
            while ( <$client> ) { 
                if (defined($_)){
                    #    chomp;
                    $out2 .= $_;
                    next;
                }
                last;
            }
            close $client;
            print $out2;
        } else {
            warn "I encountered a problem: connecting to $_ on $target",
            IO::Socket::SSL::errstr();
        }
    }
    else {
        print "port not supported\n";
    }
}

