基于 Nginx + ModSecurity V3 实现对 web 流量的安全访问控制

您所在的位置:网站首页 男装冲锋衣品牌推荐几个牌子 基于 Nginx + ModSecurity V3 实现对 web 流量的安全访问控制

基于 Nginx + ModSecurity V3 实现对 web 流量的安全访问控制

#基于 Nginx + ModSecurity V3 实现对 web 流量的安全访问控制| 来源: 网络整理| 查看: 265

ModSecurityModSecurity 是一个开源的、跨平台的 Web 应用防火墙,它可以通过检查 Web 服务器收发的数据来对网站流量进行安全防护最初设计 ModSecurity 项目时,它只是一个 Apache 模块。随着时间的推移,该项目已经扩展到支持其他平台, 如 Nginx; 为了满足对额外平台支持不断增长的需求,有必要删除该项目底层的 Apache 依赖项,使其更加独立于平台当前 ModSecurity v3 由 Libmodsecurity(对 ModSecurity 平台的完全重写) 和 对应 web 服务器的连接器(模块)组成请求处理阶段 Request Headers、Request Body、Response Headers、Response Body、Logging组件版本nginx v1.20.1libmodsecurity v3.05ModSecurity-nginx v1.0.2coreruleset v3.3.2CentOS Linux release 7.6.1810 (Core)

libmodsecurity

基于 SecRules 的 web 流量处理引擎, 提供了加载/解释以 ModSecurity SecRules 格式编写的规则的能力

1、安装 libmodsecurity 所需依赖库

# 安装依赖 yum install -y epel-release git gcc gcc-c++ autoconf libtool pcre-devel libxml2-devel curl-devel yajl-devel flex-devel lua-devel lmdb-devel ssdeep-devel # 安装依赖项 libmaxminddb wget -c https://github.com/maxmind/libmaxminddb/releases/download/1.6.0/libmaxminddb-1.6.0.tar.gz ./configure make && make install

2、下载编译安装 libmodsecurity v3.05

git clone https://github.com/SpiderLabs/ModSecurity cd ModSecurity # 下载 libInjection git submodule init git submodule update # 开始构建, 默认安装位置 /usr/local/modsecurity/ ./build.sh ./configure && make && make install

modsecurity-nginx

nginx 和 libmodsecurity 之间的连接器, 其实就是一个第三方 Nginx 模块, Nginx 可以通过静态或动态方式加载该模块

1、Nginx 的编译安装

wget http://nginx.org/download/nginx-1.20.1.tar.gz tar xzf nginx-1.20.1.tar.gz && cd nginx-1.20.1 yum install openssl-devel git clone --depth 1 https://github.com/SpiderLabs/ModSecurity-nginx.git ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_temp --http-scgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_mp4_module --with-http_random_index_module --with-http_realip_module --with-http_secure_link_module --with-http_slice_module --with-http_ssl_module --with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' --with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-dynamic-module=../ModSecurity-nginx make -j4 mkdir /var/cache/nginx/ && useradd -r nginx && mkdir /etc/nginx/modules cp objs/ngx_http_modsecurity_module.so /etc/nginx/modules # nginx 配置文件首行添加如下配置, 全局加载该模块 load_module modules/ngx_http_modsecurity_module.so;

2、添加 modsecurity 相关配置文件 modsecurity.conf, main.conf

# 创建配置文件 modsecurity.conf mkdir /etc/nginx/modsec && cd /etc/nginx/modsec wget https://raw.githubusercontent.com/SpiderLabs/ModSecurity/v3/master/modsecurity.conf-recommended mv modsecurity.conf-recommended modsecurity.conf # 修改 modsecurity.conf 中的对应配置项 vim /etc/nginx/modsec/modsecurity.conf SecRuleEngine On # 默认 DetectionOnly(开启规则匹配,但不执行任何拦截操作), On(开启规则匹配并进行相应的拦截) #SecUnicodeMapFile unicode.mapping 20127 # 默认启用, 现变更为注释该行 # 创建主配置 main.conf 及自定义规则 cat > /etc/nginx/modsec/main.conf OK (ModSecurity permits access. But this page itself does not exist. So we get 404, Page not Found) curl http://localhost/login/displayLogin.do?debug=on -> FAIL curl http://localhost/login/admin.html -> FAIL (Again a 404, but the error log should show a deny with status 404) curl -d "username=john&password=test" http://localhost/login/login.do -> OK (ModSecurity permits access. But this page itself does not exist. So we get 404, Page not Found) curl -d "username=john&password=test&backdoor=1" http://localhost/login/login.do -> FAIL curl -d "username=john56789012345678901234567890123&password=test" http://localhost/login/login.do -> FAIL curl -d "username=john'&password=test" http://localhost/login/login.do -> FAIL curl -d "username=john&username=jack&password=test" http://localhost/login/login.do -> FAIL



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3