int main(int argc, char *argv[])
{
while(1)sleep(1);
}
$>g++ main.cpp -o a.out
$>ll
-rwxr-xr-x. 1 test1 test 6780 Sep 16 15:32 a.out
文件所有者是test1,我们用test1用户执行a.out程序
$>su test1
$>./a.out &
$>ps -ax -o ruid -o euid -o suid -o fuid -o pid -o fname | grep a.out
502 502 502 502 3192 a.out
(看到结果是4个uid全是test1;)
现在我们用test2用户执行test1的程序看看结果
$su test2
503 503 503 503 3234 a.out
再用root用户执行
0 0 0 0 3257 a.out
int main(int argc, char *argv[])
{
if( setuid(503) < 0) perror ("setuid error");
while(1)sleep(1);
}
$>ll
-rwxr-xr-x. 1 test1 test 6780 Sep 16 15:32 a.out
使用root用户执行
$>./a.out
查看状态,所有uid都变成test2用户。
503 503 503 503 3592 a.out
把代码中setuid改成seteuid函数,会把euid和fuid改成test2用户
0 503 0 503 3614 a.out
把代码中setuid改成setfsuid函数,会把fuid改成test2用户
0 0 0 503 3636 a.out
当把代码改成下面样子
if( seteuid(503) < 0) perror ("seteuid error");
if( setfsuid(504) < 0) perror ("setfsuid error");
while(1)sleep(1);
或者
if( setfsuid(504) < 0) perror ("setfsuid error");
if( setfeuid(503) < 0) perror ("seteuid error");
while(1)sleep(1);
用root用户执行,得到都是一样的结果
0 503 0 503 3614 a.out
到了这里我来总结一下:1、setuid和seteuid是有区别的,setuid是永久的放弃root用户权限,转让给非root用户后,
无法再restore到root用户,seteuid是临时放弃root用户权限,可以通过seteuid(0),restore到root权限。
这点应该是总所周知的特点,本文就不举例子演示。
2、seteuid 会同时改变euid和fuid都为设置的euid值。
3、root用户可以通过调用setxxuid 来改变权限用户。非root用户是无法改变和转让权限用户。
int main(int argc, char *argv[])
{
while(1)sleep(1);
}
$>g++ main.cpp
$>ll
-rwxr-xr-x. 1 test1 test 6780 Sep 16 18:18 a.out
$>chmod u+s a.out
$>ll
-rwsr-xr-x. 1 test1 test 6780 Sep 16 18:18 a.out
使用root用户执行,查看用户ID为
0 502 502 502 4133 a.out
int main(int argc, char *argv[])
{
if( setfsuid(503) < 0) perror ("setfsuid error");
FILE * fp = fopen("test.log", "a+");
if(fp == NULL)
{
perror ("fopen error");
}
else
{
fclose(fp);
}
while(1)sleep(1);
}
使用s权限位,文件所有者为root,执行者为test1,改变fuid为test2,这样就构造出3个uid各部相同,方便观察效果
$>ll
-rws---r-x. 1 root root 7397 Sep 16 18:53 a.out
运行查看状态,ruid为test1,euid为root,fuid为test2
502 0 0 503 4240 a.out
$>ll
-rws---r-x. 1 root root 7397 Sep 16 18:53 a.out
-rw-rw-r--. 1 test2 test 0 Sep 16 18:54 test.log
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有