找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12428|回复: 0

nginx配合modsecurity实现WAF功能

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

6 e; }. r3 _0 S一.准备工作
: B" f; G- j% V  K# K7 j& K
) j8 o/ w% e- W, m) s0 g. N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, E7 w3 k& ^/ [3 H
8 j* [" Z. f: n
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 l0 u) j5 j5 r! Y3 j+ `7 ?) U' X3 X( V4 p2 i! W( }4 D; K
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" |. s& v/ ^* E. O) Y* w5 Q1 P9 J. l0 k3 p- @  }1 S4 K
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( h  e8 P1 |4 }; t2 _1 ]4 o2 O

2 f: j1 |8 x( E4 _! q依赖关系:& Z! }& |( V$ p. k( A' O1 u
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. x. Z/ B( r3 I) j) h
: L' C  c) D0 I4 _# P6 Vyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' r( Y( W$ i  I9 h; V- `4 w
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 t: b% P9 d" t9 q5 }& y2 x6 E

2 L) N9 Z/ `. Hyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
3 H7 @6 r! M) L( y3 b二.启用standalone模块并编译! t9 s$ B! W) f+ p5 @3 ^$ H  M) }

5 f. x: {' {6 r* _" i下载modsecurity for nginx 解压,进入解压后目录执行:
/ k2 _; W% A2 D0 Y) c2 W# |% Y4 h
$ T8 K: a' `" c" A2 F./autogen.sh2 S9 @% D1 x2 `5 X
./configure --enable-standalone-module --disable-mlogc; V0 H5 n& e; D7 v8 G: ^
make 2 i/ Y+ c. P7 T) [9 O7 n6 W
三.nginx添加modsecurity模块
, A: n5 [0 U. t5 m6 y9 A
# c% |, s  \$ O6 f6 h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# p1 f5 R. O2 S7 F, T! F" y( b$ D
: s5 c+ w4 \* F# z& ^' J. w! C./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ l6 ?. X7 _+ ~
make && make install" }2 u, `# s7 k# I4 z
四.添加规则
/ D' i- w8 f# c8 w. D% ~/ N  X
/ J( L% D# \; e7 A) d$ ^( m. E8 Y+ Nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ g  Y: g. x) f* [
, F3 R+ s% O. G; ]& n7 h
1.下载OWASP规则:
) F, T. Y" n8 I: V5 ?2 c: r/ \# Z' M$ o7 t# S) e! _  {6 V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ f% M$ r# _( n6 a6 l* m/ O9 j+ w6 h- t+ s
mv owasp-modsecurity-crs /opt/tengine/conf/  H, s% Q, |7 E/ O3 M2 n; B% [% j
& N6 J/ Z. Z6 n1 _
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 A7 h1 l/ A" b* s8 D
2.启用OWASP规则:2 w& Q8 i7 }" e. }2 d  F. [
  k# h* c1 g2 E# X# `( \0 j
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 K: E3 f: R$ H  q. ]+ q  c1 P4 W) h
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% ~) v4 Z* K; |! n; T0 }* e9 |$ |% d# C; x/ W% r
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 ~" Q1 e3 b: |7 w: ^$ B" v9 d/ d$ u
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* w" o6 c. n4 g, U. t4 F8 C0 p6 pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; e. J2 a. I6 j; t- S& |) o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ n; I' [% K+ _/ E" J! A& o! d2 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' c3 U8 }$ i$ Q- k9 }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! L3 e1 `5 ?7 V0 J. A* ?2 S# A/ _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" D6 Q, {8 e+ o2 g
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
- g# Z5 @% U1 O  T2 Q$ }五.配置nginx% |' f! |/ Y( {% y# g
# N/ ^" W$ j1 t( S4 u9 _
在需要启用modsecurity的主机的location下面加入下面两行即可:5 I, z9 Z2 @3 E, i  z; v. u

