我偶然发现了一个使用suid可执行文件访问系统文件的问题。
我写了一个简短的POC:
#include <unistd.h>
#include <stdio.h>
int main()
{
if (access("/etc/shadow", W_OK) == 0)
printf("shadow writable!\n");
else
printf("shadow not writable\n");
return 0;
}然后,我用chown root:root和chmod u+s编译并给出了suid (由root运行)
这是生成的可执行文件。
-rwsrwxr-x 1 root root 4847 Apr 14 08:40 a.out目标文件具有以下权限
-rw------- 1 root root 1836 Oct 8 2014 /etc/shadow当我运行这个程序时,它给出了这个输出:
[d.berra@srvr ~]$ ./a.out
shadow not writable这一切为什么要发生?我是说..。我正在访问这个文件,因为root和root 可以在这个文件上写入!
注意:selinux已禁用
想法?
发布于 2015-04-14 06:58:23
来自access
检查是使用调用进程的实际UID和GID完成的,而不是在实际尝试文件上的操作(例如打开(2))时执行的有效UID。这允许设置用户ID程序轻松地确定调用用户的权限.
所以您可以成功地打开这个文件进行写入,因为您的有效UID和文件系统UID现在是0,但是access仍然会返回错误。
正如@nos所指出的,您忘记将可执行所有者更改为root
$ sudo chown root:root ./a.out但是,即使这样做,由于access的行为,您仍然会得到“不可写”:
$ ls -l ./a.out
-rwsr-xr-x 1 root root 12651 Apr 14 09:53 ./a.out
$ ./a.out
shadow not writable发布于 2015-04-14 06:58:29
设置suid位,这意味着可执行文件将作为拥有可执行文件的用户运行。可执行文件将以d.berra的形式运行,不能写入/etc/shadow。
如果您想让可执行文件作为root运行,而不管运行它的用户是谁,那么除了设置suid位之外,可执行文件还应该由root拥有。
发布于 2015-04-14 07:59:41
试试这个:
#include <unistd.h>
#include <stdio.h>
int main()
{
setuid(0);
if (access("/etc/shadow", W_OK) == 0)
printf("shadow writable!\n");
else
printf("shadow not writable\n");
return 0;
}它将用户ID设置为root,以便能够访问该文件。setuid( 0 )只在有效用户ID也为0的情况下工作。
https://stackoverflow.com/questions/29620903
复制相似问题