找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11719|回复: 0

nginx配合modsecurity实现WAF功能

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

$ ]8 n+ Q2 a3 b5 s一.准备工作1 l# S5 v# ?5 v  A0 o7 u3 |

% o0 M+ G: `2 d) y, ~4 |3 o" ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( B4 R. n/ g" z. x9 e
7 p4 U( f3 T9 i# i' xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 `9 `$ _5 {2 i7 j  B# e$ g2 a; R, K8 }4 Y- ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ ]: S2 t) h5 _2 P  b; K" E  I2 \& k
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: v2 k4 M6 a. q9 ]. m8 _! R- J
- p& }/ K* S  D* i依赖关系:) _& e! P0 F6 |- G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. e5 A- N* C1 }% F+ l2 q

- _2 K2 s" W  ~/ syum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) z- g5 u, S# j3 @modsecurty依赖的包:pcre httpd-devel libxml2 apr$ s5 ~7 j( Y, K

) G5 h) @& v6 k6 l) h9 cyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel& \% O3 R3 M; U- z& r6 @
二.启用standalone模块并编译
' d# H' L" C. X9 m5 G1 Y
# ]0 z( Q4 o5 E, S! i下载modsecurity for nginx 解压,进入解压后目录执行:4 W# m# v% ?' H0 x/ y. ^* P, H

6 p. T+ ^7 i8 {) j' L* e; S/ ~) M./autogen.sh5 X( m' B0 v# r0 V4 y- r0 q* J. l
./configure --enable-standalone-module --disable-mlogc
" X" b( ]/ P- M% wmake
; {# ~8 ?2 l# d三.nginx添加modsecurity模块
4 }7 B6 b- s) i* a5 [7 l0 |8 M* e0 }
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: H4 e1 @) r! `- S/ a7 d* E
( X  Z* a3 }- q* X; X./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  q/ m& D( C; j+ z0 n3 _/ ^, j
make && make install+ ]: G- c- A) G# `9 |
四.添加规则
  J, i# @3 u6 J" a
# G( T, a0 c6 A: _! y- o4 ^% @" c% jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% y/ G  J9 X, z/ S5 R/ s

" V: h$ t. l3 p4 r# `1.下载OWASP规则:& y) x/ O& E" ^4 z2 Y

