12 限制 agent 检查

概述

您可以控制 Zabbix agent 或 agent 2 在执行监控项检查、远程命令或脚本时允许或拒绝使用哪些监控项键值。

为此,请使用这些 agent 配置 参数来定义允许/拒绝规则:

  • AllowKey=<pattern>
  • DenyKey=<pattern>

<pattern> 必须包含单个监控项键值,并支持通配符(*)。 通配符会匹配其所在位置上的任意数量任意字符,并可用于匹配监控项键值或参数(例如,vfs.file.*[*])。

为提高安全性,建议使用精确的监控项键值,而不是通配符。 有关详细信息,请参见 Securing allow/deny rules

与其他 agent 配置参数不同,您可以指定无限数量的 AllowKeyDenyKey 参数。

重要说明

  • 默认情况下,所有 system.run 监控项都被禁用(即使 DenyKey 为空),就像设置了 DenyKey=system.run[*] 作为最后一条规则一样。 因此,你可以允许特定的 system.run 监控项,而无需显式拒绝其他 system.run 监控项。

  • AllowKey 中指定的监控项也必须在 DenyKey 中指定(system.run 监控项除外);否则,Zabbix agent 将不会启动

  • 如果可能,请使用 AllowKey 仅允许所需的监控项,并拒绝其他所有内容。 某些键可能会被滥用于通过路径遍历读取非预期文件(例如 vfs.file.contents["../../../../etc/passwd"]),而新的 Zabbix agent 版本可能会引入你的 DenyKey 规则未覆盖的键。

  • AllowKeyDenyKey 配置不会影响 HostnameItemHostMetadataItemHostInterfaceItem agent 参数。

  • 被拒绝的监控项会变为不受支持,且不会给出任何提示或错误消息;例如:

    • Zabbix agent --print (-p) 命令行参数不会显示被拒绝的监控项键。
    • Zabbix agent --test (-t) 命令行参数会对被拒绝的监控项键返回“Unsupported item key.”。
    • 当启用日志记录(LogRemoteCommands=1)时,Zabbix agent 日志文件不会记录被拒绝的远程命令

Allow/deny 规则顺序

您可以指定无限数量的 AllowKeyDenyKey 规则,但它们的顺序很重要。

  • 规则按从上到下的顺序逐一评估。
  • 当某个监控项键匹配某条规则时,该监控项要么被允许,要么被拒绝,并且规则评估随即停止。

例如,在评估 vfs.file.contents[/etc/passwd] 时,规则处理如下:

AllowKey=vfs.file.contents[/tmp/app.log]    # 监控项键模式不匹配,agent 继续处理下一条规则。
AllowKey=vfs.file.contents[/etc/passwd]     # 监控项键模式匹配;agent 允许该监控项检查并停止规则评估。
DenyKey=vfs.file.*[*]                       # agent 忽略该规则,因为评估已停止。

以下规则顺序将拒绝该监控项检查:

DenyKey=vfs.file.*[*]                       # 监控项键模式匹配;agent 拒绝该监控项检查并停止规则评估。
AllowKey=vfs.file.contents[/etc/passwd]     # agent 忽略该规则,因为评估已停止。
AllowKey=vfs.file.contents[/tmp/app.log]    # agent 忽略该规则,因为评估已停止。

示例

以下示例展示了 AllowKeyDenyKey 的常见配置模式。

允许特定检查和命令

仅允许两个 vfs.file 监控项检查和两个 system.run 命令:

AllowKey=vfs.file.contents[/tmp/app.log]
AllowKey=vfs.file.size[/tmp/app.log]
AllowKey=system.run[/usr/bin/uptime]
AllowKey=system.run[/usr/bin/df -h /]
DenyKey=vfs.file.*[*]

无需设置 DenyKey=system.run[*],因为默认情况下所有其他 system.run 命令都会被拒绝。

允许脚本

允许 Zabbix agent 通过所有可用方法在主机上执行脚本:

  • 全局脚本,可在前端或通过 API 执行(此方法始终使用 system.run[myscript.sh] 键)
  • 来自 动作操作 的远程命令(此方法始终使用 system.run[myscript.sh,nowait] 键)
  • 带有脚本的 system.run Zabbix agent 监控项,例如:
    • system.run[myscript.sh]
    • system.run[myscript.sh,wait]
    • system.run[myscript.sh,nowait]
