ModSecurity与NginX的兼容性

问题描述 投票:1回答:3

我正在使用NginX并希望将ModSec合并为一个模块。在做了一些研究之后,只有一个链接讨论了如何“使用主服务器的源代码编译ModSecurity”。我假设这意味着它需要用Apache编译? (不太确定,因为我是新手)

ModSec与NginX兼容吗?

如果没有ModSec的替代方案吗?

如果可能,请提供见解/链接!

谢谢!


更新ModSec首席开发人员的答案:

此实现包含3个部分,ModSec,NginX和ModSec-NginX连接器。

教程可以在这里找到Ubuntu 16.04:[link] https://www.howtoforge.com/tutorial/nginx-with-libmodsecurity-and-owasp-modsecurity-core-rule-set-on-ubuntu-1604/

PS。当你下载NginX的最新稳定代码时,请确保

nginx server mod-security
3个回答
1
投票

ModSecurity最初是为Apache创建的,但已经(有些)可用于其他Web服务器。

因此可以在Nginx上安装它,并在Google上有多个博客文章。例如:https://geekflare.com/install-modsecurity-on-nginx/

然而,当前版本的ModSecurity(2.9.1)仍然有许多遗留的Apache代码和依赖项,而某些功能仅适用于Apache。下一个版本(3.0)将是一个更清晰的分离,将具有共同的核心(称为libmodsecurity)和Nginx特定的连接器:https://github.com/SpiderLabs/ModSecurity-nginx。虽然没有ETA,但我自己也没试过,所以不能谈论它有多好。

Nginx还提供了带有ModSecurity的Nginx付费版本,如下所示:https://www.nginx.com/blog/modsecurity-waf-released/,它使用的是更新版本。我不确定它是否是GitHub上的确切代码(版本3仍标记为“不稳定”)或者Nginx是否进一步修改了它。

另一种选择是在Apache上运行ModSecurity作为Nginx前面的代理。但考虑到这一点,最好将所有内容移至Apache。


0
投票

ModSec与NginX不兼容

但是你可以在fastcgi_params设置一些有用的规则

