找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11035|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 H7 _& B9 }( g0 h. L6 B
) f$ F2 W: l! ]
一.准备工作1 v5 G3 Z) W3 [1 g, T. i; C
1 i2 O6 K1 ^7 Y' J' |; R
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ L7 J$ i$ s% m& ?2 V% ~( S$ P

9 L' G) f. m! W+ ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 o# y* i; V6 \  O; o- Y  d# p( g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' t1 {) p) O2 ]. J) [# x9 b: Y# k- o5 X' w0 A0 Y0 V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' u, t  J) s& k

  c$ k2 ^1 }0 z& d3 R1 O5 ?依赖关系:) t2 d7 P7 d" W! W( e; R8 |! g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 r- P: m7 O, ?  c( E7 h) x7 A' \; V
0 B) S. J  v3 E. V0 Ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
" W9 n6 \4 W: h; |modsecurty依赖的包:pcre httpd-devel libxml2 apr
& c- {- G2 p  A+ n$ _* n" n6 X$ g  i! F" ~0 \6 K3 M6 \
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
" r! y9 I  u5 z" R6 o* |) ?1 Y6 ]二.启用standalone模块并编译. \; l7 S' Y+ a( t1 N
( i2 w% t  H- L* U: o/ n( l- k+ C
下载modsecurity for nginx 解压,进入解压后目录执行:
1 N# a- F/ ~  c; K( I! i/ t
3 ~  h% g( @" e. ^% \7 V# _: O./autogen.sh
$ s7 x2 J' D/ k, A: [/ Q./configure --enable-standalone-module --disable-mlogc
# ]( `2 V- M1 ?9 s9 tmake + G. m: B" o8 T9 M( H9 c
三.nginx添加modsecurity模块9 E7 d  q/ f- a$ N$ |+ |; n

2 Y+ Q$ U2 I# S: o% W在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ T  q( a0 G. f, v6 H/ S
  z/ t6 O% a+ t
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine6 S# T1 }# H) f, z, ?# T9 ]6 w
make && make install, K3 z  g, q3 S& |8 L
四.添加规则
" X, o3 f* g! M( R- J- L8 ]6 |
1 T0 N. _$ m* j; S: Q4 q! }. umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 i% R* ^: Z0 A2 \
' ]" T" Y; b4 ]
1.下载OWASP规则:8 o* ~7 L6 g( Y* P' q4 |& X# k, t$ I
: g% m+ q  v- \# Z4 Z$ ~
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; j5 Z; M" \, q! k7 t" R
8 Y/ C6 F% q2 P3 o9 g1 J4 j
mv owasp-modsecurity-crs /opt/tengine/conf/
- r2 j0 h+ g0 T3 x# G& B% W9 u' p7 z" l7 ^$ x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ P- l2 E; t) c) f+ U
2.启用OWASP规则:7 ^* P& E! M( F, |' w, @. U

1 B% D  x* |% x' L7 s0 d5 D复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. z' C# I4 t/ d' y; G

& y( p+ I. W7 g3 b& }# [/ [编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- P3 x1 h8 f' ^' Q$ n. O1 O, l' o( L7 `5 G. S. o, D
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* r" C* b& P1 Z9 ~6 z1 D

. r" E& g( S) O) j! e+ XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! s7 C1 s: f$ f1 W* g* D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
3 |0 L, I, A1 ~& Z" l/ \) D& h8 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. Z* o# d$ \) f3 y" y9 W, @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. V" _6 o- W5 p) U# {* u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 ?9 k3 R" y  ]2 e2 d  @9 vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf0 |# o0 N; M# @& v5 v+ z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! D5 Y0 M2 |2 G( ~0 p* s五.配置nginx
' u1 A) o( a1 a, j# R) T/ ]6 ?  f8 x  n& M: W) B% y
在需要启用modsecurity的主机的location下面加入下面两行即可:
( \9 Z  e8 W: Y0 e
; l) h& k, q& l, b6 NModSecurityEnabled on;  
) k4 b6 p: j% c+ b5 t( }+ H! _& ^; wModSecurityConfig modsecurity.conf;
4 a. t* c: n: q3 Q下面是两个示例配置,php虚拟主机:5 L* q3 u0 P0 D" w0 n2 d

3 S. q. O1 O+ |' y; E5 l0 B* p, e) t* gserver {
$ L. p8 v$ T; Z! n      listen      80;" E( w7 z* ?- R' |" O
      server_name 52os.net www.52os.net;
4 w3 R6 {) ?9 w1 y1 L4 B$ X     
6 D  L& a7 ]& |* C      location ~ \.php$ {
: x3 N: E0 C6 q  }" g, b; Z      ModSecurityEnabled on;  
. ^5 D* N' R$ @' i2 m      ModSecurityConfig modsecurity.conf;1 Z2 W# h3 e8 F* Z& w' z6 n" y
% V$ G7 t: r9 O
      root /web/wordpress;
5 O4 z& L. c7 e4 ^- j3 k      index index.php index.html index.htm;0 Q% F$ C: s4 B$ [  I
  ) M+ u8 [) k0 k4 R4 i# w
      fastcgi_pass   127.0.0.1:9000;4 s, O1 a: V9 Q* G6 Z) W
      fastcgi_index  index.php;
