找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12340|回复: 0

nginx配合modsecurity实现WAF功能

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

/ n0 j' M! ^) T' l5 T# N1 D5 l一.准备工作
& ?5 c0 s$ m1 y! a$ k& H' S
( R$ S% i! v% N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 ]9 \9 t8 G; j, C; N) ?0 d+ x+ N" v. [8 q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. z( j/ k' R( g
2 ~) m- q( D8 M: V! P
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 @" R9 I% L* }) F4 Q2 F) G# \3 c

* H$ ~9 h! E3 }) P; k' u7 QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 f% ~2 Q$ ~( ~5 }$ T/ }

6 D8 c0 l: Z: g( q/ \依赖关系:8 @/ p$ j( a& C) h6 ?# R& ?
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: o- P  p7 s2 f& b/ U  _: X+ T# s7 I) i! a
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- A& J! e" L. k  }& h+ p1 M6 o$ I0 fmodsecurty依赖的包:pcre httpd-devel libxml2 apr: \# U2 {, |  n

& e1 y: @; Q$ b1 q+ h+ Oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 z) w( U# K( ]" f9 a. N
二.启用standalone模块并编译
9 r) _) [$ Z! c5 G5 Q* n- w1 ^* s' g7 U) v. P! v2 q
下载modsecurity for nginx 解压,进入解压后目录执行:: \" `4 Y3 S  f. q% ~

6 v6 R9 c& M6 k5 e% ]./autogen.sh$ l; @# h, O. E; |4 K5 V, d: }
./configure --enable-standalone-module --disable-mlogc
) w0 q2 O- [' d2 u4 gmake 5 p3 y' T) }8 t! U6 _
三.nginx添加modsecurity模块" I4 Y9 ]$ u+ {: ~$ ?" A' ]

