找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12575|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ {7 O4 N. s9 W3 a! Y/ F% x! G- Q) l" T- {* n- h- D( f' Y
一.准备工作
% Z+ t. z. p/ I8 Q2 x) Y1 z# R, I7 O4 v  G& A7 b9 ^
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" i5 e& V- M3 G% r* r% I0 W6 o

3 _6 w* g2 K0 Btengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: N- ]9 A3 ~& ^3 m

' \% G4 O5 ?  A  D0 Nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 n9 T3 c/ T. ^2 N6 p5 v

7 k1 L( ]5 T- |3 zOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) A" K0 K7 S! X( L0 g

. n, M7 w: L( \依赖关系:) `, l1 {# q- a' f, p3 R) |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
9 n$ v  c3 b1 |1 w7 _; K% m2 `8 i) ?1 q
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel- o5 N# _# o. t: v3 o+ X
modsecurty依赖的包:pcre httpd-devel libxml2 apr' [; ~. i: r% K1 s5 }: @

7 C2 h. p' Y6 D3 n; \6 Lyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel% b, Q0 q6 F! ], s5 E+ T' H& V
二.启用standalone模块并编译9 j0 F% m  F* ~' v. |- B

6 d( K7 {; V$ t& b下载modsecurity for nginx 解压,进入解压后目录执行:' w4 X( x9 `; {. D% K% n; C
9 v2 w% \' p7 I  w9 g7 Z' V; M
./autogen.sh  {' w8 W9 P) i0 s4 w% L# U- E
./configure --enable-standalone-module --disable-mlogc. Z1 U, v# e- V$ T" L
make 3 E# ^' _9 Z) m2 J# W4 Z
三.nginx添加modsecurity模块9 |( w7 `; A2 U( n, Y9 y
- b  |4 G; Q, B9 D/ A1 A  e( m$ M0 [3 a
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! `7 Z6 g1 N! V4 y, m$ K; d

; g% R) |/ g  S1 P- e  f./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine( N1 m. q3 G7 o: P! ^* ]/ s  u
make && make install
, T: a2 s9 J7 R* M& g1 D四.添加规则, t, m6 v8 S; h5 X+ s; _
, s3 x# v2 o- Y  f
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 u& ?8 M8 r% \) L- v6 d6 h4 r( R9 Y6 j# i5 y, a* _+ ]
1.下载OWASP规则:
5 P4 e( u" Q5 }  u' O3 x
" Z* A; R( k3 c5 bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs* X) D6 K7 ^0 H& O# e

6 w. M7 K: ^& S  umv owasp-modsecurity-crs /opt/tengine/conf/: e8 B& D8 J/ R( l2 k5 R% k: W
& P/ ], z# D( p) q
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 J7 M0 \4 F& E! M; k, E
2.启用OWASP规则:+ ^9 \) o0 ~7 u9 u
% H- C- N. j! x1 K
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% X/ S; H. ^  Y6 v& Q
3 }$ l- |$ o: p! T7 S2 p编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 W( u! B$ g- u! Y$ V  ~) K  q+ D" X% T2 O2 c% ~" F
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# Z9 A1 h5 F5 Q
  y3 ?: i/ U* n, JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" T3 |( Z7 ~1 [6 C2 g7 Y' @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' o: R8 Y: b9 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ L, J: S) J3 v: k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- a% V/ t0 M# g. Y) H8 _" i$ {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ Y( W/ ?# i3 Q: B' M0 n& q2 wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( M/ y+ {4 V; D# aInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" _+ w" B# Z6 Q+ B/ g( R( Z6 ~
五.配置nginx, x* y# _2 K/ ?" a5 x# z

, q( f  y" g* _1 n& a在需要启用modsecurity的主机的location下面加入下面两行即可:
0 U2 d6 b" _# W' o
0 C  E& i5 \. |* dModSecurityEnabled on;  " H. q) i2 j0 x6 Z- w1 @/ ~
ModSecurityConfig modsecurity.conf;
- Z, Z# B0 h! F. l$ z' O/ Y下面是两个示例配置,php虚拟主机:! d8 K( H( f& e. c

$ m& e+ x9 |+ jserver {
; X2 a7 z) b' k8 N7 n3 S% J: s      listen      80;, L& z0 L5 b! f" j0 `
      server_name 52os.net www.52os.net;5 w' J) m& R$ _  z& [+ V
     ) G+ ], U, u, k7 I8 u. ~* B9 B
      location ~ \.php$ {( y- t, Z3 `0 C! |% Q2 r
      ModSecurityEnabled on;  
- k- Q+ P/ ^' ^; J- C* F      ModSecurityConfig modsecurity.conf;( f2 z3 I1 x3 u( @% q+ r' Q! t

/ e9 P1 `) J% _. f- ]) u; \      root /web/wordpress;
" z9 `+ B7 K3 a  B" _      index index.php index.html index.htm;7 K7 r% G: s/ `" c& ?
  
5 I' U  c6 |1 L5 e$ e8 f7 O      fastcgi_pass   127.0.0.1:9000;
* V0 s5 j6 Q/ J9 c      fastcgi_index  index.php;3 ]4 [, E: F) D" s+ C  G
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
- R" @( I& e4 i" C2 {5 r; a      include        fastcgi_params;
2 K2 T5 m/ k3 D$ K0 Q. b6 q      }7 @& r; d( I( @6 d9 X$ Y) `. e( B- H
  }) ]! L, j( q2 X
upstream负载均衡:
4 Y- m5 m- c/ T) T
9 l2 i+ P6 L- @' v2 Hupstream 52os.net {5 F: R( E0 r0 X* c  }
    server 192.168.1.100:8080;) E3 _0 Z! o1 K9 X/ H2 c1 @
    server 192.168.1.101:8080 backup;
