[英]Run bash command via PHP, change dir then execute binary
So here's what I'm trying to do. 所以这就是我想要做的。 I'm trying to start a binary under another user, via a PHP script.
我正在尝试通过PHP脚本在另一个用户下启动二进制文件。 Apache has sudo access.
Apache具有sudo访问权限。 The command works fine when ran via putty logged in as "test".
通过以“测试”身份登录的腻子运行时,该命令运行正常。
passthru('bash -c "sudo -u test cd /home/test/cs/ ; ./hlds_run"');
also might I add that 我也可以补充一点
passthru('bash -c "sudo -u test ./home/test/cs/hlds_run"');
Won't work because of how the binary is written (it won't find it's resources unless you cd to the folder before, tested on terminal) 由于二进制文件的写入方式而无法工作(除非您之前将其cd到该文件夹并在终端上进行了测试,否则它将找不到它的资源)
If everyone has access to /home/test/cs
: 如果每个人都可以访问
/home/test/cs
:
passthru('cd /home/test/cs && sudo -u test ./hlds_run');
If only the user test
has access to the directory: 如果只有用户
test
有权访问目录:
passhtru('sudo -u test sh -c "cd /home/test/cs && ./hlds_run"');
To arrive at the second invocation, you should already be familiar with system
vs execve
semantics (used by passthru
and sudo
respectively). 要进行第二次调用,您应该已经熟悉
system
vs execve
语义(分别由passthru
和sudo
使用)。
This is the tested shell string we need to run as a specific user: 这是我们需要以特定用户身份运行的经过测试的shell字符串:
cd /home/test/cs && ./hlds_run
We can ensure that it always runs as a specific user with sudo
, but sudo
uses execve semantics. 我们可以确保它始终使用
sudo
作为特定用户运行,但是sudo
使用execve语义。 We have to convert our shell string to an execve
array, and since this command A. relies on shell functionality like cd
and B. does not include dynamic values, the best way to do this is to simply invoke a shell to interpret it verbatim: 我们必须将我们的shell字符串转换为
execve
数组,并且由于此命令A.依赖于cd
和B之类的shell功能,因此不包含动态值,因此最好的方法是简单地调用shell逐字解释它:
{ sh
, -c
, cd /home/test/cs && ./hlds_run
} {
sh
, -c
, cd /home/test/cs && ./hlds_run
}
We can now apply sudo
to run as our specific user: 现在,我们可以应用
sudo
以我们的特定用户身份运行:
{ sudo
, -u
, test
, sh
, -c
, cd /home/test/cs && ./hlds_run
} {
sudo
, -u
, test
, sh
, -c
, cd /home/test/cs && ./hlds_run
}
passthru
runs as a shell, so now we have to convert the execve
array above back into a shell string, taking extreme care with quoting to ensure the shell will parse it into the exact argument list above. passthru
作为shell运行,因此现在我们必须将上面的execve
数组转换回shell字符串,并格外小心地加引号,以确保shell将其解析为上面的确切参数列表。 Fortunately this is a relatively simple case: 幸运的是,这是一个相对简单的情况:
sudo -u test sh -c "cd /home/test/cs && ./hlds_run"
We can now give it to passthru
: 现在,我们可以将其传递给
passthru
:
passthru('sudo -u test sh -c "cd /home/test/cs && ./hlds_run"');
Have you try this? 你有尝试过吗? (make sure if you have exec right for hlds_run)
(确保您具有hlds_run的执行权)
passthru('bash -c "sudo -u test && /home/test/cs/hlds_run"');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.