8 K0 u; p3 q' ^% OModSecurityEnabled on;  " x& S& ^3 _2 t& Y8 W
ModSecurityConfig modsecurity.conf;/ `7 n. |+ O. S% A
下面是两个示例配置,php虚拟主机:' V' C8 K. B9 K2 w) S! y5 ]; c, e

3 H$ r1 _3 O4 C$ A: J2 D6 Y1 hserver {+ w4 g+ w6 G* i6 F' m; \
      listen      80;
* F4 \/ z! F2 |6 k/ T: u' O6 H      server_name 52os.net www.52os.net;
8 T& Z" J9 |- h3 N. C3 w  w% e% H1 M! M- {     
. v) C  j+ K0 h: d      location ~ \.php$ {
. e6 t* l  F: E9 \% p      ModSecurityEnabled on;  8 o: }; P8 F! a& ~, Y
      ModSecurityConfig modsecurity.conf;
* r: d, s: N* g1 w' ^. k& H8 o/ y2 D  t
      root /web/wordpress;
% G3 V1 P0 Z/ I4 ?) Z" d+ C      index index.php index.html index.htm;
/ g2 N- C: Y4 B& ]) P/ V- M  # }  o9 @/ x. a) _* V* u, C' e, N; L
      fastcgi_pass   127.0.0.1:9000;
. f' n' N5 O) v# m4 _: P  b      fastcgi_index  index.php;
+ S  p0 Q2 h5 Y) H, ?      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;5 R* s: f3 M& D
      include        fastcgi_params;2 W; E+ j! v2 c9 s: {" }. {+ W
      }
) `  |! ^: p9 u% Y$ k4 d# O  }6 B: r/ d2 e3 C6 O) V% Y* d
upstream负载均衡:6 O: f$ u0 {) P5 U& N. s1 v
. W9 S# B+ U) c" b/ g
upstream 52os.net {0 l- ^6 U* M1 }4 I
    server 192.168.1.100:8080;
2 p% s( H- Z7 r, d& U    server 192.168.1.101:8080 backup;, ?: R; P2 e% ^% s  n$ s8 @# X
}
4 s, |+ c+ {# z8 M# V8 {4 m; a; T/ m- k  T- T4 O8 v& H0 _! o
server {
/ U) Q8 Q4 i! K# f, X+ `3 w" Zlisten 80;
+ U( K$ D/ L9 Rserver_name 52os.net www.52os.net;
( B/ j& |. y. I$ {0 {4 e# q7 l6 F" v
location / {
: x2 E1 N. A; d    ModSecurityEnabled on;  * ]( `0 D$ x* J7 l% E- [9 r! U$ n
    ModSecurityConfig modsecurity.conf;  0 E6 F1 d3 T) f5 x5 |# W
0 G9 u& K  A. \- v; E
        proxy_pass http://online;& r, T. E5 b3 ]- @- i. o9 B3 ?
        proxy_redirect         off;4 y8 j. A, }& ^1 Q! \# |1 b
        proxy_set_header Host $host;
! I6 n5 l2 }3 m6 B  C9 q) Z        proxy_set_header X-Real-IP $remote_addr;
; D5 X& ?- H3 h5 T        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;$ ~" p  }+ K6 x. H3 L% T* S
    }# C0 B0 `* P2 L0 s: m
}. Z% _) c5 U8 }+ N+ O+ T
六.测试
# k- t2 @! @* p" w5 i4 O/ Z6 P
6 `3 I, n9 N* P& n; N3 x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 r8 e. ~6 U; F1 Y+ h
) |2 Z$ ^* I$ `$ b, m<?php
8 h6 s- G8 `0 \' L8 l  G    phpinfo();   
( ]" [# `4 ^* \' [# l7 p?>. W' k2 _& J8 R3 U+ `
在浏览器中访问:  @3 }! E6 k* t) P: @
4 s* i" J) d& F/ {
http://www.52os.net/phpinfo.php?id=1 正常显示。: F! h* X1 m2 Z0 X) b2 `
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
) V% p. o& ]# K6 ?  [1 M0 Ohttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' A4 q* e; ]. Q4 c+ l7 Z
说明sql注入和xss已经被过滤了9 Z- X; g7 \$ Y! O

# l6 C2 O6 t: j3 i- m! o七、安装过程中排错
. o, Q) R  {8 {. o2 N0 e3 ?
" P  }: \% s" T' }' C6 d- E9 w1 v& i1.缺少APXS会报错
5 Y* q7 M' V1 Q5 ~; Z
: S. |+ a3 W+ I3 ]) ~# {configure: looking for Apache module support via DSO through APXS
. S0 A# x) F& \' t6 E; N$ p2 ]configure: error: couldn't find APXS
2 X! b# X, \5 H. b! b5 ~8 _9 gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) N1 A1 [: R9 B
解决方法:
1 I4 p$ ~9 P% S$ a
& o# i( N) K3 w/ _2 [/ o% cyum install httpd-devel; w6 [# Y# W; N& ^; f# x+ M
2.没有pcre
8 J6 i7 J2 ~8 b3 }. g9 t' p# i% {( |. f3 G, y. U4 a
configure: *** pcre library not found.
* p: e! }6 v* h0 z, C/ j% T0 j+ S. _configure: error: pcre library is required2 z9 e8 e  j, s3 l- [- d/ i" M& M
解决方法:7 }7 d1 D! N' [, U$ v& N
7 T6 N/ V  |+ P; W8 P- w) b
yum install pcre pcre-devel6 k3 z; b* r. u) f4 F0 y
3.没有libxml22 k7 r7 H3 G- Q- f- b& L. f0 K  @
" H: p. G/ Q% Z; T) N  @% Q
  f& W  A6 N9 u5 ]8 ^3 V
