简体   繁体   English

用 C 语言设置 SUID 是不够的

[英]Setting up SUID in C language is not enough

For pedagogical purposes, I want to set up a basic command injection in C.出于教学目的,我想在 C 中设置基本命令注入。 I have the following code:我有以下代码:

#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv) {
    char cat[] = "cat ";
    char *command;
    size_t commandLength;

    commandLength = strlen(cat) + strlen(argv[1]) + 1;
    command = (char *) malloc(commandLength);

    strncpy(command, cat, commandLength);
    strncat(command, argv[1], (commandLength - strlen(cat)) );

    system(command);
    return (0);
}

I compile it, set the binary as owned by root and set the SUID to 1, as follows:我编译它,将二进制文件设置为root拥有并将SUID设置为1,如下所示:

gcc injectionos.c -o injectionos
sudo chown root:root injectionos
sudo chmod +s injectionos

I obtain the following result:我得到以下结果:

ls -la
total 40
drwxr-xr-x 2 olive olive  4096 Jan  6 13:17 .
drwxr-xr-x 3 olive olive  4096 Jan  6 12:15 ..
-rwsr-sr-x 1 root  root  16824 Jan  6 13:17 injectionos
-rw-r--r-- 1 olive olive   415 Jan  6 13:17 injectionos.c
-rwx------ 1 root  root      9 Jan  6 12:43 titi.txt
-rw-r--r-- 1 olive olive     9 Jan  6 12:16 toto.txt`

So, basically, with the SUID set to 1, i should be able to open both toto.txt and titi.txt files by performing the following injection:因此,基本上,将 SUID 设置为 1,我应该能够通过执行以下注入来打开 toto.txt 和 titi.txt 文件:

./injectionos "toto.txt;cat titi.txt"

But executing this command, I got a permission denied when accessing titi.txt .但是执行这个命令,我在访问titi.txt时得到了一个permission denied Finally, when I add a setuid(geteuid());最后,当我添加一个setuid(geteuid()); in my code, the injection is working and I can access to titi.txt file.在我的代码中,注入工作正常,我可以访问 titi.txt 文件。

Given that injectionos is ran as root and titi.txt belong to root, I supposed that it was enough, but apparently no.鉴于 injectionos 以 root 身份运行,而titi.txt 属于 root,我认为这就足够了,但显然不是。 What am I missing here?我在这里想念什么?

The privileges are being dropped by /bin/sh executed as part of the system() call.作为system()调用的一部分执行的/bin/sh正在删除特权。 See the man page for bash and the -p option请参阅 bash 和-p选项的手册页

If the shell is started with the effective user (group) id not equal to the real user (group) id, and the -p option is not supplied, no startup files are read, shell functions are not inherited from the environment, the SHELLOPTS, BASHOPTS, CDPATH, and GLOBIGNORE variables, if they appear in the environment, are ignored, and the effective user id is set to the real user id.如果 shell 以不等于实际用户(组) id 的有效用户(组) id 启动,并且未提供 -p 选项,则不读取启动文件,shell 函数未从环境继承,SHELLOPTS 、BASHOPTS、CDPATH 和 GLOBIGNORE 变量,如果它们出现在环境中,将被忽略,有效用户 id 设置为真实用户 id。 If the -p option is supplied at invocation, the startup behavior is the same, but the effective user id is not reset.如果在调用时提供了 -p 选项,则启动行为是相同的,但不会重置有效用户 ID。

Well, technically debian uses dash by default, but it does the same thing.好吧,技术上 debian 默认使用dash ,但它做同样的事情。

So the default behavior of the shell has been adjusted to mitigate this injection at least somewhat.因此,shell 的默认行为已进行了调整,以至少在一定程度上减轻这种注入。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM