Advertisement
infodox

SCANNER.PL

Jan 4th, 2012
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 13.99 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2. #Code by s4m3l0
  3. use strict;
  4. use warnings;
  5.  
  6. use threads;
  7. use threads::shared;
  8. use Thread::Queue;
  9. use Thread::Semaphore;
  10.  
  11. use Bloom::Filter;
  12. use URI;
  13. use URI::URL;
  14. use Web::Scraper;
  15.  
  16. use LWP::UserAgent;
  17. use HTTP::Cookies;
  18. use IO::Socket;
  19.  
  20. use String::Diff;
  21. use String::Diff qw(diff_fully diff diff_merge diff_regexp);
  22. use URI::Split qw(uri_split uri_join);
  23.  
  24. $ua = LWP::UserAgent->new(
  25.                 agent   => 'Mozilla/5.0 (X11; Linux i686; rv:5.0.1) Gecko/20100101 Firefox/5.0.1',
  26.                 timeout => 10,
  27.               );
  28. if ($^O =~ /MSwin/) {system("cls");} else {system("clear");}
  29. print " Do you want to do reverseip? (y/n)>";
  30. $chreverse = <STDIN>;
  31. if ($chreverse =~ /y/) {
  32.     reverseip();
  33.         print " Do you want to do sqli? (y/n)>";
  34.         $chsqli = <STDIN>;
  35.         if ($chsqli =~ /y/) {
  36.         print "Scanning for vulnerabilities in --------------> $addr";
  37.         foreach $urlsqli (@webtarget) {
  38.             sqli();
  39.         }
  40.     }
  41. } else {
  42.     print "Website you want to scan..\? > ";
  43.     $urlsqli=<STDIN>;
  44.     print "Scanning for vulnerabilities in --------------> $addr";
  45.     sqli();
  46. }
  47. sub reverseip {
  48.     if ($^O =~ /MSwin/) {system("cls");} else {system("clear");}
  49.     print "IP/Website you want to reverse..\? > ";
  50.     $website=<STDIN>;
  51.     chomp($website);
  52.     $weblist = 0;
  53.     $step = 0;
  54.     $rob = 0;
  55.     $same = 0;
  56.     $youg = 0;
  57.     if ($website =~/http:\/\//) { $website =~ s/http:\/\///;}
  58.     if ($website =~/\/$/g) { $website =~ s/\/$//;}
  59.     my($addr)=inet_ntoa((gethostbyname($website))[4]);
  60.     print "IP target : $addr <-------------------------------------------------------------\n";
  61.     $ua = LWP::UserAgent->new(
  62.             agent   => 'Mozilla/5.0 (X11; Linux i686; rv:5.0.1) Gecko/20100101 Firefox/5.0.1',
  63.             timeout => 30,
  64.               );
  65.     #--------------------Stephack.com----------------------#
  66.     print "Stephack.com <-------------------------------------\n";
  67.     $res = $ua->get('http://stephack.com/re_ip/'.$addr);
  68.     $ct = $res->content();
  69.     while ($ct =~ /http:\/\/(.*)<br/g) {
  70.         $webtarget[$weblist] = $1;
  71.         ++$weblist;
  72.         ++$step;
  73.     }
  74.     print $step." list for stephack.com\n";
  75.     #--------------------Stephack.com----------------------#
  76.     #--------------------Robtex.com------------------------#
  77.     print "Robtex.com <---------------------------------------\n";
  78.     $res = $ua->get('http://www.robtex.com/ip/'.$addr.'.html');
  79.     $ct = $res->content();
  80.     while ($ct =~ /<span id=\"dns\d+"><a href=\"\/dns\/(.+?)\.html\"/g) {
  81.         $check = $1;
  82.         $check =~ s/^mail\.//;
  83.         $check =~ s/^\*\.//;
  84.         $check =~ s/^\.//;
  85.         $check =~ s/^www\.//;
  86.         $webtarget[$weblist] = $check;
  87.         ++$weblist;
  88.         ++$rob;
  89.     }
  90.     print $rob." list for robtex.com\n";
  91.     #---------------------Robtex.com------------------------#
  92.     #---------------------sameip.org------------------------#
  93.     print "sameip.org <---------------------------------------\n";
  94.         $res = $ua->get('http://www.sameip.org/ip/'.$addr);
  95.         $ct = $res->content();
  96.     while ($ct =~ /<a href=\"http:\/\/www\..+?\" rel=\'nofollow\' title=\"visit .+?\" target=\"_blank\">(.+?)<\/a>/g) {
  97.         $webtarget[$weblist] = $1;
  98.         ++$weblist;
  99.         ++$same;
  100.     }
  101.     print $same." list for sameip.org\n";
  102.     #---------------------sameip.org------------------------#
  103.     #---------------------yougetsignal.com------------------------#
  104.     print "yougetsignal.com <---------------------------------\n";
  105.     my $res = $ua->post('http://www.yougetsignal.com/tools/web-sites-on-web-server/php/get-web-sites-on-web-server-json-data.php', {
  106.            remoteAddress => $addr,
  107.     });
  108.         $ct = $res->content();
  109.     if ($ct !~ /Daily reverse IP check limit reached/g) {
  110.         while ($ct =~ /\[\"(.+?)\"\,/g) {
  111.             $checky = $1;
  112.             $checky =~ s/^www\.//;
  113.             $webtarget[$weblist] = $checky;
  114.             ++$weblist;
  115.             ++$youg;
  116.         }
  117.         print $youg." list for yougetsignal.com\n";
  118.     } else { print 'Daily reverse IP check limit reached'; }
  119.     #---------------------yougetsignal.com------------------------#
  120.     @webtarget = dul(@webtarget);
  121.     @webtarget =  sort (@webtarget);
  122.     print "\n"."-------------------------------------------------------------------------------"."\n";
  123.     foreach my $list (@webtarget) {
  124. #       if ($list !~ /^www\./) { $list = 'www.'.$list; }
  125. #       if ($list !~ /^http:\/\//) { $list = 'http://'.$list; }
  126.             print $list."\n";
  127.         open (weblist, ">>$website");
  128.         print weblist "$list\r\n";
  129.         close(weblist);
  130.         }
  131.     $numweb = scalar(@webtarget);
  132.     open (weblist, ">>$website");
  133.     print weblist "\r\nTotal results of $addr is : $numweb site (sorted)\r\nPowered By s4m3l0\r\n";
  134.     close(weblist);
  135.     print "-------------------------------------------------------------------------------"."\n";
  136.     print "Total ".scalar(@webtarget)." website\n";
  137. }
  138.  
  139. sub dul {
  140.     return keys %{{ map { $_ => 1 } @_ }};
  141. }
  142.  
  143.  
  144. #crawling with signed cookie
  145. my $cookie_jar = './cookie.lwp';
  146.  
  147. my $tmp_ua = LWP::UserAgent->new;
  148. $tmp_ua->timeout(15);
  149. $tmp_ua->protocols_allowed(['http','https']);
  150. $tmp_ua->agent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727;.NET CLR 3.0.04506.30; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
  151.  
  152. $tmp_ua->cookie_jar(HTTP::Cookies->new('file'=>'./cookie.lwp','autosave'=>1));
  153.  
  154. push @{$tmp_ua->requests_redirectable}, 'POST';
  155.  
  156. my $max_threads = 15;
  157.  
  158. #my $base_url = $ARGV[0] || 'http://china.ccsafe.com/';
  159. my $base_url = $ARGV[0] || 'http://ccsafe.com';
  160. my $host = URI::URL->new($base_url)->host;
  161. print $host."\n";
  162.  
  163. my $queue = Thread::Queue->new( );
  164.  
  165. my $semaphore = Thread::Semaphore->new( $max_threads );
  166. my $mutex = Thread::Semaphore->new( 1 );
  167.  
  168.  
  169. #my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
  170. #my $logfile = "crawler".($year+1900).($mon+1).$mday.$hour.$min.$sec.".log";
  171. #open(BANLOG,">>$logfile") or die("can't open logfile:$!\n");
  172.  
  173.  
  174. my $filter = shared_clone( Bloom::Filter->new(capacity => 1000000, error_rate => 0.001) );
  175.  
  176. $queue->enqueue( $base_url );
  177. $filter->add( $base_url );
  178.  
  179. my @tmp_url = ();
  180. push(@tmp_url,$base_url);
  181.  
  182. while( 1 ) {
  183.         # join all threads which can be joined
  184.         #my $joined = 0;
  185.         foreach ( threads->list(threads::joinable) )
  186.         {
  187.                 #$joined ++;
  188.                 $_->join( );
  189.         }
  190.         #print $joined, " joinedn";
  191.  
  192.         # if there are no url need process.
  193.         my $item = $queue->pending();
  194.         if( $item == 0 )
  195.         {
  196.                 my $active = threads->list(threads::running);
  197.                 # there are no active thread, we finish the job
  198.                 if( $active == 0 )
  199.                 {
  200.                         print "All done!\n";
  201.                         last;
  202.                 }
  203.                 # we will get some more url if there are some active threads, just wait for them
  204.                 else
  205.                 {
  206.                         #print "[MAIN] 0 URL, but $active active threadn";
  207.                         sleep 1;
  208.                         next;
  209.                 }
  210.         }
  211.  
  212.         # if there are some url need process
  213.         #print "[MAIN] $item URLn";
  214.         $semaphore->down;
  215.         #print "[MAIN]Create thread.n";
  216.         threads->create( \&ProcessUrl );
  217. }
  218.  
  219. # join all threads which can be joined
  220. foreach ( threads->list() )
  221. {
  222.         $_->join( );
  223. }
  224. sub ProcessUrl
  225. {
  226.         my $scraper = scraper
  227.         {
  228.                 process '//a', 'links[]' => '@href';
  229.         };
  230.  
  231.         my $res;
  232.         my $link;
  233.  
  234.         while( my $url = $queue->dequeue_nb() )
  235.         {
  236.                 eval
  237.                {
  238.                         $scraper->user_agent($tmp_ua);
  239.                         $res = $scraper->scrape( URI->new($url) )->{'links'};
  240.                 };
  241.                 if( $@ )
  242.                 {
  243.                         warn "$@\n";
  244.                         next;
  245.                 }
  246.                 next if (! defined $res );
  247.  
  248.                 #print "there are ".scalar(threads->list(threads::running))." threads, ", $queue->pending(), " urls need process.n";
  249.  
  250.                 foreach( @{$res} )
  251.                 {
  252.                         $link = $_->as_string;
  253.                         $link = URI::URL->new($link, $url);
  254.  
  255.                         # not http and not https?
  256.                         next if( $link->scheme ne 'http' && $link->scheme ne 'https' );
  257.                         # another domain?
  258.                         #next if( $link->host ne $host );
  259.  
  260.                         #search for the sub domain
  261.                         next if(!($link->host =~ /$host/));
  262.  
  263.                         $link = $link->abs->as_string;
  264.  
  265.                         if( $link =~ /(.*?)#(.*)/ )
  266.                         {
  267.                                 $link = $1;
  268.                         }
  269.  
  270.                         next if( $link =~ /.(jpg|png|bmp|mp3|wma|wmv|gz|zip|rar|iso|pdf)$/i );
  271.  
  272.  
  273.                         print "test:$link\n";
  274.  
  275.                         #EscapeUrl,skip query form values
  276.                         my $tmp_link = &EscapeUrl($link);
  277.                         #print "Escape:".$tmp_link."\n";
  278.  
  279.                         $mutex->down();
  280.                         my $tmp_mark = 0;
  281.  
  282.                         #print "test start:$link\n";
  283.                         if( ! $filter->check($tmp_link) )
  284.                         {
  285.                                 #print "Test filter ok:$tmp_link\n";
  286.  
  287.                                 #DiffUrl,diff $link from queue with number
  288.  
  289.                                 foreach(@tmp_url)
  290.                                 {
  291.                                         #print "Test Queue:".$tmpurl."\n";
  292.                                         #print "test-1:$_\ntest-2:$tmp_link\n";
  293.                                         if(&DiffUrl($_,$link))
  294.                                         {
  295.                                                 $tmp_mark = 2;
  296.                                                 last;
  297.                                         }
  298.  
  299.                                 }
  300.  
  301.                                 if( $tmp_mark != 2 )
  302.                                 {
  303.                                         $queue->enqueue($link);
  304.                                         #print "add queue:$link\n";
  305.                                         $filter->add($tmp_link);
  306.                                         print "add filter:$tmp_link\n";
  307.                                         #print BANLOG $filter->key_count(), " ", $link, "\n";
  308.                                         #print $filter->key_count(), " ", $link, "\n";
  309.  
  310.                                         push(@tmp_url,$link);
  311.  
  312.                                 }
  313.                                 else
  314.                                 {
  315.                                         print "pass:$link\n";
  316.                                 }
  317.  
  318.                         }
  319.                         #print "pass:$link\n";
  320.  
  321.                         $mutex->up();
  322.                         undef $link;
  323.                 }
  324.                 undef $res;
  325.         }
  326.         undef $scraper;
  327.         $semaphore->up( );
  328. }
  329.  
  330. #skip arg
  331. sub EscapeUrl
  332. {
  333.         my $urlold = shift;
  334.         my ($scheme,$auth,$path,$query,$frag) = uri_split($urlold);
  335.         my $urlnew = uri_join($scheme,$auth,$path);
  336.  
  337.         my $u = URI->new($urlold);
  338.         my @tmp_array = $u->query_form();
  339.         my $tmp = '';
  340.         my $i = 0;
  341.         for($i=0;$i<@tmp_array;$i+=2)
  342.         {
  343.                 $tmp .=$tmp_array[$i]."=&";
  344.  
  345.         }
  346.         if(@tmp_array != 0)
  347.         {
  348.                 $tmp =~ s/&$//;
  349.                 $urlnew .= "?".$tmp;
  350.         }
  351.         undef $u;
  352.         #print $urlnew."\n";
  353.         return $urlnew;
  354.  
  355. }
  356.  
  357. sub DiffUrl
  358. {
  359.         my $urlold = shift;
  360.         my $urlnew = shift;
  361.         my $urloldx = &EscapeUrl($urlold);
  362.         my $urlnewx = &EscapeUrl($urlnew);
  363.  
  364.         my($old,$new) = String::Diff::diff($urloldx,$urlnewx);
  365.         #my($old,$new) = String::Diff::diff($urlold,$urlnew);
  366.         if (($old =~ m/(\[\d+\])/i) && ($new =~ m/{\d+}/i))
  367.         #if ($new =~ m/{\d+}/i)
  368.         {
  369.                 #print "test num success.\n";
  370.                 return 1;
  371.         }
  372.         else
  373.         {
  374.                 #print "test num failed.\n";
  375.                 return 0;
  376.         }
  377.  
  378.  
  379. }
  380.  
  381. sub sqli {
  382.     if($urlsqli !~/^http:\/\//) {
  383.         $urlsqli  = 'http://www.' . $urlsqli;
  384.         }
  385.     $res = $ua->get($urlsqli);
  386.         $ct = $res->content();
  387.         if ($ct =~ /\.php/g) {
  388.                 print "--------------------->" . $urlsqli . "<---------------------\n";
  389.                 while ($ct =~ /<a href=\"(.*?)\"/g) {
  390.                         $checkphp = $1;
  391.                         if ($checkphp =~ /\.php/g) {
  392.                                 print $checkphp."\n";
  393.                         }
  394.                 }
  395.         print "--------------------->"."End"."<---------------------\n";
  396.         }
  397. }
  398. =bin        foreach $path(@imgs) {
  399.         chomp($path);
  400.         $webcl=$path;
  401.         $webcl = trim($webcl);
  402.         $url = $webcl;
  403.         if($url=~/facebook/ | $url=~/mailto/) {
  404.                 next loop;
  405.         }
  406.         if($url=~/=/) { } else { next loop; }
  407.         $incount=$incount+1;
  408.         if($incount=~/36/) {
  409.                 next loop2;
  410.         }
  411.         if ($url =~ m/=/sim) {
  412.                 $url =~ s/=/='/g;
  413.         }
  414.         print "\n $url";  
  415.         my      $req = HTTP::Request->new( GET => $url );
  416.         my      $response = $ua->request( $req );
  417.         if( $response->content =~ /SQL/ || $response->content =~ /\/var\/www\//) {
  418.                 open OUTFILE, ">>", "scanned.txt" or die $!;
  419.                 print OUTFILE "$url \n";
  420.                 if(($count+1)%2) {
  421.                         print HTML "\t\t\n<tr><td><a href=\"$url\"><font color=\"#66FF66\"><strong>$url</strong></font></a> \n </br> \n </br> \n";
  422.                 } else {
  423.                         print HTML "\t\t\n<tr><td><a href=\"$url\"><font color=\"#66FF66\"><strong>$url</strong></font></a> \n </br> \n </br> \n";
  424.                 }
  425.                 $count++;
  426.                 print "\n [+]", $url, "(",$count,"/",$incount,")";
  427.  
  428.                 close OUTFILE;
  429.         }
  430.         }
  431.         print "\n\n $count vulnerable links found in $linds. Extracted link count: $linkdo \n";
  432. }
  433. =cut
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement