找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13153|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 O( p8 G. u& }# n+ R
* m! p8 ^: D/ p2 H
一.准备工作' P1 [; K* n  j+ |6 `7 h" \

; Z6 {1 z4 v: `' \5 o$ p- U系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 u9 _  e( u6 r5 a! j, E! b% p4 _# a& Y7 ]5 c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 t1 ?* }( F& H5 o8 h9 G( j
  }% p- B% c6 i. [modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, j$ p, `6 `% }) m% C3 v& C0 i$ E9 @+ C5 N+ ]) p
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 x( u) ]. A' e: F; R, ~' l
# j9 p1 N; w% Z9 F( N依赖关系:% r3 \$ m8 {3 P. n, y' e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
: i3 a# S- r$ x
! Y: w& h0 Q* [+ o0 o2 Z) c9 x4 N6 F" syum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
: }' ?! \6 e# V: s0 Y1 U; imodsecurty依赖的包:pcre httpd-devel libxml2 apr
' [6 @0 Q# ]" ^# e" ], K  H" e3 A; @& w2 b( J8 |
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
5 i. P2 \& T0 h  o6 _二.启用standalone模块并编译
) r8 l8 h9 p  z7 ^. l8 S$ z* N, Z- M# }, s
下载modsecurity for nginx 解压,进入解压后目录执行:( Q# p5 j; a- P5 M
0 P* A: [. x: M3 Y
./autogen.sh
: b; t1 R9 [) w4 a* s./configure --enable-standalone-module --disable-mlogc, I" h- A# S' p6 z1 E% t
make
- a" X  `1 |1 t1 m三.nginx添加modsecurity模块
8 }) m+ X" r; L& r. m
3 l$ J6 Q- h# ?3 \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- p, t# K1 K# T0 [+ {5 P/ U
( A6 J+ }# v" a+ e5 ?. ]./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 p0 ^( [  Y( ?- D
make && make install
% a% a6 P- P$ l6 V1 S( x四.添加规则: d% l6 D" u( N- |  |5 U
, O/ `  u- [6 ?
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, e. o: B2 W; a
( b( m/ S3 ?! c) q2 {
1.下载OWASP规则:
/ I6 i% N3 W# d# M/ s# B) H0 R' e+ m) p' T; R0 @3 e
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ `$ l$ q8 f+ X0 ]. C9 }

0 c. `+ z& d6 i0 C2 Omv owasp-modsecurity-crs /opt/tengine/conf/  b. v: N! v9 d: n/ ]

: K  c! a/ N" c& u4 ~  J% Acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 v8 J' k" B5 v$ x5 c1 t
2.启用OWASP规则:
1 K& q! g) l8 H+ E2 F% ]! U" ]# g' [' \/ v+ S: R/ O2 X
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. S! H6 k+ w1 O4 ]) n5 p% x  J# c; s
( [/ P* N* F* x7 ~1 R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: @& P, \7 X# x7 ?* B9 J3 j
: c5 ]* n4 T, k# _) I! a8 {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  G, w7 d# S2 S8 b- L9 z9 i7 v6 @
5 B1 |& Z; j! \" L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
( ~1 |: Z8 g8 h, h' ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" D' q* ~% Q1 [) n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% v7 B3 V( t$ G' m: A# kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( E9 u' \0 y: e8 \, @Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 @4 }- _) a, _  X! i1 B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 b. Q3 [+ i. @/ ^2 H; @$ b% H* NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 r3 m( @+ i9 L; D3 L8 Z- t
五.配置nginx
, L: V, [/ R6 {5 A$ }7 N. `9 h( _! L; ]6 d3 r3 F' Z1 j' h) |1 a
在需要启用modsecurity的主机的location下面加入下面两行即可:7 m3 r' `# K( h- A- r$ P7 f) W

