找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13169|回复: 0

nginx配合modsecurity实现WAF功能

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

" J7 @9 _% P# M( `3 d' c一.准备工作
& r( n" k1 q/ [6 P
2 }% f4 L  c" n2 ^! O3 a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' p) f# }: l. A* I+ b6 F" D: J3 ~  n
: I" L; Z! Z: K* L+ O' Ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. h& ^$ p8 V; W  P% g" K
5 |8 T% r' m/ i5 g9 \& @3 M7 M( hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 _. U. }' `. z% M+ O
3 z- i8 D& j$ \- @$ k' ^
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 ^$ I3 G, a1 f& e0 o6 Q7 U

# ]# T5 ]& _, z依赖关系:
4 y; _/ x2 F" \  c" btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 v- _( ]  P$ w3 X, E0 H
; F1 @! @: S7 T' G/ [7 |* |
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel8 b( \* ]1 K! M9 }
modsecurty依赖的包:pcre httpd-devel libxml2 apr3 _# Y/ ]8 `3 H1 P$ W* F( X

3 g6 R4 u; Y7 _4 v2 W  A8 G/ Zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
% L+ j- H2 p. C; L4 J1 x二.启用standalone模块并编译
  d6 Z' \3 i: r# Z: L! x
: c) w6 A$ M4 y# g; W0 y下载modsecurity for nginx 解压,进入解压后目录执行:" x; D5 U% K$ T+ s
$ s) G, x* o# E% X! r
./autogen.sh9 [! e& c* }: G" `
./configure --enable-standalone-module --disable-mlogc. n: j6 x$ q( M7 O2 O9 L, u9 p
make
% Z& L. h, G' k, K% j3 R三.nginx添加modsecurity模块
, c& c8 J, |! k, @( i+ E0 ]% z) S8 ?  u+ L0 c# ^
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 N& x: O# J) x  q5 R/ W! ?
' Z5 D' [0 ?7 D4 [7 q) A3 h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine. i# L) Z8 |& t. V/ V
make && make install. [) D& {5 q+ R. h9 X# s/ ?2 o0 H
四.添加规则2 F: G. R- i8 b1 {

& \' I8 M% m2 o/ X% t9 Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; M, }' ^/ q, t& P: M( f7 D8 n
, d% X/ Z) c* N( P  U# i. f( y) x$ V
1.下载OWASP规则:
/ s  a# l! P6 x5 k7 [* C0 ?0 o
2 b& \; e. N9 rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ [) `6 v  i) l" }% k: _4 |2 F7 [
' k6 ?0 p# j2 J3 u
mv owasp-modsecurity-crs /opt/tengine/conf/
  Q6 r7 c' x6 D% E& I! q: o" I/ l# J$ P; ?0 a. ^7 L, W8 U
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 n" k  n6 U. |+ x
2.启用OWASP规则:
  a' O9 W/ o- e
- c$ {8 m) ?" t# J; u5 a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& Q1 t  R4 g5 r3 |! k; o& K, R
9 I. w4 Q$ ?" h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 T) i: E6 T' ]( U

  X$ j" f0 @. c5 `  s( t- @owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
  s" |; y& U$ Z! t' E, [- f7 {8 j8 K$ _) r
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. s- j8 K  g, M6 ^2 u/ G8 z8 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- M* F/ k5 T) h# e2 `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. S( F/ ~/ I8 X$ U" M6 _7 ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
: [: Q0 j& }5 ~* x5 rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 s% D3 H0 a* c' R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) c' ]. d" [# X: s! [9 B( O& CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' |- ]) ^/ s/ ]8 ~. Q1 w五.配置nginx& f  b9 i0 j  z3 d6 E& \! Y1 h
, M& F0 E/ r) q7 m  {" ~' v5 l
在需要启用modsecurity的主机的location下面加入下面两行即可:9 N8 R4 y4 w; l; V2 O
# t) O# {$ Q) v+ `) f
ModSecurityEnabled on;  : \  t# l0 \! l) H& W0 ^5 I& X
ModSecurityConfig modsecurity.conf;/ o* ]9 c% z) ?. W7 B+ C' k" v
下面是两个示例配置,php虚拟主机:
  v: E5 d# z- u: c" c( {3 j
1 T" g! q/ F1 [3 P0 gserver {/ Q- k. C% C* U) ~7 u
      listen      80;& K% O$ ?2 ~, X7 l( S, y, g
      server_name 52os.net www.52os.net;
; ?. S/ A2 }; ?     
/ E* x$ L! Z* D) d4 ?$ W      location ~ \.php$ {
. w! e9 [% g+ ^2 w8 L3 T      ModSecurityEnabled on;  $ U/ |2 V  a' ?( O
      ModSecurityConfig modsecurity.conf;
/ E# E0 F1 M/ a
% R) ?: z; |; k      root /web/wordpress;! }, o. G7 G, i- e7 u
      index index.php index.html index.htm;; k: N) _1 U: }5 B
  
