複数の文字列を含むファイルの検索

複数の文字列を含むファイルの検索

一般的なマルウェアを含むスクリプトを見つけようとしていますが、各ファイルで複数の文字列を一致させる必要があります。私が使っているのはうまくいきますが、なぜ出力に次のものが表示されるのかわかりません。

# egrep -rli --include='*.php' 'return base64_decode' . | xargs -0 grep -l 'isset'
grep: ./wp-content/themes/twentyfourteen/js/menu61.php
./wp-content/themes/twentyfourteen/themes.php
./wp-content/themes/twentytwelve/file68.php
./wp-content/themes/twentythirteen/inc/page.php
./wp-content/themes/twentythirteen/inc/template.php
./wp-content/upgrade/include.php
./wp-content/plugins/wp-slimstat/browscap/diff8.php
./wp-content/plugins/quick-contact-form/gallery56.php
./wp-content/plugins/addthis/css/include.php
./wp-content/plugins/addthis/includes/include.php
./wp-content/plugins/tpc-memory-usage/images/code77.php
./wp-content/plugins/gotmls/images/index.php
./wp-content/plugins/tinymce-advanced/langs/object56.php
./wp-content/plugins/wp-security-audit-log/dirs70.php
./wp-content/plugins/wp-security-audit-log/css/list76.php
./wp-content/plugins/wp-security-audit-log/proxy.php
./wp-content/plugins/image-widget/lang/alias.php
./wp-content/plugins/my-page-order/template.php
./wp-content/uploads/2015/01/footer87.php
./wp-content/menu.php
./wp-includes/js/thickbox/db.php
./wp-includes/js/jquery/ui/footer39.php
./wp-includes/js/imgareaselect/general.php
./wp-includes/css/page25.php
./wp-includes/Text/Diff/Engine/dump.php
: No such file or directory

出力は良好で、私が望むものですが、行1に表示される理由は次のとおりです。

grep: ./wp-content/themes/twentyfourteen/js/menu61.php

最後の行には常に次のものが表示されます。

: No such file or directory

最後に、ファイルにパイプすることは機能しません。

# egrep -rli --include='*.php' 'return base64_decode' . | xargs -0 grep -l 'isset' >> asd

答え1

xargs引数なしで実行してみてください-0。これらのパラメータはxargsnullで区切られた引数を期待するように指示しますが、ここではそうではありません。

答え2

あなたが見るのは結果ではなく、エラーメッセージです。存在しないファイルを検索するように求められた場合、grepのエラー形式はgrep: file name: No such file or directory次のとおりです。

$ grep foo bar
grep: bar: No such file or directory

だからgrep:始まりNo such file or directoryと終わりから。これは出力をファイルにリダイレクトできず、標準出力ではなく標準エラーで印刷され、後者をリダイレクトする理由でもあります。2>>出力を保存する代わりに使用できます>>。しかし、これはエラーなので、望むものではありません。

問題は、nullで区切られたデータが必要であることを知らせるために-0withを使用していることです。xargsデータは実際に改行で区切られているので(各ファイルは1行にあるため)、その名前のファイル(複数行のセット)を見つけるように指示xargsします。grepあなたがしたいことは:

 grep -Erli --include='*.php' 'return base64_decode' . | xargs grep -l 'isset'

grepあるいは、ファイル名に改行文字を含めることができ、それをサポートするバージョンがある場合は、grepの-Zフラグを使用してnullで区切られた出力を生成します。

grep -ZErli --include='*.php' 'return base64_decode' . | xargs -0 grep -l 'isset'

また、私はとを使用しており、grep -Eそれぞれegrepとを好むegrepfgrepgrep -Egrep -F

関連情報