找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13084|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ s/ M) `; P) S! V& ~+ ]

/ w" k- J$ x/ @3 I9 A一.准备工作, f% f5 a- c7 C- u1 o

8 K: N  {% `3 \7 t( m: P' z9 s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) z; G0 ~& _- b) l- Y
2 Y6 a. a" g, T) T; otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- E: n* f8 z: {3 S. M2 ]
8 s* X) a- ]% k, v2 m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 ^8 G2 d" ^& p* ~! Z8 n- T+ i

# q! N% [$ B9 l9 w7 h- D4 T! Q. c8 ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ @$ X( ]2 J0 P* ~9 Q* X# W( `* {2 U+ E3 o6 d6 z  p: v- ]0 K
依赖关系:" H0 a8 c) O- T; I
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; ]+ L9 P. r7 c' d9 r1 J' C  W3 M, e" i6 p+ [: G7 ~
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
, F$ [1 [" ~- W7 umodsecurty依赖的包:pcre httpd-devel libxml2 apr
/ Z- P7 [' @* Y% j+ r* A7 {9 r  }, s# C0 @
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
( z1 N, G( Q* n7 R二.启用standalone模块并编译& @1 G+ V1 T5 L; ?
) m( N# u4 L+ l9 L
下载modsecurity for nginx 解压,进入解压后目录执行:2 Z* g9 t' r: f' b5 H3 j2 }& q

  z- y. v( g  S, K3 n- T& ^./autogen.sh
% D. b- U6 R0 _& Q- z6 ?./configure --enable-standalone-module --disable-mlogc
1 X" N! i, J+ i1 c  t6 d$ m( lmake / }7 e% T7 L0 h/ O* f3 C& s9 L
三.nginx添加modsecurity模块" K* c  l8 j0 T$ A

/ Z' n: O- H( X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:7 Z" ~/ N" U4 r- G/ O: O/ Q
% D& \; i: k+ I# Z2 }+ k+ H9 B& T
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 S- f2 s" x% Umake && make install
& Z& _" r0 K$ Q; ~# u四.添加规则
& u/ W$ n/ `8 ?+ }; ?" w6 G" |" k; s( p6 B
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 Q: M1 o% W% `6 [0 J1 P' w, x" k
+ s) _9 E3 [, v3 @. E1.下载OWASP规则:
0 Q3 S+ z0 l8 c9 d6 S) C
- O4 P& s$ z9 P' g, f* t; r  hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
- Q. a0 d$ N  a4 |; {1 m5 Z) W* P9 R! S5 V. h
mv owasp-modsecurity-crs /opt/tengine/conf/$ F! T1 K5 t5 z# B' [2 A  i6 I! B6 ]

6 j( v! @- P# f3 t4 [cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, e, J$ i5 b& i3 E6 l9 g
2.启用OWASP规则:
/ C% J9 `1 C& T- ?5 ^  a  ^! ]: r" d! L
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, y/ \% r2 T6 Z0 E& ]

+ }$ x7 z; f, e8 i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' t9 Z: ]- i* r6 K
  G. f5 `5 y8 m
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" ]/ Z/ K! o2 j8 Z, |- J3 i* i  O3 ?: }! d- A% {
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, o9 W4 f; z7 }1 Z" P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; x4 Y, P8 M" G& xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 ]1 H, f! r' u/ S% c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ ^+ ?0 ]9 y4 ]7 F* e6 d6 S) L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 w3 M2 h4 r" [' Y1 a2 t  j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 p3 Z5 r/ o+ c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 \% B. {* i& f. p' z# i
五.配置nginx. ]- _) V2 [( s0 W7 \
# o( P+ c0 G. [8 T" T
在需要启用modsecurity的主机的location下面加入下面两行即可:
/ c' Y7 H  L( f  n8 \  C
$ F0 p0 N$ o. b5 i. }ModSecurityEnabled on;  
! t  m/ _* G! K) `; ?  k9 \8 F( KModSecurityConfig modsecurity.conf;
0 g) R8 M1 M, N5 M下面是两个示例配置,php虚拟主机:
2 S$ ]) J) {" t$ @+ ^' Q1 b) @8 D/ @
server {
7 l6 ^$ N, l' D2 A6 b7 D# W4 v8 I      listen      80;, s  }, v" Y" l' d5 G% Z* h
      server_name 52os.net www.52os.net;  S" |0 G* D  V: ~
     
" J5 @0 p6 @( y+ b2 a+ h7 Z7 ^9 G      location ~ \.php$ {' C! n, h. F3 }3 Q# a
      ModSecurityEnabled on;  ( L% T6 p9 P/ h( M7 u' t* U: z0 W2 Q
      ModSecurityConfig modsecurity.conf;
  U* r% \8 d+ T* L/ w
2 K- h: b/ F5 i+ A3 J* {" F  T: i4 |      root /web/wordpress;9 C5 F/ r) y0 U0 _% ], w! U
      index index.php index.html index.htm;. V% e  Z0 e1 `% S& \4 i. z! ~, W
  ; k5 @2 s/ Z( ?. Q7 l" M
      fastcgi_pass   127.0.0.1:9000;
: F! P) o' {% H/ q6 q9 {6 k      fastcgi_index  index.php;$ n3 m4 w/ q  q2 L! z  @7 |
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
3 o2 j9 e: ~% H: |! l      include        fastcgi_params;2 j( F# Q' I( ^, Q: q; g
      }
/ M5 l; o6 N  ?  }
0 x6 v' `0 @9 o" Wupstream负载均衡:
  u$ I9 T/ t3 m8 G' z. b, w7 G! y8 |! D, a+ n3 u4 T" G# y
upstream 52os.net {, b' j4 ^) `3 H* Y3 P7 r! ]" S7 b
    server 192.168.1.100:8080;* Y2 P/ \9 E, S8 a. l- e) x
    server 192.168.1.101:8080 backup;0 c* ?: v3 L* f( w
}% o$ o$ ~  f; x: \0 v
7 K& I. `) {. l+ Q0 l: E3 M
server {: B- {* }- I0 c9 c0 D
listen 80;
3 b6 U  ~4 {- J1 s  K1 U  @server_name 52os.net www.52os.net;( j5 q) h* ?2 s9 K4 w

3 q. _/ i8 D2 \* G3 {location / {
6 Z* o+ M: A# D+ Q4 _2 E    ModSecurityEnabled on;  9 s# B5 Y7 G; L5 x! H: I
    ModSecurityConfig modsecurity.conf;  
5 H6 t8 W" g! v" b. b5 r2 P
0 j$ F4 m# f% v4 R! e2 D        proxy_pass http://online;
$ d* M: K8 h' V: G- `        proxy_redirect         off;
# l) w* m( l# E1 N7 |: c        proxy_set_header Host $host;4 f8 R+ g5 ~/ i2 O% b
        proxy_set_header X-Real-IP $remote_addr;
' A" ]) G2 S+ M8 Q0 [6 F% k        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;/ M! e( X) w3 S' W/ l
    }4 F2 H* D2 A7 r3 J' L7 N
}1 b6 e. G5 h+ X+ v( `5 T
六.测试
+ {4 z7 d- x# |  a' v) D
: j1 l9 h" X# ^! T$ O5 f9 L2 W- |; H我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* Y7 ?. J- s+ G% O& T5 j/ o  I
! b" @. n  h+ I# Z; G  c" h<?php
, s' ^) r& z3 Z( p. R    phpinfo();   
0 @6 A: b3 E9 p9 N; C0 k?>* }( g% e3 }# @* S
在浏览器中访问:8 j( [4 G$ [& v% V# c* j
5 ], p8 E7 @: S( h6 Y
http://www.52os.net/phpinfo.php?id=1 正常显示。
% c0 |0 K6 r4 [- U$ |2 }+ Dhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。; r  B7 k( b; }4 j! p
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  c4 r% q: d) B& f3 K! `  ~! v9 g
说明sql注入和xss已经被过滤了
: y% ^6 a# y8 D5 T$ x
/ o! i, X/ L) S: [8 ~5 I# y$ U8 G七、安装过程中排错
4 e/ F" b+ X; Z+ l6 ~8 \! y; h2 S: W8 A. l$ {% h
1.缺少APXS会报错5 h, f+ \: m3 Z( D% k' b
! F% x* d. o: |+ O0 x2 q: E# ?
configure: looking for Apache module support via DSO through APXS
1 B2 o4 D4 P* v: Z0 }  ?/ econfigure: error: couldn't find APXS# `  I/ a- }% q8 A2 B- F* O
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ n" k3 X5 f4 o7 h3 T, Z+ g4 ~
解决方法:2 j6 Z/ D' z9 K
2 i2 r# e; E# B- b- N. X" s  x
yum install httpd-devel$ c/ ^2 b3 q2 G0 B9 z
2.没有pcre4 O6 ?6 \5 B8 x2 }# G5 |

