【发布时间】:2021-11-11 23:08:28
【问题描述】:
我正在尝试为 linux 上的普通用户创建一个执行程序,并设置了 SUID 位,因此作为参数传递给程序的任何命令都可以在 root 权限下执行。但是,当我尝试将其实现为 bash 脚本时,这不起作用,在 C 中实现时它可以工作。我想知道我对 shell 脚本做错了什么。代码如下
Shell 脚本:
#! /bin/bash
if [ $# -lt 1 ]; then
echo "Usage: $0 <Command String>"
exit 1
fi
$@
#Also tried this, same result
#exec $@
执行:
root#: chmod 755 exec.sh
root#: chmod u+s exec.sh
root#: ll exec.sh
-rwsr-xr-x 1 root root 75 Sep 19 16:55 exec.sh
regular_user$: ./exec.sh whoami
regular_user
C 程序:
#include <stdlib.h>
#include <stdio.h>
int main ( int argc, char *argv[] )
{
if ( argc < 2 ) {
printf( "Usage: %s <Command String>\n", argv[0] );
return 1;
}
else
{
argv[argc]=NULL;
//setuid(0); //Works without these
//setgid(0);
int exit=execvp(argv[1], argv+1);
return exit;
}
}
执行:
root#: gcc exec.c -o exec.obj
root#: chmod 755 exec.obj
root#: chmod u+s exec.obj
root#: ll exec.obj
-rwsr-xr-x 1 root root 6979 Sep 19 17:03 exec.obj
regular_user$: ./exec.obj whoami
root
两个文件具有相同的权限
-rwsr-xr-x 1 root root 75 Sep 19 16:55 exec.sh
-rwsr-xr-x 1 root root 6979 Sep 19 17:03 exec.obj
【问题讨论】:
-
sudo 有什么问题? ;)
-
方法不对,应该配置
sudo或者使用super -
实际上我们有很多带有 ldap auth 和 ldap sudo 组的 linux 服务器,但是这个特定的服务器运行一个 oracle 数据库,我们需要将其锁定,所以我需要一个简单的 hack 来让 DBA 运行有时以 root 身份执行命令。
-
您可以在该机器上拥有一个特殊的
/etc/sudoers,或者安装super并让您的DBA 使用它。 -
我决定将 sudo 与本地 sudoers 文件一起使用,我也得到了为什么它没有询问的答案。谢谢大家。