Read the entire post before trying it; there is a choice.
Binary shell solution (with suid bit)
1) Create a script (preferably .sh ) that contains what you want to run as root.
# cat > php_shell.sh <<CONTENT #!/bin/sh /sbin/service sshd restart CONTENT
2) This file must be owned by root, and since it will work as root, make sure that only root has write permission to the file.
# chown root php_shell.sh # chmod u=rwx,go=xr php_shell.sh
3) To run the script as root, no matter which user runs it, we need a binary shell. Create the one that runs our php_shell.sh .
# cat > wrapper.c <<CONTENT #include <stdlib.h> #include <sys/types.h> #include <unistd.h> int main (int argc, char *argv[]) { setuid (0); /* WARNING: Only use an absolute path to the script to execute, * a malicious user might fool the binary and execute * arbitary commands if not. * */ system ("/bin/sh /path/to/php_shell.sh"); return 0; } CONTENT
4) Compile and set the correct permissions, including the suid bit (saying that it should run with root privileges):
# gcc wrapper.c -o php_root # chown root php_root # chmod u=rwx,go=xr,+s php_root
php_root will now run as root and execute the commands specified in php_root.sh .
If you do not need to easily change which commands will be executed, I would recommend that you write the commands directly in wrapper.c under step 4 . Then you do not need to have a binary file executing an external script executing the corresponding commands.
In wrapper.c use system ("your shell command here"); to indicate which commands you want to execute.
Filip Roséen - refp Dec 16 2018-11-11T00: 00Z
source share