找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11023|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 D; E( J7 f: v' J

! ~* J7 F& K. M6 N; V. }5 J- p# w; Y9 r一.准备工作$ C" s- E4 z& i

6 K+ a: n# R1 d0 A; D& O7 p系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' W$ o7 h: {! S7 D( h3 q& g# J6 L4 m5 ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- J- J7 ^$ {9 _& m5 y2 J7 J

  L0 @) Y9 s. O2 G  F- bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 C; C4 ?8 T9 Z9 o% Y: c( M5 _& ^7 {- N# \5 t
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 Q' i8 [9 c4 A; z7 k( N* q5 j4 M& `* o! a% h% ^8 x% {8 c
依赖关系:9 V: ]1 J: r( V% Y; S0 _( w
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 v) _$ t& x0 E( v" G& M: C' {
8 i. E3 E+ o) z$ ?: V8 T( t
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' ~8 M0 `, c6 j; R& mmodsecurty依赖的包:pcre httpd-devel libxml2 apr- ^* P2 t3 t) O3 K5 v2 `# b% j0 x
: d. i1 ^/ R) R& V6 E
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
# e  }! O: y6 e; H6 A二.启用standalone模块并编译
5 `' y5 |9 s; D
$ Z/ z2 ]( X% y下载modsecurity for nginx 解压,进入解压后目录执行:! x, P- W0 J$ J/ h2 o) w- s
( I6 a8 q  ]2 P* ^" b
./autogen.sh
1 A( `- l3 Y1 _2 R, W./configure --enable-standalone-module --disable-mlogc
' a* l$ P6 s4 V  P/ J# g; Q- N  ~make   j/ K5 d! i0 H7 O
三.nginx添加modsecurity模块
. `: T* Y4 ~( U% U1 @% U
+ [' c- Y' y% P: ]  j/ V在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
  a% T' ^  }8 y: B. D4 V! v- {8 e. X- o$ Q- f) v/ {% {1 t
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ Q, g; Y8 s$ k/ ?; q6 g  l6 Emake && make install
: V6 Y) \5 w2 O8 a3 i4 E四.添加规则
# X% [' d. J( s. P* q: j/ {' ^. w. I" \3 E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- g8 c/ F; o" e

) U$ h$ Z# ?. v" l% T. {+ \) W1.下载OWASP规则:7 w' D3 w& o4 Y" |2 J) N: S& o

# o+ w4 \* L' lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs; V0 `. A: D1 _) c- w" p% f

- E5 m% D8 ~% M5 ?mv owasp-modsecurity-crs /opt/tengine/conf/
- x  I" o! s6 _1 w9 Z, w& x! }- x9 i" p4 c1 H( p9 }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
7 I' V2 u1 B- ?! @. R' `+ `* W' C2.启用OWASP规则:7 Y4 E' h# ^2 z% {1 i

6 z$ d- @4 U+ C6 m" _复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. a6 i6 `8 Z0 A; [
' f# k7 a: ~/ `' Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 ?3 p$ `( v7 g" a- M. x# I
- i  N# z+ ~8 _9 ~4 s% ?" V2 Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 ?$ J5 J- `' \" c* J8 ?

! u) L) W7 f6 f8 TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; _# ?1 ]' A3 c5 j  ?1 {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 s9 w# q- D; O' a- o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 ^  r+ Q: B7 ?# ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. p! s( Y6 A/ }4 _+ U2 k# p9 v& U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" |6 ^7 r1 b5 C6 N7 H( M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! B7 F  a4 @$ g7 `. lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf$ [' t" I9 r, y8 o
五.配置nginx4 V, i) I& d9 [2 f1 W" H' F
, `5 n. `) V, ?( ~
在需要启用modsecurity的主机的location下面加入下面两行即可:
6 K: l) Z) A  I- Q
  G  r2 ^8 }6 o4 x7 t2 TModSecurityEnabled on;  
" w# y' O/ a1 x9 t+ M4 V. N$ M+ |ModSecurityConfig modsecurity.conf;' g" G6 ^0 j6 Q; X$ |+ P3 Z
下面是两个示例配置,php虚拟主机:3 y0 H9 T' p" x- Z0 D

9 z, `, W- y7 q8 D+ lserver {
5 }7 S" M, O0 z: o& a      listen      80;
& [7 [2 F8 o' n+ C; h4 l3 Y; `6 l' O      server_name 52os.net www.52os.net;  R1 M( |6 w. U. `4 }; \
       ?) U" I) n* P4 a- o* n# `' A
      location ~ \.php$ {0 y5 _) u5 I4 a) d3 N$ E8 a9 r
      ModSecurityEnabled on;  