AllowKey=system.run[myscript.sh,*]

要控制 wait/nowait 参数,必须设置不同的规则。 例如,您可以只允许 system.run[myscript.sh,wait] 监控项,从而排除其他方法:

AllowKey=system.run[myscript.sh,wait]
保护 allow/deny 规则

此示例展示了如何保护过于宽松的 AllowKeyDenyKey 规则。

请考虑以下规则:

AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat*"]
DenyKey=vfs.file.*
DenyKey=system.cpu.load[*]

在 Windows 上,必须使用脱字符 (^) 转义路径中的空格。

这些规则包含通配符 (*),可能会被滥用:

  • test.bat 脚本可以使用任意参数执行,包括非预期参数。
  • vfs.file.* 模式只匹配不带参数的监控项键;但是,所有 vfs.file 监控项都需要参数。
  • system.cpu.load[*] 模式只匹配带参数的监控项键;但是 system.cpu.load 监控项不需要参数。

要保护这些规则,请显式允许 test.bat 仅使用特定参数执行,并拒绝正确的监控项键模式;例如:

AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat status"]
AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat version"]
DenyKey=vfs.file.*[*]
DenyKey=system.cpu.load
DenyKey=system.cpu.load[*]

您可以通过运行以下命令来测试这些规则,这些命令将返回 ZBX_NOTSUPPORTED

cd "C:\Program Files\Zabbix Agent 2"
zabbix_agent2.exe -t system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat debug"]
zabbix_agent2.exe -t vfs.file.size["C:\ProgramData\MyApp\config.ini"]
zabbix_agent2.exe -t vfs.file.contents["C:\Windows\System32\drivers\etc\hosts"]
zabbix_agent2.exe -t system.cpu.load
zabbix_agent2.exe -t system.cpu.load[all,avg1]
模式示例

下表显示了监控项键模式的匹配方式:

  • 只有当监控项键满足 Matches 列中的所有条件时,才会与该模式匹配。
  • 参数必须完全包含在方括号中(例如,vfs.file.contents[*vfs.file.contents*utf8] 都是无效模式)。
Pattern Matches Examples
* 任何带参数或不带参数的键
vfs.file.* 键以 vfs.file. 开头
无参数
Matches:
vfs.file.size
vfs.file.contents

Does not match:
vfs.file.contents[]
vfs.file.size[/var/log/app.log]
vfs.*.contents 键以 vfs. 开头
键以 .contents 结尾
无参数
Matches:
vfs..contents
vfs.mount.point.file.contents

Does not match:
vfs.contents
vfs.file.contents[]
vfs.file.*[*] 键以 vfs.file. 开头
任意参数或空参数
Matches
vfs.file.get.custom[]
vfs.file.size[/var/log/app.log, utf8]

Does not match:
vfs.file.get.custom
vfs.file.contents 键为 vfs.file.contents
无参数
Matches:
vfs.file.contents

Does not match:
vfs.file.contents[/etc/passwd]
vfs.file.contents[] 键为 vfs.file.contents[]
空参数
Matches:
vfs.file.contents[]

Does not match:
vfs.file.contents
vfs.file.contents[*] 键为 vfs.file.contents
任意参数或空参数
Matches:
vfs.file.contents[/path/to/file]

Does not match:
vfs.file.contents
vfs.file.contents[/etc/passwd,*] 键为 vfs.file.contents
第一个参数为 /etc/passwd
第二个参数任意或为空
Matches:
vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd,utf8]

Does not match:
vfs.file.contents[]
vfs.file.contents[/etc/passwd]
vfs.file.contents[*passwd*] 键为 vfs.file.contents
第一个参数包含 passwd
无第二个参数
Matches:
vfs.file.contents[/etc/passwd]

Does not match:
vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd,utf8]
vfs.file.contents[*passwd*,*] 键为 vfs.file.contents
第一个参数包含 passwd
第二个参数任意或为空
Matches:
vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd,utf8]

Does not match:
vfs.file.contents[/etc/passwd]
vfs.file.contents[/tmp/test]
vfs.file.contents[/etc/passwd,utf8] 键为 vfs.file.contents
第一个参数为 /etc/passwd
第二个参数为 utf8
Matches:
vfs.file.contents[/etc/passwd,utf8]

Does not match:
vfs.file.contents[/etc/passwd,]
vfs.file.contents[/etc/passwd,utf16]