找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12868|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( J  P+ F, i* X* c7 z# Z
7 }( H3 s) V+ g2 C0 s' V
一.准备工作
9 l$ S8 d5 E" s- u5 ^, [$ [) f7 ?8 D7 y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 o, J+ n+ r# E, C7 d" i' D' N/ h9 h# \  _2 ?
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 p& F4 D; ?3 J9 @" v# O
+ D9 h8 R/ ^: }& Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' d1 i& F, n# J* P0 r/ B0 M& j8 \5 t8 a1 h1 x6 m( g+ ?+ u2 N
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* @3 |3 ?# c# K
- w6 D# T6 ?% P7 s, e7 V
依赖关系:3 o7 P3 D+ ]3 d9 {9 j3 a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  |8 j6 x; x' D: {( S+ A7 {+ W

, j  k' a+ X& R5 p9 h. Kyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 y, o4 `. W5 y1 nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
- D6 O, p* D6 x, @  T1 [3 S) x3 v& v( V+ z4 F4 [: Y* S
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel+ ~5 K+ Z/ R; ^- c4 E
二.启用standalone模块并编译7 k8 X, ~. h9 m: X) ]3 t& K# w( f

0 K& b9 E7 j* V下载modsecurity for nginx 解压,进入解压后目录执行:
" z( Q  D  C% N7 D% G% m" }5 R1 O- t( ?+ H$ N
./autogen.sh$ D% y' W" x& \+ L5 Q! ~
./configure --enable-standalone-module --disable-mlogc5 n6 `7 o! e8 u" F% H* T+ y
make " p% {7 ^, T  P
三.nginx添加modsecurity模块
% i; |1 `. s% b! F$ C1 y. j8 B' i5 ^. r+ Y' ?* U# B
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 ?* I: f: z. O1 V; c9 N4 o
% z! d; Z% @8 s, h- q7 q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine# y) i. `- `) ^. t) Q
make && make install2 u5 p  n" {! A( y6 S/ n
四.添加规则' k0 i) L$ ]  K

6 A; A: z) \, y; l+ }2 Omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- J0 Y7 @  ~5 A+ s8 M

