限制 agent 检查
概述
您可以控制 Zabbix agent 或 agent 2 在执行监控项检查、远程命令或脚本时允许或拒绝使用哪些监控项键值。
为此,请使用这些 agent 配置 参数来定义允许/拒绝规则:
AllowKey=<pattern>DenyKey=<pattern>AllowKeyRegexp=<pattern>DenyKeyRegexp=<pattern>
<pattern> 必须包含单个监控项键值。
对于 AllowKey 和 DenyKey,<pattern> 支持通配符(*)。
通配符会匹配其所在位置上的任意数量任意字符,可用于匹配监控项键值或参数(例如,vfs.file.*[*])。
对于 AllowKeyRegexp 和 DenyKeyRegexp,<pattern> 支持正则表达式。
正则表达式模式可用于匹配监控项键值及其参数字符串。
另请参见正确转义的示例。
请注意,Zabbix agent 和 Zabbix agent 2 可能支持不同的正则表达式语法。
为提高安全性,建议对 AllowKey 和 DenyKey 使用精确的监控项键值,而不是通配符。
同时也建议对 AllowKeyRegexp 和 DenyKeyRegexp 使用尽可能具体的正则表达式模式。
有关详细信息,请参见保护允许/拒绝规则。
与其他 agent 配置参数不同,您可以指定无限数量的 AllowKey、DenyKey、AllowKeyRegexp 或 DenyKeyRegexp 参数。
重要说明
-
默认情况下,所有
system.run监控项都会被禁用(即使DenyKey和DenyKeyRegexp为空),就像设置了DenyKey=system.run[*]或DenyKeyRegexp=^system\.run\[.*\]$作为 最后一条规则 一样。 因此,您可以允许特定的system.run监控项,而无需显式拒绝其他system.run监控项。 -
如果可能,请使用
AllowKey/AllowKeyRegexp仅允许所需的监控项,并拒绝其他所有内容。 某些键可能会被滥用,通过路径遍历读取非预期文件(例如,vfs.file.contents["../../../../etc/passwd"]),而新的 Zabbix agent 版本可能会引入您的DenyKey/DenyKeyRegexp规则未覆盖的键。 -
AllowKey、DenyKey、AllowKeyRegexp和DenyKeyRegexp配置不会影响HostnameItem、HostMetadataItem或HostInterfaceItemagent 参数。 -
被拒绝的监控项会变为不受支持状态,且不会提供任何提示或错误消息;例如:
- Zabbix agent
--print (-p)命令行参数不会显示被拒绝的监控项键。 - Zabbix agent
--test (-t)命令行参数会对被拒绝的监控项键返回“Unsupported item key.”。 - 当启用日志记录(
LogRemoteCommands=1)时,Zabbix agent 日志文件不会记录被拒绝的 远程命令。
- Zabbix agent
Allow/deny 规则顺序
您可以指定无限数量的 AllowKey、DenyKey、AllowKeyRegexp 或 DenyKeyRegexp 规则,但它们的顺序很重要。
- 规则会从上到下逐一评估。
- 当某个监控项键匹配某条规则时,该监控项要么被允许,要么被拒绝,并且规则评估会停止。
例如,在评估 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 忽略该规则,因为评估已停止。
示例
以下示例展示了 AllowKey、DenyKey、AllowKeyRegexp 和 DenyKeyRegexp 的常见配置模式。
允许特定检查和命令
仅允许两个 vfs.file 监控项检查和两个 system.run 命令:
使用 AllowKey 和 DenyKey:
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.*[*]
使用 AllowKeyRegexp 和 DenyKeyRegexp:
AllowKeyRegexp=^vfs\.file\.(contents|size)\[/tmp/app\.log\]$
AllowKeyRegexp=^system\.run\[/usr/bin/(uptime|df -h /)\]$
DenyKeyRegexp=^vfs\.file\..*\[.*\]$
无需设置 DenyKey=system.run[*] 或 DenyKeyRegexp=^system\.run\[.*\]$,因为默认情况下所有其他 system.run 命令都会被拒绝。
允许脚本
允许 Zabbix agent 通过所有可用方法在主机上执行脚本:
- 全局脚本,可在前端或通过 API 执行(此方法始终使用
system.run[myscript.sh]键) - 来自 action 操作 的远程命令(此方法始终使用
system.run[myscript.sh,nowait]键) - 带有脚本的
system.runZabbix agent 监控项,例如:system.run[myscript.sh]system.run[myscript.sh,wait]system.run[myscript.sh,nowait]
AllowKey=system.run[myscript.sh,*]
或者
AllowKeyRegexp=^system\.run\[myscript\.sh,.*\]$
要控制 wait/nowait 参数,必须设置不同的规则。
例如,您可以只允许 system.run[myscript.sh,wait] 监控项,从而排除其他方法:
AllowKey=system.run[myscript.sh,wait]
或者
AllowKeyRegexp=^system\.run\[myscript\.sh,wait\]$
保护 allow/deny 规则
这些示例展示了如何保护过于宽松的 AllowKey/AllowKeyRegexp 或 DenyKey/DenyKeyRegexp 规则。
请考虑以下规则:
使用 AllowKey 和 DenyKey:
AllowKey=system.run["C:\Program^ Files\Zabbix^ Agent^ 2\scripts\test.bat*"]
DenyKey=vfs.file.*
DenyKey=system.cpu.load[*]
使用 AllowKeyRegexp 和 DenyKeyRegexp:
AllowKeyRegexp=^system\.run\["C:\\Program\sFiles\\Zabbix\sAgent\s2\\scripts\\test\.bat.*"\]$
DenyKeyRegexp=^vfs\.file\..*$
DenyKeyRegexp=^system\.cpu\.load\[.*\]$
在 Windows 上,必须在路径中的空格前使用脱字符 (^) 为 AllowKey 或 DenyKey 转义,或者使用 \s 为 AllowKeyRegexp 或 DenyKeyRegexp 转义。
这些规则包含通配符 (*)(或基于正则表达式的模式中的 .*),可能会被滥用:
test.bat脚本可以使用任意参数执行,包括非预期参数。vfs.file.*(^vfs\.file\..*$)模式会匹配带参数和不带参数的监控项键;但是,所有vfs.file监控项都需要参数。system.cpu.load[*](^system\.cpu\.load\[.*\]$)模式只会匹配带参数的监控项键;但是system.cpu.load监控项不需要参数。
要保护这些规则,应明确只允许使用特定参数执行 test.bat,并拒绝正确的监控项键模式;例如:
使用 AllowKey 和 DenyKey:
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[*]
使用 AllowKeyRegexp 和 DenyKeyRegexp:
AllowKeyRegexp=^system\.run\["C:\\Program\sFiles\\Zabbix\sAgent\s2\\scripts\\test\.bat (status|version)"\]$
DenyKeyRegexp=^vfs\.file\..+\[.*\]$
DenyKeyRegexp=^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]都是无效模式)。 - 这些示例仅用于说明模式匹配;实际监控项需要参数。
对于 AllowKey 和 DenyKey:
| Pattern | Matches | Examples |
|---|---|---|
* |
任何带参数或不带参数的键 | |
vfs.file.* |
键以 vfs.file. 开头带参数或不带参数 |
Matches:vfs.file.sizevfs.file.contentsvfs.file.contents[]vfs.file.size[/var/log/app.log] |
vfs.*.contents |
键以 vfs. 开头键以 .contents 结尾无参数 |
Matches: vfs..contentsvfs.mount.point.file.contentsDoes not match: vfs.contentsvfs.file.contents[] |
vfs.file.*[*] |
键以 vfs.file. 开头任意参数或空参数 |
Matchesvfs.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.contentsDoes 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]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] |
对于 AllowKeyRegexp 和 DenyKeyRegexp:
| Pattern | Matches | Examples |
|---|---|---|
^.*$ |
任何带参数或不带参数的键 | |
^vfs\.file\..*$ |
键以 vfs.file. 开头带参数或不带参数 |
Matches:vfs.file.sizevfs.file.contentsvfs.file.contents[]vfs.file.size[/var/log/app.log] |
^vfs\..*\.contents$ |
键以 vfs. 开头键以 .contents 结尾无参数 |
Matches: vfs..contentsvfs.mount.point.file.contentsDoes not match: vfs.contentsvfs.file.contents[] |
^vfs\.file\..*\[.*\]$ |
键以 vfs.file. 开头任意参数或空参数 |
Matchesvfs.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.contentsDoes 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]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|windows-1252)\]$ |
键为 vfs.file.contents第一个参数为 /etc/passwd第二个参数为 utf8 或 windows-1252 |
Matches:vfs.file.contents[/etc/passwd,utf8]vfs.file.contents[/etc/passwd,windows-1252]Does not match: vfs.file.contents[/etc/passwd,]vfs.file.contents[/etc/passwd,utf16] |