Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A QuizKindly log in to use this feature. We’ll take you to the login page automatically.
LoginGeneral Tech Bugs & Fixes 3 years ago
User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
Congratulations! You are on the path to giving unrestricted com/tag/root">root access to anyone who can make your nginx server run arbitrary com/tag/code">code. You had better be sure that every single CGI script and php page and anything else that might be used to execute arbitrary com/tag/code">code is secure.
Your C wrapper is equivalent to configuring sudo to allow nginx to run any command at all as com/tag/root">root.
DON'T do it like that.
Write individual shell script (or whatever) wrappers for specific commands and then grant sudo access only to those wrapper scripts. For example, /usr/local/sbin/restart-nginx.sh which does nothing but service nginx restart and give nginx sudo access to that script.
Then write another, completely separate script to run, say, dmidecom/tag/code">code -s system-uuid as in your previous question. And give nginx sudo access to that script too.
The simpler and less complicated each individual script, the better. Safest of all is to take no user input at all, not from the command-line and not from environment variables.
If some of your wrapper scripts must take user input, sanity check and sanitise all user-supplied input before using it. And quote your variables - e.g. always use "$variable" and never just $variablewithout quotes.
If your wrapper scripts are getting excessively long and complicated then try to identify just the minimum command or set of commands that need to be run as root and write them as a separate script (or scripts), which are called by sudo from the main script. i.e. run as little as possible as root.
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.
Kindly log in to use this feature. We’ll take you to the login page automatically.
Login
Ready to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
Your experience on this site will be improved by allowing cookies. Read Cookie Policy
manpreet
Best Answer
3 years ago
I am running a LEMP stack and wish to write a simple control panel for it.
So, I want to be able to restart
php-fpmfrom a php script. To achieve this, this is what I did.Created a binary wrapper in
clike this php-shell.c:This program was compiled like this:
I have then added nginx user to
sudo visudolike this:Then I executed the command in a php script like this:
var_dump(shell_exec('sudo /path/to/php_shell "service nginx restart" 2>&1'));As soon as I run this script php script, I get
502 Gateway Errorand it appears allphp-fpmprocesses has been killed off and it does not start back up.Any ideas? Am I doing this wrong? I want to be able to restart nginx server from php script by executing
service nginx restart. How can I achieve this?