Advertisement
parkdream1

reveseip.pl

May 1st, 2012
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.37 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. use strict;
  3. use Getopt::Std;
  4. use LWP::UserAgent;
  5. use POSIX;
  6. use Socket;
  7. #########################################
  8. #   wir3dev@c0rrupt Oct 1/2011  #
  9. #########################################
  10.  
  11. our @fetched_hosts;
  12.  
  13. sub gather_hosts
  14. {
  15.     my($ip, $step, $verify, $active) = @_;
  16.     my $url = undef;
  17.     my $cont = 1;
  18.     if($step == 1)
  19.     {
  20.         $url = "http://www.bing.com/search?q=ip:$ip";
  21.     }
  22.     else
  23.     {
  24.         $url = "http://www.bing.com/search?q=ip:$ip&go=&qs=n&sk=&sc=1-16&first=$step";
  25.     }
  26.     $step += 10;
  27.     my $search = LWP::UserAgent->new;
  28.     $search->agent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  29.     my $res = $search->get($url);
  30.     die "Failed to retrieve contents\n" unless($res->is_success);
  31.     die "No results for IP $ip\n" if($res->decoded_content =~ /No results found for <strong>ip/);
  32.     $cont = 0 if($res->decoded_content =~ /Ref A:/);
  33.     if($cont)
  34.     {
  35.         print "[+] Gathering hosts, page " . floor($step/10) . "...\n";
  36.         my @fetched_page = split(/\n/, $res->decoded_content);
  37.         foreach(@fetched_page)
  38.         {
  39.             my @tmp_res = /<cite>[:\/\/]*([\w\.\-]+)[\w+\/\.\-_:\?=]*<\/cite>/g;
  40.             foreach(@tmp_res)
  41.             {
  42.                 if($active)
  43.                 {
  44.                     my $req = LWP::UserAgent->new;
  45.                     $req->agent("Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1)");
  46.                     my $rep = $req->get("http://" . $_);
  47.                     if(!$rep->is_success)
  48.                     {
  49.                         print "[INACTIVE] Site $_ appears to be inactive, skipping...\n";
  50.                         next;
  51.                     }
  52.                 }
  53.                 if($verify)
  54.                 {
  55.                     next if(!&verify_hosts($_, $ip))
  56.                 }
  57.                 push @fetched_hosts, $_;
  58.             }
  59.            
  60.         }
  61.     }
  62.     &gather_hosts($ip, $step, $verify) if($cont);
  63.     @fetched_hosts;
  64. }
  65.  
  66. sub verify_hosts
  67. {
  68.     my($host, $ip) = @_;
  69.     print "Checking host $host\n";
  70.     return 0 if($host !~ /[:\/\/]*([\w\.\-]+)[\w+\/\.\-_:\?=]*/);
  71.     my @host = gethostbyname($host);
  72.     if(!@host)
  73.     {
  74.         print "[ERROR] Error converting host $host to ip, skipping...\n";
  75.         return 0;
  76.     }
  77.     my @arr = unpack('C4', $host[4]);
  78.     $host = join('.', @arr);
  79.     if($ip !~ /$arr[0]\.$arr[1]\.$arr[2]\./)
  80.     {      
  81.         print "[VERIFY] Host [$host] does not match range $ip, skipping...\n";
  82.         return 0;
  83.     }
  84.     1
  85. }
  86.  
  87. sub array_unique
  88. {
  89.     my @tmp = @_;
  90.     my %sorted;
  91.     $sorted{$_} = 1 foreach(@_);
  92.     keys(%sorted);
  93. }
  94.  
  95. sub write_file
  96. {
  97.     my $write = $_[0];
  98.     open FILE, ">$write";
  99.     print FILE "$_\n" foreach(@fetched_hosts);
  100.     close FILE;
  101.     print "[+] Saved results in $write\n";
  102. }
  103.  
  104. sub usage
  105. {
  106.     die "Reverse IP coded by wir3dev\nNote: Options in square brackets are required\nUsage: $0 [-q] (ip to check) -w (save file) -v (verify results) -a (verify if site is active)\n";
  107. }
  108.  
  109. print "[#] Reverse IP coded by wir3dev [#]\n";
  110. my %opts;
  111. getopts("q:w:va", \%opts);
  112. usage if(!defined($opts{'q'}));
  113. usage if($opts{'q'} !~ /^\d+\.\d+\.\d+\.\d+$/);
  114. if(exists($opts{'v'})) {$opts{'v'} = 1;} else {$opts{'v'} = 0;}
  115. if(exists($opts{'a'})) {$opts{'a'} = 1;} else {$opts{'a'} = 0;}
  116. print "\n\nGathering hosts for IP $opts{q}...\n";
  117. gather_hosts $opts{'q'}, 1, $opts{'v'}, $opts{'a'};
  118. @fetched_hosts = array_unique @fetched_hosts;
  119. print "---------------------------------------------------------------------------\n";
  120. for(my $i = 0; $i < $#fetched_hosts; $i++)
  121. {
  122.     print "$fetched_hosts[$i]\n";
  123. }
  124. print "\n---------------------------------------------------------------------------\n";
  125. print "[+] Complete. Fetched " . ($#fetched_hosts) . " hosts\n";
  126. write_file $opts{'w'} if(defined($opts{'w'}));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement