CentOS 6设置防跨站

2018-10-20 23:06:40 5241

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


$ 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://www.landui.com/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/baidu,com/.user.ini

$ chown -R root:root /home/wwwroot/baidu,com/.user.ini

$ chattr +i /home/wwwroot/baidu,com/.user.ini

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


提交成功!非常感谢您的反馈,我们会继续努力做到更好!

这条文档是否有帮助解决问题?

非常抱歉未能帮助到您。为了给您提供更好的服务,我们很需要您进一步的反馈信息:

在文档使用中是否遇到以下问题: