百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术资源 > 正文

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

off999 2025-01-06 14:49 32 浏览 0 评论

ModSecurity

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

组件版本

  • nginx v1.20.1
  • libmodsecurity v3.05
  • ModSecurity-nginx v1.0.2
  • coreruleset v3.3.2
  • CentOS 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
# 备注: ./build.sh 执行中提示 `fatal: No names found, cannot describe anything.` 暂时忽略

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 << EOF
Include /etc/nginx/modsec/modsecurity.conf
# 自定义安全规则, 参数 x 值中包含 test 字符串, 则返回 403 状态
SecRule ARGS:x "@contains test" "id:1234,deny,log,status:403"
SecUploadFileLimit 15    # 配置在multipart POST中处理的最大文件上传数量
EOF

# 在 Nginx 的 server 上下文中添加如下配置
modsecurity on;
modsecurity_rules_file /etc/nginx/modsec/main.conf;

3、完整的 Nginx 配置文件如下

cat /etc/nginx/nginx.conf
load_module modules/ngx_http_modsecurity_module.so;
worker_processes  1;
events {
    worker_connections  1024;
}
http {
    include       mime.types;
    default_type  application/octet-stream;
    keepalive_timeout  65;
    server {
        listen       80;
        server_name  localhost;
        modsecurity on;
        modsecurity_rules_file /etc/nginx/modsec/main.conf;

        location / {
            root   html;
            index  index.html index.htm;
        }
    }
}

OWASP

OWASP ModSecurity 核心规则集 (CRS) 是一组通用攻击检测规则, 用于 ModSecurity 或兼容的 Web 应用程序防火墙; CRS 旨在保护 Web 应用程序免受包括 OWASP 前十名在内的各种攻击, 同时将错误警报降至最低

1、在 Modsecurity 中启用 OWASP 核心规则集

# 下载部署 crs
wget https://github.com/coreruleset/coreruleset/archive/refs/tags/v3.3.2.tar.gz
tar xzf v3.3.2.tar.gz && cd cd coreruleset-3.3.2/
cp crs-setup.conf.example crs-setup.conf && cd ..
mv coreruleset-3.3.2/ /etc/nginx/modsec/crs3