6 t  J. i4 k7 N0 J6 b: b8 M在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ j! g# w  r1 T: l* N9 G
& n  z4 S" n4 a+ ^  }( a# Y
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 _2 |, L; d" t" emake && make install, v( D' r) V$ K6 Y$ y* D6 `
四.添加规则
+ H! F& N2 y, o$ j9 O9 @3 {) U
2 E8 ^5 ?2 Q5 ?! ^, R- U  Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 l$ H6 I5 b5 r( b% b& P
/ ~2 k, B% q- ^8 B% C) j- T
1.下载OWASP规则:6 C4 O- V3 i( a1 f& ]% }3 g
( j/ N* |& v. ?
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ W6 [: `% \% }* h( y, k
! l. E( C1 j0 j" `( |7 Dmv owasp-modsecurity-crs /opt/tengine/conf/- ]2 Y+ r$ ?9 G- \

# k+ K( T3 @; v$ X1 ~# q  Vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf  N5 y- c! [& `, ]5 c
2.启用OWASP规则:
* Y9 {# j7 U" \# c5 X( T4 F. U: o0 B  E
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  z; j9 J$ h4 f" i/ A+ C0 l/ A# j. P& u0 \7 P! \+ g5 Q- Z2 p
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( n: g, z9 Z9 \: ?3 N
, U1 N+ S' T6 w* I# n! _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 P$ V8 ^+ r* Y2 Y
( Q2 d3 R  G+ N$ P/ }; OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, A( Q5 k# p- Y! DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! e& y  ~5 Q3 V$ |3 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 V! V0 ^' B0 J- F# mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. A. p$ b: N% v, U% R* O* w7 tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( }+ l  l" e5 X+ t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# u2 P4 F: ]1 i6 E0 P2 M
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 Z, F' }2 O) \7 p3 }  H0 W五.配置nginx3 }+ A6 x! d% g, `9 W/ P( ^
% o9 Z( z6 T9 v" m
在需要启用modsecurity的主机的location下面加入下面两行即可:- t6 O' B* E( J5 s+ }& z9 V4 H
4 ~' p6 |" i) R$ ?% `# o
ModSecurityEnabled on;  
9 m3 ~8 ?& i' ^+ PModSecurityConfig modsecurity.conf;
! m, x2 h0 t4 U1 R1 u- z下面是两个示例配置,php虚拟主机:
7 f3 z9 {! ^7 h' s& P& F/ |
: M5 a* c: e1 G& sserver {
5 X9 c/ n- C* I" l* m3 K) |8 g/ h      listen      80;$ Y2 E  h* _; ^- _1 Y
      server_name 52os.net www.52os.net;
) v: k3 l; {2 z, `: Q) R4 |     1 }3 R5 v& e% f
      location ~ \.php$ {. e4 w+ e: Y# V( T) b& K
      ModSecurityEnabled on;  
. t: _7 {7 r. P6 H! ^2 U" `      ModSecurityConfig modsecurity.conf;1 P  E/ S% ~* y! }2 X2 m; s
; Z5 m5 ], e1 o0 H4 w2 H+ g
      root /web/wordpress;
% j+ y* e, y6 O2 d" m# ^      index index.php index.html index.htm;
* Y$ D& i, Y/ X* a$ a* j" n1 r  4 p9 A# j  z, N) q: b5 c3 I
      fastcgi_pass   127.0.0.1:9000;
  U: p6 Y8 N3 V      fastcgi_index  index.php;4 l% [8 I$ E8 T5 I! U
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' M. R) q0 {% k/ J0 `4 N9 h
      include        fastcgi_params;
5 _% o* Q# m5 t$ _* f1 q      }
+ {, @3 D5 O* Y  }" T, H6 c0 k3 d% B1 F
upstream负载均衡:, {# x; h3 Z/ I3 y' _; ?
" P6 v' m0 X  ]
upstream 52os.net {
) ]9 F+ }$ H& \& S- ~) v6 H/ f    server 192.168.1.100:8080;, e' q" S! e: ~& ]
    server 192.168.1.101:8080 backup;9 ~" X- g* X& ?+ n0 I) R
}
' Q. I2 J3 S  J+ v- I- [  e, d4 I" N3 N0 {  l
server {: ~! u& q8 b7 ^4 F
listen 80;" }' d  H1 X7 w  o" C
server_name 52os.net www.52os.net;) m8 \- p# c3 E, q* J! \

  \+ l9 R0 b1 J1 u+ alocation / {* w' U2 `4 @1 h
    ModSecurityEnabled on;  
& }. R' F* y4 [$ E* o    ModSecurityConfig modsecurity.conf;  
* V/ G, |# }7 N; Z. s" e: u4 T
2 I9 f) v1 P) Z5 Q6 \! w6 c        proxy_pass http://online;' r  I  }/ I. p4 {2 E
        proxy_redirect         off;0 d+ Z0 i- J5 i& M% k( ?
        proxy_set_header Host $host;! I/ {4 d0 C. l' U8 X. R
        proxy_set_header X-Real-IP $remote_addr;5 B/ f+ b6 K. @1 o
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
$ i& W' k) h: G- L    }
% s8 A. Y! U- B! \  |6 G" W}
% y" i, {( z! y六.测试, R- i/ x' Y1 p) w) Y
( E1 q8 ?- K  j4 R8 x
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ s1 ^4 w. D6 x" G, Z# x

; i, L  d! j  X2 \7 k<?php& T' K5 V( b1 _. T7 z
    phpinfo();    - a4 a0 F9 t# n+ ]: t6 g% Y
?>
' q% s4 k- A  I. |在浏览器中访问:
; N1 E9 Z6 Q  g' P2 D% ?( n' G# V- @- B
http://www.52os.net/phpinfo.php?id=1 正常显示。  _. a! S/ |1 d$ w* T  w" G
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
! v: U) J# V2 r$ ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
. S4 n. f1 R& Z$ V- t$ d1 [* Y; {说明sql注入和xss已经被过滤了# H8 I, K! u4 j2 P
4 X5 v) J8 f+ d7 O
七、安装过程中排错0 I0 m# t5 B6 A$ |
5 v  o4 i" C2 I% S! Y
1.缺少APXS会报错
) `5 o! k% ]) T9 k9 }$ c3 v1 s7 k) x/ I6 P6 ]6 J1 u6 ~" `" \
configure: looking for Apache module support via DSO through APXS
' U. F* U) X& oconfigure: error: couldn't find APXS
  z- @, |& k1 b* Q( t- K  `6 a/ H4 lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" A; ~7 }6 c  Y; K3 V解决方法:9 a. G  s2 u# W/ l# D) b
+ l9 K/ `. C, {- D+ {  O
yum install httpd-devel+ @6 f; x$ V9 Y0 s9 l5 r
2.没有pcre
! x- x* v+ N2 F% d9 p. j, c. c3 o* e8 a6 K* A! w2 t
configure: *** pcre library not found.
  r! ^  @4 y6 \3 {configure: error: pcre library is required
, t( c' f( K( Y8 j解决方法:2 X( f  O, {+ ]* e$ I
$ r" l' y+ ]8 J/ _
yum install pcre pcre-devel
3 B4 ~$ ]; X6 v2 [& e3.没有libxml2
+ A( V, u8 ]: I; j, q& j+ j2 n& h" H' r! L

2 [2 }1 ^. O& C( pconfigure: *** xml library not found.. ?9 o% C* w, Y( Z! ^  v
configure: error: libxml2 is required
9 ?5 t% f7 \4 }& l: j, a6 t解决方法:2 E4 o) K' _) ]- Y  q; Y( b3 D4 U

  y* F4 d1 V1 Y6 f, U2 L/ B+ ?9 Wyum install  libxml2 libxml2-devel
2 Y+ f; O: i+ l. _% {! H8 K4.执行 /opt/tengine/sbin/nginx -m 时有警告
" L. c8 M# E- j# d2 _; u* j
5 ^9 C, C( P+ UTengine version: Tengine/2.1.0 (nginx/1.6.2)6 H8 P0 B( D# L/ [6 N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
, E# Z' B$ ]% g* l" M$ y: ^原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( T3 j% I) {/ j( t) R; h7 Q1 N* H
- P6 E' c& X/ s, H" n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.  c! Z$ h+ b5 P9 `4 _  K: N) j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
% d1 z' K3 B; \9 O6 Q# m2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!9 S4 v  }- R% p$ ~& {/ O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% I+ P2 n0 F; j7 }! `* S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  M5 v1 U( l5 U
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. v6 C( A5 g+ F2 G7 D& ?" n3 s- N5 d" d  h
解决方法,移除低版本的APR (1.3.9)
' X5 d8 K+ `, i/ r! _* W* ]* |* Y5 O" R
yum remove apr+ [' }* s) V" D. w
5.Error.log中有: Audit log: Failed to lock global mutex1 x  K9 Y3 e6 G1 e# h6 o
( i3 v. R5 M2 p2 `# ?. L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 y7 S2 z6 b, J( {, E2 V" E3 A
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 J% S, y8 w% ?& x/ b解决方法:; G0 \+ Q4 n- E
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ ?& `& S" J7 z3 i# V5 k
/ m# X4 }' J3 e6 r! OSecAuditLogDirMode 0777( ~2 t# S- @5 Z
SecAuditLogFileMode 0550
" J, p' u6 }  _7 H: W: }; S, WSecAuditLogStorageDir /var/log/modsecurity
; U6 z& o2 q$ g1 h; y8 ySecAuditLogType Concurrent
: }* @( X" E8 n% k# r+ l4 I参考文章:
) c: {( p5 `4 o0 N9 {) ^  e/ }2 ~& `https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX/ Q: n: n: y( y  K  o# P; n
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-28 17:51 , Processed in 0.072471 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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