查找包含多个字符串的文件

查找包含多个字符串的文件

我正在尝试查找包含常见恶意代码的脚本,我需要匹配每个文件中的多个字符串。我正在使用的正在工作,但不确定为什么我在输出中得到以下内容:

# 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参数运行。该参数告诉xargs我们期待以空值分隔的参数,但这里的情况并非如此。

答案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结尾得到 the 。这也是为什么您无法将输出重定向到文件,它被打印到标准错误,而不是标准输出,并且您正在重定向后者。您可以使用2>>而不是保存输出>>。然而,这不是您想要的,因为这些都是错误。

您的问题是您正在使用-0withxargs告诉它需要空分隔的数据。由于您的数据实际上是用换行符分隔的(每个文件都在一行上),因此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.和分别被弃用,取而代之的egrep是和。fgrepgrep -Egrep -F

相关内容