找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9202|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。+ A" I  E3 N) d0 j, T; |
/ x2 l  S& W/ u; P0 q# n2 [
一.准备工作
3 \7 \* A( `) j7 S
' H6 v' _% F7 b6 q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) q; ]$ N" B: r0 U; V" c( M/ Q$ j) f+ v# x6 k2 R' q% ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( r5 L5 Z" m# p# `

6 R& A. o! K4 jmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* l5 _4 ]. n9 j, @" i1 |; D- i0 a/ }% S- m
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' j0 E; D* W2 m$ X9 \8 h! Z" \
* h# o3 {- `8 Y
依赖关系:! ?. \9 f: T* M" H% g% S
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* u0 z& u1 n% B0 x. }
  `1 a3 Q* W* W( o' q% ]yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* D9 m& Q2 h% Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr5 `1 ]4 R  e8 t" P, O

! p4 ~: O% t, K) `- Y: U9 i: T  Jyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 [8 h' t& Z* J' m3 P: Y9 B二.启用standalone模块并编译
: P4 Y. P- S# T4 }. c
  r* Q: t: I/ {" g9 {下载modsecurity for nginx 解压,进入解压后目录执行:
9 D; a  Q. R  c8 o6 x" c, @- J  t, w& B: Y/ J3 r" w7 {
./autogen.sh
: N) d" u# r9 I: Q./configure --enable-standalone-module --disable-mlogc' Q, O) h2 \+ C* x- W7 _# t% C
make
% g, q  ]9 `2 v: s& D三.nginx添加modsecurity模块
" f$ ?" M4 r& W! ~. {! k4 E9 A9 ?* E0 C
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 i$ `' P7 o# b3 v: V7 a( G& s1 X7 ^( B# m4 n
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine9 X( p& Z7 @+ Y
make && make install; B% ?" r7 N; f2 d3 m( D' f9 {1 t
四.添加规则
$ S  c& W9 _8 m4 V/ e" r  W% j, X9 I( w7 |8 o7 f7 Q' g+ L3 a
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。0 s, L; o  w$ [8 N# a) m3 o1 z

$ v  u6 V9 W* E1.下载OWASP规则:8 U8 |, @0 |' m1 J4 M
: t9 Z9 D2 U* Z0 s- D8 o% X. P
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( i& ^# H: Y3 h; ?. s; R

2 ^0 `( z  w' v- nmv owasp-modsecurity-crs /opt/tengine/conf/
/ B/ I5 ^9 j3 `
) I( X5 k) H) k  _- F# B  mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* z% N% z# x1 {2 }' a
2.启用OWASP规则:
, U4 x4 ~6 i: m7 S7 _' ?) t! p( w5 P1 I$ y! h( X. \: Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 R& `% n0 }3 B! u( y# Z6 N/ i/ X4 c& }  u" h7 s
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. e5 X5 U8 m  u. Q; _- I$ z- s+ K1 }
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! ?- {( k) s7 ?& w$ i; v2 h+ u6 f
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ r: b) Y" u6 K3 s3 v) K* ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf5 o- \+ j+ b. N) e- E/ }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- D0 U) R2 N2 k2 d- \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 Z* B2 p0 |- z: E/ s3 LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 `. P8 X# q* m" zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf$ J" a* I# G& s7 \, O& C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 m; |0 o# D4 A8 j& t  v1 q. I
五.配置nginx
: z* [- w% W* p: N7 R/ u) A* y8 G, U  \' D
在需要启用modsecurity的主机的location下面加入下面两行即可:
6 x- X, K" y1 v, E& ?4 B; D! N9 w+ A5 ?0 u3 M/ X# P* B7 z
ModSecurityEnabled on;  
0 C" D% T/ `1 K0 k; z9 Z* s- wModSecurityConfig modsecurity.conf;
. y3 A3 l. X% A下面是两个示例配置,php虚拟主机:# o) r# C, ?' h' X$ s+ {
; H3 m; b' O6 ?. _
server {
* Z7 g7 V# p+ d      listen      80;- q  c  ?1 q  L5 `) |; ]9 r6 D
      server_name 52os.net www.52os.net;* G- Y, l! `- s4 b
     ( h, o" J. o( B
      location ~ \.php$ {
+ d9 h6 s/ J+ g1 k6 i      ModSecurityEnabled on;  
( r& k4 ~7 m# O: E9 G      ModSecurityConfig modsecurity.conf;' X- X2 ^: L  I- o$ P6 y0 S
+ w4 a" J4 V4 b/ a! O
      root /web/wordpress;9 G1 v3 z- w8 W5 F
      index index.php index.html index.htm;& U4 j) K6 K! m- ]/ P! O
  % E; H3 I+ E' r% q. U; v
      fastcgi_pass   127.0.0.1:9000;
+ T3 y9 I0 _& F; y      fastcgi_index  index.php;7 ~2 y" K" k* N; f. V
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  j9 m% u# F' ^+ X/ F6 n      include        fastcgi_params;$ q1 T5 L2 z: s* n% S7 ?
      }$ b) @# p7 r6 d  v$ z
  }# s: h. R7 G$ k( R
upstream负载均衡:  c7 q' G; o: Q6 O
1 u- |8 h2 @% C: F2 |- d: F# N
upstream 52os.net {
# f/ x5 n8 |5 e7 G    server 192.168.1.100:8080;4 B! i: b! k' E& N2 p0 r* z; b" h0 p
    server 192.168.1.101:8080 backup;' p1 A$ p/ }: I+ f
}  D# m9 a# ?2 o+ _$ u6 l

2 U# N/ ^) ~0 I6 jserver {
+ I- z, M" Y  W& }2 Z2 ylisten 80;
+ b' h0 k. P; |server_name 52os.net www.52os.net;
, p6 Y/ J2 T6 ]" d3 z; N5 y$ z  Y$ W
% F" f6 l4 C) a7 i, h2 r& elocation / {: z# [" P5 i: w1 L/ R' j
    ModSecurityEnabled on;  . \/ q$ T6 |& s4 D
    ModSecurityConfig modsecurity.conf;  
6 n" j( d4 V! e* @$ V; v0 t4 ~2 v! C
1 }1 y% y! g, n3 ?$ ~        proxy_pass http://online;
5 r. E+ X1 P) Y# _; o        proxy_redirect         off;
6 @- C0 z5 L+ U! j/ c* B7 A1 Q- U        proxy_set_header Host $host;1 A% U# C9 Q+ ?9 g" m+ g
        proxy_set_header X-Real-IP $remote_addr;) W0 N1 Y2 j" V, f7 q" P
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;) N0 Z8 w$ Z: w; D/ r
    }
# a/ t( W  L5 W9 y- A}# l: d* l9 j( u8 q, U% b* T& x6 E
六.测试
2 m7 }0 K6 B; {2 v6 T
+ k4 Z: C# I. q# y0 P5 j8 \我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; h3 y8 \/ F6 M* O; s

4 ?- t* u& I3 U; z5 t( I% H<?php
6 G9 W  M9 K, [+ p7 c) h    phpinfo();   
( K( r3 F, f' K$ G1 S9 e?>
8 S* U( A. M9 J' i4 Y' w0 {在浏览器中访问:
( ~2 a8 T" g) S
9 o+ m) m% u3 F! yhttp://www.52os.net/phpinfo.php?id=1 正常显示。8 Y- f7 d0 \" _+ f
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。, L$ X9 B8 J& X, n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 a4 h3 {; c* B7 i
说明sql注入和xss已经被过滤了  l$ ~& ~; [/ `% l( C
- a% s$ ~' \1 J% k5 d! M5 A
七、安装过程中排错. V( v3 [7 i1 e' h# w$ i

& ]; V4 k+ u) g; y- A1.缺少APXS会报错0 o" d& Z: ]3 T6 u
2 h# x* R5 F7 Y! a$ A
configure: looking for Apache module support via DSO through APXS- t/ t/ a: _! _: `* x" Z1 v" @
configure: error: couldn't find APXS
; S8 U; {& `8 R$ _1 Oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( |/ t! F; I* P( ^- R9 T) E' `5 {
解决方法:
  A# p4 l9 X$ Y# I/ b9 j
. c" b) G% Q* j4 k, S5 j' S2 q  G/ O! Jyum install httpd-devel
% f; f5 C2 S  V. c7 u/ C& }6 ~2.没有pcre
# L6 L8 X. F9 N" T$ z* T- ^1 w& z/ f5 F6 q; d1 Q0 S
configure: *** pcre library not found.
) ?; r9 J: W! ?( Rconfigure: error: pcre library is required
$ E  L# w7 L/ |0 f解决方法:) x5 K# U6 a3 v5 u9 F( a" g; W

  }4 |$ {' x0 e" D2 ?1 {6 c8 m6 R6 J  |yum install pcre pcre-devel
& |9 X/ H+ T+ M, C  c/ `3 m3.没有libxml23 `' V$ C$ D* w+ I3 P; f
3 N% }- q! `5 W  I& T& ~$ c" V3 r0 z) d

2 b$ u- ~5 B7 f2 pconfigure: *** xml library not found.
4 O( K( C& y8 J6 Q/ s7 b9 qconfigure: error: libxml2 is required2 i& m6 o( L. u; @! e$ _
解决方法:
. R9 e+ }# A% w! I8 D( l, I; J9 @% n6 h: u3 e# z) h+ A
yum install  libxml2 libxml2-devel
& G  P* l' T( u+ A2 @( Z4.执行 /opt/tengine/sbin/nginx -m 时有警告# j+ E2 e% a: p$ W
" Q1 k7 S6 \8 L/ }
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% a3 O) c7 F5 w% R5 R4 Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' A" u6 w( V: {. z5 W. d6 a3 m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 C  ^1 V+ ~" D1 U0 O9 r% w% L5 k7 ~, X" J7 K4 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 Q* `! d; o+ T0 V0 w; W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"7 g( i0 ^7 c/ K, Z4 \; `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- v0 x7 [( S6 I! L! }& P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- p9 Q7 Q" i1 t( e7 K5 G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; }/ P; q, l; x* X. n; X% }
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 A9 R: R6 B! ~, X8 h9 T' B8 a解决方法,移除低版本的APR (1.3.9)
7 x" H) E5 V3 I
5 ^9 g9 G' R4 b8 yyum remove apr1 E4 s+ {7 g' _8 ~$ }1 y- k* }/ o+ E
5.Error.log中有: Audit log: Failed to lock global mutex  g. s1 A2 F, Z% Z5 J( M
8 p# L0 k# Y& P4 a7 a- W& S/ w
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     0 m) Q  N9 S4 w* j
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- J, [3 ^$ U8 ]
解决方法:
+ x% U' l0 L1 f1 D' m: p编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 r/ \% ~' c/ h7 k' }
7 `+ a, u. z! e1 q, F# |SecAuditLogDirMode 0777
  v! m% x/ Q  z* d4 USecAuditLogFileMode 05502 X/ n, Z" [6 K' F4 j6 z
SecAuditLogStorageDir /var/log/modsecurity: }6 o0 L; a( ?6 y$ S
SecAuditLogType Concurrent' ~# {% {& c0 _
参考文章:
7 {6 ?0 Z; X+ nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& @. C  `* ]8 Z3 Phttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-25 07:12 , Processed in 0.101460 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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