找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12960|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- V  H- }$ j, _) p
, F2 z! {7 r* w$ ~
一.准备工作
- B+ `) f& p) H  V, |' e8 [: y2 _) |( W4 ~( `. B9 {' X
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- Q4 \. T* A+ E' [1 o
8 |/ j: {# R9 N% z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& H* j# N  L# _
9 c5 ]' ~( R6 f' e" S1 wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ {! b2 L5 B- e1 t  F
9 j0 p' _3 x7 H8 i8 r$ q. V+ g1 i) {OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& ~8 Z& |6 {% B2 e4 J. ^& y( d2 h4 g# I: T$ f1 u2 R
依赖关系:) E, ]: o9 z5 c* L+ z& j7 M/ V
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:# Y' b# l9 z6 H7 n
. G! C4 a9 I8 ~
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( @! y# G- x" W4 ~# A/ Q+ omodsecurty依赖的包:pcre httpd-devel libxml2 apr
! t" Q  o  r: R7 a' U7 ^+ g# v- e' D
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
; X, {3 x3 n+ l! a0 l4 H0 B二.启用standalone模块并编译; ], ~! X- d: u) A1 q" F, m( b
' \( J1 M: \+ z2 l( D/ D
下载modsecurity for nginx 解压,进入解压后目录执行:5 w' c) m+ o2 v' q3 U+ G
/ k' p& C7 v; j/ [" y2 ~- U2 Q
./autogen.sh
4 f) ]" e0 [- C5 _% k5 Z' w' [5 f( M./configure --enable-standalone-module --disable-mlogc! y& k* j5 q, ^* R# j/ U7 R
make ) ?4 s2 u# P* U+ O
三.nginx添加modsecurity模块4 o7 O5 l4 x6 o2 C% f, |4 M: Z
0 o1 u. P: J& ?& Z' @6 h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ e. h5 T. H( N' X* ^
- `. T/ V* s5 n- Y# z5 ~/ Q" G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  e0 ~7 [, r3 r7 D# R& l
make && make install1 ]/ s8 j4 k! }! K( h
四.添加规则8 ~: v8 D/ u- v7 z0 U0 C9 G

6 P& G3 T9 x; w5 i7 e8 G* `" }modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 G' Q+ u. Q7 K1 @
7 H# x9 {, b( `- b0 M& G( a% l+ I
1.下载OWASP规则:
( L! j; }4 L6 a- M
& @% L) M! V9 c4 j! Egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 u' g4 s! P4 @) V. |- K6 F& q. Q
5 n8 \1 d6 _7 B! G* t8 ]: p" e% E
mv owasp-modsecurity-crs /opt/tengine/conf/5 `$ m& R/ ?# ?, O% v8 B- z

  b( ~' z* K7 Q& I9 x( V5 \cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf& W! `; M" V" m
2.启用OWASP规则:
0 W. `* {& o. V
/ d9 f6 ~: ^7 A' ~) x! W复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。  `  j# X  d7 S/ E  y

