AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1150968
Accepted
user3223819
user3223819
Asked: 2024-01-06 04:01:58 +0800 CST2024-01-06 04:01:58 +0800 CST 2024-01-06 04:01:58 +0800 CST

为什么 grep 停止匹配并在二进制文件匹配时出错

  • 772

问题

为什么我的 grep 命令匹配某些行,然后停止并显示此错误 grep: /var/log/apache2/modsec_audit.log: 二进制文件匹配

我的 grep 命令: grep '^[' /var/log/apache2/modsec_audit.log

我猜日志文件中有二进制内容,这会弄乱 grep 吗?但我是猜测,所以请解释一下原因是什么。另请解释我可以采取什么措施来解决这个问题。

日志文件本身是 ASCII 文本。我可以使用 less 来读取它文件 /var/log/apache2/modsec_audit.log /var/log/apache2/modsec_audit.log:HTML 文档,ASCII 文本,有很长的行(9938)

设置

在我的网络服务器上我安装了 modsec。审核文件是此处描述的多行记录 https://github.com/SpiderLabs/ModSecurity/wiki/ModSecurity-2-Data-Formats#user-content-Parts

仅供参考,我正在记录这些部分 SecAuditLogParts ABCEFHJZ

A 部分是审核日志标头,只有一行,包含以下信息: 时间戳 唯一事务 ID 源 IP 地址(IPv4 或 IPv6) 源端口 目标 IP 地址(IPv4 或 IPv6) 目标端口

例如 [05/Jan/2024:00:45:31.734758 +0000] ZZdRKyjPxuLDuK2XVhEfLgAAAAU 198.12.243.17 13914 192.168.2.143 443

我正在尝试做什么

我试图从很多很多行中找出 A 部分,并将其用作任意一天发生的事情以及机器人活动最繁忙的时间的简单参考。有很多更好的方法可以做到这一点,但请保持正轨 - grep 出了什么问题以及如何克服它?

linux
  • 1 1 个回答
  • 37 Views

1 个回答

  • Voted
  1. Best Answer
    Håkan Lindqvist
    2024-01-06T04:11:05+08:002024-01-06T04:11:05+08:00

    grep默认情况下不喜欢输出二进制数据(例如,输出二进制数据很可能会弄乱终端),因此它默认只指示binary file matches二进制文件的匹配项。

    如果您无论如何都想要输出,您可能需要该-a选项。
    有关详细信息,请参阅手册中的相关部分:

       -a, --text
              Process a binary file as if it were text; this is equivalent to the --binary-files=text option.
    
       --binary-files=TYPE
              If  a  file's  data or metadata indicate that the file contains binary data, assume that the file is of
              type TYPE.  Non-text bytes indicate binary data; these are either  output  bytes  that  are  improperly
              encoded for the current locale, or null input bytes when the -z option is not given.
    
              By  default, TYPE is binary, and grep suppresses output after null input binary data is discovered, and
              suppresses output lines that contain improperly encoded data.  When some  output  is  suppressed,  grep
              follows any output with a message to standard error saying that a binary file matches.
    
              If  TYPE  is  without-match, when grep discovers null input binary data it assumes that the rest of the
              file does not match; this is equivalent to the -I option.
    
              If TYPE is text, grep processes a binary file as if it were text; this is equivalent to the -a option.
    
              When type is binary, grep may treat non-text bytes as line terminators  even  without  the  -z  option.
              This  means choosing binary versus text can affect whether a pattern matches a file.  For example, when
              type is binary the pattern q$ might match q immediately followed by a null byte, even  though  this  is
              not  matched when type is text.  Conversely, when type is binary the pattern . (period) might not match
              a null byte.
    
              Warning: The -a option might output binary garbage, which can have nasty side effects if the output  is
              a  terminal  and  if  the  terminal  driver interprets some of it as commands.  On the other hand, when
              reading files whose text encodings are unknown, it can be helpful to use -a or to set LC_ALL='C' in the
              environment, in order to find more matches even if the matches are unsafe for direct display.
    
    • 0

相关问题

  • Linux 主机到主机迁移

  • 如何在 Linux 机器上找到有关硬件的详细信息?

  • 如何在 Linux 下监控每个进程的网络 I/O 使用情况?

  • 在 RHEL4 上修改 CUPS 中的现有打印机设置

  • 为本地网络中的名称解析添加自定义 dns 条目

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve