简体   繁体   English

Bash脚本在php脚本中执行

[英]Bash script being executed in a php script

I know there are other posts about this, but I have tried out all the solutions and fixes that were marked as solution and none of them works for me. 我知道还有与此相关的其他帖子,但是我已经尝试了所有被标记为解决方案的解决方案和修补程序,但是它们都不适合我。

As the title says, I am trying to make a php script execute a bash script when being loaded in a web server (apache). 如标题所述,我正在尝试使php脚本在加载到Web服务器(Apache)中时执行bash脚本。

I am using a wrapper as I saw in another post, so here is the wrapper.c: 正如我在另一篇文章中所看到的,我正在使用包装器,因此这是wrapper.c:

#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>

int main (int argc, char *argv[]) {
    setuid (0);
    system ("/bin/sh /var/www/html/scrip/debugport/testversion.sh");
    return 0;
}

This is the testversion.sh script: 这是testversion.sh脚本:

#!/bin/bash
screen -S skywars -X stuff "stop
"
cd /tmp
pid=$(fuser -n tcp 12321 | awk '{print $1}')
kill -9 $pid

And this is the php code that I am using: 这是我正在使用的php代码:

<?php
    $message=shell_exec("./var/www/html/scrip/php_root");
    print_r($message);
?>  

When I do ./php_root (which is the compiled wrapper.c) everything works fine, but when I try to load it from a web server the script doesn't work at all. 当我执行./php_root(已编译的wrapper.c)时,一切正常,但是当我尝试从Web服务器加载该脚本时,它根本无法工作。

It's probably something to do with the permissions, but I used every permission exactly like in this post's solution: Execute root commands via PHP 这可能与权限有关,但是我使用每个权限的方式与本帖子的解决方案完全相同: 通过PHP执行root命令

The wrapper is supposed to make the script be executed as root no matter what user executes it, but it doesn't work. 无论用户执行什么操作,包装程序都应使脚本以root用户身份执行,但是它不起作用。

Thanks guys. 多谢你们。

Try check access to script. 尝试检查对脚本的访问。 May be user of web-server can not run your bash script. 可能是Web服务器的用户无法运行您的bash脚本。

I recently published a project that allows PHP to obtain and interact with a real Bash shell (as user: apache/www-data or root if needed). 我最近发布了一个项目,该项目使PHP可以获取真实的Bash shell并与之交互(作为用户:apache / www-data或root(如果需要))。 Get it here: https://github.com/merlinthemagic/MTS 在这里获取它: https : //github.com/merlinthemagic/MTS

After downloading you would simply use the following code: 下载后,您只需使用以下代码:

//Setting the second argument in getShell():
//true will return a shell with root
//false will return a shell with the php execution user
$shell    = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$return1  = $shell->exeCmd('/full/path/to/script.sh');

there is no need for the wrapper. 不需要包装器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM