Jump to content

Run server script from PHP (Linux) (Safe)

Joveice

Hello,

 

Server is Ubuntu 16.04. I talk about restarting webserver but thats just one of the things I want. I might wanna do other things later on so see it as a "placeholder".

 

So I want to run server scripts from my php scripts. etc restart the webserver from my admin dashboard.

 

How would I do it? Allow www-data sudo, Works? Yes. Good idea? no, not at all, just forget about it x3

 

So my idea is to create a script .sh that can restart, stop and start based on the paramerters given and will return the output of the run. This file can then be used by the webserver but not edited.

 

Is this a good way to do that? / is it even possible to do that? I think I saw that I can allow a script to run as sudo without password.

 

Thoughts?

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

Well, your server is probably already set up so that PHP and apache are basically already running as root. So you could just run exec('YourCommand'); and that would work perfectly fine.

Pros;

-Easy AF

-Extends PHP functionality beyond standard web use

 

Cons;

-Disabled in safe mode

-Someone who finds a security flaw in your PHP script may be able to completely destroy your server

 

So yeah, possible, but you have to be pretty damn sure your exec stuff is well protected

I am good at computer

Spoiler

Motherboard: Gigabyte G1 sniper 3 | CPU: Intel 3770k @5.1Ghz | RAM: 32Gb G.Skill Ripjaws X @1600Mhz | Graphics card: EVGA 980 Ti SC | HDD: Seagate barracuda 3298534883327.74B + Samsung OEM 5400rpm drive + Seatgate barracude 2TB | PSU: Cougar CMX 1200w | CPU cooler: Custom loop

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, Hazy125 said:

Well, your server is probably already set up so that PHP and apache are basically already running as root. So you could just run exec('YourCommand'); and that would work perfectly fine.

Pros;

-Easy AF

-Extends PHP functionality beyond standard web use

 

Cons;

-Disabled in safe mode

-Someone who finds a security flaw in your PHP script may be able to completely destroy your server

 

So yeah, possible, but you have to be pretty damn sure your exec stuff is well protected

PHP was configured to run as www-data and has never been root so I can't do that :P

And my exec commands will be hardcoded with no dynamic content.

Back-end developer, electronics "hacker"

Link to comment
Share on other sites

Link to post
Share on other sites

12 hours ago, Joveice said:

PHP was configured to run as www-data and has never been root so I can't do that :P

And my exec commands will be hardcoded with no dynamic content.

I would use the symfony Process Component:

Quote

The Process component executes commands in sub-processes.

It's great for handling output, errors and so on.

https://symfony.com/doc/current/components/process.html 

 

But basically you are free to use exec(). If the commands are hard coded, there isn't much of a security risk...

Business Management Student @ University St. Gallen (Switzerland)

HomeServer: i7 4930k - GTX 1070ti - ASUS Rampage IV Gene - 32Gb Ram

Laptop: MacBook Pro Retina 15" 2018

Operating Systems (Virtualised using VMware): Windows Pro 10, Cent OS 7

Occupation: Software Engineer

Link to comment
Share on other sites

Link to post
Share on other sites

@Joveice You can give your webserver's user permission to only execute sudo commands in scripts in certain locations (IIRC that's defined in in /etc/sudoers.d). You can also only give it permission to alter the folders and their contents that you want it to be able to alter ( chown -R and chmod xxx ). You can also run the webserver under a system user which doesn't have a shell (set in /etc/passwd - means that you can't su -u webserver as a different user to use those sudoer permissions).

 

To make sure that nobody passes extra arguments to your scripts (service webserver restart&&rm -rf /) you can also whitelist the arguments permitted, which would be something like this:

 

#!/bin/bash
  
serviceName="webserver"
passedCommand=${1}
PERMITTED_ARGS=( "restart" "stop" "start" "status" "condrestart" )
  
for i in "${PERMITTED_ARGS[@]}"; do
  if [[ "${i}" = "${passedCommand}" ]]; then
    service ${servicename} ${passedCommand}
  fi 
done

 

Intel i7 5820K (4.5 GHz) | MSI X99A MPower | 32 GB Kingston HyperX Fury 2666MHz | Asus RoG STRIX GTX 1080ti OC | Samsung 951 m.2 nVME 512GB | Crucial MX200 1000GB | Western Digital Caviar Black 2000GB | Noctua NH-D15 | Fractal Define R5 | Seasonic 860 Platinum | Logitech G910 | Sennheiser 599 | Blue Yeti | Logitech G502

 

Nikon D500 | Nikon 300mm f/4 PF  | Nikon 200-500 f/5.6 | Nikon 50mm f/1.8 | Tamron 70-210 f/4 VCII | Sigma 10-20 f/3.5 | Nikon 17-55 f/2.8 | Tamron 90mm F2.8 SP Di VC USD Macro | Neewer 750II

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×