找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12146|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 S9 J+ X. J  }+ ~1 ]7 I, G6 W! x
5 d6 H8 H% G. D
一.准备工作* {! \' z+ ]4 X' h

% O' v. V! _6 `3 h系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! f& R2 u& B2 y# B- f4 Q1 P( b" U3 D% i, l+ R4 n( @/ q% E
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: a+ m1 W7 j3 F( x$ }0 J

3 {  L; n& f3 C  f, Z2 R. ^' Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 F1 M7 g: P( j% Z/ ~7 y7 }% P, t( [# W( O2 {" s3 C
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 t" C( C/ _$ B* T/ D, f) `3 d+ S4 u, G! o% u$ L+ x7 Y
依赖关系:1 m7 y, m* t" k* g, b) Y* j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ l' b  k9 E7 w( c% S" O% [( R& Y5 p, U
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
- n" o4 d4 F/ o! E# r, i) Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
9 T1 v! U* D# {1 Y$ W3 m- {  A! a" k- `
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
8 Z7 O' W* z+ ^二.启用standalone模块并编译
/ \" T" q* z  |* \  B
6 P7 G( A( F  Y2 }1 _下载modsecurity for nginx 解压,进入解压后目录执行:' J4 H: Y$ y5 W! W8 s" ]* T( P7 Z

2 q/ k! w* v; ]./autogen.sh
0 i3 k6 G+ l( Q' B./configure --enable-standalone-module --disable-mlogc% N% t' ~5 {: H' I" [5 `' F
make   H: D! \8 V3 X2 Q8 v) {7 z+ z
三.nginx添加modsecurity模块
* f! q$ p, c3 Y5 m6 i1 n1 j
; @* H+ N  F/ J/ m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' u0 a4 U5 Z( B, x; z6 O1 \" P& V
" z2 E7 |! [2 \; S4 b- v4 p
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  _& }* b; T7 p0 c) I) p
make && make install+ i/ H+ W" ~- r& O
四.添加规则
' y5 U) t. A5 ^1 f9 _
& {, X" v% k3 @5 Wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( e  `  s' v/ l% d" f9 c

2 G% M8 n( F3 I4 e$ ]9 R9 x* F1 A1.下载OWASP规则:& m7 a: s# |6 u! {) B* s0 Z5 X

6 N" D2 Z( X1 `/ X# rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 Q. E, S) \# r& x
9 g' a+ y1 F% @# u1 k3 x. omv owasp-modsecurity-crs /opt/tengine/conf/
# `& M/ g, Z' H+ t' d. s1 d" @6 t  v  p  p2 ?; V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- I& \: }' a. d5 f  H' r) a* G  R2.启用OWASP规则:7 a% V" C$ @3 y3 L* i) L/ [& p

# x& O/ [0 n: ]# o0 ]: Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 R9 U. ~! B. l/ Z/ G' j) Z, R. G9 r% j& i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! `2 E  I: Y9 Y4 k
+ ?" P8 n% G$ E2 xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% S0 ~$ Z; s& _
9 M3 T( T) ~$ W' A3 V# gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf' c! D5 H" A; X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" N8 ^/ s2 P0 I; F( |# YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
  Q: d) M( g( P, ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 X, X* O! f$ ]) h, f* d- E3 T' X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% ?' ~% n8 {, Z8 uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 o1 w' \0 }& Y8 cInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 R9 b* w' K% N/ h, o五.配置nginx
: Z8 T, b1 w5 r/ e0 O2 a8 m
7 x/ _8 i8 O& K在需要启用modsecurity的主机的location下面加入下面两行即可:2 u! i. Z5 {: F

( h" Z1 O/ v8 F; K" `8 O1 IModSecurityEnabled on;  " y5 R- ^, g7 o3 j8 @6 g
ModSecurityConfig modsecurity.conf;/ i( b, C1 J: l, e9 M
下面是两个示例配置,php虚拟主机:4 ^5 k" Z& b# T- y  U" L# {
5 E1 [8 f, M# ~
server {
* ~% i: e) z1 Z% l- U      listen      80;3 O4 M8 }) w) o+ J: w$ H0 U8 Q* ?
      server_name 52os.net www.52os.net;: B3 E/ }* b' V3 ?6 Y4 g4 B
     
# Q6 ^1 r  B" u$ w4 r8 d      location ~ \.php$ {
9 {1 V" t4 \/ I, Q      ModSecurityEnabled on;  
# l& e; f+ @( Z5 w4 R8 @& h  M. F: ?      ModSecurityConfig modsecurity.conf;
0 i9 l& E. N" Q" R0 v
* i& W( T7 {+ }      root /web/wordpress;, O) i) }9 t, U/ P& x4 U
      index index.php index.html index.htm;
5 h. }$ k* I# h% r& k  
, R8 |8 t, U6 e& U  z      fastcgi_pass   127.0.0.1:9000;: ~1 K$ x; ]  ?" h6 H4 e7 v
      fastcgi_index  index.php;
& g/ v- e# Z! I) h% Q      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# i3 l) v5 o# `) S$ z* ^' L
      include        fastcgi_params;
6 g$ e" Q5 y7 k2 m; d2 T$ D' D      }. q. F8 g5 ~9 t! w2 \8 L% I: p* c
  }
2 x* V, h# }0 l& L; G1 rupstream负载均衡:
3 p9 G1 N( m9 V4 h" w: g) z  E- h2 v" Z9 D% K" o
upstream 52os.net {
; c9 R/ h! n1 e+ B4 f: ^1 ~    server 192.168.1.100:8080;
0 f" ?4 A- @; B    server 192.168.1.101:8080 backup;& s+ u( J% {' N3 [
}
/ b' m5 c! W0 N0 e8 P/ ^3 F" B- Y, Z" i
server {
4 Q% q- v) z% w  jlisten 80;
- J7 e, e0 z! S' {server_name 52os.net www.52os.net;" J, [. T3 I% E: E. m

) K. ^2 w9 a! R7 g1 Nlocation / {
: {' e  O4 z$ G    ModSecurityEnabled on;  
% z# ~  _; g6 X& g    ModSecurityConfig modsecurity.conf;  $ o8 C; M9 ?. r3 V" T) c# s

8 |- t0 m1 o: W" Y        proxy_pass http://online;' c* }& y$ b% Q; u; o0 U! D/ g
        proxy_redirect         off;
5 \1 P% _+ r; K4 O2 N' E/ b        proxy_set_header Host $host;: L  [+ u8 s( Z" ^  Z) r
        proxy_set_header X-Real-IP $remote_addr;* V) x  z% `3 }& X- [4 ?$ k2 {) g
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: Y0 `) ~2 k5 X    }
1 B2 Q  \4 h& C) c( M}( g, J0 j* `- o! u* U; B
六.测试
4 ~* D/ l, s  V6 V" b5 M
; |& B2 r  u! t! R( A8 Y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ |, i6 v( j' Q$ @
4 L, V' c( C& h3 U<?php
4 E  O1 I: v1 F* i) O    phpinfo();   
( A8 V& w" }) Y, z0 }) s3 i; |( a?>
5 R" J7 O# c2 _; K% ?在浏览器中访问:
+ g  E' x8 s+ f) o# T
7 G: F4 o& h+ X3 X4 Thttp://www.52os.net/phpinfo.php?id=1 正常显示。
! m. q  X% I* Shttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% P5 _; l. {  @) l" C$ lhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- _. k1 e' G- k( s! {说明sql注入和xss已经被过滤了
. W  Q/ o1 [1 o' a* V/ L# _- n" U' a" u0 A4 D; ?
七、安装过程中排错7 [1 t6 \. R# b+ T' s8 R; G% P9 h9 `
- R* h$ F4 t7 d
1.缺少APXS会报错% Q$ @. K' u! n$ Q( X

. I$ U9 T( N% t$ c' G% H- Iconfigure: looking for Apache module support via DSO through APXS& n+ L, |1 F* c( ?2 E
configure: error: couldn't find APXS
) E( l! K! k- E# bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) Z8 h' V. `2 Y  E" A
解决方法:* @/ Y" V/ C  U3 U5 @) r
  s9 ^; \* e; V2 i$ P
yum install httpd-devel
. t. m: K+ V) w! h5 G$ n( q2.没有pcre
- k" ^' H, a" a( X9 ~  g9 Z% f7 ^, y6 G# B. f" Y; @
configure: *** pcre library not found.
: a  p, K5 @! N! p* u- dconfigure: error: pcre library is required& {" C  s$ ]/ F* }& T; ?  ?
解决方法:
6 `- ~8 \3 V- G# o- {" s
# I$ |) U3 _) P" P/ F! h: F7 Zyum install pcre pcre-devel8 y* f0 \5 S3 W* S1 g
3.没有libxml23 K* t" w$ v+ L* t# Q# n8 ?

1 i$ k2 ?0 [7 y9 j- m! f* Q& v& R  L: c5 G% X1 y6 _
configure: *** xml library not found.
5 W( X/ q: i# C9 wconfigure: error: libxml2 is required$ H9 W* D$ H* m. O' W
解决方法:6 P* |9 d. l# V3 [! u9 x- q

, |+ w$ N3 Y( k5 X* u2 ?2 M/ }yum install  libxml2 libxml2-devel
/ i. F+ I$ {6 _3 x: S" P4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ {0 l' y) X- j5 T7 e- t# z$ N( a6 U4 h5 {, [) _' ~4 O9 b
Tengine version: Tengine/2.1.0 (nginx/1.6.2)- a' [1 p) `2 W& D7 Y5 W6 \! B$ a. q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 `- w8 N6 ^' k( H" g
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ Y, a6 F$ A0 E0 C) `) P* L& o
1 m9 U5 U) X% a( `1 f! Z' G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
3 |; c3 U+ x% W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
2 G/ ?# v! m' j7 |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) Y# Q6 P5 A1 g$ X' _8 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' M* S$ P$ W- Y0 Y: P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" o! @1 a- q- A9 h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ p# N! ^: c$ {$ d2 @9 d: Q
解决方法,移除低版本的APR (1.3.9)4 D0 z/ a$ x4 C
3 X$ m. I- u: U6 Y
yum remove apr$ ?8 f8 r& U& f% q0 \; d, y
5.Error.log中有: Audit log: Failed to lock global mutex# x! c/ c0 N8 r8 C+ n
: a5 ]+ h- i+ d7 y5 ?6 C
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 ?( V7 E) e$ q2 s* @
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 v" R2 [! F  B  P2 x" }/ E
解决方法:
4 T. g  I5 _, q1 U8 j% }( W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 c) s% u& L/ @+ K

' d; c' v+ Z+ u( M7 f+ P2 V+ V' R9 SSecAuditLogDirMode 0777
* l' m+ u7 W2 mSecAuditLogFileMode 0550
+ G4 {  `0 \4 w! T: L# R; [SecAuditLogStorageDir /var/log/modsecurity
$ ?  y9 w3 r4 J( n# U% JSecAuditLogType Concurrent0 c; i6 H1 ]( \3 W0 ?  w8 K6 C
参考文章:& u4 H2 E% k, d. }+ c  x
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: T' _% I4 ^. [4 g( lhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-13 13:52 , Processed in 0.056837 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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