找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11316|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 D0 h  u& q: Q) x0 V& R: r. Y* x" m

7 @; a; ~% V, g* {3 N一.准备工作7 k* n; D- }) d/ B% T; W+ C

( v, a5 I7 Y: I, I1 P0 P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( D( M" R, N- _& u1 @
9 ?7 f/ c6 O+ h7 C% b/ j4 n3 o6 M: Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# |5 k) @8 A) ], o
4 c/ w* X9 @) d, y7 d3 }# bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& @, f. D  B3 X# ?5 |

  t# A. K* o3 U) X' {! HOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! N' g) J9 h3 b3 X; o3 P; V1 ^0 c
: p1 z+ a4 g6 m5 X依赖关系:
" K9 Y% T3 o, i3 _/ otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) L3 u* U- Q* {" Y0 t" B- z0 c0 L- |) a+ ~. D& e
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
& T# w! s% n: R, Amodsecurty依赖的包:pcre httpd-devel libxml2 apr
8 w) t) f* |) @0 g7 \' _, h: S* @3 r; I  K( }7 d4 J
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel$ K( z, A% W5 u4 f9 D" S  T
二.启用standalone模块并编译  w1 F$ k* ~) p. I/ `6 H3 H* ^

& J4 y) P  m/ I8 C8 O下载modsecurity for nginx 解压,进入解压后目录执行:
; ~9 ~  G/ s* F0 }( l, Q4 k/ U5 s) D& |
./autogen.sh& {4 `( Z4 g! B+ I/ i
./configure --enable-standalone-module --disable-mlogc
1 N" w. Q/ i/ lmake
2 ]% J* q+ X! [" M9 U三.nginx添加modsecurity模块
: Q# g" `1 w! b/ @* N. j
7 `! a7 H* G: c) l' b, H在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:4 g1 l1 ]; N: w! N! l/ u
/ {' z5 Y9 P/ b# N2 k; t' l& |' P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine4 T. i/ J( p; J3 F  o5 I
make && make install/ z* ?9 g9 X& X- O1 k$ @
四.添加规则
6 r* U# h& J: X4 G& [; M# S  y" X6 m/ `9 s( A7 o9 }
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 Z6 V4 h" u0 i- s/ w+ p4 n
5 Z) F  M) `$ I+ a; u1.下载OWASP规则:
# Q2 p7 q' Q* C9 C; `( ]" f% B: q9 }* a3 C2 a, S' t3 L# Z
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
, a! j7 R& g9 ~! o3 r4 X7 R2 N6 ]' T4 g; S4 Q9 m
mv owasp-modsecurity-crs /opt/tengine/conf/
. g$ C2 a" L; z0 E% U7 ~# M
5 W2 j" R% [0 v/ M' {" _; \* j2 vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( U. v% x1 E8 q& n2.启用OWASP规则:! {# x/ X3 p- o4 e# L

2 D: i) F. D) b+ c; F' T# i$ t复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。  ~" f8 x; L4 a