2 b( p/ Q8 {. w2 O9 ~git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
  T9 i4 a; B3 l9 t) [% {5 ]" y: G6 X6 x) r
mv owasp-modsecurity-crs /opt/tengine/conf/
- {' r: B5 Q8 M- [9 Y  x, A' ~  u* V% r$ g' s/ X# c) M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 C: V" [( {0 G* u2.启用OWASP规则:
) E# B1 F% ?% w7 R' Q$ B7 w9 w, d( Q5 M% |: G
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 e& U( P- f) `" L
7 X+ l; f+ g3 L* U9 q; n
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, U) ^, p5 R, n
' c+ E6 B' b& N0 F0 {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
* o+ z9 ^3 v/ `0 c$ h: F. A9 g0 R3 S1 v% S- b* g% R
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ m5 _# f; q6 S: |; ^! R& `) G- v5 ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 v8 }; W4 K' W  S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" B: u5 V, ?7 n' n; }; ]3 M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. k& d6 f+ t7 P9 }/ a  x5 @5 }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 n8 \( r6 x/ A5 G5 Q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf2 n6 f0 z! Z: F  F( x
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! J/ _' Z8 Q) H; U$ u  r' Y  u
五.配置nginx
& j' h+ |5 j& B* B1 i7 F  f0 o1 x3 {" m! U" y5 j
在需要启用modsecurity的主机的location下面加入下面两行即可:8 ^' \5 `. G& O( S
% `- o4 G6 j4 r! r2 \( K
ModSecurityEnabled on;  
: O4 @9 f4 O3 x+ d4 Z% zModSecurityConfig modsecurity.conf;
% t3 l, }1 F5 g# @' l+ z- |8 I下面是两个示例配置,php虚拟主机:
8 F0 P5 G' @8 S. ~% K; U2 {6 m6 h6 _  w+ W. ^
server {2 F" ^9 w& z+ o" t
      listen      80;' H9 V( J2 X+ _7 B/ ~. x; B! s
      server_name 52os.net www.52os.net;# @  G  _# l. d' d% y! c
     
# E9 K; B" [  F' |" n      location ~ \.php$ {
' c9 F4 B  M$ q: M# o      ModSecurityEnabled on;  , R, g7 j9 D# k" ?* v/ R/ Z
      ModSecurityConfig modsecurity.conf;
$ H7 P2 _6 m. D9 \! d( K
& a) l0 Q! s3 N% R6 ]7 X. H8 `      root /web/wordpress;5 e! a: x1 X6 @
      index index.php index.html index.htm;
  l. F6 x- t  U% Y( M* b5 s  & s! @* R( A; P- T, ^
      fastcgi_pass   127.0.0.1:9000;2 \" l! B" R$ J' a1 ~9 j
      fastcgi_index  index.php;" o  g. n! T1 j" z' [) `  Q* Y+ Y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
' n3 H" _$ {2 M5 }6 H, Z1 |      include        fastcgi_params;
- ~! W1 L6 }+ a2 G+ _( n3 n# d) C4 k      }2 x/ p, f* v& {( V( ^! ~
  }( N: }& Y2 u' n7 [0 e) }1 \' q
upstream负载均衡:
# f7 ~; \0 V* U" a9 _7 @* ]
: g! N8 A# w7 p2 x0 ~; D! }- {6 Yupstream 52os.net {
" I8 L9 N5 `: V' `2 {, h    server 192.168.1.100:8080;
  D( p8 y2 b" y0 i2 |' E3 j    server 192.168.1.101:8080 backup;
5 j  p" F) n# {; X+ V}9 [/ `9 V8 ~# l! e+ I2 U0 G, q0 E

/ W+ E2 x0 e1 q' i0 Vserver {
# H/ v8 q: C& ]) \! Tlisten 80;$ M# ~: d7 W7 ]* W$ b* E( z
server_name 52os.net www.52os.net;
. ~* D+ M7 u+ v9 y0 ]
1 Z& H1 E% e* q6 q6 V$ Elocation / {+ j; ~" n1 Z$ k: M8 S' k
    ModSecurityEnabled on;  
. w; d$ d$ N0 s0 d6 ~1 f    ModSecurityConfig modsecurity.conf;  9 N/ \$ _/ k& ]
. \" X6 _5 ]( K( j0 P  }+ m
        proxy_pass http://online;' S. t% j+ L2 j( X- Y3 q+ M4 |4 p. X1 x
        proxy_redirect         off;0 Z( T% J( ]  b& y$ w
        proxy_set_header Host $host;$ }# b0 a" R* ~0 i- V) W) S
        proxy_set_header X-Real-IP $remote_addr;
9 f( X0 ^4 l% E. u2 @) T        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 b& V# |. D3 u! k. D$ i    }/ k- j/ o* @0 t! y0 A
}
9 C5 U2 B9 B3 R8 o六.测试; V5 H" s4 z: y+ z- _

' Q# c4 i8 _5 W  L我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ K4 o- S' n7 }; X. S) [0 f
5 l+ Q2 V- ~, A; K0 X% }# p<?php1 X& K+ y+ _$ F8 `
    phpinfo();    + d/ O' }9 G( v5 Z4 _+ {
