針對 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.%';