找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12666|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。; J( |2 e9 o0 A. I$ }- `% x+ c
2 T7 d2 k  w- w) n
一.准备工作
& U4 r4 h5 ?: l3 [1 G( x! e
' Q/ f) i6 s1 o/ b* p1 e+ E: \系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 s; b) D' c* q  v6 p5 f7 B" I& d$ g" u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 D- A& X0 V* k8 o4 i; E) K* t' U, x+ G6 Z# V, N; N" u  l
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ \; A3 I+ H. h  n. m# h0 F( W( @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: u' _& z* r" v& K6 b
3 M4 H; E( p9 E+ b2 z  O$ `依赖关系:
8 ?# L% s2 C* N  o  Y1 T6 Wtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' L3 o1 z0 D% Z6 X8 ~6 W! a: U( i. O, ~" ]
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel9 ~) \, Q0 Y8 y/ g7 o
modsecurty依赖的包:pcre httpd-devel libxml2 apr, H2 p, f: Y! \3 y* r  \
+ n3 K' d$ ^5 U4 P
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
: |5 }- k$ L- p& U2 S. d2 k! j( ]二.启用standalone模块并编译) q+ C# S4 W0 @$ h+ o, Z
4 E) g8 |7 ?! V# P4 c- i8 w9 B0 r
下载modsecurity for nginx 解压,进入解压后目录执行:6 ]; ~! Z8 A5 C! r9 N/ U

, H0 }8 }9 K* o$ r, _/ C# ?./autogen.sh
" D0 _2 X& X) P( y0 T/ ?./configure --enable-standalone-module --disable-mlogc
7 X! T2 C* E( b# G: ?, X, Y8 Imake
5 A  f9 q, d% _8 k- |三.nginx添加modsecurity模块' j" P) `$ B+ v# @/ n

2 e6 O" M. \! S9 w在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 X( x! o3 H' g$ e' L$ \1 q( V3 l. J) n
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& X$ p" k, ^& F' g# ^, Y! w& X8 P2 t/ J, Omake && make install7 P9 K$ N. S; c. C( |$ x5 m) `
四.添加规则1 \8 |/ `4 b& d( j
6 {1 ]/ G# K% ~% t  c+ C/ Y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; w8 m/ Z/ ~1 Z9 A8 j) W
0 ?: i$ J1 ]3 R6 E' Y% y
1.下载OWASP规则:
) Z, `9 ?& Q9 X9 y7 @: x5 C9 }  I& t/ L, `5 e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ F1 d. \) V" ]* J8 h0 L" D
' a1 ^/ i3 P- _9 P* G3 pmv owasp-modsecurity-crs /opt/tengine/conf/% o' P5 z) M; ^; j
2 x# C  w& @6 {! k
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" [/ g- q! z) }' [# @2.启用OWASP规则:' {$ p& P; f! i
" C  k" v2 h8 y7 T
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 A6 L3 L, F* `8 x. @5 f, w

5 g! ~1 Z. T* u0 y! j: p% }: {编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: i8 l( E7 [/ h+ K5 Y* I  S, |; U2 G3 `+ E& y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 u. w$ I7 F  l! w( Y

7 o- N4 C' D$ K5 \+ u- `Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ E- n. f" h8 m: G7 L2 e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: @# O) \5 }2 ~, z) M% t; MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 t" j& \# D; l2 Z# I1 ?) S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% u6 ^: Y! }' N* P$ T! t9 g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) q0 X. k1 X  t' D4 U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 g$ _' `/ w, d: g: M9 S5 j' uInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
  a: ~! s! Y. d- {- P五.配置nginx
8 i+ h4 L9 u% G( t5 A8 {" d% s! V2 a+ d  c: w3 o1 q* z8 o, o, J
在需要启用modsecurity的主机的location下面加入下面两行即可:9 W5 g$ x1 o; @

; o3 R# x# K* V. @8 g3 lModSecurityEnabled on;  
% H0 D5 q& ]; \/ x6 bModSecurityConfig modsecurity.conf;
; c/ F' |  f3 G4 H' |下面是两个示例配置,php虚拟主机:
  s6 ?  [8 a/ X& ?( m. R
+ @: G; w; v  m& d- @" ?: _server {3 s; f# q4 S% M- t% W: s9 u9 }! n
      listen      80;0 U4 J. r/ T7 ]4 j  s
      server_name 52os.net www.52os.net;3 D; t. Y: Z9 c/ E7 H
     
/ f0 r9 g; t; x1 y      location ~ \.php$ {; j, |1 g% J9 r# O
      ModSecurityEnabled on;  ; ]5 n. w& o% t
      ModSecurityConfig modsecurity.conf;
7 ?/ {9 P5 Z6 J( X; d2 n* R) V1 @# `* V
      root /web/wordpress;8 I, F% z# X# }& O# W4 y1 [3 h4 U
      index index.php index.html index.htm;
5 Y: `% p. e8 ^! X# t  # @" H) O( y. ]  {/ c) C
      fastcgi_pass   127.0.0.1:9000;: ^+ A- \* b3 q* R+ F
      fastcgi_index  index.php;
8 }1 Y8 V3 n5 N- P! C2 d      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
" w) d! Q$ D+ `# g& F/ O3 C      include        fastcgi_params;7 U7 b. l! _( T9 `: a8 T
      }
" @* w) o( e* e7 t7 M, d* C  }9 p6 ]9 ]" ]! f, U+ x
upstream负载均衡:
$ t0 ?$ y! N9 B9 s: m4 ?
! u/ @- Z" j2 @6 Zupstream 52os.net {: p; z1 h0 @" T3 L
    server 192.168.1.100:8080;
) r( U# S2 U5 z5 p; e) J' s2 I" N    server 192.168.1.101:8080 backup;
7 \7 t7 J" Q  z8 j* T/ O  [2 V}6 @6 O. w' H; @3 z

/ g5 z) Q* K! {3 yserver {
) d$ P+ l! ?/ h5 o, _listen 80;
& {6 t, E( V" a: V/ ^% ?* J. Yserver_name 52os.net www.52os.net;% X9 R( i9 X! @6 o( v

2 Y3 L  h9 b$ X% O- g9 e$ tlocation / {" c* A% T0 E# ?" _  X
    ModSecurityEnabled on;  
* }6 I8 B/ Q: Z! A) \    ModSecurityConfig modsecurity.conf;  
) L7 G2 r9 B& s* P$ k1 d% g0 e+ _
, e$ \! }# v. N5 f        proxy_pass http://online;3 E- y1 M0 }; O7 b: Z
        proxy_redirect         off;) H9 r* a: Q" |( Q
        proxy_set_header Host $host;7 t$ B% U7 }1 |- O7 S! O' \( ?" w
        proxy_set_header X-Real-IP $remote_addr;! h2 @; ~: I) q; N0 U
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
5 h+ `1 Q. w, O# W8 P% h    }3 y( p% c% D0 a+ s5 J2 s' {, s3 Y2 a
}
- g8 `) U2 O6 T9 M* `& M六.测试
; {7 Q, _% B; {# e* h8 J! M4 n$ q$ O
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 N7 R" U1 A: p- M: m
. s- W- v  E5 A( }
<?php+ u8 o( I8 F( H% q/ r# _% O
    phpinfo();   
) C: b- w# b: k0 G# \?>
. u( [* }: m: X6 k在浏览器中访问:2 h4 Q: v8 e) h. N0 C3 }
5 `7 L5 z( d  x  X0 V
http://www.52os.net/phpinfo.php?id=1 正常显示。' l  N% o- u0 N: v% U3 D7 c1 a+ y
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。8 c7 V7 N( G8 O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 v; a8 \0 M0 v说明sql注入和xss已经被过滤了. j$ K1 }8 M) U