?>& k7 U8 e' [! B$ ?9 N2 u  {
在浏览器中访问:& Q) L0 I% G! U
/ |1 g7 X& U, \4 T% I
http://www.52os.net/phpinfo.php?id=1 正常显示。
1 B% [7 X5 ]1 Y9 Z+ K0 j$ v9 Qhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
4 K( a1 f5 f+ ]/ ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' f8 z2 e: x& ^
说明sql注入和xss已经被过滤了
# i9 p0 L" F9 g% e) W$ A4 p: Z2 d$ Q
七、安装过程中排错
( h! x0 M, S! O9 c- u  @  J: e( }+ X" _* F# F
1.缺少APXS会报错
6 ?0 c/ T( s% X9 h! w. X: \
7 @( }" ]0 `, b$ Jconfigure: looking for Apache module support via DSO through APXS
, l; h/ [, ^6 }5 @* }3 kconfigure: error: couldn't find APXS
. c8 ~5 i$ ~+ V3 Bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) p9 T' K6 X# U% k1 q1 y2 |$ ]
解决方法:3 o* Z& J7 D% O  Z2 ~& F0 H
2 [5 |0 d$ \8 g  S, a/ p
yum install httpd-devel/ ~2 g, @/ t1 f
2.没有pcre! A' [1 L6 t" d5 `' `7 X& K
# a6 `+ `  I" k' U: A
configure: *** pcre library not found.
8 D1 g# d/ T- Z4 y' Q+ T& A$ J# ^configure: error: pcre library is required0 Y7 P/ G. ~8 P3 h4 S, f: e( q- M$ a
解决方法:- e) r! T0 k( D8 U( B. H8 I& d

5 Q5 n2 {" D0 r  ~) ]yum install pcre pcre-devel
5 u' t5 c* ^' ~& k( k3.没有libxml22 v/ J. K( \( z! E: u" W
1 g3 d0 `# K& J# f; u7 P
; w5 |+ v+ b! h
configure: *** xml library not found.4 r! [  v0 j# ?# K% \7 d8 M
configure: error: libxml2 is required) j" Z8 H. p- |5 p& W9 Q
解决方法:, X; N# _# J8 v; g
+ Y* F* {4 j  p9 `2 f2 r! T6 d
yum install  libxml2 libxml2-devel
6 e, g. _5 [: n9 V; D4.执行 /opt/tengine/sbin/nginx -m 时有警告% A, f% Q, f; v% a  C
' E) y& i$ R% _/ K
Tengine version: Tengine/2.1.0 (nginx/1.6.2)1 ^  b7 c  v9 z: P; z: ?- Q! M/ c# `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 ?  {% R+ G$ J' d9 z1 M5 t6 k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ R& ?5 \: G& ]

1 u# n. K% S$ S$ k9 J6 H% t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- l3 e& E# w6 B, h- Q7 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"* I8 L; N1 U+ x( y8 m* A( O
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) f/ ?; c  ~) F1 [/ o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, t% e8 q% ]: a8 K7 q% `: Z# S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 G% c- Y* d' ]1 A6 `
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 L/ W! \  K; n; z6 G! o
解决方法,移除低版本的APR (1.3.9)
$ x! m/ {9 D1 b9 h8 c
6 @" G' S( D7 ?6 z+ m, Pyum remove apr9 v1 @8 P# z5 H( k% T" R6 u5 Q. U! `$ Z
5.Error.log中有: Audit log: Failed to lock global mutex
4 I" X  @: B( d4 l) Z) t4 ^( R
+ Q- x1 k  x$ O, R) `1 P6 |$ d2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
! Z" Y. w! a& B/ _4 kglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 M* P! P, V7 I! R7 ?解决方法:5 }$ H# @1 h7 D6 s! l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
4 X+ z4 I4 {- r, a" I
. \3 u; u: Y( z1 vSecAuditLogDirMode 0777
0 V0 i* Z. M( HSecAuditLogFileMode 05500 k6 z5 l9 b" J! O
SecAuditLogStorageDir /var/log/modsecurity
2 h8 V( R# A) q+ f; oSecAuditLogType Concurrent8 K/ Q) u# J4 k7 O. {4 W! J
参考文章:; V7 i3 ^7 T# p2 f# j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# I7 S4 ]* }3 @, L9 U% ~
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-8 15:07 , Processed in 0.075160 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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