我需要从PHP脚本的强制上下文调用可执行文件。在性能和安全方面,最好不要在web服务器进程和可执行文件之间调用shell。
当然,我搜索了网页,没有成功(在这样的PHP上下文中)。许多其他语言都允许这样做,并且有清晰的文档。
唉,反引号,exec()
, shell_exec()
, passthru()
, system()
, proc_open()
, popen()
调用shell。pcntl_fork()
似乎不可用。
如何测试函数是否调用shell。
这是在Debian 6 64位PHP 5.3.3-7+squeeze15上测试的。在http://pastebin.com/y4C7MeJz
上测试代码为了获得一个有意义的测试,我使用了一个技巧,即要求执行一个shell命令,该命令也不能作为可执行文件使用。umask
就是一个很好的例子。任何返回类似0022的函数都被称为shell。exec()
, shell_exec()
, passthru()
, system()
, proc_open()
都有。详见http://pastebin.com/RBcBz02F .
现在,回到目标:如何在不启动shell的情况下执行任意程序?
Php的exec像预期的那样接受字符串参数数组,而不是唯一的字符串。但是pcntl_fork只是停止请求,甚至不记录日志。
编辑:pcntl_fork失败是因为服务器使用Apache的mod_php,参见http://www.php.net/manual/en/function.pcntl-fork.php#49949。
编辑:根据@hakre的建议,将popen()
添加到测试中。
回答你的句子:
在性能和安全性方面,最好不要调用shell所有在web服务器进程和可执行文件之间。
关于性能,是的,php内部分叉,shell本身也分叉,所以这有点重。但是你真的需要执行很多进程来考虑这些性能问题。
关于安全性,我在这里没有看到任何问题。PHP有escapeshellarg函数来清理参数。
我在没有pcntl的exec
中遇到的唯一真正的问题不是资源问题,也不是安全问题:创建真正的守护进程真的很困难(没有任何与其父进程的连接,特别是Apache)。我通过使用at
解决了这个问题,在双转义我的命令:
之后$arg1 = escapeshellarg($arg1);
$arg2 = escapeshellarg($arg2);
$command = escapeshellarg("/some/bin $arg1 $arg2 > /dev/null 2>&1 &");
exec("$command | at now -M");
回到你的问题,我知道以标准 (fork+exec)方式执行程序的唯一方法是使用PCNTL扩展(如前所述)。不管怎样,祝你好运!
为了完成我的答案,您可以自己创建一个exec
函数,它的功能与pcntl_fork
+ pcntl_exec
相同。
我做了一个my_exec
扩展,做了一个经典的exec+fork,但实际上,我不认为它会解决你的问题,如果你在apache下运行这个函数,因为与pcntl_fork
相同的行为将适用(apache2将被分叉,当execv
不成功时,可能会有意想不到的行为与信号捕获等)。
配置。m4 phpize
配置文件
PHP_ARG_ENABLE(my_exec_extension, whether to enable my extension,
[ --enable-my-extension Enable my extension])
if test "$PHP_MY_EXEC_EXTENSION" = "yes"; then
AC_DEFINE(HAVE_MY_EXEC_EXTENSION, 1, [Whether you have my extension])
PHP_NEW_EXTENSION(my_exec_extension, my_exec_extension.c, $ext_shared)
fi
my_exec_extension.c扩展名
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "php.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define PHP_MY_EXEC_EXTENSION_VERSION "1.0"
#define PHP_MY_EXEC_EXTENSION_EXTNAME "my_exec_extension"
extern zend_module_entry my_exec_extension_module_entry;
#define phpext_my_exec_extension_ptr &my_exec_extension_module_entry
// declaration of a custom my_exec()
PHP_FUNCTION(my_exec);
// list of custom PHP functions provided by this extension
// set {NULL, NULL, NULL} as the last record to mark the end of list
static function_entry my_functions[] = {
PHP_FE(my_exec, NULL)
{NULL, NULL, NULL}
};
// the following code creates an entry for the module and registers it with Zend.
zend_module_entry my_exec_extension_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_MY_EXEC_EXTENSION_EXTNAME,
my_functions,
NULL, // name of the MINIT function or NULL if not applicable
NULL, // name of the MSHUTDOWN function or NULL if not applicable
NULL, // name of the RINIT function or NULL if not applicable
NULL, // name of the RSHUTDOWN function or NULL if not applicable
NULL, // name of the MINFO function or NULL if not applicable
#if ZEND_MODULE_API_NO >= 20010901
PHP_MY_EXEC_EXTENSION_VERSION,
#endif
STANDARD_MODULE_PROPERTIES
};
ZEND_GET_MODULE(my_exec_extension)
char *concat(char *old, char *buf, int buf_len)
{
int str_size = strlen(old) + buf_len;
char *str = malloc((str_size + 1) * sizeof(char));
snprintf(str, str_size, "%s%s", old, buf);
str[str_size] = ''0';
free(old);
return str;
}
char *exec_and_return(char *command, char **argv)
{
int link[2], readlen;
pid_t pid;
char buffer[4096];
char *output;
output = strdup("");
if (pipe(link) < 0)
{
return strdup("Could not pipe!");
}
if ((pid = fork()) < 0)
{
return strdup("Could not fork!");
}
if (pid == 0)
{
dup2(link[1], STDOUT_FILENO);
close(link[0]);
if (execv(command, argv) < 0)
{
printf("Command not found or access denied: %s'n", command);
exit(1);
}
}
else
{
close(link[1]);
while ((readlen = read(link[0], buffer, sizeof(buffer))) > 0)
{
output = concat(output, buffer, readlen);
}
wait(NULL);
}
return output;
}
PHP_FUNCTION(my_exec)
{
char *command;
int command_len, argc, i;
zval *arguments, **data;
HashTable *arr_hash;
HashPosition pointer;
char **argv;
// recovers a string (s) and an array (a) from arguments
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sa", &command, &command_len, &arguments) == FAILURE) {
RETURN_NULL();
}
arr_hash = Z_ARRVAL_P(arguments);
// creating argc and argv from our argument array
argc = zend_hash_num_elements(arr_hash);
argv = malloc((argc + 1) * sizeof(char *));
argv[argc] = NULL;
for (
i = 0, zend_hash_internal_pointer_reset_ex(arr_hash, &pointer);
zend_hash_get_current_data_ex(arr_hash, (void**) &data, &pointer) == SUCCESS;
zend_hash_move_forward_ex(arr_hash, &pointer)
)
{
if (Z_TYPE_PP(data) == IS_STRING) {
argv[i] = malloc((Z_STRLEN_PP(data) + 1) * sizeof(char));
argv[i][Z_STRLEN_PP(data)] = ''0';
strncpy(argv[i], Z_STRVAL_PP(data), Z_STRLEN_PP(data));
i++;
}
}
char *output = exec_and_return(command, argv);
// freeing allocated memory
for (i = 0; (i < argc); i++)
{
free(argv[i]);
}
free(argv);
// WARNING! I guess there is a memory leak here.
// Second arguemnt to 1 means to PHP: do not free memory
// But if I put 0, I get a segmentation fault
// So I think I do not malloc correctly for a PHP extension.
RETURN_STRING(output, 1);
}
test.php使用示例
<?php
dl("my_exec.so");
$output = my_exec("/bin/ls", array("-l", "/"));
var_dump($output);
shell script运行这些命令,当然使用你自己的模块目录
phpize
./configure
make
sudo cp modules/my_exec_extension.so /opt/local/lib/php/extensions/no-debug-non-zts-20090626/my_exec.so
结果KolyMac:my_fork ninsuo$ php test.php
string(329) ".DS_Store
.Spotlight-V100
.Trashes
.file
.fseventsd
.hidden
.hotfiles.btree
.vol
AppleScript
Applications
Developer
Installer Log File
Library
Microsoft Excel Documents
Microsoft Word Documents
Network
System
Users
Volumes
bin
cores
dev
etc
home
lost+found
mach_kernel
net
opt
private
sbin
tmp
usr
var
vc_command.txt
vidotask.txt"
我不是一个C开发,所以我认为有更干净的方法来实现这一点。但是你明白我的意思了
在PHP 7.4+中,如果cmd
作为数组传递,proc_open将直接打开进程
从PHP 7.4.0开始,cmd可以作为命令参数数组传递。在这种情况下,进程将直接打开(不通过shell), PHP将负责任何必要的参数转义。
所以这个例子:
<?php
$file_descriptors = [
0=>['pipe','r'],
1=>['pipe','w'],
2=>['pipe','w']
];
$cmd_string = 'ps -o comm=';
$cmd_array = [
'ps',
'-o',
'comm='
];
// This is executed by shell:
$process = proc_open($cmd_string,$file_descriptors,$pipes);
$output = stream_get_contents($pipes[1]);
$return = proc_close($process);
printf("cmd_string:'n%s'n",$output);
// This is executed directly:
$process = proc_open($cmd_array,$file_descriptors,$pipes);
$output = stream_get_contents($pipes[1]);
$return = proc_close($process);
printf("cmd_array:'n%s'n",$output);
输出:
cmd_string:
bash
php
sh
ps
cmd_array:
bash
php
ps
我考虑试试pcntl_exec()