找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10616|回复: 0

nginx配合modsecurity实现WAF功能

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

! M1 \0 C% c  B! @  A, ?一.准备工作  W+ y2 Q$ N+ N3 {0 {; q4 H

6 @. v- u: Q1 ~系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ g' E% F7 F7 d# k4 K: M
/ X% D5 [! j" s7 _
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# C' Q: g$ ~+ y6 {1 R( a8 X; r  _
0 s+ O) V- ]3 Y% ]6 G( ?; Z0 g0 }; c
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
) a; K7 Q+ ~: a1 j. q
( V  c( ?% C% C. f, vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 ~: r/ {) [  d# A; |6 E; B4 m
, G8 y: t- l9 @* M. Q0 J依赖关系:
7 Y- M3 e6 [$ F3 a( V' f( Itengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( c: y5 s& P- _" I6 X( A
" l, Z- B' j! n6 Fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' s5 Q- F  J( z5 M
modsecurty依赖的包:pcre httpd-devel libxml2 apr7 J- o+ T" b9 q9 m2 L+ j. {; a
7 c9 z) _" x* o
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel- M+ @6 F, Z, ~# d
二.启用standalone模块并编译
8 G; M4 L/ @& R- r4 t) [8 p" t0 C; a# I* B# `9 ^2 f
下载modsecurity for nginx 解压,进入解压后目录执行:
1 R/ s9 ^5 A# K) N9 J
% s0 |8 j4 a2 O" I" ?./autogen.sh& a* }/ V2 c( O& t
./configure --enable-standalone-module --disable-mlogc
% n  D' z8 {) Q, @: a3 qmake
, f. s9 N) p7 E! [三.nginx添加modsecurity模块. v4 A8 N7 ~7 j: m

" j' b& \# A% T, y% q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, M4 y8 T. w: L9 h! H9 g6 N$ |

1 n$ l+ r* B* Q# j8 l./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 f6 F8 ^  m: o; n4 c9 G; b$ Vmake && make install5 L* L; W+ h. y# K5 ~
四.添加规则  y) S6 x8 M+ l  S9 s) r

1 [4 j! E) K$ \: x6 Lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 Q' L3 I  ^3 G

/ d# J' A+ M) n( a. }1.下载OWASP规则:7 h7 h5 E1 c- B% d1 y- c3 g3 a! L
5 k0 t) P1 q5 _' ]
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs2 r- Y8 a' J) F6 X7 E6 b' R& u

1 B: V% K4 k  Z: S  p- A3 d" X/ a( Xmv owasp-modsecurity-crs /opt/tengine/conf/& X! g, M/ f: W

" L- N* n2 _2 V% Q5 ]* W+ h5 S9 Pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% ~$ p' c2 H; u* \' P
2.启用OWASP规则:
7 H5 d! p! [7 N! M: |. l6 q: F  V" u$ }  Z8 p$ H2 H
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 f: w; H& G/ ?
; ~6 R: M5 E8 g% z- c
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( ]; y+ `$ c+ j2 v% r: @
; m2 @' p$ W# X# f+ ~0 v$ |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 f2 x$ \, A' ~6 }

: T/ V3 ^1 }! N& Q9 Z. [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) Q( }5 I' p/ Z0 @! P* x" M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 e7 x1 K) g+ H/ }& GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 S+ L$ L: e1 @' n8 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 A5 f. C: G9 V: ^& m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf9 c" y4 m8 K1 J1 V8 W7 `- i* q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf: Y1 M; X/ x# C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 ~& }& y2 A8 R" |1 _# Z- v
五.配置nginx
0 m3 W4 a* |5 ?$ W1 Q/ t3 W' b  p% f, \8 z
在需要启用modsecurity的主机的location下面加入下面两行即可:, o* A& t5 T4 W0 |5 ~4 m7 s
! O8 m  \* d: j# A) t0 b
ModSecurityEnabled on;  $ w) a6 ~" n$ }4 }
ModSecurityConfig modsecurity.conf;
( B$ s% U, V3 U8 v4 G, V下面是两个示例配置,php虚拟主机:" M1 T. E" V; K7 X/ Q4 h

* f! h. U4 Z" D$ J" y0 U5 t9 ?6 q1 tserver {! e2 d( ?' p( @
      listen      80;( v, A% c" [# g0 X+ ?% O
      server_name 52os.net www.52os.net;2 o; [9 T5 O4 R& l
     
- B3 k! Y" s' O% c      location ~ \.php$ {1 f4 h1 k6 P4 \& Y) h1 B( T$ U. Q
      ModSecurityEnabled on;  
+ U. y" i% p; q  \2 `7 X9 t      ModSecurityConfig modsecurity.conf;
3 g9 f, g/ ^1 U9 u* V: p( @3 n' t' C; C6 ^! n( ?& N
      root /web/wordpress;
& b( d$ B5 |% b! Y( u, p" D. ^      index index.php index.html index.htm;" z4 g& F3 a" \1 B
  ! ?6 _8 l; R, x' ^( p
      fastcgi_pass   127.0.0.1:9000;
9 n1 m2 k- {' ~      fastcgi_index  index.php;7 r& @7 A2 o) s
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ B) k  Z) T7 N, Q
      include        fastcgi_params;
% |* C  t1 {& k. }, M0 y      }
8 K& }8 k7 |2 a  }6 j$ [, p6 c' Z6 W8 O6 Y  ^
upstream负载均衡:
) e$ f+ |8 K9 _5 x. N
2 i% @; w! S; Kupstream 52os.net {
0 d5 B" I2 O/ Q% h: P0 b    server 192.168.1.100:8080;
+ c* c9 d4 |$ i0 Q% w" C1 x    server 192.168.1.101:8080 backup;
* Z: L0 n; Y4 f/ c% I}
9 g0 @% e8 e  w# {$ U# P
# E" M5 Y3 E9 z9 \. `1 B) o8 Xserver {
$ i! ^6 H# y$ B8 b7 [4 l- x& m6 ilisten 80;
$ q8 H  H6 `3 X8 ^5 f+ j# Userver_name 52os.net www.52os.net;9 j* I! A$ l5 G4 r

( e9 i$ Z# w' N/ a  wlocation / {
- f7 p" a8 ~! f8 @1 e% h0 J9 P) |    ModSecurityEnabled on;  
0 O3 @" b3 H7 Y    ModSecurityConfig modsecurity.conf;  
- W" Q$ m7 @/ v: q2 j1 K: `1 x  o! s; m* Y5 a% Y
        proxy_pass http://online;8 {. @3 h2 o9 a! q
        proxy_redirect         off;1 E8 I3 ?6 k. P9 `1 [
        proxy_set_header Host $host;
6 Y: M% [8 w9 ]. p        proxy_set_header X-Real-IP $remote_addr;2 `7 C+ F: O1 o% s  Z9 o
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;% x  z  Z% l  o0 U+ o6 L2 A
    }
( D6 s4 O! \- R( I# j% a" _# y! v  J}) D, z7 ?, {  R# V: m( ~
六.测试% n5 O6 |# |% H  ^7 O/ }
3 u* H5 `/ E6 m
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 Q' j, X8 m; H- s$ U: R6 |
/ p4 J- x" B( B- c, v! i<?php
0 h8 g6 ?6 p% j# P1 m  g    phpinfo();   
- E0 Y4 S# A+ _8 }?>
- Y+ l4 t: d5 q在浏览器中访问:
" C* S; P) w. Q8 j* F0 ~0 p- \/ l9 ^4 v3 }
http://www.52os.net/phpinfo.php?id=1 正常显示。* f1 b& w8 v; F# I9 F
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
* P$ z1 m# X3 h  ]3 o, ]3 Z* Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 |3 S  l5 E' |- K) V* Z说明sql注入和xss已经被过滤了6 B. j8 C: e) D5 N

+ f, d3 j4 M# b/ O4 t0 t9 h/ _七、安装过程中排错: x" F+ S$ k% a3 E& q$ T. v0 K
6 p/ h' u4 ~0 `" k& w  i
1.缺少APXS会报错& |9 a/ ]& l; r$ X0 r4 _

9 A& e. l! s2 e! a+ qconfigure: looking for Apache module support via DSO through APXS
5 D0 S2 j+ ~; v# i- d5 Tconfigure: error: couldn't find APXS0 Q$ C7 [6 d7 f4 Q+ ^' y0 B' f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" K9 V* s6 l- e0 Z8 o$ T, h% u. c解决方法:, B+ E- e4 W  K/ U" A8 D+ m. J
0 Z/ R2 O3 ^7 k9 X
yum install httpd-devel
% I. `9 R8 c7 T& _2 I1 [( o8 h$ m2.没有pcre
# S. s% ^" g! r3 w  X7 K& t4 k# h9 \3 `) x8 J3 {
configure: *** pcre library not found.& \2 Q0 y) w% ^0 G6 u1 {# r
configure: error: pcre library is required/ R7 Q- \5 \5 t) _6 T5 v/ e& L+ U
解决方法:
( T1 h: t3 k/ U- C- X8 @4 Z( T$ _+ \
yum install pcre pcre-devel
2 c  Q; d, G, y$ V' |3.没有libxml2
4 Q- B. S, m! P' d. Q) L1 i0 I! r$ Y3 I( J6 K$ W  V# j4 _

/ H7 e3 k5 N; O- ^/ w+ r' Mconfigure: *** xml library not found.
' f% p3 e* b! y# G. Z9 i; Mconfigure: error: libxml2 is required# J/ K) t! m  n, f' q
解决方法:; V: }$ j. Q" n" `

) V0 c! N5 h# I: gyum install  libxml2 libxml2-devel
4 x* l8 a+ n% Z4 l4.执行 /opt/tengine/sbin/nginx -m 时有警告  G- i6 `* y+ Y6 m

- i. ^& H) G, n2 a1 V0 Y6 V* \( _Tengine version: Tengine/2.1.0 (nginx/1.6.2)0 ]- G. P& W: P  l1 K( E( `- e' f6 P
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
6 K, ^8 ~7 H5 T* N& B+ U# O# M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' o* U& b) [( f5 l% O* M
1 ?% j: x" H) l2 u6 M$ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 [  v7 ~/ L' v. e0 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
  I( j% u8 c) S/ r  H$ Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; t5 L0 s7 T3 ^- z6 h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 a9 w) A3 s9 }; `/ u( x0 k5 m( i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
; h: }' s' S* b# w" a3 Z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! E  L9 N4 S  j; g! E$ y
解决方法,移除低版本的APR (1.3.9)9 R- o1 C9 a4 Q$ W

" Q  h* \4 {- o1 e* byum remove apr1 I; S" z5 M& A3 t' W& D+ i
5.Error.log中有: Audit log: Failed to lock global mutex8 d+ j( ?  f8 w7 B" Z; n  }

5 a7 S2 b- m6 u4 F' \! s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; H- h& X8 [( @6 Sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; U: w9 T" T% N解决方法:- ~% r' M4 S; ~  }
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) W1 U  V, V3 X2 c: N3 t, D
4 f' B, x  ?) Q) G8 K5 s/ Y! h
SecAuditLogDirMode 0777% s" ]+ U6 C" ?% l9 s& ~) w" Q0 J
SecAuditLogFileMode 0550
- ?9 y6 N8 C8 F, N6 {+ N, `SecAuditLogStorageDir /var/log/modsecurity
) E7 @! l; F. C& u# c9 i' i) zSecAuditLogType Concurrent. J( A% y# R1 D8 r
参考文章:7 e, u5 ]  O9 B, J8 z% z5 x
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# {) Q  x2 B! t+ z+ U' F- q0 I" t, x3 r
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-4-12 16:07 , Processed in 0.069029 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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