找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11845|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# }* z( T! m  m: o% [5 H; f- C9 L: O
一.准备工作
7 {. D5 I0 m( f7 ]( d6 i1 N) |) Z" a$ S, R1 r6 ]  T' Z5 r* H& H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( l; O* e- l% @
& E, l5 q2 m# x* {4 i; n; wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 O+ @  s; I  G# \& k
9 `% x; S; i: G* p/ F0 B* s
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% E8 l% g+ w) [* P5 Z3 U" G% e# L( G9 v3 ^- Y; d
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ g) M( K; J% ?: u# Q

- q' j0 Z# `3 ^4 k依赖关系:& v9 f; A7 ]+ t: s" Y8 e+ y
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 q0 `) \+ B4 T. H' q' h/ U% b0 h2 O0 z- ?7 z8 z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, l" g8 {" O8 Z) h9 j, G
modsecurty依赖的包:pcre httpd-devel libxml2 apr
. D/ e0 \4 @, M
: u! _9 ^4 U6 k: wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 s7 d: z# U: f1 I! k6 O- W. c# o
二.启用standalone模块并编译
1 |) }) L1 v- w) y+ I1 A5 l: {$ V9 u3 |7 \, C7 g8 |
下载modsecurity for nginx 解压,进入解压后目录执行:
* z& ]( Z) {: {% R4 U& L) M* v+ U6 l
./autogen.sh
5 @0 h) d, u9 n  A./configure --enable-standalone-module --disable-mlogc
0 e8 L/ ~6 B& w7 pmake 6 N6 i7 j! n$ j9 }5 x# x
三.nginx添加modsecurity模块
( l& W( n) D1 d6 C
9 b" G* [  Y& e* D: }5 G- S2 ]9 Q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:4 d0 l6 M. @& i$ B0 ~; O

& c8 L6 ~1 G+ d+ t1 R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! F4 @. L9 [% I. K& p5 r
make && make install, v) T. U0 `2 r8 a( {
四.添加规则
9 J7 o: W- h7 e, \5 W3 Y8 m3 E7 f6 d( C& g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. A1 j" S% K4 n5 T, Q  x( \: p9 ?

( c) v  L4 H' F; \" ~3 ^. L1.下载OWASP规则:
( q' R) j2 _) M' N5 z( L
4 T3 M" f, D0 ?6 Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 i9 N% I7 ?! E; U

, v' Y% u( y& H: c8 d; A" p$ Wmv owasp-modsecurity-crs /opt/tengine/conf// E# W4 F1 _% f7 }) |
& X1 [2 Z$ V; e. q* O' v) `' |& |
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 V- I* X: G/ E8 Z. u/ N2.启用OWASP规则:
/ |+ ~* S, k# z
) m. |! e! W/ Z0 _复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# ~. Z* I- j; P5 C

2 X' V" b/ D7 g: R5 y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 h9 N: K4 O+ ?$ v, R1 Y
# r6 i. y0 p0 y4 jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 }, W  {/ L, ?! c# s  @/ k
0 ]+ w  U0 p& e2 n7 Y6 ^) D6 L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf+ D  I$ y* U$ Z) ]  M1 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. w2 {/ D' v/ D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ H( a2 J3 @1 V0 B2 J! f* D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ J2 a" ]- V8 Y& _8 }5 J% N8 X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  u. O( M7 M, i+ y$ P+ _3 {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( `" y2 X/ S# \5 kInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 p2 g/ Y* S0 A: G五.配置nginx
9 N2 l0 {3 r; J$ h* v3 I# t- a) D
! |& T2 T9 s6 n/ A( ~, Y  A) x在需要启用modsecurity的主机的location下面加入下面两行即可:; ~" S9 F6 B  i8 Q; `0 D. ^) C$ \

; q& s% e" ?$ f  k8 J# Y- Y6 uModSecurityEnabled on;  
# V0 Z9 b8 }, R/ `* ~  uModSecurityConfig modsecurity.conf;! Q" C2 n' H4 k) {
下面是两个示例配置,php虚拟主机:3 B8 R: b( g6 S+ {" x

+ ]* E. C6 S  K) gserver {' w, Q1 y8 S6 A
      listen      80;
) P' c  F2 o- T. ?      server_name 52os.net www.52os.net;
! Q6 X2 Y! N' C+ Q. k7 _5 ]     % k/ F: D1 C* @% I( t$ C7 \
      location ~ \.php$ {6 X. v( S  \6 O/ j7 F& w
      ModSecurityEnabled on;  
7 j7 b: K5 k0 n" s, Y% Z) g      ModSecurityConfig modsecurity.conf;
  p) O! i1 s; v3 g: j7 h/ l" b5 J5 [0 v
      root /web/wordpress;
* @- T* K3 K8 q8 c( b      index index.php index.html index.htm;
& y: [" z, I# ]3 J7 ]  
7 s* g4 K; I4 P8 Y1 x; z2 [6 S      fastcgi_pass   127.0.0.1:9000;. \) J% ?3 s; j+ K5 |% m- \
      fastcgi_index  index.php;
/ r4 m, b; |; u8 J      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
( y9 W4 m+ Q- h4 i+ n! H      include        fastcgi_params;
$ ^: A, l7 x0 E. {% K  o/ n      }4 P9 U9 f1 Q% [1 X4 v: h1 d
  }; `, F, H% M/ z# p# W9 I
upstream负载均衡:
$ a8 U; a8 ^: [" a' A0 `
" `; J: n8 y, [$ Gupstream 52os.net {1 b! b1 i& W0 N7 i5 A" k
    server 192.168.1.100:8080;' x" G9 Y3 C. U' H4 b
    server 192.168.1.101:8080 backup;
8 Q, ~6 V9 D  o  n}3 M/ {& a2 X& w* x8 L8 R8 @

# M0 U2 e( x  y6 V, U# m7 G, Nserver {2 k6 M. p7 b" Q3 C) }1 |% Q5 L
listen 80;
; m- _  C6 C2 @$ ?! g# n3 Sserver_name 52os.net www.52os.net;
) |6 L2 u: p  n( X5 p9 ^( u, ?7 }$ s
location / {6 n5 z1 I+ M8 o7 |
    ModSecurityEnabled on;  7 y- q1 V# `9 T3 I& K7 R$ ?
    ModSecurityConfig modsecurity.conf;  
! T/ _. j. z3 c% e1 N  ^
6 m4 D- J2 P0 S+ p% W7 v8 Q, f8 m        proxy_pass http://online;
. M! t4 s  _  o' u# s        proxy_redirect         off;
0 L# @5 G4 W0 @& k/ `/ P2 M        proxy_set_header Host $host;6 X6 b3 H0 r0 ~- g3 R. M3 {
        proxy_set_header X-Real-IP $remote_addr;
* d' ~5 Q* \) b3 r( y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;1 `' S! ^. y. G: N( Q
    }; g. a7 C2 b9 P. E" k
}* |( n9 f: q8 g  s- m/ G
六.测试4 K1 a/ g2 q. M  b4 K
6 k. L) F1 s9 m& I( R2 @8 H9 _
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ v! ?) X9 z% _' x
& ]8 ^. i1 b" r& i2 Q) n2 ?, C<?php5 K- m$ |3 R& I8 ~
    phpinfo();   
. k! |* G8 |. X3 F5 ]: R?>1 G) G4 }4 H6 T: q! n; P- \/ x
在浏览器中访问:  u- W1 |; G  }. h' b( M3 s

