一,真实用户id和有效用户id的区别:
1, 真实用户 ID (RUID) 是启动进程的用户的 ID
2,有效用户 ID (EUID) 是当前执行进程的用户 ID
即:进程启动时的用户id是真实用户 ID (RUID),
但实际执行时为了控制权限会切换为:有效用户 ID
二,代码:切换用户和组的有效id
<?php
//查询得到目标用户的信息
$user = 'www-data';
$rowu = posix_getpwnam($user);
$dest_uid = $rowu['uid'];
$dest_gid = $rowu['gid'];
echo $user."的uid:".$dest_uid.",gid:".$dest_gid.":\n";
// 获取当前执行用户的用户ID
$uid = posix_geteuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程有效用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getegid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程有效组:".$ginfo['name']."的gid:".$gid.":\n";
$uid = posix_getuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程真实用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getgid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程真实组:".$ginfo['name']."的gid:".$gid.":\n";
//注意:要先切换组再切换用户
echo "开始切换\n";
//切换组egid
if (!posix_setegid($dest_gid)) {
die('无法切换组');
}
// 切换用户euid
if (!posix_seteuid($dest_uid)) {
die('无法切换用户');
}
echo "切换完成\n";
//检查切换完成后是否成功:
$uid = posix_geteuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程有效用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getegid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程有效组:".$ginfo['name']."的gid:".$gid.":\n";
$uid = posix_getuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程真实用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getgid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程真实组:".$ginfo['name']."的gid:".$gid.":\n";
?>
运行结果:
root@lhdpc:~# /usr/local/soft/php8.3.9/bin/php /home/liuhongdi/work/whoami.php
www-data的uid:33,gid:33:
当前进程有效用户:root的uid:0:
当前进程有效组:root的gid:0:
当前进程真实用户:root的uid:0:
当前进程真实组:root的gid:0:
开始切换
切换完成
当前进程有效用户:www-data的uid:33:
当前进程有效组:www-data的gid:33:
当前进程真实用户:root的uid:0:
当前进程真实组:root的gid:0:
说明:可以看到: posix_setegid/posix_seteuid 只改变有效用户和有效组,
而真实用户和真实组不受影响
三,代码:切换用户和组的真实id
代码:
<?php
//查询得到目标用户的信息
$user = 'www-data';
$rowu = posix_getpwnam($user);
$dest_uid = $rowu['uid'];
$dest_gid = $rowu['gid'];
echo $user."的uid:".$dest_uid.",gid:".$dest_gid.":\n";
// 获取当前执行用户的用户ID
$uid = posix_geteuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程有效用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getegid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程有效组:".$ginfo['name']."的gid:".$gid.":\n";
$uid = posix_getuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程真实用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getgid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程真实组:".$ginfo['name']."的gid:".$gid.":\n";
//注意:要先切换组再切换用户
echo "开始切换\n";
//切换组gid
if (!posix_setgid($dest_gid)) {
die('无法切换组');
}
// 切换用户uid
if (!posix_setuid($dest_uid)) {
die('无法切换用户');
}
echo "切换完成\n";
//检查切换完成后是否成功:
$uid = posix_geteuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程有效用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getegid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程有效组:".$ginfo['name']."的gid:".$gid.":\n";
$uid = posix_getuid();
// 获取当前执行用户的信息
$user_info = posix_getpwuid($uid);
$cur_uid = $user_info['uid'];
echo "当前进程真实用户:".$user_info['name']."的uid:".$cur_uid.":\n";
$gid = posix_getgid();
// 获取组信息
$ginfo = posix_getgrgid($gid);
echo "当前进程真实组:".$ginfo['name']."的gid:".$gid.":\n";
?>
运行结果:
root@lhdpc:~# /usr/local/soft/php8.3.9/bin/php /home/liuhongdi/work/whoami.php
www-data的uid:33,gid:33:
当前进程有效用户:root的uid:0:
当前进程有效组:root的gid:0:
当前进程真实用户:root的uid:0:
当前进程真实组:root的gid:0:
开始切换
切换完成
当前进程有效用户:www-data的uid:33:
当前进程有效组:www-data的gid:33:
当前进程真实用户:www-data的uid:33:
当前进程真实组:www-data的gid:33:
说明:可以看到: posix_setgid/posix_setuid 会改变真实用户和真实组,同时也会改变有效用户和有效组
标签:uid,33,用户,指定,gid,切换,进程,php,root From: https://www.cnblogs.com/architectforest/p/18344944