# 在主配置文件 main.conf 中引用 modsecurity 及 crs 配置
cat > /etc/nginx/modsec/main.conf << EOF
# modsecurity 基本配置
Include /etc/nginx/modsec/modsecurity.conf
# OWASP CRS v3 rules
Include /etc/nginx/modsec/crs3/crs-setup.conf
Include /etc/nginx/modsec/crs3/rules/*.conf
# 自定义规则集
SecRule ARGS:x "@contains test" "id:1234,deny,log,status:403"
EOF

安全验证

1、请求参数 x 不包含字符串 test 的请求, 返回状态 200 http://192.168.31.66/?x=wyun

2、请求参数 x 包含非法字符串 test 的请求, 返回状态 403 http://192.168.31.66/?x=wyuntest

# 在 /var/log/nginx/error.log  中可以看到拦截的详细日志
2021/09/13 19:28:06 [error] 24702#24702: *7 [client 192.168.31.220] ModSecurity: Access denied with code 403 (phase 1). Matched "Operator `Contains' with parameter `test' against variable `ARGS:x' (Value: `test11' ) [file "/etc/nginx/modsec/main.conf"] [line "3"] [id "1234"] [rev ""] [msg ""] [data ""] [severity "0"] [ver ""] [maturity "0"] [accuracy "0"] [hostname "192.168.31.66"] [uri "/"] [unique_id "1631532486"] [ref "o0,4v8,6"], client: 192.168.31.220, server: localhost, request: "GET /?x=test11 HTTP/1.1", host: "192.168.31.66"

相关推荐

腾讯手机管家电脑版(腾讯手机管家电脑版官方下载)

手机管家关闭电脑的方法。首先,打开电脑管家主页面。2.再点击“更多选项”图标。3.然后,点击“设置”。4.选择“手机管理”。5.勾选“不需要提醒”。6.再单击“确定”。7.最后,我们关闭面...

腾讯qq最新版下载(腾讯qq下载2019 8.0版)

可能是网速问题!或者你先看看你是否已经下载安装了QQ游戏,是不是最新的版本,如果之前没问题的话你不要到QQ页面进QQ游戏,试试从我的电脑中的文件夹中进入,再不行的话或者重新安装下QQ游戏:再不行的话建...

qq邮箱手机版登录(手机qq邮箱app登录)
  • qq邮箱手机版登录(手机qq邮箱app登录)
  • qq邮箱手机版登录(手机qq邮箱app登录)
  • qq邮箱手机版登录(手机qq邮箱app登录)
  • qq邮箱手机版登录(手机qq邮箱app登录)
樱花校园模拟器中文版下载(樱花校园模拟器 下载)

1可以在应用商店搜索“校女园模拟器”下载最新版本。2原因是应用商店会及时更新最新版本,而且下载安装十分方便快捷。3如果在应用商店找不到最新版本,可以在官方网站进行下载,但需要注意安全问题。同时,...

单机斗地主 单机版 免费版下载

免费的单机斗地主有《欢乐斗地主单机版》联众《单机斗地主》。1,《欢乐斗地主单机版》《欢乐斗地主单机版》是根据欢乐斗地主规则改编的pc电脑单机游戏。这是789游戏中心好玩的斗地主游戏,电脑具有较高的人工...

腾讯游戏平台官网入口(腾讯游戏平台官网入口网址)
  • 腾讯游戏平台官网入口(腾讯游戏平台官网入口网址)
  • 腾讯游戏平台官网入口(腾讯游戏平台官网入口网址)
  • 腾讯游戏平台官网入口(腾讯游戏平台官网入口网址)
  • 腾讯游戏平台官网入口(腾讯游戏平台官网入口网址)
三国志5手机版单机游戏(三国志5手机版单机游戏有哪些)

1.提示DirectX的话--下载驱动人生更新驱动重启,保证能玩2.如果不是上面问题试下--右键游戏图标,属性,兼容性,禁用桌面元素钩上就OK了3.发现安装后桌面的启动图标是GameSt...

免费的视频app网站入口(免费的视频app网站入口)

柠檬影院是个网站,柠檬影视是个软件柠檬影院是一个真正免费看电影电视剧的影视网站,以提供高清影视在线观看,下载,网络休闲等为最终发展目标,致力于给广大的互联网用户带来最丰富影视柠檬影院每日实时更新,每日...

光影魔术手手机版下载(光影魔术手手机版下载 app)

1、打开光影魔术手软件,点击“打开”。2、选好需要去除文字的图片,插入光影魔术手中。3、然后点击左上角的“数码暗房”按钮。4、在打开的页面中选择“人像”。5、下拉菜单,在“人像”中选择“祛斑”按钮。6...

电视直播中央一台(电视直播中央一台开学第一课)

如果我们想在电脑或手机上观看中央电视台一套直播,只要用爱奇艺pps就可以了。1、点击进入应用商店,在搜索框中输入“爱奇艺”,点击搜索,并安装。2、安装结束后,在手机或电脑主页面找到爱奇艺图标,点击进入...

远程控制软件哪个好用(免费远程控制软件哪个好用)

一、UPDF——PDF文档处理分享二、腾讯会议——远程开会三、向日葵——远程控制四、Github——代码协同五、Snipaste——屏幕截图六、Canva——在线设计工具七、博思白板BoardM...

魔兽世界手游官网公测(魔兽世界手游官网公测 新闻)

2005年4月26日2005年4月26日由九城代理的《魔兽世界》国服开启公测,6月9日国服正式运营。2009年4月,暴雪将《魔兽世界》在国服的代理运营权由九城转交给网易。

qq2012(qq2012旧版本下载)

建议你升级到最新版本,因为一般来说旧版本都不会怎么更新、维护,容易出现BUG!

香蕉加速器(香蕉加速器怎么样)

OPPO系统自带的游戏加速器是HyperBoost据OPPO介绍,HyperBoost是OPPO花了三年时间自主研发的一项技术,由系统引擎、应用引擎和游戏引擎三部分组成,能够实现全场景系统,提升...

小说下载txt电子书免费全本下载

因为违规了。久久小说网的许多小说,都没有作者的授权而且他的名声太大了,被人给投诉然后违规追究了,就关闭了这个网站。所以平时看小说的时候,尽量到正版的网站去看,一些大网站,或者是一些小网站都可以去瞅一瞅...

取消回复欢迎 发表评论: