#!/usr/bin/perl 
#
# Copyright (C) 2008, 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
# (at your option) 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
# use strict;
#
# find_vhosts.pl - finds all the vhosts for an ip using msn live 
#
# ex: ./find_vhosts.pl -i 67.78.61.227 
#
use strict;
use Getopt::Long;
use HTML::SimpleLinkExtor;
use vars qw( $PROG );
( $PROG = $0 ) =~ s/^.*[\/\\]//;    # Truncate calling path from the prog name
my $AUTH    = 'Joshua D. Abraham';  # author
my $EMAIL   = 'jabra@spl0it.org';   # email
my $VERSION = '1.00';               # version
my %options;                        # getopt option hash
my ($ip, %print_links);
my $extor = HTML::SimpleLinkExtor->new();

#
# help:
# display help information
#
sub help {
    print "Usage: $PROG [Input Option] [Option] 
    -i  --ip [ip]           IP address
    
    -v  --version           Display version
    -h  --help              Display this information
Send Comments to $AUTH ( $EMAIL )\n";
    exit;
}

#
# print_version:
# displays version
#
sub print_version {
    print "$PROG version $VERSION by $AUTH ( $EMAIL )\n";
    exit;
}

#
# check_authority: URI -> 
# Adds URI to the print_links hash if it isn't a M$ address
# 
sub check_authority {
    my ($uri) = @_;
    my ($scheme, $authority, $path, $query, $fragment) = $uri =~ m/(?:([^:\/?#]+):)?(?:\/\/([^\/?\#]*))?([^?#]*)(?:\?([^#]*))?(?:\#(.*))?/;
    unless ($authority =~ /msn/ or $authority =~ /microsoft/ or $authority =~ /\w+\.live/){
        $print_links{$scheme . '://' . $authority} = 1;
    }

}
if ( @ARGV == 0 ) {
    help;
    exit;
}
GetOptions(
    \%options,
    'ip|i=s',
    'help|h'    => sub { help(); },
    'version|v' => sub { print_version(); },
) or exit 1;

if ( $options{ip} ) {
    $ip = $options{ip};
}
else {
    help();
}

my $url = "http://search.msn.com/results.aspx?q=ip:$ip&FORM=QBHP";
$extor->parse_url( $url );

my @links = $extor->schemes( qw( http https ) );
foreach(@links){
    check_authority($_);
}
foreach my $key (sort keys %print_links){
    print "$key\n";
}
