I am compiling and executing a C program that uses a PHP System Command on Windows XP Server.
If the C program contains a System command like System("shutdown -a") or any system command, then it turns my system down.
I want these kinds of commands to be denied. How do I show "permission denied" as an output when a program tries to run a system command?
Here is my code.
PHP script contains-
system(gcc code.c -o out); system(out.exe);
IF C program contains-
int main() { system("shutdown -r"); }
Is there a way to block those commands from being run?
Windows runas command
You might be able to use windows runas command to run a command as a specific user. And in that user's profile, set a list of white listed commands. https://superuser.com/questions/42537/is-there-any-sudo-command-for-windows
Roll your own command white list
Create a list of commands in PHP or C that are allowed to be run through the C program, and if the command isn't on the approved list, it is denied. Or of you like to live dangerously, create a black list, and define a bunch of things you don't want run.
Ok, so the simple answer is "Don't run as root" (then the user can only access files he/she has created, so not so much chance of doing damage)
The more sophisticated answer is to farm out the running of the code onto a virtual machine within the server. A virtual machine can be cloned from a template (or whatever the VM software calls such a thing) in fairly short time. Within a VM, the code running there has no access to the "REAL" machine that it runs on, so if the user wants to destroy his/her virtual machine, that is his/her problem - just have to request a "new virtual machine" before they can continue working.
Related
I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)
I can get system or exec to start processes that work silently. Here's a simple example
<?php exec ("echo hello > hello.txt");?>
I can get it to execute a bat file that has no visible output.
I can't get any program that has a screen to run such as a report generator or notepad...
<?php exec ("explorer");?>
doesn't do anything. or same for system
Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).
Do not wait for application:
pclose(popen("start /B notepad.exe", "r"));
Wait for application:
system('start notepad.exe');
This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.
Side note
On my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.
Another super late answer, but this comes up on Google when searching for "php run gui program"...
I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).
This is a Ubuntu system and I need the PHP to execute a program with a specific username, let's say, userA.
I used php exec() function to call sudo -Eu userA command_to_run_program, it did not work because of some security features or environment variables missing on Ubuntu.
So I'm thinking if this alternative way can be achieved:
From the back-end, there is a shell script with a fixed Pid running and waiting for signals. If this process receives a specific signal, it would execute the program. Let's say I manually started this shell script with userA. So I assume when it receives the signal and execute the program, the program is executed with userA.
And there is a apache server with PHP on this machine. The front-end user goes the PHP page and the php program sends the signal to the running shell script, awake the shell script and thus the program is executed by userA.
If it can be achieved, what is the best practice to do so?
Thanks for your help!
In other words, you need some kind of privilege escalation from the user account of the webserver to a (specific?) different user. This has security implications, but I assume that you have considered the possibilities.
Anyhow, following steps should work:
Create a program (albeit just a shell script) that runs the according code.
Put the program into a place where the webserver can access it.
Make the target user the owner of the program and set the SUID bit. That way, executing it will cause it to run under the user's account.
Make the webserver group the group of the program and only allow it (not other users) to run the program via the executable bit.
See the manpages of chown and chmod for further info.
I'm trying to run notepad on the server (localhost for now).
exec() and system() functions are working fine when for example write ping 127.0.0.1.
But this does not work (working fine if I write the command directly in the command prompt):
$command = "C:\WINDOWS\system32\notepad.exe";
$result = system($command);
print_r($result);
Using Windows XP with xampp. Probably I don't have permissions because the command is executed from some other account but I don't know how to check this.
Any advices?
Edit:
As bwoebi said, I have opened processes but they are opened from a different user (SYSTEM) and I can't see when the application is opened. So, I have to paraphrase my question: how to change the user which is used when executing commands from a PHP script?
First you need to escape the backslashes in your command string if you're not using single quotes :
$command = "C:\\WINDOWS\\system32\\notepad.exe";
Also note that if Apache is running as a Windows service, it does not have desktop interaction permission, so it can't open a GUI, try running the script directly with PHP on the command line.
EDIT
The user used to run command is the user that is running PHP. To change the user running PHP, you'll have to change the user running Apache, if you want this user to have desktop interaction permission, you'll have to run Apache yourself and not as a service.
let the process a bit sleep after executing the shell command and search in the TaskManager for a Notepad... Then you'll see that this are two different users (and you don't see the other users Notepad)
Note pad is a GUI program so requires the windows TTY to be active.
Ping is command line so can be ran by the system directly and piped results into the program calling it.
With out getting into too much detail of how os's work basically it can't be done on a windows machine (its possible on unix machines but more difficult.)
The Setup:
I have a LAMP server (Ubuntu 9.10) which is also hooked up to my HDTV. Upon boot, one user is automatically logged in so as to have a desktop environment on screen.
What I want:
I've been developing a remote, web interface in PHP for various things (play music, for example). However, I've hit a snag in that I would like to run a windowed program and have it display on the TV. Obviously, since PHP/Apache is running under the user www-data, this isn't going to happen just by running my command via exec().
Is there a Linux command that can run it as the currently logged in session of my other user, or a program that?
The (X) program starting uses the DISPLAY variable to determine which X session to hook up to. You'll need to figure out which X session the user currently has, if this is a one-user box is's most likely to be :0.
Then you could write a simple bash script to
1. Set the DISPLAY (and other variables as needed)
2. Execute.
--
Another solution would be to write the necessary information to a flat file and then have a cron job checking for updates every one or three seconds. The cron can be configured to run as a specified user. Ugly, but sould work.
As mataisf says - you need to set the DISPLAY variable so the program knows where to generate the window. However there is an authentication system which prevents unauthorized programs from access an X server (the place where the keyboard, screen and mouse are). One way around this is to let any program connecting from the local machine have access:
xhost +localhost
....but a better solution would be to run the program as the user logged in. There are lots of different ways to do this - but probably the most practical is via sudo, e.g.
sudo -u console_user program
Note that before you do this you might want to set the HOME variable so that xauth works properly (you can do this with the -H flag for sudo).
Note that you need to configure the program, the console_user and the webserver user in the /etc/sudoers file.
C.
I'm running in a controlled, xp, intranet only environment and I need to start external processes from a PHP applications. (Backups, Reports etc.)
I can get system or exec to start processes that work silently. Here's a simple example
<?php exec ("echo hello > hello.txt");?>
I can get it to execute a bat file that has no visible output.
I can't get any program that has a screen to run such as a report generator or notepad...
<?php exec ("explorer");?>
doesn't do anything. or same for system
Very late answer, but I was working on this myself and found that it is indeed possible to run a GUI program from PHP with the Apache server on Windows XP.
Start->Run, type "services.msc" to bring up Services control (other ways to get there, this is easiest IMO)
Locate your Apache service (mine was called "wampapache" using WampServer 2.0)
Open the service properties (double-click or right click->properties)
Flip to the Log On account and ensure the checkbox titled "Allow service to interact with Desktop" is checked
Flip back to the General tab, stop the service, start the service
Now, using the code below, you can spawn UI processes from PHP. In the first code snippet, the script will not wait for the application to close; the second snippet waits for the program to close before continuing (blocking).
Do not wait for application:
pclose(popen("start /B notepad.exe", "r"));
Wait for application:
system('start notepad.exe');
This has been tested on Windows XP. I have not tried it on any other Windows versions, your millage may vary.
Side note
On my particular installation, I was using the other option in the Log In tab of the service - Apache was running as a domain user so it could access several network shares with domain user permissions. The checkbox isn't available for that option, only when the service is running as Local System. After extensive research, I've found that there is simply no way for a single service to both interact with the current desktop AND utilize the credentials of a specific user. It's a one-or-the-other proposition, with the suggested remedy being to split your service into two components - one that uses the user account privs and one that interacts with the desktop. Not very practical when the service you're talking about is the web server. This note is probably pretty specific to my use case, but I wanted to put it out here in case I can save someone else the frustration in the future.
Another super late answer, but this comes up on Google when searching for "php run gui program"...
I have been able to launch a GUI app in Windows 8.1 by making, running and deleting a scheduled task:
shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
What behavior are you expecting? Calling system('notepad') works fine - it just doesn't display the GUI. It runs in the background, and PHP sits there patiently waiting for notepad to close itself (and only continues if you kill notepad from the process list).
If you're expecting it to pop up a GUI, I'm fairly certain that you can't do that. ;) An option might be to write out a batch script (file_put_contents('runme.bat', 'notepad hello.txt')) and have that batch script queued (with Windows scheduler or whatever the cron-equivalent is on Windows) to run in an async fashion (and clear itself at the end).