Abstract: In this article, let's discuss a lot of tips and considerations for building a basic server supervision engine based on PHP language, and provide a complete source code implementation. I. change the title of the working directory. when you write a supervisor, Summary: In this article, let's discuss a lot of tips and considerations for building a basic server supervision engine based on the PHP language, and provide a complete source code implementation.
I. change the title of the working directory
When you write a supervisor program, it is usually better to set its own working directory. In this case, if you use a relative path to read and write files, it will take the initiative to process the user's status as needed. It is always a good practice to restrict the application path in the program, but it is not as flexible as it is. Therefore, the safest way to change your working directory is to apply both chdir () and chroot ().
Chroot () can be used in CLI and CGI versions of PHP, but requests the program to run with root permissions. Chroot () actually changes the path of the current process from the root directory to the specified directory. This allows the current process to only perform files in this directory. Usually, chroot () is used by the server as a 'security device' application to ensure that malicious code does not correct files outside a specific directory. Keep in mind that although chroot () can prohibit you from visiting any files outside your new directory, any currently opened file resources can still be accessed. For example, the following code can open a log file, call chroot (), and switch to a data directory. then, you can log on successfully and then open the file resources:
<? Php
$ Logfile = fopen ('/var/log/chroot. log', 'w ');
Chroot ('/Users/george ');
Fputs ($ logfile, 'Hello From Inside The Chroot \ n ');
?>
If a Exploitation program cannot apply chroot (), you can call chdir () to set the working directory. For example, this is useful when the code needs to load specific code that can be located anywhere in the system. Note that chdir () does not provide a security mechanism to prevent unauthorized files from being opened.
2. give up privileges
When writing Unix daemon, a classic security precaution is to give up all unnecessary privileges; otherwise, having unnecessary privileges easily leads to unnecessary troubles. When the code (or PHP itself) contains vulnerabilities, you can minimize the loss by ensuring that a daemon runs as a minimum-privilege user.
One way to achieve this is to perform the daemon as a non-privileged user. However, if the program needs to open resources (such as log files, data files, sockets, and so on) that are not authorized to open by non-authorized users at the beginning, this is usually not enough.
If you run as the root user, you can discard your privileges by using the posix_setuid () and posiz_setgid () functions. The following example converts the privileges of the current running program to those permissions owned by the user nobody:
$ Pw = posix_getpwnam ('nobody ');
Posix_setuid ($ pw ['uid']);
Posix_setgid ($ pw ['gid']);
Like chroot (), any privileged resource opened before giving up the privilege will remain open, but new resources cannot be created.
3. ensure exclusion
You may often want to achieve this: a script runs only one instance at any time. This is especially important to protect scripts. multiple instances can be called by chance when running in the background.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service