找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13050|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- y6 P  B# {' D- g6 T, V4 X  N6 h- h
一.准备工作* e/ U% S8 E  O* V7 M4 k  _
. J: X! ^' e2 l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
  @; g+ [$ e" b' Q' I  B* |" }- `/ e" P  d
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ |9 t( z. B) a( ~' e/ D3 r% D. `- d# ?: p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ ~# o0 A  U; H8 }- `: ?+ ?2 @
" ?; I  R+ G3 TOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. u5 e3 ]: i" m' R! S
' r! `7 j/ h. N0 ^% m
依赖关系:/ T5 }0 D# f5 h7 V/ I( k
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. I3 O+ {4 W& z* N) w( v. S8 {& e5 y
5 j1 g7 A6 b( S5 Z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) z5 m! N  p9 }; P3 U3 ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
) ]9 a$ r+ B1 ^, f3 }. f, A0 a7 A, q, W3 ?7 k
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
, J  |5 V6 e# H; t二.启用standalone模块并编译
/ @, g6 ]$ v- I0 A8 K2 J+ [* C
% ]4 l5 A( M/ \' Z* d; F下载modsecurity for nginx 解压,进入解压后目录执行:
& u# g1 r" f( E5 z, S7 T0 B5 |9 E! s5 R
./autogen.sh
  ^* P" ]/ O: Y./configure --enable-standalone-module --disable-mlogc; R5 @$ W8 u/ M6 x: Q, F
make
; v; T  ]) d( f6 _6 m三.nginx添加modsecurity模块* _- C$ j& D5 P
: i2 t# [2 o: L! l4 c1 G
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, B3 m5 p* ^  k+ y+ m3 |5 w8 q0 W& X2 d3 z% {" F- b5 m
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine" H" h# z- D6 O: V) R6 \9 p0 _
make && make install
- Q: Z  D# A. D1 D8 p# F四.添加规则
# ~3 D5 P0 v5 v. r
9 @9 v/ h* {9 R5 j, Bmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ x/ C0 w- z0 U8 j& Z" Z  {3 f- A, W+ _& X
1.下载OWASP规则:$ E/ f6 ?& G$ V- P7 K

2 z0 [( J8 Y# K0 E$ Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 [1 G. Z, r; s0 I  {
/ Y. S- x) L0 B, V# R; X, ]. T
mv owasp-modsecurity-crs /opt/tengine/conf/+ E9 O6 ?/ ?3 K( k7 x6 @$ V$ x

- L& v' h5 o4 u/ s- n  z) icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, |$ Y: H6 b% u- S8 y  L" l
2.启用OWASP规则:7 t) d1 i4 n5 n2 _6 V" u

$ j9 ~( k' s7 X6 D复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 |0 n. \; F& v) W6 z( ~

, l2 X/ K( [! I  P0 r3 |( v& P* I编辑modsecurity.conf 文件,将SecRuleEngine设置为 on  d) h4 R6 J, L
* Z) H2 O1 K$ A
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 O) ^; Y, _- I6 T
! J0 ~# X3 B9 |$ ?
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 r( U" b7 [( F- f4 |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: |& t& A: i$ p7 h! u% m) O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf  e; n4 ]. M5 k4 H9 |. k4 Q/ ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" L$ M/ ~2 h- c. ^. ]$ W: L* A! m" Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* N2 T1 U- [' w- h' Q  M5 rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# {/ N: x) K4 t! C, \8 p& Y4 n
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 ?" T5 r7 ]+ H" G
五.配置nginx' a' Y# \/ B6 Q

+ o6 |" L/ t" y+ a2 ]7 j在需要启用modsecurity的主机的location下面加入下面两行即可:- d( i* N% i8 ~  F/ C5 h( ^8 g7 E

" X( T( Z" f/ I  A/ ^ModSecurityEnabled on;  
' \; G; K( i) i3 \ModSecurityConfig modsecurity.conf;4 V  C0 r0 W- u5 m1 @2 y4 O9 f
下面是两个示例配置,php虚拟主机:
& u3 }+ J9 d# I4 t: }- r
! x! L9 h' y  ^) Tserver {7 L+ J( n  h8 k4 E
      listen      80;( @1 X5 _4 d) e# m9 t6 f$ V
      server_name 52os.net www.52os.net;
1 h" I4 E( E2 |$ S     7 o" f9 V5 ^& B4 s$ N5 [* A& {
      location ~ \.php$ {
+ M8 r* E. s$ W8 e      ModSecurityEnabled on;  
9 s. I  S1 D, b5 S* E4 m1 M9 V6 ?      ModSecurityConfig modsecurity.conf;
) b  Y3 F: C- b1 W8 \" @+ d: @) Q) w; F* ]# g( G) F) |
      root /web/wordpress;
7 P# c+ L; s5 B1 Q$ J      index index.php index.html index.htm;; C& R) @) T8 D# M
  : Y4 p  d9 R  C/ Y% @; I
      fastcgi_pass   127.0.0.1:9000;& V5 r; X% ]) h5 J8 q6 m" T( U$ Q7 ~5 }
      fastcgi_index  index.php;
; h9 ~& m2 K' `6 a7 d      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. n1 j# z9 L+ y9 R. r! M
      include        fastcgi_params;
& ~% W% o* Z) R& |5 M3 g3 \+ S      }0 c% d/ D. f  V* f
  }* L( U5 y% }- q* `9 m- U2 f) ~
upstream负载均衡:& [7 d- m: R" ]; Z) x5 C
: E  g1 }+ U, A6 ~. W7 w- S
upstream 52os.net {& `- ^: w' s: w7 E' T
    server 192.168.1.100:8080;7 v/ w+ B/ f) E3 n1 S3 U6 _6 q
    server 192.168.1.101:8080 backup;
2 y9 }8 _. y! z3 o1 i0 ?}
" y6 I! p2 c' h2 t1 S
5 ?! r8 p0 R+ Xserver {
- E2 |4 P) j8 o1 k+ b6 W. Llisten 80;/ F" B$ K; F9 q2 h3 R, Q9 \; u0 m# r
server_name 52os.net www.52os.net;
' h7 w! g. F4 o1 p
2 X8 U: Z% ?+ R3 ~+ {location / {! b! c4 s; p) v: H
    ModSecurityEnabled on;  
, k& A3 {, l3 Y* p+ X7 i" x    ModSecurityConfig modsecurity.conf;  ! j# d2 D# g" J' w% V/ @

* j6 f3 n3 @$ G& A        proxy_pass http://online;+ T: S6 q2 ?' Y" |2 X1 q
        proxy_redirect         off;
" R: z# k) V3 q/ P        proxy_set_header Host $host;
: L( j9 }5 i, g& {        proxy_set_header X-Real-IP $remote_addr;
! u* t- T3 k1 w1 w# X        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 r* P1 Z2 u: B6 L# W4 ]4 T
    }
- h& y; @8 v6 G9 ~: m}
# |2 F& B5 h1 o  m; `- m/ ~六.测试
; Q. \6 L0 z; V) P* H4 X) J9 t
7 }; c2 F( j2 J4 \& `. k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, \; z8 |: C9 v- U8 C. E
% o/ \4 p- V2 B<?php9 ~/ I' a. _* c1 o
    phpinfo();   
2 _% d! Z# d8 \?>
. F+ Y. [  i2 j0 ]1 Z9 z在浏览器中访问:
' h+ ?  Y: R! |- o! s; P! c/ \, w( l- G+ ]( l* N# i  X
http://www.52os.net/phpinfo.php?id=1 正常显示。& @$ Q! j9 Z% @
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 R1 ~. @* h7 f$ Y/ i2 H) L
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: l. Z' ~4 _9 K/ a说明sql注入和xss已经被过滤了, }; M# N; `9 V$ r
, r  Y5 Y" B2 s# k' F' y
七、安装过程中排错( Z) A6 l* ^9 g) `8 E
. d7 n  z1 I0 m
1.缺少APXS会报错2 g7 j! N- T2 j1 \' u& h0 l: A

! G0 {0 j  y7 H8 _7 f, @5 rconfigure: looking for Apache module support via DSO through APXS, ~6 w& J% U: U2 Z
configure: error: couldn't find APXS
9 x# m4 s, S3 \& Xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 [% C: W9 Y$ @0 h$ k解决方法:% ~" b/ b) g( e8 C( o

" `3 K) {6 y( x, eyum install httpd-devel! X/ h7 u1 ^4 c4 Y0 C9 W% \4 V
2.没有pcre
/ N+ r0 G  b: D* M
9 G- X) k, @4 ]# @# E; A2 \3 l- jconfigure: *** pcre library not found.1 `: O, ~* W% Y4 l0 O- U, v  I% o2 g- b
configure: error: pcre library is required
$ S8 X1 M, u) u4 |" K" |解决方法:
# p& z$ B5 c& [2 r$ I  L
, Y  L& B+ @. N6 iyum install pcre pcre-devel
5 V* O, Q+ d  s1 r* t3 _8 w3.没有libxml2
% |6 A* o" d* w4 G! {
- c2 v+ \  N# `* `, X
7 G- f2 ]- B. U6 @$ t/ Y: Yconfigure: *** xml library not found., i; V( U6 Z& R& S( e+ Y0 B9 T7 ?) Q! g
configure: error: libxml2 is required3 U+ y' I6 v9 F1 R
解决方法:  k" v. r, e2 G$ N: T/ b0 K; K8 W3 y
* ?6 A  R2 a+ N' o" ^: K; c# t8 z
yum install  libxml2 libxml2-devel" C$ Z+ j1 m& ~  N
4.执行 /opt/tengine/sbin/nginx -m 时有警告
, ?+ i  Q' k1 Y; Q, S4 M9 |- G  s' \+ t2 g* a3 y  h
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 e( Z/ ?6 ]( o5 \* N! w3 i! Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. v; P  {! h( q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 a& f- K" j6 |7 c
7 c6 F; n9 ?' i' |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ r4 U& n" Y( d3 q0 x$ ~& S, [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ u# A+ u: i" X( Z' E! n4 Z8 U2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ ^  N1 y+ U8 `" ~& H8 F* T2 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 W) A/ f* d% i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
( }5 s# ~" j! {; [6 I) x2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 h5 i$ P, }5 ~
解决方法,移除低版本的APR (1.3.9)
" X# G9 @/ G% M! O
! H& Y7 ?% Z! e! \yum remove apr
) I# D7 U% N' ^5 q& U& \5.Error.log中有: Audit log: Failed to lock global mutex
' ^) T, I. j* O( x
* n; h7 @9 o/ X1 f1 b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock       H8 {3 W- E. H, O
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 L/ q( B8 x; P' I4 b+ w- k: Q
解决方法:
) G9 T4 c& Q+ K7 Z* c. D9 M# H编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: g- }# L. z( f8 o/ L  M/ }
- A+ O: _) n' n4 X6 L+ u# R
SecAuditLogDirMode 0777
  N% ^% ]$ h8 t! ?/ s$ pSecAuditLogFileMode 0550
* t" U1 D( i1 {6 R( ySecAuditLogStorageDir /var/log/modsecurity
0 S1 W( a0 ]" ~, m# b" L2 ESecAuditLogType Concurrent$ f. g7 Y; @3 f$ y! N0 |/ }7 l  p
参考文章:
6 I/ t1 D; ]& V- ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 j/ R' H+ `5 [) L
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-8 12:41 , Processed in 0.066812 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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