#!/usr/bin/perl
####################
#
# gethead.pl v2
# CHANGES for v2:
#       - added six second socket timeout
#       - added option to scan multiple hosts, input from a file
#       - added option to output data to a file
#       - added -v verbose option
#       - misc cleanups
#
# gets web server version... w0w... INET skillz...
#
# thanks enz00 from helping me beta test this...
#
# shoutz to:
#   -[ all the [geeks], and #31337 on angrypacket, 
#      all those #sin assholes, ;)
#      and to vim of course, the best editor in the world! ]-
#
# dmuz@angrypacket.com
#
####################

##### MODS
# We need the IO::Socket Mod
use IO::Socket::INET; # XXX should really use Socket; here.. I'm lazy
# This is a great mod for grabbing CL args
use Getopt::Std;

##### VARS
$when="05.12.2001";
$version="2";
# This is the string we send to the web server,
# two \n gets it to pop back what we want
$stinkstring = "here comes an angrypacket!\n\n";
#$stinkstring = "HEAD \/ HTTP\/1.0\n\n"; # heh... got low pro?

##### SUBROUTINES
sub INTRO {
        &GETVARS;
        print "\ngethead.pl version $version by dmuz\n\n";
}

sub GETVARS {
        getopt(hpfo);
        # Check for $host or $host_file, run &USAGE if not there
        if ($opt_h) {
                push(@hosts,$opt_h);
        } elsif ($opt_f) {
        # Check if they want to grab hosts from a file
                $host_file = "$opt_f";
                &GETHOSTS
        } else {
                &USAGE;
        }
        # output to file?
        if ($opt_o) {
                $out_file = $opt_o;
        }
        # Check for port, or set default 80
        if ($opt_p) {
                $port = "$opt_p";
        } else {
                $port = "80";
        }
        if ($opt_v) {$verbose = "1"}    
}

sub USAGE {
        print "\n$0 -h hostname -p port -f input-file -o output-file -v\n\n";
        exit;
}

sub GETHOSTS {
        open (HOSTS,"<$host_file") or die "** problem with $host_file: $!!\n"; 
        while ($line = <HOSTS>) {
                chomp $line;
                push(@hosts,$line);
                if ($verbose) {print "Got $line from infile\n";}
        }
        if ($#hosts < 1) {print "dood.. your lame\n";}
}

##### MAIN - GO GET THE FUNGUS MANG!!!!!
&INTRO();

foreach $host (@hosts) 
{
        if ($verbose) {print "starting to scan $host\n";}
        # Initiate our socket
        $smelly_sock = new IO::Socket::INET (
                PeerAddr => $host,
                PeerPort => $port,
                Timeout => 6, # 6s socket time out, change to your tastes
                Proto => 'tcp');
        # If we did not connect no point in going on
        if (!$smelly_sock) {print "** problem with SOCKET on $host: $!\ngoing to next host\n";next;}
        if ($verbose) {print "Succes! Our smelly sock(et) is connected to $host!\n";}

        # Send our string to the web server
        print $smelly_sock $stinkstring;

        # Grab the web servers reply into var $fungus, up to a 1000 bytes
        read $smelly_sock, $fungus, 1000;

        # Split fungus up, so each newline is a seperate element in our array
        # This is so we can grab the info we want, and toss the rest
        @datar = split(/\n/, $fungus);

        # Check all the lines we got from the web server
        # We only want ones that contain the string "Server"
        foreach $i (@datar) {
                $toejam = $i if ($i =~ /Server/);
        }

        # This ugly little bit gets rid of the html tags around our data
        $_ = $toejam;
        s/<ADDRESS>*//;
        s/<\/ADDRESS>//;
        $toejam = $_;

        if ($out_file) 
        {
                # print to our file
                open(OUT,">>$out_file") or die "** problem with $out_file: $!!\n";
                print OUT "$host|$toejam\n";
                close OUT;
        } else {
                # Print out to STDOUT
                print "----------------------------------------------------\n";
                print "The web server is running: \n$toejam\n";
                print "----------------------------------------------------\n\n";
        }

        # Make sure we clean up after our selves!!!
        close $smelly_sock;
}
if ($verbose) {print "\nall done.\n";}

# like, uh... wo0p n' stuff...

