php with sudo works on command line but not in browser - php

Hello I am writing a little php/html page and trying to pass user & password to run, although it works on command line, it does not work in the browser.. Any advise?
$user = "echo password!";
$row = exec('$user | sudo -u user_id -S /usr/bin/VBoxManage list vms',$output,$error);
echo "\n";
while(list(,$row) = each($output)){
echo sprintf($row) . "<BR>\n";
}
if($error){
echo "Error : $error<BR>\n"; exit;
}

One possible cause is that the sudo binary requires a full path too.
However, your syntax is incorrect - single quotes prevent $user from being expanded.
Even then, it is possible that exec will not honor the pipe request and you will instead need to authorize the user running Apache to issue VBoxManage commands without password, using visudo. Or you may want to look into proc_* functions.
This should work:
$output = shell_exec("/bin/sh -c '{$user} | sudo -u ...'");

Related

Run eJabberd register user command as root/normal user

I have installed the eJabberd and its working fine. Now I have to integrate the registration and other functionality with PHP code.
I am trying to run following script to register user using PHP code :
<?php
error_reporting(E_ALL);
ini_set('display_errors', '-1');
$username = 'user1';
$password = '123456';
$node = 'localhost';
exec('echo <password> | sudo -u root -S ejabberdctl register '.$username.' '.$node.' '.$password.' 2>&1',$output,$status);
if($output == 0)
{
echo "User created successfully.";
}
else
{
// Failure, $output has the details
echo '<pre>';
foreach($output as $o)
{
echo $o."\n";
}
echo '</pre>';
}
?>
But its giving me following error :
[sudo] password for www-data: Sorry, try again.
[sudo] password for www-data:
sudo: 1 incorrect password attempt
Is there any other way to run eJabberd commands using PHP ?
Any reference will be very helpful. Thanks in advance.
You web service will be running as user www-data in Ubuntu. You can allow this run run specific commands as root using /etc/sudoers.
Add this line to your sudoers.
www-data ALL = NOPASSWD: /[FULL PATH TO]ejabberdctl
Replace [FULL PATH TO] with the full path to that script.
What this will do is say that the user www-data can run the script at that location as root. It also says no password is required.
Obviously this has some security considerations - ensure that you trust the script and that it cannot be replaced by other malicious code.
An alternative solution would be to make ejabberdctl always run as root. To to that use this on the executable.
chmod +s ejabberdctl
Once again this may have implications as now any user on your system can use that command. You have to think through the difference scenarios and choose which introduces the least risk for your given circumstances.

Run a command on a SSH screen through SSH with phpseclib

I have a PHP script that starts a detached screen through SSH:
$ssh->exec("screen -m -d -S ".$user);
I now need to execute a command in that screen without being in that screen. I have the code that does that, which I have tested through a SSH client, but when I try to use it with the phpseclib exec command, it does not work. This is the code that works:
screen -S ".$user." -X stuff "cd minecraft/servers/".$user."/;sh start.sh $(printf '\r')"
And this is it in the PHP script:
$ssh->exec("screen -S ".$user." -X stuff \"cd minecraft/servers/".$user."/;sh start.sh $(printf '\r')\"");
I attempted to escape the extra double quotes in the code.
Is there anything I can do to make this work through PHP? Thanks
Hmmm...
create please two bash script, first: create screen with user parameter with name f.e. run_screen, second: tester for SSH client with user parameter with name f.e. run_test.
Run first script:
$ssh->exec('[full_path]/run_screen ' . $user);
and second:
$ssh->exec('[full_path]/run_test ' . $user);
bash syntax is here bash syntax
Sure that the user of server (f.e. Apache) has permissions to run scripts.

run command as root via php

I have a php script which contains su. when I run it in shell it ask for root password and works well. but now I want make an interface for it. as you know all command in php-apache runs as www-data user and this user doesn't have specified password or it's not in sudoers. so I can't use "echo pass | sudo -S ..." because I don't know what is www-data's password and It is not logical on every server set password for www-data or add it in sudoers+nopass, is it?
What solution can you suggest to solve this problem?
one of my commands:
su -c \"/usr/sbin/dmidecode -s baseboard-serial-number | tr \ \-\"
phpseclib should be best library choice as it does not requires any additional extensions.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('example.com');
if (!$ssh->login('user', 'pass')) {
exit('Login Failed');
}
echo $ssh->exec('whoami');
echo $ssh->exec('pwd');
?>
Another alternative is libssh2, but it has to be compiled separately and is notoriously difficult to setup/use.
I recently published a project that allows PHP to obtain and interact with a real Bash shell, you can easily get a shell with root. Get it here: https://github.com/merlinthemagic/MTS
After downloading you would simply use the following code:
$shell = \MTS\Factories::getDevices()->getLocalHost()->getShell('bash', true);
$strCmd = "\"/usr/sbin/dmidecode -s baseboard-serial-number | tr \ \-\"";
$return1 = $shell->exeCmd($strCmd);
echo $return1;// return from your command

