找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9049|回复: 0

nginx配合modsecurity实现WAF功能

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

6 ~  ?' h6 d6 B, d* q, o# P一.准备工作. u! I, R' f2 r* C0 f& U

% Z1 a9 w; _7 o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& b: ^6 z6 P2 _9 D1 h9 Z6 a, B5 w

4 P% X9 k. ]6 }tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 c1 b; w$ F; [: `) V
( M8 S% j/ S& \' B9 @8 I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' F( G& I" b, F! d8 m7 y- t" w! N3 U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 Y! b' ]: Z9 i' D- V, i
6 ~' a! K& A- M/ J8 r2 F' [) s依赖关系:! m9 w, u. O3 `. a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' a5 X& [" M2 v; }: o7 F
, F: X4 H. ?* Q" _4 w" nyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
6 C: y3 l* s" }0 L( Y+ j4 imodsecurty依赖的包:pcre httpd-devel libxml2 apr
9 w5 ]" F; b! O$ l4 h1 D& H, U
. J0 d; J8 J: b5 c; }2 X+ J+ `yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel' B6 `; q2 F, h: P
二.启用standalone模块并编译+ h  q0 r( L) q, e1 u2 e' `
" }9 M$ f" U6 V  N4 q5 M5 @
下载modsecurity for nginx 解压,进入解压后目录执行:
3 K. G7 R, ]1 x6 t, P+ \6 N  @
4 i* ]- R( z) `1 S+ p- H* I0 a8 v$ v./autogen.sh) `* ?+ }' u3 g9 Z3 ?2 \
./configure --enable-standalone-module --disable-mlogc4 E& l' U2 |: J9 N2 ?& T
make
2 K  R/ c" z1 C. X* J( W. v6 b三.nginx添加modsecurity模块8 f8 k0 f+ q9 t, _$ ?5 `) P

/ ]; Z0 m+ ]  H- C在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; R4 _& Q4 J2 _6 I+ h

5 F. T( f* O7 [% ?./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 G3 k) Z# t4 k- M
make && make install- y3 W* p, }- L  u& f
四.添加规则# U/ T" o' D& x: ?3 H7 z

