找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13049|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  b9 Q) y7 K" M8 ~& U, b, r. o, X/ _7 t: V) n
一.准备工作' Y' A0 ?, X# M, u
+ Q% C+ A7 N3 F" ^2 b
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 v" o$ p3 ^' c/ B+ z4 D# u4 Y8 \
7 N, m# j: d1 {; [tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ y! v* l, L) P) U* S: f' J; M
6 ~$ b' w2 N! {) Y0 n( S
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, f; s) f8 O8 b; F& G! {/ Y) b4 P5 a  x) c% ?$ J
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& }% ^8 f$ r! @9 @( P' l" e  ]9 k( O7 c
依赖关系:
' \9 K3 Z: r6 z$ ?- Ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 l' f9 V" @' x! U' h3 N5 j' O$ D

  Y, E& Q# R( X- h- ]yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel4 v, O) d/ V# ^/ P7 G
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- G4 y% J. l, V& n( a4 o/ I. \) U) A4 L! ~4 [; r
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel% p; R7 [" _$ K: T4 g, J; y1 |$ O# z
二.启用standalone模块并编译
% W4 t8 z" _" W- h6 _1 C& t* R
; O% J' {9 _5 s" m$ y0 P. L% j下载modsecurity for nginx 解压,进入解压后目录执行:
4 E/ g: {3 o) Q- x/ r
7 S! |- q; H! U$ i7 K& U./autogen.sh% [% h- x& D: }" B" f
./configure --enable-standalone-module --disable-mlogc
1 B  w( a6 |/ |0 Y+ ~make % o: K- V7 ~0 H* d: }  r8 V( p. ^
三.nginx添加modsecurity模块
  s# _0 G# F: H* x" n8 G
- Q) }. d9 {7 [4 [2 R$ H4 s3 R7 L在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. I3 L& b) u9 N. x6 T  S/ W" H/ I9 T
/ m% ~0 O' s. C* r# t; l./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* T6 E1 d, C4 W2 S" o% W8 zmake && make install! Y* @' r2 A* M9 s
四.添加规则2 D" c& ]& o9 R  m' L
$ j( }6 t$ m* ]1 ]! Z5 e/ N3 f" U
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: o( K1 ~3 ~" Y: S0 u$ o+ A. q  r( D( ?9 G) {# @9 o
1.下载OWASP规则:8 }% o  Y9 z5 B" d! P, u" o

3 c+ j$ w2 ~' mgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ e: m, g- i: L4 G: i* r9 A
& [# F/ R; V: k0 R# h0 f
mv owasp-modsecurity-crs /opt/tengine/conf/
2 q* g$ N. a5 I: [- r! T2 s4 t& }" ~% I+ B% ~/ W
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# C4 n, R, z* t9 g# `2.启用OWASP规则:- g! c4 g4 d. m" |+ V$ [# W# H1 {

/ C. J: K# c/ |; K. Y  H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% M; U' z1 Z' `9 i) G, U; P, G

4 {$ q1 T) O! b. C. R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
5 y3 Y/ G, M7 A( G! j9 I+ x+ q& s8 E0 J! ~4 k. ?. Q( S5 U3 t, z# x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) S/ f7 t: w% V9 ~7 q0 S( ?& J; G

1 Y5 J; O! ~& x  e7 k1 ]1 k9 s; zInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ n) L) [( A* u: x  yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
  ?( b: f& T2 {4 @; Y7 P8 [% BInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 I/ @6 r, @* Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: N7 D  ?) Y: d& i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 |+ u0 E+ r5 N. X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- }) j  |  J4 W6 {  ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. ^! @  ~8 Z0 R
五.配置nginx8 ]( F# r4 I/ a) B( ?  u% w

9 z: T% H4 F+ t; e2 V- q在需要启用modsecurity的主机的location下面加入下面两行即可:0 i' J* E' y& m" I* X* k( v' Z
: H/ d5 q$ V4 W8 ~' z
ModSecurityEnabled on;  0 G; n5 Z5 z/ D4 A
ModSecurityConfig modsecurity.conf;
4 f; I; k  ?; b2 H7 T) v" y6 f下面是两个示例配置,php虚拟主机:
" c8 O4 |) x9 h6 b0 s6 W- Q4 T/ D7 J2 ?6 _9 ^; I
server {
; a* {$ [* t5 O# G3 Y' y      listen      80;2 j* s# N! d  N
      server_name 52os.net www.52os.net;
& Y! f" N) }; X, ?3 m. m     
- G7 p# d5 P. d( ~4 E      location ~ \.php$ {( Q6 ?9 b' m  c* n
      ModSecurityEnabled on;  2 _1 W& V+ b( R, x2 @. g
      ModSecurityConfig modsecurity.conf;
, A1 i9 F* P$ K. {$ S$ q
3 T* p* ?# A3 X5 `, o      root /web/wordpress;
7 u2 |3 M% }: r, x7 _2 I      index index.php index.html index.htm;
5 V, k; a0 x; f' G. A, u- q0 t  ; M6 F; z1 y6 g( z7 V3 G
      fastcgi_pass   127.0.0.1:9000;
# C% T: W) @3 ?. o      fastcgi_index  index.php;/ P' I  @4 S" Q/ N0 N, v9 \" B- R
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;  X& ^( B+ j( |. M% _
      include        fastcgi_params;
" x+ m% E: H5 O5 B; b! x      }: N  h9 k8 d" b+ V7 w: c1 R
  }  P8 ^8 R( n6 o! Q( P9 b
upstream负载均衡:
! S. Q' E4 |) i, \7 R: B# O
5 `6 }& l0 b. P3 O- Wupstream 52os.net {( B. C  q) q2 I- S7 k5 R
    server 192.168.1.100:8080;
* w1 U7 H1 x8 ?7 E$ n/ b' g    server 192.168.1.101:8080 backup;6 C+ D: G! ^, c  X2 J4 Z# U8 V
}9 W6 |9 L5 c7 _/ y

4 V9 d. _4 M) `9 k, tserver {
; Y% I: E2 t6 {" q/ T5 f- {listen 80;
# L2 U1 D) q$ t+ a/ n4 kserver_name 52os.net www.52os.net;
% y! e2 b$ e. e( Y
+ G/ R& W8 Q0 Z' D+ [location / {; `- z) G! w8 q1 Z" W+ |8 j
    ModSecurityEnabled on;  
. F: X$ z- f" {! I' W; U    ModSecurityConfig modsecurity.conf;  $ _  w% h% I, K% J) A
  B/ d' H, Q( T0 l' F
        proxy_pass http://online;
; _1 Y3 ?2 ~$ \6 \3 u        proxy_redirect         off;
5 [, c- c6 [* l9 {# d        proxy_set_header Host $host;
& h" T" V. P2 g8 A        proxy_set_header X-Real-IP $remote_addr;& T7 i. a0 F+ l
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
3 u. a6 q& H+ R# a) j3 C    }8 W' J6 h4 ^: Z) A; w
}1 h1 a9 K/ Z/ j, L  p7 ^
六.测试
# s4 w+ ?9 T2 s6 ^% G
- @3 ]9 b" {: ]我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:' T0 v  E4 S, V  l/ c
/ `4 Z( ~5 D& q6 d: `& T
<?php
% V3 c) A: P& g: E( i5 B" ^; G    phpinfo();   
9 C; g2 @1 Y+ {% ]' [; M! D( j?>
7 b: M8 }8 r, s) g在浏览器中访问:2 \" @8 C& ^$ J* `

5 ^2 E/ ]' [6 Y# I0 khttp://www.52os.net/phpinfo.php?id=1 正常显示。
! t/ @" k$ e* C. ^3 q1 uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
, ?8 Z& r/ N& {# e9 g& {" Fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: Q5 T1 M/ d3 G* |% F: ~说明sql注入和xss已经被过滤了$ _/ v# x1 f. T- y
, ^& i7 \/ b0 X8 C) W
七、安装过程中排错
6 t5 W9 y8 j. u; p% u
9 `4 U- _  s- W9 K3 w1.缺少APXS会报错
7 r1 z7 E5 |+ X# @- r' ~4 G
  W: D; y* i/ R2 [3 g! oconfigure: looking for Apache module support via DSO through APXS
. P5 d' \( v9 P" P* d) ?1 M8 \configure: error: couldn't find APXS1 i9 a$ f& V* p& U- Q. j4 g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
+ r6 t; q5 b* c% t; }* M, D9 F解决方法:
& o# ^/ y# g. ^' J" P, H9 r7 @' W6 R$ r
yum install httpd-devel
* L. w6 J) T& D2.没有pcre
+ I) ?& p. w# V. t
2 v; A8 U* h, t( O2 ~configure: *** pcre library not found.9 @) H/ ^4 e( t; o
configure: error: pcre library is required6 f9 r4 G/ e+ R( ~
解决方法:+ A- i. w7 V2 a& O8 G- r

( E' ^) R. f' O/ kyum install pcre pcre-devel. Z4 q$ a$ d- m5 i: C1 t' v
3.没有libxml2, [1 e$ o' l9 U! C: u/ G

- ~% g! V4 v4 N2 H' w( f1 P: t6 ^: ]. Q7 Q  X& D
configure: *** xml library not found.( o  j9 c" h" A# {. X
configure: error: libxml2 is required
" y1 |& }- u( |6 |解决方法:' `$ S' B# h: v* I( r5 T
0 P3 ~) ]6 g' a2 u  a' r3 U. H5 h
yum install  libxml2 libxml2-devel
! B5 ^4 L/ w6 j& P: x- `! D4.执行 /opt/tengine/sbin/nginx -m 时有警告
8 K+ A, e! \5 T+ f% B* |  `8 ?: ~& c: k* I
Tengine version: Tengine/2.1.0 (nginx/1.6.2)& w( k; x0 I0 ]: V. n  [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# S% X" i3 B$ o/ S( \7 j
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, t8 {6 A9 u# ?; C- Q
' [8 k0 G1 }8 {- }7 _( M5 J7 t, U0 \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, {& v# t! |5 H% {! Q( B: O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
- Z' M) c# m4 R6 H0 |9 g2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 E7 ?, K6 g+ z$ r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": q$ |! X& \) \; i$ q) `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ K& d# m( [( s( o) {7 x  v" {
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 l$ G( Q' J$ ?- e- l' Q2 \解决方法,移除低版本的APR (1.3.9)
; k& Q6 l( e6 N  B- A  [8 w8 U7 P7 J  ~  m
yum remove apr
3 P; k9 T# n5 p6 u5 _9 Z( G6 ^. V5.Error.log中有: Audit log: Failed to lock global mutex9 c+ E8 a3 l! T& ?; @* c3 X

! ]6 Z9 ?" @1 R$ q( m' N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; P7 O3 D* Q7 S9 J0 Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" B1 m8 z8 P3 u4 I: @  \
解决方法:
3 I# y' Z2 {' e+ a  C3 j; [编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
  s. @$ {- Y' y
2 M7 s& K/ l- c  r. ^* b- ?SecAuditLogDirMode 0777
& m' {0 g" s" l% F3 GSecAuditLogFileMode 0550
& v! X* U- o* P( c7 x9 KSecAuditLogStorageDir /var/log/modsecurity
, E3 b2 Q8 |$ _- P/ V0 Z7 s" NSecAuditLogType Concurrent) I9 ~5 i) K" Z, H
参考文章:* n8 R$ x8 Y7 w: K4 h# J( R
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 e( X, b& M1 h1 W
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-8 05:55 , Processed in 0.064697 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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