找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13134|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 U& U: h$ f" ]! g! L  I
7 ~( k" _4 e9 Q1 r. z5 |# I一.准备工作
5 D3 E' ^' h9 G: Z2 w, i
0 _+ R8 U7 @/ O- y, s1 ?系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 L4 o* Q( e) Z1 {
9 I/ S* W/ q* Gtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 O  p1 I( l5 H' _0 W% }2 S. ~
+ B1 Z; `6 A1 `' t4 O" T/ @# H
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
  h+ C% M' _# z& a# U5 O  k7 W, o4 Z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
% G4 `2 x$ b. f
8 M7 L- ~" M. D依赖关系:7 [+ X" h4 |# u9 ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ ?$ S" Z$ z; E9 B" \) E) A# {6 K6 ^" L
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
, v( h' |3 J9 p! e/ s  T% o4 s; ?modsecurty依赖的包:pcre httpd-devel libxml2 apr. _8 [, T0 y9 t7 V9 r/ _! {
* Y( Z( z5 v4 j
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( H( K2 V# K7 }, l
二.启用standalone模块并编译: q* R" x7 ?+ ~& s% U6 B( t

8 \$ \# E% N, H% P. r下载modsecurity for nginx 解压,进入解压后目录执行:
0 e- J# x0 y6 G- \; ]
3 R0 V9 z. B# z./autogen.sh% y& O; Q& {) S
./configure --enable-standalone-module --disable-mlogc) a- n( k# V/ a" J
make
! [) v, [- @$ B( m6 B0 I三.nginx添加modsecurity模块
/ J9 H8 @% t$ m- B, L8 K/ G7 j) a
6 s8 P/ @# S6 C1 I( Q* I在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 C+ l+ Q+ H* d% o8 r3 A# d2 e# E5 T+ d' H/ X0 Q
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
0 J. F9 K! ~/ }" E. E. j/ D6 \make && make install
# W& t$ z% `4 L3 |四.添加规则' D9 q/ X3 S+ w- E" ~5 O6 ^2 [
' \' ]" l3 \  a4 t6 {, Y+ x9 G
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。  c. }, H& o: C4 D" h

9 B# [: S: [( f' ~9 D1.下载OWASP规则:
4 q' g) u4 I1 ]4 O9 t! Y8 o* o# N3 e  N4 k  d, w
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 Q- Z* Y+ I  I/ x) n9 Q$ @
) v' c( Z+ ^) _5 S9 j# Hmv owasp-modsecurity-crs /opt/tengine/conf/
- y' U; E  J/ G" N+ q  X! e7 r! N' R% S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf- d+ p( f8 L: l/ W0 W0 ?+ E
2.启用OWASP规则:
# c) ?) Q0 ~* n. b
4 V) |  E0 a8 M* u. ]复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& i  Q6 |5 V# o6 i! b' Y  L1 I) v
* [9 l% n/ z- O' _% R( I5 S
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& Z: F' t% R" R: W! x

% u, C' Q0 {7 s# Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% S% U  _& I% ?& s# ?7 M% X

& l7 E* `! r% X2 LInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 T( k* c6 ?8 PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# m  `- c* V# M) }7 R( `/ ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" n; f' z5 |+ Y' G6 V; k$ `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 n' N1 D6 `+ @/ o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 o, l" x$ J0 eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 H7 \- u: O7 b& u( G( ?Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
  n4 k/ G7 P8 i& W2 m9 T五.配置nginx
% [& d' m/ f1 a; w  u2 b* \" n8 _
在需要启用modsecurity的主机的location下面加入下面两行即可:
% ]: U4 A- g$ U% g- y6 n, T# w, F  s
ModSecurityEnabled on;  
9 \$ R; W/ ]# {8 H* lModSecurityConfig modsecurity.conf;
& u3 p2 P' I  J/ E" a; e下面是两个示例配置,php虚拟主机:, ?5 ~, v# k) F5 Z1 I: i5 s8 a; c% x
6 w' I- {# d- [9 R7 ?- z2 u
server {8 C! w$ F5 X! i
      listen      80;; O& u6 X% ]$ ]6 H% ]& @0 ^
      server_name 52os.net www.52os.net;1 Q! L5 |( s: o; [6 \5 ^! {
       f; a( g) `0 `3 C6 Z) W- G
      location ~ \.php$ {
) M7 N+ a( D) ?( T% y      ModSecurityEnabled on;  
/ K8 @/ D3 s" N      ModSecurityConfig modsecurity.conf;
4 ?# c- I2 o" q5 v' i) ]$ J" B6 a, k
# T  x  W$ _+ T* a      root /web/wordpress;
5 H% I: D9 w- m( k* T" h      index index.php index.html index.htm;/ {; @4 z7 C# I- K8 h3 @) S7 Q
  , V  h+ k! z, p7 V% v! h% C  q
      fastcgi_pass   127.0.0.1:9000;
/ ]) J* [0 \3 [, }      fastcgi_index  index.php;
8 q: {' Q8 [/ G8 @0 X% [! D      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;2 l2 d% g8 O7 ?8 c, j
      include        fastcgi_params;. h! m8 Q" F# U8 a0 Y" x% K
      }+ O9 `+ ^- Y( R* K
  }$ R7 H8 C. D. m, u8 e
upstream负载均衡:7 w5 R- H& e: o

6 h- Q" J+ _/ u% L0 h: ~- Yupstream 52os.net {- |/ O1 T0 S6 p: o
    server 192.168.1.100:8080;: C5 a& b* c4 B( B8 F# E
    server 192.168.1.101:8080 backup;
0 N) p( R2 ^* ~; M$ m( K% M}, R4 O% m/ O, j$ ~) O3 w

) }# y, G, a" }( H& {8 _server {; Z3 n0 H: M0 G$ y# D3 D% z
listen 80;+ T) O. s$ `% D' }" p2 G% ~  ?$ h
server_name 52os.net www.52os.net;) C* W" C+ x" }

& l) t/ R+ U# [5 P( S- Tlocation / {
( U! ~$ i/ B8 r6 ?( A6 T) n0 d    ModSecurityEnabled on;  
, a. G8 h4 ^8 L8 n    ModSecurityConfig modsecurity.conf;  
8 G) v3 p1 F- f+ y
! k5 m" ~3 m9 e        proxy_pass http://online;5 T0 w# C' O/ T' u; F' A
        proxy_redirect         off;. X9 A; l0 s: |
        proxy_set_header Host $host;
