灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2498回复:0

Discuz论坛安全设置防黑

楼主#
更多 发布于:2013-07-25 12:17
 Discuz可以算是php里的一个比较优秀的bbs程序了,最近公司了增了discuz应用。不过在服务器上运行php程序相较java程序来说,安全性上总觉得不那么让人放心。果不其然,运行不久,网站就被人给黑了 。之前该机一直平安的运行jsp安全的运行了一年多没有问题 。当然,此处并不是说java较php优秀 。之前运行jsp程序之所以没有问题也有两方面的原因:其一,java本身所具有的相对安全的特性;其二,在运维和程序代码本身都做了很多的努力,防止sql 注入,上传漏洞等。所以相信php做了一种快速高效的开发语言,如果安全加固做的比较到位的话,其与java代码相较并不处于弱势。
 
针对discuz的安全加固如下:
 
1、在nginx入口上对data|images|config|static|source|template 这几个可以上传的目录里的php文件禁止访问 。(更安全一点就是列出放行的,其他全部禁止。)
 
location ~* ^/(data|images|config|static|source|template)/.*\.(php|php5)$
   {
   deny all;
   }
按照以上的配置,即使出现上传漏洞,上传到了上面配置的几个目录php文件,也会报403出错无法执行。更安全的做法是,放行部分,其余全部禁止,例如:
 
location ~ (index|forum|group|archiver|api|uc_client|uc_server).*\.(php)?$
    {
                allow all;
                fastcgi_pass  127.0.0.1:9000;
                fastcgi_index index.php;
                include fcgi.conf;
}
不过上面只是一个示例,根目录下的php文件,我此处并未列全。而且还有一个问题就是,每次有新的版本发布,如果目录结构变列或者根目录新增php文件,都要相应的修改nginx 的安全配置。
 
2、优化nginx和php-fpm程序运行用户。例如,该用户为www,让www用户没有家目录,没有shell,无法登陆。nginx和php-fpm程序是通过root内部调用切换到 www用户。类似于mysql的启动。具体增加语句如下:
 
useradd www -d /dev/null -s /sbin/nologin
如果感觉还不够安全,可以再利用chroot和sudo等程序,限制www用户所能访问的目录和可以调用的命令。
 
3、目录权限控制。除了discuz下的data目录有写的权限,取消所有其他目录的写权限。
 
该步,我看到网上有列出执行的shell命令为:
 
find source -type d -maxdepth 4 -exec chmod 555 {} \;
find api -type d -maxdepth 4 -exec chmod 555 {} \;
find static -type d -maxdepth 4 -exec chmod 555 {} \;
find archive -type d -maxdepth 4 -exec chmod 555 {} \;
find config -type d -maxdepth 4 -exec chmod 555 {} \;
find data -type d -maxdepth 4 -exec chmod 755 {} \;
find template -type d -maxdepth 4 -exec chmod 555 {} \;
find uc_client -type d -maxdepth 4 -exec chmod 555 {} \;
不过按这样的shell语句执行是有警告的,具体所报内容如下:
 
find: warning: you have specified the -maxdepth option after a non-option argument -type, but options are not positional (-maxdepth affects tests specified before it as well as those specified after it).  Please specify options before other arguments.
规范的写法是:
 
find source -maxdepth 4 -type d -exec chmod 555 {} \;
find api -maxdepth 4 -type d -exec chmod 555 {} \;
find static -maxdepth 4 -type d -exec chmod 555 {} \;
find archive -maxdepth 4 -type d -exec chmod 555 {} \;
find config -maxdepth 4 -type d -exec chmod 555 {} \;
find data -maxdepth 4 -type d -exec chmod 755 {} \;
find template -maxdepth 4 -type d -exec chmod 555 {} \;
find uc_client -maxdepth 4 -type d -exec chmod 555 {} \;
注:上面的' -maxdepth 4 ' 也是可以取消的。
 
对目录设置完成后,还要对文件配置权限:
 
find . -type f -exec chmod 444  {} \;
#设置论坛目录的文件只可读,然后设置那些需要写的文件,一般只有data下的文件是可以的。
find data -type f -exec chmod 755 {} \;
#设置data 文件为755
4、禁止php相关函数的调用及跨站。在php.ini文件中开启以下两项
 
open_basedir = .:/tmp/
disable_functions = passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status,ini_alter,ini_alter,ini_restore,dl,openlog,syslog,readlink,symlink,popep
assthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix
_getcwd, posix_getegid,posix_geteuid,posix_getgid, posix_getgrgid,posix_getgrnam,posix_getgroups,posix_getlogin,posix_getpgid,posix_getpgrp,posix_getpid, posix_getppid,posix_get
pwnam,posix_getpwuid, posix_getrlimit, posix_getsid,posix_getuid,posix_isatty, posix_kill,posix_mkfifo,posix_setegid,posix_seteuid,posix_setgid, posix_setpgid,posix_setsid,posix
_setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
以上主要是对系统层面的调用函数全部禁止 。
 
5、将该KVM机进行隔离。和其他主机无法连接。
 
该步骤配置是有条件的,在有KVM环境的条件,可以将各个应用之间利用KVM进行隔离。web访问模式为 物理机iptables nat ——KVM虚拟机 或 web入口——物理机iptables nat ——KVM虚拟机 。
 
6、数据库控制。
 
在mysql新建用户时,一是适当分配给用户所需的权限,二是要在创建用户时最好能限定来源IP 。例如:
 
CREATE USER 'discuz'@'192.168.0.%' IDENTIFIED BY '66ZX811a';
grant select,insert,update,delete,create,index,trigger,create temporary tables on discuz.* to 'discuz'@'192.168.0.%';

喜欢0 评分0
游客

返回顶部