【问题标题】:Linux dropping root permissions - C scriptLinux 删除 root 权限 - C 脚本
【发布时间】:2018-03-14 02:07:40
【问题描述】:

我在 linux (Ubuntu 12.0.4) 中运行以下 C 脚本作为设置根 UID 脚本 (chmod 4755)

#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

void main()
{ int fd;
    /* Assume that /etc/zzz is an important system file,
    * and it is owned by root with permission 0644.
    * Before running this program, you should create
    * the file /etc/zzz first. */
    fd = open("/etc/zzz", O_RDWR | O_APPEND);
    if (fd == -1) {
        printf("Cannot open /etc/zzz\n");
        exit(0);
    }

    /* Simulate the tasks conducted by the program */
    sleep(1);
    /* After the task, the root privileges are no longer        needed, it’s time to relinquish the root privileges
    permanently. */

    setgroups(0, NULL);
    setregid(getgid());
    setreuid(getuid()); /* getuid() returns the real uid */

    if(setregid(getgid()) == 0){
        printf("Still root GID!\n");
        exit(0);
    } if(setreuid(getuid()) ==0){
        printf("Still root UID\n");
        exit(0);

    if (fork()) { /* In the parent process */
        close (fd);
        exit(0);
    } else { /* in the child process */
    /* Now, assume that the child process is compromised,
    malicious attackers have injected the following
    statements into this process */

    write (fd, "Malicious Data\n", 15);
    close (fd);
    }
}

据我所见,它应该将权限设置回真实用户(ID 1000),但我收到“Still root”错误。

我已经尝试插入 setuid(1000)setuid(0) 来删除任何已保存的 UID 问题,但这只是允许它绕过 if 语句,但仍然允许写入“恶意数据”。

我还尝试在删除权限之前关闭文件close(fd),因为我不确定在以 root 身份打开的文件仍处于打开状态时您是否无法编辑权限。但我仍然遇到同样的问题

关于我在这里做错了什么有什么想法吗?为什么它不起作用?

【问题讨论】:

    标签: c linux permissions user-permissions


    【解决方案1】:

    我假设您使用sudo 运行程序。在这种情况下,getuid 将返回 0。 您必须明确调用将 uid 设置为所需的(例如 1000)uid。

    另外,"Malicious Data\n" 将被写入,因为当进程具有提升的权限时,fd 已经打开,即使您的进程失去权限,您仍然可以在那里写入。该进程现在无法再次打开文件。

    一切都符合规范:如果您想禁止进程写入文件,请确保在删除权限之前将其关闭。

    【讨论】:

      猜你喜欢
      • 2011-03-22
      • 1970-01-01
      • 2011-09-01
      • 2011-02-11
      • 2015-11-09
      • 1970-01-01
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      相关资源
      最近更新 更多