3 J% d* N4 H( [, V) y+ R6 ~      ModSecurityConfig modsecurity.conf;% F7 J/ \2 D9 A8 ?, S

: O( s4 q: e! U4 b$ @$ J      root /web/wordpress;
4 I! @) u+ e2 k' X+ m7 ^; Y      index index.php index.html index.htm;9 b8 ^5 X' ]3 [2 N' }7 }
  
( R, t* h+ F6 [% l5 m! Y% e1 h6 F      fastcgi_pass   127.0.0.1:9000;9 i0 l7 w' d2 C: e5 M
      fastcgi_index  index.php;' t" I& k& A$ V; N" @+ U: l
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: Q1 v$ F$ a2 N2 f- D/ S: N3 t6 N
      include        fastcgi_params;" u3 f4 L  D6 c( q( F* `: n
      }+ [+ g$ Z3 }$ @, p
  }
2 Y9 ?2 q" b  }+ w' S) R, q# fupstream负载均衡:
8 Y3 j4 e4 l" }/ ^& i, i7 Y8 D6 h( p
upstream 52os.net {
, _& D" ?$ Y9 X    server 192.168.1.100:8080;" x" Q/ @( @# o# w2 p5 P- }
    server 192.168.1.101:8080 backup;1 j- `/ _8 q# g8 g9 L% n2 X$ a
}
1 z" O: o) S$ d& X2 t0 i1 [% m0 ]: u& c5 a/ ?# Z
server {$ |* Z3 A+ I6 t2 b6 T, Q
listen 80;9 V0 [$ f* d: U( n, M' \% |
server_name 52os.net www.52os.net;: @4 f/ T# T2 O+ j

6 f0 {* Q, c5 z2 }location / {
! |9 \2 x8 a7 x! p    ModSecurityEnabled on;  ( R; U% s& @+ d- I" }4 u
    ModSecurityConfig modsecurity.conf;  
7 J* b, I" a. ]
8 M+ T6 r  B5 `4 d% r        proxy_pass http://online;
+ [* e5 h( x/ A        proxy_redirect         off;) J5 {- b. d2 l; I2 P+ l/ B
        proxy_set_header Host $host;
0 A, x3 U! S6 ]( Z! t- m; D        proxy_set_header X-Real-IP $remote_addr;
0 {" d# G% ], s2 I# k. {        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
$ e* Y; ]' f8 g: Z7 h    }
. V( J2 \% E  {( H+ y}
; A# q1 G4 K2 {% h- I六.测试
) M( r' P3 |' F$ c4 J" `
" ?8 O/ Z/ d, d2 @* g( ]; ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' p3 H0 s4 E  @
6 f$ H9 q* _; e$ r<?php
+ F+ b$ f& Q/ O    phpinfo();   
  ^& H/ S$ n  B# [?>% P; Z) y" D. ?" _0 N' B$ c1 s
在浏览器中访问:6 `, H3 |0 @% c- O& ?. j

1 X3 P% j! O" k' i/ Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
* Q0 {% Z6 R- [http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. D$ e2 E1 `6 F# rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 W# H7 f$ ]/ x4 K" H# T
说明sql注入和xss已经被过滤了7 J! ^" R, |, J4 g, g

) G1 E8 o2 L- h! `& q6 u, C9 e七、安装过程中排错3 n; Z' z  B# m4 w* E9 s% O* s+ F

" D! O. {. l" x& T6 R* n6 Y) R0 Z1.缺少APXS会报错" t) {- I/ m8 J8 Y1 j4 @1 a
: ^, c$ {, T6 X1 F% I
configure: looking for Apache module support via DSO through APXS
- ]' \( u) \9 y& n5 ^configure: error: couldn't find APXS
/ h' t& `8 Y1 \9 T2 I. y/ {apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。$ Q) u, D) z; k% _& F* h
解决方法:6 t) y) E8 @6 f
, v' _) K% g/ H. O
yum install httpd-devel
2 i; Z5 [7 ~+ l% ?5 E% {* l2.没有pcre) g) t; \* N  P* ~3 H4 |: b( g; S
* J8 q& i. ?3 B1 Z! m! m1 V
configure: *** pcre library not found.
$ {( m# r/ l# a9 r2 kconfigure: error: pcre library is required, w7 p7 a6 k+ R! Q' {1 a& _
解决方法:* H- U5 a3 z, b

5 `+ m; J: b2 x& Q; B: \yum install pcre pcre-devel
  [/ i- c$ C) t/ ~0 Z$ w. z0 C3.没有libxml2
4 m) C7 M2 l! v" B6 W- i
: `+ |- R. X7 v! N  V. b! y
2 \/ W  J) u$ _% \3 @7 Mconfigure: *** xml library not found.! C2 x( r' @0 R9 x+ H9 p
configure: error: libxml2 is required; G% n* u1 B9 w+ g0 ^! f/ f" S+ b( [
解决方法:
3 h3 Y6 V- ]- _, `6 c0 @5 F7 Q* [" o2 a' S* x
yum install  libxml2 libxml2-devel9 r0 l: E2 h7 R( d2 p
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 n' }8 i3 z$ l: {
  ^! o  ?6 ^$ l1 G8 y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
& [7 X, H3 _$ anginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( y1 D# r8 T+ Z2 S原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' p3 l* o# |3 B/ d4 _4 A0 p) J  z9 A0 o3 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ v7 D1 u3 }# F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 Y/ |' |" w: T  P% V( U" T, h
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 }: L" j1 c6 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 g( P% Y4 k" T" f/ q- {% C4 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& e+ S1 E$ J8 e
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 s( J* W( N' c+ V5 g* n, x4 R解决方法,移除低版本的APR (1.3.9)8 R  G4 y/ \  q: j7 m4 z
# e( ?$ d6 U) Y' @) }
yum remove apr- ?' c% q$ K$ X) M1 i
5.Error.log中有: Audit log: Failed to lock global mutex6 e6 I8 `( N) i: D/ I& A

& |' m: d) L8 E/ ^8 @1 ?2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     3 K1 j8 e- c7 M  ]7 T
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: x$ Y$ p4 {9 l% z7 J) `* x
解决方法:
- [! W, N6 q! F, C% n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ s  a+ _" S( d# y6 w2 i" Q7 C1 q

: Q* r* U8 H' X; e+ \SecAuditLogDirMode 0777
! u* S* f8 v: ?$ m7 K& K) ^) kSecAuditLogFileMode 0550
9 E, G0 ], ?2 GSecAuditLogStorageDir /var/log/modsecurity
  C' }; \) H: }$ c% ^# c" g* ^SecAuditLogType Concurrent
( `! E; Y& }2 l& r1 W参考文章:/ n! J6 {  s! v+ F/ P
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX0 d: f% z8 @2 q) X
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-13 14:10 , Processed in 0.055095 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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