“安全配置”
#XOOPS安全配置
保护您的 XOOPS 安装免受常见 Web 漏洞影响的综合指南。
在启动您的网站之前,请实施以下安全措施:
- 文件权限设置正确 (644/755)
- 安装文件夹已删除或受保护
- 主文件。php 免受修改
- SSL/HTTPS 在所有页面上启用
- 管理文件夹已重命名或受保护
- 非网络敏感文件-accessible
- .htaccess 限制到位
- 自动定期备份
- 配置安全标头
- CSRF 保护已启用
- SQL注入保护激活
- Modules/extensions 已更新
文件系统安全
Section titled “文件系统安全”适当的文件权限对于安全至关重要。
| 路径 | 权限 | 业主 | 原因 |
|---|---|---|---|
| 主文件.php | 644 | 644根 | 包含数据库凭据 |
| *.php 文件 | 644 | 644根 | 防止未经授权的修改 |
| 目录 | 755 | 755根 | 允许读取,阻止写入 |
| 缓存/ | 777 | 777 www-data | Web服务器必须写 |
| 模板_c/ | 777 | 777 www-data | 编译模板 |
| 上传/ | 777 | 777 www-data | 用户上传 |
| 变量/ | 777 | 777 www-data | 可变数据 |
| 安装/ | 删除 | - | 安装后删除 |
| 配置/ | 755 | 755根 | 可读,不可写 |
设置权限脚本
Section titled “设置权限脚本”#!/bin/bashXOOPS_PATH="/var/www/html/xoops"WEB_USER="www-data"
# Set ownershipecho "Setting ownership..."chown -R $WEB_USER:$WEB_USER $XOOPS_PATH
# Set restrictive default permissionsecho "Setting base permissions..."find $XOOPS_PATH -type d -exec chmod 755 {} \;find $XOOPS_PATH -type f -exec chmod 644 {} \;
# Make specific directories writableecho "Setting writable directories..."chmod 777 $XOOPS_PATH/cachechmod 777 $XOOPS_PATH/templates_cchmod 777 $XOOPS_PATH/uploadschmod 777 $XOOPS_PATH/var
# Protect sensitive filesecho "Protecting sensitive files..."chmod 644 $XOOPS_PATH/mainfile.phpchmod 444 $XOOPS_PATH/mainfile.php.dist # If it exists (read-only)
# Verify permissionsecho "Verifying permissions..."ls -la $XOOPS_PATH | grep -E "mainfile|cache|uploads|var|templates_c"
echo "Security hardening completed!"运行脚本:
chmod +x /usr/local/bin/xoops-secure.sh/usr/local/bin/xoops-secure.sh删除安装文件夹
Section titled “删除安装文件夹”CRITICAL: 安装后必须删除安装文件夹!
# Option 1: Delete completelyrm -rf /var/www/html/xoops/install/
# Option 2: Rename and keep for referencemv /var/www/html/xoops/install/ /var/www/html/xoops/install.bak/
# Verify removalls -la /var/www/html/xoops/ | grep install保护敏感目录
Section titled “保护敏感目录”创建 .htaccess 文件以阻止对敏感文件夹的 Web 访问:
文件:/var/www/html/XOOPS/var/.htaccess
<FilesMatch "\.(php|phtml|php3|php4|php5|php6|php7|phps|pht|phar)$"> Deny from all</FilesMatch>
<IfModule mod_autoindex.c> Options -Indexes</IfModule>文件:/var/www/html/XOOPS/templates_c/.htaccess
<FilesMatch "\.(php|phtml|php3|php4|php5|php6|php7|phps|pht|phar)$"> Deny from all</FilesMatch>
Options -Indexes文件:/var/www/html/XOOPS/cache/.htaccess
Options -Indexes<FilesMatch "\.(php|phtml|php3|php4|php5|php6|php7)$"> Deny from all</FilesMatch>保护上传目录
Section titled “保护上传目录”防止在上传中执行脚本:
文件:/var/www/html/XOOPS/uploads/.htaccess
# Prevent script execution<FilesMatch "\.(php|phtml|php3|php4|php5|php6|php7|phps|pht|phar|pl|py|jsp|asp|aspx|cgi|sh|bat|exe)$"> Deny from all</FilesMatch>
# Prevent directory listingOptions -Indexes
# Additional protection<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /xoops/uploads/
# Block suspicious files RewriteCond %{REQUEST_URI} \.(php|phtml|php3|php4|php5|php6|php7)$ [NC] RewriteRule ^.*$ - [F,L]</IfModule>SSL/HTTPS 配置
Section titled “SSL/HTTPS 配置”加密用户和服务器之间的所有流量。
获得SSL证书
Section titled “获得SSL证书”选项 1:Let’s Encrypt 的免费证书
# Install Certbotapt-get install certbot python3-certbot-apache
# Obtain certificate (auto-configures Apache)certbot certonly --apache -d your-domain.com -d www.your-domain.com
# Verify certificate installedls /etc/letsencrypt/live/your-domain.com/选项 2:商业 SSL 证书
联系SSL提供商或注册商:
- 购买SSL证书
- 验证域名所有权 3.在服务器上安装证书文件 4.配置网络服务器
Apache SSL 配置
Section titled “Apache SSL 配置”创建HTTPS虚拟主机:
文件:/etc/apache2/sites-available/XOOPS-ssl.conf
<VirtualHost *:443> ServerName your-domain.com ServerAlias www.your-domain.com DocumentRoot /var/www/html/xoops
# SSL Configuration SSLEngine on SSLProtocol TLSv1.2 TLSv1.3 SSLCipherSuite HIGH:!aNULL:!MD5 SSLCertificateFile /etc/letsencrypt/live/your-domain.com/cert.pem SSLCertificateKeyFile /etc/letsencrypt/live/your-domain.com/privkey.pem SSLCertificateChainFile /etc/letsencrypt/live/your-domain.com/chain.pem
# Security Headers Header always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" Header always set X-Content-Type-Options "nosniff" Header always set X-Frame-Options "SAMEORIGIN" Header always set X-XSS-Protection "1; mode=block" Header always set Referrer-Policy "no-referrer-when-downgrade" Header always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'"
<Directory /var/www/html/xoops> Options -Indexes +FollowSymLinks AllowOverride All Require all granted </Directory>
# Restrict install folder <Directory /var/www/html/xoops/install> Deny from all </Directory>
# Logging ErrorLog ${APACHE_LOG_DIR}/xoops_ssl_error.log CustomLog ${APACHE_LOG_DIR}/xoops_ssl_access.log combined</VirtualHost>
# Redirect HTTP to HTTPS<VirtualHost *:80> ServerName your-domain.com ServerAlias www.your-domain.com Redirect 301 / https://your-domain.com/</VirtualHost>启用配置:
# Enable SSL modulea2enmod ssl
# Enable sitea2ensite xoops-ssl
# Disable non-SSL site if existsa2dissite 000-default
# Test configurationapache2ctl configtest# Should output: Syntax OK
# Restart Apachesystemctl restart apache2Nginx SSL 配置
Section titled “Nginx SSL 配置”文件:/etc/nginx/sites-available/XOOPS
# HTTP to HTTPS redirectserver { listen 80; listen [::]:80; server_name your-domain.com www.your-domain.com;
location / { return 301 https://$server_name$request_uri; }}
# HTTPS serverserver { listen 443 ssl http2; listen [::]:443 ssl http2;
server_name your-domain.com www.your-domain.com; root /var/www/html/xoops; index index.php index.html;
# SSL Certificate Configuration ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem; ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;
# Modern SSL Configuration ssl_protocols TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ssl_session_cache shared:SSL:10m; ssl_session_timeout 10m;
# HSTS Header add_header Strict-Transport-Security "max-age=31536000; includeSubDomains; preload" always;
# Security Headers add_header X-Content-Type-Options "nosniff" always; add_header X-Frame-Options "SAMEORIGIN" always; add_header X-XSS-Protection "1; mode=block" always; add_header Referrer-Policy "no-referrer-when-downgrade" always; add_header Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'" always;
# Restrict install folder location ~ ^/(install|upgrade)/ { deny all; }
# Deny access to sensitive files location ~ /\. { deny all; }
# PHP-FPM backend location ~ \.php$ { fastcgi_pass unix:/run/php-fpm.sock; fastcgi_index index.php; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }
# Static files caching location ~* \.(js|css|png|jpg|gif|ico|svg)$ { expires 30d; add_header Cache-Control "public, immutable"; }
# URL rewriting location / { try_files $uri $uri/ /index.php?$query_string; }
# Logging access_log /var/log/nginx/xoops_access.log; error_log /var/log/nginx/xoops_error.log;}启用配置:
ln -s /etc/nginx/sites-available/xoops /etc/nginx/sites-enabled/nginx -tsystemctl restart nginx验证HTTPS安装
Section titled “验证HTTPS安装”# Test SSL configurationopenssl s_client -connect your-domain.com:443 -tls1_2
# Check certificate validityopenssl x509 -in /etc/letsencrypt/live/your-domain.com/cert.pem -noout -text
# SSL/TLS test online# https://www.ssllabs.com/ssltest/# https://www.testssl.sh/Auto-Renew 让我们加密证书
Section titled “Auto-Renew 让我们加密证书”# Enable auto-renewalsystemctl enable certbot.timersystemctl start certbot.timer
# Test renewal processcertbot renew --dry-run
# Manual renewal if neededcertbot renew --force-renewalWeb 应用程序安全
Section titled “Web 应用程序安全”防止SQL注射
Section titled “防止SQL注射”XOOPS 使用参数化查询(默认情况下安全),但始终:
// UNSAFE - Never do this!$query = "SELECT * FROM users WHERE name = '" . $_GET['name'] . "'";
// SAFE - Use prepared statements$database = XoopsDatabaseFactory::getDatabaseConnection();$sql = "SELECT * FROM " . $database->prefix('users') . " WHERE name = ?";$result = $database->query($sql, array($_GET['name']));交叉-Site脚本(XSS)预防
Section titled “交叉-Site脚本(XSS)预防”始终清理用户输入:
// UNSAFEecho $_GET['user_input'];
// SAFE - Use XOOPS sanitization functionsecho htmlspecialchars($_GET['user_input'], ENT_QUOTES, 'UTF-8');
// Or use XOOPS functions$text_sanitizer = new xoops_text_sanitizer();echo $text_sanitizer->stripSlashesGPC($_GET['user_input']);Cross-Site 请求伪造 (CSRF) 预防
Section titled “Cross-Site 请求伪造 (CSRF) 预防”XOOPS 包括 CSRF 令牌保护。始终包含令牌:
<!-- In forms --><form method="post"> {xoops_token form=update} <input type="text" name="field"> <input type="submit"></form>禁用上传文件夹中的PHP执行
Section titled “禁用上传文件夹中的PHP执行”防止攻击者上传并执行PHP:
# Create .htaccess in uploads foldercat > /var/www/html/xoops/uploads/.htaccess << 'EOF'<FilesMatch "\.(php|phtml|php3|php4|php5|php6|php7)$"> Deny from all</FilesMatch>php_flag engine offEOF
# Alternative: Disable execution globally in uploadschmod 444 /var/www/html/xoops/uploads/ # Read-only配置重要的HTTP安全标头:
# Strict-Transport-Security (HSTS)# Forces HTTPS for 1 yearHeader always set Strict-Transport-Security "max-age=31536000; includeSubDomains; preload"
# X-Content-Type-Options# Prevents MIME type sniffingHeader always set X-Content-Type-Options "nosniff"
# X-Frame-Options# Prevents clickjacking attacksHeader always set X-Frame-Options "SAMEORIGIN"
# X-XSS-Protection# Browser XSS protectionHeader always set X-XSS-Protection "1; mode=block"
# Referrer-Policy# Controls referrer informationHeader always set Referrer-Policy "strict-origin-when-cross-origin"
# Content-Security-Policy# Controls resource loadingHeader always set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline'; style-src 'self' 'unsafe-inline'; img-src 'self' data: https:; font-src 'self'"管理面板安全
Section titled “管理面板安全”重命名管理文件夹
Section titled “重命名管理文件夹”通过重命名来保护管理文件夹:
# Rename admin foldermv /var/www/html/xoops/admin /var/www/html/xoops/myadmin123
# Update admin access URL# Old: http://your-domain.com/xoops/admin/# New: http://your-domain.com/xoops/myadmin123/配置XOOPS以使用重命名的文件夹:
编辑主文件。php:
// Change this linedefine('XOOPS_ADMIN_PATH', '/var/www/html/xoops/myadmin123');管理员 IP 白名单限制管理员访问特定 IP:
Section titled “管理员 IP 白名单限制管理员访问特定 IP:”文件:/var/www/html/XOOPS/myadmin123/.htaccess
# Allow only specific IPs<RequireAll> Require ip 192.168.1.100 # Your office IP Require ip 203.0.113.50 # Your home IP Deny from all</RequireAll>或者使用 Apache 2.2:
Order Deny,AllowDeny from allAllow from 192.168.1.100 203.0.113.50强大的管理员凭据
Section titled “强大的管理员凭据”为管理员强制使用强密码:
- 至少使用16个字符
- 混合大写、小写、数字、符号 3.定期更改密码(每90天) 4.使用密码管理器
- 启用两个-factor身份验证(如果可用)
监控管理员活动
Section titled “监控管理员活动”启用管理员登录日志记录:
管理面板 > 系统 > 首选项 > 用户设置
Log Admin Logins: YesLog Failed Login Attempts: YesAlert Email on Admin Login: Yes定期查看日志:
# Check database for login attemptsmysql -u xoops_user -p xoops_db << EOFSELECT uid, uname, DATE_FROM_UNIXTIME(user_lastlogin) as last_loginFROM xoops_users WHERE uid = 1;EOF更新XOOPS和模区块
Section titled “更新XOOPS和模区块”保持XOOPS和所有模区块更新:
# Check for updates in admin panel# Admin > Modules > Check for Updates
# Or via command linecd /var/www/html/xoops# Download and install latest version# Follow upgrade guide自动安全扫描
Section titled “自动安全扫描”#!/bin/bash# Security audit script
# Check file permissionsecho "Checking file permissions..."find /var/www/html/xoops -type f ! -perm 644 ! -name "*.htaccess" | head -10
# Check for suspicious filesecho "Checking for suspicious files..."find /var/www/html/xoops -type f -name "*.php" -newer /var/www/html/xoops/install/ 2>/dev/null
# Check database for suspicious activityecho "Checking for failed login attempts..."mysql -u xoops_user -p xoops_db << EOFSELECT count(*) as attempts FROM xoops_audittrail WHERE action LIKE '%login%' AND status = 0;EOF自动执行每日备份:
#!/bin/bash# Daily backup script
BACKUP_DIR="/backups/xoops"RETENTION=30 # Keep 30 days
# Backup databasemysqldump -u xoops_user -p xoops_db | gzip > $BACKUP_DIR/db_$(date +%Y%m%d).sql.gz
# Backup filestar -czf $BACKUP_DIR/files_$(date +%Y%m%d).tar.gz /var/www/html/xoops --exclude=cache --exclude=templates_c
# Remove old backupsfind $BACKUP_DIR -type f -mtime +$RETENTION -delete
echo "Backup completed at $(date)"使用 cron 进行计划:
# Edit crontabcrontab -e
# Add line (runs daily at 2 AM)0 2 * * * /usr/local/bin/xoops-backup.sh >> /var/log/xoops_backup.log 2>&1安全检查表模板
Section titled “安全检查表模板”使用此模板进行定期安全审核:
Weekly Security Checklist========================
Date: ___________Checked by: ___________
File System:[ ] Permissions correct (644/755)[ ] Install folder removed[ ] No suspicious files[ ] mainfile.php protected
Web Security:[ ] HTTPS/SSL working[ ] Security headers present[ ] Admin panel restricted[ ] File upload restrictions active[ ] Login attempts logged
Application:[ ] XOOPS version current[ ] All modules updated[ ] No error messages in logs[ ] Database optimized[ ] Cache cleared
Backups:[ ] Database backed up[ ] Files backed up[ ] Backup tested[ ] Offsite copy verified
Issues Found:1. ___________2. ___________3. ___________
Actions Taken:1. ___________2. ___________- 服务器要求
- 基本配置
- 性能优化
- OWASP 前 10 名:https://owasp.org/www-project-top-ten/
标签: #security #ssl #https #hardening #best-practices
相关文章:
- ../Installation/Installation
- ../../06-Publisher-Module/User-Guide/Basic-Configuration
- 系统-Settings
- ../Installation/Upgrading-XOOPS