警告
      
      
        本文最后更新于 2020-10-25,文中内容可能已过时。
       
     
  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; /bin/bash");
}
  | 
 
  编译
一定要使用普通用户
这里可以试一下执行程序,发现依然是当前用户
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
  | 
sudo chown root super
sudo chmod ug+s 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]#
  | 
 
  参考链接