Linux 删除 root 权限 - C 脚本
Linux dropping root permissions - C script
我在 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 语句,但仍然允许 "Malicious Data"待写。
我也曾尝试在删除权限之前关闭文件 close(fd)
,因为我不确定在以 root 身份打开的文件仍处于打开状态时您是否无法编辑权限。但我仍然遇到同样的问题
关于我在这里做错了什么有什么想法吗?为什么它不起作用?
我假设你 运行 程序 sudo
。在这种情况下,getuid
将 return 0。
您必须显式调用将 uid 设置为所需的(例如 1000)uid。
此外,"Malicious Data\n"
将被写入,因为当进程提升权限时 fd
已经打开,即使您的进程失去权限,您仍然可以在那里写入。该进程现在无法再次打开该文件。
一切都符合规范:如果您想禁止进程写入文件,请确保在删除权限之前将其关闭。
我在 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 语句,但仍然允许 "Malicious Data"待写。
我也曾尝试在删除权限之前关闭文件 close(fd)
,因为我不确定在以 root 身份打开的文件仍处于打开状态时您是否无法编辑权限。但我仍然遇到同样的问题
关于我在这里做错了什么有什么想法吗?为什么它不起作用?
我假设你 运行 程序 sudo
。在这种情况下,getuid
将 return 0。
您必须显式调用将 uid 设置为所需的(例如 1000)uid。
此外,"Malicious Data\n"
将被写入,因为当进程提升权限时 fd
已经打开,即使您的进程失去权限,您仍然可以在那里写入。该进程现在无法再次打开该文件。
一切都符合规范:如果您想禁止进程写入文件,请确保在删除权限之前将其关闭。