PHP中的一些安全配置

PHP中的配置至关重要,包含php.ini的配置,还有系统权限的配置,一下是我总结的一些配置

一、PHP的模块

./configure    
--with-libdir=lib64     
--prefix=/usr/   
--exec-prefix=/usr    
--bindir=/usr/bin    
--sbindir=/usr/sbin    
--sysconfdir=/etc    
--datadir=/usr/share    
--includedir=/usr/include    
--libexecdir=/usr/libexec    
--localstatedir=/var    
--sharedstatedir=/usr/com    
--mandir=/usr/share/man    
--infodir=/usr/share/info    
--cache-file=../config.cache    
--with-config-file-path=/etc    
--with-config-file-scan-dir=/etc/php.d     
--with-mysql=/usr/local/mysql   
--with-mysqli=/usr/local/mysql/bin/mysql_config   
--with-iconv-dir   
--with-freetype-dir=/usr/local/lib   
--with-jpeg-dir=/usr/local/lib   
--with-png-dir=/usr/local/lib   
--with-zlib   
--with-libxml-dir=/usr   
--enable-xml   
--disable-rpath   
--enable-bcmath   
--enable-shmop   
--enable-sysvsem   
--enable-inline-optimization   
--with-curl   
--enable-mbregex   
--enable-fpm   
--enable-mbstring   
--with-mcrypt=/usr/local/lib   
--with-gd   
--enable-gd-native-ttf   
--with-openssl   
--with-mhash   
--enable-pcntl   
--enable-sockets   
--with-xmlrpc   
--enable-zip   
--enable-soap   
--enable-opcache   
--with-pdo-mysql   
--enable-embed=shared   
--enable-debug   
--enable-dtrace 

 上面是一些比较常用的配置选项

[root@localhost]# php -m
[PHP Modules]
bcmath
Core
ctype
curl
date
dom
ereg
fetch_url
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mcrypt
memcached
mhash
mongo
mysql
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvsem
tokenizer
trace
vld
xhprof
xml
xmlreader
xmlrpc
xmlwriter
Zend OPcache
zip
zlib

[Zend Modules]
Zend OPcache

 我们可以通过php -m 来查看,

1、有些不需要的模块,在我们编译的时候就不要启用了

2、有些默认启用的模块,在我们编译的时候要禁用

二、防止PHP版本泄漏

可以通过expose_php来关闭

[root@localhost ~]# curl -I  192.168.1.30
HTTP/1.1 200 OK
Server: nginx
Date: Wed, 15 Jul 2015 19:16:10 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Vary: Accept-Encoding
X-Powered-By: PHP/5.5.23
[root@localhost ~]# vim /etc/php.ini
expose_php = Off

服务器的版本信息也更改,在编译nginx之前可以将nginx修改为apapche

三、记录PHP和Nginx的日志

display_errors = Off
display_startup_errors = On
log_errors = On
error_reporting = 0
error_log = /var/log/php_errors.log

另外还要记录apache或者Nginx的访问日志,这个要在web server中配置

四、限制文件上传

web程序最不安全的就在这个地方了,用户可以上传文件,比如图片、脚本,然后再通过其他方式,对网站做一些破坏性的工作,所以上传的时候,要严格检查文件的格式

file_uploads = On
upload_tmp_dir =/data/www/tmp
upload_max_filesize = 2M
max_file_uploads = 20

 五、关闭远程资源的访问

如果这个特性被启动,将会禁用file_get_contents()、include、require中获取诸如FTP或网页内容这些远程数据

 allow_url_fopen=Off

 六、POST的限制

post_max_size=2m

 七、dos控制

最大执行时间、用于处理请求数据的最大时间、以及最大可用内存数、防止hash构造

max_execution_time = 30
max_input_time = 30
memory_limit = 40M
max_input_vars = 1000

 八、权限以及安全模式的配置

[root@localhost ~]# vim /etc/php.ini
disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source
open_basedir=/var/www/html/
safe_mode_exec_dir = /usr/local/bin/

#确认以Apache或www这种非root用户运行Apache。/var/www/html目录下的owner也应是非root用户:
[root@localhost ~]# chown -R apache:apache /var/www/html/

#DocumentRoot下的文件应禁止运行或创建。设置该目录下的文件权限为0444(只读):
[root@localhost ~]# chmod -R 0444 /var/www/html/

#设置该目录下的所有文件夹权限为0445
[root@localhost ~]# find /var/www/html/ -type d -print0 | xargs -0 -I {} chmod 0445 {}

#配置文件加上写保护
[root@localhost ~]# chattr +i /etc/php.ini /etc/php.d/* /etc/my.ini /etc/httpd/conf/httpd.conf

 九、使用防火墙限制传出连接

攻击者会使用wget之类的工具从你的Web服务器下载文件。使用iptables来阻挡Apache用户的传出连接。ipt_owner模块会为本地数据包的生成者分配不同角色。它只对OUTPUT chain有效。下面指令允许vivek用户通过80端口进行外部访问

/sbin/iptables -A OUTPUT -o eth0 -m owner --uid-owner vivek -p tcp --dport 80 -m state --state NEW,ESTABLISHED  -j ACCEPT

/sbin/iptables --new-chain apache_user
/sbin/iptables --append OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
/sbin/iptables --append OUTPUT -m owner --uid-owner apache -j apache_user

# allow apache user to connec to our smtp server 
/sbin/iptables --append apache_user -p tcp --syn -d 192.168.1.100 --dport 25 -j RETURN

# Allow apache user to connec to api server for spam validation
/sbin/iptables --append apache_user -p tcp --syn -d  66.135.58.62 --dport 80 -j RETURN
/sbin/iptables --append apache_user -p tcp --syn -d  66.135.58.61 --dport 80 -j RETURN
/sbin/iptables --append apache_user -p tcp --syn -d  72.233.69.89 --dport 80 -j RETURN
/sbin/iptables --append apache_user -p tcp --syn -d  72.233.69.88 --dport 80 -j RETURN

#########################

## Add more rules here ##

#########################

# No editing below

# Drop everything for apache outgoing connection
/sbin/iptables --append apache_user -j REJECT

十、Auditing log

apache/nginx log

[root@localhost ~]# tail -f /var/log/httpd/error_log
[root@localhost ~]# grep 'login.php' /var/log/httpd/error_log
[root@localhost ~]# egrep -i "denied|error|warn" /var/log/httpd/error_log

php log

[root@localhost ~]# tail -f /var/log/httpd/php_scripts_error.log
[root@localhost ~]# grep "...etc/passwd" /var/log/httpd/php_scripts_error.log

php backdoors

[root@localhost ~]# grep -iR 'c99' /var/www/html/
[root@localhost ~]# grep -iR 'r57' /var/www/html/
[root@localhost ~]# find /var/www/html/ -name *.php -type f -print0 | xargs -0 grep c99
[root@localhost ~]# grep -RPn "(passthru|shell_exec|system|base64_decode|fopen|fclose|eval)" /var/www/html/

 

其他的有些安全配置

1、安装Mod_security

ModSecurity是一个开源的入侵检测和防范的Web应用引擎。安装mod_security可以保护Apache和PHP应用免受XSS和其他攻击

2、安装防DDOS模块mod_evasive

可以限制在一定时间内的访问频率

英文原文

http://www.cyberciti.biz/tips/php-security-best-practices-tutorial.html

原文地址:https://www.cnblogs.com/chenpingzhao/p/4653023.html