找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12316|回复: 0

nginx配合modsecurity实现WAF功能

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

3 u7 b! l/ u" c  ?: y' w一.准备工作0 t& U8 O4 R: z5 U
% y$ M+ O4 x9 n0 W  c  f
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ R; {6 H* s& `) @- C- I

) n* q" L# q9 C: ?' M9 L; mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 Z- [6 Z3 e; p3 P$ f
. s8 j9 ^9 u7 k; w% ?modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 N$ j& d( I* T: q4 E* ]
' i  D0 b9 S. o9 ~0 cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 [+ c- K! R% N, z* i6 n$ X/ f5 i" v5 c* J8 ~
依赖关系:
9 D2 ?3 K& H) h; Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) g& o) L  z8 C% h& o
! z$ J2 p9 }& N2 V# A7 _3 Yyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 o$ z4 t; W! Y" Y6 K3 j( o. wmodsecurty依赖的包:pcre httpd-devel libxml2 apr
; M4 Y  F$ J+ |; T6 d
+ T3 Z$ `% k  C4 |6 _& H, Z* Q- A4 I( ~yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel% M' g/ o3 }/ l4 `  u" G
二.启用standalone模块并编译# M! y; n5 T6 r9 a" Q

3 v, a4 z: Z6 q8 `& s+ u下载modsecurity for nginx 解压,进入解压后目录执行:
: n" _* T3 j6 ^  V7 a2 g. t
9 A0 H+ ?2 b' z1 |' G./autogen.sh  i) s# X5 @0 `7 l4 G) j7 W9 A
./configure --enable-standalone-module --disable-mlogc
/ }6 C2 P& V. W( ^4 \3 K& zmake 9 y/ c/ E( f" a0 c
三.nginx添加modsecurity模块
9 h3 k4 t( {! M* c3 O+ ]+ m
# a% e  x3 J! r5 `6 G( ?0 z在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; ?$ H7 t" L$ {5 v$ s3 t0 u6 J
8 ~% z7 F) V; L, @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  S* y  D# _  d, W/ ]1 vmake && make install
9 ~- B9 M1 ~: X' n" {! S; s  `四.添加规则( J- p# j( x% `& @* b9 n/ ?

% L, b3 [& H/ \% v$ mmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( ^+ D6 Q- f" q7 V. }* J3 v

, ?0 S1 Z0 ^6 V8 z1.下载OWASP规则:4 u. o; P. P* l% X
  r* f! b/ _# R3 ^# P
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ W% X' O* a  m; m0 |7 C/ f

4 A6 _( {  s  O+ v' wmv owasp-modsecurity-crs /opt/tengine/conf/( S# V7 @" Q+ u; q+ |' H; l

- h) d7 v, a) J! c2 Wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& m/ C2 Q0 Q" |7 D, B4 O% V3 v2.启用OWASP规则:
$ o. \: J7 J: q- P) n2 R1 u- v! b! ?  u6 m2 t
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, [# _$ U  v* \9 c- C2 N
$ y+ x; `5 q) {5 n6 t* k" j5 I编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. m: _5 u7 |4 f4 T/ ]
/ l; L0 [9 x2 jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; H$ M! i# E. S: f4 b9 u6 m$ ?1 W. j9 G
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% J4 U+ y; ]* @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ b6 S( f: N. v7 AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. x3 |: d9 z) K: Z) uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" P& R; [. N* i" c& s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& y# ]+ b9 H' I% Y" _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& z6 b* [- s0 a. [) C, x
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 S5 N9 M5 m+ u" \# N
五.配置nginx8 T0 K, m/ }" ~( |! C* d4 F/ |+ M+ }

" {& @; H$ C8 K! q0 P在需要启用modsecurity的主机的location下面加入下面两行即可:
- L5 ^, P* Q. X8 |' M% T0 v; E) J0 u1 J6 B9 e0 n, \
ModSecurityEnabled on;  
4 S0 j" j% P% o+ V) KModSecurityConfig modsecurity.conf;
- O; v3 @% y) W. |下面是两个示例配置,php虚拟主机:
+ Y) e1 ?  S: v5 X8 ?! B# t0 N
+ R3 x" y- @% O2 R. Rserver {
& |: k2 z/ s- J" H" G      listen      80;
9 Z8 a! f( j1 f: N" l      server_name 52os.net www.52os.net;
) m5 h9 n0 C) X/ K6 w# i     9 _, Y% d( }- D/ _
      location ~ \.php$ {6 z4 O7 r1 q0 z5 Z+ o: L5 I" U
      ModSecurityEnabled on;  
6 B: q0 a$ c" c: M" \      ModSecurityConfig modsecurity.conf;
' T- z3 K& A( y5 o
& c% |# P5 |+ T6 f6 r% K: ^+ i      root /web/wordpress;
2 u' q2 p4 c* b- k* ]" h2 ]      index index.php index.html index.htm;. O: C% \9 u- W( ]% b
  
" f3 ]6 m! _  c      fastcgi_pass   127.0.0.1:9000;
4 O: R; I3 k  t% s+ n      fastcgi_index  index.php;
! G' U6 J: f1 a- W' h0 y      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
- N, {& ?* X$ a( ^$ v0 u4 F" M      include        fastcgi_params;
' J! }& O0 d' Y3 d8 h6 f      }
5 _. ^9 n% O+ ]  }
9 N2 T1 _8 e# X0 e; t% {6 Nupstream负载均衡:% D2 }3 j7 ^( D

7 T2 F$ o8 T, K2 E' E6 Zupstream 52os.net {. V  y' m# I6 c0 l2 B8 P1 W
    server 192.168.1.100:8080;
# Z+ q: U9 z8 `    server 192.168.1.101:8080 backup;4 s* f+ u+ n  w, Q& N
}
0 x$ y  N+ |! \% C' Y# a% h5 r) O  u) n, N' ?: e
server {
+ c/ e" t  ]0 P9 y' T0 R) Klisten 80;9 l5 _+ S; Y- \2 B3 W# D8 ?
server_name 52os.net www.52os.net;
/ s  [" B+ M4 _( |1 ^" n) d5 y  Q5 a# f) [( Z
location / {0 B9 Z0 u6 N. J4 R" ^4 [/ t8 X3 a
    ModSecurityEnabled on;  ; g3 N8 c; a4 [8 F6 q0 h8 b
    ModSecurityConfig modsecurity.conf;  
( W: t  |/ Z. s8 a( j( l, Y8 C2 {7 j" T- q, |1 @4 h- C
        proxy_pass http://online;4 y, u9 X( _9 f7 s- N* w; Z
        proxy_redirect         off;
1 P* X# Z: V$ a! k2 n- z9 j        proxy_set_header Host $host;0 D( v1 z' D. p* O/ K1 a
        proxy_set_header X-Real-IP $remote_addr;
6 ^: G  O1 s3 K        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
$ y* ^$ X! S0 v  g* o& w, K    }, w5 i& I. |( s' m- H
}" G9 v% f) z2 Y$ y* S
六.测试
( e* g2 o4 P8 Q5 ]- `3 P+ u; u5 x. K* Z; E: }
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# n. m+ W6 y) ^7 }% m4 `5 m, _7 `' F

! D* ~) o; I+ [) W<?php! k% s6 a7 H, T7 O
    phpinfo();   
+ d. B: B* e9 S: b+ v?>: n9 j4 O+ W" d0 s* {8 Q3 g7 i
在浏览器中访问:! d# M* n+ p$ C% v: ]6 ?
$ X$ c! H! ?+ ~- e! F9 q1 C3 i
http://www.52os.net/phpinfo.php?id=1 正常显示。4 N0 l/ |( u4 q% N
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。5 Y) @. c4 b. E4 C: o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。7 w, E+ }9 j! N- U9 ^. [. N
说明sql注入和xss已经被过滤了, R& n7 p' _7 |2 t) Q6 K4 u' H
! f6 F6 U5 I) d% W- s
七、安装过程中排错
2 b/ ]2 n4 {" A8 L/ c( k8 m
# J9 d. s8 I% A& l( {* t' @1.缺少APXS会报错
' |- c/ w' v; {% O: b; W" M  B$ G* Q4 E- h5 d
configure: looking for Apache module support via DSO through APXS' i4 T- f  ^5 D( D  I
configure: error: couldn't find APXS
7 b3 j% W, K- I4 E; \7 z# Fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 Z$ S/ H4 o0 l+ j
解决方法:
6 u6 w4 D; x: ~0 {+ h6 b. X; w$ `1 p- v  S/ `" R6 E! O3 t
yum install httpd-devel
3 V- j: y; F" S( X: W9 D2.没有pcre3 d" |; H7 y- ]  B# Z( P; x

1 l! Q( }1 ~! pconfigure: *** pcre library not found.4 y6 q5 [6 Q% D! K% Z" A* ]
configure: error: pcre library is required
8 m/ t2 \5 M; b/ W解决方法:' j& L* k7 {+ I% ~. F6 ^" Z

, [: }3 l3 L" {- l( h2 Lyum install pcre pcre-devel
4 W: z- L- ]( T+ H/ R1 W) y3.没有libxml2" a8 ?2 Q- B3 x2 U$ f0 @  r& U

) k- F; o+ D5 B1 S/ u
0 P0 U. L/ v/ t) L2 R0 Lconfigure: *** xml library not found.0 `5 q: I7 b" Y# W2 Q/ U- @8 i
configure: error: libxml2 is required
9 v3 `9 M, v) y; h8 E4 [$ ?解决方法:- T8 U' v2 L6 \) ?/ J" A7 T9 X
- t) C/ G5 n+ T
yum install  libxml2 libxml2-devel
% E6 V  _5 J/ ?. L( K4 O' w4.执行 /opt/tengine/sbin/nginx -m 时有警告
. y3 v5 K5 E; J$ T; m3 z2 T# T: i# p" Z0 W; V3 D: E" w
Tengine version: Tengine/2.1.0 (nginx/1.6.2)" Q7 }1 C  O* [+ \+ c- g7 z/ U! E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& m+ E# t; s; z: R5 n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( p- u) d7 L7 I

$ a+ `7 p0 X4 o) {5 ^, A0 n# R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 C2 G3 c0 x$ |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! M# w! Q$ E; ^, A2 g; @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 E* [- B) H4 O7 G3 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 u4 F0 ^% O+ I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% j4 I2 G; G$ n$ h$ R
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 t' {; ], n0 K: d! [  k3 g
解决方法,移除低版本的APR (1.3.9)
% K/ p3 O8 H8 L
6 j3 M" r6 U1 m, y. y5 V- p0 dyum remove apr; f0 U( r1 ]3 D8 c! F" Y  j
5.Error.log中有: Audit log: Failed to lock global mutex$ l5 @4 g/ l4 [0 A0 l" R
. L% x; ?: t4 j* n% Y1 C- z0 U
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
) W' {5 U7 }5 w4 [8 d4 Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]# X0 [3 }4 V8 I- Q
解决方法:
, k& Y0 O: T9 k; w) l0 H3 F编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! W# P0 w% R  V8 i. n# m. H
6 E) @0 O% j2 o$ YSecAuditLogDirMode 0777
+ @4 w& M: F& I$ X7 F8 }! q1 {0 dSecAuditLogFileMode 0550
, R: }0 @5 b6 O% L8 oSecAuditLogStorageDir /var/log/modsecurity
) J. l% T3 i7 V3 R6 ]SecAuditLogType Concurrent
. w. f+ K; `3 z8 m# o# I参考文章:3 G1 x9 p7 w, |% N
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: t  c! C+ n- ^; x
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-26 03:48 , Processed in 0.045657 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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