( W5 Z2 \( A& G' R# t* _: S}4 m5 j$ L6 R! i
6 g  y' y9 \  S$ I+ @! R" I% N
server {+ y9 D; ~- c' P$ l  X
listen 80;
! R* a' p# ^8 F; T2 cserver_name 52os.net www.52os.net;5 \% b% o# i3 b5 J! ~
7 z! c4 @/ b/ O  S% |% k
location / {; `: D6 v; }' l& G0 w) x
    ModSecurityEnabled on;  
, x5 j6 o1 h3 e8 u( y( f    ModSecurityConfig modsecurity.conf;  
3 l/ z  J4 w' }! k. K3 x$ N) X% Y. D% U8 P/ i
        proxy_pass http://online;  u- L  Z. B9 N+ C2 Z' D  d
        proxy_redirect         off;
( k- ~4 Z0 b7 S" V/ H! W* N        proxy_set_header Host $host;) R- h/ w0 z2 t1 N0 b2 o5 A
        proxy_set_header X-Real-IP $remote_addr;# l# \& a/ l$ s+ A1 V* x
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 o0 @$ W1 H  l; D8 U4 o
    }
, c, M. j7 h* j# X, r1 t) B}& `& K/ F! M7 \
六.测试' W4 b/ k( J2 N" }

) v" ~1 w6 V. {4 H0 i6 q% O我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 m* Z+ F1 G0 L9 u. ]1 {4 L9 }5 t* V6 z
<?php
9 s, P% V6 v4 d/ ?7 }) ]    phpinfo();    + f2 R" o7 C+ q1 u; m6 s  {- C& T$ P
?>% ?* f$ X  h" G/ {2 `
在浏览器中访问:
" V; X& `% v' d7 }6 N. f" _4 `) l& n% p$ x; G8 U+ h9 D
http://www.52os.net/phpinfo.php?id=1 正常显示。
! J; }! T, Y$ U( ~4 `( mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 x9 u# {3 G5 p: D: X5 Z3 U9 j
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 L$ m# w& H% t: H说明sql注入和xss已经被过滤了# U; k5 ^1 V% s: ^: }* h& s' y
1 B: P$ d  J7 O
七、安装过程中排错; g2 M5 w( y! b1 e; ]0 q3 P

. S- k: v" o* A& H2 k1 V1.缺少APXS会报错
- i2 ~, ?; D+ ]
6 ]- |: \. v! @* gconfigure: looking for Apache module support via DSO through APXS6 O3 S2 `& _! \  g
configure: error: couldn't find APXS' v9 W( Z" k( g3 k; _: g6 G" M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( \/ R% P7 t' E7 u解决方法:
* J' q3 u+ @+ c% C0 V
; b7 C! m+ T8 P% i6 {5 ?  X; H" }yum install httpd-devel
' Y0 C$ A% [  I0 y/ ~$ ?. e# F0 H2.没有pcre! P, i0 e. V; O! @$ r

& x& A7 w5 o3 [0 n* qconfigure: *** pcre library not found.+ T! ]9 p. d- q' c# W2 V
configure: error: pcre library is required
8 j3 G! E* B! m9 R1 b$ F) y( g解决方法:" [0 [- ], ^! t; e

, m9 l) i: L! B- X# T# V- k$ E  G4 ]yum install pcre pcre-devel
2 _4 N: r; t3 l' r. w  `8 c3.没有libxml27 Y, i" X* y# H/ J