location ~* union.*select.*\( {    deny all; }
location ~* union.*all.*select.* {   deny all; }
location ~* concat.*\( {   deny all; }
## Block common exploits
location ~* (<|%3C).*script.*(>|%3E) {   deny all; }
location ~* base64_(en|de)code\(.*\) {   deny all; }
location ~* (%24&x) {   deny all; }
location ~* (%0|%A|%B|%C|%D|%E|%F|127\.0) {   deny all; }
location ~* \.\.\/  {   deny all; }
location ~* ~$ {   deny all; }
location ~* proc/self/environ {   deny all; }
location ~* /\.(htaccess|htpasswd|svn) {   deny all; }
## Block file injections
location ~* [a-zA-Z0-9_]=(\.\.//?)+ {   deny all; }
location ~* [a-zA-Z0-9_]=/([a-z0-9_.]//?)+ {   deny all; }
## wordpress security
location ~* wp-config.php {   deny all; }
location ~* wp-admin/includes {   deny all; }
location ~* wp-app\.log {   deny all; }
location ~* (licence|readme|license)\.(html|txt) {   deny all; }

 set $block_common_status 0;
    if ($query_string ~ "(<|%3C).*script.*(>|%3E)") {
        set $block_common_status 1;
    }
    if ($query_string ~ "base64_(en|de)code\(.*\)") {
        set $block_common_status 1;
    }
    if ($block_common_status = 1) {
        return 403;
    }



 if ( $http_referer ~* (babes|forsale|girl|jewelry|love|nudit|organic|poker|porn|sex|teen) )
     {
         # return 404;
         return 403;
     }

    ## Block SQL injections 
    set $block_sql_injections 0; 
    if ($query_string ~ "union.*select.*\(") { 
        set $block_sql_injections 1; 
    } 
    if ($query_string ~ "union.*all.*select.*") { 
        set $block_sql_injections 1; 
    } 
    if ($query_string ~ "concat.*\(") { 
        set $block_sql_injections 1; 
    } 
    if ($block_sql_injections = 1) { 
        return 403; 
    } 

    ## Block file injections 
    set $block_file_injections 0; 
    if ($query_string ~ "[a-zA-Z0-9_]=http://") { 
        set $block_file_injections 1; 
    } 
    if ($query_string ~ "[a-zA-Z0-9_]=(\.\.//?)+") { 
        set $block_file_injections 1; 
    } 
    if ($query_string ~ "[a-zA-Z0-9_]=/([a-z0-9_.]//?)+") { 
        set $block_file_injections 1; 
    } 
    if ($block_file_injections = 1) { 
        return 403; 
    } 

    ## Block common exploits 
    set $block_common_exploits 0; 
    if ($query_string ~ "(<|%3C).*script.*(>|%3E)") { 
        set $block_common_exploits 1; 
    } 
    if ($query_string ~ "GLOBALS(=|\[|\%[0-9A-Z]{0,2})") { 
        set $block_common_exploits 1; 
    } 
    if ($query_string ~ "_REQUEST(=|\[|\%[0-9A-Z]{0,2})") { 
        set $block_common_exploits 1; 
    } 
    if ($query_string ~ "proc/self/environ") { 
        set $block_common_exploits 1; 
    } 
    if ($query_string ~ "mosConfig_[a-zA-Z_]{1,21}(=|\%3D)") { 
        set $block_common_exploits 1; 
    } 
    if ($query_string ~ "base64_(en|de)code\(.*\)") { 
        set $block_common_exploits 1; 
    } 
    if ($block_common_exploits = 1) { 
        return 403; 
    } 

    ## Block spam 
    set $block_spam 0; 
    if ($query_string ~ "\b(ultram|unicauca|valium|viagra|vicodin|xanax|ypxaieo)\b") { 
        set $block_spam 1; 
    } 
    if ($query_string ~ "\b(erections|hoodia|huronriveracres|impotence|levitra|libido)\b") { 
        set $block_spam 1; 
    } 
    if ($query_string ~ "\b(ambien|blue\spill|cialis|cocaine|ejaculation|erectile)\b") { 
        set $block_spam 1; 
    } 
    if ($query_string ~ "\b(lipitor|phentermin|pro[sz]ac|sandyauer|tramadol|troyhamby)\b") { 
        set $block_spam 1; 
    } 
    if ($block_spam = 1) { 
        return 403; 
    } 

    ## Block user agents 
    set $block_user_agents 0; 


    # Disable Akeeba Remote Control 2.5 and earlier 
    if ($http_user_agent ~ "Indy Library") { 
        set $block_user_agents 1; 
    } 

    # Common bandwidth hoggers and hacking tools. 
    if ($http_user_agent ~ "libwww-perl") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "GetRight") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "GetWeb!") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "Go!Zilla") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "Download Demon") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "Go-Ahead-Got-It") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "TurnitinBot") { 
        set $block_user_agents 1; 
    } 
    if ($http_user_agent ~ "GrabNet") { 
        set $block_user_agents 1; 
    } 

    if ($block_user_agents = 1) { 
        return 403; 
    } 

0
投票

Ubuntu服务器+ Nginx 为了跳过这个手册冗长的安装进度,我创建了脚本,负责下载所需的文件,相应的nginx版本源代码下载和编译等等。只需按照以下简单步骤即可安装和配置ModSecurity:

# DOWNLOAD INSTALLATION SCRIPT
wget https://gist.githubusercontent.com/Chetan07j/edc16d6a55a25475cafeb6995f9c0857/raw/011f5f07b1b22366fee2b5fb8f4f2adc69b3e3ed/libModSecurity.sh
​
# MAKE THIS FILE EXECUTABLE
sudo chmod +x libModSecurity.sh
​
# RUN THIS FILE
./libModSecurity.sh
​
# DONE

安装完成后,您需要在nginx.conf中添加几行,并在站点可用文件夹中的服务器配置中添加几行。

在/etc/nginx/nginx.conf文件中,在pid行后立即添加此行:

...
pid /run/nginx.pid;
load_module /etc/nginx/modules/ngx_http_modsecurity_module.so; # ADD THIS LINE

然后在/ etc / nginx / sites-available /下的服务器配置文件中

server {
    # ...
    modsecurity on;
    modsecurity_rules_file /etc/nginx/modsec/main.conf;
}

谢谢。

© www.soinside.com 2019 - 2024. All rights reserved.