找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12249|回复: 0

nginx配合modsecurity实现WAF功能

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

% r2 O/ g5 A  ?: [一.准备工作
7 F2 [, l* ?) f
4 x  D9 N1 x4 z8 N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 I" K0 ^. G# ]; `
1 U* l. C5 z8 t1 Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ B& ?, a& Q2 s( C9 N
' E5 P: i4 ^) h7 p4 umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" E$ C5 _0 z( d& Z1 ]( F4 w" \1 K/ ^$ y5 t9 D
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 d+ H2 c0 w  D5 Q0 G; J2 W2 Q& O3 C( J" }5 y3 I6 B- {2 k
依赖关系:6 q0 |- c: l( {% P4 L6 ^' _) Z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  _$ g! v' _6 I. ?: X" r; `

; h* W9 ]% V' L# i6 ayum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" @3 V* z) s8 B& h: T
modsecurty依赖的包:pcre httpd-devel libxml2 apr! [2 q; |2 M$ w+ z% H
6 a8 i0 m0 }( j6 x) r" Y
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel% L/ r+ Y, t1 r" T- ~
二.启用standalone模块并编译
. |7 t+ r8 y) L; X6 E0 V; p$ X
) f+ f" P1 z% e* s7 m$ c  K; X9 F下载modsecurity for nginx 解压,进入解压后目录执行:3 G+ l" q% c5 P: i8 v+ B5 g
3 \  L$ ]3 H. ]% }5 w3 M) l
./autogen.sh
, S% S$ |& K$ P: `$ |- ?, s./configure --enable-standalone-module --disable-mlogc
) o2 [  p/ b" r  W) L( D- zmake ( E; @4 u/ ~! K+ ^5 _3 U5 i
三.nginx添加modsecurity模块  W+ t) l, v" {) C
  G. P7 [' f8 ?3 G7 |, Y# g" y
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- r: s9 D+ h! K' h3 \$ K
, c6 B/ B9 W/ U2 d" D7 H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
: W( ?% ?! ^0 n1 Rmake && make install  f. G* U" V+ O, E0 \7 s! f& {
四.添加规则! w' o( [' o1 Z% z3 Y+ x
& x. d5 a$ C. j) N
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& b! e6 f; H8 }6 O& Z& ?$ s/ I/ B+ A. N& q  \: E' C+ x$ R
1.下载OWASP规则:% N; }5 w: k9 J# j) q9 G* ~( {& A
& j: U2 V% t* E( j
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 b) P. d, u: V9 m: l

0 b; {' S8 K- e6 U* e7 W! kmv owasp-modsecurity-crs /opt/tengine/conf/% g/ N/ Y8 S8 D4 e) ^
" G# U  C1 L# E1 Y
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! \$ L/ I3 p% @! s4 E& ?" L
2.启用OWASP规则:% ~1 Z4 W% u) G
8 k1 t" S9 [* x3 f
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" J/ l! d/ c- G( @4 Q  J% V7 c
1 |0 v* p/ K& {- p. ?% g2 p/ ^- r编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) _& H: w+ |% b6 ]% i" e7 Z, U7 S+ s8 I. r: z: e& i0 ~4 ~
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 ]: a8 V5 S0 }, C% d4 `
7 |8 ^+ x% S, f2 gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 n1 T- l3 o# W+ b0 P, D. E" F( I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! X, C) P7 n9 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ O1 ^. h; S- j  p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf6 c3 ]9 p' U0 v) X  u  C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 ~  a- m2 H* C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 ^( x. j" a  O" @7 W6 d
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# Z9 ?2 r) B" F7 x( O7 S/ y4 C1 V
五.配置nginx5 A. U" r* F5 L: ^
0 X2 E; L6 R3 e
在需要启用modsecurity的主机的location下面加入下面两行即可:
9 h8 G9 C2 r0 H- v, U
! L, K- |$ S" n% K8 uModSecurityEnabled on;  ; ^7 X! m; f) V' z- {( J# _
ModSecurityConfig modsecurity.conf;* ?0 n* g4 f: R3 k5 t4 f0 _" Y3 ]- O
下面是两个示例配置,php虚拟主机:
2 Z# s8 j% _, S) u+ x
# H; x" d2 V) G; Q, z$ iserver {, J/ W" ]) D' N
      listen      80;9 `$ c6 b, L2 x( p% m2 C' q
      server_name 52os.net www.52os.net;, ^; W2 ^9 _4 @: o5 S% i) Q8 u
     : M" M8 B, T( F# l' v
      location ~ \.php$ {0 M( E+ I" @/ A2 C' c* A! ?+ U9 W
      ModSecurityEnabled on;  $ P3 M* z; g0 E
      ModSecurityConfig modsecurity.conf;
$ A. `5 }& o3 q# L* H; H* Z* ~4 `5 ^% X6 a! {2 ~7 o# h3 J
      root /web/wordpress;