7 `6 Y5 [1 a. g; HModSecurityEnabled on;  % J3 D/ b0 E" L6 p2 B
ModSecurityConfig modsecurity.conf;
8 ^. ?. W! L# l/ a- w下面是两个示例配置,php虚拟主机:
: _+ T4 t. T* c, n2 r& H3 C$ S  i1 o
server {- }8 u5 {. X/ J6 r! _: F4 }
      listen      80;) d" D( v) T7 W
      server_name 52os.net www.52os.net;
2 D; q4 g) g  R     . c6 l4 L7 l# Z( A- i& k
      location ~ \.php$ {
0 V( u+ }! @( g% w6 T      ModSecurityEnabled on;  
# V* d( G5 n, M. q; x" W; a  E( u! W      ModSecurityConfig modsecurity.conf;& a7 s& e7 U7 A( P7 }+ c, @

0 u; W7 v& c# y5 Y: W) U      root /web/wordpress;7 c5 J9 _4 k4 F' f3 N5 y9 K. T( ]
      index index.php index.html index.htm;- B! \5 s/ Q4 N- D. {
  
4 y3 o% r5 W' f1 [7 k! m% O0 I* E      fastcgi_pass   127.0.0.1:9000;5 J2 M: ~1 y1 X& w* f
      fastcgi_index  index.php;
: ?7 X: _+ e! P/ j      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
" D% g0 g) t  g. @      include        fastcgi_params;$ O6 x$ Z1 v' I
      }
' p7 A) u3 Z, P& z0 M+ C  }
9 q/ p/ C! g7 Q- \upstream负载均衡:! \! a6 h' E8 ?/ ~) q' h

) s! m( V4 J. D8 N4 P  wupstream 52os.net {* c1 G( I& h( y. K9 A& e
    server 192.168.1.100:8080;
4 L& Z* U6 ]2 D1 Y' X5 Z    server 192.168.1.101:8080 backup;5 j: z, m% w0 e& z* V9 k
}( l7 @4 ~/ m- q3 a2 Y) ]# |4 O# S
. v6 X7 H: v- X6 h5 a) I
server {3 g9 D' w; j3 \9 B4 H
listen 80;
! j$ N% r2 W# R$ B/ s0 u' Fserver_name 52os.net www.52os.net;0 u; Y" @0 j/ R3 P& J% w  K: |

4 h: X3 \) H5 I6 ~( Y/ \2 slocation / {- a" Q9 V+ P) T% w% N
    ModSecurityEnabled on;  
" K* Q9 R$ n+ q9 ~4 e, Y    ModSecurityConfig modsecurity.conf;  
! P6 p# V. A) w  O
% q# x) u! _7 P; m2 I. E/ @. N        proxy_pass http://online;
/ `5 N1 U# [" f9 d3 ~6 J        proxy_redirect         off;$ v/ Q9 j: Z3 e4 A! h
        proxy_set_header Host $host;
