【发布时间】:2021-01-06 12:30:13
【问题描述】:
出于教学目的,我想在 C 中设置一个基本的命令注入。我有以下代码:
#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);
}
我编译它,将二进制文件设置为root拥有并将SUID设置为1,如下:
gcc injectionos.c -o injectionos
sudo chown root:root injectionos
sudo chmod +s injectionos
我得到以下结果:
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`
所以,基本上,将 SUID 设置为 1,我应该能够通过执行以下注入来打开 toto.txt 和 titi.txt 文件:
./injectionos "toto.txt;cat titi.txt"
但是执行这个命令,我在访问 titi.txt 时得到了一个permission denied。最后,当我在代码中添加setuid(geteuid()); 时,注入工作正常,我可以访问titi.txt 文件。
鉴于 injectionos 以 root 身份运行,并且titi.txt 属于 root,我认为这已经足够了,但显然不是。我在这里错过了什么?
【问题讨论】:
标签: c security debian code-injection