6 `: g/ V5 |! {8 P9 x      index index.php index.html index.htm;/ T3 b. i( S4 Q! t+ h/ s
  
; N* J5 {  W+ N7 e) r) e7 v( K) }2 y      fastcgi_pass   127.0.0.1:9000;. g5 f8 ~! Q- h4 u. Z$ {
      fastcgi_index  index.php;
- _0 s( @: a* s# i      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;- i4 _% x3 C. c& T) d
      include        fastcgi_params;
7 H9 [: H8 ^0 F2 U( A" n      }
6 ]2 h6 F  y  p# l3 H  }
$ P4 l1 P) R) j, _upstream负载均衡:* H3 P: N( R' c" ~9 ?1 u* I9 T
0 R/ ^# i% G  ^. o
upstream 52os.net {2 {. a# G& t/ M
    server 192.168.1.100:8080;
, T  }$ W6 p& X' k    server 192.168.1.101:8080 backup;
. G+ W6 _" K: N. X}
" J9 s0 {4 @1 I4 g% v
9 F" o% w' J- Y' C; g4 zserver {- g: k+ O6 p1 D* \6 G
listen 80;6 @1 m  N8 ]& `! U- h7 h8 f/ Z
server_name 52os.net www.52os.net;
7 y; M. ]2 E% F5 _0 B
9 z) `% G8 s+ n+ slocation / {
3 L1 H  l' }- d; B    ModSecurityEnabled on;  6 t0 A& Q* z8 l1 V; e' C
    ModSecurityConfig modsecurity.conf;  6 N) \( x8 `" o

$ N; W; s* k+ R6 j        proxy_pass http://online;
8 _# f/ g4 x! Y' t) z+ o& b        proxy_redirect         off;7 m9 Q7 ?% g7 t# @0 w! X. ^
        proxy_set_header Host $host;
! Q  m0 f2 I! u, y/ A. n        proxy_set_header X-Real-IP $remote_addr;/ e( k1 @: {1 u# x5 p: P+ L
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( J9 D+ h3 c$ {# q3 q" o7 \8 x    }! [3 @* P# I! D$ e
}$ d( A- Q# a1 _* q# j% a8 E, j
六.测试* A  M+ q5 t2 M8 M) h: n
2 k5 Z1 k( F, p" N4 u* k' \) @) M
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" a( g2 L, ~6 N% T! L2 K* p# G9 I" o" |
<?php- C* M8 ?8 P) w) k7 C
    phpinfo();   
: _  D! G$ k! Y# z! I?>
" W; s5 F; t" U2 F. W在浏览器中访问:
. ]2 s3 O1 h/ n9 V& \& v8 o7 U. N5 V8 ~# |- B. R5 `1 V! T1 G
http://www.52os.net/phpinfo.php?id=1 正常显示。
7 u3 B4 A7 c& l. R& hhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。4 h0 u/ A) d/ B: a
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。$ X- i. l* L; O5 l
说明sql注入和xss已经被过滤了
% K: y3 ]& K4 G7 K
* e. Y2 `. c+ A/ K/ _4 e七、安装过程中排错
9 I, A  f- t9 @8 \' X3 M- L
, S7 r+ g; k0 u* o( E0 G1.缺少APXS会报错
' ~6 _; o, r( h. I/ c+ C
% U. N( q7 j5 I" w" ?7 Lconfigure: looking for Apache module support via DSO through APXS0 y" u# b1 ~" J( \
configure: error: couldn't find APXS1 D/ E' ~$ B% Q
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& j' L6 Y5 ?2 h" Y" k
解决方法:( F. Q3 o; p$ ]9 M3 Z
2 {8 a0 A# `$ M
yum install httpd-devel9 G# v4 _  j1 d) j( E
2.没有pcre
1 o& x' J9 Z4 ~# a3 R7 w
3 r. w) e) k, V* I# ~$ l  uconfigure: *** pcre library not found.
  Q: ~! Z+ r: M" Iconfigure: error: pcre library is required
