找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11934|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ J9 c+ g3 [' y' g3 B- O, i
$ `5 W  P( w. W, b一.准备工作' n8 k7 I. I1 \' D2 e

5 O+ U+ z; @  Z7 c4 O4 @* O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* s2 {! t' c3 S0 B0 b4 `' f# J3 A! |* I1 T2 Z) F$ I* Z! J' u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% v, u; R" P9 `, Y$ M+ B
, x. b9 \# D9 a+ B& pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 r. k5 E6 u: h+ ~0 y$ y& \

  F; {# X0 Q$ POWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 H$ z4 M0 Z8 @) t# {' F
9 r( B/ k+ y5 B: X& w- E依赖关系:
6 W! e; U: v7 Q# i: _& Z9 Otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' s! F) M7 E9 K/ f. f+ t

3 X- G" [4 R) hyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel8 e' ~6 \1 f. g
modsecurty依赖的包:pcre httpd-devel libxml2 apr
: h  @& E$ R8 ?: M' ^$ |8 I
! G5 @  S$ y  Kyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
7 W5 _  y% b* V; E3 d- Z/ J  k二.启用standalone模块并编译: G" N# G8 L* M5 h  v2 |; q
/ ]5 X* i( I7 B2 V. t) Y6 I% c6 I
下载modsecurity for nginx 解压,进入解压后目录执行:
+ s8 p, P) ?/ W$ u3 g2 J
0 i* P3 v- C3 N& J1 Z, J./autogen.sh+ s$ s7 g. L! u
./configure --enable-standalone-module --disable-mlogc- o/ X; O- n+ U0 X' y4 r2 h
make 2 C4 ~: \1 X; q) b0 ~  Y8 c6 I
三.nginx添加modsecurity模块
& g# E& r/ H! w- E8 H9 i
/ x6 x1 B  F0 k! @6 z在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( M. Z. {$ n! C) j
$ V" D4 a! ?+ q9 U, \6 F( M0 [8 Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
6 q+ O. G- M. ]! @$ B4 E7 l7 O, y: Ymake && make install& ?6 z% U* A# a- F5 Q
四.添加规则
# G0 }) g% S3 g' v5 U- w6 j& A% M
. }( p- \; o. r$ ?modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 Q9 D+ n( W. w
; q5 P+ U- u7 P2 r# D! y; S- p7 W6 Q
1.下载OWASP规则:
& z1 g6 L, C( U5 O" }& B. |% h: _1 X
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 w1 z3 {. J! e9 K( @. n8 C+ H
% p1 C6 Q. u/ Y) R# fmv owasp-modsecurity-crs /opt/tengine/conf/
" w/ y7 ?! n1 l' d: C) L1 G2 S& |5 m( g* x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, ?( C1 v& D. U1 o& L
2.启用OWASP规则:5 |# u1 D3 z0 ?
- V$ M% I. Z/ \5 \9 N9 m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& G: J( Z; R& W) X0 c7 w8 ^
- P0 h2 T6 Y3 x! z% @1 o! }编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& C4 V$ u+ y2 g$ s# Q7 [( ^) u
5 _, {3 J0 M- j$ \
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 {- X3 U: |! G# E! Z1 y6 H/ P9 U- ], D2 z  @- V* y* F
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, t% L; s' ]2 X# f4 G1 |' uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ t" S/ d/ T6 K1 b, p, ]9 t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 E! Z5 u* y0 w- b; U8 tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 T. L' j  j) V$ W  h; D1 j+ c0 J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 ^6 Q3 f) s. z5 d/ {0 ~, j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ n% W" Q% m2 F" D0 O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 l) \' l9 b( V1 p! c+ z1 A
五.配置nginx
0 f, u$ Q. T2 L7 b7 m
8 x5 E0 c% Q% Y; ~" W4 i; ~9 D: T在需要启用modsecurity的主机的location下面加入下面两行即可:; L# O# D1 V9 I# k+ X- t+ |. H
  |- Y" y% q' z% M
ModSecurityEnabled on;  0 E8 j6 Q' r# ?# j% J  i
ModSecurityConfig modsecurity.conf;
# \) I/ `3 e9 B/ N下面是两个示例配置,php虚拟主机:
/ \2 L: y: ]4 u8 p
, l& ^$ d, w& V, Cserver {
; g$ s4 k# B6 Y' T) s- P      listen      80;* {6 K. {0 t, G
      server_name 52os.net www.52os.net;3 g4 J% A2 J5 h
     2 p; B+ @' a5 C& g" h
      location ~ \.php$ {
4 V; b) u7 r! D. u, Z% j% c; I( b      ModSecurityEnabled on;  / ^6 J0 N* |1 i8 {7 ^" f
      ModSecurityConfig modsecurity.conf;
7 \/ U+ Y8 t4 h  H) u* m% d5 v( r3 m) Y
      root /web/wordpress;
% c5 C2 t3 S; N1 p8 {      index index.php index.html index.htm;
% \* u( b" X; N5 X  - f# G5 M; [: E4 [6 H8 b2 d
      fastcgi_pass   127.0.0.1:9000;( U' E4 p% M0 E9 S% x: C) a) D
      fastcgi_index  index.php;
) O% j/ i& i# \. ]      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
. l; ?" k* m" P5 E8 h      include        fastcgi_params;
* i3 C& d" J; Z- v+ z8 |      }
, n% F( h) y! y$ t+ l1 o  }, V2 W) U3 R# \
upstream负载均衡:5 v' \9 ~' S1 I! F& Z! |

, {" R0 x/ C" n% |" H- \upstream 52os.net {
. s* A8 d; [0 `. M: X    server 192.168.1.100:8080;
, f! U3 w" h3 v0 `! j( p3 s    server 192.168.1.101:8080 backup;, R- }) H; ?+ k! L: J, c) t+ s
}
! I3 @/ R$ Q% t1 [
' q' P$ M& `: B5 w4 x1 v+ Hserver {
, K- T- U% ]$ D5 {4 hlisten 80;
' ^- D2 w: [: M: t5 ^6 x2 `server_name 52os.net www.52os.net;
  V9 C& N6 [) ], r0 w3 j$ y1 ]! U5 \+ |8 T  _9 W; c, t
location / {, w+ v: N: R( \5 E( l% Z; U
    ModSecurityEnabled on;  
# h% M2 J; D* V0 B) L; x9 c    ModSecurityConfig modsecurity.conf;  
, _  P$ i* {- v% Q8 U) D2 q
: B5 y- e# Y3 i4 A/ y; E; e2 i- `        proxy_pass http://online;
: A! P- v4 C3 W6 {' B3 G' m4 w' h1 L        proxy_redirect         off;
+ |$ D$ _7 E; b        proxy_set_header Host $host;
) ~# _& U0 d( D" x; j  s        proxy_set_header X-Real-IP $remote_addr;- P. C' J$ j! i4 X: e
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
9 j! G2 _. J* n4 I    }$ s: n( {* V3 n8 w4 W; D+ P
}
( k8 a6 l( B% o六.测试/ ]+ q! b; F. u  n" j0 s* ]

8 C/ B& w4 }/ k# d6 n0 I# C我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 c- r8 k: Y9 T1 m; ?5 T
8 ?3 i$ O" A* M8 N" [! ]
<?php
& C8 A. |* s, j& V: A# a2 ^/ C    phpinfo();   
4 P" o& J% s7 }# N& z6 E0 ?: h. B?>; B0 P. C: `/ v& ]
在浏览器中访问:
  B9 S8 v2 ?, E0 `3 M0 e* ]. }3 c6 C( E4 o. C
http://www.52os.net/phpinfo.php?id=1 正常显示。
& u, S3 Z: ~. v/ _- L2 q, e+ rhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% y  S$ y: v, l% w( E1 ~& ohttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 \7 L- d- }7 P5 |1 ?! p
说明sql注入和xss已经被过滤了" b7 }# G* `( ~5 D  h
* R6 y4 ?3 y) g5 _
七、安装过程中排错0 k8 c% _! l5 p; N6 _8 G
: Y$ |! ~, o- C
1.缺少APXS会报错
) Z8 P+ L$ i$ E/ ?. t" s8 |+ p% g3 ]! H8 }5 o0 j
configure: looking for Apache module support via DSO through APXS
9 j6 N# r  v9 O* y8 R( W: B* ^configure: error: couldn't find APXS) c4 m& t; c* e- P3 |
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 T( R) B0 t( F" [$ p4 y
解决方法:. t+ `, _" {2 ^3 Z4 \, C
+ I& h. q* C# [% _
yum install httpd-devel- @; n% R! Q8 ^6 U
2.没有pcre1 `: ?! {$ {: r- c6 Y" @% V
$ I8 `. r- b. [" {) g! d1 w7 g
configure: *** pcre library not found.
/ e. S+ M( v# s6 Nconfigure: error: pcre library is required  T2 V$ O* d2 P7 e1 f9 w% G
解决方法:
* ]) B3 \8 d0 p# C8 R+ Z  v7 z2 A7 K" i# @) A5 _
yum install pcre pcre-devel& k+ j% _$ t4 V
3.没有libxml26 ?' W2 ?' u- B# E; o! W- r
: A- w- T. m( U& {

- A/ L# Z; G' b3 ~  Sconfigure: *** xml library not found.2 v  z/ U( [/ r
configure: error: libxml2 is required
2 W" h4 b2 `  a: |0 m/ |解决方法:. y3 S  V% P: C# q  T* o9 X" D+ S

5 p' }& P4 @' O4 [  q' |yum install  libxml2 libxml2-devel
( y; D& g: J# p; b) S( D, [4.执行 /opt/tengine/sbin/nginx -m 时有警告
" p6 u' U. k7 p' b, {/ d! G
% S7 v/ o  Y! p& XTengine version: Tengine/2.1.0 (nginx/1.6.2)
4 b6 U4 u# N1 D- A6 r8 Y5 r) snginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! U# C: T4 U" F7 b5 y6 M/ a原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: ^- h& N6 H! ^5 G: T3 ]( s# C3 g( ^8 _" m9 y+ [+ o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ Z1 l; L! d" c5 K" P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 W4 c/ P+ t( m7 d, q4 {
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!9 s: s, a. g# I- b0 [1 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# h. G0 }0 ]2 X; ~/ I# F3 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; H( k8 _# T5 n$ X+ v* O1 o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% a. v. Q4 c& N+ p解决方法,移除低版本的APR (1.3.9)' ~! H1 p. F4 b5 g1 l8 C
4 H5 k: W7 y9 d& a: I: \
yum remove apr
# n0 w$ |& |/ a+ t5.Error.log中有: Audit log: Failed to lock global mutex
  ?1 |/ q  A$ a; H+ _! d! N' G* ^
; c8 y! W( M& P  a2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     3 A/ d" q1 [  p
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 Z5 c( y$ n: X+ _7 V* e解决方法:# }0 G- A# w- v3 M2 w5 A
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* R7 K' ]9 d. \. D2 T% G6 i4 {; ^6 R' x. Y% n0 @: F- [2 ~, M" C# x8 k
SecAuditLogDirMode 0777
/ c/ U! @' W0 ?; OSecAuditLogFileMode 0550
1 g) E# Z/ I' Q5 [4 zSecAuditLogStorageDir /var/log/modsecurity
2 _6 X% R6 t9 P4 C$ ?8 ZSecAuditLogType Concurrent
; o6 q% j  I" k- q& i参考文章:/ o+ N9 u: i3 S3 D! K- g6 S( l3 k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 h# c8 X2 z' j3 khttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-27 18:46 , Processed in 0.065524 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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