Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/235.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux中shell脚本的输出重定向在PHP';行政长官()_Php_Linux_Bash_Apache_Centos - Fatal编程技术网

Linux中shell脚本的输出重定向在PHP';行政长官()

Linux中shell脚本的输出重定向在PHP';行政长官(),php,linux,bash,apache,centos,Php,Linux,Bash,Apache,Centos,有一个shell(bash)脚本,在CentOS上运行,SELinux设置为Permissive,它只有一个目的——将某些内容写入文件: [root@centos ~]$ cat /var/www/html/test.php <?php $output=shell_exec("/opt/sms/script.sh"); var_dump($output); ?> [root@centos ~]$ cat /opt/sms/script.sh #!/bin/bash whoami &

有一个shell(bash)脚本,在CentOS上运行,SELinux设置为Permissive,它只有一个目的——将某些内容写入文件:

[root@centos ~]$ cat /var/www/html/test.php
<?php
$output=shell_exec("/opt/sms/script.sh");
var_dump($output);
?>
[root@centos ~]$ cat /opt/sms/script.sh
#!/bin/bash

whoami > /tmp/a.txt

cat /tmp/a.txt
[root@centos ~]$ php -f /var/www/html/test.php
string(5) "root
"
[root@centos ~]$
在你这样做之前,这仍然是好的:

[root@centos ~]$ cat /tmp/a.txt
root
[root@centos ~]$
[root@centos ~]$ find / -name a.txt 2>/dev/null
/tmp/systemd-private-689e87297de1452e98dcfaa5bd686a1f-httpd.service-gMJKi0/tmp/a.txt
/tmp/a.txt
[root@centos ~]$ cat /tmp/systemd-private-689e87297de1452e98dcfaa5bd686a1f-httpd.service- 
gMJKi0/tmp/a.txt
apache
[root@centos ~]$ cat /tmp/a.txt
root
[root@centos ~]$
什么

然后你要这样做:

[root@centos ~]$ cat /tmp/a.txt
root
[root@centos ~]$
[root@centos ~]$ find / -name a.txt 2>/dev/null
/tmp/systemd-private-689e87297de1452e98dcfaa5bd686a1f-httpd.service-gMJKi0/tmp/a.txt
/tmp/a.txt
[root@centos ~]$ cat /tmp/systemd-private-689e87297de1452e98dcfaa5bd686a1f-httpd.service- 
gMJKi0/tmp/a.txt
apache
[root@centos ~]$ cat /tmp/a.txt
root
[root@centos ~]$
问题:为什么要将输出写入该
/tmpp/systemd-*/tmp.a.txt
文件而不是简单的
/tmp/a.txt
?我提供了绝对路径,这应该是服务于非常明显的目的。如何/在何处控制我的输出被写在其他地方?

这通常被称为“chroot监狱”,在允许您将进程的根目录设置为其他内容的系统调用(和shell命令)之后。这有效地将进程隔离到某个子目录中。所有绝对路径都将相对于此根目录进行解释

这是一种经典且众所周知的安全技术。如果您的某个PHP脚本可被利用,攻击者名义上只会在chroot监狱中处理文件,而将系统的其余部分隔离

systemd
可能使用Linux名称空间,而不是
chroot
本身,但想法是一样的)