[原创]IP->DNS域名反查小工具


版权声明:可以任意转载,转载时请务必以超链接形式标明文章原始出处和作者信息

原理很简单,利用MSN Search的特殊搜索语法ip:x.x.x.x
废话少说,上代码,想到哪儿写到哪儿,以后用的时候再慢慢完善

基本用法:
perl SEHack.pl [options]
-ip ip:x.x.x.x #单个IP地址
-iplst file #从文件中读取IP地址列表,格式为ip:x.x.x.x,一条记录一行
-dbglevel [0,1,2] #调试记录级别,2最高
-o file #输出结果文件的文件名

附代码如下:

#!/usr/bin/perl
use LWP::UserAgent;
use strict;
use Getopt::Long;

#Global variables here
my ($baseGoogleURL) = http://www.google.com/search?q=;
# the base google url

my ($baseMSNURL) = http://search.msn.com/results.aspx?q=;
# the base msn url

my ($baseMSNurl) = http://search.msn.com;
# the base msn web site url

my $maxerror = 3;
my $dieOnFail = 1;
my $proxy = “”;
my $debuglevel = 1;
my $debuglog = debug.log;
my $ip = “”;
my $file = ips.txt;
my $ofile = domains.txt;
my $hlp;
my $oquery;

GetOptions(ip:s => \$ip,
iplst:s => \$file,
dbglevel:i => \$debuglevel,
o:s => \$ofile,
h => \$hlp
);

my @queryList = ();

main();

sub main
{
if($hlp){
help();
exit(0);
}
if(length($ip) < 1 && !-e $file){
help();
exit(-1);
}
elsif(-e $file){
open(FILE, <$file) or die Could not open file $file for read: $!\n;
while(my $line = <FILE>){
chomp $line;
if(length $line > 1){
push(@queryList,$line);
}
}
close(FILE);
print loaded . scalar(@queryList). ips from $file\n;
}
else{
push(@queryList, $ip);
}

foreach my $query (@queryList){
logMe(query is $query,$debuglevel,$debuglog);
$oquery = $query;
open (FILE,>>$ofile) or die Could not open file $ofile for write: $!\n;
print FILE $query.\n;
my @domainNames = ();
my $url;
my $hasNext = 0;
my $count = 0;
my %seen = ();
do{
$url = buildSearchURL($baseMSNURL,$query);
my @rsContent = getURLasArray($url,$maxerror,$dieOnFail,$proxy);

my @matches = ();

my $found = 0;
#iterate throughout the line
foreach my $rsLine (@rsContent)
{
#logMe (“当前解析行的内容:”.$rsLine,$debuglevel,$debuglog);
if (@matches = $rsLine=~ /<li class=”dispUrl”>([^\s]+?)<\/li>/g)
{
foreach my $match (@matches){
$match =~ s/\/[a-zA-Z\.\?~=%-_&;\d^\x00-\xff]*//g;
# remove numeric ip
if($match =~ /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/){
# ignore
}
else
{
if(!$seen{$match}++){
print (get domain name: .$match.\n);
push(@domainNames, $match);
print FILE $match.\n;
}
}
}
}

my @nextUrls;
if($found == 0){
if(@nextUrls = $rsLine=~ /<a class=”nP” href=”\/results\.aspx\?q=([a-zA-Z\.\?~=%-_&;\d^\x00-\xff]*)“>[^\s]+?<\/a>/g)
{
foreach my $nextUrl (@nextUrls){
$query = htmlDecode($nextUrl);
#logMe(“next page url is $query”, $debuglevel, $debuglog);
}
$found ++;
}
}
}
if($found > 0){
$hasNext ++;
logMe($query,$debuglevel,$debuglog);
}else{
$hasNext = 0;
}

}while($hasNext);

if(scalar(@domainNames) > 0)
{
print We got . scalar(@domainNames). domain names related to .$oquery.\n;
}
else
{
print We got no domain name related to .$oquery.\n;
}
print FILE \n;
close (FILE);
}
}

