找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13352|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。* Z- d& B' f2 r$ o, S' _
5 E& y6 o' e7 G" _; n/ q. y3 c; \3 L  i
一.准备工作
9 ]# l% J' o+ K& V: @6 [% `( l( P# S- Z) h' g6 G
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. r! \! [  j' G
# n. C3 B' a1 K* v+ M
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" A- R; L* O- d+ e& w3 Z
$ w& ^( S$ C) E9 r% @+ gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 \& ^8 c4 k. L( @0 G2 Z
/ Q2 G7 T+ N0 L+ J; s  vOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" g) G3 ^- K* u- B- H" Y4 R
. M$ g! n& A1 n$ q# K0 W+ T
依赖关系:
7 ~/ H1 m- [' Y+ D# @: Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 b6 B% n3 p" E
) E9 N* ]/ C* ?+ p2 Iyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, h; q$ s# T: ?8 |. O/ {
modsecurty依赖的包:pcre httpd-devel libxml2 apr
0 @% a" G$ J# t- P
7 }# l9 b; N  d* O2 eyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel1 k4 M4 {  \- S- a7 t. K1 d
二.启用standalone模块并编译7 M' K/ F9 Z, U$ Q+ U! l, `
+ \& E; e8 C' g) x/ E/ C
下载modsecurity for nginx 解压,进入解压后目录执行:
' Z$ S( e3 n* G  A
) ?$ t7 Q5 Q( e; p+ B./autogen.sh" L; g' Z# L% X! w5 Q" k2 R6 a
./configure --enable-standalone-module --disable-mlogc
8 t# y4 y3 F% v; ?3 Q4 Zmake
0 Z, v! n# i# F9 D4 ]: r) i6 w三.nginx添加modsecurity模块$ U1 T1 U. O# \- I6 {

4 c* ~$ L/ Z- s. e在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' b! t: F( Q2 p! S6 X8 P2 G! V( ^5 h, M& n, l
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* {7 C; R. o( e1 S" j. \
make && make install5 D+ A+ {1 G' z) l7 r' }
四.添加规则
: t; V5 H' g) G7 J& `
7 L9 P( _9 S: w/ ~5 V: @modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 d$ X+ P( H% F5 g. v4 a& g
$ K9 v' k; {8 h; M7 O2 C: u1 P( u1.下载OWASP规则:
/ D- C6 ^3 q  ^- R2 y! q' j9 T, _, h/ [3 Q/ U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# g$ D% Z6 I4 a# p6 w' I

/ R0 ]- u' A* M( N8 }mv owasp-modsecurity-crs /opt/tengine/conf/5 F; \% V  w. Y6 u! w

9 h! H9 R" T4 A- V0 [cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: C' D) |7 o( O* t9 X' `2.启用OWASP规则:
! V, y) d7 j+ {  E, ]" ]+ m8 w" s; z6 o0 i* X
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 b/ C( c+ z0 V3 H2 \, r
. T7 ^1 `0 r% K编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 S1 \0 g: I2 a+ Z2 M
4 k7 v/ N& B2 u" E9 Kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 k$ V8 J/ s1 m
# j$ C3 w. l* G4 s2 H3 a- O% OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; W. C" e; }! y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 g  S0 E6 u: ^3 I! E$ B& G( [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 D* b- J) Y* A( ^5 DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ f* c- }* F9 e% D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ ^3 `4 x7 D& e; d) A! z- @( @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 R& I  L6 l7 S4 tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf: L$ m6 m% [4 C, A/ F$ S
五.配置nginx$ m1 V/ G" o' v, N

( K. Y6 M; r- H/ P/ S在需要启用modsecurity的主机的location下面加入下面两行即可:- o$ g3 d0 I7 O# c& P- u( C2 T
+ ~& y5 f) k! v  V) w6 m
ModSecurityEnabled on;  
* A6 L9 C+ s2 V/ n/ WModSecurityConfig modsecurity.conf;& J) W( e1 x' p* d7 p# I3 @7 G0 Q# A
下面是两个示例配置,php虚拟主机:
- a* ^: K3 v6 C- v6 |0 z, y/ _
8 F& q# T3 c( E; A* D# D% ], mserver {
7 Z% I/ s$ C* N$ i$ Y& f) ?0 b      listen      80;" d4 M3 _/ n9 m3 L) H
      server_name 52os.net www.52os.net;/ z- Q$ Y" ~: y  M# b. F5 Y: |/ @
     5 h* R' _: K5 _* \$ x
      location ~ \.php$ {
' ]+ D+ {- A$ a' l      ModSecurityEnabled on;  0 Y) H/ s0 y- K) V
      ModSecurityConfig modsecurity.conf;5 s$ }$ _( @* e9 l
  C$ L1 _4 }8 B/ {8 k6 f
      root /web/wordpress;* ~" S4 ?/ D/ x8 @$ i
      index index.php index.html index.htm;
" f* q9 s# b# @# ~  
5 d& v0 h+ p4 C6 v      fastcgi_pass   127.0.0.1:9000;. _. r" E& }. Q  W' N
      fastcgi_index  index.php;
3 u2 Q% |7 x: T# G. _      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
$ m( [( d5 b! A1 j4 A9 `* F      include        fastcgi_params;9 I1 ]4 F6 b3 m( y
      }4 ~0 L6 K; G, @6 G0 L" {  m" A' W
  }& x! \* J- g( X) N- G0 J' K
upstream负载均衡:* Y* _: Y4 K9 B

' p% G$ G9 F5 ?( ]) z- d! R( Fupstream 52os.net {
8 ]0 L) \: V- ?+ h, S% f    server 192.168.1.100:8080;2 L% P* ~0 ~0 T9 ]7 b6 d. t0 D
    server 192.168.1.101:8080 backup;+ w5 r7 Y7 x. a) S
}; f0 Y+ E4 {" u4 R8 i* J  z8 p. N8 P

- H; x+ t9 g* w, s# M+ xserver {3 b5 A& t. L- I0 f
listen 80;
2 P$ }; w" w, K7 P' Q+ x$ }5 zserver_name 52os.net www.52os.net;
) t% }+ S3 P! o0 `  R4 E
! b6 _$ f5 p# [& V5 X( ?( b! Xlocation / {
, Y6 [# o3 H. V3 R1 {1 B2 q1 c    ModSecurityEnabled on;  # m, W" D* |% _* O- H( @
    ModSecurityConfig modsecurity.conf;  
3 Z$ S5 U1 Y& F5 G- S8 }
) L, V" H5 f. N; I        proxy_pass http://online;
, D, }# j% @1 H! x2 a8 K/ h        proxy_redirect         off;
) O0 J9 W; G; D/ t" m" J- e, W0 {5 @        proxy_set_header Host $host;
+ j2 S* h) N  U  Y        proxy_set_header X-Real-IP $remote_addr;
1 R% q& @  r9 ]3 z2 Z% b        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 G" u& j" Z4 W, i: _/ Y) S    }
/ N+ Q1 W3 k1 ], t  X' _0 G}
2 g: z! N# ]" f4 w" t0 e六.测试
0 n$ B4 k1 l! ?  P6 K. k& ?  F  p' W+ v5 @7 B
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 H* A- P4 f8 E