. p0 H2 D) e! ?) q! r7 v  t1.下载OWASP规则:
. t, c/ [9 [  p2 E; N# l( l! [0 H* }/ Z1 Q+ F7 a- |8 ?% Q( ?7 i
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( @- }' L& z. A
. b" d" d2 p+ i+ Xmv owasp-modsecurity-crs /opt/tengine/conf/" e) ~: D  R1 R; ~3 J  f; M
! K% V; N/ \8 k5 g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; E; T. G- w7 S. k8 k: }2.启用OWASP规则:, _- `8 X5 W0 Y4 g' W
% q2 m8 W) }2 B# T6 G
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 g2 v+ u' _, ]5 s6 ^1 B3 P6 t* W7 m, @2 R/ D
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" m: m& r0 n) Z- P7 t1 x( p
- T+ F' d+ R, Z/ o/ x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* ~, s) v! `0 A4 o+ p: {
: c3 e) p- C0 t4 E
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& S& o  l( _: Q% f, d! B. ?' P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: P8 j7 |1 G& YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! c6 Z6 G$ D, OInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( g+ C; E! Z, [" u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' l, j; r/ f( ^* e- qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 n, H6 B0 }$ G+ z2 x3 BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& S* j, ?# r3 F) x! x
五.配置nginx
( z( n4 a5 k6 O, ^* O  ~" W) X" v7 q: Q4 m1 |
在需要启用modsecurity的主机的location下面加入下面两行即可:6 [0 |. y" @% O" Y3 ?" U$ H" E" ^4 y

1 j3 l  c8 `$ X5 C& P) F8 ?8 eModSecurityEnabled on;  
* H1 V/ e! r! G# j/ m# W2 `ModSecurityConfig modsecurity.conf;
6 N9 f* s. D: O9 Y' m2 j; S- A下面是两个示例配置,php虚拟主机:
) l% z9 o- {  i; i* a# b
& g8 T0 H3 r8 u. l  c' Q1 H' l; @5 U0 yserver {+ y9 j4 ~( I; ]( q' Q
      listen      80;( }8 ~1 b% \" U" M4 U* a
      server_name 52os.net www.52os.net;
' S% b9 H( e" F6 y8 Q$ o- o, W( e     ( |1 i) a) s0 h5 q
      location ~ \.php$ {
  G) d( r* ^- b% R$ r( ?1 i7 ]      ModSecurityEnabled on;  ( h& G- f* P/ G9 ^: c8 v
      ModSecurityConfig modsecurity.conf;3 l: ^* F0 I$ y8 V# `  L

( K' V8 y6 R% z/ V5 P6 n9 h: Q5 [      root /web/wordpress;
. r2 T+ |3 ?  m9 t" {( @! Y      index index.php index.html index.htm;
, i+ v; h! e3 L! e& O' u  
" ]- ?" Q3 M( D# W% S  m( Z      fastcgi_pass   127.0.0.1:9000;6 Z! m+ ~/ T  W$ ?5 @
      fastcgi_index  index.php;
9 E2 }" \3 h& e3 c9 J- o      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;  }( F0 P: n% N
      include        fastcgi_params;( R' h  Z$ N4 \+ y* H9 o2 n9 g
      }( {8 U0 ~/ G, {4 X7 ?
  }% p5 R- A8 w  T
upstream负载均衡:
  H5 z! U3 o/ e  @$ A( o* W6 v3 k% ?1 d% `  J
upstream 52os.net {* B  r' O* H/ s6 W, \9 e/ @
    server 192.168.1.100:8080;0 m, Q8 F, B0 [! J- Z2 S
    server 192.168.1.101:8080 backup;
  L% J' g1 a# g; T}
9 Q9 i+ k3 ~/ _" ]* s7 }
% u  c, B* K, a! {+ U! Qserver {
8 U5 l# j& o) W$ F/ m  D" \& mlisten 80;/ S' E9 H1 l, I9 h
server_name 52os.net www.52os.net;" i# p, a& g  P+ W2 I

5 A% X8 Y; T  C/ e2 Rlocation / {
; O- b1 }6 l2 Z    ModSecurityEnabled on;  
. S( w3 B1 x4 }2 u5 d2 h( U    ModSecurityConfig modsecurity.conf;  
0 H: C$ K3 f  W# X8 o
9 ~3 S. }9 H% u; C- ~# H3 R        proxy_pass http://online;6 g* ^# T! _( Q/ @; |& w, g- G
        proxy_redirect         off;) i  N  C7 ]  U
        proxy_set_header Host $host;
7 N3 A- \+ g4 @        proxy_set_header X-Real-IP $remote_addr;
/ h. Q, o0 y; f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;$ |2 c3 j& C. Y
    }
; a7 f' e$ q% Q8 Y2 M}# L6 P2 n7 h. v0 E2 V- W
六.测试) k+ _3 f8 d; f0 e

6 p4 C" ?* }9 A2 E$ s6 I, A# j我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 S, b. v6 H& b* M  l, j& U9 r! N7 M5 R& E
<?php. ~9 E5 @% A1 s# |2 z4 H
    phpinfo();   
! h* T( u1 b3 _% \1 A3 t) ~+ X" Y?>! e/ U+ k, _; E, E  E
在浏览器中访问:  n: h% t3 \  O8 x
% b9 W9 d: B7 l, `* M, d
http://www.52os.net/phpinfo.php?id=1 正常显示。- j$ C! f5 ]! A/ [) w( ?0 W
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。, V! W3 U$ i; P* e/ O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 H1 B/ Q% h  a/ n' I说明sql注入和xss已经被过滤了: r$ p$ H3 B3 X& `. ?

( ~) H- X5 x+ b. k七、安装过程中排错
. g( ^. E9 P$ c  `9 u7 M$ v
! E( s9 h' |  L+ `' T, c1 c: Q" i1.缺少APXS会报错
* f" ]: S  o) P  \0 M* ?
3 i* g, F6 F$ }- o. qconfigure: looking for Apache module support via DSO through APXS! ^) w  N; _" f) `6 @- l' f
configure: error: couldn't find APXS: X" t# @7 i- b# G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: c% Z4 c4 ?4 R' W# q* N/ I4 v
解决方法:
  Q: H( b. _7 z) z4 D. n  _/ \' I$ t" S4 g; B/ n( `  F' f
yum install httpd-devel/ M3 O- O+ E0 Y: g7 E
2.没有pcre) r" d* Y3 T! |+ l& ~  {% |2 m/ k
( X) y: o+ _# e. f2 ?6 |1 M; Y8 I0 _
configure: *** pcre library not found.
( N0 ]- U9 |. S# Vconfigure: error: pcre library is required; U( y& e1 `, }- y; \6 W6 j0 Z+ P5 r1 c
解决方法:- X: h- J$ o- _# v3 i* w
1 q8 g6 J8 t3 I2 U% D7 W
yum install pcre pcre-devel
3 i+ G( i# X% l3.没有libxml2! f# i; P) N6 s: x6 E3 q
9 a" k& Y! F% P/ c

1 }  Y4 ?3 ], G) z$ x" i9 Econfigure: *** xml library not found.
0 `$ M( `1 F1 cconfigure: error: libxml2 is required
7 K) ?- i0 Z! v1 t解决方法:
* k1 k9 g0 v) _! A! S/ ?" l6 U9 }. o
) Q; k1 x3 d: Z, q+ d. Y3 G# lyum install  libxml2 libxml2-devel( I; ~; x& U5 {  w
4.执行 /opt/tengine/sbin/nginx -m 时有警告! ]+ P! C' X/ g( c) Y8 K+ V7 q

' T8 U0 i' O0 J# Z$ cTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 Y. J) k5 |9 v0 Y2 W5 Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: N0 u  B% L( |0 @* c2 n3 G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 [: H5 j* f- E( Z8 \! C
& |# Q$ M* V' Y$ c( _% J0 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: y* m0 Z5 O  a$ a/ b3 l9 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
) S: u* [1 X  J2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 B4 b3 f* n8 m2 M, Q0 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& ]% J6 d/ @- F* w. R6 b' y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, s; E2 B+ }. O' P2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! `! b- K7 b# u8 l; ]
解决方法,移除低版本的APR (1.3.9)- J$ }; v; ]- e% ]. q8 O, k

3 |- _4 |% ?$ D: V0 G6 w4 W6 uyum remove apr! p0 G3 G1 U" |( y: p
5.Error.log中有: Audit log: Failed to lock global mutex5 v) F0 e: ]/ j. v4 a4 i

% Z& a& b/ X5 _& E2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 M( k/ v: `- t  ^global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! A6 {. t$ }) W
解决方法:
3 `: ]% Q' v& P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
  p; h, F9 c1 J% b* a# ]- {" f$ V' c% b5 N) s
SecAuditLogDirMode 0777
( I  B3 y0 v2 ~. Y. eSecAuditLogFileMode 0550
/ U# L$ q5 S# S- ZSecAuditLogStorageDir /var/log/modsecurity: K$ r3 c5 N3 N" T: H
SecAuditLogType Concurrent' U( c  y3 R) Y$ d) c+ c/ E+ J
参考文章:/ Y7 V+ C3 d/ O& V* E0 D: F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. H: b$ y+ w9 l) @http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-20 07:26 , Processed in 0.045844 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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