[nginx]-nginx 安装naxsi模块

nginx 安装naxsi模块

 

如果需要 替换yum源为阿里源

 

查询已安装的yum

rpm -qa | grep yum

 

使用root权限卸载已安装的yum源

rpm -qa | grep yum | xargs rpm -e --nodeps

三、下载并安装阿里的yum源rpm包

 

创建安放rpm包的文件夹

cd /etc/yum.repos.d/

mkdir yum-rpm && cd yum-rpm

 

# 下载rpm包

 

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-161.el7.centos.noarch.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-50.el7.noarch.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-utils-1.1.31-50.el7.noarch.rpm

wget https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-9.el7.noarch.


 

安装rpm包

cd /etc/yum.repos.d/yum-rpm

rpm -ivh *.rpm  --force --nodeps

 

下载阿里镜像到/etc/yum.repos.d/目录下

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

 

修改Centos-7.repo文件将所有$releasever替换为7

vim /etc/yum.repos.d/CentOS-Base.repo

 

:%s/$releasever/7/g

 

运行以下命令

 

yum clean all

yum makecache

yum update

 

yum -y install gcc gcc-c++ autoconf automake make

yum -y install zlib zlib-devel openssl openssl-devel pcre pcre-devel

yum -y install GeoIP GeoIP-devel GeoIP-data

 

 

 

 

安装nginx

cd /tmp

wget  http://nginx.org/download/nginx-1.14.2.tar.gz

wget https://github.com/nbs-system/naxsi/archive/0.55.3.tar.gz

tar zxvf nginx-1.14.2.tar.gz

tar zxvf 0.55.3.tar.gz  -C /usr/local

cd nginx-1.14.2

./configure   --add-module=/usr/local/naxsi-0.55.3/naxsi_src/ --with-http_ssl_module  --with-http_stub_status_module --with-http_flv_module --with-http_gzip_static_module --with-http_realip_module

#如果已经安装nginx  则只需要执行make 然后覆盖nginx二级制文件即可

make &&make install

cd /usr/local/nginx/conf

touch naxsi.rules

在naxsi.rules中加入以下规则

#LearningMode  启用学习模式,即拦截请求后不拒绝访问,只将触发规则的请求写入error_log选项定义的文件中。如果对规则产生的影响不太清楚,可以先设置为学习模式。启用学习模式不能起到拦截非法请求的防御的效果。建议先使用学习模式,规则测试完成后再启用拦截模式。这样可以避免出现对网站、服务器某些不可知的影响。

#SecRulesEnabled|SecRulesDisabled

SecRulesEnabled;      #启用Naxsi模块

DeniedUrl "/RequestDenied";     #拒绝的请求发送到内部URL

#check rules  设置各规则不同的触发阈值。 一旦该阈值触发,请求将被阻塞。

CheckRule "$SQL >= 8" BLOCK;

CheckRule "$RFI >= 8" BLOCK;

CheckRule "$TRAVERSAL >= 4" BLOCK;

CheckRule "$EVADE >= 4" BLOCK;

CheckRule "$XSS >= 8" BLOCK;

#naxsi log

error_log        logs/naxsi.log;    #nginx相对路径都是相对于根目录

 

1 配置方法通过文件更改,在location下添加include naxsi.rules;

创建ngxsi规则

 

cp -r /usr/local/naxsi-0.55.3/naxsi_config/naxsi_core.rules  ./

 

为了方便测试 我修改了nginx.conf如下

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    include  naxsi_core.rules;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost 192.168.10.96;

 

 location / {

          include naxsi.rules;

           root /ftp/www/;

                 }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            return 418;

        }

                    location = /403.html {

                                     root   html;

                    }

location /RequestDenied {

   return 403;

  }

                    }

    }

 

 

 

 

启动nginx

/usr/local/nginx/sbin/nginx

 

http://192.168.10.96/index.html

http://192.168.10.96/index.html?asd=----

---是sql里面注释的语法 所以会匹配上

 

 

如果错误日志出现NAXSI_FMT的信息说明成功。

2 通过直接把naxsi规则location中

修改nginx.conf为

worker_processes  1;

events {

    worker_connections  1024;

}

http {

    include       mime.types;

    include  naxsi_core.rules;

    default_type  application/octet-stream;

    sendfile        on;

    keepalive_timeout  65;

    server {

        listen       80;

        server_name  localhost 192.168.10.96;

 

 location / {

    SecRulesEnabled;

    DeniedUrl "/RequestDenied";

    CheckRule "$SQL >= 8" BLOCK;

    CheckRule "$RFI >= 8" BLOCK;

    CheckRule "$TRAVERSAL >= 4" BLOCK;

    CheckRule "$EVADE >= 4" BLOCK;

    CheckRule "$XSS >= 8" BLOCK;

    error_log /var/log/nginx/foo.log;

           root /ftp/www/;

                 }

        error_page   500 502 503 504  /50x.html;

        location = /50x.html {

            return 418;

        }

                    location = /403.html {

                                     root   html;

                    }

location /RequestDenied {

   return 403;

  }

                    }

    }

 