. H0 w7 X; ~* P) b- V编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 A: J5 `$ c6 W  ]6 {5 a. y* t* Z4 w

1 b9 j9 G$ ?' k6 ?) ]+ K  ]owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 X- I! I# T3 E" w& \8 C+ E% s( \8 U0 j3 g; W- ~0 I# Q  M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 _5 q0 J* x- }2 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 }8 \! [3 B; u/ S2 R; Y+ M! k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' b) z- `% k0 c# @) Z; y8 e- Q. g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf* d/ K- j3 I3 H# r8 P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 |9 _5 T0 b4 E' X+ K# W+ d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ t$ E. f: _: N4 ^0 m0 @0 \
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, [- [! ^, `9 M
五.配置nginx
" [, b7 l8 z2 G) J' |
. K6 r4 y1 X/ o4 c在需要启用modsecurity的主机的location下面加入下面两行即可:. B5 g6 v" r& E$ x% F
7 O  Z1 A0 i8 e$ H7 v  R; t# d1 }
ModSecurityEnabled on;  
6 W9 C/ Y9 t5 t& m# S) LModSecurityConfig modsecurity.conf;
" X' k; Z2 d2 ?2 ^0 A# I" s4 H5 Z: w下面是两个示例配置,php虚拟主机:
9 P1 c" ?) Y. Z' \  _5 r, Y6 B
9 `9 o; @  C7 U3 W9 o: cserver {
& ?( O1 f$ I* R2 Y; D      listen      80;7 d0 @  ]& e8 \+ N; I
      server_name 52os.net www.52os.net;9 `' n( p* a1 O
     
2 ~+ s) O+ W, X5 \      location ~ \.php$ {
# Z4 o) g( H/ J% N4 k+ T3 U      ModSecurityEnabled on;  
5 U6 u0 {3 t0 C/ S5 _      ModSecurityConfig modsecurity.conf;
1 Y+ I% }; h9 N
4 A5 C" R: j9 v5 t/ w/ ?/ C      root /web/wordpress;* j! c6 u8 h5 Y1 W- Z+ S
      index index.php index.html index.htm;
3 {1 l: [7 G, {  
% |' j" h5 j8 a$ ^- `/ g" Y+ c      fastcgi_pass   127.0.0.1:9000;
9 i4 s- R% J3 k; v      fastcgi_index  index.php;* D& v* y/ }, v% L4 S
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;' Q# W  t: V, s- y! U: V
      include        fastcgi_params;3 |% n( g& u" C5 S5 H
      }7 F& }; r) c5 ~3 X/ T( N) r
  }0 N. c9 m! m- O, O' r
upstream负载均衡:9 Z! j: P9 i- c2 F8 H/ v4 F

4 z$ j0 ~" p  g' E5 c- k! aupstream 52os.net {1 Q# E, A' d( `, s& m0 `
    server 192.168.1.100:8080;
+ B, S! d' L; k' y1 Y, X& K    server 192.168.1.101:8080 backup;5 o8 a' O  ~/ M
}% r+ g! o8 D2 }" Z

' t! U. t/ k+ k. @server {
( R3 ?/ N( ~6 H1 |) }3 |* }listen 80;
1 S' Y1 S2 R" ]$ l$ Z1 L" oserver_name 52os.net www.52os.net;6 K' [2 ?2 @# f- `; O

1 }! N& |6 X. {5 }location / {. V* y# P/ V- G! f9 ?5 S5 D1 n) l
    ModSecurityEnabled on;  
% B5 |1 j( z6 a& j$ A0 e    ModSecurityConfig modsecurity.conf;  - z# o# G/ I% z% m

3 ^8 W* S5 ?$ V        proxy_pass http://online;
% H3 L6 ~# t0 }; L        proxy_redirect         off;1 Z- ~2 N- ]1 W1 n5 P" Y
        proxy_set_header Host $host;9 E" o/ G3 C# G- R0 l* p
        proxy_set_header X-Real-IP $remote_addr;) Z2 @, S2 q8 z1 Y2 B$ |) j( w
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
& s9 n5 v: h6 v4 J  m2 t* v    }
2 ^. S. N5 {. O+ }3 j- G6 m}/ R2 P. S+ B  s& d% s, N, B
六.测试
* l7 X$ H* |, A7 s, _1 l2 ~7 t! @7 F' m1 \" F: X  k2 p2 K( B
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
8 ^; L3 k  C% n$ m6 W  @0 t$ y# m  G0 P4 s2 f/ r
<?php, z) J+ q; X/ s, e/ e& V: I: E
    phpinfo();    * m+ }9 ^2 b, j& l; |
