我试图弄清楚如何在 sudoer 中创建一个条目,在那里我允许一组有限的参数是一些可选的,但命令仍然非常严格。
有什么简单的方法可以限制这些限制吗?
我希望用户能够使用 -w 标志和可选值运行,但仍然受到限制。我不想为 -w 选项硬编码值。用户应该能够运行这些命令中的任何一个,其中 10 是任意数字。
/usr/bin/iptables -nvL *
/usr/bin/iptables -w -nvL *
/usr/bin/iptables -w 10 -nvL *
Run Code Online (Sandbox Code Playgroud)
我想出了这4个条目。有没有更好的方法来定义可选值?
username ALL=(root) NOPASSWD: /usr/bin/iptables -nvL *
username ALL=(root) NOPASSWD: /usr/bin/iptables -w -nvL *
username ALL=(root) NOPASSWD: /usr/bin/iptables -w [[\:digit\:]] -nvL *
username ALL=(root) NOPASSWD: /usr/bin/iptables -w [[\:digit\:]][[\:digit\:]] -nvL *
Run Code Online (Sandbox Code Playgroud)
小智 11
我担心不会:/
如果您不想使用通配符,如 »?« 甚至 »*«,您必须完全准确地提供您想要的内容。
根据 sudoers 的手册页,它只提供一般通配符:
Wildcards
sudo allows shell-style wildcards (aka meta or glob characters) to be used in host names,
path names and command line arguments in the sudoers file. Wildcard matching is done via the
glob(3) and fnmatch(3) functions as specified by IEEE Std 1003.1 (“POSIX.1”).
* Matches any set of zero or more characters (including white space).
? Matches any single character (including white space).
[...] Matches any character in the specified range.
[!...] Matches any character not in the specified range.
\x For any character ‘x’, evaluates to ‘x’. This is used to escape special characters
such as: ‘*’, ‘?’, ‘[’, and ‘]’.
NOTE THAT THESE ARE NOT REGULAR EXPRESSIONS.
Unlike a regular expression there is no way to match one or more characters within a range.
Run Code Online (Sandbox Code Playgroud)
小智 9
正如其他人指出的那样, 不支持您想要的内容sudo
,它仅支持通用通配符。
在 ers 格式中使用通配符存在一些严重的危险sudo
。例如,如果您允许rm -Rf /some/path/*
,那么您就可以sudo rm -Rf /some/path/blarg /another/not/allowed/path
并且基本上允许他们访问rm
任何文件,只要他们也使用该路径即可。同样,您可以滥用相对路径,您仍然只能删除另一个路径,同时通过rm -Rf /some/path/../../../../some/path/that/should/be/restricted
.
您可以考虑使用“代理”脚本来进行输入验证。
我强烈建议阅读整个系列的帖子,但如果不是,至少阅读通配符上的帖子:https ://blog.compass-security.com/2012/10/dangerous-sudoers-entries-part-4-wildcards/