PHP Warning: shell_exec(): unable to execute '[command]' - php

background: I have a PHP script which calls shell_exec. For the moment i just want to test that it works and am running a basic command through it. Separate copies of the same script exist in two separate webapps on the same server. Both apps' anonymous authentication are set to IUSR.
Here is the example code:
$output = shell_exec('dir 2>&1');
print_r($output);
Trying it on one website in IIS gives the desired output. However, on another, it does not capture any output and instead give the below error:
PHP Warning: shell_exec(): Unable to execute 'dir 2>&1' in C:\inetpub\webapp\script.php on line 4
The only information i could find on this error is from here:
With PHP on Windows, if you get the 'Warning: shell_exec()
[function.shell-exec]: Unable to execute' error, then you need to
check the permissions on file 'C:\WINDOWS\system32\cmd.exe'. You need
read/execute permission on this file. I would recommend using the
sysinternals Process Monitor 'procmon.exe' to confirm the user that is
trying to run 'cmd.exe'. Filter on 'Process Name' is 'php-cgi.exe' and
'Path' ends with 'cmd.exe'. Look at the event properties for the task
with the access denied error, and it will show you the 'Impersonating'
user name. This is usually the 'Internet Guest Account', often 'NT
AUTHORITY\IUSR'.
Yet this doesnt seem like a permissions issue. I cannot understand why its working through on website and not another. Anonymous Authentication for both websites are set to IUSR.
also, safe_mode in php.ini is set to off.
Is there anything else i need to check here? Does cmd.exe need explicit read/execute permissions for IUSR?

It turns out, for whatever reason, that IUSR was the problem. I cannot exactly know why, but changing Anonymous Authentication of the affected website to use another user it worked. So it is permissions related after all.

Related

PHP Warning fopen() permission denied

I am new to PHP and I have little experience with manipulating files with PHP; I keep getting a 'permission denied' warning message and can't seem to figure out a solution.
I've made a simple web application with one php file that has just these two lines:
$file = fopen( '/Users/<myname>/Desktop/file.txt', 'r' );
var_dump( json_decode( $file ) );
to see whether the variable has been set.
When I run the script on chrome, I get:
Warning: fopen(/Users/<myname>/Desktop/file.txt): failed to open stream: Permission denied in /Applications/XAMPP/xamppfiles/htdocs/testwebsite/filereader.php on line 1
NULL
Presumably, the NULL is because $file has not be set or is empty.
I have read numerous posts online and I am still very confused about the explanations given. From my understanding, it is a permissions problem where I have not given the right privileges to some file.
I have tried many command lines such as chmod 755 /Applications/XAMPP/xamppfiles/htdocs/testwebsite/filereader.php to give permission to the script to at least be able to read a file but to no avail.
I have also read that it might be due to some user privileges that were not properly set so I've ran these to command to see who the user was:
echo get_current_user();
return 'daemon'?, a program that runs in the background?
exec('whoami');
returns '<myname>', my current machine's name
I am not sure how to proceed from here, whether to give permission to my machine that runs the script or 'daemon'.
I just want the application to run on at least my personal computer.

Getting access is denied for cmd command using php function

I am running PHP under IIS 6.
When I use any PHP function to run cmd commands (like exec() or system()), it always shows "Access is denied".
Even commands like ipconfig are showing same "access is denied" error. I have given all the permission to my project folder but it did not work.
Please let me know anyone the cause of it.
When you execute some command using exec, that command will be executed with the user account that is running IIS (which tipically is IUSR_«machine-name» on IIS 6), so your project's permissions don't have nothing to do with this.
What you need to do is to chance the permissions of the file C:\Windows\System32\cmd.exe and add permissions for IUSR_«machine-name».
Besides this, you'll probably need to use CACLS to change the access control list. Replace IUSR_MachineName on the following command for the user executing PHP and then execute this command:
CACLS c:\windows\system32\cmd.exe /E /G IUSR_MachineName:F
If you get an error and can't change the ACL, your last resource is to execute the following command first:
takeown /F c:\windows\system32\cmd.exe
You also might need to set the correct permissions and ACL for the commands you're trying to execute (for eg C:\Windows\System32\schtasks.exe)
Note: Do this with caution though. If you have some vulnerability that allows your users to execute commands, it can have nasty consequences.

Trouble with custom PHP extension

A couple of months ago, I built a serial connection library in C for the Arduino Micro-controller. I was thinking about how awesome it would be if I could write a PHP wrapper for the library.
I wrote a PHP extension with just one function called "acm_get_door()," which should either return a -1, 0, or 1, depending upon the status of a certain pin on my Arduino. If it returns -1, then that means that the connection wasn't set.
So after loading up the custom extension and changing both of my php.ini files, and restarting everything else, I ran this test via the CLI:
$ php -r 'echo acm_get_door();'
It returned a '1'. I was excited, so I played with the circuitry to see if I could get a '0'. Well I did and I thought my extension was working perfectly.
I then went to a .php file in my test webserver (same computer) and tried to run the function. I got a '-1' result from it. The extension does load, but I can't seem to initiate a connection to the Arduino.
I am wondering if it might be a permissions thing regarding access to certain files. The function needs to get access to a file called "/dev/ttyACM0" which it is able to in the CLI version. It is possible that my Apache web server doesn't have the permissions to access that file, and if so, how would I be able to adjust stuff so that it does?
Thank you for your time.
Probably the group has permissions to read/write the device.
Therefore, find out the group owner of the device using ls -l /dev/tty/ACM0. Then, add yourself to the group using:
sudo adduser <me> <the_group>
(where me is your username and the_group is the group of the device)
You might need to log out/in to gain the effect of the changed group permissions.
Okay, thanks to John Jesus, I just had to change the permissions of the file /dev/ttyACM0.
It was set to 660, 666 seems to work (though it it probably a bad idea).

Apache and CAP_NET_RAW

I have a php script that carries out a trace route without using system(). It uses SOCK_RAW and ICMP.
I have managed to get this script to run with out been a root user or changing anything in sudoers by using CAP_NET_RAW.
The thing is, the scrip only works when called through the shell, not through the browser. When I go through my browser I get the error: socket_create(): Unable to create socket [1]: Operation not permitted . So something is going a miss here.
After googleing and googleing I am unable to work out why the same script is not working when called through apache. Do I need to add CAP_NET_RAW to an apache config file as well?
Any advice on this would be very much appreciated.
Jason
Typical Apache + PHP installations run under a separate system user (usually www-data). My guess is that this pseudo-user is heavily restricted. Try executing from the shell as the www-data user - it should fail, if I'm right.

php problem to execute CutyCapt command

i'm having this problem
sh: CutyCapt: Permission denied
my php code is
<?php
echo exec('CutyCapt --url=http://www.google.com --out=/var/www/google.png --javascript=on 2>&1');
?>
When calling an executable from PHP, it is called with the permissions of the user PHP runs as (often the Apache server, for example).
That user account does not have permission to call that executable - probably because it belongs to a different user, and has the "executable" bit only for that user or group.
That's all that can be said for sure without more information.

Categories