找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12153|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; V: g( R& ^9 s6 E+ I
) V9 g# D5 X: I5 v  A2 y  E一.准备工作6 l, P3 ]4 @" s5 s% F( _+ E
; }' g9 h: g3 F; g8 ]4 l- m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 y/ Z) U- H7 R. a7 N: [3 c
/ `/ w7 d  {# _  S- @+ W" C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 `* s# k# \" d# y7 [( d  T

3 O9 ]1 K  G' Z7 s" Ymodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 O- T% N) H1 v0 {0 i5 c! E3 a6 V* y

0 A7 c) A8 T" jOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs  F$ _: D+ j( X& ~
0 o* }" n& R# H) j7 S
依赖关系:+ `5 J  M! R( [# n) a4 y6 j) O
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 Y- M. G* q1 T  u

) P7 b( Y! j8 V: h1 S* jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
3 Q+ [. u$ W/ Z0 h3 ymodsecurty依赖的包:pcre httpd-devel libxml2 apr
7 `4 A! ]8 q0 ]9 @+ a/ ^. P' M2 @  t3 K
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel& d0 W8 T# s. P" I+ Q1 p3 H
二.启用standalone模块并编译) X" o( L' y1 _$ q- m

  B3 ?' H+ g1 {; I/ K* g下载modsecurity for nginx 解压,进入解压后目录执行:+ y$ p5 O- J/ |8 J# A4 B3 T/ |5 _$ }  P5 l. V

