【发布时间】:2021-06-06 17:17:18
【问题描述】:
我正在尝试编写一个以用户 smith 权限运行 /bin/bash 的程序,
smith:x:1000:1000:Basket:/home/smith:/bin/bash
我试过了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
setgid(1000);
setuid(1000);
char command[50];
strcpy( command, "/bin/bash" );
system(command);
return(0);
}
我使用这些命令来设置所有者、组和权限
chown smith command
chgrp smith command
chmod +x command
chmod u+s command
命令后的权限:
-rwsr-xr-x 1 smith smith 16840 Jun 6 17:11 command
但没有用,我尝试使用 root 作为下一个:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main () {
setgid(0);
setuid(0);
char command[50];
strcpy( command, "/bin/bash" );
system(command);
return(0);
}
我使用相同的命令来获取权限等等,但我没有使用 smith,而是编写了 root 并且我工作了,当我运行它时,我以 root 身份获得了一个 shell。
那么,我该如何使用 smith 用户呢?
【问题讨论】:
-
Setuid/setgid wrapper 围绕 shell 脚本的 C 程序和运行其他命令的
system()调用在不引入可利用的安全问题的情况下几乎是不可能的...... -
你需要设置你的程序的gid:
chmod g+s program,这样它就是一个setgid程序(我认为命令是正确的)。然后在程序中做setgid(getegid()); setuid(geteuid());