Shell 脚本和安全性

发布于 2024-12-28 11:21:06 字数 163 浏览 0 评论 0原文

TLDP 的高级 Bash 脚本指南指出,shell 脚本不应用于“安全性很重要的情况”需要保证系统的完整性并防止入侵、破解和破坏。”

是什么让 shell 脚本不适合这样的用例?

TLDP's Advanced Bash Scripting Guide states that shell scripts shouldn't be used for "situations where security is important, where you need to guarantee the integrity of your system and protect against intrusion, cracking, and vandalism."

What makes shell scripts unsuitable for such a use case?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幻梦 2025-01-04 11:21:06

由于 shell 的可塑性,很难验证 shell 脚本是否执行了其预期功能,并且在面对对抗性输入时执行该功能。 shell 的行为方式取决于环境以及其自身的众多配置变量的设置。每个命令行都经过多个级别的扩展、评估和插值。某些 shell 结构在子进程中运行,而该结构包含的变量在父进程中扩展。在设计可能受到攻击的系统时,所有这些都违背了 KISS 原则。

Because of the malleability of the shell, it is difficult to verify that a shell script performs its intended function and only that function in the face of adversarial input. The way the shell behaves depends on the environment, plus the settings of its own numerous configuration variables. Each command line is subject to multiple levels of expansion, evaluation and interpolation. Some shell constructs run in subprocesses while the variables the construct contains are expanded in the parent process. All of this is counter to the KISS principle when designing systems that might be attacked.

素手挽清风 2025-01-04 11:21:06

可能是因为很容易搞砸。当 PATH 设置不正确时,您的脚本将开始执行错误的命令。在字符串中的某个位置放置空格可能会导致它稍后变成两个字符串。这些可能会导致可利用的安全漏洞。简而言之:shell 为您的脚本行为提供了一些保证,但对于真正安全的编程来说,它们太弱或太复杂。

(对此,我想补充一点,安全编程本身就是一门艺术,任何语言都可能搞砸。)

Probably because it's just easy to screw up. When the PATH is not set correctly, your script will start executing the wrong commands. Putting a space somewhere in a string might cause it to become two strings later on. These can lead to exploitable security holes. In short: shells give you some guarantees as to how your script will behave, but they're too weak or too complex for truly secure programming.

(To this I would like to add that secure programming is an art in itself, and screwing up is possible in any language.)

寂寞笑我太脆弱 2025-01-04 11:21:06

我不同意这种说法,因为脚本本身并不安全。如果遵循一些简单的准则,Bash 脚本是完全安全的:

  • 脚本是否包含其他人不应查看的信息?
    如果是这样,请确保只有所有者才能读取它。
  • 该脚本是否依赖于来自某个地方的输入数据?如果是这样,请确保输入数据
    不能以任何方式被污染,或者可以检测到被污染的数据
    并被丢弃。
  • 如果其他人尝试运行该系统是否重要?
    脚本?如果是这样,与第一点一样,确保没有人可以执行它,并且最好不要从中读取。对于执行系统功能的脚本来说,chmod 0700 通常是个好主意。
  • 您希望脚本具有 setuid(通过其解释器)的情况是
    极其罕见

将脚本与已编译程序分开的两点是源代码是可见的,并且解释器会执行它。只要解释器​​没有受到损害(例如有 setuid 位),就没有问题。

当编写脚本来执行系统任务时,拼写错误、搞砸以及编写脚本时的一般人为错误确实在某种程度上代表了潜在的安全故障,但编译后的程序也会出现这种情况(很多人倾向于忽略这样一个事实:编译的程序也可以反汇编)

值得注意的是,在大多数(如果不是全部)Linux 风格中,大多数(如果不是全部,事实上,想不出任何不是)服务都是通过 shellscript 启动的。

I would disagree with that statement, as there is nothing about scripts that make them inherently unsafe. Bash scripting are perfectly safe if some simple guidelines are followed:

  • Does the script contain info that others shouldn't be able to view?
    If so, make sure it's only readable by the owner.
  • Does the script depend on input data from somewherE? If so, ensure that input data
    can not be tainted in any way, or that tainted data can be detected
    and discarded.
  • Does it matter if others were to try and run the
    script? If so, as with the first point, ensure that nobody can execute it, and preferably not read from it. chmod 0700 is generally a good idea for scripts that perform system functions.
  • And the cases where you'd want a script to have a setuid (via its interpreter) are
    extremely rare

The two points that separate a script from a compiled program would be that the source is visible, and that an interpreter executes it. As long as the interpreter hasn't been compromised (such as having a setuid bit on it), you'd be fine.

When writing scripts to do system tasks, typos and screwups and general human error when writing it do to some extent represent a potential security failure, but that would also be the case with compiled programs (and a lot of people tend to ignore the fact that compiled programs can also be disassembled)

It is worth noting that in most (if not all) linux flavors, most (if not all, in fact, can't think of any that aren't) services are started via a shellscript.

赤濁 2025-01-04 11:21:06
  • 坏孩子更容易让 shell 脚本以不同的方式工作(它与其他进程、路径、shell 函数、prifile 进行大量交互),
  • 而好孩子则更难处理敏感数据(传递密码等)
  • it's easier for bad boys to make shell script work differently (it interacts a lot with other processes, PATH, shell functions, prifile)
  • it's harder for good boys to deal with sensitive data (passing passwords, etc)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文