C 来自';设置用户根目录';程序
我希望从“设置用户根”程序启动根命令, 因此,我编写了以下C示例程序:C 来自';设置用户根目录';程序,c,suid,C,Suid,我希望从“设置用户根”程序启动根命令, 因此,我编写了以下C示例程序: #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <unistd.h> void main(int argc, char *argv[]) { if(argc > 2) { setuid(0); printf("setuid(0) exec
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
void main(int argc, char *argv[])
{
if(argc > 2) {
setuid(0);
printf("setuid(0) executed\n");
} else
printf("setuid(0) NOT executed\n");
system(argv[1]);
}
在Slackware 14(32位)上,其行为不同:
$ gcc foo.c -o foo
$ su
Password: *****
bash-4.2# chown root:root ./foo
bash-4.2# chmod 4755 ./foo
bash-4.2# ls -l foo
-rwsr-xr-x 1 root root 6292 dic 11 17:53 foo
bash-4.2# exit
exit
$ foo /bin/sh
setuid(0) NOT executed
sh-4.2$ exit <<<<< USER SHELL
exit
$ foo /bin/sh 12345
setuid(0) executed
sh-4.2# exit <<<<< ROOT SHELL
exit
$gcc foo.c-o foo
$su
密码:****
bash-4.2#chown root:root./foo
bash-4.2#chmod 4755./foo
bash-4.2#ls-l foo
-rwsr-xr-x 1根目录6292 dic 11 17:53 foo
bash-4.2#退出
出口
$foo/bin/sh
setuid(0)未执行
sh-4.2$exit为了解释稍微奇怪的setuid行为,您需要了解/bin/sh
实际上可能是bash
,而bash
的默认行为是删除euid,除非使用-p
调用它
这意味着,如果使用-p调用bash,则应该会看到类似于“root”的shell:-
natsu ~> id -a
uid=1000(me) gid=1000(me) groups=1000(me),4(adm),15(kmem)
natsu ~> ./foo "/bin/bash -p"
setuid(0) NOT executed
bash-4.2# id -a
uid=1000(me) gid=1000(me) euid=0(root) egid=0(root) groups=0(root),4(adm),15(kmem),1000(me)
而不使用-p
选项调用会产生观察到的行为:
pshanahan@natsu ~> ./foo /bin/bash
setuid(0) NOT executed
bash-4.2$ id -a
uid=1000(me) gid=1000(me) groups=1000(me),4(adm),15(kmem)
但实际上,您只有有效的用户id 0,而不是真正的用户id 0
正在使GUI在这种情况下运行。。。那完全是另一回事;但这应该有助于您理解这种情况下的行为
natsu ~> id -a
uid=1000(me) gid=1000(me) groups=1000(me),4(adm),15(kmem)
natsu ~> ./foo "/bin/bash -p"
setuid(0) NOT executed
bash-4.2# id -a
uid=1000(me) gid=1000(me) euid=0(root) egid=0(root) groups=0(root),4(adm),15(kmem),1000(me)
pshanahan@natsu ~> ./foo /bin/bash
setuid(0) NOT executed
bash-4.2$ id -a
uid=1000(me) gid=1000(me) groups=1000(me),4(adm),15(kmem)