【问题标题】:Why do my setuid root bash shell scripts not work?为什么我的 setuid root bash shell 脚本不起作用?
【发布时间】:2021-04-20 17:48:52
【问题描述】:

我创建了这个简单的脚本,以允许用户在不给他“su”的情况下删除由 Web 服务器在其主目录中创建的文件。两个脚本都设置了"chmod 4750"

最疯狂的是,它们确实有效,而现在却无效。这是脚本:

#!/bin/bash

# Ask for directory to delete
echo "Enter the file or directory you would like to delete, the assumed path is    /home/user"

read DIRECTORY

rm -rf /home/user/"$DIRECTORY"

echo "Deleting /home/user/$DIRECTORY ..."

exit 0

2:

#!/bin/bash

# Reset permissions
echo "Resetting the ownership of the contents of /home/user to user."

chown -R user /home/user

exit 0

我会让它们更高级一点,并为多个用户工作,但现在我什至无法让简单版本工作。当然,它在以 root 身份运行时有效。它曾经在以用户“用户”身份运行时工作,但现在不行。我明白了:

user@dev:/home/user$ delete.sh
Enter the file or directory you would like to delete, the assumed path is /home/user/[your input]
test-dir
rm: cannot remove ‘/home/user/test-dir/test-file’: Permission denied
Deleting /home/user/test-dir ...

chown: changing ownership of ‘/home/user/test-dir’: Operation not permitted

可能是什么问题?

-rwsr-x--- 1 root user 291 Nov  6 05:23 delete.sh
-rwsr-x--- 1 root user 177 Nov  6 05:45 perms.sh

【问题讨论】:

  • 检查 SUID 是否工作 添加脚本:echo whoami 输出根目录? .
  • 作为一种安全措施,大多数系统不允许设置 shell 脚本。不过,不知道他们为什么过去工作。
  • 奇怪的是它今天早些时候在同一个系统上工作。我不知道我改变了什么或改变了什么。我会试试这个。
  • 你猜对了,但是我该如何解决它,为什么它之前会起作用?
  • 是什么阻止有人将../../etc/passwd 输入DIRECTORY 以重置所有密码(包括root)?

标签: bash permissions chmod chown setuid


【解决方案1】:

https://unix.stackexchange.com/questions/364/allow-setuid-on-shell-scripts 有一个相当全面的答案

底线是反对它的两个要点:

  1. 内核打开文件以查找应执行的解释器与解释器打开文件以读取脚本之间的竞争条件。
  2. 在没有适当检查的情况下执行许多外部程序的 Shell 脚本可能会被欺骗执行错误的程序(例如使用恶意 PATH),或者以损坏的方式扩展变量(例如在变量值中有空格),并且通常它具有较少控制它执行的外部程序处理输入的能力。

从历史上看,原始 Bourne shell 中有一个著名的错误(至少在 4.2BSD 上,我在其中看到了这个),它允许任何人通过创建一个名为 -i 到 suid 的符号链接来获得交互式 root shell外壳脚本。这可能是被禁止的最初触发因素。

编辑:回答“我该如何解决”- 配置 sudo 以允许用户以用户 root 的身份仅执行这些脚本,并且可能使用类似 https://stackoverflow.com/a/4598126/164137 的技巧来查找原始用户的名称和在他们自己的主目录上强制操作,而不是让他们传入任何任意输入(即在他们的当前状态下,您问题中包含的脚本中的任何内容都不会阻止user1 执行脚本并将它们传递给users2 的目录,或任何目录)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-14
    • 1970-01-01
    • 2013-10-20
    • 2020-07-04
    • 2023-01-18
    • 2018-09-01
    • 2016-11-03
    相关资源
    最近更新 更多