【发布时间】:2020-09-11 00:52:27
【问题描述】:
我有以下 SUID 的 c 程序。
int execute(char *command, char *envp[])
{
pid_t pid;
int status=0;
char *argv[] = {"/bin/bash", "-p", "-c", command, NULL};
if ((pid = fork()) < 0) { /* fork a child process */
printf("*** ERROR: forking child process failed\n");
exit(1);
}
else if (pid == 0) { /* for the child process: */
if (execvpe(*argv, argv, envp) < 0) { /* execute the command */
printf("*** ERROR: exec failed\n");
exit(1);
}
}
else { /* for the parent: */
while (wait(&status) != pid) /* wait for completion */
;
}
return status;
}
int main(int argc, char * argv[], char *envp[])
{
char command[256];
char name[64];
char menu[10];
char notes[128];
char filename[128] = PREFIX;
strcat(filename,"mynotes.txt");
printf("What's your name? ");
fgets(name, 64, stdin);
strtok(name,"\n");
printf("Welcome %s!\n", name);
do {
printf("\nWhat would you like to do? \n");
printf("[1] Check the weather\n");
printf("[2] Write a note\n");
printf("[3] Read my notes\n");
printf("[4] Get the flag\n");
printf("[5] Quit\n");
printf("Enter your choise (1-5): ");
fgets(menu,10,stdin);
switch(menu[0]) {
case '1':
strcpy(command, "/usr/bin/curl wttr.in/?format=4");
execute(command,envp);
break;
case '2':
printf("Please type in your notes (128 characters max.):\n");
fgets(notes, 128, stdin);
sprintf(command, "/bin/echo '%s' >> %s", notes, filename);
execute(command,envp);
break;
case '3':
sprintf(command,"/bin/cat %s", filename);
execute(command,envp);
break;
case '4':
printf("I'm sorry %s, I'm afraid I can't do that.\n", name);
break;
}
}
while(menu[0] != '5');
return 0;
}
如何通过 shell 命令注入来利用它来显示同一用户拥有的另一个文件的内容。 例如,我尝试过 ./shellwrapper ;cat flag.txt - 但权限被拒绝,因为 ./shellwrapper 已完成并且 suid 不再有效。菜单选择是否存在漏洞?
【问题讨论】:
-
您需要将此示例改进为可验证的最小示例 - 您的问题与您的笔记系统、天气或任何菜单系统无关。向我们展示代码,该代码准确且仅关注您尝试对可以在此处显示的文件执行的操作。
-
同一行上的其他命令不会作为新 shell 的输入。您原来的 shell 会解析命令行,并分别执行它们。
-
另外,您从 shellwrapper 运行的 shell 从
-c选项获取命令,而不是从标准输入。 -
@Barmar 是的,我可以看到它们被单独执行,因为 cat flag.txt 被拒绝了权限。我想知道如何在 ./shellwrapper 中找到 flag.txt 文件,因为那是 SUID 程序