找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12719|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 p; v: N5 ?% j9 n3 r8 n+ b- H" t3 ~3 H
一.准备工作4 E6 u, ?0 G/ e; O

! I- B! L$ x  ]5 @3 t; n! }. K. B系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! w+ j0 I: m1 {  q  T, [
2 H6 c1 E" t0 X, y# K4 Htengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 k  u) g  l/ g( p$ z; c. M
+ f# d$ D; j2 o1 r, B, ^modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ N5 H1 H0 t3 \5 n6 Q  ~6 `+ E7 I* U. T; r$ v/ z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs8 J* y9 C( y4 }! X$ k  H
! e8 ~  E/ e/ P" ~0 C" b: H
依赖关系:
0 M. p) I$ V; k% ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ ~' l+ {7 c/ W1 W6 Q
  Q; [( w0 S0 C5 @6 s' U8 X# w
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel* V8 C# u1 U3 i! i
modsecurty依赖的包:pcre httpd-devel libxml2 apr8 c" Z: |2 n' q! ]6 {0 C& A$ t6 y

. f1 N1 g  O2 ~( D' I' Y4 jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 _. j# T0 e" t' U1 e
二.启用standalone模块并编译0 h8 b$ K! L) g' x

" R8 d1 q6 H& R下载modsecurity for nginx 解压,进入解压后目录执行:
5 `  h: L; P3 b, N, n, I2 h
( x+ S( L3 f" O+ |/ m7 {! z./autogen.sh6 G/ i5 m) W* l2 h2 P  n
./configure --enable-standalone-module --disable-mlogc( c, q/ v; T; u
make $ N9 T2 @$ v2 {, f
三.nginx添加modsecurity模块
+ I, i5 ]8 V6 I1 N! p
1 k) ?, p7 k, m2 G- w9 M% l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- `& c% W0 I' y+ H( ?" ?5 A+ u
9 I( P5 i! z. f; }, v9 z
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* F/ D% k. m. m5 Q1 h1 V0 b$ \make && make install
2 Y. @6 w" T+ ~/ z2 p8 l! J) L$ Q/ V四.添加规则
! [" r8 b0 x- [. v5 I/ D& q. t' S  z6 O+ \, F/ P
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。  f2 B8 s/ I9 M) t4 |: @* [3 X: N

( J0 l' T1 Y% E. U" v. ^1.下载OWASP规则:6 y7 C- U2 |% j0 Q

6 f$ _* E$ p7 j0 P! z% M9 D8 kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs: h+ e" C0 \' A# S

: j1 D6 I( X  L/ a6 f: lmv owasp-modsecurity-crs /opt/tengine/conf/
, ^+ [: l5 t+ ]! ^9 p+ Y  K  W: R$ A- A7 {" R, D  q' Z% L1 `, G
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 q! @, e& h+ ?8 e
2.启用OWASP规则:
3 y" @6 L' r/ M7 K$ h9 ?1 f) w* e  N9 ^+ q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ j* t: Y  o& @% J: N  T1 }

% m5 |/ y; Z3 U! v, n* y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 S) R  e( P$ p; W7 S5 i

  `* W9 j4 {  D: Q3 mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 r8 K7 D( K) k7 ]) r' g
: q+ W/ X! {% g0 X0 T& d; xInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 ?/ ?0 [1 Z* DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 y  x8 D) d' K7 T/ b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. X' B" J( h& o7 H, _6 Y/ b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 \* N) d) U3 Q" u  f# oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& [1 e: C" D$ G! Z3 }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: p% b# k" {8 m) f0 Z& bInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
- K* b0 a1 ~1 `五.配置nginx" T4 F( n' W8 q' U

4 Q2 k# i4 Y- v& n, c在需要启用modsecurity的主机的location下面加入下面两行即可:
2 B3 N! e0 z% n; ^% s/ Y, t- t( ?4 R! x0 F
ModSecurityEnabled on;  
- R. z8 U( D% gModSecurityConfig modsecurity.conf;
7 E" w3 }; U7 s$ X- m下面是两个示例配置,php虚拟主机:8 o% Y5 @! @7 f& [& M: ^# e: D+ x
. q7 v! Z3 g) i$ j# ^6 Z
server {7 ~8 j( v4 h# T- P. Y4 P. ^
      listen      80;9 r" p/ `* ]: n6 A) [  ~( k# N
      server_name 52os.net www.52os.net;+ A; |/ C7 c  y3 O1 f) z% x# q
     
2 J5 @4 p/ u# b8 A3 X" i, }      location ~ \.php$ {
0 E8 [8 L$ l# S* C& S      ModSecurityEnabled on;  : P- L2 t1 V) ]0 Z
      ModSecurityConfig modsecurity.conf;4 w+ g7 V5 u! f8 G

8 [) v% Z: d) u: u! ?# Z      root /web/wordpress;
( I, L8 }) ~- Y! |0 o% A% @4 ]9 U/ c      index index.php index.html index.htm;
- k3 {: r( K# D& D& x6 E  * W) M8 M; x$ z( N6 B+ o" I0 G
      fastcgi_pass   127.0.0.1:9000;
# D6 ?. H2 w( g      fastcgi_index  index.php;
' h# E9 o* M# G' J$ s1 d      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 O5 m7 `6 ~+ C
      include        fastcgi_params;
9 J: F$ _  `# {9 t5 @      }1 s3 z1 V$ ~' O8 ^
  }
% [2 I& ]# x. p0 I' Aupstream负载均衡:( T; C4 O! e9 o" t6 q0 t, t) H
) B1 M: Q' h2 V
upstream 52os.net {
& z( N3 d  n6 t  Y- d7 b$ c, R    server 192.168.1.100:8080;
: a$ I, c( m$ o! F) y* [1 X    server 192.168.1.101:8080 backup;9 }3 B% |3 l9 N3 P2 ]; q
}
, k! U2 J2 T7 |! r  j  }. `* ?) |+ B0 _! u5 u
server {
+ i+ g% {8 V7 Rlisten 80;
, A0 Y$ d- {$ W! S" A* T6 ~, `server_name 52os.net www.52os.net;% E; D+ q$ y+ n$ b
- i" }4 e, o( V/ W
location / {
3 r, e- d, o0 Y8 g! y    ModSecurityEnabled on;  
/ I2 M' {0 z  ^. w3 L! R. P+ e    ModSecurityConfig modsecurity.conf;  
8 l3 F' }8 H6 F4 T2 ?! L# k  c! }2 j* [
        proxy_pass http://online;
, p: J% ]' m, T& T4 `3 [        proxy_redirect         off;
6 ]  j. W  X' u        proxy_set_header Host $host;8 F, T2 h! g9 c7 Q- G
        proxy_set_header X-Real-IP $remote_addr;
0 F; @% n! b! W& p! a        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;) E0 v) o( _% m! s& w/ y& ^/ h
    }3 J9 `4 ]% t: Y4 d
}
& L3 E- |) i9 g! \六.测试4 Q4 i3 W' `8 }$ Q* W  H7 a

7 }  ?- l7 a, D2 i. W" R- l1 O( Z6 o我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:! j' h9 }" I8 X; h- j* }
- P* B* S/ w9 {- L
<?php) M# ^( B% I% M
    phpinfo();   