开启学习模式  修改naxsi.rules

    SecRulesEnabled;

    LearningMode;

    DeniedUrl "/RequestDenied";

    CheckRule "$SQL >= 8" BLOCK;

    CheckRule "$RFI >= 8" BLOCK;

    CheckRule "$TRAVERSAL >= 4" BLOCK;

    CheckRule "$EVADE >= 4" BLOCK;

    CheckRule "$XSS >= 8" BLOCK;

    error_log /usr/local/nginx/logs/foo.log;

 

 

请求注入的url  返回404

nginx的日志如下

 

 

 

 

 

 

 

还可以添加白名单naxsi_BasicRule.conf   也需要再conf中include

conf/naxsi_BasicRule.conf

BasicRule wl:0 "mz:$ARGS_VAR:script";

BasicRule wl:0 "mz:$ARGS_VAR:id";

 

这表示xss攻击正常是被拦截的,若被添加白名单,则不被拦截.此处是Get参数名若为id或者script,则不被拦截.

basicrule规则参考  http://code.google.com/p/naxsi/wiki/BasicRule

 

 

 

 

白名单规则语法:
BasicRule wl:ID [negative] [mz:[$URL:target_url]|[match_zone]|[$ARGS_VAR:varname]|[$BODY_VARS:varname]|[$HEADERS_VAR:varname]|[NAME]]

wl:ID (White List ID) 哪些拦截规则会进入白名单
wl:0:把所有拦截规则加入白名单
wl:42:把ID为42的拦截规则加入白名单
wl:42,41,43:把ID为42, 41和43的拦截规则加入白名单
wl:-42:把所有拦截规则加入白名单,除了ID为42的拦截规则

mz:(Match Zones)
ARGS: GET的整个参数,如: foo=bar&in=%20
$ARGS_VAR: GET参数的参数名, 如:foo=bar&in=%20中的foo和in
$ARGS_VAR_X: 正则匹配的GET参数的参数名
HEADERS: 整个HTTP协议头
$HEADERS_VAR: HTTP协议头的名字
$HEADERS_VAR_X: 正则匹配的HTTP协议头的名字
BODY: POST的整个参数内容
$BODY_VAR: POST参数的参数名
$BODY_VAR_X: 正则匹配的POST参数的参数名
URL: URL(?前的)
URL_X: 正则匹配的URL(?前的)
FILE_EXT: 文件名 (POST上传文件时上传的文件名)

Naxsi 社区提供了一些常用的白名单规则,例如 wordpress 。可以在 https://github.com/nbs-system/naxsi-rules 下载白名单规则。

 

在使用中发现jumpserver https的请求可以通过 ,但是yearning的话 有些请求会被拦截掉。导致选择工单提交的时候下拉框为空。

 

 

 

暂时添加了一条规则

[root@op-jumpserver01 nginx]# cat /etc/nginx/naxsi_whitelists.rules

BasicRule wl:16 ;

 