2 z/ L8 G9 A( j/ B+ t) f4 `4 qconfigure: *** pcre library not found.
- J. K) A( `1 g' e! d$ `7 o/ Y1 Jconfigure: error: pcre library is required% j; z7 o& q: e! \/ U" L4 ^
解决方法:: j9 Q4 B5 ~7 i9 S" x' @

; |$ Z* k7 C0 l) q6 R: k# `/ Xyum install pcre pcre-devel( H) E- Z3 k$ p( y) t& a
3.没有libxml2
* J2 Y) f8 I$ u3 v& a8 D% J6 D9 `( v

: y; W+ ~' ^+ |3 R# d& a+ J7 j9 iconfigure: *** xml library not found., p. {6 o7 L" y: k
configure: error: libxml2 is required) X' ~# _- v  S6 Q' e: u3 h7 R
解决方法:
6 N& w- ]) V# X
6 k' W* ^& K7 ~! g9 V, v3 N% Vyum install  libxml2 libxml2-devel
; M+ ]8 u" r; I( l6 d$ w4.执行 /opt/tengine/sbin/nginx -m 时有警告; x4 ^. Y' c  @! z5 c
) b6 V8 T/ b% Y4 m3 _$ T0 {
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' n" M( e7 l/ A) S
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!. J# Y4 s& E8 i( u8 T
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* N/ i1 I) ^0 @) P( T  o  I5 ~$ `

) d& R5 X* H  {% _# |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 u  D0 C6 |$ L9 M3 }5 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 B  m% }6 Y# F9 |! r" S" S( ^2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 s1 |5 `* T0 \% l% j+ T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* e, L) `! ~6 l& Y: L: s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! t5 [' ~& l; h  O# n2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; R: k3 j& h' q解决方法,移除低版本的APR (1.3.9)# B# m( O  _" F& C3 _' A

5 B3 C( Q4 N- ^6 h  J3 Myum remove apr
$ ~7 V* w/ g0 W) g" ~( D$ O2 ]5.Error.log中有: Audit log: Failed to lock global mutex
' O: u( X2 _1 @$ |# L+ ?4 E8 }' g0 \/ f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
+ c) U* o: q9 S9 Q( j9 F) ~& a- ]global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# q" B! m* M, b% j. l解决方法:* Y6 ~. u* U) ~
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& l8 O1 y* l! Y9 a8 p+ n4 }7 L( p" T% D2 _. m
SecAuditLogDirMode 0777
# j" z3 b( w' |! c7 |# MSecAuditLogFileMode 0550
# `+ M; X. e) T8 S+ Y, mSecAuditLogStorageDir /var/log/modsecurity
$ ?, X; _9 t$ ~8 K0 PSecAuditLogType Concurrent
- s7 Q2 `; ?& S2 Y& v参考文章:
3 Q. X6 M. y6 H" nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! H. `, s4 x2 K$ Yhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-12 10:40 , Processed in 0.048857 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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