; y* `5 @  |7 Y8 D?>
7 K) r# k/ o1 r, u( p$ d( J7 r1 F3 Y" n在浏览器中访问:, I2 B& ?5 `* ?) a  k  v: g

- s1 k6 Z$ g+ l2 `9 S0 Vhttp://www.52os.net/phpinfo.php?id=1 正常显示。
9 d2 w' s2 j; P' [http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; t5 G. o# `3 j& @http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 J# L+ a: o2 c0 ], |7 i, Y) u说明sql注入和xss已经被过滤了
/ I. S# c' J. {1 `
, R2 \" `9 H' e) O% K七、安装过程中排错
/ k1 v3 X8 t+ g: u3 @
& |2 R+ o: C6 l5 Y4 P* q. g1.缺少APXS会报错0 i# k' E% S8 h  r$ O
& {  K+ X9 a" u. q8 t: c7 F$ U
configure: looking for Apache module support via DSO through APXS9 Y7 R1 e. e. u3 F3 r
configure: error: couldn't find APXS
" a  S& c2 [! m1 Y3 L6 k( Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) j+ w4 W2 l  N0 Y8 e
解决方法:
' Y! d' \/ O8 P% Y/ S5 p3 t. k& B/ @0 H& M  ]" [% ]
yum install httpd-devel
( e9 H2 g# c5 P7 Y$ Y% a2.没有pcre' r" x/ a+ |' K- d' n  D9 ~
* P3 b- M/ [) E- Z$ C& W
configure: *** pcre library not found.9 A8 v9 p- c2 k4 X' u9 b/ a
configure: error: pcre library is required
1 Y9 ]0 h; o; z解决方法:  d3 k3 Z5 O" q$ [. h( O( l
0 W" ^- s$ c, a8 A. F7 g
yum install pcre pcre-devel
( B" Q1 b& q- y, K- |7 |" s3.没有libxml2
6 w5 h2 [+ M& `& s' u3 w2 t) `( N( t5 Q7 J8 v3 ^( u

' `$ {3 W' R" \% u6 Y) {5 Z0 |: T% zconfigure: *** xml library not found.$ ]% S6 I' M/ @5 z1 @. _1 ?
configure: error: libxml2 is required. t( O; E2 ]# L  ?4 j. S: a
解决方法:* Y6 P4 k" j" E. R( x8 }. ]

2 y! d8 |7 D. Z# {, zyum install  libxml2 libxml2-devel
, D8 u. O" Q  E0 T4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ s5 r5 n2 H- ^# Z- l
) Z7 t% n) ^8 I: }  b( oTengine version: Tengine/2.1.0 (nginx/1.6.2)
' R: y$ E# ?# ]- \* `5 s$ Cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!* Z( _. {+ B5 v1 q& o$ z5 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' F! [6 O- _% R8 @& H  H+ E& D, g6 {8 P& x/ o2 G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# U7 u5 I6 x5 k! ]/ `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! [, L7 X& j' Z5 H, R
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( o0 ~, n- v/ J% C# q# k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 c. P' T+ S% {# X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 T9 M0 v! o4 x( ?: r% U7 _- m2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& }( J5 ]0 [$ M0 X. K
解决方法,移除低版本的APR (1.3.9)) E1 {% z. `% Z, `

. p# E1 n3 u  \2 f! m6 Wyum remove apr4 T) y$ r6 x: k$ h' V$ C% R( {
5.Error.log中有: Audit log: Failed to lock global mutex
; u# D' ?4 o4 A7 Z( G' w
* F' ^: h. p! ~- F/ y% X2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
' o' I% e$ T+ Z$ i$ O  rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 D) B( u6 K6 y4 A解决方法:
; n1 f! Y" d+ c编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; @, \# c/ w" E- W( `7 |
6 w& f6 a! c9 |/ e2 SSecAuditLogDirMode 07774 T- G3 @; O/ V4 n' U# d+ N  s8 o
SecAuditLogFileMode 0550
, a, {1 }& ]( G) W# j1 h* {SecAuditLogStorageDir /var/log/modsecurity
* c% G2 ?  l# T; MSecAuditLogType Concurrent
- z2 q% W2 o/ ?$ H1 Z; o/ F参考文章:
4 i6 I) l" }- T8 shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# K2 u% j& y1 n3 A
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-3 19:04 , Processed in 0.071313 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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