# N6 r* B1 t$ i( ?/ X8 Y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on; x0 _& p0 b" E0 {; w( [
8 f/ u- s9 f1 ]# I) f0 w% r$ }" C7 B: V
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- t: M0 {3 r( j# _: C; E$ j

" w% i2 e( h" J0 WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
  _3 ]5 S+ a1 yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( [4 U! _! N  W5 z  E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( u+ ~, N% d* iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf1 P& m% X9 t1 {9 @) v! L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- F0 d+ E6 {' d! l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# `! k9 D: c: O3 E' C2 s/ `' c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' R: Z( o7 B7 s6 x) S- \& N/ b9 b五.配置nginx( |+ o0 ^, V' t7 `$ F! u: |8 R

2 z4 Q8 r/ S, A4 N" i9 t. t! E/ I在需要启用modsecurity的主机的location下面加入下面两行即可:3 c& y; A7 X: ]( p& o3 x4 A5 m6 `
5 d3 s- C+ K2 O# _" I  [! J
ModSecurityEnabled on;  ! M/ A' F* G0 M% R# n. R( m
ModSecurityConfig modsecurity.conf;; q: L. g6 k+ A2 a* U
下面是两个示例配置,php虚拟主机:+ ]! x3 x- o' C, `/ E. G/ Y

1 h6 t  D# F1 \& Aserver {
  ?* V4 |8 }& o  j      listen      80;2 c3 x( Y0 `0 ?: _- A0 @; t8 T
      server_name 52os.net www.52os.net;. M3 F1 r# p! u( S2 N" B
     % ]. `1 _) R0 w+ f1 k
      location ~ \.php$ {
# r! a+ i- S' K3 N. i8 [, a8 _, X      ModSecurityEnabled on;  ) c# e  C6 L9 w1 J' T, c$ m6 \
      ModSecurityConfig modsecurity.conf;
6 j% X: X! w- y4 ^" E& I6 e6 L" o# G+ U: H5 g& v( k: W
      root /web/wordpress;
6 ^& T( r. w; t: u. ?( q$ i$ ^1 [      index index.php index.html index.htm;2 n- S1 x( r" N1 p$ }
  8 k; `5 h5 J# C# [' ]* f9 |
      fastcgi_pass   127.0.0.1:9000;* B% `0 I. s- \& _) o5 M4 J* T. B
      fastcgi_index  index.php;9 C( M! u& w0 F' Y: C
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
$ e' d$ j5 v! u7 |& N2 ]      include        fastcgi_params;: {5 M1 F( ^0 Y& s
      }
% k$ n' B) c. V5 U  }
3 `& X+ U* k% M# h5 Oupstream负载均衡:: }8 t2 S! S$ f; t# W9 R

2 X5 D/ M) t3 A( n0 C5 a/ Aupstream 52os.net {
7 t) O$ O0 Y% q4 d+ ^# V    server 192.168.1.100:8080;
: ~+ i, f( Y1 _7 |4 G+ g9 C    server 192.168.1.101:8080 backup;
3 l1 ]% w7 x. O$ ?4 Z9 v}
. r2 p' M: V9 M/ X/ k# `  j) y
* P" e1 c: V  s7 I/ B3 @) y0 Xserver {" h, i7 `' z6 ~/ p6 t2 ?
listen 80;& S$ F2 D  R& P" j" T  D
server_name 52os.net www.52os.net;. p0 q8 [0 `5 a! ?4 [; A0 S
7 a6 [- b$ g& \9 V  x
location / {
& W$ A7 H# y8 p# |2 K    ModSecurityEnabled on;  # P4 z, N: a  G3 I5 Z) j7 Q$ ?
    ModSecurityConfig modsecurity.conf;  
8 N8 N; u4 W% d: D( @+ ~- t* u% [! ?2 M( M2 m! s+ \2 H1 y7 Z
        proxy_pass http://online;
9 O' I9 u2 X. G. t. q1 D        proxy_redirect         off;
+ _' R+ I3 k+ k8 T, X        proxy_set_header Host $host;
0 g1 I/ e/ ]+ K6 m# x8 W9 d        proxy_set_header X-Real-IP $remote_addr;
! `# d# ~2 S' ?$ l: f7 j        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  a) m% q/ P5 ^- |
    }
' S- a3 i! r( H; x) ]6 C& U}
; J  j5 ]9 f/ v& t) A六.测试: n7 p) K5 r& h$ M- q
* v  D, g/ N) l. m1 A3 @
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ X1 k: @" S/ ?% L1 {+ e
6 g( w1 Z+ f( G" h<?php
" Q) `( @" x4 z    phpinfo();    ; [: V3 B9 N3 S* K6 O8 q3 h& ]) v
?>
/ [" D/ B; F& E: @1 j2 R在浏览器中访问:1 G1 Y7 M9 D, S- t6 k" {2 R
0 F  ^# |0 z# V$ v) f; w& @7 O# p. F
http://www.52os.net/phpinfo.php?id=1 正常显示。! C* @) F5 U- E# b8 i8 S5 w  i
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
3 M  m" _8 v+ u- Uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
5 {5 M$ B, B. |/ B4 L% }9 D3 C说明sql注入和xss已经被过滤了- ~. s9 Y8 |. ]9 d: s+ Q/ x
( ~( T: m3 ^- I1 ^! L1 i: S5 r
七、安装过程中排错
& |9 `: k. y3 |- h9 C) h) z3 ~& d# u1 ~
1.缺少APXS会报错7 H' `1 c8 w5 _4 Q' \7 B
% @" O' c( c1 {3 @
configure: looking for Apache module support via DSO through APXS/ Z  ~  `- m9 b* k  B; Y
configure: error: couldn't find APXS+ t3 o6 O, C+ n8 W( I. _
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
5 r$ }9 m+ |/ w4 z2 X解决方法:8 k# \/ q$ b3 I" ?

8 |2 o& e; r7 _' D7 K+ a' Ryum install httpd-devel
! f% [) V4 |6 u2 ^/ g6 i2.没有pcre
, L. F: J# i' R' o( I2 l0 R8 X, p5 |( y5 t( [1 `  b8 D
configure: *** pcre library not found.8 p7 F. [- K# z7 l1 F
configure: error: pcre library is required5 K. s; @( }3 G
解决方法:8 x) w* ^  ?# B+ m

& A/ {0 b8 W8 }yum install pcre pcre-devel
: j- B0 f" @) u; A# B/ w3.没有libxml2$ {- C! g( t: [

2 _/ z8 q6 ~, s1 `. Y+ I5 f; {7 f7 ^' Q
configure: *** xml library not found.' `5 m" \  F( `+ h% A: U
configure: error: libxml2 is required( K/ [, w; `& P, H
解决方法:
' f% [: `; i+ H2 C0 {! a( v9 I. a- A, }6 \
yum install  libxml2 libxml2-devel" x" Z' \$ ?& L
4.执行 /opt/tengine/sbin/nginx -m 时有警告' Z8 o* K2 a* u

$ X1 q* [: X; K" }% fTengine version: Tengine/2.1.0 (nginx/1.6.2)3 `( \5 m6 X0 F) E! W$ l
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! E) l( _- w2 G3 l原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 j5 u* K3 G# w- e

3 \& c/ T* b% l) ?! v3 w4 Z' G) a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) @6 _! ]* B2 ^$ N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 {' A4 L9 F% f2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 _8 I0 P, `0 v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# m" X$ h) l( q- ?' N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! B  D+ X- E. `. l
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
  h6 ?& l3 ]' _" \+ o% Y* l% p解决方法,移除低版本的APR (1.3.9)5 j" ~+ F2 P( s& ]% T1 E8 P
3 Z' _6 I  u; P7 A2 F
yum remove apr' h  h2 n- N. y) b
5.Error.log中有: Audit log: Failed to lock global mutex
' M2 o* T% G0 t( _' f
, C& D6 A- ], k6 p7 @& r5 @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     2 f; l; I6 q3 a' r# h! ?# j
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]/ J) S! P6 Y# I8 T( O
解决方法:
5 L: c9 k+ _8 Z# q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ g% W, r+ v$ ~% `0 C1 u% D- F

4 @3 F3 @- N+ L6 ^SecAuditLogDirMode 07773 ~" r% x+ K: g% I4 z; s3 U
SecAuditLogFileMode 05502 v( u1 i, T6 f2 s, s* C
SecAuditLogStorageDir /var/log/modsecurity
8 ?/ |/ A7 t2 ?  r0 ^( y8 k; T' NSecAuditLogType Concurrent5 |! d- s7 N5 z& ]6 s1 s# R4 n, p9 ~
参考文章:
$ I  ]7 w6 u/ m5 ~https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" `( W+ `2 z. Q0 k( a
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-30 08:36 , Processed in 0.057239 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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