Editing system files with higher permissions and PHP

I am trying to create a very simple web interface for changing some system settings on a network device. I am new to PHP and am getting a painful realization of how easy it is to do this in hacker ways, so I try to write everything based on best practices.

However, what is best for editing files belonging to the root / some other account? Will the creation of a secure shell / perl / whatever script run by PHP? Using setuid is another option that has come up, but it has no way to limit users.

I hope this was not too vague, let me know if you need more details, and I will be happy to share.

Additional information: just for clarification - by editing system files, I mean specifically ifcfg and some confidential licensing information. Therefore, for simplicity, we just say a simple web interface to change the ip / subnet / gateway / dns settings on a Linux-based network device.

+4
source share
3 answers

Few things. Make a backup first. Then write to a temporary file. Then check this file (reread it to make sure that it is syntactically correct and means that you think it does). Only then, a MOVE ( mv or rename() ) file on top of the original. That way, you can never get stuck in a position when another process is trying to read while you are still writing, or a write failure causes a syntax error, etc.

Edit:

There are several things you can do to escalate permissions.

One could write a script (shell) to check the file and perform the move. Then you can install this file. So you are writing a temporary file using PHP, checking it against PHP (in the end, you can never check too much). Then call the script to move the temporary file to the desired position (with elevated privileges).

Another would be to add an account with write permission only to these files (either via sudo, or normally). Then use PHP for su newuser -c "mv tmpfile finalfile" . You would have to disable authentication, but this is better than running PHP as consistent ...

Another option is to use the SSH extension for ssh in the field (using the private key), upload the file and copy it to the final destination.

But somehow you do it, if PHP is hacked, they have access to these files, since PHP has a way ...

+2
source

Not

This is the best practice.

Cause? You are new to PHP. This first of all leaves everything that you write very suspiciously to a lot of pitfalls.

Your question was a bit vague. Develop a little more about what you are trying to change, why you need to change it, who controls it, what is the scope, etc., And more effective recommendations can be made.

Some things to keep in mind are easily accessible system backups. Always make copies of files before editing them. Do not write directly to the file. Copy the existing file to the backup file and the temporary file. Edit the temporary file and then move it (rename) to the original file name. This simplifies recovery (if necessary) and prevents the file from freezing during recording if it does not work.

+3
source

I would use a shell script to edit as you say, and just use PHP as a web interface. That way, you can also use the script yourself from the command line if you need to (perhaps a cron job or something else). The shell script could be just as easily written in PHP CLI as perl, python or bash, so the solution could be completely PHP if you want.

Josh K and ircmaxell also make very good backups, etc.

0
source

All Articles