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

Posted by c4pr1c3 on December 14, 2007

原理很简单,利用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();

}

if(length($ip) < 1 && !-e $file){

help();

exit(-1);

}

elsif(-e $file){

open(FILE, «/span>$file) or die Could not open file $file for read: $!\n;

while(my $line = </span>){

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 = ;

my $count = ;

my %seen = ();

do{

$url = buildSearchURL($baseMSNURL,$query);

my @rsContent = getURLasArray($url,$maxerror,$dieOnFail,$proxy);

my @matches = ();

my $found = ;

#iterate throughout the line

foreach my $rsLine (@rsContent)

{

#logMe (“当前解析行的内容:”.$rsLine,$debuglevel,$debuglog);

if (@matches = $rsLine=~ /<li class=”dispUrl”>([^\s]+?)«/span>\/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 == ){

if(@nextUrls = $rsLine=~ /<a class=”nP” href=”\/results.aspx\?q=([a-zA-Z.\?~=%-_&;\d^\x00\xff]*)“>[^\s]+?«/span>\/a>/g)

{

foreach my $nextUrl (@nextUrls){

$query = htmlDecode($nextUrl);

#logMe(“next page url is $query”, $debuglevel, $debuglog);

}

$found ++;

}

}

}

if($found > ){

$hasNext ++;

logMe($query,$debuglevel,$debuglog);

}else{

$hasNext = ;

}

}while($hasNext);

if(scalar(@domainNames) > )

{

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) = $_[];

my ($maxerror)= $_[1];

my ($dieOnFail)= $_[2];

my ($proxy)=$_[3];

# my ($maxerror)= 3;

my ($content)=“”;

my (@content);

my ($errorcount)=;

my ($seconds)=5;

# my ($debuglevel)=””;

# my ($debuglog)=””;

my ($response);

my $ua = LWP::UserAgent->new(env_proxy => ,

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) = $_[];

my ($keyWords) = $_[1];

my ($url);

$url = $base.$keyWords;

$url =~ s/ //g;

return ($url);

}

sub logMe($)

{

my ($text) = $_[];

my ($level)= $_[1];

my ($filename)= $_[2];

if ($level==)

{

return();

}

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/«/span>/«/span>/g;

$str =~ s/>/>/g;

$str =~ s///g;

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

return $str;

}

sub htmlEncode {

my $str = shift;

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

$str =~ s/&/&/g;

$str =~ s/«/span>/«/span>/g;

$str =~ s/>/>/g;

$str =~ s///g;

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

return $str;

}