找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13117|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' {" U& C" u$ ~9 Q. w3 ?
) @- j9 P6 C7 ~0 I
一.准备工作
9 G; ?- R& G* g" J1 N( p; a; B8 r, d# I7 d/ f' e$ l0 i) ^! c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0* V5 z2 i4 n' h6 z( b7 d
! }1 V& k0 q% I$ A
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ f: a8 @* C1 G. M- C; m% B: G) z
5 _2 |( |4 R) X; ~  P5 M4 H3 m
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 Q7 p' h6 X+ j1 _- W% Y

, n4 d2 b7 U3 L# |1 u0 o1 XOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' w9 m, B+ T. b, D& c3 s' e8 [8 b8 Z
6 b" h  z0 Y9 D# e7 _/ e0 o
依赖关系:
( L  _- a. R& [. N1 R: ]9 Ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 l  W' C; _* I0 H0 Z0 A$ O6 c* w  r+ y* Z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 v% d# T/ V) ^; L/ f, Cmodsecurty依赖的包:pcre httpd-devel libxml2 apr  k) c' v& _" m8 i0 O4 f  K, o
# S0 A( p, l& g/ Z1 ?
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel* S- F0 p9 ^' d- K9 o; ~! g3 f# w
二.启用standalone模块并编译
8 i; W. f, s2 S( n; l
8 r9 K( w1 {1 ~4 ?5 }下载modsecurity for nginx 解压,进入解压后目录执行:- e- h% [3 a" f

/ k. m* m8 t8 G7 g- _4 v7 x# f5 e; R./autogen.sh  L& d6 _( f, S: l
./configure --enable-standalone-module --disable-mlogc; u2 B! x# F* |% Y5 `
make
& p* d, n( W0 k% r4 H$ s1 y三.nginx添加modsecurity模块
1 v0 u; n: D8 p' c' g4 M5 A7 \
, k/ h/ Y4 O- f& {$ d在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 q5 k7 E9 W: ^/ s& R) b2 H
0 z4 Q% s3 z) |% o$ H3 w& i$ e1 ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine" M2 m' f8 G( b0 Q
make && make install
, L: s0 A5 \' C9 B/ k四.添加规则
3 u* G6 k; T4 v- d6 w& e9 G
& U' h7 Y+ R% K5 emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( `4 x4 N1 ]# S& W* k) o) I: `9 h
% q* P1 e& c' H: F1.下载OWASP规则:3 _5 r; @" R- a. g; ?9 z

* g3 c3 G0 T  p% Jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
+ u# v/ r  T+ [8 A- A, u
) d  _: ^' N0 v. H  B- T4 umv owasp-modsecurity-crs /opt/tengine/conf/
# ~1 z9 m$ o  ?2 V% S. g" N/ F( _+ P5 s0 ?
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf  L$ ?$ k3 x- C
2.启用OWASP规则:; R5 h+ C. O, c3 k/ f& x1 W/ X

- t( P1 z% V, Y( n复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. ^1 ?  S/ b, o9 ]
$ o; z  w/ K5 |# w) x; g; J编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. Q! i4 M1 S0 _" x
8 G' K1 y6 G+ W9 i! g" p, Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 d0 F* ]# p$ d0 U! b

4 C, `3 R5 v% R8 Q% K* B8 U5 F' a9 l$ CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf8 V! M! R2 L& q& y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; p. Z* Z* V6 m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 Z' u: _: j( w* j* d- s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& A* b& j% e3 M2 @! p5 _$ O( n) [
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 |; v; G% n+ z- k" R! q! G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- \- I2 h+ p* P1 c; }, ]7 L4 gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
( f8 b7 a8 _6 ]五.配置nginx
$ _+ H3 U5 e6 v
2 Q5 r% d( r% }2 O; S在需要启用modsecurity的主机的location下面加入下面两行即可:
# M* I+ m! Q" m& e  E6 }; T" E$ l) v- }/ E
ModSecurityEnabled on;  
+ d6 x5 R: q+ L8 L- RModSecurityConfig modsecurity.conf;8 G% [6 X% @: d" N" c! Y( ^2 Y1 P
下面是两个示例配置,php虚拟主机:- G# ^2 T8 Z3 m' J; ^8 V0 }

4 f! F- p: Q$ H' F! X  Vserver {" [! s% z7 B. R9 [  C( ]" A
      listen      80;
) E  {) i" [0 _      server_name 52os.net www.52os.net;) e& y) E) {5 {3 e5 r
     
" k- J* |0 o, ~9 b      location ~ \.php$ {0 I6 |# K3 g( t; L. G
      ModSecurityEnabled on;  . d8 e! w1 S, p; h, i/ C5 p/ i" ]$ V
      ModSecurityConfig modsecurity.conf;
2 p: V; B+ ]* v% {" v) S5 D
5 X3 ]3 A% {* W" m5 C  Z      root /web/wordpress;
. R, K8 a6 s+ W      index index.php index.html index.htm;7 R2 [0 u' e) J; s. K  c: d, ~
  
3 K9 c/ L  y; C8 b- B6 U      fastcgi_pass   127.0.0.1:9000;
4 ^- r0 ?: e, S" A- u      fastcgi_index  index.php;
; n" b  \" q$ N  u      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
9 f1 s! V7 d4 q- R& D      include        fastcgi_params;
* l. D5 R* k1 q& \/ f$ I      }
) Q2 H( J8 [: C  }. d7 G+ q8 d) D. S* q
upstream负载均衡:' a1 s% K, t: S, T( S7 ^

' N' X0 K% U* i7 y6 m3 bupstream 52os.net {6 J- K5 f. N5 M: Z+ o" g! C1 |
    server 192.168.1.100:8080;8 n. L6 l: x6 m+ _& B, n1 T2 q6 W
    server 192.168.1.101:8080 backup;, m( g( \* u) q/ s% |$ m
}
) R) ?7 J! a& E; P: F9 ^
3 Y" d0 [5 a, q9 g* C. s, ^server {
6 E* T9 v( x; U5 ^: I$ Ulisten 80;) r1 ^) z5 ]8 p( [
server_name 52os.net www.52os.net;8 _/ Z$ z( g( {# K$ h& e  \/ P

0 O. U0 Q7 z8 @5 u+ G  Elocation / {
0 q6 j- o% l. d: E' x& p( ?    ModSecurityEnabled on;  3 |" D# H4 Z$ x% V! d0 m
    ModSecurityConfig modsecurity.conf;  
# _- g  a4 [& T4 L; u& c! G8 \& f+ s5 y: {
        proxy_pass http://online;
. }0 ~7 a+ `2 P& P0 X( G        proxy_redirect         off;0 j$ m/ _+ m: ^5 d$ s1 }( }# R
        proxy_set_header Host $host;; K: k2 W% A2 c) T( G2 X6 P
        proxy_set_header X-Real-IP $remote_addr;
  A% J- m% {* [        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 u6 G; Q1 _" }    }
3 a0 W- [: S" B  n4 W}
9 ?5 T; ^9 b$ x3 d' {  S0 t1 ?六.测试
9 R. C1 k" n% W* G5 d( |. Y! ]  Q5 A
$ l+ r" b3 m, T) D9 l+ k  Q0 K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ v; g7 F* E4 E6 p! O

- d" j* F( p3 B<?php
2 ?9 j  Y/ |  ^: {3 [' ?2 m. [    phpinfo();   
; b# k7 c! x/ D$ V: ?; t?>+ x3 ~, P' S; w4 @& J
在浏览器中访问:
& d; h' F- ]. n/ d4 t# ~
' M8 n" V9 U( \* s$ phttp://www.52os.net/phpinfo.php?id=1 正常显示。& F. E6 [+ ~3 T( r
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
6 w# o$ J8 `+ {9 @) H7 z: Bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 F6 ]9 F  u8 ^( d8 ^说明sql注入和xss已经被过滤了
7 l7 U( B* S; K( S; v6 e8 Q% X5 ]- l
七、安装过程中排错9 N4 x! E/ o9 o4 q5 H/ T# G
3 J9 \8 T# z; V7 `! d5 G
1.缺少APXS会报错
/ @4 B; Y3 Y1 E$ `2 C; T( K% v/ H( l
. @2 }1 B  |% n3 U8 Econfigure: looking for Apache module support via DSO through APXS
4 u4 B: H/ U- q* J  d/ X6 Jconfigure: error: couldn't find APXS/ P9 X! {8 m* s7 f9 l% t+ \
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! x3 g* i( N( o) N
解决方法:) k% Y8 P( d6 Q5 l  K& Y( g) Y4 j
1 a4 ?, W+ W2 s& n
yum install httpd-devel' E3 y# V  o# }
2.没有pcre; z5 `7 L  N! U8 f* B

/ C% V6 J1 x$ p# Iconfigure: *** pcre library not found.
3 P% q9 S( j) `8 W5 c/ uconfigure: error: pcre library is required( |6 s7 B$ J1 ^# ^
解决方法:
' G4 H3 I! R4 q/ U" Z5 F4 t; g; R
yum install pcre pcre-devel4 Z( M9 D, o/ e; P/ \
3.没有libxml2- g7 J2 J* v+ Z' a' O
6 S) @; |& Q  S' I: E0 z/ j
4 b8 ?6 Z0 D$ N( f0 P
configure: *** xml library not found.
1 E  r, K) L1 h% r" m: P9 hconfigure: error: libxml2 is required) |* U4 d& V& b/ z% A! B
解决方法:/ q* i7 r7 \8 L0 i$ W

& o' u+ `9 f! j6 v( J& a4 a( p% Kyum install  libxml2 libxml2-devel- Q7 ]& `4 R) R
4.执行 /opt/tengine/sbin/nginx -m 时有警告
; x# e5 S1 D$ b- c" v! L! Y- ?4 Y* X2 j4 \+ v* l- \
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ V+ [9 r9 `$ W( [! p2 s) y1 y
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. F) B$ t1 A8 a- ^+ H原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 k! V- H) O; ]+ ~* {" N. U: d8 K, w! }- @5 Q; \5 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 J5 K/ A/ w( q4 Y* u0 c: x+ f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 t6 E8 a- P/ c+ f2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* h" G, H* K" }" b% h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" K7 k( O  |% ~2 q9 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 a9 f/ Y7 \+ j6 ~! [: l) N1 q( |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 t- H0 X' C5 `5 t0 r( o2 V/ E+ r
解决方法,移除低版本的APR (1.3.9)( V1 i/ V7 p) M1 J

. R- ]  G( `  {: l; N* Kyum remove apr! }4 `% T* u- J3 }
5.Error.log中有: Audit log: Failed to lock global mutex( W) U* A: I' q& Z5 J3 G

" i: [2 i- s5 N- p4 t' C& ?* ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ; @9 P$ y# ^* |1 l& C4 e
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! W0 o8 _, J+ U- g8 j3 z; e3 ^
解决方法:
; u5 [% R$ x" C, k# h# P! `- N编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 }4 |) A% m, P3 ?' Z2 o* \6 w% [0 A* a
SecAuditLogDirMode 07772 o" n5 W* ]7 U, L$ x* ^* N2 d
SecAuditLogFileMode 05505 O+ I/ a2 [9 u, U2 x, s; L5 I
SecAuditLogStorageDir /var/log/modsecurity5 @5 F: q. G' ~
SecAuditLogType Concurrent* q, l! D* I$ u* h
参考文章:1 c5 t; F: W* S" @& @. C" y% S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 b  @) N1 H( D; {# f! I
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-16 17:44 , Processed in 0.088951 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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