找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11921|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 U, q. |' s/ R( a1 |
- J: |2 L4 U3 a+ H5 ?9 Z) n" x
一.准备工作
9 I( d+ F: x0 O7 s$ O6 k- X' e0 d. V, @% W6 {% y! N# s
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! v9 y1 v2 y( a+ U# c
7 a* e3 v! K2 t& otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ ^* ^6 _) M" M6 _6 k) a
# ^+ Q) w& v6 Y$ Q$ ^modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 j! K4 D0 K; y% W# ]. p7 w; Z( s

, Q) o# x# O+ u2 @  T- B* `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. |7 D4 |$ y$ g6 t

9 y" T/ a& \% y, E2 ?依赖关系:+ N- G/ g9 T/ g" U$ [
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( A6 t0 h3 W" k3 D" s( x

# h( X! T$ S* lyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel" n1 M! G  Y9 c8 o5 T( X
modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 ]( R; N. E. q9 I, j; }. h
$ k  W3 @1 }( Q: `$ `yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
$ ~/ c9 s# K0 y% o' w二.启用standalone模块并编译
! `' U* p% \, [
3 a: ]  t; \& s9 }下载modsecurity for nginx 解压,进入解压后目录执行:$ z7 z5 P2 R# g2 a6 ]) Z$ @. ^" o/ g. _
" O; d) H% e/ L
./autogen.sh
% A. G* F) D( ]./configure --enable-standalone-module --disable-mlogc4 R% b7 O3 g" ~- [4 U
make
" k& S2 j8 m7 C5 C# ?. y: H  s三.nginx添加modsecurity模块
' e! Z  T; a9 i) d( G/ p+ I3 W
3 d" a4 i/ {. R& F6 Q/ X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; G8 f% }0 I% v. @9 n
' l$ |2 F$ O0 r) G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 I  ^2 w2 C3 S8 I: H) C. M# z
make && make install
4 U: p- w3 v) L四.添加规则" ~. w. {8 [$ V

0 B( d* {7 O- ~; I9 U" ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 M; G% F; |/ n4 J( w
: _) l3 g% {# b9 F1 e- v3 m5 I
1.下载OWASP规则:
# g9 ~- T: ]* h. O5 z6 x& w0 L3 X4 G+ N  P
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 W% j* h& m) s( X
% V8 R  ]8 O) @mv owasp-modsecurity-crs /opt/tengine/conf/* N4 o! U% v6 f6 J2 F' [
" `  e7 j% Q' I1 J+ ?7 P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) A) }) w0 {3 M$ ]* ?$ ^
2.启用OWASP规则:
- w+ @2 f4 l5 X  r4 c6 X' K4 R" |+ C: R1 }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ p  H. j# ]6 y

+ c7 |- y0 b! L% S  s2 B编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# r: W. J' `3 g6 n: p5 A
0 }3 X. l/ P) J9 ~
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 d4 N8 m1 B' V8 j* y: u# S! O7 g9 n% i& z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
; I3 {/ ?/ f- Z6 FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# ~! H# Q5 A# U' y( k; g1 EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& \' H; v- |0 T7 I  l; P0 C; J" T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& B# y2 y0 E0 Q, x4 |9 U. [: e9 w8 lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ y: N9 Q0 Q: r* {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 }9 ?6 v- E3 ^, @
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf/ G" E7 k2 y. A4 i
五.配置nginx
, g; O" d9 v; i9 ?/ [) z. w5 z3 T+ b- ]2 C) J! P
在需要启用modsecurity的主机的location下面加入下面两行即可:5 L% C3 L, n1 u' Y. a+ }+ y+ V, V

6 Y/ f! }$ D" h: D" J2 y- v; oModSecurityEnabled on;  ; I6 p* c  [7 [& k8 M* B* n
ModSecurityConfig modsecurity.conf;7 [% p# x& X% u$ I8 @
下面是两个示例配置,php虚拟主机:1 I  x$ I: c% P5 c: B

2 y) H7 u. z9 [* h, @3 O; Eserver {
. s. u/ z2 L- f3 A2 c7 Q      listen      80;3 M0 i, k1 `0 ^# }; C, l: ?
      server_name 52os.net www.52os.net;" ~, D' ^$ n. G" ?5 p1 T
     
. b( F. x: r1 v+ @0 E. F      location ~ \.php$ {
4 c4 n( w6 @4 E+ J: e* {( g* Y      ModSecurityEnabled on;  , ]4 w& M! o  l, R3 c
      ModSecurityConfig modsecurity.conf;
  R8 Z0 m9 _* Y# D4 N8 T- k# x9 q4 R- h/ `2 T6 n
      root /web/wordpress;
+ U! v1 h7 z3 t) ]/ G1 t5 N; T0 B      index index.php index.html index.htm;
: ~! H( K: i% \# H% q; c2 ]9 N% p% f  6 T" o( Y0 b( G; `0 k
      fastcgi_pass   127.0.0.1:9000;5 e1 }& o7 H, H, @
      fastcgi_index  index.php;. Q  P% f% {8 g* R& Q
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ m0 v+ H5 Q# J
      include        fastcgi_params;
, q" y- E6 f& A' N      }
0 \  e, O+ R" K7 \: U' M$ U  }
; w; d5 f3 g! q, w6 E- R0 y5 k5 nupstream负载均衡:
, h+ l6 b. d0 K
* A/ B  @- J) k* C/ v" R% v1 cupstream 52os.net {* S* {+ U3 D6 f" T$ {
    server 192.168.1.100:8080;6 n5 v6 x* U7 x: h2 ?
    server 192.168.1.101:8080 backup;  q/ g( c2 C! w$ V4 H
}
4 G" I' g( j1 `: M) q' ]
) C. w1 S+ K$ S; iserver {
4 D5 K. P" M3 c! k: qlisten 80;
" c8 i  V$ _3 I5 a; s9 q- z% o4 b. iserver_name 52os.net www.52os.net;+ {9 I, _* t* X, p, ?% d/ W

& z2 G9 |+ g2 t* qlocation / {
7 l# C( {" @2 M0 d8 L: y% I    ModSecurityEnabled on;  1 L1 d1 L: k5 H# [' C$ a( o
    ModSecurityConfig modsecurity.conf;  
4 \4 L: a0 A* y' U; I/ x, W- |  H
        proxy_pass http://online;- [- l& [5 J) o- F+ c9 k
        proxy_redirect         off;1 `! ~7 |4 }  N4 d: M
        proxy_set_header Host $host;
, J! k) Y7 H* l        proxy_set_header X-Real-IP $remote_addr;
0 C8 h7 J8 g5 N* O% Z5 X3 i/ a5 e        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  R3 S' A3 _4 h) R+ {! k' ^
    }
1 M8 F: H6 w" a1 U}
$ \% v0 |  d9 q; [8 H: P六.测试
% B( h% Z/ c9 P; m1 Y9 b: \! \  V/ w7 D1 y7 O, I
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 }* {* K; b1 l
( F  Q+ O4 K( i  o, J. u4 ~<?php+ |$ K! B( t/ Y" a
    phpinfo();    9 }" o/ i. \% S! V* C' w
?>9 I1 ~# n0 S# Q4 D
在浏览器中访问:4 k( n' A3 f2 [7 Z7 s% d0 @4 c
- _& o& d! N$ a( M9 j% y2 f
http://www.52os.net/phpinfo.php?id=1 正常显示。
1 n0 G$ e. ?  `) m4 t0 nhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 M  g3 j4 n  U; X; G
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
2 F4 `+ z0 i- z2 A! o' ^说明sql注入和xss已经被过滤了" c* E. q* m1 o2 V

0 ~' m2 r7 [9 |0 D, Z1 Y$ ^七、安装过程中排错  e! S4 R/ a+ v! I( J- q: A

8 S/ ^' \9 W% ]3 \# o1.缺少APXS会报错' `7 A+ B, i9 a" g4 L+ m, E: ^

, d* S+ T- K8 s# |, bconfigure: looking for Apache module support via DSO through APXS6 P* q& A7 k' z" N* V1 a
configure: error: couldn't find APXS8 a$ G6 B8 ?# P1 w+ b' [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 F! ^3 y1 `+ Y5 [
解决方法:
2 Y/ f) |- I0 C( h! I9 |1 D  r# W! P" P' G! y& d
yum install httpd-devel
9 P# Z8 i0 P& o! Y' P2.没有pcre; y( v7 |8 q, F/ {' m6 w
- p. N: X2 `0 F# P* i  c( c
configure: *** pcre library not found.
: M* t# S% r! H0 y, k& Pconfigure: error: pcre library is required9 \) @! s! @4 R: j  H  i
解决方法:
6 U# H& U! Y& o
8 j3 l! x& U! p  Tyum install pcre pcre-devel
- L5 R1 B) v" A9 y( ]7 `, i3.没有libxml2
7 B4 v4 U7 o7 N. q9 d  {' Y& a, n
9 \; l  i& K# e8 B& M* L! x) N& {5 y+ @& ]. B
configure: *** xml library not found.  E2 `) ]2 `. d0 H. i  ?3 G4 [. ?
configure: error: libxml2 is required) p0 j# D4 _4 ]4 G, h
解决方法:
7 v) B. h* |  `/ p
5 p* p$ D/ X. H) L. }yum install  libxml2 libxml2-devel% a/ c* Q7 u& Y$ L8 y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 v) D; |" F+ G2 n; d! T/ i, n2 a
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
& o2 g; p% K3 R, Y7 znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 N- ?8 ]$ k6 C5 u' ^/ L# I原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log1 O7 V( @! B  e

- T+ w8 v2 Q# a2 I6 {& Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., J, f% ~5 h* S0 M7 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ \" Z4 ~6 q" a+ A. L3 Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& }0 ^9 P  W8 M3 e$ t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 T* ]* e' F  Y' K% y3 e3 ~" h( g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") J3 J0 |, @. b  ^; l! Y9 Z
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ e- M# k6 B4 P, O  n6 _
解决方法,移除低版本的APR (1.3.9)
/ w/ `' U/ X% i2 o- U/ ~2 r
$ `1 Y# ^5 [6 W9 Q# kyum remove apr* o% ^3 \& ?* Y; G4 C
5.Error.log中有: Audit log: Failed to lock global mutex
7 J6 g) O! Q: i/ r5 K. j( T6 o' l% d; u% O! Q! {) m
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     4 s" q0 T9 ]0 E2 l: ~' N
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]; _0 k$ I  Y: j
解决方法:- N' Y7 r. g! c( u# M6 V4 l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( m  m7 S: z) ]% T. ]0 X4 C5 z: a! }& y2 Z/ u
SecAuditLogDirMode 07774 ~" Y. e  Z; t0 ~2 I. w
SecAuditLogFileMode 0550! f9 x" w& l* Z2 F0 z- C! ]' g
SecAuditLogStorageDir /var/log/modsecurity0 y( j  D6 ^# N6 z9 X& O. @
SecAuditLogType Concurrent
& L' M- v( N& \% k; A" Y参考文章:
1 ]8 I+ q: l; ]https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 _% U. P$ L' dhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-25 21:38 , Processed in 0.071021 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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