找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10929|回复: 0

nginx配合modsecurity实现WAF功能

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

. T8 x5 v3 \& Q3 @7 k, J% L% e一.准备工作0 w% A" T5 ~2 e& f- Y. _# f

7 P( T7 Q  y) X4 v$ @  k8 ]0 Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0& }  Y$ k) ~7 K# e
0 y4 s) m+ W! o, q2 P/ N
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; Z7 I; |% E  _: H# s" Y8 z; U! z* j- P
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: b8 z  g* o' _, d
8 u  h) M8 d( j- c9 IOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* T0 |2 Q$ A+ v/ s: D( s1 U
1 b3 ?  A  w* H依赖关系:
) P& a( M% N# Q& l+ }8 F% Stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  f5 b: h- L! h

; e4 R0 A0 O0 Tyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
. Z% W4 m, }; X* k8 U  j7 W" Rmodsecurty依赖的包:pcre httpd-devel libxml2 apr, x; c% [2 `( G6 ?
$ u" M7 J# r' l. ~
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
- u7 A' V* d4 F$ b. ^4 y二.启用standalone模块并编译8 i! K' s/ a4 p! C1 M0 C+ H

' ?6 ?5 V4 }4 W4 S2 w% {下载modsecurity for nginx 解压,进入解压后目录执行:
6 j. K. m$ ~+ g" S- e) N  P' [  e! D/ U& M, Y2 W
./autogen.sh
0 b% F& I9 \$ e) m: ?./configure --enable-standalone-module --disable-mlogc( h1 B- P5 |. D6 I6 g5 \; r
make 9 q1 p+ i& ?9 u) J% P
三.nginx添加modsecurity模块9 }  m0 s- y: I( b
2 Q" L0 A; Q& T* D( i; H
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 d. M- V6 a) ?; F
6 P2 S. n2 I3 B/ t% L: D./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine0 R, A: q; I9 d& n/ w4 r5 c
make && make install
$ H0 a0 Z6 u/ d8 {/ k" K( H5 [四.添加规则
( }, [2 y# {& |* ~. d$ v7 p( o: a8 f1 i- Z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; e% g+ w; h8 T, m
; Q0 m# Z+ W" \
1.下载OWASP规则:5 ^# G5 I9 O" Z

/ s/ v. h8 l* o7 n3 Q  }" hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs: @9 B; a3 k. |% `0 L
" g' Q- [" ?. o
mv owasp-modsecurity-crs /opt/tengine/conf/
- `8 h1 W) e* u
3 J2 H# {7 Y' q6 dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# {  w5 o( o9 {2 s$ k2.启用OWASP规则:
7 p* i" x4 V, K4 Q$ c3 }4 h) d
! n! ~8 ^; ~- h3 h1 g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* b7 x+ K  S! h- \. h+ |
% o7 g' N% c; T" i' v6 w5 F
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* G: Z6 L0 s9 k5 g0 s5 L* B5 D$ j/ z$ E( M0 B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- F+ K4 J; A' R! _
% t  n0 Q- e/ i* `* P1 s/ T+ ]Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 g5 F3 ?, W5 R! \4 @3 S+ G; M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# \: u1 a6 f3 p. @* X: e1 K# J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; I% v( q: J8 c" PInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& d3 g* \; O* I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 |: ]. ]8 \# y! g5 qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( w8 t2 C! u6 d8 @% j
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 |# P' M! P; H; V8 V五.配置nginx. I/ ^& [5 w$ W6 m: L

3 U- N. p# F8 u& T在需要启用modsecurity的主机的location下面加入下面两行即可:
6 R+ f$ K. }4 l0 Z$ l( m7 T' x5 m8 q! X! l0 A; u6 U
ModSecurityEnabled on;  
, W# ~4 x" ]3 y! ?ModSecurityConfig modsecurity.conf;
. p3 ^; j& a/ W$ }. Q% }# w9 ?下面是两个示例配置,php虚拟主机:
7 S4 `  `  T, k/ }$ H1 L# n5 X- B6 `# w4 Q1 e5 q
server {
9 s/ y9 Y7 e3 M) }3 D, X+ N      listen      80;  _" h$ r9 u' T
      server_name 52os.net www.52os.net;
