找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12080|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" G. d# J' @9 T+ U% ^- K* p
' @# U' e( L/ h4 H% A) K* ]( o
一.准备工作
& G+ I& f3 {$ N' c2 }& `" M4 D
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 f5 e  \2 C9 g( O
' @( R) s; V3 t  P  itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 R( {9 j; l7 Q. r$ }2 M
. ^0 L. c9 a3 l1 u8 v0 r8 j( A5 ^9 Pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* U! `; [+ Z' |

5 t' a3 }% P$ b( P5 b* FOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 o, M& L! {# d0 a  }, {% |/ K3 ~* F! h- [! \3 i: w) s! x/ i# U
依赖关系:- g; R' {" C* v7 o0 y5 v4 `; o% M9 R
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 Z3 C/ @* d# E6 B

  O  q( U7 u" x2 A9 d5 B$ D5 Q' T+ jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" O( @5 H' c' P; M' Z
modsecurty依赖的包:pcre httpd-devel libxml2 apr. o) F2 L; O% l4 ?' K
5 [  ~; c. ^1 ^2 ]5 Z6 K! x" M
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel9 e" y  P& X5 M1 X
二.启用standalone模块并编译
! |' C) ^' }, ]9 ]2 @
5 _/ T* B% g- p3 X7 f! M下载modsecurity for nginx 解压,进入解压后目录执行:
5 O' p7 T* |" K# P  _3 f! y
0 r7 e8 S* V5 @$ I; D" Z: L./autogen.sh. i* U# W4 F. [
./configure --enable-standalone-module --disable-mlogc$ z$ _# V- W& c* ]7 t' A
make 5 a" Z( ~2 @' p; U# L
三.nginx添加modsecurity模块0 l! d! ]! P. V6 ~7 `! [  N3 a6 [

; y6 B+ u3 v& H% l在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 T) T+ U8 r' a! `) k. l) Z# b3 V% c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
9 K3 q- i+ A' x7 |& l5 fmake && make install3 b3 c% ~: d' H4 O& P/ o- j
四.添加规则4 F6 r: {; N& K5 c
1 w: f! y! c2 j/ [- r/ x% |" S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 C7 k# n' {, p1 r- x1 {3 ?; k: A3 E9 @, n4 m# D
1.下载OWASP规则:. |! [6 L% p, ]4 @' |

' ~! H) `4 K$ U; @/ A+ n4 x" Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 n5 [+ J% G# }' N. }6 g7 f# h' H6 q" Z( o/ `) {" T
mv owasp-modsecurity-crs /opt/tengine/conf/; e4 t" M/ Q4 b

( R8 D4 f% K0 C4 a. G$ F! Jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 p4 c, L) x7 k* ~% R1 V8 f" r
2.启用OWASP规则:
5 O, _0 a( t7 U; u/ a2 \) w% R
3 m6 P1 m; j  U3 \6 l% q" `2 \复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. U) x  P  G; f, i; d- R7 z! ~  y4 X4 j8 G( T
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 n+ M6 m) ?2 u1 L& T" d7 F% M1 Y0 C, `0 g1 w/ @
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 R" \) u- K: J2 o! g& _
( l9 }& D& D: @! O& E$ F  @Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, P/ y  p8 e: O' k8 m6 V5 q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! W  e, R1 a  }7 Z- I2 [% w: [- T" kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 A; |, Q6 A, i0 ~5 d. }  G% CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. I/ b8 I9 W3 t% Y+ T$ wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 [. o0 r7 S" A" F  |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 j0 c0 A+ q( `& h# gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! e$ K: N( o, h7 X
五.配置nginx
% u3 c* Q1 K4 N+ m: H$ t
' h/ U* p; U$ x5 o5 ?- v) {) G在需要启用modsecurity的主机的location下面加入下面两行即可:3 l7 x3 A( Q" }" R$ Z. ~
2 j" b: E+ N5 D0 O' `
ModSecurityEnabled on;  
. B: _6 G1 q. z9 j3 x8 vModSecurityConfig modsecurity.conf;
& e* S1 [! d/ ~# q6 V4 M5 P下面是两个示例配置,php虚拟主机:
5 c9 _7 V7 h% \' U' X7 ^
/ D7 g; s& E) E2 j6 t* x8 |server {& }2 }0 P$ Y, x* L; r# ~& X& w
      listen      80;! G; a8 Z( t5 a8 K# [. I
      server_name 52os.net www.52os.net;
# w# z9 J9 V, K: J3 ]     
/ E0 b8 m' D% r3 x+ F7 g      location ~ \.php$ {
; k* m, G, m: _0 e      ModSecurityEnabled on;  0 y5 f' z$ G; p) d$ K
      ModSecurityConfig modsecurity.conf;8 A7 B1 E0 m- O- p- t: u7 T
2 ?/ c* b9 i1 x8 ?& I& }
      root /web/wordpress;
  g* m+ o: u; W/ f. {7 D: U0 Z$ W, N      index index.php index.html index.htm;2 v4 l' b( k" U. k, s
  , Q3 p7 z& e7 t" B  M
      fastcgi_pass   127.0.0.1:9000;
7 L& A4 s" U! s& l      fastcgi_index  index.php;
# M" d* Z/ T5 d. |; l, O# ^! N% z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
. a& b  o# F* a      include        fastcgi_params;+ B/ V0 ]1 d# K" w4 r6 N
      }) J/ E2 _7 {, ^
  }5 ], z) h- f% H* L, }' h
