找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11951|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" ~3 D% R- x1 @7 F: B" P
/ n- ], E3 X; \一.准备工作( P3 o! l  |7 ^  w
% l" k: S% o$ _+ G/ f' Y2 r; g
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 Z4 h/ G" Z1 J' Z( Y( u1 f, t! ]5 p4 \+ ?+ ?
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* G4 C/ N: K; A$ c0 @6 M; O; L9 [. m+ z3 C# G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: T% [7 @& W  K
/ T; y2 v: f. _  kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 J' g+ n; a5 [
/ a! r& Y) o& m. [) F5 e依赖关系:9 Q1 z8 w2 O7 ]% j1 A8 ]0 M) k
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* ^. J) a0 K, @$ z; r4 m" a. i

0 z# y2 N4 d' ^( Kyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 R5 W7 r& C* I4 J$ Imodsecurty依赖的包:pcre httpd-devel libxml2 apr
+ L, r. \- k( f$ v; C% d
' ?1 f" z1 U/ m" ~8 b9 X9 Pyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& V, t+ Z5 w- B' A) X二.启用standalone模块并编译9 I6 A6 Z: [/ P/ K9 J% U% Q) h
# e3 @8 M) \0 {% l' B; X' k6 d8 Y. ~
下载modsecurity for nginx 解压,进入解压后目录执行:
, P+ U" ^' T% \. O' G. Q
- `: F- k5 g8 Q: ^$ c$ \5 Y./autogen.sh6 V# W8 G) j1 K4 I' z
./configure --enable-standalone-module --disable-mlogc$ y  K! Z9 x5 ]+ v$ C1 r5 }' t
make
" O+ w% |6 ]8 I+ k, p5 X( G  |三.nginx添加modsecurity模块8 J# G! A, S% T" C' T3 Y7 I0 i9 S" K

/ j9 H& r" y- J8 L在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& b4 Z; p5 L" p) v. A9 m6 |+ H* y. j' i- r) ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
8 _$ ?3 G8 u& s1 F# B' Q5 pmake && make install. f0 _" U! h( j6 j- c! {4 I
四.添加规则$ Y4 W; ^  ~' |- N7 j- _8 @+ h$ S
3 G/ ?6 q2 D6 W, _+ c) h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。  I+ I& P' A- s; l

& y, z- \9 j$ i1.下载OWASP规则:  C  k& i$ D- o; p# B

% C4 C7 ^8 s& S0 V& sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
6 S, l# Y  A# Z$ _5 R1 O9 ~& A; [, k9 \  W) D. ?1 ^; Z# M, ^
mv owasp-modsecurity-crs /opt/tengine/conf/
' o$ S7 _6 N  H. Z3 X. H$ F& f* i; X- C( T, V' l2 H9 f# x* \" u7 I
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" m3 B( h& x/ p: Z/ I+ _- g1 [0 e: L
2.启用OWASP规则:. i0 T9 a7 P' n$ D3 D4 J

/ `9 f9 A% V" ?  t复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 S$ P* P2 l* ?1 g; W
$ W( q8 C  \% Q! [) p2 j6 ]* L- e6 c
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 v. ~0 h7 l8 i. ?

+ S8 e8 w8 @6 R+ h% Z& aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; |* u: @% O1 n" t1 |9 j