/ F* y: B5 r- d, J' ^: s* X3 V      fastcgi_pass   127.0.0.1:9000;
5 ^; S6 |  o" u- F) L3 L4 |      fastcgi_index  index.php;- H" m5 S6 H( I9 `; T7 F4 A: A' H3 \
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ [2 {7 t2 R: I8 S      include        fastcgi_params;& U* n3 D  m! O$ B( E; Z
      }
  {$ U/ s) A; u7 R* s* G  }
4 p0 P4 j3 Z9 V0 y+ b  \upstream负载均衡:8 s! H" b6 B' Y1 r0 f
3 H" I, S4 y; ^3 @4 C
upstream 52os.net {3 b- Q2 c: Z* F7 @6 Z" O
    server 192.168.1.100:8080;
  X4 _5 Q0 e- f0 v3 J2 @& f' k3 L' n5 O    server 192.168.1.101:8080 backup;
9 i- e1 q7 {1 e3 x- u' F}
: d3 g- @, w6 c' S# D. F1 B& {1 |$ }! q: C; }+ N
server {
; V5 ^# j* g& N! T* q& @4 I0 L: |/ Ylisten 80;
' N2 u) ?" z  q, @server_name 52os.net www.52os.net;
6 u  @9 S2 q4 v" S" L: e# l6 `. }# |  ^6 D3 C" @
location / {- J+ R+ ?/ p" e+ u7 R9 k2 h  V! w
    ModSecurityEnabled on;  . `" G9 C  @8 Y
    ModSecurityConfig modsecurity.conf;  ( u0 B2 m5 N3 L& V5 [4 ]
8 \4 C, i  U! ~8 ?- N0 h4 R8 u
        proxy_pass http://online;
3 V4 h; e- R3 V; B& ^        proxy_redirect         off;
+ o' n$ x; C3 |' B: T, W        proxy_set_header Host $host;
+ H4 b, h, K% n! A  g. K0 n, A: u2 N        proxy_set_header X-Real-IP $remote_addr;1 N6 p6 y" n+ B  H/ x! j: o0 G- l
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;7 F$ r: ?- Z0 S+ V
    }$ N  ~+ \9 t# a5 M# K4 N! g- B2 e
}
- K) Q/ h" C9 f3 N; `& F. v六.测试% p; j' w/ \( G$ m% [. i( f! b, ]. R
* X7 m& i9 r% t9 w0 u& L
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ C& z8 V2 t2 A4 c
$ l1 C1 [$ U, V& q
<?php; S$ i$ ~: [; P0 V" V' X/ V
    phpinfo();   
; a' O6 U& d" X( ]?>
1 t' P: H* W) g% J0 A+ j在浏览器中访问:
6 X/ v0 i" P# U$ D
% f% V# E, y8 z! E. n) @# Whttp://www.52os.net/phpinfo.php?id=1 正常显示。
& O2 i- z! c; e( b! `http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
1 B- O+ A0 d. k3 Y. k; d2 ~2 {) Fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 a: z2 T, \3 F+ h$ t$ a: B, U
说明sql注入和xss已经被过滤了7 s7 W/ a1 L, A: G! W
7 @( t! W! u4 j0 K7 \- P& c
七、安装过程中排错6 K/ O5 y# }, l8 H2 l
1 V8 g8 Q. s3 n
1.缺少APXS会报错
  {3 ]4 S. w3 r  S( |
# j) j, @5 e1 `: p7 m8 g) L6 N8 S: Kconfigure: looking for Apache module support via DSO through APXS
0 a! B4 z# z. F) ]9 G* Cconfigure: error: couldn't find APXS2 f6 F: P+ r' @. f3 L  o% o
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% O# C, `" l. m3 I- E解决方法:
& D* u; _6 i4 g. G2 j& N: v7 J; n( t4 b3 ^" B
yum install httpd-devel! ^2 G+ t- O! a% }( q* y
2.没有pcre3 M9 y- n' {* v8 [# E/ i
( x0 Z" h+ G' j+ Z- w4 o
configure: *** pcre library not found.4 V$ e, L3 z" c. o
configure: error: pcre library is required- s  h& i2 g" @7 ~8 Y- _4 a
解决方法:! ?+ ]4 I. O. s- f! m

! q/ ~$ u" n# H7 I7 D% C8 m1 z$ qyum install pcre pcre-devel4 X' O8 S# [( u6 _% O
3.没有libxml2
' T& n* c$ {; g' f+ Q/ \- X# u
3 g0 i- M' s. \, I! C: x6 v& e6 A0 ^
configure: *** xml library not found.
5 T) ]( g* f5 A- bconfigure: error: libxml2 is required3 ]* e& i1 i/ d( x& z: G9 z
解决方法:
7 u5 J5 d/ r# z8 v' E5 D
  P/ A0 J0 Q9 n  b8 Hyum install  libxml2 libxml2-devel
* K% d  ?3 W8 U4 z; c8 [4.执行 /opt/tengine/sbin/nginx -m 时有警告" N5 P+ A) z6 n+ U. w

' x  m8 h' \- S: S' TTengine version: Tengine/2.1.0 (nginx/1.6.2)
- G' T3 J; A- ?$ W( r0 @& Gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!# w2 x& Q* T9 x- }1 x
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 w' s0 w9 \( R% U: K' ~  n5 Y
4 B- F" M6 l8 O! ^9 ?; x9 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 E0 F- M6 x' d* C- F) l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  i0 ^3 G3 u$ }( j: N2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!3 h& E9 {/ {! W+ f' m: u: n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* A) B9 M* F( n8 L9 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 b/ B& I2 `" ]  P2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; f. J8 d" l2 s. n, J3 y% [
解决方法,移除低版本的APR (1.3.9)7 g1 _  N6 t, z/ U
. [- e# ]8 r# n: e* S3 I
yum remove apr3 x6 S9 B) v) Q3 d
5.Error.log中有: Audit log: Failed to lock global mutex
9 e3 f& G! \. _, {& C0 }+ C7 m- e
: ?$ ^5 z0 O/ \; o! }  `( `2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 e2 a/ k/ |. M7 u. P* \  ]; \global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ Q, z2 I2 S' G+ l解决方法:, c* m5 m( J  [, E2 |
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 a3 D7 R0 @5 D8 v6 s4 a! @" Z1 N! H9 o3 X7 n) |; G" ~
SecAuditLogDirMode 0777
4 g5 v5 B% X  Z# F9 aSecAuditLogFileMode 0550" M1 L% b& u7 i8 T+ V" g
SecAuditLogStorageDir /var/log/modsecurity
! p4 t  x; |& q: z% U+ RSecAuditLogType Concurrent
. w& R0 c$ y5 ?# R+ n- I& |6 J  I$ Q' f参考文章:
  [! A; K( e# a5 M9 e4 L2 Ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 V4 P5 _+ i) z; x9 p6 o4 h# X
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-21 06:05 , Processed in 0.069397 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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