#!/usr/bin/perl

#
# googlegath.pl - simple Google-Search to gathering site/domain informations; 
# Copyright (c) 2006 by Matteo Cantoni <goony@nothink.org>
#
# Note: put your google's key in "GOOGLE_KEY" (http://www.google.com/apis/)
#

use strict;

use Getopt::Std;
use Net::Google;

use constant GOOGLE_KEY => "INSERT YOUR KEY";

my $name        = "googlegath.pl";
my $version     = "v0.3";
my $description = "simple Google-Search to gathering site/domain informations";
my $copyright   = "Copyright (c) 2006";
my $author      = "Matteo Cantoni <goony\@nothink.org>";

our ($opt_a, $opt_i, $opt_t, $opt_s, $opt_f, $opt_k, $opt_m, $opt_d, $opt_l, $opt_h);

my $usage = "$name $version - $description\n$copyright by $author\n
 Usage ./$name [-a allinurl] [-i inurl] [-t intitle] [-s site/domain] [-f filetype] [-k keyword] [-m max_results] [-d debug] [-l logfile.html] [-h]

	-m, default max_results is 100;
	-d, enable debug: print url, domains and rdns;
	-l, enable logging in html format; 

 Examples\n
	./$name -a /backup/ -s gov -f txt -m 16
	./$name -a /cgi-bin/ -s com -k awstats -m 10 -d
	./$name -a /cgi-bin/ -s de -f pl -m 10 -d
	./$name -a /scripts/ -s edu -f cgi -m 10 -l logfile.html
	./$name -s edu -f cgi -m 20 -d
	./$name -t \"VNC Desktop\" -i 5800 -m 10 -d -l logfile.html
	./$name -i \"main.php\" -k \"phpMyAdmin\" -s com -m 10\n
";

getopts('a:i:t:s:f:m:k:dl:h');
die $usage if $opt_h;
die $usage if !$opt_a && !$opt_i && !$opt_t && !$opt_k && !$opt_s && !$opt_f;

$|=1;

my $allinurl    = $opt_a || "";
my $inurl       = $opt_i || "";
my $intitle     = $opt_t || "";
my $site        = $opt_s || "";
my $filetype    = $opt_f || "";
my $keyword     = $opt_k || "";
my $max_results = $opt_m || 100;

my $localtime   = localtime();

my $google = Net::Google->new(
	key=>GOOGLE_KEY
);

if ($opt_l){
	open (LOG, ">$opt_l") || die " Cannot open the log file: $!\n";
	print LOG "<html><head><title>$name log file</title><body>\n";
	print LOG "<h2>$name log file</h2>\n";
	print LOG "<h3>$localtime</h3>\n";
}

print "$name $version - $description\n$copyright by $author\n";

if ($allinurl){
	$allinurl = "allinurl:$allinurl";
}else{
	$allinurl = "";
}

if ($inurl){
	$inurl = "inurl:$inurl";
}else{
	$inurl = "";	
}

if ($intitle){
	$intitle =~ s/^|$/"/g;
	$intitle = "intitle:$intitle";
}else{
	$intitle = "";	
}

if ($site){
	$site = "site:$site";
}else{
	$site = "";
}

if ($filetype){
	$filetype = "filetype:$filetype";
}else{
	$filetype = "";
}

if ($keyword){
	$keyword =~ s/^|$/"/g;
}else{
	$keyword = "";
}

my $string = "$allinurl $inurl $intitle $site $filetype $keyword";

$string =~ s/^\s\s+|\s\s+$//g;
print "\n[+] $string\n\n";
print LOG "\n<b><font color=#770000>$string</font></b><br><br>" if $opt_l;

scan($string);

print "\n";

if ($opt_l){
	print LOG "</body>\n</html>";
	close LOG;
	print "[+] log file $opt_l created.\n";
}

exit(0);

sub scan {
	my $string = shift;

	my $search = $google->search(
		max_results=>$max_results
	);

	$search->query(($string));

	foreach my $r (@{$search->response()}){
		my @results = map { $_->URL(); } @{$r->resultElements()};
		foreach my $res(@results){

			if ($opt_d){
				my (undef,$site) = split(/http:\/\/|https:\/\//,$res);
				($site,undef) = split(/\//,$site);

				if ($site !~ /\w+\:\d+|(\d+)\.(\d+)\.(\d+)\.(\d+)\:\d+/g){
					my $addr = gethostbyname("$site");
					my $ip_add = join('.', unpack("C*",$addr)) || "-";

					print "$res $site $ip_add\n";
					print LOG "<a href=\"$res\">$res</a> <i>$site $ip_add</i><br>" if $opt_l;
				}else{
					my (undef,$addr,undef) = split(/\:/,$res);
					$addr =~ s/\/+//g;
					my $ip_add = gethostbyname("$addr");
					$ip_add = join('.', unpack("C*",$ip_add)) || "-";
					print "$res $addr $ip_add\n";
					print LOG "<a href=\"$res\">$res</a><br>" if $opt_l;
				}
			} else{
				print     "$res\n";
				print LOG "<a href=\"$res\">$res</a><br>" if $opt_l;
			}
		}
	}
}