找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12504|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* u! e6 L! e2 E7 s# u7 m' K$ x
; J- h8 q& Z, B" V一.准备工作
7 |! o$ O3 p0 _6 z: y2 u+ N9 X( n- r& |( I
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& h# G( W- W4 `. G
) h. v! }% M  l$ itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz/ J, s, _& q3 P% t) l: v

* M3 J' T$ c0 N7 \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% {! f5 I& o0 O

: G8 T# G+ X" k( s7 I2 iOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 h, N! Q& {# o/ `9 T1 b+ k4 E4 ~7 m/ l1 C# L2 R
依赖关系:9 M0 u9 {7 W1 j0 {
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:) p0 U" A: }; o3 f* r. q
0 i* P; N6 r0 T) ^% `1 r6 V
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* |. P3 F" C9 w  Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr+ T1 J* {  b% w: X
1 G2 i* W! m7 n; ~
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 C- k: N4 x$ ]# ~( d二.启用standalone模块并编译/ l: [# k5 o1 R3 s6 W' E4 r
. j- x1 Y4 d( @; J* T  k
下载modsecurity for nginx 解压,进入解压后目录执行:* G7 a. ^# M, l# v
8 N5 i0 s6 {; F9 D: _
./autogen.sh
* b  A. v* p6 U./configure --enable-standalone-module --disable-mlogc
2 u0 k( E" G# z- v& ]- rmake , f; H% p' d6 |$ R6 N& Y2 {
三.nginx添加modsecurity模块
0 G; R* `" m9 f3 P2 e# v0 e1 H. u( p1 X$ ]' d* i! ]' x/ C
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 ]% x2 p5 x+ k
. n4 ^6 \" S' g7 W' h6 r0 l
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ y/ F! a) Y' U" h2 d! ]* k
make && make install: ^: Z8 P6 W( Z& S1 w
四.添加规则1 }/ c+ K6 G' t9 \( P

( @& e9 r6 V& lmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, @. V" H2 h# {% P: Z! p" l& S, T" R% ~( |% K2 C+ L8 {
1.下载OWASP规则:4 g+ c: }) F% Q; O. p2 b3 @1 h: J& z

: o0 ?) k) u$ {/ xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs; z- a2 i* x9 r2 M& @

/ ?4 X7 D/ R! F7 o  P8 w5 [6 Lmv owasp-modsecurity-crs /opt/tengine/conf/
5 H3 b1 B$ c% Q* }: u0 b& H0 r. Y8 [8 [6 u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; @# R  r; N0 N3 U2 Y- G2.启用OWASP规则:
8 v0 E; n- Q- P/ x) T( Q- r$ d; p+ j9 i. L# E
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 S" @/ @* j! I9 U

- ?+ I( U$ {6 `编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 V0 N! a& D+ D4 n2 ~8 |; a
" R6 }7 ~6 f) g+ U9 M: {  k# I' u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 `% W0 i( w1 @# I- U" V, H* |: Z2 a2 a  @! ~! l1 B9 P
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% p$ F0 _; ]2 e* h, Q8 E, YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 m0 T* l3 C7 ^2 n; o( B+ SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' M* f: N* u+ \# MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 H* ~3 G6 z: Y+ ~' c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 m" i, ]# o* ?6 H5 V4 [: j. I& ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 K: ?4 v/ {8 V1 ~6 r  P1 gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf  P8 J( }& d6 ^8 `8 q5 H
五.配置nginx
6 H2 D) }) [0 `% U. N, |
" R" o* @0 E# J" i7 H: G! F0 {在需要启用modsecurity的主机的location下面加入下面两行即可:& S6 G. e8 v8 w/ g
$ G% V3 u; a% P: Y. @! y' ]  B
ModSecurityEnabled on;  
  |- E  R+ n+ ^5 g3 ]9 l+ WModSecurityConfig modsecurity.conf;
& M) b& \- ]8 ^: D4 X下面是两个示例配置,php虚拟主机:, m: K; b  `; v( ?

+ `$ U& M7 x4 i8 u$ xserver {9 c- D$ `: W0 m- U) T% B. Q% \0 j
      listen      80;
3 w" N/ E3 g" w1 Q6 u" z; w( n: W% C      server_name 52os.net www.52os.net;/ H1 V9 ~5 d9 v
     ! Y4 M  z& R" J4 i4 d
      location ~ \.php$ {# S3 Q' E0 h8 F( W% e. s3 r! w- w" Z
      ModSecurityEnabled on;  ) ?/ S% ]- V8 ?: D
      ModSecurityConfig modsecurity.conf;
2 k! K: U: y7 T1 ]3 h
: M- I4 N, ?% D- b0 K$ P      root /web/wordpress;
) L; C5 ]% F& |, C      index index.php index.html index.htm;
: ~/ w7 n# X: X! S$ P& x/ \  
: f, |* X/ p6 c# Z2 h      fastcgi_pass   127.0.0.1:9000;- l9 D  }4 |$ ]" a
      fastcgi_index  index.php;
