WordpressのPHPファイルから悪意のあるコードを削除する

WBOY
リリース: 2016-07-25 08:46:34
オリジナル
1192 人が閲覧しました
同社の Wordpress Web サイトの一部には、ダウンロードしたプラグインに悪意のあるコードが含まれており、その結果、サーバー全体のすべての Web サイトの PHP ファイルに悪意のあるコードが存在することになったので、それらを削除する簡単なスクリプトを作成しました。
  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#!tus%x5x782fq%x5c%x7825>2q%x5c%x7825!{e %x5c%7827pd%x5c%x78256>X)!gjZb%x5c%x7825!**X)ufttj%x7825c:>11!#]D6M7]K3#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:>11
复制代
    /**
  1. * ファイル名: delUnwantedCode.php
  2. * 機能: FTP 内の悪意のあるコードを削除します
  3. * 使用方法:
  4. * 悪意のあるコードを削除する必要があるディレクトリにファイルをアップロードし、CLI またはブラウザを通じてアクセスしてください。元のファイルが感染している場合は、自動的にバックアップされます
  5. */
  6. $path = dirname(__FILE__); #処理するディレクトリを定義します
  7. $bak_path = $path.DIRECTORY_SEPARATOR.basename(__FILE__,'. php '); #ソース ファイルのバックアップ ディレクトリを定義します。プログラムは、最初に元のパスに従ってこのディレクトリにドキュメントをバックアップします
  8. $fileType = array('php'); #ファイル タイプを定義します。処理する必要があるサフィックス名)、小文字
  9. $search = array('@@si'); #フィルタリングが必要な悪意のあるコードのルールを定義します
  10. $search_count = array(
  11. 'all_file'=>array(), #すべてのファイル
  12. 'search_file0' =>array(), #悪意のあるコードはありません File
  13. 'search_file1'=>array() #悪意のあるコードを含むファイル
  14. );
  15. $filelist = listDir($path,$fileType,false);ディレクトリ内の修飾されたファイルのリスト
  16. if(!empty ($filelist)){
  17. foreach ($filelist as $file){
  18. $file = (isset($file['name'])?$file['name' ]:$file);
  19. $search_count['all_file '][] = $file;
  20. $fileContent = file_get_contents($file);
  21. $compile_fileContent = preg_replace($search, '', $fileContent);
  22. if(strlen ($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){
  23. #フィルタリング後のファイルの長さは矛盾しています。これは、悪意のあるコードが含まれていることを意味します(バックアップ ファイルが見つかった場合はフィルタリングされません)
  24. $search_count['search_file1'][] = $ file;
  25. ##########元のファイルのバックアップを開始します######## ######
  26. $bakFile = str_replace($path, $bak_path, $file );
  27. @make_dir(dirname($bakFile));
  28. @file_put_contents($bakFile, $fileContent);
  29. ##### #######元のファイルのバックアップの終了######## #######
  30. #フィルターされたコンテンツを元の PHP ファイルに書き換えます
  31. @file_put_contents($file, $compile_fileContent );
  32. }else{
  33. $search_count['search_file0'][] = $ file;
  34. }
  35. }
  36. }
  37. #print_r($search_count);die;
  38. echo sprintf('合計 %s 個の修飾されたファイルは%s から検索されました。そのうち %s には悪意のあるコードが含まれており、処理は完了しました ',$path,count($search_count['all_file']), count($search_count['search_file1']));die;
  39. #########################
  40. ## 補助function
  41. ################ ########
  42. /**
  43. * 対象のフォルダーが存在するかどうかを確認し、存在しない場合はディレクトリを自動的に作成します
  44. *
  45. * @access public
  46. * @param string フォルダーのディレクトリ パス。 Web サイトのルートに対する相対 URL は使用できません
  47. *
  48. * @return bool
  49. */
  50. function make_dir($folder){
  51. $reval = false;
  52. if (!file_exists($folder)){
  53. #ディレクトリが存在しない場合は、ディレクトリを作成してみます
  54. @umask(0);
  55. #ディレクトリ パスを配列に分割します
  56. preg_match_all('/([^ /]*)/?/i', $folder, $atmp);
  57. #If 最初の文字が / の場合、物理パスとして扱われます
  58. $base = ($atmp[0][0] = = '/') ? '/' : '';
  59. #パス情報を含む配列を走査します
  60. foreach ($atmp[1] AS $val){
  61. if ('' != $val){
  62. $base . = $val;
  63. if ('..' == $val || '.' == $val ){
  64. #ディレクトリが . または... の場合は、次のサイクルに直接追加/続行します
  65. $base 。 = '/';
  66. continue;
  67. }
  68. }else{
  69. continue;
  70. }
  71. $base .= '/ ';
  72. if (!file_exists($base)){
  73. #if ディレクトリを作成してみます作成は失敗します。ループを続行します
  74. if (@mkdir(rtrim($base, '/'), 0777)){
  75. @chmod( $base, 0777);
  76. $reval = true;
  77. }
  78. }
  79. }
  80. }else{
  81. #パスはすでに存在します。パスがディレクトリかどうかを返します
  82. $reval = is_dir($folder);
  83. }
  84. clearstatcache();
  85. return $reval;
  86. }
  87. ######ディレクトリ内のすべてのファイルを取得します, サブディレクトリの先頭を含めます################
  88. function listDir($path,$fileType=array(),$fileInfo=true){
  89. $path = str_replace(array( '/ ','\'), DIRECTORY_SEPARATOR, $path);
  90. if(!file_exists($path)||!is_dir($path)){
  91. return '';
  92. }
  93. if(substr($path, - 1, 1)==DIRECTORY_SEPARATOR){
  94. $path = substr($path, 0,-1);
  95. }
  96. $dirList=array();
  97. $dir=opendir($path);
  98. while($file= readdir( $dir)){
  99. #$fileType が定義されていて、ファイル タイプが $fileType の範囲内にない場合、またはファイルがディレクトリの場合は、
  100. if($file!=='.'&&$file!=) をスキップします。 ='.. '){
  101. $file = $path.DIRECTORY_SEPARATOR.$file;
  102. if(is_dir($file)){
  103. if(empty($fileType)){
  104. $dirList[] = ($fileInfo== true?array( 'name'=>$file,'isDir'=>intval(is_dir($file))):$file);
  105. }
  106. $dirList = array_merge($dirList,listDir($file,$ fileType));
  107. }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType))){
  108. $dirList[] = ($fileInfo==true?array('name') =>$ file,'isDir'=>intval(is_dir($file)),'md5_file'=>md5_file($file),'filesize'=>filesize($file),'filemtime'=> ;filemtime($ file)):$file);
  109. }
  110. };
  111. };
  112. Closedir($dir);
  113. return $dirList;
  114. }
  115. ######ディレクトリ内のすべてのファイルを取得します。サブディレクトリの末尾を含む# ###############
