找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13300|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: ?9 r) m/ r+ y3 D$ c
2 ?3 P# k& E1 d9 n2 |) s. M
一.准备工作  q7 y& m0 F8 X

, B' H! r) v7 V$ I- A系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.02 |6 Y, E, T/ t6 R% b" E
! `4 E. G9 X' J: U
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 {, t; |1 E. ~6 i1 H* _9 r( Y
+ t/ S: ?: I9 s6 h  T5 {
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ i* ^0 o( f" W. K7 v
9 E' N/ L2 P% W
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- D  O; c. s2 x8 o# G0 o* H# s- I2 j; k& u( Y& K. h
依赖关系:7 `4 L% X& n3 k( b5 Z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ N! |1 d' {% k) P/ v) X
- I" R$ |( F) Vyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 T7 n3 }( H% W0 P' `modsecurty依赖的包:pcre httpd-devel libxml2 apr4 M1 W( y; @- j6 T( y: s

. f  Y6 \. l& l/ |0 T1 byum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel* u. C+ W% R5 C
二.启用standalone模块并编译
/ j* f. m1 R* O+ T- h
$ H9 {7 y# g/ I3 V" h9 n下载modsecurity for nginx 解压,进入解压后目录执行:, ?, \  e9 g! w( b' C1 ~& S6 f

" l/ |( C* c" }6 a3 P. H$ W./autogen.sh
$ C. Y* e1 s3 F$ u) ~' L: [./configure --enable-standalone-module --disable-mlogc: g! J  d: w9 X7 g
make ' X; h5 T. a1 ^9 q; `% D5 @( C& L
三.nginx添加modsecurity模块
' C( r( A% _* C( g: o
# G# t0 W$ {! _5 ?在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! ~8 G; `! y  t5 q
: R) g& A: `/ c* e: I8 z+ s./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 H" g% D! o+ z* Y, f
make && make install
: ]$ v$ C$ `' P9 W四.添加规则# \3 c! \% o3 r1 v7 q9 P" M

% R7 y5 F' ~, |4 j8 Wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! W! f; C5 r9 Y9 L+ _1 W9 s4 k. {( D; c& A3 P
1.下载OWASP规则:
8 G# ^' G# a7 @- e+ k' s! v; @; w; Q+ n' d% a) J, N( v
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" T1 W2 w1 y2 M: j0 R2 V
# N2 l, t7 w  l/ W- _2 h! b: Mmv owasp-modsecurity-crs /opt/tengine/conf/
: R+ m6 I' g: ~8 J# F
8 O+ `) I  x/ `% |5 n7 j3 \1 P/ hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; A9 ?) b- x5 T, ]5 F; E% k; n2.启用OWASP规则:
- i" _( c- I  S' {
. G# j: p  c- ]) R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 A/ B1 k" n% m$ v3 z9 j! D" U9 t& I) b& P1 X+ a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 a8 S# n0 v* x( g' e: j( z
* N6 O7 J: ]8 I; }6 Qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 a: v9 S# g! V, z. d1 m1 f6 C! }

