查看: 4878|回复: 0

Discuz论坛安全加固

[复制链接]

38

主题

45

帖子

897

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
897
发表于 2018-4-9 10:43:08 | 显示全部楼层 |阅读模式
本帖最后由 HainaTec 于 2018-4-9 17:12 编辑

Discuz可以算是php里的一个比较优秀的bbs程序了,最近公司了增了discuz应用。不过在服务器上运行php程序相较java程序来说,安全性上总觉得不那么让人放心。果不其然,运行不久,网站就被人给黑了 。之前该机一直平安的运行jsp安全的运行了一年多没有问题 。当然,此处并不是说java较php优秀 。之前运行jsp程序之所以没有问题也有两方面的原因:其一,java本身所具有的相对安全的特性;其二,在运维和程序代码本身都做了很多的努力,防止sql 注入,上传漏洞等。所以相信php做了一种快速高效的开发语言,如果安全加固做的比较到位的话,其与java代码相较并不处于弱势。

针对discuz的安全加固如下:

1、在nginx入口上对data|images|config|static|source|template 这几个可以上传的目录里的php文件禁止访问 。(更安全一点就是列出放行的,其他全部禁止。)




  1. location ~* ^/(data|images|config|static|source|template)/.*.(php|php5)$
  2.    {
  3.    deny all;
  4.    }
复制代码


按照以上的配置,即使出现上传漏洞,上传到了上面配置的几个目录php文件,也会报403出错无法执行。更安全的做法是,放行部分,其余全部禁止,例如:

  1. location ~ (index|forum|group|archiver|api|uc_client|uc_server).*.(php)?$
  2.     {
  3.                 allow all;
  4.                 fastcgi_pass  127.0.0.1:9000;
  5.                 fastcgi_index index.php;
  6.                 include fcgi.conf;
  7. }
复制代码

不过上面只是一个示例,根目录下的php文件,我此处并未列全。而且还有一个问题就是,每次有新的版本发布,如果目录结构变列或者根目录新增php文件,都要相应的修改nginx 的安全配置。


2、优化nginx和php-fpm程序运行用户。例如,该用户为www,让www用户没有家目录,没有shell,无法登陆。nginx和php-fpm程序是通过root内部调用切换到 www用户。类似于mysql的启动。具体增加语句如下:


  1. useradd www -d /dev/null -s /sbin/nologin
复制代码

如果感觉还不够安全,可以再利用chroot和sudo等程序,限制www用户所能访问的目录和可以调用的命令。


3、目录权限控制。除了discuz下的data目录有写的权限,取消所有其他目录的写权限。

该步,我看到网上有列出执行的shell命令为:


  1. find source -type d -maxdepth 4 -exec chmod 555 {} ;
  2. find api -type d -maxdepth 4 -exec chmod 555 {} ;
  3. find static -type d -maxdepth 4 -exec chmod 555 {} ;
  4. find archive -type d -maxdepth 4 -exec chmod 555 {} ;
  5. find config -type d -maxdepth 4 -exec chmod 555 {} ;
  6. find data -type d -maxdepth 4 -exec chmod 755 {} ;
  7. find template -type d -maxdepth 4 -exec chmod 555 {} ;
  8. find uc_client -type d -maxdepth 4 -exec chmod 555 {} ;
复制代码


不过按这样的shell语句执行是有警告的,具体所报内容如下:


  1. 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.
复制代码

规范的写法是:


  1. find source -maxdepth 4 -type d -exec chmod 555 {} ;
  2. find api -maxdepth 4 -type d -exec chmod 555 {} ;
  3. find static -maxdepth 4 -type d -exec chmod 555 {} ;
  4. find archive -maxdepth 4 -type d -exec chmod 555 {} ;
  5. find config -maxdepth 4 -type d -exec chmod 555 {} ;
  6. find data -maxdepth 4 -type d -exec chmod 755 {} ;
  7. find template -maxdepth 4 -type d -exec chmod 555 {} ;
  8. find uc_client -maxdepth 4 -type d -exec chmod 555 {} ;
复制代码

注:上面的' -maxdepth 4 ' 也是可以取消的。

对目录设置完成后,还要对文件配置权限:


  1. find . -type f -exec chmod 444  {} ;
  2. #设置论坛目录的文件只可读,然后设置那些需要写的文件,一般只有data下的文件是可以的。
  3. find data -type f -exec chmod 755 {} ;
  4. #设置data 文件为755
复制代码

4、禁止php相关函数的调用及跨站。在php.ini文件中开启以下两项


  1. open_basedir = .:/tmp/
  2. 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
  3. assthru,stream_socket_server,escapeshellcmd,dll,popen,disk_free_space,checkdnsrr,checkdnsrr,getservbyname,getservbyport,disk_total_space,posix_ctermid,posix_get_last_error,posix
  4. _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
  5. 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
  6. _setuid,posix_strerror,posix_times,posix_ttyname,posix_uname
复制代码
以上主要是对系统层面的调用函数全部禁止 。


5、将该KVM机进行隔离。和其他主机无法连接。

该步骤配置是有条件的,在有KVM环境的条件,可以将各个应用之间利用KVM进行隔离。web访问模式为 物理机iptables nat ——KVM虚拟机 或 web入口——物理机iptables nat ——KVM虚拟机 。


6、数据库控制。

在mysql新建用户时,一是适当分配给用户所需的权限,二是要在创建用户时最好能限定来源IP 。例如:


  1. CREATE USER 'discuz'@'192.168.0.%' IDENTIFIED BY '66ZX811a';
  2. grant select,insert,update,delete,create,index,trigger,create temporary tables on discuz.* to 'discuz'@'192.168.0.%';
复制代码

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

 
QQ在线咨询
售前咨询热线
0473-6109550
售后服务热线
0473-2020208
快速回复 返回顶部 返回列表