ubuntu

Ubuntu下Apache如何配置防盗刷

小樊
39
2025-12-11 01:41:11
栏目: 智能运维

在Ubuntu下配置Apache防盗刷,可以通过以下几种方法来实现:

方法一:使用mod_rewrite模块

  1. 启用mod_rewrite模块

    sudo a2enmod rewrite
    
  2. 编辑Apache配置文件: 打开你的网站配置文件,通常位于/etc/apache2/sites-available/目录下,例如yourdomain.com.conf

    sudo nano /etc/apache2/sites-available/yourdomain.com.conf
    
  3. 添加防盗刷规则: 在<VirtualHost>标签内添加以下内容:

    <VirtualHost *:80>
        ServerAdmin webmaster@yourdomain.com
        DocumentRoot /var/www/html
    
        <Directory /var/www/html>
            Options Indexes FollowSymLinks
            AllowOverride All
            Require all granted
        </Directory>
    
        # 防盗刷规则
        RewriteEngine On
        RewriteCond %{HTTP_REFERER} !^$
        RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com [NC]
        RewriteRule .* - [F,L]
    </VirtualHost>
    

    解释:

    • RewriteEngine On:启用重写引擎。
    • RewriteCond %{HTTP_REFERER} !^$:检查HTTP_REFERER是否为空。
    • RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain\.com [NC]:检查HTTP_REFERER是否不是你的域名。
    • RewriteRule .* - [F,L]:如果条件满足,返回403 Forbidden状态码,并停止进一步处理。
  4. 重启Apache服务

    sudo systemctl restart apache2
    

方法二:使用mod_security模块

  1. 安装mod_security

    sudo apt-get install libapache2-mod-security2
    
  2. 启用mod_security模块

    sudo a2enmod security2
    
  3. 配置mod_security规则: 编辑/etc/modsecurity/modsecurity.conf文件,添加防盗刷规则:

    sudo nano /etc/modsecurity/modsecurity.conf
    

    添加以下内容:

    SecRule REQUEST_URI "@rx \.(php|jsp|asp|aspx|jspx|html)$" \
        "id:123456,\
        phase:2,\
        deny,\
        status:403,\
        log,\
        msg:'Access to restricted file types is blocked.',\
        tag:'application-multi',\
        tag:'language-multi',\
        tag:'platform-multi',\
        tag:'attack-restricted-file-types',\
        tag:'OWASP_CRS/WEB_ATTACK/FILE_INCLUSION',\
        tag:'WASCTC/WASC-21',\
        tag:'OWASP_TOP_10/A7',\
        tag:'OWASP_AppSensor/CIE1',\
        tag:'PCI/6.5.10'"
    

    这个规则会阻止访问某些文件类型,你可以根据需要调整正则表达式。

  4. 重启Apache服务

    sudo systemctl restart apache2
    

方法三:使用第三方防盗刷插件

你也可以考虑使用一些第三方防盗刷插件,例如mod_evasivemod_security_crs,这些插件提供了更复杂的防盗刷功能。

使用mod_evasive

  1. 安装mod_evasive

    sudo apt-get install libapache2-mod-evasive
    
  2. 配置mod_evasive: 编辑/etc/mods-available/evasive.conf文件:

    sudo nano /etc/mods-available/evasive.conf
    

    添加以下内容:

    <IfModule mod_evasive20.c>
        DOSHashTableSize    3097
        DOSPageCount        2
        DOSSiteCount        50
        DOSPageInterval     1
        DOSSiteInterval     1
        DOSBlockingPeriod   10
    </IfModule>
    
  3. 启用mod_evasive模块

    sudo a2enmod evasive
    
  4. 重启Apache服务

    sudo systemctl restart apache2
    

通过以上方法,你可以在Ubuntu下配置Apache防盗刷,保护你的网站免受恶意访问。

0
看了该问题的人还看了