* m5 b1 r4 E7 T" X: Dhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 N5 E' w' z; N3 Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ v9 [8 |& |3 T9 V4 N. r
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
3 @) b, I( ?# ~! y( l说明sql注入和xss已经被过滤了/ b. m) Y; x9 c% _
0 G' l9 q0 q* l9 K
七、安装过程中排错* Q4 X1 I$ Z7 ?9 V) B0 Q* d
5 v  F/ ]1 U& Q1 \, ~1 z" t
1.缺少APXS会报错
; a* H2 i- v; @1 O4 {( u
" t4 }- [6 u( P) U/ N1 ]configure: looking for Apache module support via DSO through APXS
0 k3 \0 z: |3 r( rconfigure: error: couldn't find APXS* Y, a/ s3 ~: P2 B
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) A$ c5 I( P: a4 n解决方法:  y# x3 I1 E) S: j+ x
2 ^5 Y& A6 h! k' w3 b
yum install httpd-devel
# o) {' r% j) p0 F  Y" z) S2.没有pcre8 ?( {6 @2 A: n. }( s/ d

" B. a: X1 I) c4 S% ]1 ?configure: *** pcre library not found.
0 J7 W, N- _- r& P! x/ Uconfigure: error: pcre library is required  s  n1 t0 r# q$ V" ?
解决方法:+ ]( `. t% h; R( m' j+ O$ d3 w. V
7 P" n& \4 p& G% \5 I/ E
yum install pcre pcre-devel
; ~4 ]/ `! i& L* Q& u3.没有libxml2
9 q$ b+ f! T9 q  c8 Z) N. r3 z# n7 b

; e( o4 [9 Y# H* o) J7 \, R7 a0 X" N& Oconfigure: *** xml library not found.4 j6 O. n' Y+ e- c! @
configure: error: libxml2 is required. H$ E: y; x. Q  F9 s: P
解决方法:' g' ?* ~3 f: F# g8 n7 q8 m
# h" _) a, F' @% M3 l/ l" d
yum install  libxml2 libxml2-devel3 t' z  Y& F0 V7 r2 \% e& m0 A
4.执行 /opt/tengine/sbin/nginx -m 时有警告
5 ?1 S5 N7 ^6 }8 Y" ~  a' R' {. K5 u& L9 O; w" F5 L- T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
  k: @; r- ?9 g; onginx: [warn] ModSecurity: Loaded APR do not match with compiled!, f( l9 }  m+ c; X$ k
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ e: u0 f; u5 [
: y* u) [9 V! m& |3 U& J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 ^. g) G. y/ W, S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"6 d% K# S1 H+ h6 _; L
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
* J6 g% J5 J, y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" c% G) p; g# I+ q, [. s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 b/ }  T" k4 s) O8 w% D. {! o2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) Q& e0 c2 {7 l9 i- G5 s% H解决方法,移除低版本的APR (1.3.9). U- j2 ?; V# x  |' u2 ~$ q1 i! l

+ @) G2 Y, J- Q+ `) J2 iyum remove apr
6 S0 c; c2 Y& P5.Error.log中有: Audit log: Failed to lock global mutex
; y% @6 b8 k4 O) r$ K2 H
# \! C$ A4 q$ s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
- X& n8 w0 u# A; d+ A  l0 K! pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]! m( q3 E* ~, m1 D
解决方法:
; N7 t* {2 Y& A6 V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ K- x$ t; A3 D4 k
2 b1 g9 {. k2 i5 x# `. K! j3 S
SecAuditLogDirMode 0777
* x1 q( g# J  T; v* I+ ^SecAuditLogFileMode 0550
: V+ C/ v6 h3 n* l$ vSecAuditLogStorageDir /var/log/modsecurity3 n8 y0 D' n* ]6 h: p
SecAuditLogType Concurrent
; x8 K; {2 \% g# ~( c* |参考文章:6 W* d7 ?# K9 N0 V' @
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 l9 x8 \5 @) mhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-18 03:28 , Processed in 0.081212 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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