ModSecurity与NginX

时间:2017-07-07 19:05:46

标签: nginx server mod-security

我正在使用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的最新稳定代码

4 个答案:

答案 0 :(得分:1)

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

因此可以在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是否进一步修改了它。 / p>

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

答案 1 :(得分: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; 
    } 

答案 2 :(得分:0)



Ubuntu服务器+ Nginx

为了跳过此漫长的手动安装过程,我创建了scripit,该文件负责下载所需的文件,相应的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;
}

谢谢。

答案 3 :(得分:-1)

如果您使用的是 ubuntu 20.04,只需按照以下步骤操作:

安装nginx: . http://nginx.org/en/linux_packages.html#Ubuntu(所有步骤)

安装 libmodsecurity3: . https://www.nginx.com/blog/compiling-and-installing-modsecurity-for-open-source-nginx/(仅第 2 步) 运行:#sudo apt-get install libmodsecurity3 libmodsecurity-dev

安装 modsecurity-nginx-connector: . https://www.nginx.com/blog/compiling-and-installing-modsecurity-for-open-source-nginx/(从第 4 步开始)

ubuntu 20.04不需要编译libmodsecurity3,安装运行即可: #sudo apt-get install libmodsecurity3 libmodsecurity-dev

我试着编译了几天都没有成功,直到我发现不需要编译。