r/PHPhelp • • Feb 26 '24

Any way to SAFELY execute shell scripts from PHP?

I have a website accessible from https://exampleurl.com:1234 (i.e. nonstandard port).

After logging in, this website gives my users a GUI and a few buttons. The buttons interact with PHP scripts on an Unraid VM, which then launch shell scripts. The shell scripts are used for controlling streams via streaming software called "OBS-Studio".

Here's an example:

my.php
<?php
$output = shell_exec("/bin/bash /var/www/html/streamstart.sh");
echo $output; 
?>


streamstart.sh
#!/bin/bash
obs-cmd streaming start

I'm relatively new to PHP, and I'm not sure how this could be exploited. Is there a way to make this more secure?

I have set the password for my users to be extremely secure, but that feels like the only line of defense right now.

9 Upvotes

10 comments sorted by

6

u/[deleted] Feb 26 '24

Executing other applications (whether it's a shell script or something else) from a PHP application is not unsafe per se. The security depends heavily on how safe the application is and how much a user can control of the input.

You must ensure a user never can control what exact command line is executed (and ensure that every parameter is properly escaped). If not absolutely necessary the user should also not control parameters of the application (and if only a specified subset of them).

As long as you just call a static script without any dynamic input which is somehow user controllable, you should be relatively safe if the application and the action performed is safe.

Maybe you should think about restricting or sandboxing the called applications (using techniques like selinux, user permissions, or containers), so it can only do its desired job and can do as little harm as possible.

1

u/XandrousMoriarty Feb 26 '24

And don't forget the cardinal rule - sanitize, sanitize, sanitize any output you get from any command. It may not seem to be needed, but it's good practice to check for proper output from one program before you manipulate it or utilize it in any way.

1

u/rifts Feb 27 '24

Everyone is giving you kinda weird answers, so here's whats up. What you are doing in this case is ok since you don't let the users send data.

Now if you did something like

$output = shell_exec("/bin/bash /var/www/html/$_GET['user_sent_text']"); 

that would be very unsafe and a terrible idea because the users can send malicious code. I hope that helps.

1

u/Ok-Breakfast-1522 Feb 27 '24 edited Feb 27 '24

Thank you. I do have one instance of this. For one script, the user inputs a string and that is fed into a sed command that replaces a string in a JSON file that OBS uses to create its scenes. They’re not typing it per se - they’re selecting a list of files and JS sends the right file name. But a savvy user could send whatever they wanted through the console. I didn’t think there would be anyway to exploit this because, at the end of they day, it’s still just a JSON file that only gets touched by OBS. But I could be wrong. I suppose setting some sort of limit on what can be fed into the sed command might add another layer of security.

1

u/rifts Feb 27 '24

yeah if there is a set amount of options you could use an array and just check the users inputs equals one of the values in the array and only use it then.

1

u/allen_jb Feb 27 '24 edited Feb 27 '24

Just like with SQL, this can be made safe by proper validation of input and escaping to ensure users can't specify unexpected values.

You could go a step further and execute commands as a different user (and/or environment) using a job queue. For example the user executing the scripts may have no permission to write to files outside a specific directory, and have limited permissions to execute other commands or access the network - either through advanced permission systems like selinux or using containers.

Validation of input may include restricting allowed characters or character sequences (eg. forbidding '..' from paths to prevent directory traversal issues) and/or checking files exist in a specific directory (using realpath() to resolve paths, then checking that path points to a specific location).

Use escapeshellarg() to escape output appended to commands.

1

u/PuzzleheadedPilot128 Mar 02 '24

You mean to say alot of the answers are gptd and literal rewrites of one another. Lol. 

1

u/HolyGonzo Feb 26 '24

Executing a process from PHP is not inherently unsafe.

There are some common scenarios that can create safety issues, though.

First you have to realize that PHP is basically like a virtual user on the server. When you tell it to run a process, it runs that process with the user account that PHP is running as. So if PHP is running as a user account named "phpuser", and you tell PHP to run /usr/bin/foobar, then it'll be the same as if you logged into the server with the "phpuser" login and then ran foobar.

That's not unsafe for you to log in and run that command, but let's pretend for a moment that you can't run foobar - it gives you some security error message. You look closer and see that foobar can only be executed by the "root" superuser account.

The appropriate fix might be to see if the permissions on foobar are incorrect and need correction. Or it might actually be that you need to be root.

The wrong thing here is to "fix" it by running PHP as root but some people do that. If PHP is ever compromised, then the attacker gets root-level access to the entire system.

So the first thing to know is that you should never increase the permissions of the PHP to get around a security restriction. In almost every case, there is a better way, even if it's permitting sudo for just that one command.

The second thing is to be aware that whatever information you pass on the command line will be visible to anyone else on the system who is looking at the list of running processes.

So if I ran shell_exec("/usr/bin/customservice --password=FOOBAR"); ...then while customservice was running, anyone who listed the processes with ps or top would be able to see that password.

So don't put any sensitive information into a parameter.

The third thing is to know that environment variables will propagate from PHP to the child process you run. Some people follow the bad (but currently popular) practice of using environment variables for credentials. So make sure you clear any sensitive env variables before launching the process, so that the process doesn't accidentally leak them.

Lastly, and most importantly, don't ever take user input and use it within the command line (unless you've heavily sanitized it).

1

u/Besen99 Feb 28 '24

Please use a proper library for shenanigans like that: https://symfony.com/doc/current/components/process.html

-7

u/chmod777 Feb 26 '24

If you ever find yourself needing to do this, you are probably Doing It Wrong.