6 F2 H! G7 j" [- R( X     
) |3 a  H4 C+ {4 F6 w: k      location ~ \.php$ {2 B5 h- l4 z; z- b4 ~  V. [
      ModSecurityEnabled on;  
& `* d. A* H9 J' e9 ]% a8 U      ModSecurityConfig modsecurity.conf;1 A; |3 N. d$ h( }; x% {* A% |

1 w3 ?$ ^9 v* V2 @" V0 I, Q' L! n      root /web/wordpress;
9 {4 I3 [" @/ L* D, q      index index.php index.html index.htm;
" w+ [9 N% ]. c4 n4 S' U. z$ T# z  % x7 K' q- _/ x2 ^" X$ F
      fastcgi_pass   127.0.0.1:9000;' k% f- N, v3 m2 N! a: d
      fastcgi_index  index.php;
  ^0 M% P6 q* o. x: e      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;4 E) T. x8 C( G3 G6 Y, f5 m
      include        fastcgi_params;
- ^* U8 p# W/ B1 M      }
) n' g7 S5 [$ l: O( g$ b" b: I  }- e( ~" i! Z3 u& U; K# m
upstream负载均衡:
: i. I4 i: q% \0 C2 P5 ^! L! `% C) \( Y2 m
upstream 52os.net {
  P" G5 t. O, _. J5 h( ?    server 192.168.1.100:8080;- n3 ]3 O7 J: H) q8 u6 |
    server 192.168.1.101:8080 backup;
; B2 S0 D7 V3 s: F1 i7 B}# n. T4 w; l! G
0 d& k# @3 e1 O$ `; T$ T: R1 x9 }
server {
/ i7 E7 u& C1 I1 ~; jlisten 80;) N- Y' B" H7 q6 Z
server_name 52os.net www.52os.net;; ], d% i5 j( m8 r4 c7 {
3 C3 E. G2 O2 A: G: F, r$ v! m! V
location / {
( s) _' m8 j9 c2 Q    ModSecurityEnabled on;  
$ W( Y( D, @2 u: H8 u+ F$ E    ModSecurityConfig modsecurity.conf;  
6 v! Y  V! k5 e" S
( D) E' E( `% ?  U+ n% h8 V/ C        proxy_pass http://online;2 H# _' r& p7 p3 Q
        proxy_redirect         off;& _( y2 j/ Z) r& [& r
        proxy_set_header Host $host;/ [5 ^8 c% Q9 @/ e* f
        proxy_set_header X-Real-IP $remote_addr;+ p6 v0 O! A% ?; L4 e, b
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& `2 F$ A9 {/ k& x
    }
5 e* p8 P' y0 q* v7 X$ |}4 X- Z3 l3 _% J  T
六.测试
4 |% ^/ k" o+ M9 D+ v
+ ^8 I3 {. y6 x; `2 c; i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% F) f4 C9 E; Q+ ?7 a

& Q# W1 m' `4 t$ `2 N2 k<?php
6 i/ |2 v$ l$ i) \- }' {+ B7 }) P- h- {0 }  S    phpinfo();    5 f3 v/ c! {" U1 b9 I. V- L! N7 t
?>: j$ K; G7 z( T8 @
在浏览器中访问:
, O8 N- O% l- b% ]$ J3 z; a5 D* w
- z4 H9 k1 x# @2 e. N# m5 K1 N& ]http://www.52os.net/phpinfo.php?id=1 正常显示。
$ [! ~+ `0 |1 E; l7 C( hhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. o9 e' i& r3 g- j7 {
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。+ i7 X$ k8 N$ A7 n" ~5 M1 p
说明sql注入和xss已经被过滤了; {) u; W1 }7 E7 y6 u, f
/ `9 w$ \/ |+ b" T! G
七、安装过程中排错5 \; n* |& H3 V3 j
$ H4 |; E" b% J; d. ]# B
1.缺少APXS会报错6 A. n( W; t$ D  h9 b
2 a5 V2 z. |. o1 ~
configure: looking for Apache module support via DSO through APXS& j" ]% |" r; T3 K/ y
configure: error: couldn't find APXS
+ S. f1 X1 w# W( Kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 d, W- [' {3 O4 e; i9 L  Z解决方法:
  N) T# j, H$ A8 ?9 ]7 m
2 I3 }4 g4 C8 c2 ayum install httpd-devel1 m0 Z  z- H& v# T1 ?, C0 K
2.没有pcre' c, Y% l2 Q* H  V) b# s( C/ ]

- F; `/ h' M2 |3 B- b; jconfigure: *** pcre library not found.' p7 r" W$ `6 `! o& r% Z; d1 `: b
configure: error: pcre library is required
* k0 _6 M! ~% [& o5 C; _: I) G. c. L解决方法:
" z& B- p5 V! H  I+ M3 g( ^  n1 B9 M0 G' {
yum install pcre pcre-devel
: S& |/ _# ]  N9 [! }3.没有libxml21 q, f6 u% _; z& M' q
! q& N! ?- P; v' U
. l# E+ d' v$ l. S( @
configure: *** xml library not found.# @3 \! q9 J* G& G- h& J; _
configure: error: libxml2 is required
3 r6 q$ P; ~8 O# c* h解决方法:4 c8 T- B% d1 ~$ ], J5 P2 S% z

/ c& v4 q$ t1 Z) E6 X& e* y1 w6 E; pyum install  libxml2 libxml2-devel$ }0 X3 m* {) {( P( r: [
4.执行 /opt/tengine/sbin/nginx -m 时有警告) I! d! o+ a3 Q
! l4 C1 P  N4 O! [- }. J. D$ ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ ]& j4 B( {# b, U* i
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!! l: o  m, q  O$ v
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) p$ R# U0 j5 A7 X
- ]& J$ W; T2 j; \& w; \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 R7 j! N& x" l3 j3 v7 K+ G/ P7 i) F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"7 Y. X& e* i9 @5 {/ }  c& k6 \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ s1 d9 D" _9 `% a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. t" {" i0 Y' |; |$ {9 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 a- |* N5 p* g) G
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! _" s) W6 p. f, U% Q6 R# ?) Q3 j
解决方法,移除低版本的APR (1.3.9)
; \8 ~1 t* J; S4 C" p( {& c/ L
6 b! _' H" `& I5 W, Vyum remove apr) x* X  e7 \% P9 i+ f
5.Error.log中有: Audit log: Failed to lock global mutex8 j- s, _4 t( v) @

- b" `! U/ N: H9 z+ t, g2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
- f* @- p0 {+ \, v2 m8 Iglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* p0 N: {" ?. G3 x& U0 \# X  f: V& ~解决方法:
: a$ q" Q1 ]1 g2 U/ D9 v$ z) p/ X4 q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) g; L0 q# X: t. ]# y  g% f
4 d) a+ q" f! T- T3 F: }SecAuditLogDirMode 0777) r! p; C1 J4 q+ p& o) G+ E
SecAuditLogFileMode 0550
) n! o3 P* K9 L' J+ b8 V$ YSecAuditLogStorageDir /var/log/modsecurity
0 c: l& J" \0 ]# ?' w$ q) Z# v6 BSecAuditLogType Concurrent
; A9 `, m9 p( l7 k参考文章:
( P* T; S1 Y% l/ P. I$ f( ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! \5 q, z' j3 }# R" F8 d
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-5-7 07:13 , Processed in 0.073948 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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