找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12884|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。; ]5 t2 ^5 U( c% b4 @% A3 U0 ]
$ R6 `! n5 p" H6 `: _* p% g
一.准备工作; k5 r7 ]4 |4 O' G  Y  J2 g

% U+ u  \5 z7 `5 B/ j- O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 z8 U7 G4 D: m, X5 E
6 W4 G5 Y/ K0 G1 T% }' v1 C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* {- T+ I! H' i" Z8 @5 e' Z+ S9 t7 Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: c8 x  T+ K+ {7 l

" S: o( Z/ ~6 L6 v1 L" f7 f& ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- c: J7 f1 h0 ~, ]5 r1 ?' j
; ]7 ^/ c& r, X  s8 u( M依赖关系:
9 v; r) y' \/ u# H& j3 ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' v0 c  }! I4 ^9 j" F
. y' P, x# {, |2 nyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel0 F- l: D$ f; P
modsecurty依赖的包:pcre httpd-devel libxml2 apr* t! V. Y1 j+ M6 c# z; B

4 a2 o& i  @5 w3 g5 p. w/ Jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel$ X% J* }8 t: x- Z: j2 u
二.启用standalone模块并编译$ m' r- t  g5 N% D

  S. h. S" r! B下载modsecurity for nginx 解压,进入解压后目录执行:8 o1 O9 e5 R- T
1 Z% u- U6 P6 z& K
./autogen.sh: Y# ?5 w8 }! O! {0 S
./configure --enable-standalone-module --disable-mlogc, f8 B/ G1 G: W/ C0 t3 S: i4 M# x, P. L
make 0 i, P+ A, y, K7 {+ P2 }0 P
三.nginx添加modsecurity模块8 w8 {; b# J; t

1 T' E* o, v, I9 Q4 J2 l. l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* O3 Y9 B+ }9 P! r! ?* p- I

. U: k6 D  d+ m( C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 e; ]% S8 D3 Q  G1 i' c$ smake && make install
4 T9 ?' S4 Z# T* v, O- F& u四.添加规则0 x' \7 V2 ~' G+ K5 L7 ]
- U' }" W8 w) q: o
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 W( l4 ?7 g% s% r! e3 G, N  A' Y* f$ t# B1 s: L
1.下载OWASP规则:
: x" ?7 e& W2 I( [0 h9 j* e7 k7 }
5 \$ l& N8 a# g. o6 T) r0 Q. Qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 E6 e- u1 P0 W% p
: w2 D9 P5 r' O. W2 C6 S
mv owasp-modsecurity-crs /opt/tengine/conf/
6 x/ V6 M% e2 ~" n8 E
* {9 E1 k, j; r4 _, A; \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! P( o  h, n; B. S" I  w8 h
2.启用OWASP规则:
9 [3 o3 ?. v$ Q0 x: G1 P( {
: z' H  f2 H" a: s2 K$ V复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" |: N: I2 f7 x7 H' q
9 n$ V  T0 o9 w( c# j) o8 D3 c
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" N7 \# U7 ?  G: e' \. p; v. R) D9 v  k0 [% i7 P+ z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ b- l& I  |' E6 P" g3 p
* u3 p# l7 |# O( U* u# f8 mInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% _; O( U' Z6 K) P; l1 o1 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, w4 G. Z; n% R, E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ Z  ~8 }, ^- O! t# \5 S& ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% j/ Y( {5 b  c' `# i& _% R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 Z7 l) _. Y" F9 L' F" k5 x) \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! b) A0 m# C9 }) H# NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* z7 Z' `; U, N4 Z4 n; A五.配置nginx
0 j0 p6 _1 E& W2 J7 d2 g6 t  @! U- q$ n, Z8 Q5 E
在需要启用modsecurity的主机的location下面加入下面两行即可:8 |  J  Z/ }, m

  [0 C% J% V% O4 e2 u8 LModSecurityEnabled on;  
9 o1 r6 S& E7 e0 \7 ]) }$ E; b& t/ gModSecurityConfig modsecurity.conf;( F( C8 ?# x4 @& n; E/ J  l% U
下面是两个示例配置,php虚拟主机:: G- [6 }5 ^# Y1 n) R# ?

9 P) e" p5 }" m- h, tserver {0 N6 v# M! v* y8 A& ]7 d
      listen      80;
+ K+ u& G6 N. E  Q, }      server_name 52os.net www.52os.net;: q, x2 G" h7 J' N' c5 @
     ; U# b1 A* w4 k" d. y2 v- Y
      location ~ \.php$ {( A2 |4 J- q' V: n9 ~
      ModSecurityEnabled on;  
& [8 i6 T# f4 A$ X( K      ModSecurityConfig modsecurity.conf;4 o$ }" k& I0 K% A8 J* c

+ c* z1 t3 C! q8 l8 g      root /web/wordpress;
8 X# Y, X/ L# f- o% ^! A8 y      index index.php index.html index.htm;
1 e( g. }- }% O: p  I0 `( j  
" U0 J7 ~5 O8 {  h+ j( m      fastcgi_pass   127.0.0.1:9000;, C, D+ q( I- s, i" E( q
      fastcgi_index  index.php;
, i& k/ ]! C' H' A% h& h      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
+ O. r  B4 v1 U5 @% [      include        fastcgi_params;
! g: `( z6 O0 H4 b8 D: o. o$ K      }9 Z/ q+ C  u) F. k' r. a& g2 q
  }
1 S. R5 I- @; h" R" O; A9 f2 Iupstream负载均衡:
% T+ X$ N! P2 N: H1 {! a9 i/ r  u1 F  P: Y! {
upstream 52os.net {
% u( ]# a8 D: T9 o. N2 [    server 192.168.1.100:8080;
5 }9 J9 }: M9 _5 z* I2 \+ z    server 192.168.1.101:8080 backup;( h/ I& J# G: E) d( m& v
}
: m7 `) A6 u. I  h5 M1 Z
( F) S6 _& J6 ?$ L% U: wserver {3 h5 h) P3 s  m" h' e
listen 80;* x6 s- V& t1 C/ v- l
server_name 52os.net www.52os.net;3 q, y! n+ P& ~& z6 R
/ L/ L% c# k- K( w  B) T0 k
location / {0 n, c1 @6 _' U
    ModSecurityEnabled on;  1 P1 `, d. s6 i/ [4 f
    ModSecurityConfig modsecurity.conf;  % G- [  h5 V/ D& v/ G) u; l; q, w

! f6 G, P+ U: Y1 e6 L5 x/ K' Y1 Y, C        proxy_pass http://online;
+ W4 B5 @& T4 }+ r        proxy_redirect         off;6 E9 w8 [2 `% s9 R
        proxy_set_header Host $host;0 C: y* {4 q2 h7 O1 |
        proxy_set_header X-Real-IP $remote_addr;
+ A' v! J! l) y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: V- D4 D6 [/ u* \
    }
+ z" g5 u6 S9 h- F9 t1 A8 z}9 J1 L. [& N0 C4 z: N  y
六.测试
4 {0 ]$ o  M0 L' Y! t5 z2 L9 Q! p/ X" I4 W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, `9 L, ~6 _# r, {4 _- w
, Q4 p+ a! L$ \- I<?php! ^3 \* S6 r$ y. z* p
    phpinfo();    8 u/ t6 c% e+ t0 s6 m
?># ]- u% h2 {! M1 k/ H
在浏览器中访问:. m6 _9 ^" T) _8 V) S% Y1 q
' w/ z# d. E! n( {: P
http://www.52os.net/phpinfo.php?id=1 正常显示。# B+ }# Y2 e7 O# J+ D) |: [
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。5 V6 w3 ^* ~# h* O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
8 e+ ?3 I- p& T! l说明sql注入和xss已经被过滤了
6 a8 E$ b$ l  w$ W  }6 l0 T$ S3 z1 f3 ^: O
七、安装过程中排错
0 r5 w4 {, O% H4 ^4 J
" M- N2 m" S0 {: }) D  |1.缺少APXS会报错+ b: G( `) z. `8 h  d2 N# D

# ~* h9 u% O0 Z! gconfigure: looking for Apache module support via DSO through APXS
0 g+ J; q4 s  [, C' T9 jconfigure: error: couldn't find APXS
7 a; s4 z- D9 H) C  w; gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 Q. r) s! M; F. ^( a3 i7 o
解决方法:
) o1 q1 E; ~# R  x9 b7 r9 l% c" o. k3 B2 |7 s
yum install httpd-devel2 S% H! r. H, K8 Q& ?: `  M, _& s
2.没有pcre; R. E! e- f6 r4 x9 D
4 W' _3 g4 v- U1 c( i7 l% f
configure: *** pcre library not found.2 i/ _, n2 a% |# R2 D: p
configure: error: pcre library is required# p+ k/ v+ V+ U9 B' O# W4 D
解决方法:5 i- m, z' a. D# i/ h

& m; j2 h3 f( M+ a0 x: L' |yum install pcre pcre-devel$ s* `1 W; f8 Q7 o" g. F& x/ @
3.没有libxml2
: {$ ~0 v$ K8 G9 s( T! c8 T) B+ G" N
4 z  F$ x  p7 o5 D  N( I' Z% l$ r% y& \/ L/ u% e  X7 u
configure: *** xml library not found.
: U9 }6 ]: t" b6 ]6 `configure: error: libxml2 is required
* u5 q  [2 K; [0 h" E解决方法:& e5 t2 X4 B9 ?; w

8 P& }1 }5 Y8 V1 S- x8 T5 syum install  libxml2 libxml2-devel
& Y6 W4 B$ W7 Z4.执行 /opt/tengine/sbin/nginx -m 时有警告
) b( Z5 v- z; S& D5 H0 p- ], ]8 q: k
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
: K6 F1 m( G5 nnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 M' B5 [% v* Z6 C7 |8 V7 ?原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 a3 s2 n1 W1 H- _% U, l; Q" Q  e5 t+ b" t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ O+ ?+ G: h- N. Q# [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! u& n7 ^0 E& K9 D) _) h8 j; ^
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* D8 S% J$ r9 r5 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"/ r" w  }$ I" f$ z. {2 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
  C% ?$ W2 k; f8 f- R: l2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 ^/ X* a( t( _解决方法,移除低版本的APR (1.3.9)
: c/ G- Y5 W3 _. B1 U9 D$ e, x4 ~. y) I" s2 U9 Q5 t* [  ]: n: d
yum remove apr
6 \7 }8 g3 k# o3 F5.Error.log中有: Audit log: Failed to lock global mutex8 K" C5 {% Q" m2 |" ~9 K

9 L* K7 P% Y$ J1 y8 {2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
7 |/ n1 X6 B+ P8 d1 Y. P" Lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 p9 d5 T2 j! P7 X, U# E
解决方法:
; K! U5 ?- G6 O' C% S/ h8 ]编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 Z- g$ n2 {' w. A9 j" h8 X
, c" ^' h9 H5 X3 d) T2 E5 bSecAuditLogDirMode 0777
. l% j- s/ w: w8 qSecAuditLogFileMode 0550
% X* f2 L8 z8 n& \: \# h, xSecAuditLogStorageDir /var/log/modsecurity3 ~9 C* i4 J6 [7 M& k9 |4 u
SecAuditLogType Concurrent% k0 O# i* Z& w9 F0 Y: c8 d
参考文章:- \* O6 F2 M, L& T6 ?  f4 @) @
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 `' _! j: [6 M  p2 |2 I1 D
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-21 17:32 , Processed in 0.067341 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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