9 z# L, n# p$ a) K" F7 ~* ]) `./autogen.sh
/ k, V! x) Y! r, j* M3 q, P0 k/ Y& X./configure --enable-standalone-module --disable-mlogc
0 N- f, T8 j0 U0 c+ smake 7 i7 w& {& s$ l) G
三.nginx添加modsecurity模块6 v' m- [+ q9 o( H* ?
/ D1 {! [* d8 h6 @$ H, F7 W
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' Y2 M# x5 e; @( d) |

: K0 }7 S% L0 E5 y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
1 \& n5 }: Z3 ?/ j' M8 L. t. Rmake && make install
( ?. Y2 _3 P' ~4 U0 K6 D. V四.添加规则3 K! D) I9 Q7 {) t9 e$ ?3 O

5 G2 x7 l% Q& z1 {0 Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。* Q1 B9 a% Z# M4 l% r
. T, a; L; c& a* k, u9 Z4 a
1.下载OWASP规则:! w' d  ?) q, d1 s. W0 m
! t+ u2 r9 w; p# k3 r  y$ {
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ E1 O2 X* y7 R6 K

4 X/ {, ?6 n0 Q. k* j, pmv owasp-modsecurity-crs /opt/tengine/conf/
" Q4 H- a$ z. p) w/ `' O# Q7 F: U$ Q0 W6 q# K1 A4 D7 z! F$ E
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# _* u1 [: U! _8 G2.启用OWASP规则:
( y4 r+ v4 i0 m$ b. W* w$ ^5 K  c5 p% R; A
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 ^7 h: i% P  M* M/ v0 b

% o1 V+ k6 _) i  }3 X编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ ]5 z% R. s7 O) @3 t5 l2 o2 k4 ~+ u( Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 L2 R& B: W9 _3 L' J" F. S0 A

: a; t1 c# r* b% l4 bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 N6 E2 N8 [  O$ K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf* E% }4 i/ H% N0 o/ ?4 ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* f8 A$ k8 {8 e3 T- H: a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. P& c( E! e  H9 m$ N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf: W, `' I1 s8 U) o* K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 T; n4 @% @0 W8 q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; c4 J* ~% y! B/ w: q五.配置nginx
4 z) ~4 C6 m  Q& Y# J1 |' a6 ?* e8 `8 k1 o
在需要启用modsecurity的主机的location下面加入下面两行即可:
- f( a) W' X0 Y
* m7 g$ G$ }4 ^0 ^ModSecurityEnabled on;  
1 j+ N% r! m  ]ModSecurityConfig modsecurity.conf;+ z5 X: x0 A5 ^# k* w& N
下面是两个示例配置,php虚拟主机:
% |8 H1 R# o9 v# {" s0 H2 w, p5 z7 S! X  b
server {2 k7 \5 w4 f2 O
      listen      80;
' A! H8 v9 f5 S9 ~5 q, k' e      server_name 52os.net www.52os.net;0 j% u# @9 t) {, J; C6 b+ B
     5 A& a, c/ J0 W  y- O4 j) m  Q9 c) c
      location ~ \.php$ {- [) t$ z( ?7 y( J4 ?: `
      ModSecurityEnabled on;  
  T. m8 i' O- o$ b- I5 F      ModSecurityConfig modsecurity.conf;
# \: z5 @! `# e0 z9 K
/ y1 U: ]" U3 t8 Y      root /web/wordpress;# y) v6 y1 U! q& x" P2 i0 S; _5 ~
      index index.php index.html index.htm;: H1 R2 U/ C0 H
  
; |. [9 t: n% \2 J) {$ H% w4 T8 b      fastcgi_pass   127.0.0.1:9000;! L1 Q( C, r: O% D+ a1 Y2 R
      fastcgi_index  index.php;% w' I. X7 ?! \6 O2 v; X0 t/ B6 x$ p
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;7 m! d  Q/ M/ c
      include        fastcgi_params;! y6 V' N: N+ C% c6 Y1 K! K/ {' a
      }1 O: T' A! \, `. y
  }
( s* Z2 Y: G1 P! H& o/ Bupstream负载均衡:( X8 H6 Q+ D2 `3 X9 l7 Z+ D

: S! B$ r, ?% A: }3 Rupstream 52os.net {
/ J- ^/ }$ B0 ~    server 192.168.1.100:8080;
9 l' \3 m$ C" I% K/ Z+ p    server 192.168.1.101:8080 backup;
3 P- s+ N4 F  F; o}
  M% s8 }, P9 Z3 A8 `# X1 z$ S# M! B% ?5 ?: q. |4 j/ G2 H
server {' v# D  I  `: v! g' Y* t+ z- W  P
listen 80;1 I' x1 H8 \' b2 b6 q& p
server_name 52os.net www.52os.net;( y7 |- Y$ E; u; M

  k& a+ ~1 T) @$ U7 b" S+ Slocation / {
" @* c2 l7 N$ j) U. {( ~3 K" \    ModSecurityEnabled on;  4 |. Q) e; M; M1 c" g! F6 A) K) R
    ModSecurityConfig modsecurity.conf;  
6 Y$ E5 K2 \& I: t1 y( E! K; H5 J: s$ ]
        proxy_pass http://online;: H  |* u- Z3 n( D) [
        proxy_redirect         off;
2 {$ N1 X: f" _3 R* g- I        proxy_set_header Host $host;
" b0 g% E+ b$ L! x# E& J  _. T3 s        proxy_set_header X-Real-IP $remote_addr;
! j! V# W" A1 n  @8 M        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;$ ^# ?8 b) U( N5 ]
    }9 C. j, e- b; x& m6 p
}: U2 C0 `0 q& x) T/ O# \3 ]
六.测试
4 i% @8 [! V* K3 f5 x
1 U" @0 V- g/ G0 X' s我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. R1 t+ y- _: Z, p$ ]# w( [

( k. Y. E5 m' I2 s5 N<?php
7 F) K  M7 O5 k2 @    phpinfo();   
$ C& q( u# l5 U6 F* K' l/ U6 |?>$ K# }9 M% ]0 ]: n% D" O
在浏览器中访问:- C4 _: t4 X+ e3 ^+ G; O0 ^
- S. s* [9 e& a6 ^/ d( M! m9 I
http://www.52os.net/phpinfo.php?id=1 正常显示。# j2 [7 h* b0 X( _; y2 |
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& d2 o9 r* Z! @2 B7 s- T" P% O
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
8 Q5 Y) w3 ]6 h' N1 e: K9 I8 s说明sql注入和xss已经被过滤了
: d2 i+ n" T, Q8 n: i  O6 H1 ^& n  j% I# R# c; h# h! q9 X8 S
七、安装过程中排错
% Q" x, A6 q  m5 @
) p, s% V( U/ |1.缺少APXS会报错
2 ?4 d" _: d3 q9 v2 t  A. N, o6 N6 x% B, [: S7 }9 J4 e  w9 F) R
configure: looking for Apache module support via DSO through APXS
' Q$ i7 x# V, d/ G3 J: o6 yconfigure: error: couldn't find APXS
/ T* I* W7 b) z4 Gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 c' |' H& [1 r- C1 \* Y) p解决方法:
2 J9 [% J8 z! t8 E1 Y, L
! c% K- i' S4 ?7 l4 X6 L$ }, S- Uyum install httpd-devel
7 D8 {; t) c$ H/ [9 p. h# m2.没有pcre3 S( c7 ^% u$ l
4 H* {2 S+ F  P; j
configure: *** pcre library not found.
, j; V# X) c7 t# d$ Z1 ?3 tconfigure: error: pcre library is required
6 J, u' L7 W1 S: p$ L0 H- W解决方法:
" U4 I3 G6 i8 [3 U5 I( z: s% r$ ^% Y0 Z
yum install pcre pcre-devel$ B) O! ^9 i. J5 b& h! _
3.没有libxml2
' C% q4 {. @/ v: Z  r; @/ `- X; H! T" \, i  b: F# K- V1 ~. N3 t

# o/ \2 P/ w$ _3 d' f( Y5 {+ f$ T( Uconfigure: *** xml library not found.
7 v3 i. A/ O/ I, z7 i( hconfigure: error: libxml2 is required
9 O, x, e' G2 d解决方法:
9 k1 l$ s  g6 g4 Q& W# F$ b& A; M( w, W! W6 F& b6 b
yum install  libxml2 libxml2-devel
3 U. d! i7 g% J7 b/ Y4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ t, b3 W) f1 N9 Q% O8 i+ C
1 d. r% i4 W0 {' V8 iTengine version: Tengine/2.1.0 (nginx/1.6.2)6 Y1 P  b9 i! N/ m3 f" z& k8 }5 ]6 `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 j7 w: K, |' C7 z/ s& s
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 J7 a- o5 U! l
: E8 V9 J* k" A% a( R# u4 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& L3 `8 X* _# C" p9 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
2 G, B% e% ^0 r0 F0 c: v2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" N2 \8 M0 z# J' k2 m( e6 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 P% p0 Z' h( K! ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 g; q8 n, b* ^2 @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.) N- W. z6 o/ ~% j) _- V
解决方法,移除低版本的APR (1.3.9): t2 V7 h3 S$ O9 H
8 I  x) ?% _. B* }7 ^9 L) z! l
yum remove apr
4 F5 l* F6 l$ X3 ~5.Error.log中有: Audit log: Failed to lock global mutex! |( f' g; e' M
  I/ x" W/ Y" u& s" ~( x
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     " l1 }+ V" C9 l8 y# d! n
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' G& \- ~# Y8 j/ \& B
解决方法:) d% m! O# m9 c: [! z) {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ c; P# q- I) Z# G, X& `
$ c: O9 ~: Y& g# q0 f8 ~! L0 FSecAuditLogDirMode 0777
  w5 w/ F6 z* E, e0 b: ^/ z: S1 @SecAuditLogFileMode 0550# u: P3 o5 K8 J. {* c* b# J
SecAuditLogStorageDir /var/log/modsecurity4 I; G, q: J; e& j3 M( H
SecAuditLogType Concurrent8 l$ D6 A# D# j5 n4 F. i$ N1 \
参考文章:
9 A- n. P" T& U! f, ]https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
0 @) A* a" ?) vhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-14 12:35 , Processed in 0.038106 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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