找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12570|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  S3 ^) Y  l8 x# s
- W/ M9 y' t1 s" \2 Q$ K: }% G+ L) V一.准备工作) n& a4 M( E# I& z

% @; P$ ~- J7 ~4 f7 \系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 j" @. s$ Z5 j! L

& A$ R0 S0 a# A; P' rtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* O* a6 l5 z: Y+ J1 ^3 r/ c! x' a8 V% q3 ~0 ]7 Y
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 N' l* G7 X# {3 s1 u/ x) s1 |

1 `% Q1 w8 L* ]: X- @. ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' z; ?' @# Z3 p6 A8 m  N& ?. q! Q+ f8 q, F% q' `$ Q% H" U5 k/ Z2 e
依赖关系:+ O4 \( I+ v; p! a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  f( j* `  M+ Q' C  U& d

% g' ]4 _- O7 w6 L) [yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( B" T7 ~( W- C/ nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. y/ A: h- E+ O
0 L( K, G) Z- s( \6 e' X/ E& Myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel# {1 A8 D2 |' z( ?! ~6 Y
二.启用standalone模块并编译
& P8 i& U& z: ]* d3 _: _( L3 V' h
5 I- S7 \! `8 g. p' W; q, \  \下载modsecurity for nginx 解压,进入解压后目录执行:. y; R6 L% G' m5 H, H
6 v- z+ H3 G0 z& ~0 l- g
./autogen.sh
' }+ O- n7 k1 P" q./configure --enable-standalone-module --disable-mlogc* K+ E0 D" Q/ H$ ^/ s4 `+ Q
make
7 e" S; J8 o" m# z4 H' s- N三.nginx添加modsecurity模块' A5 S5 e  z/ B: v; d

# `, }) b* [( U, K8 T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( t; ?; A' q, e

9 p: d+ Q+ X' w7 W& V. V* \./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& e0 C: z# R. c) k9 H& Jmake && make install" e% G* V0 j/ z
四.添加规则
/ @' d6 r: y: r' O. h/ K0 f  ?; l; F; B; [
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; i. U$ r: n. y* }/ y) A3 L  K

* \0 I3 k' ~6 \+ Z% s6 g# l: X- |! d- Q1.下载OWASP规则:  {: a) ?% p2 T1 X, N* }
: o" X8 m; F* l. D- j8 B  w
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 E$ v" |- P5 Q5 |  B' y. w! j; e) h( O' h+ \
mv owasp-modsecurity-crs /opt/tengine/conf/" O4 C% E1 N: j& k; @( o# H: A

) `; [$ u1 U# f# }. ^cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* {2 a* w0 \* e) H  D  }2 C3 {
2.启用OWASP规则:  R! l. [5 s: O. V5 P8 y7 j4 U+ e
$ [) u8 W5 X' }7 |4 c$ e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 e5 v$ q" c, |& ~! K) d) v9 `

+ @+ [% u3 `1 D- P9 e' |$ L编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- A% g5 B3 l" |! D7 ?* g( N: x- \
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。# ?9 a- V3 F' P4 ^

" S7 Y7 w8 k/ h# |1 L" i% W( R8 kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; f7 z7 [! j3 \1 j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; M2 p4 F) s- K) _- QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- G5 x/ t) _( [  R1 _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% ~, U/ ^0 P$ ~: W4 G! s5 D4 l- dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 g+ v% S; W9 R. WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 ^# O) W4 q0 s% n' H3 WInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 Y3 U5 R% \! ^$ T五.配置nginx- r, B9 y, Y/ Z) a
0 ]8 T1 |* P$ ]) {: G
在需要启用modsecurity的主机的location下面加入下面两行即可:
, J! _/ o6 N- n2 w7 v( B* U  v2 Z" I7 K: Y
ModSecurityEnabled on;  % ?; M. L- c1 F/ L  s$ u9 g+ u6 |
ModSecurityConfig modsecurity.conf;* U4 U: o9 c2 Y0 L$ O
下面是两个示例配置,php虚拟主机:" C7 i1 }0 g+ `1 n3 c: b

5 I# f# T9 ?/ N4 k8 r, Wserver {" p) M! X( ~0 p3 J& C9 j
      listen      80;
( z* y) `. H  |% B! t9 ?      server_name 52os.net www.52os.net;
4 p/ i3 u( X$ Z     5 F' m8 D3 U1 i2 C" e1 k
      location ~ \.php$ {0 ?# H. W  t' X' s5 J
      ModSecurityEnabled on;  " C) r7 ^# a6 G2 b/ S+ h
      ModSecurityConfig modsecurity.conf;0 l% [% _$ B2 C  n: g/ z

; u* o* ]" @2 O6 ^) _) ], ]$ n- C      root /web/wordpress;
1 V8 ?1 i- d0 M0 `7 O. h      index index.php index.html index.htm;
" W3 g& n- A% _& l( Y1 ?  
) m' |  R/ G( \! B- c6 h5 B      fastcgi_pass   127.0.0.1:9000;. J( y, o; I4 w4 E1 }$ r& s% E
      fastcgi_index  index.php;
" p& b) U! Y' n/ v3 k0 `% t8 B      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 ?% B. M8 |1 r3 e; {) |0 V! g3 E
      include        fastcgi_params;
% m# X3 O* k$ [& k% c; j      }
0 \) l3 ]9 f: v: F' J  }  y- X5 ~$ a' d; S; \
upstream负载均衡:4 p1 ?- h# @1 V# n" [