?>
& ~' Q! d' L" w0 X9 R/ @1 G在浏览器中访问:) j6 H6 d, z+ m( N+ O- _
6 J! L5 T8 N, @2 u  ?
http://www.52os.net/phpinfo.php?id=1 正常显示。
( T& n1 i/ K- {0 X( S$ }2 F/ vhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。$ W6 b- d0 u6 a$ Z' K/ ?0 O0 R
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。( f0 {& [+ |. h$ f& [
说明sql注入和xss已经被过滤了  r9 s: t! L1 J/ u
5 P' O) V( w+ @  r2 X- D
七、安装过程中排错
4 ]) }. p0 n* T" H9 B' r! B" K; z/ [9 H7 K% V. E2 [. b
1.缺少APXS会报错
3 S7 H8 @9 B: v1 t% w7 c( A% j, m0 z" J4 D, j' n
configure: looking for Apache module support via DSO through APXS
/ G7 n/ w* Y) G, l5 oconfigure: error: couldn't find APXS+ R# K; z4 V# O+ J6 `( A) [5 s
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, S" Z. l8 g5 W: X5 Z' `, v* P解决方法:( K6 z# _$ f: ~

% ?+ n0 N7 D0 `3 }* U8 }yum install httpd-devel
. [8 k' A, k5 W/ c. c. g2.没有pcre8 p3 {' q) I) S( r

) ~9 g5 j( `9 G. ?% D( t1 _& g, dconfigure: *** pcre library not found.
8 y$ @% \: C# aconfigure: error: pcre library is required
* e0 L" Z) W" y解决方法:, I. g7 y# }" \, r. }  c5 M
4 i6 g; ~% ~: [# q5 q3 ?
yum install pcre pcre-devel
# G6 A. N, _0 _8 p* [9 h5 A3.没有libxml2
  u7 v) z: Z! a( D/ d
% t6 c; R+ {& B- [( K
, e3 y1 [+ R. u9 L7 kconfigure: *** xml library not found." a% `7 F' X' z
configure: error: libxml2 is required
1 z( S0 N( D& S/ W3 F  k" r解决方法:
6 x) G5 f. N* r  |6 |7 x" i; u: ]# ?
4 ^8 M) K5 z' _8 D, f" Iyum install  libxml2 libxml2-devel. W/ K& p: {1 Z) w# [) S6 d. ?5 Q& U6 b
4.执行 /opt/tengine/sbin/nginx -m 时有警告
. N# W& E7 i8 a/ e. y7 h
8 k; ?: f" v: xTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ j4 s) w9 b) H6 _1 Y& b  Wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ F: J" B- C% `3 }原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( c/ o/ i' A& `5 \* B1 |  @7 G2 i0 k* w2 j% l/ u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( s. ]5 Q& t+ E6 r7 c7 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
' \0 I) G# p% `4 z' D# _2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* d% b: X" r, I) U, @+ v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
  ]# ^/ }0 k. i+ ?. b+ H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) p! S, c  U3 v7 N% s2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ X8 [& ?8 j0 Z9 B/ f! l: a
解决方法,移除低版本的APR (1.3.9)
$ E- V* |: M  ~) x) }9 m0 l# b8 n% _9 W( c' p4 R+ d
yum remove apr
' t9 @' U. t4 X1 ^5.Error.log中有: Audit log: Failed to lock global mutex
0 L1 [# s3 s5 U( X* Q8 ]- Y4 q+ j1 V* A0 U, I' h
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
2 b# r' Z, ]$ P! z) ]global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! Q7 Y' g" p3 t0 \/ l0 q( H
解决方法:, [# X3 Y8 @5 M2 o/ w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 m; w# o/ P( V
6 C! P9 M4 ~, V" N+ ^SecAuditLogDirMode 07771 F! c" u3 e4 T/ o8 _, s3 s
SecAuditLogFileMode 05505 F: i$ A& @( v( U
SecAuditLogStorageDir /var/log/modsecurity
) \1 ?8 i+ z" V) y9 |2 PSecAuditLogType Concurrent
4 P+ Z& E! a, ?6 W参考文章:2 d; Y+ L$ z) `; L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
+ A5 u5 x, b5 \' ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-9 03:04 , Processed in 0.045148 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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