找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12238|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
7 J4 g0 q: R6 r
. H' d/ s: q3 |7 \( D一.准备工作8 [" A2 x' Q3 b$ o8 K. Y$ |; x5 ?# R
, T) h* W  `: g9 m, |: C% e
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 a' `( @* C2 M' @, s% r
0 h3 O  G. S) Otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& m5 }4 ~9 N. G0 k8 G# |5 n! L5 U. k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# W+ A- H8 Y. y) P& c3 ?- T6 ?* A/ J2 w! O5 M2 v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs- J0 U5 p6 r  S/ D, e* X6 d

! @2 X% m0 ?7 U. R- i( T( i: m. V依赖关系:
$ Y( z$ s& Z% ]2 F  Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" N' u1 h3 S% z7 {5 b# J  t: A% u& T' x5 n
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel4 P& Y% f' t: E% i
modsecurty依赖的包:pcre httpd-devel libxml2 apr: S) `% l& M8 @* A8 `: C8 ^8 y" B" l

% O/ O! O1 Z9 g9 d7 U+ D( v1 ~yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& U5 `  @/ r6 P7 l4 Y; a9 H二.启用standalone模块并编译( o* x6 v) b# P  y* g0 \
; A. k9 Z$ [5 W; E$ k7 S
下载modsecurity for nginx 解压,进入解压后目录执行:
: H' f; o& F) l3 R2 N/ W
9 z- e+ a7 I; k4 S, v( S  c./autogen.sh
, f. _- @: A! l+ K" I4 L! T./configure --enable-standalone-module --disable-mlogc
8 d" V) [  P9 W; S+ hmake
5 L. I8 P' y& i/ ~# L& F7 `2 ~三.nginx添加modsecurity模块5 k( ?& |: L( S
, X/ }: \+ C6 h, W/ e9 k6 J
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, A+ f& ^! s2 W. e% P$ B
; t7 x5 F, H3 t' W0 c$ o./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine; Z3 U% Z9 e6 n/ c0 U1 z
make && make install5 m$ w7 S9 u8 `, C( F
四.添加规则
$ z6 a9 H* {# D/ T, D% |) a8 d+ G. t! s% ?0 q5 B5 a$ c# x" c
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 C+ a' y" E) n3 i) l4 g
  P  K- L" N% I' l, m1.下载OWASP规则:- P& @0 c" S* ^2 |9 v$ G/ C4 n7 e
. W6 a: d( Z* T* g* k
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 y( @7 g+ s& M; b( T9 ~6 ?
7 _! |- h/ Z$ _* k4 P# i) fmv owasp-modsecurity-crs /opt/tengine/conf/3 Q) {: S4 G$ b- |/ l

6 c9 W2 a7 \( n' icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 U# J9 \* I3 V& u
2.启用OWASP规则:
. K, C+ c8 A) T# i& r/ I7 j4 F. q. o# y* o" C9 B# v3 Y/ S
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 }+ O7 ]. B0 U- r1 P
, m# I  P; |9 j% W0 E9 ^- U1 U编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 t$ C: e1 ^; }" C3 \

$ t5 |1 l5 ?( Y6 A3 I7 o& [; {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 L8 a" W/ m) h/ Z
* K0 ^3 c0 a  o2 U# sInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 t8 V  Q8 F- o/ }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 i9 h. g% p2 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
/ o- Q$ K8 P# ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- |; V) L+ n6 k- {3 S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; G8 D/ R+ `* j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 z6 L/ [7 ^( a5 H8 S
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ E8 o0 g" I9 N& A& y
五.配置nginx. k2 i& a% [! e5 i) U) ]/ R# z9 f
+ m8 e% A1 t% G) O# R
在需要启用modsecurity的主机的location下面加入下面两行即可:& y' [5 U/ ?& a1 [3 [4 N6 G

2 G: N/ K  R+ l5 r: z$ pModSecurityEnabled on;  4 U: {+ X/ O* Z7 x" i  C5 t9 N
ModSecurityConfig modsecurity.conf;
. Q7 Y. D1 f$ M- J  a下面是两个示例配置,php虚拟主机:" o6 G/ f0 p/ A- e$ G- |
2 I! f! z( y# M3 T9 ?# [
server {* ^. t$ R; y; \4 E; T) m  W
      listen      80;6 U6 X" F) e  I7 b
      server_name 52os.net www.52os.net;2 o1 ]/ l" H9 z% a; [) a  l
     
1 u, r$ v, u# b& ^      location ~ \.php$ {
7 U3 W9 ~+ |. d; y3 Q( \% B; r3 b      ModSecurityEnabled on;  $ ?* q& i, s! q9 C& I: ~
      ModSecurityConfig modsecurity.conf;
0 ?2 f# r( g0 y' W6 U" \$ H
% u3 x. X) C, S: U) E4 I      root /web/wordpress;6 P" i/ z4 h4 x% }
      index index.php index.html index.htm;
3 n9 K! b* A6 x4 D  
( r  i6 Y. l! K( e6 O* W8 o      fastcgi_pass   127.0.0.1:9000;
0 J7 M. Y, z1 Z$ O, [( q# d      fastcgi_index  index.php;+ R0 g* K3 e& q+ h
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" A% w8 J0 Y; K4 m' N/ w& G6 i
      include        fastcgi_params;
/ k6 z# o6 C4 w$ h+ E; H* N' S; ~      }
+ n7 {2 g# u1 P2 @) _3 s  }7 G7 P, b* a- A( g. i7 Z
upstream负载均衡:
) h$ R1 B7 w0 f' [/ [( g: t
* V0 p- ?: `  K; Uupstream 52os.net {
0 \% z, b7 i, a/ I    server 192.168.1.100:8080;* Z  P3 ]5 o2 d& B
    server 192.168.1.101:8080 backup;
  `$ d+ q/ |0 M; r. ~}
1 r+ |" E! ^% r& g8 S
5 u$ b& c4 z$ L9 w7 s/ U4 Nserver {
9 a) H7 G9 Q& F' d7 \( C/ j0 Rlisten 80;; D, d+ u/ r+ K* Z
server_name 52os.net www.52os.net;. v1 T/ q6 ]/ v) s' k

: F: H) Y: J  h6 J7 ]# \# flocation / {" q# `4 H, p9 O) H  S% k) s
    ModSecurityEnabled on;  2 H$ w* u$ I1 t: V& e* m
    ModSecurityConfig modsecurity.conf;  : n7 H' m: m/ D' s8 p
- S$ s5 v; m8 [$ [* Q
        proxy_pass http://online;
( J% \0 m- ^: j; L2 i( D9 m        proxy_redirect         off;9 f. n  `! ]" s- |% I
        proxy_set_header Host $host;
& h/ s, M1 g1 g, l' W% L! P        proxy_set_header X-Real-IP $remote_addr;
" u: c, y# C/ k5 s* w" S+ P        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
3 ^+ M3 p% O) D5 z/ M: l    }
$ q+ I+ Z+ L) v8 B: C; [/ r}
% n/ \, k6 _! P六.测试
3 Z! O. P+ W' X# w
; D$ Y8 H: Z4 c, j: ~7 r0 N我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ f6 `5 |' [* K4 T/ u0 ?% O
+ L5 z  D. a- E/ y<?php- M6 e, v( p9 B! r0 J( R) N
    phpinfo();   
  N4 ?, V4 \- y. ^?>+ h6 i/ ~% x, l4 q+ F4 C8 N5 k6 l
在浏览器中访问:& P8 S2 C( a  W: V
# b9 V8 K8 }% e6 f$ A) W
http://www.52os.net/phpinfo.php?id=1 正常显示。
7 f- f& M( l! N, l% \http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。# d7 {- D! P) \" ]
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。( a3 g7 L  p- D& @4 ~' T+ G
说明sql注入和xss已经被过滤了2 U& @( R: E, _4 j/ _* X3 f

: }/ k" T: Z2 |1 E. t七、安装过程中排错" [! W# L0 g: G! a

' o& T' N5 J/ S2 B2 X1 Q1.缺少APXS会报错+ v  u8 X* J# h7 C6 k  y1 T
' z8 h$ H; W0 r: v* v7 q
configure: looking for Apache module support via DSO through APXS
3 \$ ~; j6 ?0 i; i: m# }2 Jconfigure: error: couldn't find APXS
+ t6 l) P1 ^$ n6 d* Z5 Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 `4 ]/ G- Q3 ]+ t, {0 M3 O
解决方法:
; W5 H8 T. N2 N3 w  M! L7 C- m
8 L; _$ T, b2 d6 Tyum install httpd-devel: j) J: m5 Y  ^" n
2.没有pcre" ~( j7 {+ B# m. ~$ F: z
% U9 ~9 I9 N0 @# `' w( N1 S
configure: *** pcre library not found.
2 m6 r; Q+ ?* H3 Hconfigure: error: pcre library is required
" H' _! c! ^2 b" w6 ]解决方法:
+ Q4 ^* e/ X: r1 j- B: t5 W( s8 M3 Y
yum install pcre pcre-devel( y! P, z- S4 {6 E! T9 i6 W
3.没有libxml2
9 Z4 s$ k2 c5 H5 S5 m% q- i4 X) I- q5 z9 ~* p/ E
# W$ m+ Y  s, M) R0 b. }
configure: *** xml library not found.
$ F- W3 z+ W+ C5 D0 l4 R! t9 P+ Yconfigure: error: libxml2 is required
1 c4 w4 @5 W6 {- C5 L7 U. e解决方法:( z! o  N) ^0 D. w! q5 G* X6 j

( P0 }7 Y" t; c% q, E6 S; c/ vyum install  libxml2 libxml2-devel- J/ X% n5 j. R4 p* C
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 J! F$ P' H! W% H) h8 H

/ J& B; H7 l6 a) D+ o4 d* Y& ^Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 R, ]5 D, I5 e6 M3 @$ O' [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; T$ p6 Y( l3 x, P, X2 a$ B; }原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: E; H! Z5 i# l( a! V' C0 S, m- v1 v, a3 K
' ?# \- L3 @! A  R: e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 d/ a. a% `* v/ S# x+ E: ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9", m! v7 k! Q" t) b# X3 X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# N# Y3 u8 h9 M) C% X. Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& J3 j8 I8 _) T1 d1 u2 R: P$ O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& d/ D8 F9 [; R# B% g  g" l5 F
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 q# f; k, `+ w4 }
解决方法,移除低版本的APR (1.3.9)
+ k6 w3 P4 q+ {" G+ |# _; ]
' R( T' @" Q7 a! cyum remove apr6 R" ]% ~2 b3 D' q; n
5.Error.log中有: Audit log: Failed to lock global mutex
; v- H8 K, s9 v" n8 o* r6 ^
2 I% n, C  h7 y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
" v2 }5 m) O2 G' c, Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
: d: N1 L' R) M" E8 v6 @解决方法:
+ S# A1 \* ?9 ~8 ~. |编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, R2 V4 Y! t4 N/ R( k5 ^2 H# S: r% i/ b6 }, ], K9 r4 S
SecAuditLogDirMode 0777
* E! [! v1 F) h6 ^- ISecAuditLogFileMode 0550
# }* r6 [6 A( r: JSecAuditLogStorageDir /var/log/modsecurity! `7 T8 }1 T* y# a
SecAuditLogType Concurrent; g8 `5 S8 O: M3 T- w
参考文章:; j! t( z  U1 U  h4 E6 q+ x
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 O* r$ g* u( p' W' Mhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-18 07:19 , Processed in 0.070023 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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