0 }" z3 p. @# {8 Q8 t      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;; ^  L- l" F2 g1 |( L
      include        fastcgi_params;
6 x6 b. c) Z1 f      }
9 j* d- F$ x/ D- l4 H$ j; y& J7 r  }
4 q9 r" V& @7 Y& ?+ z9 }upstream负载均衡:
% k' u  O- n! p* g
, |. v) T. ^3 X( Bupstream 52os.net {
4 ?: }3 Y" J' g( m( i* K    server 192.168.1.100:8080;
. [3 p! U) T  g$ t, b    server 192.168.1.101:8080 backup;
' V$ K" X/ }7 |}' E3 o4 F6 l" H0 O, b

# a8 ?' x2 H# `+ @. hserver {
* N; H% D# x, u  f6 h$ _8 `, nlisten 80;
; K& c- E$ T; j1 Hserver_name 52os.net www.52os.net;
0 E- f9 D1 d2 J) ?7 I$ g" c6 r- z9 f& ^+ F, X
location / {
$ U) }) z9 Y9 b# J! C5 y% L    ModSecurityEnabled on;  
% g1 ^* Q1 k" {    ModSecurityConfig modsecurity.conf;  
. s7 y) Y+ x+ n4 y' G  W2 e& Y, V0 B( H; d5 G* P0 ~
        proxy_pass http://online;  I9 |4 y9 u; N3 n: {* _9 [$ {
        proxy_redirect         off;
9 w; F8 P& Y+ g        proxy_set_header Host $host;
1 _& u  X" U5 r) X( t        proxy_set_header X-Real-IP $remote_addr;0 L0 R( |, O  r/ m+ V; {7 b# ~
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( l: O2 L1 }+ M8 ?- [- u. b* G    }
( P1 v# j3 S) N}
; S; `$ v. a  X% a/ P六.测试4 w" E6 q6 ^( E$ E  h
% |. e. z5 Y* H0 w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 ~: g& {8 ]% i- h6 k5 ~

4 v' L( N: Y$ b  t! o8 z<?php
4 `; l' P) U7 k' A2 {, U% d- Y5 D    phpinfo();   
2 t! f7 X# |, W8 d?>( q7 u+ V- f2 B9 j
在浏览器中访问:+ |8 b" j9 x# }1 W9 b. o

) B$ w! z9 D+ n. g& g* R6 B. m4 uhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% e7 j& O$ x+ K% y- {http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* M* e) Q+ \/ [( _2 b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 Z9 j( _$ Z" l3 W说明sql注入和xss已经被过滤了' G# K  E7 {& `

  L3 F3 p( A5 S七、安装过程中排错
# g3 P5 z/ ~$ c( J5 m/ c. g3 E1 d3 {: C' b2 e  h
1.缺少APXS会报错3 ?* K" d0 _% M. Z5 R

8 g8 X. l7 y+ Gconfigure: looking for Apache module support via DSO through APXS4 Y+ M' Q3 P. M9 Y6 P2 p! n
configure: error: couldn't find APXS' ?) D6 `: y$ Y. _
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 ]( ?, x- @+ V. g: v3 ^解决方法:
7 v" ]/ J4 M# x4 M% c# L) L
! Q& J( y$ L3 o; n! myum install httpd-devel
2 H4 l2 |( w7 x) G2.没有pcre8 T3 f) U2 q4 d$ E0 i+ R! N

* W. n, v1 w1 A4 C& _* P2 e, uconfigure: *** pcre library not found.5 M7 R/ H/ b  F
configure: error: pcre library is required0 H' O, @# I1 Q- j8 V: X
解决方法:
! E* H& L6 B6 L. ~/ g5 \' t  M
- T, l# F' e; f9 C" R) k  j, pyum install pcre pcre-devel7 x% b0 {, \# q& w" c& `5 z
3.没有libxml2
2 u/ d/ [# ^. n- M7 k  k6 J% [. Z) a: o, ^3 Y. t2 P2 ?! f

+ }6 J1 B- T$ N# Q, dconfigure: *** xml library not found.  r# _8 q! }: K0 m5 L1 c
configure: error: libxml2 is required/ T9 N: F9 f+ Z2 c' ]" }8 ]
解决方法:: Q; ~2 F) ^  R5 |! b

7 q( Z4 `, `$ byum install  libxml2 libxml2-devel
1 u. p5 l. ?7 l3 Y# r: s4.执行 /opt/tengine/sbin/nginx -m 时有警告
) P" M' c9 J6 w$ e5 a5 o- S5 `+ `9 {- s' A
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( s& ]# W/ e! m9 j) o2 c
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: S- O! N3 ]' z+ d4 ?1 r" f
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) o6 T8 G$ Y( M" u  {
0 i: A* t% x/ B6 j9 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; k$ b& S8 r- g. h( Z3 G, \; L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
- ?4 z" D6 G; A" I; a* f  a, \2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
# ~* E7 q  ?; p+ G! [& O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. _; E$ ^) Q+ _( l' Q; V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' l" k  q% q! `' ^& C' ]0 i/ R: S
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! |8 t- \8 h  P+ c9 i. @
解决方法,移除低版本的APR (1.3.9): \2 L. r% b2 P* M& N
7 }$ ~3 Q- @% f+ }4 \, `/ n
yum remove apr
& r) j/ {! y( J7 f5.Error.log中有: Audit log: Failed to lock global mutex
# ], A, {; R, R& J1 [% t. S/ ?0 _8 {3 @! G* [; g( B
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     9 O: G: s4 S( }% J! J: u' ]
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- s) }9 {' I  \9 n解决方法:
( V% _. g0 r( d9 N' Z) E9 Y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 M. V5 Z' `: t# B. W6 M, F: g
6 h" b7 c9 Y( o/ H9 l7 [SecAuditLogDirMode 0777
; _7 [9 j# S- y1 ~/ eSecAuditLogFileMode 0550& i' K& R* O% g8 o% \3 V" ~
SecAuditLogStorageDir /var/log/modsecurity
/ s$ q& O! g% Q5 v, j" mSecAuditLogType Concurrent  l  c2 Q! U8 |% A8 N* j! D
参考文章:4 S, u8 n. H* A+ v4 O3 S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
4 H5 h& H* A" z* h2 ~; a# u; ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-11 14:10 , Processed in 0.062771 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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