1 g& x8 v" s# W9 A- U      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' Q7 a7 Q4 I7 W$ q- t$ w
      include        fastcgi_params;& Q) D7 W* D/ `" l! ^. E
      }! j  t& a% q$ D5 d, W, |2 \
  }
& I7 @2 e/ X0 U- bupstream负载均衡:, `# O) o9 |6 Q$ z+ R# x, \

6 D+ ?' `8 W* M) k! Uupstream 52os.net {5 i* C: Z" i7 {5 P# e! t0 R( X
    server 192.168.1.100:8080;  Y. s8 a6 r  }8 F- I; m
    server 192.168.1.101:8080 backup;
5 L' V: U; S! I}# n+ m2 x/ v  w; h. A4 v

- v/ d. e) S- w, E( U4 j5 M! vserver {2 M& O: s( M. E3 {
listen 80;# v1 F# Z. N/ F+ i
server_name 52os.net www.52os.net;
- T" b; X# C# Z5 W+ d8 D: \, y% d
/ K/ c, i/ \; w  @# V4 ?! @) G: Elocation / {
6 N) r+ g( Y7 q+ T3 O4 p- @    ModSecurityEnabled on;  6 q% g2 M2 |/ f, p
    ModSecurityConfig modsecurity.conf;  
) z4 E8 m2 M7 I' p  q1 m; [' f! ?5 A
% \* D: {; b+ l$ N  S" ~( R# C        proxy_pass http://online;
5 Z" W. ?, G: h! D        proxy_redirect         off;
+ J  \( |( R, L. Q4 J* k        proxy_set_header Host $host;
9 R( M+ z! Z& V& s/ t        proxy_set_header X-Real-IP $remote_addr;9 v- D3 o/ r5 o7 F
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
3 s$ L, W: f; r$ e9 B    }
3 y* S% H: A7 Y}# m) Q9 N4 i1 \* J5 [
六.测试
; e9 y! U$ z- I; K' E; U3 g+ F5 f$ y/ V3 ~
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 z2 E: @) [, [  N* @0 N1 l) k* t
9 _7 a# Q  x3 A1 S9 {
<?php$ A# V$ s& f: v  b* A7 a4 O6 s
    phpinfo();   
# L/ `  K; s( D8 o' m* Q8 @6 A1 c' H* g?>
. \6 [: W' z1 X! d; `在浏览器中访问:
7 h/ `2 v2 U# g6 R$ V) w; Q% }* N3 B- \- r0 K# A- X& ~( u8 G; O
http://www.52os.net/phpinfo.php?id=1 正常显示。- N4 N$ {7 [: y6 ?& [
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
1 g4 [2 O. ~2 P/ c# x! \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
* M9 T8 z( M* R5 @: ^; Z. ]说明sql注入和xss已经被过滤了
# D2 O2 D1 Y5 H4 Q8 Z0 I: j) M% G4 u* a1 t% y2 Q: N  S3 f
七、安装过程中排错0 R/ A! _" v; D9 a+ h' ^$ y% ~4 J
9 c% L3 b, D! F1 [3 A
1.缺少APXS会报错
7 P) g/ u4 \4 ^
6 R. w. K: v. Nconfigure: looking for Apache module support via DSO through APXS# h" W7 N( Q4 g+ Q
configure: error: couldn't find APXS/ E6 A4 V  K( y! ?) H7 u% I6 k# D
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) E5 U5 y5 Q! V0 @解决方法:
( S. u9 Q  _% D) t* q5 `3 F! N- t7 J9 ]& h/ m% n; a' f) k2 T/ y
yum install httpd-devel5 ~, U2 B; f0 O/ w. ?& ?9 I
2.没有pcre3 h9 N( u$ R# l3 X& B

3 L: A( p, }2 j# gconfigure: *** pcre library not found.5 K) B4 }' l6 p
configure: error: pcre library is required( Q" r8 G7 L& n" P7 A0 f' u5 i7 e
解决方法:6 p8 ?% O4 E$ y! G2 h0 }  F

+ D0 U) n" ]2 ?. p$ X; x7 Myum install pcre pcre-devel
# b$ [/ F; X+ C3 Z0 |% k: J5 w3.没有libxml29 z% y0 V+ L3 s2 y
' M  z- @; E. M" y) k

- N2 g( n6 r+ P! D( f" Uconfigure: *** xml library not found." V$ j5 _9 n0 |+ K, r
configure: error: libxml2 is required
( i) o) k8 X+ x解决方法:. I' K9 ?7 c' ~

/ _0 t8 X8 s# H6 L7 Zyum install  libxml2 libxml2-devel! E) w. L( K; b4 o8 B" m
4.执行 /opt/tengine/sbin/nginx -m 时有警告. O8 P. N. e0 ?9 c8 }
. _3 l& W2 Y7 S+ P2 ]8 M: L, c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ N7 f5 C/ a- |# F
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) Z% c& `$ p( \4 g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 s, }. `. s& o) T4 C9 h
& x1 C6 G7 K  B/ {0 R- w: C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) I  f5 o( s3 P* `" `. u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"/ t# O6 ~8 s8 n! ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 J! S; l0 P& @6 ~1 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"  x# a0 l) {' A
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ A1 s  n  X2 Y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 f2 y6 o8 N6 i' U' E7 R8 n解决方法,移除低版本的APR (1.3.9)
6 t/ V* u6 m" p- e* z. o$ g& a
# H3 c- t: _6 V% ^9 Q3 }' Wyum remove apr3 p% W, b" ^! U, c: L8 y) _3 u5 J
5.Error.log中有: Audit log: Failed to lock global mutex
! t+ g" L% D/ K" V! w( y7 L1 P6 F
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * P) t- D" v* v$ J8 H# S; k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 Z% p4 a0 C5 L! N% P' m. H8 S0 g0 O
解决方法:7 X5 o! H) J1 k
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ ?. }& I3 H8 t& _2 B0 x. [8 j
  y+ f8 I* ^) f9 m0 bSecAuditLogDirMode 0777
: Z9 t, n- \9 V4 }8 x  {SecAuditLogFileMode 0550% _4 T7 a5 e* ]+ [+ n. P
SecAuditLogStorageDir /var/log/modsecurity
: x3 Q) O! ?1 h, `/ d, u4 w3 Y- KSecAuditLogType Concurrent8 P: Y! t# }- U! c9 c& \  }. j
参考文章:( b0 S8 i! \9 F$ F$ f+ `
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
7 n2 k7 Y  D* `9 y' @* Z4 k4 m( zhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-14 23:46 , Processed in 0.073021 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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