Press "Enter" to skip to content

CentOS 6设置防跨站

利用open_basedir将PHP脚本的操作限定在某一个目录内,可以防止跨站攻击。本文基于CentOS 6,PHP 5.5.5版本。假设我们有一个网站位于/home/wwwroot/zhukun.net。

$ vim /usr/local/php/etc/php.ini    #保证php.ini文件里的相关设定未被修改
; open_basedir, if set, limits all file operations to the defined directory
; and below.  This directive makes most sense if used in a per-directory
; or per-virtualhost web server configuration file. This directive is
; *NOT* affected by whether Safe Mode is turned On or Off.
; http://php.net/open-basedir
;open_basedir =        #这一项保持默认即可
......
; 建议关闭的函数
disable_functions = passthru,exec,system,chroot,chgrp,chown,shell_exec,proc_open,proc_get_status,popen,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popepassthru,stream_socket_server
......
; 确保如下项保持默认
;;;;;;;;;;;;;;;;;;;;
; php.ini Options  ;
;;;;;;;;;;;;;;;;;;;;
; Name for user-defined php.ini (.htaccess) files. Default is ".user.ini"
;user_ini.filename = ".user.ini"

; To disable this feature set this option to empty value
;user_ini.filename =

; TTL for user-defined php.ini files (time-to-live) in seconds. Default is 300 seconds (5 minutes)
;user_ini.cache_ttl = 300
$ vim /home/wwwroot/zhukun.net/.user.ini    #写入如下一行
open_basedir=/home/wwwroot/zhukun.net:/tmp/:/proc/

#相关安全权限设定
$ chmod 644 /home/wwwroot/zhukun.net/.user.ini
$ chown -R root:root /home/wwwroot/zhukun.net/.user.ini
$ chattr +i /home/wwwroot/zhukun.net/.user.ini

很多人觉得,我设定的open_basedir是否有效?很简单,只要将open_basedir后面的路径改一下,重启一下PHP服务,然后观察网站是否还能正常访问即可。

Leave a Reply

Your email address will not be published. Required fields are marked *