找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12552|回复: 0

nginx配合modsecurity实现WAF功能

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

2 F' o0 C  C$ i+ z一.准备工作
! ?1 `: |" j" P
7 ^* q" ?" x8 w系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' o) [' \- M9 w8 m# H
: [* G8 W% f' h% i
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' y. d- }. W  J5 G$ {' ~9 s' y
2 g* K" K- z/ j. b3 \5 P* r
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( F" m, D5 K) @: j. E4 @, W

6 B4 g* i; S) x& ~, `# Y$ I; u0 e3 L$ POWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  _) c( V. j: W/ @  X. a; u

% g% l: I1 Z$ h' Y& I/ K依赖关系:
4 C+ g1 E9 h" X6 ~$ P0 S$ ~3 Utengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! c6 m5 b7 e) _" H' U
  M& x3 U  i4 J; I+ Myum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 ^$ ?2 F( l3 M
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# C- B$ S& }3 S1 R* w
; v: w# I8 Z$ ^. t; o2 s% p! Y) Fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
- ^2 E3 F* U+ t  t# {二.启用standalone模块并编译
9 o; |" v" I' d  b7 o7 z' `4 X1 G1 o' g4 h) i
下载modsecurity for nginx 解压,进入解压后目录执行:9 V, ?4 k; s1 ~- y4 q# N) \) _+ p9 q

- m" u% U! e5 }7 M0 W./autogen.sh* m8 z- g1 }4 x( ~2 N9 y8 z
./configure --enable-standalone-module --disable-mlogc
9 H' ]8 r3 d* u3 P5 hmake 4 z5 \! f! L0 r- N3 u4 K; J4 `6 O
三.nginx添加modsecurity模块* v! s  X5 \' {& V

6 R3 Q0 \1 A) e7 _/ ^7 Y9 |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; L/ c3 u" B% ^/ L9 m; V  ]

! D4 N9 h9 K# ?" \  I2 b. d' Z# L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 s4 S& l* N, ~- l9 S$ Omake && make install
4 b- U) x) X# a" K& E四.添加规则
' a8 J+ `: q$ C3 W. U* D# N8 u; y; W; d% j. m% V  L
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
  t. [2 h; w* y2 ?% z* ~  }  t  j% u5 Y$ V) [3 i& P
1.下载OWASP规则:
( z& e. }1 z& _) e* c0 m, }; m% `% h3 P5 w/ m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 x3 O6 C9 L; E  }2 l& S

9 ~1 b3 T8 ]; [# ^9 M9 Gmv owasp-modsecurity-crs /opt/tengine/conf/# g; t$ C; R. i- F

+ Y# n! m6 ]! t8 A: fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 o- n/ D) Q6 T. A& k! ~
2.启用OWASP规则:
1 ?* j0 f; L$ n1 u2 S3 _
5 T! O8 n9 k. A% D8 X. D; Z8 d. J9 f复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 D# M- z. R/ w& Q
  j: @* ?6 Y8 O$ i4 l5 W编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* q# C/ u7 w+ ?  a; f& u) T1 [  m9 I8 G. G* F; E6 M) Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  {3 @: A8 S. m. q. m/ b
) D& ^4 R, r: F+ Q0 f. v
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& U! @4 `; {8 E6 N: L7 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 h. R# g, V# {8 r6 ~' p7 cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# S! e, c4 T3 y# @$ _% l, wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# N- H2 e4 p, _9 a8 q6 e) xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ A3 c' S0 |) {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( o; M. {6 h) `3 f, c* g5 vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! Y4 \& u# g1 `% w2 ?* c  W五.配置nginx- g! \1 H: t1 B0 @2 Y" `

: |  Q$ Y' N% V, _. C  y在需要启用modsecurity的主机的location下面加入下面两行即可:/ I' V2 Z; a  e0 W6 r8 Q' R

6 k# k  j! _8 Q( A$ f- aModSecurityEnabled on;  2 a* Z- T, G2 f
ModSecurityConfig modsecurity.conf;
0 X/ S( Q  I! Q2 c9 G  u2 J! S下面是两个示例配置,php虚拟主机:8 T! l0 p3 }/ c, \# R, ?

7 R5 F) x8 A8 b& `$ b5 ]server {' Q# h) d" j$ Y, T5 y% _; B! M( m
      listen      80;
6 H, J6 K# i7 r) B8 f7 v! I* S      server_name 52os.net www.52os.net;7 Y# _5 L0 }5 e9 a
     
8 ~1 S0 E& V% z- r3 f  a8 t      location ~ \.php$ {
7 t4 A& y9 H8 @      ModSecurityEnabled on;  
6 S% Z$ U, _! y9 o; ~# ]& w% P      ModSecurityConfig modsecurity.conf;$ }* O9 \) G# b$ x+ W, D/ }

6 B8 h* s4 T  G, J, a" ^. Z7 Z      root /web/wordpress;" b% t  U/ |8 \2 s, P% `$ q
      index index.php index.html index.htm;8 O  Y$ @% m" W7 V0 T
  