# v  _3 p5 j8 m8 |8 RInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
3 D! c: b- G  W$ d9 b: ~; P9 B# CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf% m, V! ?, `2 K1 P2 w! @' R  I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 Y% d; x7 l# x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: F) t: q* M+ p! ?! {" F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf  B, H5 `6 a# z5 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ t0 I2 o5 f9 [Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" M. i7 p) ~: c) v
五.配置nginx
2 F3 \1 B7 k- M, A# \7 n% M  t# R; w
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 r2 i  x5 T# Q; Z3 `' z9 [" D6 v! h/ \% k# W
ModSecurityEnabled on;  
0 R) T5 z$ M4 y; }ModSecurityConfig modsecurity.conf;2 t* A$ k  {1 \/ @0 j' n
下面是两个示例配置,php虚拟主机:0 r9 F8 Y% T7 ?! I% j* e

( j( q; ~8 p3 J# ^1 q: Bserver {3 f3 G3 e/ B( E* s) M+ b  z
      listen      80;
9 |* C- t) j9 u      server_name 52os.net www.52os.net;
* e; l+ o# j7 U4 ^6 T  |     : M8 N* L: f3 L
      location ~ \.php$ {7 I  l, k" c4 e" O  M
      ModSecurityEnabled on;  
; v% P* V0 _; K( A      ModSecurityConfig modsecurity.conf;
. A$ T% |. a. I6 ~+ U1 {4 M6 ?! N; o
% G( S4 v7 z' T* {$ ^% `6 ]      root /web/wordpress;
4 u% O6 Y0 d& ^' K( d      index index.php index.html index.htm;4 m) Y$ n- n) K2 i
  
' y( J. g6 u. f9 Y8 \" G      fastcgi_pass   127.0.0.1:9000;
; s  Z: Q3 i  k6 L/ ^      fastcgi_index  index.php;. \4 V# W4 d" E0 M) k  v( x4 a
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;  s- o, S  @% }9 g* V6 F8 w
      include        fastcgi_params;
4 o( N0 T+ t# |) v9 \      }1 x% U2 }3 d) D4 \* S
  }
9 u0 `: _8 Q# h! U$ \- A/ s4 v7 |) jupstream负载均衡:8 u! i+ S+ m. j  r/ T

. S2 D- y3 T  `2 k/ y4 H8 ~$ D' D! Xupstream 52os.net {
1 G3 N8 Q& R. h6 v# W, g) l: u    server 192.168.1.100:8080;
- H* u, h- G, e+ c$ V5 h: P1 |1 @    server 192.168.1.101:8080 backup;8 \& C5 u/ v) g; x5 T
}
7 U" {$ F/ R4 }# P8 q9 [; M
6 n! i* N3 z+ m3 ?/ Tserver {
5 S/ T2 s% U7 E4 I" }/ nlisten 80;5 t$ }8 w/ k$ }, l. i3 l2 ~
server_name 52os.net www.52os.net;
0 N1 k8 d; X, z0 r0 g8 j+ `- }* `8 [7 Y  ?! Y  [
location / {: F$ m, b. u, d
    ModSecurityEnabled on;  ' t# W( U8 }; u1 E
    ModSecurityConfig modsecurity.conf;  7 f5 W+ Y: ?6 B* x6 D
7 e; g! N0 m8 {! P+ Q, u
        proxy_pass http://online;
  h# J  j2 M& s( u        proxy_redirect         off;
" y" v7 a7 q. X* n4 l7 g: V        proxy_set_header Host $host;+ s8 T" {, C" v
        proxy_set_header X-Real-IP $remote_addr;
' a+ `. B. k( u0 p* M- E  x; U        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
! v: B  ~9 L& U& |4 N7 f: ]  @    }! V- o2 e9 N; c6 t. c$ W1 l0 V4 e
}
8 ]* {- @- f4 ^" Z$ e9 d8 e2 j六.测试
6 w3 v" k1 h+ y0 S6 D* B4 b8 C& x1 m8 n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 a3 j0 e; h! |/ V6 ^1 o
" }+ I/ j' m+ P! b' G1 @7 I<?php
# c7 X8 I$ D5 f    phpinfo();    # m6 i: g% ?) y, x, l7 q
?>  a: P" b) {% E
在浏览器中访问:
: v) ^2 ~2 o! q% U4 _
* M+ j+ s! l, D2 B' w7 {http://www.52os.net/phpinfo.php?id=1 正常显示。5 Y9 h3 Y% Y2 D' O
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& Y* y" o9 J& m3 n  Z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 ?3 F1 E2 s0 \
说明sql注入和xss已经被过滤了9 ?9 a; i; o3 j8 X- H+ V0 W

. x6 n* C9 L5 ]! p2 _$ A七、安装过程中排错
  H. Y- A* v2 o& r8 U, c3 u/ M( i7 I" v5 l  P& ^
1.缺少APXS会报错
7 G1 I6 J8 d" w5 ~$ h" J6 z7 i1 J+ V: N4 u5 y, |
configure: looking for Apache module support via DSO through APXS
/ M: q; w/ W9 Z' l& G) N* j; |configure: error: couldn't find APXS
9 Z$ g0 j3 x% b: W4 |. o0 d! ]. ^apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; e3 F/ k, I% W9 G. Q6 B解决方法:- L1 _" U0 _9 P
! b9 O  c: Q" O; x
yum install httpd-devel
0 a% S2 a$ ^; o- V2.没有pcre4 u5 D* t. T% f
3 j- L, ~; {. ^: b& J
configure: *** pcre library not found.. I" c9 m8 t+ M: j- i$ L, e
configure: error: pcre library is required
8 t* `) W5 D4 _& ^9 Q1 d解决方法:
3 s% h: {. }  @' P* t7 k4 C2 E6 N. N' @# j, I& p* i
yum install pcre pcre-devel9 Y: K3 @# K. n
3.没有libxml28 `/ @* C' Y4 v( G

# U* Z" C' `: k5 a- Q3 I: Z0 m( }, @$ n, }* M9 t( R/ ]
configure: *** xml library not found.  T, Z- z; F% j6 B. {& @# j
configure: error: libxml2 is required' B+ ~( g$ O) P0 |; ^( B
解决方法:5 v6 W& v. N- r7 B; N) ]* p

; f+ S1 ?) k; W2 ^% Q1 R( myum install  libxml2 libxml2-devel& @  G+ B/ a$ |# s* C
4.执行 /opt/tengine/sbin/nginx -m 时有警告
# y" m* {8 w7 x) s4 {: ?, H3 h: q0 b( f) o4 k1 y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)  M! |1 B$ p! a5 ?8 K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 T3 ?3 b+ P! b
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log6 E* m4 C8 n7 Q' v6 `& x; J7 ~0 D

3 X% I5 ?) z6 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  g# B% ?+ A) d: G! S4 a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( q. i0 }( N( @) L2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 i; ^$ o  b0 G* [4 _  B9 }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* l2 r: u- y! \9 o" N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ J! v0 L' G( o  K1 ]/ T  p
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 g' x5 q* Z: {$ j1 Z3 Y解决方法,移除低版本的APR (1.3.9)6 c2 O' W% o1 w( ]8 O
/ m7 U" E9 Q, ^4 L2 t6 y2 U
yum remove apr
2 X+ U% b1 z1 O% f; @6 p: f5.Error.log中有: Audit log: Failed to lock global mutex
4 K2 f$ p# w+ i  i  e7 T8 P7 k( _. ~" H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
& K/ r1 i% h; Dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 N1 c$ |8 N1 W( ~3 B% U
解决方法:
0 `$ d& y9 X$ \: W9 t9 f0 z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, ?! `, S& M  E
1 c; I8 U4 F' \+ l
SecAuditLogDirMode 0777
2 ?* N% K3 W9 T$ H9 [3 q0 nSecAuditLogFileMode 0550# P% [  w6 ~# X7 [# H5 f4 x% ]
SecAuditLogStorageDir /var/log/modsecurity) p7 e1 p4 T+ s' d4 S& |# p( K
SecAuditLogType Concurrent
1 B, ^# f9 `4 Z# ?1 ]参考文章:
9 c( _3 u  Y/ Q7 f' ?& m( E/ L5 I. Ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 c& ]- K, P: M2 I
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-29 09:19 , Processed in 0.063121 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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