找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12607|回复: 0

nginx配合modsecurity实现WAF功能

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

1 d) q* l: C+ b* s: `, I- _5 r一.准备工作5 L* q: s3 F. r+ p# ^# m2 h- U$ P

! b3 U# q1 k$ k1 E. U! \. R系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. F4 L! ^: o- r5 l3 B5 [) M9 g# z
, A7 D/ V( c; g4 D% \1 X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- a0 A9 ^* I0 ^& d9 j
+ w# v, l, ?. I6 \" J8 v
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 `% P: ^. ~7 l8 o/ G2 ~
7 \* z* b& |" R+ I& c" I1 b3 j( [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) @, H9 I4 @2 s1 X4 I5 \
5 L& i7 b# R2 W9 I% d$ i依赖关系:8 V: {2 t2 Y* F7 N1 A" I0 G% @
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; l1 J- c0 \% y1 o6 i: I
5 j, ?$ ^7 f! x6 c" G! X2 ~
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" G0 Z* D5 o3 C. Y- [
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! y9 z2 G  Q0 {0 }- W$ B' j& M$ u* |% Q! q% Z! i5 N
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel) B( [9 a$ W/ N8 T$ N
二.启用standalone模块并编译7 _# |  _9 j" P: e; D; I+ w
& `/ ?0 I1 m- _5 ]) e
下载modsecurity for nginx 解压,进入解压后目录执行:; X! z0 Q* V+ n% h
+ j7 P+ ^+ U. V. E3 Q$ [; w
./autogen.sh
" ?* q5 c2 Z( W+ [./configure --enable-standalone-module --disable-mlogc6 ?' U+ _- Z" P" P
make 0 }$ Z, f2 T! K+ I! b' A4 }" @0 c
三.nginx添加modsecurity模块' y, s) ?2 ~' U  ~6 O

. l8 o! A2 q8 B) P6 D在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* L5 f! x9 d; O$ T. W! |4 P
* {5 H/ o  o0 X# s& ?
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* Z2 M$ l1 A( G$ K& z" E
make && make install9 l) E- B4 I* |& `1 U
四.添加规则4 t% w4 |- Z' q
3 N1 B0 m2 O2 K4 x1 l
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 Y/ U# D' y" `9 P
  e* T8 d1 D  D( R
1.下载OWASP规则:
5 \4 d  {3 m) v* w/ G$ ^
. g: @! A; L, Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 S8 J0 i0 U& e; a( G' ]+ _

4 I$ D  v/ B( N& r( Cmv owasp-modsecurity-crs /opt/tengine/conf/
9 O) t- G( I, d$ U
$ P; S8 X- d7 i+ c9 y. rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, Q- h# D+ j9 o9 |# F% g2.启用OWASP规则:' V8 R6 B1 k- g% l5 t  R9 x  x

) Y7 C/ }$ x5 s" t) o复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 `9 m* j2 |+ w8 h2 |
! L. n, ?- `4 U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 E5 L5 ~& b+ e  n* l
+ w+ a5 q; A' ^- B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 \9 T3 A: E$ a

1 w+ @% C( {) j. _4 KInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ x( o9 o$ _3 j0 I" D9 N: r9 V% s: ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* \" f! l- d, i' g. iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( x' N% U! G5 X  `1 H! {# I( A! hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 y( P& r6 r* mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 T" M7 S% d5 n$ tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 \9 s3 F. M  {
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( Q0 d) E/ i9 I# S$ j五.配置nginx
1 E7 e6 G% m: w3 \$ Q
, V* T4 w+ z2 z6 `2 d% ^在需要启用modsecurity的主机的location下面加入下面两行即可:
1 p, v8 L- g- q3 h% l1 h( _: D
: s# E' V! U. P9 v4 lModSecurityEnabled on;  , M9 P* S0 i  F" i9 W- ?
ModSecurityConfig modsecurity.conf;
/ l: G6 [% q# _4 w; J* D+ {下面是两个示例配置,php虚拟主机:
3 i) q1 ]0 F; d+ b: X9 @' Z2 m# U* l/ i0 S# y4 _9 r4 y
server {  y$ y5 u4 O' J2 V
      listen      80;
5 o4 a# u) y. W4 y      server_name 52os.net www.52os.net;
' I$ M  k9 K8 ]; e     
6 r' j, C5 {, @      location ~ \.php$ {& u8 E# ~' o; H7 n; X# s/ g. ~
      ModSecurityEnabled on;  9 G) w. ?" [: D- B
      ModSecurityConfig modsecurity.conf;
* ~/ j$ J( P+ w: a. m; ^4 {" Q- f  {- k- m7 E
      root /web/wordpress;
# _1 M; Y0 s$ y9 e      index index.php index.html index.htm;
1 m8 w  S: w- l( a  ( r6 A4 \4 m! [" O' ^
      fastcgi_pass   127.0.0.1:9000;
" \( W6 f% F  G2 A1 x: }  k0 }      fastcgi_index  index.php;+ d) n( L/ P& Y- C
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
" M* ]" J6 G  W      include        fastcgi_params;$ n0 x2 c. t' n2 a6 k' n9 ^& \3 J
      }
7 Z: H" Q9 j+ u  U. U) Y  }& i6 w) z& T# D. ^  ^$ s9 s4 n
upstream负载均衡:
+ G8 X. H; X6 g5 Z2 K% h6 W4 _: N1 N" M* U
upstream 52os.net {
5 Z8 B8 a+ I6 B/ M: R* K    server 192.168.1.100:8080;
9 o0 ~4 R0 a2 o6 D8 M$ Z0 |    server 192.168.1.101:8080 backup;
8 [+ E* E  ?1 X}# O7 [$ W9 N* @8 K" n  Q! q) h

3 H6 t  w5 n5 Jserver {: I$ {' M$ P* u; E) ^
listen 80;
3 b! [2 i+ l( Pserver_name 52os.net www.52os.net;0 _# b8 w; t! z/ N: M: Q3 U& _
5 K! v" Z6 L6 `/ F4 M' {- `
location / {
  Z( G, Y  j1 e9 ~8 r2 R    ModSecurityEnabled on;  
8 `2 u# r" v# i% H- w) y: G4 N    ModSecurityConfig modsecurity.conf;  $ `! V0 M! F  U( w* U" w  e; x6 K
+ q$ l& k- `# p! C4 Q$ q& h
        proxy_pass http://online;
4 s5 y8 m5 t7 a. {5 a        proxy_redirect         off;
) T$ i0 R; R) S0 a, L        proxy_set_header Host $host;
2 C/ e1 R7 F, O7 `9 _8 n% s& q7 q6 i        proxy_set_header X-Real-IP $remote_addr;
5 T. H; T2 U$ O8 ^9 H) y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
% n) ~1 p: G% Y' g) W* }7 V/ j8 a    }/ ]9 ~$ }5 Y8 }5 T7 M
}
' [8 |9 B# p4 F9 }9 J六.测试; V. n: X# R3 [7 ~6 z2 h
+ O2 [- x! y+ u& Z3 b( j
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 W, ]* ^7 Y1 M+ S& f! f9 V

7 T( Z6 [. `4 Y# g( h4 d  G<?php
* z: v" m; }! ], e1 q  A    phpinfo();   
+ @! J/ c, e; R9 _?>
5 Y- f3 f3 u: }) p: q/ B在浏览器中访问:1 }2 {9 I. a+ p

8 @1 Z' V4 R  [& M4 S# ihttp://www.52os.net/phpinfo.php?id=1 正常显示。! M1 q  e( j7 B. M* S" X$ j2 ]
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  A; m% l: e8 B- d
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
7 l$ S3 O( s# o2 f' n, B说明sql注入和xss已经被过滤了
% C0 m& `4 x6 q# {- A( U. @5 W& V9 k# n1 r' P% e5 Z- r5 q
七、安装过程中排错
" f" v' q. i% X' x# G! H; c6 ?- R. Y
1.缺少APXS会报错
2 i- p* E6 @: t2 E8 }$ }
; g$ L. e& ]. o2 r( Cconfigure: looking for Apache module support via DSO through APXS/ d, f1 t3 H; e6 b
configure: error: couldn't find APXS9 i- L( }- s& I" U- m# ]4 i6 R0 g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" _' _+ z5 G/ ]0 G% A+ y
解决方法:- E5 b( I) U( v7 \. \

5 Z: w6 u% S; |1 d- s3 N. Z& \# |yum install httpd-devel3 M) o% f; g2 B5 a& b: \, K
2.没有pcre
3 \- g8 q( z/ R6 k+ a, w% b9 _7 b; H3 O0 `( C
configure: *** pcre library not found.
# K0 I1 d/ S$ D+ i$ [3 ?* P9 |configure: error: pcre library is required+ H8 ~" ]3 ^( c! ^- U* l
解决方法:
5 o$ S: G) m! ?* ^7 G. e
& }2 {  {& E: j! Myum install pcre pcre-devel
( B$ y8 s+ A4 x) g' i# f9 T3.没有libxml2
8 F2 [9 W+ f' W% }* H/ L* `1 G
3 h  W- K' ^  X: d; o5 n' G7 a0 ]7 H; W# i3 ]  G
configure: *** xml library not found.2 R. M7 J9 M2 Z0 l4 W
configure: error: libxml2 is required
3 h2 p- |6 X0 p* q解决方法:  X" p0 E* q% F( l- s$ n8 J' C

! z* _8 U$ \& \3 l/ N8 _# U- g; ~yum install  libxml2 libxml2-devel
9 R- u. g% m/ D! B! x7 o4.执行 /opt/tengine/sbin/nginx -m 时有警告; g8 s2 U/ Z8 Y- j6 i( S

% E$ Z7 f% }; ?" W4 m( X, WTengine version: Tengine/2.1.0 (nginx/1.6.2)
( j4 W% m' p. k+ ^8 t4 }nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 E" |; s2 H8 x1 F& a/ z7 e2 i( `2 T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 l/ Y2 H4 \& o
+ Y4 f1 E1 V) H7 u7 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 S5 E. Z  A9 d% z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
% [- x: _, ^0 N) x* ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 @) _, g+ W  [; ], j% k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 b' f% l5 \5 n* b$ t8 S! k7 {' }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ Q  B- r1 q* N" p$ g: P5 q1 M! O* u2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- z4 j. c. N* B. c/ _+ S
解决方法,移除低版本的APR (1.3.9): S! I' f6 j3 m/ W7 F

( ~; d1 y! J+ E9 [) Wyum remove apr
+ G4 o7 I8 y# q" {6 h5.Error.log中有: Audit log: Failed to lock global mutex! D; }# C) f2 |, Q

, L' v8 c- F' b. Y4 ?5 s, m2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     $ f  X7 t1 i' V
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' k* M2 l/ p3 z
解决方法:
$ I- W5 S% J+ }& D) l, q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 D9 U8 r$ M- y, h% [5 h9 Z, F% p8 j$ Y0 x+ I8 [3 ^
SecAuditLogDirMode 0777& B9 n- k; p1 o0 S( \
SecAuditLogFileMode 0550) G1 E' t! k  e! T/ C
SecAuditLogStorageDir /var/log/modsecurity
. A4 L& m3 o: H* r2 RSecAuditLogType Concurrent
" w5 \+ R3 m) m: _参考文章:
- ~) w1 ^- Z# T, {- U( g" c' rhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX' Z: A5 p. W6 x' S5 y! B$ A
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-21 03:23 , Processed in 0.073100 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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