• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 269
  • Last Modified:

Call executable from PHP

Hey, I've compiled a binary on Linux
I've run the following on the binary:

chmod 777 updateBeta;
chmod +s updateBeta;
chown www-data:www-data updateBeta;

I have a PHP script to call it, (user www-data)

echo "updating...";
system("/home/virtual/site1/fst/var/www/updateBeta");

When I call it from the web browser, it only says "updating..." with no output from my binary.

How can I get this binary to execute using the web browser?
0
M256
Asked:
M256
1 Solution
 
CSecurityCommented:
Use this:

function myshellexec($cmd)
{
global $disablefunc;
$result = "";
if (!empty($cmd))
{
  if (is_callable("exec") and !in_array("exec",$disablefunc)) {exec($cmd,$result); $result = join("\n",$result);}
  elseif (($result = `$cmd`) !== FALSE) {}
  elseif (is_callable("system") and !in_array("system",$disablefunc)) {$v = @ob_get_contents(); @ob_clean(); system($cmd); $result = @ob_get_contents(); @ob_clean(); echo $v;}
  elseif (is_callable("passthru") and !in_array("passthru",$disablefunc)) {$v = @ob_get_contents(); @ob_clean(); passthru($cmd); $result = @ob_get_contents(); @ob_clean(); echo $v;}
  elseif (is_resource($fp = popen($cmd,"r")))
  {
   $result = "";
   while(!feof($fp)) {$result .= fread($fp,1024);}
   pclose($fp);
  }
}
return $result;
}

Call it like this:

$result = myshellexec("/home/virtual/site1/fst/var/www/updateBeta");

0
 
CSecurityCommented:
you can echo $result to get output binary prints.
0
 
VanHackmanCommented:


I think that the following will work:


Option 1 ------------------------------------

<? php

echo "updating...";

ob_start(); //Initializing output

// Executing and putting output in a $Output

passthru("/home/virtual/site1/fst/var/www/updateBeta"); 

$Output = ob_get_contents();

ob_end_clean(); // Cleaning Output.

// Using $Output wherever you want.

print_r($Output);

?>

Option 2 ------------------------------------

<? php

echo "updating...";
passthru("/home/virtual/site1/fst/var/www/updateBeta"); 

?>

Open in new window

0
 [eBook] Windows Nano Server

Download this FREE eBook and learn all you need to get started with Windows Nano Server, including deployment options, remote management
and troubleshooting tips and tricks

 
CSecurityCommented:
My function tries all possible ways, all functions, system function, exec function, passthru function... So no failure
0
 
M256Author Commented:
$result = myshellexec("/home/virtual/site1/fst/var/www/updateBeta");
var_dump($result);

---

string(0) "" 

Sadly, no update...
0
 
VanHackmanCommented:
Do you try my code snippets ?...
0
 
CSecurityCommented:
Not needed to var_dump, simply echo it. If you don't see anything in $result:

1) Your PHP safe mode is enabled
2) Your user www-data have not valid access to execute that binary. Permission problem.
0
 
CSecurityCommented:
3) Your server added passthru, exec, system to disabled functions list.

Check safe mode and disabled functions, then check permission
0
 
valainCommented:
Hi,

Might not be the solution, but it is generally considered a very bad thing to do if you execute shell commands from within your PHP code, for obvious security risks. I'd go about this differently, but this only works if you don't need realtime execution :

1) Let your PHP script create a (temporary) signal file, like "runmystuff.now".
2) Have a CRON job run a shell script that checks for the existence of this signal file: if it exists, delete it and run your required script.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now