linux 设置超级用户程序

警告
本文最后更新于 2022-09-13,文中内容可能已过时。

通过在 Linux 系统配置一个超级用户程序,获取 root 权限

c程序

一定要使用普通用户

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// super.c
// --------
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main(void)
{
    setuid(0);
    clearenv();
    system("export PATH=/sbin:/bin:/usr/bin:/usr/local/bin:$PATH; export TERM=linux; /bin/bash");
}

编译

一定要使用普通用户

1
gcc super.c -o super

这里可以试一下执行程序,发现依然是当前用户

1
2
3
4
5
6
7
[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 权限修改可执行程序:

1
2
3
sudo chown root super
sudo chmod ug+s super
sudo chmod a+x super

如此一来,我们便可以通过执行 super 自动获取 root 权限了

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
[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]#

添加用户到 sudoers

1
2
3
4
5
sudo vim /etc/sudoers

## 多个命令用逗号分割
## NOPASSWD 表示不用输入密码
ops ALL=(ALL) NOPASSWD:/usr/bin/bash

相关内容

william 支付宝支付宝
william 微信微信
0%