找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9835|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: i  ]# X9 X/ \) I  f+ O2 c0 N, l! z* n* p. {" N) S. s2 U% |
一.准备工作6 @8 q) R' E+ A5 Q" Z" C

% |0 ~6 G/ K0 e9 O  C- f& @系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. P( y% x7 H, v! c0 F& g! d* G3 ^

& h+ J, M/ G# U; {* K5 H* ^8 ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 k6 B/ L; [6 |) F$ S0 H0 I2 ~$ i. a0 _6 |) d- A- M, E% Z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz1 W0 m  b4 ]$ l9 a
9 P9 \" {# T4 y/ S& T
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs0 H1 k' k) j. }+ L9 L$ _7 O

3 c# ~& H) k* Z$ _4 s依赖关系:1 z0 \4 T7 J. z! n  e1 E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 |6 c9 S( ^. p- o+ X6 N& r4 K4 O( x. Y, [( y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel0 V& V( `) B% W# T; a5 o
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ c! p& V' U+ {, H" @1 p. u; m3 f' _+ E
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel5 p! @, [3 W* b3 ~* X9 @- Z+ D+ z
二.启用standalone模块并编译
" t  ]. g' q, r" g5 L5 _
- F7 o7 m1 k3 W: @. R下载modsecurity for nginx 解压,进入解压后目录执行:
  r2 D9 b3 A& O$ w
- Y  Q6 x: d7 Z* w./autogen.sh; q. j- w' F% _; E$ }$ T7 i
./configure --enable-standalone-module --disable-mlogc
  L5 d: y- M- @* U! ?make 5 O4 f% w3 ^! [; H9 `1 C" Y, z
三.nginx添加modsecurity模块( w/ \. f# t( P3 {7 H5 P
9 h! a- F5 s; ?+ p
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% A3 N% O$ x" @; s! Q# h+ {+ [- t7 A6 s5 O3 U: P: \
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ w! U: t1 X( n1 P# I; zmake && make install5 B2 `0 F- |8 b8 }; L* s" w: C1 A
四.添加规则: ~3 Q$ P0 `0 T+ A  T

; G* }8 i( v9 [. ^) g, wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 p2 ^0 |3 I$ w/ c, W
7 B) v; O, K) D: ~& u  k# A
1.下载OWASP规则:" Z* U/ Y8 s9 |
7 U2 f0 v8 ]! x( s. f; @7 P
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' G7 E! ^9 Z, l% X6 ^
  ?% S' C' }% n
mv owasp-modsecurity-crs /opt/tengine/conf/
9 @# d8 P7 l5 w! E7 y% F
$ F" x0 N# ]1 {/ g" Z" D, jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ f+ P: X: b' |( @: y2.启用OWASP规则:3 a, Q' U6 R  c" {5 o3 @1 u
8 M1 p* P6 V! Q& j# y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, q0 X: E& m! A! S
1 T: {) B* W* e5 W7 Q) V' ]编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, z' z1 g! G; I# ?" f" [% c! w
8 A6 K- ^0 b  z# Gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 V) b/ s) q+ c0 L7 m
. b+ t9 g+ M9 a" c$ C
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 c& P/ [7 y( n9 v' F) d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* A' e9 f6 b6 e8 nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 r: Y5 O- f7 [6 m: c  JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf% L2 [1 z& |3 V: }3 M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' |2 b* v9 Z1 Q9 D$ ^& \+ C) LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 ]3 L  q7 K8 KInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 b1 Q! W( y, R" C/ p4 s: L$ u# d
五.配置nginx, I# `/ |& Z: E& k/ E/ n0 u

( s6 p5 {9 L7 Q( K* V) M在需要启用modsecurity的主机的location下面加入下面两行即可:! j4 Z# Z8 P" |9 N; Z) F0 ?. T
8 e3 Z# M+ ^0 m2 q6 E: Q) F* V) K& N
ModSecurityEnabled on;  4 b; l1 c- `# V. g/ H1 S" |& _2 D# @
ModSecurityConfig modsecurity.conf;3 C* u' g% M- F. W* s+ t
下面是两个示例配置,php虚拟主机:. x) V# \* S$ l  ]# |3 o' D7 E
( F. p5 x& a( H! J6 M  l$ \3 x
server {
  d4 R% V2 `; p) l! H      listen      80;
" X  [4 n. N) w) M7 r+ [7 ~      server_name 52os.net www.52os.net;
0 f* X( r1 {; |4 }  X$ j1 F5 h+ r8 B8 ~     
1 P9 [/ @, |7 y' c. c/ [$ [      location ~ \.php$ {! m" u, q) \6 _6 M
      ModSecurityEnabled on;  
+ g+ J5 _* d9 V3 Z      ModSecurityConfig modsecurity.conf;0 c, ?* w3 }9 O
& b0 q& t5 m* Y& m
      root /web/wordpress;
0 C6 X3 c" K$ a) h" V5 q$ Z0 M      index index.php index.html index.htm;& v/ f+ M+ I: o% M& m2 c
  5 J6 S, g! l# ?$ G% `
      fastcgi_pass   127.0.0.1:9000;
$ U: v. x8 x- j5 o8 h! E, ]3 U2 _      fastcgi_index  index.php;" E4 f' _7 p* s/ S4 g
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. a8 ?. ^* u! E+ ^; e+ _
      include        fastcgi_params;
4 T7 L: c( S4 B6 `! O4 Y      }" K  V" g0 }1 q" V2 e: A
  }, N; P) L4 K7 G) E$ S, e! e' {
upstream负载均衡:: W3 L, h& U0 M& ]
7 l8 S9 E$ Z& W$ Z7 e
upstream 52os.net {
' S: p  x8 w- p# K3 O    server 192.168.1.100:8080;* T' J$ g/ }4 b, r. [
    server 192.168.1.101:8080 backup;
9 j5 ]. H4 h  [, ^; m}
  E' I" `1 t# V. S. d5 x7 o7 p2 c! E. C! A' d0 ?# E% C4 ?
server {
2 K# O) P* d3 D2 klisten 80;8 Z+ d/ [2 P1 |( q% M
server_name 52os.net www.52os.net;8 o$ a/ i' e2 V

  [' t$ @( `5 ?/ b# _5 A; clocation / {
4 B" ?; C4 o% E" }  @( t: B3 ?* T( B    ModSecurityEnabled on;  # e9 c2 m* q0 x# G, z  d
    ModSecurityConfig modsecurity.conf;  
4 u$ z7 |' x9 S) N. Y
' W8 a2 b- S! O# A  `        proxy_pass http://online;  D0 X/ ?9 q4 ~% ^1 @! c6 p
        proxy_redirect         off;
/ o1 H5 t% N. t, O( Z! w0 Z        proxy_set_header Host $host;7 {4 B7 ~" Z$ U) a7 X
        proxy_set_header X-Real-IP $remote_addr;  I$ z3 A3 G5 m2 ^
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
+ S/ u5 c3 B6 l% s4 z    }* p. S7 d/ u1 ^) G* O  E
}; w& R- R! y* f5 \" f9 r/ U( L3 Q
六.测试0 }7 P  v: z3 r/ k

, G9 M2 ~% S( `5 C我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 T" e0 v: b8 A& P2 |) C" v( x/ ^) S* u9 J2 E  p! n' W
<?php+ c3 M. t% q7 x9 J9 ^7 c
    phpinfo();    9 Z+ z# x  \/ N6 `! m$ V* a
?>
2 s0 @  Y# v; s" P5 \7 e2 `9 O在浏览器中访问:0 D  p3 w6 l. y
! _3 q0 [. Y( Y  ^8 E1 N3 b. _& P. X
http://www.52os.net/phpinfo.php?id=1 正常显示。
4 X) u( |+ d# c% T% c! u  k% uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。4 t+ I8 x' P7 l$ n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。# b) {" D) E( G$ q
说明sql注入和xss已经被过滤了
  x/ F& P! C( [6 U; l
6 g$ r8 N! B4 O$ H* g$ `4 n9 V七、安装过程中排错
% U* D( D- V1 U& K. @% T! y! m2 l
6 s; P! \7 A  ~# ?8 E1.缺少APXS会报错0 g$ [3 P8 C! ^, S7 t6 r

" u& Z, y$ m7 m* u2 Y9 ~configure: looking for Apache module support via DSO through APXS
* ]" L; e7 j4 Fconfigure: error: couldn't find APXS# D% c- t% u$ p+ h8 Q3 J$ K+ G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 k/ t' K. [3 d1 w& j9 \$ f解决方法:6 Q9 O  @" J# d
5 G1 v* }9 g! e6 ~$ x  b0 s' `
yum install httpd-devel
5 i* R+ A+ W" G5 q: s" v2.没有pcre. h! I! y* }$ i: K2 U8 B

: Z( c( _8 W2 n! oconfigure: *** pcre library not found.; H# a& J8 z% A
configure: error: pcre library is required) H  _- H. E4 f5 k1 W
解决方法:
7 \8 j4 }" _$ T* z. ~* z' ^
" }& p+ A' u* Yyum install pcre pcre-devel
+ g1 o$ z$ J7 E! }. _& j2 j3.没有libxml2
# t9 \: B- ^2 H; A8 m1 O  D: [) G: e. U- Z9 m
6 V" \7 R0 K+ N# r! G# l. ^
configure: *** xml library not found.. Y: [4 w* p7 n0 r/ V
configure: error: libxml2 is required9 _  ~( f, i  k8 G; {; p
解决方法:
: r/ a9 u: [* x* O- Z- O* b* f, j4 f, B0 T& P. R
yum install  libxml2 libxml2-devel
6 I8 E% T; Z8 P, \5 n0 w$ [; U% o4.执行 /opt/tengine/sbin/nginx -m 时有警告( ~1 f+ ~0 g3 j* N- Z

$ `. p, i- c0 T  i  R% g% YTengine version: Tengine/2.1.0 (nginx/1.6.2)# W9 H! c  g5 r, A- s4 B  R; c
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 T7 e/ W2 D$ }4 H1 {原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ C+ H: O; }5 V& ]5 m/ B

8 C- l! v1 l. Y9 a" G& O* I: ?/ m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  }' p" C) ]7 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 g  g5 e) ]& ~0 S5 {2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. B7 d, J1 N  J# V2 J& f  G0 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ J" G: E0 N0 [! t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ K' B( ^) N( Q* |3 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! I, d0 L0 y! \; F/ B8 F5 S. F5 i
解决方法,移除低版本的APR (1.3.9)& x  L! @0 g+ f4 G  d

1 v* q# |& l. N6 Myum remove apr% l8 k2 ]/ i6 A4 c+ G+ m- c
5.Error.log中有: Audit log: Failed to lock global mutex2 n9 g+ g! C; W! ?7 a1 y8 s( f( O( U
! m5 y% s/ `- P# d
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
1 E* {' I0 Z, gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. p" s4 D4 f- W6 ^4 ~! z' [: o解决方法:' s$ ^, I+ j8 d
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:/ F& ^, E* r5 q. ^3 z8 v" O

! y! }) H4 I  y, m7 W, sSecAuditLogDirMode 0777
* W. W9 g2 ?. @3 J* L" D5 JSecAuditLogFileMode 0550
5 S4 }, D! `; f. p- }, ISecAuditLogStorageDir /var/log/modsecurity
! T0 \5 ], H4 T3 S+ Y1 WSecAuditLogType Concurrent
: P, o9 j6 q( p+ j参考文章:5 _) k3 j- F' g, I% W" w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& N8 X8 U( W6 L/ ~4 W% jhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-5 11:12 , Processed in 0.066430 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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