sub getURLasArray($)
{
my ($url) = $_[0];
my ($maxerror)= $_[1];
my ($dieOnFail)= $_[2];
my ($proxy)=$_[3];
# my ($maxerror)= 3;
my ($content)=“”;
my (@content);
my ($errorcount)=0;
my ($seconds)=5;
# my ($debuglevel)=”";
# my ($debuglog)=”";
my ($response);
my $ua = LWP::UserAgent->new(env_proxy => 0,
keep_alive => 1,
timeout => 300,
agent => Mozilla/5.001 (windows; U; NT4.0; en-us)
);

#if (length($proxy)>0)
#{
# $ua->proxy(['http', 'ftp'], $proxy);
# logMe (“Setting Proxy “.$proxy,$debuglevel,$debuglog);
#}

logMe (fetching .$url.“”,$debuglevel,$debuglog);

# $content = get($url);
$response = $ua->get($url);
$content=$response->content;
@content=split(/\n/,$content);
while (! $response->is_success())
{
# if we got an error remove all proxies possibly the proxy failed …
$ua->proxy(['http', 'ftp'], );
$errorcount++;
warn (Could not fetch url \n.$url. \n attempt .$errorcount.\n);
logMe (Could not fetch url .$url. attempt .$errorcount..,2,debug.log);
logMe (Could not fetch url .$url. attempt .$errorcount..,2,debug.log);
$response = $ua->get($url);
$content=$response->content;
@content=split(/\n/,$content);

if ($errorcount==$maxerror) # on maximum error
{

logMe (Permanent error fetching url: .$url,$debuglevel,$debuglog);
logMe (Permanent error fetching url: .$url,2,debug.log);
if ($dieOnFail==1)
{
die Permanent error fetching url:\n.$url.\n;
}
else
{
warn (\a\a\a\a\a Permanent error fetching url:\n.$url.\n);
return(-1);
}
}

# increase delay to fool stupid webserver…
logMe (going to sleep for .$seconds*$errorcount. seconds,$debuglevel,$debuglog);
sleep ($seconds*$errorcount);
logMe (woke up!);;
}
logMe (got .scalar(@content). lines,$debuglevel,$debuglog);
return (@content);
}

sub buildSearchURL($)
{
my ($base) = $_[0];
my ($keyWords) = $_[1];

my ($url);

$url = $base.$keyWords;
$url =~ s/ //g;

return ($url);

}

sub logMe($)
{
my ($text) = $_[0];
my ($level)= $_[1];
my ($filename)= $_[2];
if ($level==0)
{
return(0);
}
my ($logfile);
my ($now);
$logfile=>>.$filename;

open (LOGFILE, $logfile) or die Can’t open $logfile: $!;
$now=localtime();
print (LOGFILE $now..$text.\n);
if ($level==2)
{
print ($now..$text.\n);
}
close (LOGFILE);
return(1);
}

sub help
{
print <<EOHELP;
-==========================================================================-
>>>>>>>>>>>>>>>>>>>> MSN Hack v0.0.2 by huangwei <<<<<<<<<<<<<<<<<<<<
-==========================================================================-
syntax:$0
-ip ip:x.x.x.x #单个IP地址
-iplst file #从文件中读取IP地址列表,格式为ip:x.x.x.x,一条记录一行
-dbglevel [0,1,2] #调试记录级别,2最高
-o file #输出结果文件的文件名
EOHELP
}

sub atoi {
my $t;
foreach my $d (split(//, shift())) {
$t = $t * 10 + $d;
}
}

sub htmlDecode {
my $str = shift;

return “” unless (defined $str && $str ne “”);

$str =~ s/&/&/g;
$str =~ s/</</g;
$str =~ s/>/>/g;
$str =~ s///g;
$str =~ s/\<BR\>/\n/g;

return $str;

}

sub htmlEncode {
my $str = shift;

return “” unless (defined $str && $str ne “”);

$str =~ s/&/&/g;
$str =~ s/</</g;
$str =~ s/>/>/g;
$str =~ s///g;
$str =~ s/\n/\<BR\>/g;

return $str;

}


原创文章,转载请注明: 转载自猪在笑 [ http://www.huangwei.me/blog/ ]
本文链接地址: http://www.huangwei.me/blog/2007/12/13/oip-dnsooauedhss-2/


分享家:Addthis中国
您可能还对以下文章感兴趣

, , ,