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