reasonableframework/helper/exectool.php

217 lines
6.6 KiB
PHP
Raw Permalink Normal View History

2018-07-22 14:33:23 +00:00
<?php
2018-07-22 14:35:10 +00:00
/**
* @file exectool.php
2020-07-10 07:37:58 +00:00
* @created_on 2018-07-22
* @updated_on 2020-07-10
2022-11-25 14:15:20 +00:00
* @author Go Namhyeon <abuse@catswords.net>
2018-07-23 03:15:01 +00:00
* @forked from https://github.com/scipag/PHPUtilities
2018-07-22 14:35:10 +00:00
* @brief ExecTool helper
*/
2018-07-22 14:33:23 +00:00
/*
2018-07-22 14:36:44 +00:00
* exec_test() executes a command (like "whoami") with different
* PHP functions in order to figure out which fuctions are supported
* in the webserver configuration. The function execTests returns an array, which
* contains names of all successful tested PHP functions.
*/
if(!is_fn("exec_test")) {
2018-07-22 14:36:44 +00:00
function exec_test() {
$cmd = "whoami";
$cmdPath = "/usr/bin/whoami";
$return = "";
$output = "";
$methodArray = array();
// Testing system()
$return = ""; $output = "";
ob_start();
$output = system("$cmd 2>&1", $return);
ob_end_clean();
if (strlen($output) > 0 && $return == 0) {
$methodArray[] = "system";
}
// Testing exec()
$return = ""; $output = "";
exec($cmd, $output, $return);
if (strlen($output[0]) > 0 && $return == 0) {
$methodArray[] = "exec";
}
2018-07-22 14:33:23 +00:00
2018-07-22 14:36:44 +00:00
// Testing shell_exec()
$return = ""; $output = "";
$output = shell_exec($cmd);
if (strlen($output) > 0) {
$methodArray[] = "shell_exec";
}
2018-07-22 14:33:23 +00:00
2018-07-22 14:36:44 +00:00
// Testing backticks
$return = ""; $output = "";
$output = `$cmd`;
if (strlen($output) > 0) {
$methodArray[] = "backticks";
}
2018-07-22 14:33:23 +00:00
2018-07-22 14:36:44 +00:00
// Testing passthru()
$return = ""; $output = "";
2018-07-22 14:33:23 +00:00
ob_start();
2018-07-22 14:36:44 +00:00
passthru($cmd, $return);
$output = ob_get_contents();
2018-07-22 14:33:23 +00:00
ob_end_clean();
2018-07-22 14:36:44 +00:00
if (strlen($output[0]) > 0 && $return == 0) {
$methodArray[] = "passthru";
}
// Testing popen()
$return = ""; $output = "";
$handle = popen($cmdPath, "r");
$output = fread($handle, 2096);
pclose($handle);
if (strlen($output) > 0) {
$methodArray[] = "popen";
}
2018-07-22 14:33:23 +00:00
2018-07-22 14:36:44 +00:00
// Testing proc_open()
$return = ""; $output = "";
$descriptorspec = array(
0 => array("pipe", "r"), // stdin is a pipe that the child will read from
1 => array("pipe", "w"), // stdout is a pipe that the child will write to
2 => array("file", "/tmp/error-output.txt", "a") // stderr is a file to write to
);
$cwd = '/tmp';
$env = array('some_option' => 'aeiou');
$process = proc_open($cmd, $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
// $pipes now looks like this:
// 0 => writeable handle connected to child stdin
// 1 => readable handle connected to child stdout
// Any error output will be appended to /tmp/error-output.txt
$output = stream_get_contents($pipes[1]);
fclose($pipes[1]);
// It is important that you close any pipes before calling
// proc_close in order to avoid a deadlock
$return = proc_close($process);
}
if (strlen($output) > 0 && $return == 0) {
$methodArray[] = "proc_open";
2018-07-22 14:33:23 +00:00
}
2018-07-22 14:36:44 +00:00
return $methodArray;
2018-07-22 14:33:23 +00:00
}
2018-07-22 14:36:44 +00:00
}
2018-07-22 14:33:23 +00:00
2018-07-22 14:36:44 +00:00
/*
* exec_command() executes a command (like "whoami") with the submited method
*/
if(!is_fn("exec_command")) {
2018-09-26 11:38:19 +00:00
function exec_command($command, $method="shell_exec", $options=array()) {
2018-07-23 03:15:01 +00:00
$return = false;
2018-09-26 11:46:26 +00:00
// command cache
if(array_key_equals("cache", $options, true)) {
// set command cache filename
$filename = get_hashed_text($command, array(
"salt" => true,
"2p" => true
));
// read command cache
$return = read_storage_file($filename, array(
"storage_type" => "cache"
));
// write command cache
if(!$return) {
$options['cache'] = false;
$return = exec_command($command, $method, $options);
write_stroage_file($return, array(
"storage_type" => "cache",
"filename" => $filename
));
}
return $return;
}
2018-09-26 11:38:19 +00:00
2020-07-10 07:38:45 +00:00
// An 'async' option will be return PID
if(array_key_equals("async", $options, true)) {
$command = sprintf("%s 1>/dev/null 2>&1 & echo $!;", $command);
}
2020-07-10 07:37:58 +00:00
if (empty($method)) {
2018-07-22 14:36:44 +00:00
// ob_start() will turn on output buffering to collect all output from
// exec_test() and ob_end_clean() will clean the buffer afterwards ("garbage collection")
ob_start();
$methodArray = exec_test();
ob_end_clean();
if (is_array($methodArray)) {
$method = $methodArray[0];
2018-07-23 03:15:01 +00:00
} else {
2018-07-22 14:49:49 +00:00
echo "[!] No method available";
2018-07-22 14:36:44 +00:00
exit;
}
}
2018-07-22 14:33:23 +00:00
2018-07-23 03:15:01 +00:00
ob_start();
2018-07-22 14:36:44 +00:00
switch ($method) {
case "system":
system("$command 2>&1");
break;
case "exec":
exec($command, $output);
break;
case "shell_exec":
echo shell_exec($command);
break;
case "backticks":
echo `$command`;
break;
case "passthru":
echo passthru($command);
break;
case "popen":
$handle = popen($command, "r");
echo fread($handle, 2096);
pclose($handle);
break;
case "proc_open":
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "/tmp/error-output.txt", "a")
);
$cwd = '/tmp';
$env = array('some_option' => 'aeiou');
$process = proc_open($command, $descriptorspec, $pipes, $cwd, $env);
if (is_resource($process)) {
echo stream_get_contents($pipes[1]);
fclose($pipes[1]);
proc_close($process);
}
break;
default:
echo "[!] No method defined";
break;
}
2018-07-23 03:15:01 +00:00
$return = ob_get_clean();
2020-07-10 08:16:30 +00:00
// PID is integer
if(array_key_equals("async", $options, true)) {
$return = intval(rtrim($return));
2020-07-10 08:19:38 +00:00
}
2020-07-10 08:16:30 +00:00
2018-07-23 03:15:01 +00:00
return $return;
2018-07-22 14:33:23 +00:00
}
}