并且在nginx.conf中引入

    location / {

        include  naxsi_whitelists.rules;

        include naxsi.rules;

 

 

重新reload nginx 之后 可以了。但是规则16 是不是放的太大了。还需要研究一下

 

 

白名单规则示例

以下对静态的白名单规则进行举例说明。

 

BasicRule wl:1000;
已知拦截规则1000是过滤包含select|union|update|delete|insert|table|from|ascii|hex|unhex之类SQL关键字的规则。
在本子规则中完全禁用拦截规则1000。因为没有指定区域,所以全部加入白名单。

BasicRule wl:1000 “mz:$ARGS_VAR:foo”;
在全部GET参数名为foo的值中禁用拦截规则1000 (即不用1000规则检查GET中参数名为foo中的值)。所以, http://127.0.0.1/?foo=select * from a 不会被过滤。

BasicRule wl:1000 “mz:$ARGS_VAR:foo|$URL:/bar”;
在URL为/bar的GET请求中参数名为foo的值中禁用拦截规则1000。所以, http://127.0.0.1/bar?foo=select * from a 不会被过滤。

BasicRule wl:1000 “mz:$URL:/bar|ARGS”;
在URL为/bar的GET请求中的参数禁用拦截规则1000。所以, http://127.0.0.1/bar?my=select * from a 不会被过滤。http://127.0.0.1/bar?from=weibo 也不会被过滤。

BasicRule wl:1000 “mz:ARGS|NAME”;
在全部GET请求中对所有参数名(只是名,不包含参数值)中禁用拦截规则1000。所以, http://127.0.0.1?from=weibo 不会被过滤。但是, http://127.0.0.1?foo=select 会被过滤(因为select属于参数值,不在白名单范围内)。

BasicRule wl:1000 “mz:$URL:/bar|ARGS|NAME”;
在URL为/bar的全部GET请求中对所有参数名(只是名,不包含参数值)中禁用拦截规则1000。所以, http://127.0.0.1/bar?from=weibo 不会被过滤。但是, http://127.0.0.1/bar?foo=select 会被过滤(因为select属于参数值,不在白名单范围内)。

BasicRule wl:0 “mz:|URL”;
在全部请求中对符合^/upload/(.).(.)$正则规则的URL禁用全部拦截规则。所以, http://127.0.0.1/upload/select.db 不会被过滤 (原本会触发1000拦截规则)。
以下对含正则表达式的白名单规则进行举例说明。

BasicRule wl:1000 “mz:$ARGS_VAR_X:meh”;
在全部GET参数名含有meh的值中禁用拦截规则1000 (即不用1000规则检查GET中参数名含有meh中的值)。所以, http://127.0.0.1/?smehe=select * from a 不会被过滤。

BasicRule wl:1000 “mz:$ARGS_VAR_X:^meh”;
在全部GET参数名以meh开始的值中禁用拦截规则1000 (即不用1000规则检查GET中参数名以meh开始的值)。所以, http://127.0.0.1/?mehe=select * from a 不会被过滤。

BasicRule wl:1000 “mz:$ARGS_VAR_X:^meh_[0-9]+$”
在全部GET参数名格式为“meh_数字”的值中禁用拦截规则1000 (即不用1000规则检查GET中参数名格式为“meh_数字”的值)。所以, http://127.0.0.1/?meh_12=select * from a 不会被过滤。

BasicRule wl:1000 “mz:$URL_X:^/foo|ARGS”;
在URL以/foo开始的GET请求中的参数禁用拦截规则1000。所以, http://127.0.0.1/foo1?my=select * from a 不会被过滤。http://127.0.0.1/foo2?from=weibo 也不会被过滤。

BasicRule wl:1000 “mz:$URL_X:/foo|$ARGS_VAR_X:[0-9]”;
在URL以/foo开始的GET请求中的以数字开始命名的参数禁用拦截规则1000。所以, http://127.0.0.1/foo1?2my=select * from a 不会被过滤。http://127.0.0.1/foo2?1from=weibo 也不会被过滤。

以下对RAW_BODY的白名单规则进行举例说明。以RAW_BODY为目标的白名单规则的编写方式与任何其他BODY规则相同。

BasicRule wl:4241 “mz:$URL:/|BODY”;
已知拦截规则4241是即便在学习模式下也要过滤RAW_BODT中包含字符串RANDOMTHINGS的规则。
在本子规则中,在URL为/的BODY中的内容禁用拦截规则4241,意味着该目录下的任意BODY内容都不会被过滤。

以下对FILE_EXT的白名单规则进行举例说明。

BasicRule wl:1337 “mz:$URL:/index.html|FILE_EXT”;
在本子规则中,在URL为/index.html的文件上传部分禁用拦截规则1337,意味着可在该目录下上传任意文件。

以下对JSON的白名单规则进行举例说明。

BasicRule wl:1302 “mz:$BODY_VAR:lol”;
JSON可被当作BODY, 以变量的形式表示文本内容。在本子规则中,对POST的参数lol禁用拦截规则1302,即以下JSON内容可以被通过。

{

    "lol" : "foo<bar"

}

 

手动编写白名单规则

以下为访问http://www.gugexueshu.com/?foo=select * from a的错误日志记录。

 

2018/08/19 13:35:16 [error] 1#0: *187 NAXSI_FMT: ip=*.*.*.*&server=www.123admin.com&uri=/&learning=0&vers=0.55.3&total_processed=116&total_blocked=1&block=1&cscore0=$SQL&score0=4&cscore1=$XSS&score1=8&zone0=ARGS&id0=1000&var_name0=foo, client: *.*.*.*, server: www.123admin.com, request: "GET /?a=select%20*%20from%20a HTTP/1.1", host: www.123admin.com

 

从zone0=ARGS可看出被过滤的区域是ARGS,从id0=1000可看出拦截规则的id是1000,从var_name0=foo可看出被过滤的变量为foo。因此可以编写如下白名单规则。

BasicRule wl:1000 "mz:$ARGS_VAR:foo";

 

 

 

 

 

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值