* o$ U8 C" ]& O: n& O, }        proxy_set_header X-Real-IP $remote_addr;* T) b6 Q5 h# P* ?, m# h! q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# W/ v0 D) C& ?
    }
4 J" s% H  A4 ]9 t: R}
" |$ p2 ^' Y+ r' U8 I. J  u六.测试
6 d3 J) C4 ~4 v8 U2 c% j0 i
$ ]& B) u6 e7 h* y" r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 [' w2 o; j8 i" z- @4 C

5 {6 X8 Y0 {, j* z( R& D<?php
0 K% {6 z+ c5 t0 m- S    phpinfo();    8 J" {. [' s/ O. c/ R- `$ [
?>& X# \& f- b7 c/ x
在浏览器中访问:
$ ]4 B- B- I' @7 I+ l# T' g! _, u+ U2 P! O+ Q9 `( ?; G/ e
http://www.52os.net/phpinfo.php?id=1 正常显示。
) a+ y/ T# B* i/ f' L2 ]9 d( _9 t& a% Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" m4 m$ K2 A/ ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。  o! r/ q1 D: R8 u
说明sql注入和xss已经被过滤了8 ?! k" P# Q8 b5 _
) A4 F7 d5 j% V
七、安装过程中排错4 e4 \7 A  o: `

$ a! ?, G8 g( [1.缺少APXS会报错6 J& t+ E+ U+ U& }8 B

4 d. e  ]! [* r4 C+ n/ B0 F" Qconfigure: looking for Apache module support via DSO through APXS1 @; E! g& I$ B8 v8 g
configure: error: couldn't find APXS
: K: g+ b- Y6 Z3 \1 X  }apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 J' f5 `+ k$ X5 u9 o1 B/ J7 X解决方法:
+ A! m& Z1 K! D3 Y1 j
+ v) R; v1 h0 M2 P( R0 ayum install httpd-devel
2 ]5 d# O  ^6 Y4 _; O  z, m3 x2.没有pcre' u; J) j! \0 |+ E" J# w

9 v' i) I' I% s7 T3 a' g8 {) l. Dconfigure: *** pcre library not found.: O: j& Y1 a% d# U: U
configure: error: pcre library is required
1 W5 {/ c4 k& Y4 X解决方法:0 X: g6 g1 c1 e+ `; P% T2 P* `* ~7 S
2 b: u! n- o5 F: b
yum install pcre pcre-devel8 b1 ?. ^) n9 z4 m- a! n
3.没有libxml2
9 g5 G2 D; I# I7 x" z' {$ D  G; ^; U  A- z
1 U5 O1 S7 Q4 D5 l5 V
configure: *** xml library not found.7 J  N3 s" E* x0 l: r5 n
configure: error: libxml2 is required
3 }9 T. N& v' [$ R6 p解决方法:
' x1 {# n, S9 O- O& o1 C. m2 `3 S- P
yum install  libxml2 libxml2-devel& ]0 }8 l* C1 s- a6 ^: _0 ^  I$ C8 i
4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 m' n; L) z6 K/ n% S* y- X7 k4 o; T- c4 y6 \" x/ q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)" P/ l1 f1 ~+ a
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!' v8 V: b6 f- l+ m  }6 d
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 U) Z, p6 d) C0 l9 h9 g% R. n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 z0 d/ K- E& m2 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"  n  h; Q" k& G" Q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 ?( }; }1 Q; c" ]/ H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, o! P( s# _8 u1 l0 w2 |1 q. ^  `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# `/ M2 t# k0 {) a7 T2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( ?6 B/ f2 B: C! Y+ v解决方法,移除低版本的APR (1.3.9)
5 P! g8 V3 ^  }) q! s
  X4 A, |- o6 s3 @+ syum remove apr
& E: ?+ p% U3 I6 ~+ u5.Error.log中有: Audit log: Failed to lock global mutex& M4 M' G5 D! S- b/ b2 p

7 K, J$ _8 `$ ?2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ R% g6 @3 z- X! V8 x; o3 zglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- C& t4 a3 k6 }+ B; o
解决方法:
9 l  S1 c' d. V! d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 T9 n2 ?# }: s* {7 g
& c9 a) O% f0 F5 c( d# j
SecAuditLogDirMode 0777
# }* {6 ?4 X3 T& \& q$ N- ^2 Q) iSecAuditLogFileMode 0550
( u7 G/ e" B) N8 g' B: _% `1 ASecAuditLogStorageDir /var/log/modsecurity/ k5 n! D% z% W7 H! j
SecAuditLogType Concurrent
( v3 r, i6 V( Z5 z8 Z: H参考文章:7 |1 }! K9 T' a  E
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 `% d" ?0 h! c( ~
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-18 09:06 , Processed in 0.058909 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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