5 D& b$ {- y( e6 I' x# J1 E解决方法:
- j" l9 U) @+ W( s( u2 e. }+ [9 V6 T2 H0 {6 d5 s' N7 `$ @/ W: T. m: Q# q
yum install pcre pcre-devel( ?/ Y4 }+ A- C. w
3.没有libxml29 Z  S3 p; Q) o9 s. |9 l! v

+ L* v; U# I* U6 H# Y9 u/ I" O2 U1 z- O& x8 v
configure: *** xml library not found.
9 o+ M. A0 D$ B9 }0 ^/ ^/ zconfigure: error: libxml2 is required
' i! K0 r9 P2 n1 i- W" F4 i! Y解决方法:
  {5 }. M. [9 f: p) D' z, J4 N3 r9 g- S; @4 Z
yum install  libxml2 libxml2-devel# V- X2 C) Q2 B( p8 S! ]# I; e0 }
4.执行 /opt/tengine/sbin/nginx -m 时有警告; R, h4 }/ T9 P  Y% N! ]
/ J, [! ?% c- x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
1 t$ @6 j: K( A' _) c' Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!- |8 l$ S  E# c
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 ]7 g% ?  A. b  y
. V! B, Z' X" H5 y% x/ R; h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- q+ U1 V1 u1 _, [" |8 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
' L% h: I2 U4 h: c1 d% ~& h/ c- d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!  z$ n- a1 _/ T6 y5 }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". V! K; V: g! e% K5 u( _. }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! g% t. L# M+ D1 m6 C, C2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* c! B4 N' F; H解决方法,移除低版本的APR (1.3.9)/ _! z' d; x" F4 E$ G/ L
2 w7 B% v  k* N9 f9 M, j& f3 B8 r
yum remove apr
' m% w" d5 I+ B. E. F$ I/ ^! C6 j# }5.Error.log中有: Audit log: Failed to lock global mutex
. T* W# m% E0 _8 D. l  j
! {7 s* I/ y4 S% Y# ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( N" W% j# u5 d5 W- |5 g- l: [
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% s6 Z3 i! P/ Z0 |4 O$ D解决方法:1 T' W6 u; G- B  \$ g. j; j' C
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( D0 O$ G% u% ?, \! H% W) x  ?6 U6 t, J
, S4 c' |2 @. x* v) |) R  s1 A
SecAuditLogDirMode 0777$ L& r& g/ ]1 d
SecAuditLogFileMode 0550
4 a* a# c7 g! R, i. v/ I4 ESecAuditLogStorageDir /var/log/modsecurity* s! C' i* e7 z" C/ e3 }) S
SecAuditLogType Concurrent. G" W2 [' g( w2 L8 t  {3 g
参考文章:
& L7 d1 W5 K/ F1 Q8 q+ ~  A3 bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ L, [0 ^* ]* e8 X5 }' d) d
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-19 09:00 , Processed in 0.060116 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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