php shell_exec echo

I'm working on interfacing a microcontroller with a lamp server. I am trying to run the command echo -e -n "data \r" > /dev/ttyUSB0 using shell_exec in php but with no results. It works just fine from the command line. Doing a little experimenting, I discovered that echo -e -n "1 \r" actually echoes -e -n 1. Is there a reason it won't take the -e or -n options?
Here's my code:
<?php
shell_exec('echo -e -n "1 \r" > /dev/ttyUSB0');
?>
Instead of using shell_exec and echo, why not use PHP's filesystem functions?
file_put_contents('/dev/ttyUSB0', "1 \r");
There are some other functions too, try this function maybe you get your answer.
exec(command, $output);
This function takes a command and assigns to $output an array where each element is a line of the generated output.
I ran into similar problem, calling from php
php > echo shell_exec("echo -e aaa\tbbb");
-e aaa bbb
note, that output contains "-e", while I have expected that 'echo' command will interpret -e as flag and would not send it to output.
After doing some investigation I came to following conclusion:
when exec or shell_exec are called from php - new shell interpreter is launched.
php launches "sh".
When I was running on CentOS sh was a symlink to bash, and exec("echo ...") worked as I would have expected.
Now I am running Ubuntu. sh is a symlink to dash, not bash!
And final root cause - builtin echo command in dash does not have/understand '-e' flag, so it just forwards it to output
This is an old question but it goes what worked for me in case someone else comes across with this as well.
Yesterday night I've messing around and struggling with this myself. For this to work, the command you need to use should start by calling the echo bin directly /bin/echo... instead of only echo.
Also don't forget to use single quote /bin/echo... instead of double quote to avoid PHP null byte detection error (this part you did correctly).
I had a similar problem running with -e, I was trying to change the password from a php
running this didnt work
exec("echo -e \"$pass\\n$pass\" | passwd $user");
It said passwords dont match. Checking the echo i saw that -e was includes as a part of the echoing so i changed it to
exec("echo \"$pass\\n$pass\" | passwd $user");
And then it worked.

PHP sudo in shell_exec

I want to execute a command as root with shell_exec. Now I know this is dangerous, but believe me, you need to login with MOD_AUTH and have the right privilleges to come to this page. It's secure. How can I get this done?
You could use the latest SVN version of phpseclib, a pure PHP SSH implementation, to do this. eg.
<?php
include('Net/SSH2.php');
$ssh = new Net_SSH2('www.domain.tld');
$ssh->login('username', 'password');
$ssh->read('[prompt]');
$ssh->write("sudo command\n");
$ssh->read('Password:');
$ssh->write("Password\n");
echo $ssh->read('[prompt]');
?>
The problem isn't that your page is or isn't secure, the problem is that giving a php page the ability to run some sudo command would give it to all pages including any injected code on any insecure page on any site on the server.
That said, it might be best to make a wrapper script that does just the one job that needs doing, then give the http user access to just that ONE command as sudo
http ALL=(ALL) NOPASSWD:/user/local/bin/your_wrapper_script.sh
Definitley not advised. However, you will want to look into editing the sudoers file and add the user php is running as a NOPASSWD for the command you need to run. This will only allow him to sudo that one command with out entering a password.
If you need more commands add more to it. Sudoers Configuration I know that forum/post is debian based but sudo is not strictly debian and that should help you out with the sudo configuration values that you need to put it.
I just Google'd for php sudo shell_exec and this came up as the #1 match:
http://www.php.net/manual/en/function.shell-exec.php#101440
ilya at linemedia dot ru 16-Dec-2010 04:36
sudo can be executed without storing pass in a file
system('echo "PASS" | sudo -u root -S COMMAND');
$aux=echo "admin-pass" | your command;
echo $aux;
/*******************************
************Example*************
*******************************/
Run a Perl script named my_perl_script.pl:
$aux=echo "admin-pass" | sudo -u root -S perl /path-to-the-script/my-perl-script.pl;
echo $aux;
Best way to do it:
$descriptorSpec = array(
0 => STDIN,
1 => STDOUT,
2 => STDERR,
);
if (posix_getuid() === 0) {
echo "Root\n";
} else {
echo "No root\n";
$command = 'sudo ' . PHP_BINARY . ' ' . implode(' ', $_SERVER['argv']);
$pipes = [];
$process = proc_open($command, $descriptorSpec, $pipes);
if (is_resource($process)) {
proc_close($process);
}
}
It runs the same command again, with sudo prefixed.

Categories