% b3 f# ?! b3 R9 ]8 }+ \% \. ^8 X, K5 N
configure: *** xml library not found./ u" p& Y3 I! ]4 Z4 G0 ]( {
configure: error: libxml2 is required
4 [8 E" a$ ]  ]' \解决方法:% E8 b# t' B9 W7 ?- T
1 t( r0 H8 N' }- S. C9 y: x1 h
yum install  libxml2 libxml2-devel
- l- X( ^9 r8 l2 Z  h4.执行 /opt/tengine/sbin/nginx -m 时有警告4 F7 V/ V6 \# D, Z% X7 B
( G/ }1 H" T% y/ h& h/ T2 W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 O% y* d' Q4 s- t
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
5 V, Y( C& B0 `- Q! N5 O3 U% R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 `& z( ]" q4 v. [: j/ h. ?6 k

9 ~* {3 @0 z( m2 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' i( i: D: _# F5 B" T0 q* @# K5 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 i. j8 ^+ O: t2 V6 @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* n. S* {! d0 B; [7 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. B3 C- n9 d0 t# |: j% K- X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- e; D' x/ A7 h3 c; Z8 ]. q* h
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' |! ]- }: C" g解决方法,移除低版本的APR (1.3.9)
$ ^0 l9 t0 E* O* k
2 n* W, n& V8 U8 yyum remove apr& w( i2 u5 a9 x. v9 T, t9 G
5.Error.log中有: Audit log: Failed to lock global mutex
) e9 U2 Y- [. ^- f+ s& o& P: Q
! {1 [, y& n2 }4 F5 q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; t# z% Z' k& F: N, oglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 r0 F$ p" [! I5 O解决方法:3 D! ~3 @' M" l+ C7 G0 C
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ T! x4 B8 @' _0 k6 N) {. C/ X
: f% E) u* W( g0 D( B) Z
SecAuditLogDirMode 0777
% N$ ~7 a: F$ `6 K+ iSecAuditLogFileMode 05507 {" y8 b5 V  m8 G7 F. j
SecAuditLogStorageDir /var/log/modsecurity
6 ?$ y1 D1 u# T9 K0 d5 r) J+ RSecAuditLogType Concurrent3 [+ x" ]: [5 K% e5 k/ ^$ P2 Z( o
参考文章:2 R0 m% r0 i( I& Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& f  j  H& N, ~6 A' n3 c$ y
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-18 19:00 , Processed in 0.061294 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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