找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11976|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 }. G' E4 t2 _" V: e" Y8 Z7 }3 B- f
一.准备工作4 l7 s, E" u# ~  ^

& l7 a: k& f: {. x% x* G系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, D& d5 g0 o" ^. l$ ~) L& U! E* h6 O0 h  ]- W/ w
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 Y( z# O: g; G6 ?% N3 a

2 M5 H" A4 Y4 Q# w7 Xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 W7 N# b& w) Z( @
9 ^. h3 m/ t" h' \! S9 ZOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 g2 X/ U& {# f+ }

: l4 G) Y* ?8 {( |" v依赖关系:
$ ?- T2 B8 a! @9 f7 x4 l) qtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' K# z( Z. t8 E9 ?: ^1 s3 I2 J" T8 B# V3 s# j: h6 k& w1 ?7 m' M" r
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel5 f% d# S5 d- ]
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( y0 w' N' x1 j& w: q% c' E, b6 ]" g; {3 c0 \& s2 s
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel2 o* ^$ C2 \, H% S
二.启用standalone模块并编译* f" j4 t9 [: W& \2 [. M
" f9 O9 U" S+ A+ v2 g+ s
下载modsecurity for nginx 解压,进入解压后目录执行:
  E8 l" b2 Z- |6 B
6 {. G$ L0 R5 u3 w2 S7 F; F./autogen.sh
3 G  f5 k& U4 {% v8 v& p./configure --enable-standalone-module --disable-mlogc
, }8 M/ w+ q- M$ S  O+ x# J+ w7 |, w, zmake 9 K: i4 j* ?) T7 @0 f
三.nginx添加modsecurity模块
5 d2 f! i% a% P  c2 ?$ V" Q
6 M- B+ `5 _( N- A& W/ v在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 G' _9 M& J1 `) C0 v  X
/ ]' v( {% _6 v4 D: b" o, g
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ ^$ D* J% _8 V
make && make install  A" l! U3 U" `+ I, D' N$ L, t
四.添加规则
5 J7 ^- q( }  k) `; P2 \+ O
; {0 T1 h# g6 y1 K0 b. [9 Bmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% J& M3 F" ?' A8 o% J

5 I. G" B! ^* P/ R- \! f1.下载OWASP规则:/ N6 d, D6 |7 ~% q/ [& \
/ z0 P  M6 L6 w; C- N3 X$ m1 J
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ Q- P5 X, K" z- k! e

5 F% k; U; G) d% J  V/ e9 qmv owasp-modsecurity-crs /opt/tengine/conf/2 R. L! q9 U( _8 i1 C3 Z
" d! i, z0 B" z# U4 o. E" H
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* L/ v* C' {3 J9 J
2.启用OWASP规则:0 d. z0 A6 F! o2 I/ a4 [0 l" A
; O$ ?. s. v: a2 W0 [, K) x
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! i- n' M7 n; s) v& p3 x
6 c9 N( j! `9 \7 p6 a# \8 e5 F4 ^
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, b# ^# |+ q2 z1 \
* B" v# Q% b+ V; o$ g2 j- F
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# P6 B8 B! e7 a; a9 V! J
/ x- M5 j2 F# d: O+ Y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ \# m; s2 z4 ]) E6 Y! I4 RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) w# e8 G2 g* d% F7 zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# h4 V6 S9 [1 HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf, ^& O# v2 F8 S, ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% ]% E/ j" r# Z! UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
  I7 W+ ?/ y1 B, f9 y, O* ^3 E2 GInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' [5 O+ }" L- B五.配置nginx
# ?3 B# E- ^0 z# ^3 w2 o$ I$ m8 ?( P9 g1 k3 |7 ^5 }/ t1 B
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ a5 v2 J7 d0 A% Y, F' A2 O3 i) S3 }: M: `" a& R+ E  c+ L
ModSecurityEnabled on;  . k1 G3 i. n3 x# `- B$ \
ModSecurityConfig modsecurity.conf;6 K3 ^, N) |- o4 [* e7 \' f
下面是两个示例配置,php虚拟主机:. v/ q  Z' G8 Z5 w
! n- B3 F+ A. U. M3 n9 Q5 i
server {
1 K; i; L8 n/ `6 H, O$ x! U      listen      80;
- W3 @: R3 B9 T3 w      server_name 52os.net www.52os.net;3 B# Y  Y$ h1 A% d; {' a/ c
     $ }" b4 j7 G2 Q, M% w
      location ~ \.php$ {0 b1 E$ h; w4 x4 U" q' s; `' P1 ~
      ModSecurityEnabled on;  
$ f4 E/ b8 k& c      ModSecurityConfig modsecurity.conf;" V  g+ }( v, J  Y8 H1 `; @1 X% E( `; @

4 |$ q- V) C; j6 K8 l* ^      root /web/wordpress;8 _1 q( B0 v" D8 a
      index index.php index.html index.htm;
4 p" _, ^# F( q  
; D, t0 }6 S( n# n6 }. S4 B  ~      fastcgi_pass   127.0.0.1:9000;
7 T* w  F7 u7 a% I8 H2 e* T8 S1 _: F      fastcgi_index  index.php;! y# Q  o2 F% l
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
, o' v. d0 y9 G8 c# g; E      include        fastcgi_params;3 E0 F0 {7 h+ {7 R$ W6 P
      }
* l- b. e5 F4 H7 q  }# v( M+ c& C+ D% G* J1 [' E
upstream负载均衡:
7 X0 k$ g: \4 n2 `: Z6 P+ {% B& P5 ^$ i2 U' M( p4 D" ^8 R1 S* F. K
upstream 52os.net {+ v6 {* k9 a+ t' k  U
    server 192.168.1.100:8080;
( ]; ?% ~  S" f/ q: F3 N    server 192.168.1.101:8080 backup;. h7 u; o3 f- x& y8 O& S
}
; H5 k  B* S3 Q0 r" j: A1 m; P: d6 [8 B" J4 s- c. S2 i7 r; c# o: |3 B
server {
3 l7 t  ?# _3 |  ?5 a  Y3 o! plisten 80;! O+ G0 @, {3 Z  \
server_name 52os.net www.52os.net;
0 R, d6 b: ^4 ^" K) g& G* q) G( u2 N. H9 L
location / {
( M  G1 n; R+ Z9 H, E1 G7 e% _# {    ModSecurityEnabled on;  
' z5 G9 s/ O/ l! b$ G    ModSecurityConfig modsecurity.conf;  
3 q1 Q% r% t, W; h' }  D5 i& P1 W9 R; z4 Q
        proxy_pass http://online;' {, e, M/ b( p) p+ P4 B
        proxy_redirect         off;
9 g# @: a. E# T1 W1 o) h        proxy_set_header Host $host;
( q5 S7 l2 A* ]; L" x) y9 h        proxy_set_header X-Real-IP $remote_addr;5 J, J5 U4 |% o& c2 S% ]1 f
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ B( M3 Y8 ]' F: o; Z    }5 B0 g/ \9 l/ C+ f0 O) _
}8 J2 Y0 ?. b! e
六.测试
6 Z6 }  Q6 d* K- W% E" X; ^& F5 S
- }, r1 v! T3 }7 c# O2 S( d我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% w4 E$ [/ t2 G; ^
! {' t; d+ D3 z! [$ t+ h<?php
# h! d  V6 I- `" M    phpinfo();    . M: W1 L0 M; h9 B
?>
$ @" [, ]& Q$ B; r$ F: A在浏览器中访问:
! I% M* Y9 M5 s4 [9 N
( N" m$ A5 s$ C4 `- ~- v2 b9 Rhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 m) i/ \- @/ s' {( ]* `% j! Bhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  Q. @; l5 [/ k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
( m. h! F9 \; @9 F7 u" H" V  Q说明sql注入和xss已经被过滤了5 \( B% L( K' V8 A; q

$ H% _) U  v# {7 Z/ E  W0 h1 y七、安装过程中排错
, Y' M. m* ]$ @2 \7 u/ C1 X4 J6 P" a- G9 _
( `+ D! w3 M5 @4 z7 w+ @5 k1.缺少APXS会报错9 P/ |4 c! ]( j9 t, j

' C* s7 ]1 l+ B  U0 K! S" Sconfigure: looking for Apache module support via DSO through APXS" q5 b. l/ X3 R, I/ N* _* f  i; l6 l
configure: error: couldn't find APXS3 i$ ?: K# u0 M& E% h$ `" k% k
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  b! d% I  \& I& ^3 a! O6 R$ O解决方法:- \1 k. t9 k- r1 G/ t+ ]

# K6 u) p& @% |7 T4 j, Byum install httpd-devel" S2 k5 G! N7 P8 k! |
2.没有pcre; \2 }9 [5 P0 Z3 @6 w- u
2 v7 _/ {- p4 K8 S  j) B
configure: *** pcre library not found.
4 F  v1 z' ?5 W$ J) R, bconfigure: error: pcre library is required
6 l; A% v! e0 q& x解决方法:
, o# s( D8 K8 U5 @$ p5 \& V, z  F+ Q9 q/ v: k
yum install pcre pcre-devel
2 ^/ P0 p, S$ d. C+ @' l3.没有libxml2
* M. x) K; ?$ o* c$ J: P0 g  n: m

9 g3 i' q* b: J8 f- ~' _+ uconfigure: *** xml library not found.
5 B6 h: |- F0 cconfigure: error: libxml2 is required
, y2 ^; ]. }' A* `2 x( n8 R2 Z解决方法:
% |! ]" K0 E: |8 A7 A" L
) p' m9 x4 c; D. Fyum install  libxml2 libxml2-devel# s. Z; Q7 L# X4 M- [
4.执行 /opt/tengine/sbin/nginx -m 时有警告2 K: Y, w2 C" m7 v6 I, l) K
! v8 |6 Z2 y1 R& v2 F9 T: y, a# @
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( v( _+ t& \" y8 ?2 ~3 Onginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ }# X4 z* ]' G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. n$ c0 h8 r9 t2 t4 b$ S* u
* p6 h5 e# Y. S% I' T# N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured." _2 g# D; E! f5 I7 d: O+ y2 g4 W/ G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ ]4 L9 f0 _; Y2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 K0 {4 y7 \) q, \' r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 H9 m4 y0 `, h$ ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  O( K* M* H' H; c6 t2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( Z: i0 u# _8 n/ p
解决方法,移除低版本的APR (1.3.9)5 Y3 l* W( q2 q/ O8 Y4 T. Y! b% n

( ?3 g( L9 W& j) v' Q9 ?yum remove apr- H) l: ?( d! Q+ o: ]5 K8 [. Y
5.Error.log中有: Audit log: Failed to lock global mutex' O: m+ F2 J- ~9 f+ |2 J; h! H
' q# F, ], O+ w
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. E( c4 {1 r8 N8 b( F( A* Dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 E: D. a: E7 R0 p解决方法:9 _) ^% {; i6 E" f
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ u1 Q& G- s$ S8 }! s

7 }% [8 B, l, _5 v2 l9 l- }" m# @SecAuditLogDirMode 07778 x4 D0 b/ o3 T7 [
SecAuditLogFileMode 05506 C+ E. u5 o8 ?
SecAuditLogStorageDir /var/log/modsecurity" h4 f0 C$ A4 x( i" j" K( S) [3 n7 p
SecAuditLogType Concurrent
+ z) N1 C' R: k0 X7 Z9 Q参考文章:' H1 A* J' l$ C
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* i: B; Q. Y" w
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|第一站论坛 ( 蜀ICP备06004864号-6 )

GMT+8, 2026-7-31 16:04 , Processed in 0.071498 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表