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然后用下面的替換 復制代碼 代碼如下: 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: 復制代碼 代碼如下: # 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中設置方法,在c:/windows/php.ini星外的設置:復制代碼 代碼如下: disable_functions =exec,system,passthru,popen,pclose,shell_exec,proc_open,dl,chmod,gzinflate,set_time_limit 建議增加phpinfo等,可以參考上面的設置,以后在使用過程中可能會出現php不支持部分功能的現象,然后大家可以搜索下錯誤提示,去掉相應的函數即可。 支持的越多越不安全,對于采集程序來說需要去掉curl_exec,大家多測試即可。下面提供一個更完整的版本復制代碼 代碼如下: disable_functions =phpinfo,exec,system,passthru,popen,pclose,shell_exec,proc_open,dl,curl_exec,multi_exec,chmod,gzinflate,set_time_limit, iis中設置后,運行中輸入 iisreset /restart即可。注意下面的突破方法:建議打開安全模式 PHP是一款功能強大應用廣泛的腳本語言,很大一部分網站都是使用PHP架構的。因為其提供了強大的文件操作功能和與系統交互的功能,所以大部分的服務器都對PHP做了嚴格的限制,包括使用open_basedir限制可以操作的目錄以及使用disable_functions限制程序使用一些可以直接執行系統命令的函數如system,exec,passthru,shell_exec,proc_open等等。但是如果服務器沒有對dl()函數做限制,一樣可以利用dl()函數饒過這些限制。 dl()函數允許在php腳本里動態加載php模塊,默認是加載extension_dir目錄里的擴展,該選項是PHP_INI_SYSTEM范圍可修改的,只能在php.ini或者apache主配置文件里修改。當然,你也可以通過enable_dl選項來關閉動態加載功能,而這個選項默認為On的,事實上也很少人注意到這個。dl()函數在設計時存在安全漏洞,可以用../這種目錄遍歷的方式指定加載任何一個目錄里的so等擴展文件,extension_dir限制可以被隨意饒過。所以我們可以上傳自己的so文件,并且用dl函數加載這個so文件然后利用so文件里的函數執行其他操作,包括系統命令。