( Q6 L# k" @# w$ Q, l& j6 z. `modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
  |) f& [( J8 s: a( P' M8 L
7 G$ G8 ?, Q0 s1.下载OWASP规则:
! d6 j$ A- m+ x0 B
+ W3 y* f0 K2 n. }+ J: p9 S1 Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# P7 P& x7 G0 Z1 ]. m
( y9 `# ?5 s2 q( V8 M6 T* gmv owasp-modsecurity-crs /opt/tengine/conf/, Z, [# Q( O5 z5 l$ e% z/ m

4 F$ v2 O2 }$ G6 r8 a3 lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: W, m0 A- X! Z+ ]+ A, F  {2.启用OWASP规则:
0 d$ X% O0 G7 K5 d+ b/ M/ L2 @: r3 z. j* m( F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
( U4 [2 A% E# Z" U( i7 S2 e1 k' G5 r0 i, X# D7 X
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on) G3 y8 j6 g5 e: P2 L

% S" y+ p7 [7 j; ]# q; d3 Lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。$ }+ M. T' J* z& ]+ H0 \; l
6 X" E& }/ a. j
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
# ?, a7 r: W( @6 K! T7 M) |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
  ], o) [& I" {3 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf2 K+ M  E& ~; T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 x+ f. _8 c# X1 K0 A! G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) j' `, i/ U1 g0 w- t4 tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 `$ i4 V6 u8 V. p: Z7 S0 w
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ r+ q) K& }3 M2 {
五.配置nginx
* @0 U# H( {; V) |" G7 h* k9 K4 H. u7 C* O
在需要启用modsecurity的主机的location下面加入下面两行即可:
9 z% f( B- `- |! l6 d4 d! Q
$ r9 T, N' m5 ]  CModSecurityEnabled on;  
' O$ z) v$ X! ?$ \" W: fModSecurityConfig modsecurity.conf;
5 D2 Y2 A* l* A6 L" D; v0 B下面是两个示例配置,php虚拟主机:
* h4 ]7 U2 H# b8 c5 p: E1 z9 R/ l# G) R, Z
server {
8 @0 E7 b0 f& W7 Z% Q$ W      listen      80;  V' [: T6 F* l; a1 W" x1 ^1 I/ \; v% L
      server_name 52os.net www.52os.net;4 ~9 v5 H% `) a8 k2 J0 u
     6 ]  E4 z- e9 b# e. c
      location ~ \.php$ {
9 l" k4 {# \& o( E1 L7 M$ M4 P      ModSecurityEnabled on;  
, [4 K! l9 t- B      ModSecurityConfig modsecurity.conf;
0 {  \0 ~; a+ U) {) W  W( o) T8 t2 Q* z; j
      root /web/wordpress;
/ \7 Y" |' d: L2 I      index index.php index.html index.htm;$ |* }0 i4 w. q: Y  a
  
+ w; G: V) i! e      fastcgi_pass   127.0.0.1:9000;: p$ o- B/ T. ^% Q+ T7 b5 }0 k+ h
      fastcgi_index  index.php;0 l1 O; Q; L* [9 s: P
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 }" J# F) R- P/ h. w) U( i5 H
      include        fastcgi_params;% D: i4 \- N- [1 T
      }: {! x* I' ]$ `7 L9 P. e; d6 \
  }
" Y0 J! g& z  [upstream负载均衡:
# M6 I. Z, `% `: x. w- c( D$ {6 Y, F+ [/ m: R4 R$ b" I. J  ]1 N5 u/ E
upstream 52os.net {
6 ~4 ~- f' e6 A" o+ I5 q    server 192.168.1.100:8080;
. a3 E8 h. Z+ }% |$ s    server 192.168.1.101:8080 backup;- m/ h6 x4 j3 j  P  H
}
# Q7 g% }( S' i( U3 x0 b. V% I5 ?$ h/ y" j2 ]& ^
server {" S) _" e' a' u7 Q* w6 z# ]9 n' \/ A0 g
listen 80;% W/ h* x. E* h1 Q9 t' L/ j# a
server_name 52os.net www.52os.net;" `- X: Z9 ~" n% V# X
, a+ |; b/ _) z7 w: d: t1 f
location / {" N* w9 F# a) G/ E) v* P
    ModSecurityEnabled on;  ) f# C4 v* u/ e6 |0 d- r7 H
    ModSecurityConfig modsecurity.conf;  
  i5 Q! w. a; F- I$ [" Y7 K
2 I; _5 T; v  S( o3 x8 d/ r        proxy_pass http://online;* n! o. |  S% f0 ~/ F( R6 W0 `
        proxy_redirect         off;$ @# @! Y6 [, j0 R& M7 d' ]: ]
        proxy_set_header Host $host;
. N$ l" h; i5 w. O* l' h        proxy_set_header X-Real-IP $remote_addr;9 R9 ^4 a7 a  B
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
2 R7 F* }$ |) N3 b5 v% a( Z" S    }
) ^) t& A$ u" J3 S0 B* |3 i}
& g3 n( I3 Z' W) B, {9 X( q六.测试# c3 v6 d9 y( ^8 M6 b) }2 i
# O# a7 R) X$ |- F$ _( Q# a; A: I
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ u+ n4 p& u$ T- A  u

5 S1 e+ q% z5 \) I8 H1 W<?php
7 [8 u0 f8 Q7 W1 B- ~    phpinfo();    % ?3 [+ C# I) X4 i& q7 r
?>+ ?# v3 c% y3 ?
在浏览器中访问:
3 y( J6 `/ B) a/ U
% x; y) d0 x8 ghttp://www.52os.net/phpinfo.php?id=1 正常显示。
! u  z: A+ U2 O: N% Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。5 _6 E2 j5 F1 Z' e. k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! l: C6 |# ^* a" v说明sql注入和xss已经被过滤了
3 S4 k5 F3 n8 j+ Q- r  A7 G+ l: H8 G+ q" q3 d9 }  c* A' M9 E% S
七、安装过程中排错# v8 O- q8 j( s

; h- z4 q: i7 S1.缺少APXS会报错: e: \2 V: q" S$ d) g
; ~, I7 T; m# Z1 n( q. Z
configure: looking for Apache module support via DSO through APXS
9 X( T* Y" _0 G3 J# U5 V' L- wconfigure: error: couldn't find APXS, Y, `& n" c3 |9 N$ F& `) E; I
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 V$ P4 h( \  _) q6 i解决方法:
) O  j# W7 h* F4 X) M) \9 U  z: C( a6 x- N2 f; i
yum install httpd-devel4 S; k# ?( L4 c! Q9 |" g$ @
2.没有pcre& j. F. u- t( a6 z% n
; c, i7 |+ `4 O
configure: *** pcre library not found.
, a) z& w( v$ c& Q7 ]3 `" ]$ j  Rconfigure: error: pcre library is required  q6 l/ J1 L& a3 F- {- [1 p5 _
解决方法:
) w, O4 R: o  `) W7 t1 E2 Y& K# g3 V& d
yum install pcre pcre-devel4 K3 F) g* c' s. ?# I8 e& ^9 T* J+ Q
3.没有libxml2
/ ]& p% ]) l# p1 j1 h' T  U+ n( j$ [) T# `; X
# R$ n, s4 Z( K
configure: *** xml library not found.1 g& ?4 g+ n. K$ Z
configure: error: libxml2 is required& P7 i/ \, L" o2 Y+ P0 S! ^
解决方法:8 w3 y) J# D% }# I% f

( o; l2 Q" f9 V4 Z3 N) @yum install  libxml2 libxml2-devel
, e, }: o$ [+ k4.执行 /opt/tengine/sbin/nginx -m 时有警告1 D5 y( n9 L% E
" v1 T( q. Y1 ?
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' _3 `* W. K6 r3 j9 x
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 L" ]6 s5 g% G2 t1 H* {7 C* ?* {
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& X5 F+ {' a# }" ~" d- m- S7 L3 M" }6 A9 f. W3 x! @( D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; `6 N5 u/ B2 x& c* Y7 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"+ j' }+ v" \# `: ?
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! \# F* a; b! I; ?5 n& R) k! \5 V9 N8 t9 {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 J+ X1 R. e+ s8 D) A. K: c4 r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% k8 o. d9 ?- o/ X( ]$ e  ?4 m
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.% B$ A5 W6 y+ X3 a# |1 x! }
解决方法,移除低版本的APR (1.3.9)
& I/ X0 z" _! }* r+ v5 Y
' P/ G8 G* t" ?) W, u( Z% }yum remove apr* w# S; _8 ]9 ]) Z
5.Error.log中有: Audit log: Failed to lock global mutex
) q% U5 y* ~! b) M% o; @
: {" V8 X# O7 o  c% d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( c& U. _, e) _& t( j8 O% Kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& w7 d$ y" w7 a! U, \; s解决方法:
( o$ M$ X! B* _+ o$ N  q, A编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( I9 ~7 ~7 X+ T! ]8 _, H# p. t6 v1 K" p/ B% Z2 Z2 s9 E4 ?
SecAuditLogDirMode 0777) T9 C6 z! ~3 P' S6 W
SecAuditLogFileMode 0550
* {) ^( M' [/ LSecAuditLogStorageDir /var/log/modsecurity* `" N$ U8 k* N
SecAuditLogType Concurrent$ Z, e; i/ ?3 N3 e1 \0 n. d4 `8 Y
参考文章:9 v! z6 m# J  n+ C
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' M, S; F, M5 j  x+ V, M
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-4 21:15 , Processed in 0.067304 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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