configure: *** xml library not found.- t. ?" h) C2 }8 {
configure: error: libxml2 is required, b$ U0 w" s7 y* H- R
解决方法:
  f  Y* Z# H7 q: \* u$ t( }
9 N0 a; [( k. s! q) X( u6 oyum install  libxml2 libxml2-devel
- N4 h$ |4 d( P3 C/ J4.执行 /opt/tengine/sbin/nginx -m 时有警告
& a6 b/ ?* k! K1 L8 o
9 K4 u$ k8 r* i! F. [Tengine version: Tengine/2.1.0 (nginx/1.6.2)) Q! r' q7 n  a; r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: x6 ?# S) h, ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 f( W2 ~+ [# \

8 b, D; w. g/ O. ]9 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% ]! l# k0 C8 X' g  P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
4 a0 I# @; l  G& j6 b" L2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 B) L# S6 G3 D$ u0 K* l4 w  n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 \. y! H- s/ W& d/ r. H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 J" m- g) Z2 C6 ~, p, N- O
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" ^: R1 ~) f: S, R' f解决方法,移除低版本的APR (1.3.9), A  K" t9 f8 a, H. y

0 W% J0 n0 r2 @! ~" [yum remove apr
; _2 q$ s" a/ a5.Error.log中有: Audit log: Failed to lock global mutex, u0 X: K/ n7 P) V" G

. n' X# q' v# L4 z! r5 y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
' r0 q3 R, b! ~/ {' Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
8 t  {$ Y- P$ }* @4 S# H0 k解决方法:# F( U* a8 {( H" o1 Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' ~. ]) y: i6 e" e. c, O2 `
- v% V# D: \! k" WSecAuditLogDirMode 0777* [2 W$ F7 g  N
SecAuditLogFileMode 0550
0 w7 w. m1 l. }- ISecAuditLogStorageDir /var/log/modsecurity
, z& U0 h. C; k! `. L- USecAuditLogType Concurrent& ^- Y! {' }9 t; C6 X
参考文章:
' s7 t/ v: }6 ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  A- i* E3 w6 ohttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-5 16:07 , Processed in 0.059119 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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