0 @9 t) y6 J; W" A" e2 r! H. vupstream 52os.net {5 o: `  O& B/ ?$ P; J9 h  \& V0 N
    server 192.168.1.100:8080;
7 U4 J7 D- t. H6 g$ w    server 192.168.1.101:8080 backup;, N! E1 O( R3 A& ~; L) [4 Y
}
' q6 F" [& L* |! j+ {4 Q
5 g$ J+ O9 r# ]  I, w4 _server {0 l) E4 _- H  v1 q$ o3 K+ s; r0 {3 z
listen 80;
' B! X8 M" V9 n/ [$ G$ f! W, mserver_name 52os.net www.52os.net;# X4 W' k1 c- A5 w' c
, D( |: t8 B6 {. Z3 ]* R
location / {
* q( h8 J5 D+ q/ n6 J/ f' p    ModSecurityEnabled on;  
& i/ v0 N. M) O* Z! q- J    ModSecurityConfig modsecurity.conf;  + X- Q, _& d8 l+ B1 n& W' F
# ~  b0 v% j3 ^% }
        proxy_pass http://online;- M( K7 i3 [/ N/ t9 ^4 A0 h9 b
        proxy_redirect         off;* [+ o0 \' P# s% @* ~% c
        proxy_set_header Host $host;
( ?8 O8 {' I9 G1 l+ V1 b& p        proxy_set_header X-Real-IP $remote_addr;
. |3 R& J3 a9 z( S) S! F        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: Y! J9 A5 D$ b) F  l" Y
    }
- V- N+ E( j6 f5 u9 s5 J( ^! V}5 I! k  {1 @! w; |* r
六.测试1 Y+ l8 p( e2 s/ T
; D9 ~7 z) x! J* A. V8 v
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
2 O6 m& x, Y. n  q4 V. n, O0 }+ p
  J, u5 |9 R% p- K  t# s$ S. X) J4 Y2 f<?php* |' b& {2 P4 Y0 g7 a, A: y! t
    phpinfo();   
& R1 A8 e% H* ?/ V' q4 H1 b?>) M2 C" q" W+ U! k
在浏览器中访问:
' t; c! x3 K+ P7 s; g
  C& C' l4 ?" i' k: x* ~6 thttp://www.52os.net/phpinfo.php?id=1 正常显示。
; ~7 q' w  b# Y! Vhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- W  }  U+ Y6 I
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  V0 a5 {/ t/ P- m
说明sql注入和xss已经被过滤了
5 V' R* c4 }2 |, }% Z$ ~! F+ R
' r8 R6 E/ [7 f; W' G" T6 ^3 G0 b/ J七、安装过程中排错
8 O& q8 H( ~1 N8 n) x$ _6 G% R, k$ Z6 d: h! z
1.缺少APXS会报错
5 J! B% }: t+ {  I+ x3 n  J, W' z9 l( B
configure: looking for Apache module support via DSO through APXS( o. @# m& \/ E$ V' c5 D, A
configure: error: couldn't find APXS, h: N, D& u- a4 J
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. D: ~- E! H7 v: W% _* ~
解决方法:+ x( }/ g9 C/ l) c6 I" |4 S! d2 L
$ P. c% Y# ^3 I. Y$ j/ X9 k; z
yum install httpd-devel7 w; P7 E4 x0 A" T
2.没有pcre* l8 `& \3 b7 K! a* p# H

- R* u6 v9 S7 h  k* E1 ~configure: *** pcre library not found.
* q/ V" r$ Z! l- p9 qconfigure: error: pcre library is required4 L. H9 T2 S& g) c
解决方法:  s- X' h! W4 P3 _) b

& g0 G  M+ B. ~yum install pcre pcre-devel
; k" _- Q  M5 f7 L0 y2 G! w3.没有libxml29 ^- u, e; f( Z7 B( n* w0 k

# A* I+ q4 G/ A! K1 c+ `
# K. n3 h7 }: I% `* I- d7 r" f7 ?! iconfigure: *** xml library not found.( m, y5 |: }& q0 h! ?3 D  L
configure: error: libxml2 is required! [/ h  W5 J# t& ]
解决方法:. o9 O, E6 T; ?3 d9 ~

4 S' Q6 ]( S& n8 {* @# |4 vyum install  libxml2 libxml2-devel4 Y) V& Z- u. L: d4 h6 g
4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 ^! M9 [7 F) Z# m7 e  T. w  m
) l. H! U" p; p4 r3 d0 s1 jTengine version: Tengine/2.1.0 (nginx/1.6.2)6 t* H& K$ U% {% `6 Y6 o
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!& y2 p6 j) R) [1 X. h7 ]+ P
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( V8 C6 m8 ?2 ]1 j5 b3 U7 ?0 P: x- b. {$ U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ u% t9 W, F) }. f( B$ B5 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"2 C" P- @' P" V; A
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! }9 [% b# N' s  T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ X7 q! T: g% O# |- ?( A0 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 e; I4 t6 ]& d6 n
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* \' o9 X4 Y  K, K: t, m解决方法,移除低版本的APR (1.3.9)
( ~! q) ]/ L( z0 l7 n7 u0 p% x1 n/ V- v: V% R; H* ^  d4 x0 B% K
yum remove apr
( {0 ^; d' ~3 x( D3 Q  B. `! h5.Error.log中有: Audit log: Failed to lock global mutex  A; p! U% X2 z" w# R

( Z# o8 `& r4 m# L4 Y1 a" n  y2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
7 X; L; g. [# d3 \5 x1 s: cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% m* W; Q$ J6 \) X' U0 ~/ h
解决方法:2 m' H& G* r1 I- {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& n6 K; k! L7 V: n

* Z3 J; s" t9 ^9 C/ c' b0 o5 {1 ?/ }SecAuditLogDirMode 0777# u. @: n8 [& P0 C& I1 K
SecAuditLogFileMode 0550
8 H7 C" V" g# [1 q8 t! vSecAuditLogStorageDir /var/log/modsecurity4 d9 s( y) G$ O1 h0 n8 v
SecAuditLogType Concurrent0 G) w; X& O: }) i5 Z
参考文章:$ ^( m9 T$ A: l
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. S+ Z0 K# ^  E1 J1 K
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-18 10:23 , Processed in 0.102177 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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