Linux 设置超级用户程序
[NOTE] Updated October 25, 2020. This article may have outdated content or subject matter.
参考链接:
- Calling a script from a setuid root C program - script does not run as root
- Can I make a script always execute as root?
c程序
一定要使用普通用户
// super.c
// --------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int main(void)
{
setuid(0);
clearenv();
system("/bin/bash");
}
编译
一定要使用普通用户
gcc super.c -o super
这里可以试一下执行程序,发现依然是当前用户
[trader@localhost ~]$ ll
total 16
-rwxrwxr-x 1 trader trader 8216 Oct 25 20:36 super
-rw-r--r-- 1 root trader 170 Oct 25 20:34 super.c
[trader@localhost ~]$ ./super
[trader@localhost trader]$ whoami
trader
权限
下面,我们来设置这个可执行程序的权限,使得其可以在普通用户的环境中,也是可以使用默认的 root
权限运行的。
使用 root
权限修改可执行程序:
sudo chown root super
sudo chmod ug+s super
如此一来,我们便可以通过执行 super
自动获取 root
权限了
[trader@localhost ~]$ ll
total 16
-rwsrwsr-x 1 root trader 8216 Oct 25 20:36 super
-rw-r--r-- 1 root trader 170 Oct 25 20:34 super.c
[trader@localhost ~]$ ./super
[root@localhost trader]# whoami
root
[root@localhost trader]# mkdir -p /usr/test
[root@localhost trader]# rm -rf /usr/test
[root@localhost trader]#
<<<<<<< HEAD
======= >>>>>>> 482ed4b293fc90ed22cc819c79dcaf1d0116262f
Author William
LastMod 2020-10-25