找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12253|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 ~4 L& W9 Y9 y$ {3 ?
4 C$ C+ w$ J( U5 v一.准备工作, i% m% i3 a' y; v

6 N" {- w( s$ |: T( g, E系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 t  l" r  K) C4 \  M& M
+ {: H; T: ]$ K) s4 S) \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; [( q5 A" A  L1 x7 j, q+ g

+ H; u4 z( x7 P' m/ n* j& N2 A- rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' s- v/ [) ~. S2 d" p! f1 k! [0 ^
$ y. e# v6 d" f7 yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( O" x/ e0 @/ `% {0 y% z- e: m/ f  C; K
依赖关系:6 c7 V. b  x- o
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 c) Y/ n% O% D' r
. s" r! X( \) B5 B' B. {, ~
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
! S+ ~2 h& g' j% [$ ^  R: Jmodsecurty依赖的包:pcre httpd-devel libxml2 apr* e- @9 t+ d: m- i( ^
- f2 ^2 y, r3 W4 B4 @: l; c
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel9 W0 [/ N7 O6 R. m" S8 c1 z9 T
二.启用standalone模块并编译
& X  O* ?% P5 U; U, P4 G# U# s* C9 i, `
下载modsecurity for nginx 解压,进入解压后目录执行:
6 G+ H/ E: p8 I3 x& G+ C6 l3 b% J4 t% _+ K+ ]
./autogen.sh8 e; Y2 _( e1 g& H, D- H  N
./configure --enable-standalone-module --disable-mlogc) h; u$ o# _1 z; C! Z6 N3 P
make
) w  j$ C3 d; c9 X! z, b三.nginx添加modsecurity模块
- B) l* |, Q7 t" i3 s0 S; F; n  r1 {; o! h% S8 U, X
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ d, O3 u. }% ~0 N
" J2 O8 `) A9 Q& y7 v+ v: m- ]7 M: l./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine$ [" Q# X# I" F/ W4 y7 E3 S
make && make install' y9 `! D* e+ c( v, ?( ]; H
四.添加规则
; C5 }- o9 X/ D4 {" m. F! k  R" [$ `2 t" C/ f6 r) |
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: S, x- U7 u$ U* V& a: N
6 G0 O, T! _, t- Q1.下载OWASP规则:
1 b1 [: Y  b* L9 ]# h% Y, u, k) Q; D6 F: n: |1 J" u. ]# ~
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 m; t. \8 Q9 w% [# j3 ^& _8 _

+ a' Q% z6 R) I/ W0 G2 P, O5 lmv owasp-modsecurity-crs /opt/tengine/conf/
* ^! v: r* I& O+ ?
! B1 U8 j7 h; y, L0 Bcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* N# W0 l+ `4 m* h$ L' f
2.启用OWASP规则:+ i2 o* p: Q" H, T8 ?( {* ]& r

5 h! W. A$ @) e1 p. |$ c" r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: t4 K/ f2 H* [7 Q" H$ u8 u1 T$ y* T) v8 n  N/ U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on8 X# e8 n; h# V0 q/ u/ i2 x: |& ^( {
( L8 b' v' v' C2 q4 |6 |  G
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ @" [7 C. B. j( ]9 s

3 Z; K) \9 P: r* S" Q" S. qInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 D3 _! M8 Q: K0 v; H6 q* ^! o! Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! k' e$ V7 g: s  K9 VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; p/ K7 f  W$ Z3 W! LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 p0 Q, m6 f# q% d+ X: z' TInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 W$ G' {' S1 @' C1 i& |+ s; k
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. @3 g3 i# L2 \6 Y, [" Z3 R1 [Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 Q0 I+ K0 E! w) g( u五.配置nginx
$ Z; X9 o- m1 y# E/ H
8 V( @8 j: \1 ^( |3 j# A# G在需要启用modsecurity的主机的location下面加入下面两行即可:
4 R; c4 j2 V! G! m: P% Q! o' Y
3 f4 ^/ [; e/ A5 Y4 r5 d7 K% u( _ModSecurityEnabled on;  5 \. p$ K6 D2 V6 S
ModSecurityConfig modsecurity.conf;
0 t, t# ?7 L! q; }+ K7 m- c下面是两个示例配置,php虚拟主机:9 A5 q9 j( q2 `% ~0 e

5 J0 r+ K  z: y8 xserver {1 @& I3 F% Q  P
      listen      80;
% k( Q2 T6 S; f& J      server_name 52os.net www.52os.net;) t( l7 D  H7 o7 ]% V" z! N
     " T% m, s6 F) b, M
      location ~ \.php$ {
9 P" w# n  k5 p# ~4 a. k" G" X# h      ModSecurityEnabled on;  8 V: a3 c+ D& c! l. c+ R
      ModSecurityConfig modsecurity.conf;
4 r8 b( J* _1 ^4 S  p6 I) e7 M
; c' A1 G# Y3 t. Q0 {0 J/ a/ p      root /web/wordpress;! I4 x2 U2 M, f$ j2 R; e2 |! k
      index index.php index.html index.htm;1 @& J: `. C9 M
  : w. W2 q. [" F" @* A
      fastcgi_pass   127.0.0.1:9000;
& B" P( p9 h" ~: A  `8 e      fastcgi_index  index.php;
" g/ E$ Q. G: z) Q, o/ z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
3 q( G1 S# Q+ E      include        fastcgi_params;% r" o/ U! M, u5 W% U$ a; ^$ B' g
      }
5 s+ ^9 Q4 C% A1 N) Z+ K  }
# l- |4 H! m$ c: m8 m3 I/ Qupstream负载均衡:
3 f3 O. G( u( O: j. a: X; A8 J' p5 _/ @2 [3 D) F4 E  D6 U8 E
upstream 52os.net {
$ c+ t' Q9 r6 L8 H    server 192.168.1.100:8080;
7 o1 ~( }" c* w: j6 @5 U: T6 w, p  `  n    server 192.168.1.101:8080 backup;
" w9 i" ^& q( Q5 C$ F, J}
. f7 W! i4 v3 C% X" k" f6 {: D5 M# x5 C
server {9 `/ T' W: _/ ^* G
listen 80;$ L5 \/ {' `. N. T  s9 e
server_name 52os.net www.52os.net;  l5 T- {2 E2 R
( m, V! Z& B. _- @/ A
location / {
  Q# e8 _( V% e    ModSecurityEnabled on;  . V: M. |: f6 j$ l* S7 x
    ModSecurityConfig modsecurity.conf;  
, r0 ~5 t! O2 c; Y7 y
5 e" B' j; k3 {        proxy_pass http://online;
3 d/ g; K7 C' {" t* B7 h5 N( Q& g        proxy_redirect         off;
- u) M' {# z- @0 r        proxy_set_header Host $host;
  x6 o% u6 P  S2 j1 v+ H        proxy_set_header X-Real-IP $remote_addr;  u. P2 t+ p4 n4 X. x
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% u% Q0 I- g. z9 l' [* C) A    }
7 V* [% H! x5 Q) P. [/ P; ^}6 e7 b1 [  b+ q( O* j4 G4 C. V: k
六.测试
1 F1 {* x, L1 z. M, R7 c
/ i+ c2 N$ D2 y. W6 B! ~我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 h- b/ @" M  g3 |  L9 m  r* O3 e' U% G+ j
<?php! C( z' ~: c" Y: y- D0 Y) s
    phpinfo();   
$ {0 Z: X) X, H# A?>$ R2 d* b. V. @9 G0 R
在浏览器中访问:5 a) a9 v6 a& X7 l, }

5 G* ^1 W0 V/ vhttp://www.52os.net/phpinfo.php?id=1 正常显示。. E( @, I  ~1 U4 l9 u) \
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
+ s7 t- O% W4 s+ ^, Z4 R) V& Uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! H) O$ f' p" C  ], i说明sql注入和xss已经被过滤了. x6 x6 O2 S# l- |

" n1 p0 G% Z4 B! `3 o4 I七、安装过程中排错0 s- C3 u' y. m% G( o$ X' Q, k  v

) k  U1 c8 B+ v. v  U8 K1.缺少APXS会报错# W$ E: Q" ^- A8 t3 ~- I

. ?1 V6 n  {9 t  j$ R6 [" ?configure: looking for Apache module support via DSO through APXS# C/ n& n5 k& \* @# S
configure: error: couldn't find APXS
+ S( I7 b2 L1 Y5 vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  H1 h( @+ d# j+ P; m- W# d解决方法:# l) H% r$ A) |( ]

) b3 p# R$ P' \! W# T. {3 D% }yum install httpd-devel
1 Y" O8 K$ F6 u+ Y0 o2.没有pcre
( W/ i% i0 A3 c4 ?0 k4 H
: z* x. |! d' Tconfigure: *** pcre library not found.
% y8 D6 V$ J! G2 z3 i" M3 y. ?configure: error: pcre library is required
- w: L' z) S7 I* m, k& q( q+ j解决方法:) T( E' y; y8 X

3 B' Y, U& x; N% I  I: P: Dyum install pcre pcre-devel# I, B- D" T: B6 x1 J
3.没有libxml2! d& r+ P+ `, ~1 G! v- a

9 C# J' P: T, |
4 v% l9 @" H+ v( t1 [; @; Qconfigure: *** xml library not found.
1 A; n8 O; B8 H! f+ n& h8 G# ]configure: error: libxml2 is required
: a! i2 N1 W/ Z0 w$ w, B解决方法:& X# R# E/ e' J" |0 T
6 M* [. k% c- o. i; C5 s
yum install  libxml2 libxml2-devel
2 d( V7 A. c! j2 X% K4.执行 /opt/tengine/sbin/nginx -m 时有警告7 d( N, _1 ]3 \# a% u

7 d7 e! D1 a5 iTengine version: Tengine/2.1.0 (nginx/1.6.2)
3 ^! `+ K7 {2 G/ ^- G& v8 d: ]" |% A& Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. }/ H  X# W, a, Q, c原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( C4 H8 b2 ]6 a* w! Z; V
2 I! j# S$ @1 }2 e! u# r' E" j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.* ~9 j+ C5 b1 v2 G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( C# i( L/ e" x2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 X! C: O0 q- O( g4 t" a$ T! ~. ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 t1 R8 x/ m! b5 h2 |% p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: e; A+ b/ Z: L" S. ~2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 ~/ r  b' {6 h4 P- f3 _7 O
解决方法,移除低版本的APR (1.3.9)
( u% \1 R* J- O* J; i7 i
+ `3 K) c, |7 g1 c! Z; uyum remove apr! E& V& \2 U+ I: C+ M0 q, D- m$ x
5.Error.log中有: Audit log: Failed to lock global mutex
/ g0 C& k9 j$ w2 E( Z$ ?; j7 c6 C. F1 ^3 H0 F7 I' q- M6 F* H* d' V
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 d( z; M0 e5 Y8 \* _/ d- ~1 a+ Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- i8 U6 j/ i- {- T+ v& m* ^0 i解决方法:: \+ o) F0 E: L/ ^, u- E/ u
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& \. }3 u; D2 k1 o4 V( P

9 T1 z! p% N4 J8 L; j( U% oSecAuditLogDirMode 0777  N% _1 K8 s* _" Y
SecAuditLogFileMode 0550
# s! L! [, O% i  d. @4 [SecAuditLogStorageDir /var/log/modsecurity3 l2 _& _5 o# c2 H9 R
SecAuditLogType Concurrent0 ?6 l- C) v7 @* U2 ~" K
参考文章:
& Q" ^/ b7 g# c' ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  k4 }* X6 t$ F0 `  R3 C1 qhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-19 15:07 , Processed in 0.067796 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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