& m. D2 v6 R% f, {<?php
6 E- _: z5 z) }' @8 i# [  w    phpinfo();   
9 R2 P% v) d; G* A; d?>
" W# e# D3 b! C$ t2 V4 G6 F- ]在浏览器中访问:6 O6 ~) K2 S+ Y6 X

8 d6 w  I( X! _http://www.52os.net/phpinfo.php?id=1 正常显示。1 P1 [5 k' x" H) _& v% I
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。/ H; b! w8 x/ i
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 X$ g" Q# [& k
说明sql注入和xss已经被过滤了- X/ m8 N3 Q3 H9 M3 x; V; W  C
3 O8 [2 W' R- E. p: x& y; y* U
七、安装过程中排错0 X* l9 j+ [* `. G7 m4 w$ A9 p; L
' r8 X6 l" x' D& d* E
1.缺少APXS会报错
8 S3 g# n, P0 _1 O# R; ^
, F& }3 |8 L& r: zconfigure: looking for Apache module support via DSO through APXS
2 ?' \) @7 F  pconfigure: error: couldn't find APXS
6 O8 D' [# {; k( \1 W5 Tapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& }$ s4 b2 @0 O
解决方法:
/ ^( ?" b9 J; M
' M" }$ e% s& O3 ]  M- d3 Kyum install httpd-devel
# e! H  o/ G0 ~( ?2.没有pcre2 n6 ]. ~! _; W" K
, D3 x* l/ P6 `. E) b& Q/ E4 z
configure: *** pcre library not found.
1 K1 L: n1 S; q# B; N8 J/ m2 iconfigure: error: pcre library is required( E2 [- o6 r7 l
解决方法:9 u7 n) c% h$ B$ L$ |
$ j5 |# A% ~, [/ c4 c2 C
yum install pcre pcre-devel
8 B! K  {7 Z6 f4 I$ ~1 W% b3.没有libxml2% G( ^( a4 |7 ?) R& {+ z# z
1 {- p0 f3 ^: ?
2 L/ D: E7 q8 [5 h( Y
configure: *** xml library not found.
+ k" d4 ]3 k3 d# h+ o9 gconfigure: error: libxml2 is required( I+ I7 G4 z8 o
解决方法:+ x1 j  d' N0 U; ^5 f
3 e' @, q) P! Q( h" A) K8 a0 N' [
yum install  libxml2 libxml2-devel
0 T0 T9 c; s, s. H! w6 I8 H6 {4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 e+ `! \6 K4 X7 W- v4 Y: @. w5 E1 X% `
Tengine version: Tengine/2.1.0 (nginx/1.6.2): p; C# d" H5 o
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 w5 ?, C% Z" T8 W1 }
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. ]2 s9 @0 h: W9 s

+ z0 [# }" ~# h( \% a# m( M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% ^- F; H0 X- y0 w0 _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"2 @: u  Z8 j6 Z# l! o# ]8 q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ B: h0 g+ O" W. e7 H& q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
& j7 P7 t% j: w7 c/ I. ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 y! r7 K& G% e9 |( n" M2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% p4 I1 C. q( ~6 |解决方法,移除低版本的APR (1.3.9)5 {' E# T% I  `. z5 e" E* Z4 ~( Q3 w

, c5 N. @: m- {yum remove apr
1 O( L3 m& W$ O3 H' ^0 d& \5.Error.log中有: Audit log: Failed to lock global mutex) m; C# t$ k9 Y" U0 Y
6 Y4 h% m' u& H. Z4 `1 o* a: n; V: c
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
2 P3 {- R# T9 N; dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 `8 }  G. d0 d$ W7 c9 X" ~9 G2 @
解决方法:
7 T/ L3 Z" K  g8 r编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 ]! I) k, L- A0 D
! Q( S% R4 S, G4 BSecAuditLogDirMode 0777
- S/ Y7 k$ S. n+ W' T4 C( L1 KSecAuditLogFileMode 0550( J0 z+ J$ v: u, h7 M
SecAuditLogStorageDir /var/log/modsecurity
* v; g' Q& w& Y" I0 {6 ]SecAuditLogType Concurrent
1 q3 R- E  ~2 @, H3 U参考文章:
( b' ?2 I8 h. o2 r/ o0 h! }https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 T+ x2 P' `2 {; \5 X5 ?
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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