3 `! j( \5 Z3 d+ `      fastcgi_pass   127.0.0.1:9000;
  d0 h8 y+ k, Y& O  I1 J      fastcgi_index  index.php;" f" J/ ~% T# O, P- k7 a0 J0 `. H
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
; H1 \1 F4 q. \+ i  C      include        fastcgi_params;! C7 u+ R1 Y6 h4 ?- R
      }
: v4 b$ ~0 W% u8 ^) Q  }
1 A" S; p  x# dupstream负载均衡:
* P& v. `" Y/ m. A8 |- r% y+ h/ M7 v# `5 P) u: k/ f, g
upstream 52os.net {
& V% x: O8 S/ g& h    server 192.168.1.100:8080;
1 B8 w) _2 R. l* [6 U* w9 N7 [    server 192.168.1.101:8080 backup;
+ N2 ~/ {6 N4 i4 u}. I8 J7 I% G4 d# J# o' A
7 i& F4 ~+ H" v- Q& u
server {; [& x$ G  N3 W: `5 a+ S3 U8 C9 w
listen 80;
, \3 T) @+ e1 b. r- `1 }3 Z. z, [server_name 52os.net www.52os.net;
6 c3 V  |& N8 x. P; R: g4 ~
3 o7 I$ \' K7 slocation / {. \' T# B+ q5 L+ W4 C; W
    ModSecurityEnabled on;  
" O% r! V; s: O: M    ModSecurityConfig modsecurity.conf;  
, J1 a; J( Z$ ^$ @3 j
1 T6 N  n  K: b) X1 N6 l        proxy_pass http://online;
. O- T$ G  m6 [5 d$ P/ N- p        proxy_redirect         off;
7 G0 s) `5 v: d- R+ |, X        proxy_set_header Host $host;
" y1 g1 ]9 p1 V: R+ N        proxy_set_header X-Real-IP $remote_addr;
4 B* A' E2 i# v+ R! C7 r4 j; n6 ?7 d        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
- p; s' Z& D8 B- F4 U2 i    }
# T% Z( z: D  v5 q5 h}+ Y' [- x9 z0 }+ ?
六.测试
0 k; u5 x! ~6 n- I9 b# L1 z+ q/ X1 Z3 a$ w' D, P# D3 U
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 H5 p) W) U% \8 k! i- W$ L; ^
6 \7 E4 B7 }/ |6 Z" |<?php
; k; D  o$ d% o3 o! j    phpinfo();   
$ N( C6 ^3 S. D5 _. T# ?# f?>) I- Z) [2 Z' d% X8 D/ l, l
在浏览器中访问:
. L  W" Z1 O) f3 N$ w+ ?
% `/ Z! E# K% c0 yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ q+ r+ k' y- A/ Shttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 W3 N3 y8 E+ a2 a9 D3 Y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。: Y7 w# R: D' K8 z' P& y8 q
说明sql注入和xss已经被过滤了
( t% P* Y. `4 d& Q9 x8 N4 S% B
七、安装过程中排错3 t' H% c: @8 T) n( ?. Y* h

) q# p& R1 _4 Z4 \# l5 k; `1.缺少APXS会报错1 A  t% z8 @6 E5 m

" x- u9 p7 A. k/ @configure: looking for Apache module support via DSO through APXS
$ U! E; K, h: ~9 {8 M& Lconfigure: error: couldn't find APXS' \: T/ M  Q0 z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' Y" u, E) `0 e解决方法:
5 ?  ]% L  Z2 v# E5 a" K; c% {- U# m# l% @. Q0 m
yum install httpd-devel
# x4 T3 F( J% y# S2.没有pcre
* E" @- o3 }% ?# a# A
- @+ }6 I7 a* Y3 h4 \" r1 y: aconfigure: *** pcre library not found.( c0 Z1 D1 k7 R9 z7 l1 G
configure: error: pcre library is required
4 t- B2 @1 V/ b$ S" Y4 ]解决方法:6 K. {" N( }/ d2 z: Y( Q' h* [
  W/ }7 N! L) {0 D. J2 u0 j8 \
yum install pcre pcre-devel7 E4 |& z* b+ F* M. N  r
3.没有libxml21 t2 M7 j" r% k6 m+ O+ |) K, l& A

) b2 i2 L, K1 y, w& d5 W9 S# Y# v+ [3 O- T) J
configure: *** xml library not found.
* s0 t' v7 m4 x5 |configure: error: libxml2 is required
. E2 Q/ I% U2 g- Y" n1 f5 i/ m解决方法:1 I; Q8 b2 p- O" e3 m0 \6 m
$ Y6 J0 ~, l, y0 u- l
yum install  libxml2 libxml2-devel
  j8 W, \( r) t& Q4 ~$ u4.执行 /opt/tengine/sbin/nginx -m 时有警告  G7 s6 S3 i. W1 ~0 |; ~

- H) G4 q" r" H8 s$ o1 [: D' ZTengine version: Tengine/2.1.0 (nginx/1.6.2)# {: V0 L4 [- N4 A% S4 d2 a; l0 O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!, ?6 N- j" g& @/ W
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ o# q7 T0 x7 G% O! I$ Z) k2 ~
0 l. g0 Q1 h+ w* U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 @9 D, z3 A' l2 W0 `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! W" b* L% |% y9 _9 y8 d9 e, u8 R2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 l/ v/ b1 f. d* a5 Q( C# M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: g. V* |  y( |% M6 x6 l! K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 L6 X) G) `% {4 |( f1 o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
" ~4 U' {' F8 R& q, e解决方法,移除低版本的APR (1.3.9); P4 K6 f$ `6 a6 C7 A) Z" R

) |1 v% Y4 b* X# m% tyum remove apr( Q5 C0 a0 j8 b1 F! b  H7 L
5.Error.log中有: Audit log: Failed to lock global mutex
6 c  d, D- {1 l; _: W$ ?  F: n$ v9 X7 x2 a
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( U9 N/ O+ w! B$ a3 r: aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; D; a/ f+ ]2 a3 ?) R: ^/ T
解决方法:; c( Z, n  m* o( m
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" K& S; D5 j, E5 z1 g6 x( \; X! c* t4 n4 h0 x! f
SecAuditLogDirMode 0777& v0 e% T1 y. y1 S& A, D
SecAuditLogFileMode 05506 @2 L  R! ^  i8 S2 d
SecAuditLogStorageDir /var/log/modsecurity
  k  e5 S( [% t* ]% iSecAuditLogType Concurrent- \& m8 T  y, C. s4 a
参考文章:
) K) n# Z4 L$ e! n5 ~8 ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 w$ g! U0 c9 V. M) W2 rhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-16 08:25 , Processed in 0.045203 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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