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
PHP中的一些安全配置的更多相关文章
- Ubuntu 14.04中Elasticsearch集群配置
Ubuntu 14.04中Elasticsearch集群配置 前言:本文可用于elasticsearch集群搭建参考.细分为elasticsearch.yml配置和系统配置 达到的目的:各台机器配置成 ...
- angularjs中的directive scope配置
angularjs中的directive scope配置 定义directive其中重要的一环就是定义scope,scope有三种形式: 默认的scope,DOM元素上原有的scope scope: ...
- Production环境中iptables常用参数配置
production环境中iptables常用参数配置 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 我相信在实际生产环境中有很多运维的兄弟跟我一样,很少用到iptables的这个 ...
- apache中虚拟主机的配置
一.两种方式:基于域名的虚拟主机和基于IP地址的的虚拟主机 (这里基于前者) 二.作用:实现在同一个web服务器下,同时运行很多个站点(项目) 三.虚拟主机的配置 1.在核心配置文件中加载虚拟主机配置 ...
- Linux中vim的简单配置
本文主要分享Linux中vim的简单配置 ★配置文件的位置 在目录/etc.下面,有个名为vimrc的文件,这就是系统中公共的vim配置文件,对所有用户都开放.而在每个用户的主目录下,都可以自 ...
- Entity Framework 实体框架的形成之旅--Code First模式中使用 Fluent API 配置(6)
在前面的随笔<Entity Framework 实体框架的形成之旅--Code First的框架设计(5)>里介绍了基于Code First模式的实体框架的经验,这种方式自动处理出来的模式 ...
- Centos中jdk的环境配置
在Centos中,进行配置jdk的环境,这个还是折腾了我听挺久的.特别是,在一次配置中,导致后来我的root用户无法登录,并且用其他普通用户登录,使用su - root切换到root用户,都无法使用l ...
- XAMPP中proftpd的简明配置方法
XAMPP中proftpd的简明配置方法 用LAMPP的安装方法可以开一个默认的nobody用户,用lampp security就可以初始设置相应的默认用户密码.如果要有多用户,又怎样管理.目录怎 ...
- Tools下的mdscongiguer 文件中 43行 oracle 配置 发现需要连接库 -lclntsh libclntsh.so 库是个什么东西呢?
Tools下的mdscongiguer 文件中 43行 oracle 配置 发现需要连接库 -lclntsh libclntsh.so 库是个什么东西呢? 分想一个知乎网 ...
- VMware中安装CentOS7网络配置静态IP地址,常用配置和工具安装
VMware中安装CentOS7网络配置静态IP地址,常用配置和工具安装在阿里云开源镜像地址下载镜像Index of /centos/7.2.1511/isos/x86_64/http://mirro ...
随机推荐
- unity3d摄像机参数
1. Clear Flags:清除标记.决定屏幕的哪部分将被清除.一般用户使用对台摄像机来描绘不同游戏对象的情况,有3中模式选择: Skybox:天空盒.默认模式.在屏幕中的空白部分将显示当前摄像机的 ...
- [ IOS ] iOS-控制器View的创建和生命周期
reference to : 1. 控制器View的创建 首先我们来看一下控制器view创建的流程图 控制器view加载.jpeg 从图中我们可以看出,在控制器view加载过程中有两个重要的方法lo ...
- SQL调用WebService接口
今天在做一个非常奇葩的东西.中间有个过程要在SQL触发器里面调用webservice接口.呵呵~ ALTER TRIGGER tgr_UpdateMemcached ON dbo.[User] AFT ...
- 掌握Sed命令
带书签PDF版,喜欢的话,欢迎多提意见和建议,你的支持是血蝙蝠最大的前进动力! http://download.csdn.net/detail/challenge_c_plusplus/6480007 ...
- assets raw 资源 AssetManager
assets raw 目录简介 assets核心特性:不会被编译成二进制,支持子目录(可以分类,这是相对raw目录最大的好处),通过文件名访问,调用getAssets通过AssetManager访问 ...
- 直播【95秀】JNI 基本实现 简洁
2017-2-8 基本架构 1.使用SurfaceView在UI层面展示视频 2.通过JNI调用C代码控制视频的播放.停止 基本功能 1.从服务器获取正在直播的主播的列表信息 2.进入直播界面 3.可 ...
- 关于ListView中getView被重复调用的问题
我用ListView显示数据时,自定义了一个适配器(extends ArrayAdapter),然后重写了getView方法,现在出现一个问题,就是这个getView()方法被重复调用了,比如我的_d ...
- python 斐波拉契数列数列
'''斐波拉契数列'''def Fibonacci(n): first, next = 0, 1 i = 0; while i < n: print next first, next = nex ...
- javascript进行遍历
javascript进行遍历 <!doctype html> <html lang="en"> <head> <meta charset= ...
- Memcached--分布式缓存安装教程
Memcached的Windows版本在这里下载http://code.google.com/p/memcached/wiki/PlatformWindows(或http://memcachedpro ...