upstream负载均衡:0 K1 e6 u! j+ T6 m) N, l0 K
8 M- [& Z2 U; S' m7 i: F8 o
upstream 52os.net {$ F2 D$ o/ M; U& f! r
    server 192.168.1.100:8080;8 K  r: U: G/ t5 x0 \) Q/ k
    server 192.168.1.101:8080 backup;
. S0 H) _9 C% H5 v! v9 h}
8 [( E5 a+ I. }% m3 f
- t' u/ G2 ?7 jserver {
2 {$ p' O" r6 ?5 f8 t2 K' N3 P# z2 Ylisten 80;# S' R' l% m& e9 w$ Q1 E
server_name 52os.net www.52os.net;
9 e/ X$ T$ i4 u/ K. s  ?1 N
& e' m1 t1 b+ zlocation / {* x% d9 ?! v9 [) G3 P* A- ^2 @
    ModSecurityEnabled on;  7 D: _$ B) I/ B; F% f6 u7 v: e
    ModSecurityConfig modsecurity.conf;  : E9 R* g9 I7 H
8 G* S: m7 h$ F. d. w6 B: E  a
        proxy_pass http://online;
) U) H6 v) w* S3 u; l  V        proxy_redirect         off;# \+ U, d1 g- l2 J% k
        proxy_set_header Host $host;9 F5 V& y% C; E( z8 D5 S
        proxy_set_header X-Real-IP $remote_addr;
& \" D- o8 u' t1 G- P        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ f2 T! I5 T& _+ Q    }8 u5 C9 T, Y  q4 j6 l+ P" k4 t
}; l# v9 E  a+ ^  \' a: [1 \: r) ~: b8 c
六.测试
* A% F0 _) B4 \# {5 Q/ ]* k
4 {0 o, V6 h; m4 n' b" W  _& D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ A2 T7 m/ A. _5 I# t
2 g6 r7 a" z% O. J<?php
3 u3 O; h+ m1 x    phpinfo();   
6 j' Z6 `. ]) K6 |  {?>+ w& E/ a7 A+ R" ?1 M" A2 ^1 e' {4 T
在浏览器中访问:
% x8 N5 v8 R# w' Z( m4 i4 F9 q, z( J) U! |: i
http://www.52os.net/phpinfo.php?id=1 正常显示。$ L4 l9 _% q9 \! U
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 E. l' }& Z3 E9 d8 I$ o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! a/ [4 o$ [  S, Q( p& W* B9 w9 g
说明sql注入和xss已经被过滤了
) y; H2 W* x. d' Y1 M+ Q, O
8 u5 S7 w! P. D七、安装过程中排错
8 t: M5 ?" U$ e: [: ~
5 L& N6 J/ o" b1.缺少APXS会报错
: F& Z. ~! S, b& m: r8 s* ]% D$ ~2 `
configure: looking for Apache module support via DSO through APXS- b: F; c. {6 ]: |7 r9 j
configure: error: couldn't find APXS
5 F0 F% p/ o: C9 {apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 x1 Q0 [8 \% g+ m) U, s
解决方法:
, q7 C+ Q# ^; I5 E" D! G: ~' O- c  N2 s! @3 t# D! u4 h
yum install httpd-devel
) Z: v% K* N3 ]8 G2.没有pcre) o+ N) e, `. T) Z
2 p  W3 B3 z8 t  ~% S; V0 z+ c* _
configure: *** pcre library not found.
) H) b& H; j9 i: Q9 @configure: error: pcre library is required
# b7 q7 e7 e$ I5 `  @+ H解决方法:
( j4 ~  ~. A8 u( ]) Y4 T4 t: _/ {" r: w
yum install pcre pcre-devel' d  l! }% ~7 |/ s2 c' U
3.没有libxml2
! Q2 G7 D* I$ x: I! }8 M( s3 ?9 p8 w, q6 Q2 a
# q: p0 A4 k% o" ^
configure: *** xml library not found.# b6 O: m! X) z
configure: error: libxml2 is required
: r  y; k. Q. ]3 Y0 k$ W解决方法:
$ F" @# x( {: D! [8 O/ [# l$ N% j" \+ A& P' J
yum install  libxml2 libxml2-devel" f! u" r! S0 O" |) B! V8 B
4.执行 /opt/tengine/sbin/nginx -m 时有警告0 {% W8 b7 B0 S7 d( W8 _# U
8 `( k. }. I* J* a0 }
Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 ^9 t  i0 P$ w' g/ m0 e
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 S- r2 p! D: k( z# T
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 e: Y9 @9 D7 F: B& @4 x1 \, R4 M+ {0 d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 e6 ?" f* r9 h- E- k8 J" ^, ^
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 R& h6 y3 J0 J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 v$ h8 ]' Y8 o3 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". U% Z' p) \7 Y+ R: k% t! g! Z5 C5 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 y( M" j/ U9 {: H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 l. B: i( H, f  A- S! V
解决方法,移除低版本的APR (1.3.9)
) ^$ E: V3 L9 z. x' t/ B( y) M
# @- q0 \$ Y) F+ @4 Pyum remove apr
% {" X8 M6 B6 e) H. c: L  y5.Error.log中有: Audit log: Failed to lock global mutex4 ~9 r% W' Q2 w; v* ^
/ M0 m' ?! @3 z: B) Y) s
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 N" A. w8 q: E# a  }
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 g1 F( t2 I! ~. |. z" L解决方法:3 y1 K' F2 Z9 L! z1 p/ P
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ U( j2 d+ H) p- E0 v" ?& f
6 j3 g8 a& _& Z+ B) MSecAuditLogDirMode 0777
. k  \% ^; E  ?7 S9 b4 o9 ], zSecAuditLogFileMode 0550+ c" H" W5 Z' q9 s
SecAuditLogStorageDir /var/log/modsecurity
9 C1 L$ a6 g/ n# HSecAuditLogType Concurrent! M* G+ Z% {; `# k
参考文章:
1 W4 t* G8 Q& `- w7 H" `5 S: phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX  F# ^3 N, |& A8 r
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-8 23:42 , Processed in 0.075828 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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