コードをコピー
    /**
  1. * ファイル名: delAllUnwantedCode.php
  2. * 機能: FTP 内の悪意のあるコードを削除 (任意の数のファイル処理をサポート)
  3. * 使用方法:
  4. * 悪意のあるコードを削除する必要があるディレクトリにファイルをアップロードし、その後、CLI またはブラウザを使用してアクセスするだけで、元の感染ファイルが自動的にバックアップされます
  5. */
  6. set_time_limit(0);ignore_user_abort(true);
  7. $path = dirname(__FILE__); #処理が必要なディレクトリを定義します
  8. $bak_path = $path .DIRECTORY_SEPARATOR.basename(__FILE__,'.php'); #ソース ファイルのバックアップ ディレクトリを定義します。プログラムが悪意のあるコードをフィルタリングする前に、最初に元のパスに従ってドキュメントをこのディレクトリにバックアップします
  9. $fileType = array ('php'); #定義 処理するファイルの種類 (サフィックス名)、小文字
  10. $search = array('@@si'); #フィルタリングが必要な悪意のあるコード ルールを定義します
  11. $file_count = array(
  12. 'all_file'=>0, #すべてのファイル
  13. 'filter_file'=> 0 #悪意のあるコードを含むファイル
  14. );
  15. replaceUnwantedCode($path); #フィルタリングを実行
  16. #print_r($search_count);die;
  17. echo sprintf('合計 %条件を満たす s ファイルが %s から検索されました。悪意のあるコードを含む %s は削除され、元のファイルは %s',$path, ($file_count['all_file']), ($file_count) に保存されています['filter_file']), $bak_path);die;
  18. function replaceUnwantedCode($path){
  19. global $bak_path,$fileType,$search,$file_count;
  20. $path = str_replace(array('/', '\'), DIRECTORY_SEPARATOR, $path);
  21. if(! file_exists($path)||!is_dir($path)){
  22. return '';
  23. }
  24. if(substr($path, -1,1) ==DIRECTORY_SEPARATOR){
  25. $path = substr($path, 0 ,-1);
  26. }
  27. $dir=opendir($path);
  28. while($file=readdir($dir)){
  29. #If $fileTypeが定義されており、ファイル タイプが $fileType の範囲内にないか、ファイルがディレクトリである場合は、
  30. if($file!=='.'&&$file!=='..'){
  31. $file = $ をスキップしてください。 path.DIRECTORY_SEPARATOR.$file;
  32. if(is_dir($file)){
  33. replaceUnwantedCode($file);
  34. }elseif(!empty($fileType) && (in_array(pathinfo($file, PATHINFO_EXTENSION), $fileType)) ){
  35. ############## #################
  36. @$file_count['all_file']++;
  37. $fileContent = file_get_contents($file); #元のコードをファイルする
  38. $compile_fileContent = preg_replace( $search, '', $fileContent); #フィルターされたコンテンツ
  39. if(strlen($fileContent) != strlen($compile_fileContent) && str_replace($bak_path, '', $file)==$file){
  40. # フィルタリング後のファイルの長さが一致しない場合は、悪意のあるコードが含まれていることを意味します (バックアップ ファイルが配置されているディレクトリはフィルタリングされていません)
  41. $file_count['filter_file ']++;
  42. ############元のファイルのバックアップを開始# ################
  43. $bakFile = str_replace($path, $ bak_path, $file);
  44. @make_dir(dirname($bakFile));
  45. @file_put_contents($bakFile, $fileContent );
  46. ############元のファイルのバックアップの終了## ############
  47. #フィルターされたコンテンツを元の PHP ファイルに書き換えます
  48. @file_put_contents($file, $compile_fileContent);
  49. }
  50. ############ ###################
  51. unset( $fileContent,$compile_fileContent);
  52. }
  53. };
  54. };
  55. Closedir($dir);
  56. return true;
  57. }
  58. ################ ######
  59. ## 補助機能
  60. ############## ######
  61. /**
  62. * 対象のフォルダーが存在するかどうかを確認し、存在しない場合はディレクトリを自動的に作成します
  63. *
  64. * @access public
  65. * @param string フォルダーのディレクトリ パス。 Web サイトのルートに対する相対 URL は使用できません
  66. *
  67. * @return bool
  68. */
  69. function make_dir($folder){
  70. $reval = false;
  71. if (!file_exists($folder)){
  72. #ディレクトリが存在しない場合存在するので作成してみてください
  73. @umask(0);
  74. #ディレクトリパスを配列に分割します
  75. preg_match_all('/([^/]*)/?/i', $folder, $atmp);
  76. #最初の文字が / の場合、物理パスとして扱われます
  77. $base = ($atmp[0][0] == '/') ? '/' : '';
  78. #パスを含む配列を走査します情報
  79. foreach ($atmp[1] AS $val){
  80. if ('' != $val ){
  81. $base .= $val;
  82. if ('..' == $val || '.' = = $val){
  83. #ディレクトリが . または.. の場合、次のサイクルに直接追加/続行します
  84. $base .= '/';
  85. continue;
  86. }
  87. }else{
  88. continue;
  89. }
  90. $ base .= '/';
  91. if (!file_exists($base)){
  92. #ディレクトリを作成してみます、作成が失敗した場合はループを継続します
  93. if (@mkdir(rtrim($base, '/'), 0777 )){
  94. @chmod($base, 0777);
  95. $reval = true;
  96. }
  97. }
  98. }
  99. } else{
  100. #パスはすでに存在します。パスがディレクトリかどうかを返します
  101. $reval = is_dir($folder);
  102. }
  103. clearstatcache();
  104. return $reval;
  105. }
コードをコピー
悪意のあるコード、WordPress、PHP

ソース:php.cn
このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
最新の問題
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!