1 ?0 Y& I1 n1 `5 M# {* b- t: rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 e9 y$ p7 [8 _7 |+ r; c. r( T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 M1 @1 L7 J5 ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! L5 `' M% _( B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
- T$ ^1 u. Y+ y+ kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) F' b0 y# K& D5 [; P7 q2 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ e- A/ B. s  Y$ P' U' oInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. f2 b/ R( T4 T
五.配置nginx
! w( p/ a% g7 h* X# y! H. \7 m2 a! T
. d7 C" R( q% m: q8 W) B7 ]5 P在需要启用modsecurity的主机的location下面加入下面两行即可:* P9 z; V& Y& X6 c5 x$ b

# m2 y/ N% M" E" J2 G0 b5 ^ModSecurityEnabled on;  0 C6 `; E' ~8 J4 E0 [; p
ModSecurityConfig modsecurity.conf;3 A( N8 [2 _. Q7 v4 H: A0 ]" V
下面是两个示例配置,php虚拟主机:
$ H3 D: e, e4 F" K+ ?( X% m5 Q& N' B+ r( o: X, A+ w& q* g
server {
, b/ e2 [, b3 Y% V/ H# J7 w: j! A  D      listen      80;0 ]3 j# f$ K, Y  i
      server_name 52os.net www.52os.net;
1 p" B4 V0 P$ M# @8 C' h. W     " e* e/ R1 G' p
      location ~ \.php$ {  l; v* @( u9 c* _
      ModSecurityEnabled on;  
  ^; b) C/ i4 H$ [  K3 l5 o) f      ModSecurityConfig modsecurity.conf;- j2 _! S* T( Z7 n

5 `& r& o9 d' P- `& c3 E" ?0 _      root /web/wordpress;: k+ R$ q( _* Z3 i6 J
      index index.php index.html index.htm;
' K7 t* _7 k( H' H2 k: H' p  
& q: s+ r; a. k' }8 P$ s7 Y" K: Y      fastcgi_pass   127.0.0.1:9000;+ l$ R  ~2 Y5 S+ H8 p
      fastcgi_index  index.php;
! w: g* w$ W; H1 i! m! l9 ^: v7 c      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;$ u# w3 U; Q$ f( w: l6 T& Q
      include        fastcgi_params;
5 z1 o; n9 Z2 H! M, j: t      }) y$ ~  a! ?! X# n% `% @, e" B( n& x
  }
( r0 b2 D" {+ d8 w  `upstream负载均衡:* k$ d- E: l* {- h( l* g5 H
, o% a. j8 D3 u' k9 h
upstream 52os.net {. o+ @: j: `* Y- _. |5 c: C( ~
    server 192.168.1.100:8080;
3 b3 ]: @; T% R1 h- M1 T7 O) }& j    server 192.168.1.101:8080 backup;9 [( m( S) q! `0 T& b7 i( T# }  Z
}6 n/ F( a2 G* Y5 j( v
( }# s! m' F. o& A5 v
server {( M: j0 U: y; P; s& Z
listen 80;
! B. Z  w) V/ M* p) D* N7 Yserver_name 52os.net www.52os.net;3 \9 o$ j2 I5 E# G* l
) P) H' ]# I" C  |+ B
location / {- F, m3 @, p6 Z! M: m
    ModSecurityEnabled on;  
% S  z7 o4 i5 H3 O3 N% o+ P' B8 m    ModSecurityConfig modsecurity.conf;  
1 y8 Z. p' H5 t8 }  [: @0 \, H* b4 N5 R: ^: r8 v4 t- ]6 n0 y
        proxy_pass http://online;5 U3 L, Y1 H& O
        proxy_redirect         off;
5 t5 }$ @- j* N9 L+ `- J9 F7 z        proxy_set_header Host $host;* `2 c- b% d1 c0 ]" }
        proxy_set_header X-Real-IP $remote_addr;
8 t8 s# |' p5 a( @" _        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# D: O* D# _, m
    }
% s( `" J& @  L7 j}1 Q9 N- A/ Y* X6 {1 K" ]* @' I+ B# `
六.测试
( |4 o( `! X+ R5 C& C" P/ V8 M
9 u! c3 a/ V9 t  H, x# v  N我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" X% K9 D) e2 d( I- B
: i  i3 D$ I. D/ K" i<?php! H$ }8 w  `2 d/ }% ]
    phpinfo();    $ ^/ e0 y% \/ n1 V; G
?>: C3 F9 }% M- m7 b% o4 I; c
在浏览器中访问:/ B( ~1 S& m6 e7 _/ C7 E; f
2 \4 }8 e+ ^9 t, ]7 ]# i
http://www.52os.net/phpinfo.php?id=1 正常显示。
% W' ^* Q6 B- a8 E  Ohttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
/ X8 J2 X6 ~$ H, N5 i2 rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 B7 Z. B  m, p说明sql注入和xss已经被过滤了( \+ w% n4 Y" f" u9 Z( M4 R

( r3 G2 h; n- h: ]七、安装过程中排错$ Y9 V& w& T: j* `! R2 G- z( l0 R
, B7 c+ I8 O# e2 E; }
1.缺少APXS会报错6 W5 a) G% {' i) \, v

$ Y/ \* C& P8 H! ~: t5 L$ Iconfigure: looking for Apache module support via DSO through APXS
$ H& z8 a# T1 oconfigure: error: couldn't find APXS
3 S  {# Y- h2 T* Uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) j9 C/ g8 c* b4 b* c: y9 [解决方法:
* e; q$ u3 k" T( V; j9 l
1 [! {$ d) t+ f# X) U0 jyum install httpd-devel
' h5 @# L* ^) Q& @2.没有pcre
  m2 Y8 ^& E& \
2 A7 W" u; K4 J2 r% D' @configure: *** pcre library not found.! q# Q4 }6 K$ R; O, e; |/ e
configure: error: pcre library is required
) R2 P8 N% f5 O* Y4 c6 p4 T8 C1 M解决方法:5 C4 }( _1 ?' s6 t

% G, k7 r9 O/ s! ]' O3 W3 t0 A: G( Gyum install pcre pcre-devel. h: [! c7 ~7 j  g
3.没有libxml22 V9 m, ~( M# d% {8 D3 [. }: E# l
9 L1 e! m) a5 u3 A; W) ]3 U

1 V; i& \- Z9 I) E5 z# z1 ?configure: *** xml library not found.
+ X: d5 }3 b- }7 v1 O0 lconfigure: error: libxml2 is required
( C' y' b$ F5 w解决方法:
- s' p3 {% n- q1 n+ B) b7 X* H; \$ `* \) h+ P& w
yum install  libxml2 libxml2-devel
/ t# h* x5 W. E; @4.执行 /opt/tengine/sbin/nginx -m 时有警告# H$ E: e+ _. d- C* Z. o
! v8 a1 n+ p- q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
0 L5 C5 n! q% ^: _6 }7 O- C* t( mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 }1 \( M1 J; [# j7 K& [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 y: a3 `, A0 K/ r  E* ]' R0 n4 p
: i! @# Z' M) ^0 c$ W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, k+ J& u. j, U! p5 v* f; |2 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( [* @$ O  U' x4 R" p' Q) }" g4 D2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 V, H; }% L' j% j2 Q& E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 e  Y2 h8 M, g* q; h. A& Y  j) e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ n2 A( Y1 A. _( i" [2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ e# k0 c2 l; N7 y6 @
解决方法,移除低版本的APR (1.3.9)
+ H1 K5 p, _) @4 n* C
' b" r$ t% Y2 y2 O& P: Xyum remove apr
, h0 C1 d% V3 m& G7 R- b* Z9 P0 e5.Error.log中有: Audit log: Failed to lock global mutex$ Q( t) [3 a' [) v3 Y
* P- D/ \  I- ]6 ^) Z4 |' ]' _
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     - T0 q% C0 ]1 q( g8 d/ O
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]$ ]. X1 M9 S4 o
解决方法:8 m% m+ k, h5 _: w$ i4 {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
  O7 B5 F3 |: v2 M5 Y! t+ L9 a( h! [
SecAuditLogDirMode 07774 K5 k( A) O; V! s0 x1 @
SecAuditLogFileMode 0550
) y5 _2 T+ D# z- i# SSecAuditLogStorageDir /var/log/modsecurity
( n% k2 m3 ~$ L$ }+ dSecAuditLogType Concurrent! f9 S! Q/ B5 v% [# d
参考文章:" r3 s8 B5 U: J3 z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 d5 O7 s! p! c! Thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-2 10:15 , Processed in 0.072942 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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