2 N8 V5 Q& s/ l; q5 S+ D9 i, ]# t1 ]七、安装过程中排错! M: ?( m6 p5 }0 F

% W! t9 m! b; T* M% `1.缺少APXS会报错
( E4 y0 W% I* a7 x: |. R
3 B' @3 U+ ^9 v, U; fconfigure: looking for Apache module support via DSO through APXS
6 C! E2 w. c6 d" ?configure: error: couldn't find APXS
! N) F0 e+ V3 Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! n: A. _5 z0 A9 P( Z
解决方法:3 F% E4 e% }2 {

: X) s) c! P0 `9 jyum install httpd-devel6 B6 B" C) `' f5 F2 V0 c! Q
2.没有pcre
* b# G' J' Y" e, o* I  Y& M) W; x
& j' M; T$ m) D8 |1 V( n! Zconfigure: *** pcre library not found.
! c. j) C' J9 @* ?- ~configure: error: pcre library is required
! J+ P9 B) v  L0 P5 d, }解决方法:9 D4 t: T/ {; U5 h# A9 W

+ a" E4 _6 Z0 {+ ?4 t4 jyum install pcre pcre-devel
. k' ^  T1 ?$ g, i3.没有libxml2
, ~% M  h6 n' u$ l8 j8 c3 e, F/ |! K# \6 \( x8 B1 D, l: W. r

$ Z0 N2 m6 s2 o4 {9 m- i  ^. Lconfigure: *** xml library not found.
5 R  T6 N% m5 i0 n+ B3 Xconfigure: error: libxml2 is required
) F0 ~( x/ q8 N7 }5 H: B1 Z5 e$ x3 w$ R解决方法:- }4 s/ Y5 U) [
4 {: h0 R, a* ]! |  l0 O
yum install  libxml2 libxml2-devel
, ]" D- g( G* }+ W& \4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 S0 N$ [! F9 ^2 H9 K$ N1 U
7 k! F" L% y) D- [( j; K4 dTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 e3 Y$ Q) J, r6 snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* b& N! K9 Z" c) V6 c7 t# b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& W0 t1 B# U+ [' W; w8 H
. A# @1 J2 n) @. L( G) {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! E* _$ v% C5 ^0 h/ P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
+ z) M4 O* j2 `- i3 q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
4 P# I/ f! W+ F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 x# }( ?5 M# C2 W# J5 G. r/ A4 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ t7 T  S" d% X0 k2 ^: t2 e
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 h( M+ F" I# C- B解决方法,移除低版本的APR (1.3.9)
* M3 ?, T' A7 q; v; j0 H
& J4 B; S6 @9 C$ dyum remove apr
* e0 p8 I8 ]- U5 h, Z0 |9 ~8 i. V5.Error.log中有: Audit log: Failed to lock global mutex( ]/ k) @4 R, N9 @/ o) N
. z& X# U% H" W+ n7 X4 F7 y  L( O
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - D, ~$ V) z  E# F2 F( c3 ^# m
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: B0 O' V6 l9 |# z  o
解决方法:
. ], y1 k8 p& m+ [1 U" \7 Q3 j; |- {编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ T! K; c: O. m) `( @; v
( J) \' s9 S7 p0 `- f1 l
SecAuditLogDirMode 0777, o1 m; N' f4 L
SecAuditLogFileMode 0550
0 v  b& g" i; S% j* b9 Q! @SecAuditLogStorageDir /var/log/modsecurity
7 ?# g5 [  E# ?. n4 g, FSecAuditLogType Concurrent
* o% ~& \" w) e- L$ t参考文章:
, I1 ~* ]& o! Y8 X9 Bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' G" Z; {9 M9 X4 d$ s0 W8 whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-28 02:58 , Processed in 0.085766 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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