' n+ ?0 m1 b3 S8 O' A        proxy_set_header X-Real-IP $remote_addr;- _* c* o$ z( d; C* R! [; U4 K
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
. u: l) i' p" N3 j. w    }
( Q* {( \! k  t% W/ x% D. k}" p1 J1 p/ v0 _' n6 w
六.测试& {" G& V3 Z9 G7 m5 N- A
7 Y6 t) B+ X3 e
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 {& i! h1 M( ]! k- d4 p+ d, b
4 U: W+ W( J/ v. ?) _<?php- G+ `/ t, q) U% q6 X1 u( a
    phpinfo();   
9 ]; k0 m' g& f?>
( `" e! m  \7 \; Y# o在浏览器中访问:, L5 I( R, U6 D; [: _. X

6 a, e2 t" k8 H+ u6 ]http://www.52os.net/phpinfo.php?id=1 正常显示。
: d# D2 ]6 c/ \1 jhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 q! W  R5 q' t+ d: R1 F4 M
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 }  W' _9 a3 i% i& ?; q
说明sql注入和xss已经被过滤了7 h. m& b$ p+ N2 {$ n# m

: b! ]! \% f- e4 X7 Q4 d0 J5 h七、安装过程中排错9 Q+ A: R7 Y. p

4 L% V+ z& w! y- [2 a1.缺少APXS会报错
% `. e9 M0 c; ]  O4 L7 Y+ {, y3 Y6 }  Q: o
configure: looking for Apache module support via DSO through APXS  t0 h' }8 o9 e4 c! l
configure: error: couldn't find APXS
$ ]0 h( |; ^" J! I0 _" Sapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 g2 a) a  z6 e6 J, O" s- @2 }6 t" N
解决方法:4 [# l; m: Y5 E

2 N- y, u# I2 O9 c; n+ m9 [$ ?# cyum install httpd-devel8 t4 r; C5 v7 ]+ H# W. s* x
2.没有pcre8 p) W5 }# F  I/ S( f* `, M* u
/ j, y0 B9 a( {! u3 Y1 ]0 K+ {. d
configure: *** pcre library not found.
) |7 M& h6 T; b# o: nconfigure: error: pcre library is required1 U. C* y: F4 k: X, Q* |1 |% h
解决方法:
7 @6 R& r5 ]9 W: K- n; b
  \& _$ F. s. s$ C, w1 Vyum install pcre pcre-devel' m1 n2 @/ H2 O& d6 I
3.没有libxml2
% v# W9 \& x, n  p% d1 E
) Q2 a$ m( m* L0 k
% f7 B8 `: G: \0 Q5 vconfigure: *** xml library not found.+ G& G- ^, j7 R, H" B" S6 _% l4 D
configure: error: libxml2 is required. ?! \0 u) d" d- V' B% B. U3 x! Z' V
解决方法:: T5 _% K! C# S# v

/ p1 X1 B0 p) fyum install  libxml2 libxml2-devel
! ^9 q5 a, U. `: `+ v4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 @7 Y! m' `. i6 i! r5 N' F! M! Z) t2 |3 t; L& c
Tengine version: Tengine/2.1.0 (nginx/1.6.2)7 D. j0 L- U& ]3 |" q; O6 o
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!; G+ i! s: \: }' r4 I5 f( {" q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& [; w7 ]8 X2 G) V( C4 C- \

6 X: d2 ^) a( l  f2 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. d! K8 @/ v7 V4 _: z+ \& r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"+ m3 Z2 C2 s6 l8 R& y; {6 F
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 q7 [4 D% _. j) Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ J( l& v( l& Z; d7 L& r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 c) \. v3 }4 p, t0 q( T
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: ^8 w5 H! k; w. @5 W# Z0 M. w
解决方法,移除低版本的APR (1.3.9)/ P5 F7 |& e' Z! O- i. j$ y
7 ?4 A/ D# z2 L$ Z0 z
yum remove apr8 C/ T4 z4 @0 }3 E3 o9 V/ v
5.Error.log中有: Audit log: Failed to lock global mutex
4 A" O: G7 W& _. T/ O; ?
4 o$ }0 U6 e6 k0 N3 F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     0 K5 \! z& x7 f: ]' @3 |" W
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; f* G! @2 ?" i# ]1 j解决方法:
  Z; o1 h1 A4 A: W* j: C% P/ _编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- w8 `# \( `8 E- @4 m" L1 f! f
' g+ T$ ]& T5 c) m/ X# z& O/ h
SecAuditLogDirMode 0777
9 A- N9 K, d" V$ B& E% K' BSecAuditLogFileMode 0550
% R& s3 w" \; J3 b! G# d2 G) j* p6 @SecAuditLogStorageDir /var/log/modsecurity
5 J. n" u% ^0 Y8 YSecAuditLogType Concurrent
6 b$ N) W( N' U2 t( }参考文章:" G9 y; D9 ^/ v0 I4 ]/ _1 t
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" s& t3 [+ @: W. |, ^* S( Z' b/ k6 Qhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-19 12:00 , Processed in 0.067696 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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