找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12215|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 {" J' v& c0 s* S$ U4 U& ^- S
3 |$ N2 C) B! V5 _* h一.准备工作
" N8 G, B/ A. W/ g* S8 V( _; N! V& E: k& ~9 r+ e+ N8 P
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  K# q) p  p/ I; |/ [

, H& d! X9 \2 E$ b8 Ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 h. G) y- K5 H# r
% M# ]0 z0 v5 b$ m5 }1 F
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* Z# m& G! H. `( J. n) l
, u/ i$ y5 ]7 e: \$ D. QOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: K' q! l% ]2 B6 @, C* |
: ]% P; x2 r; P# ~( ]依赖关系:
5 ^& Z3 Q6 X4 atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* M7 D1 E; N' q( p7 j
( e3 Z- A1 ~6 Q6 D$ f( q" U6 Uyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 d! R2 ]2 A4 G6 B5 l/ \% _2 lmodsecurty依赖的包:pcre httpd-devel libxml2 apr
+ x" b5 {- _) q" K" G1 L) Y3 }" T8 `/ r! \
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
7 r5 B5 ~+ ~3 B' E% F二.启用standalone模块并编译3 O3 n& \; v5 Q$ _# c; Q
$ q9 u3 U+ v. @, U
下载modsecurity for nginx 解压,进入解压后目录执行:+ m# i, s  A5 W+ e* ?3 X: j) D9 R

3 {$ A/ n7 p1 x& I1 h7 `1 p./autogen.sh
% U, u! q. ~! Y: d./configure --enable-standalone-module --disable-mlogc. x& R/ A5 {0 X  l/ U" h$ m
make
' _1 ]# s# u$ M9 _三.nginx添加modsecurity模块
- ?+ Y3 j- q: }" U6 Z+ p( L3 ^3 l4 H( R; E+ q# H* O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
; x9 E0 ]+ L0 N" `- U0 U) a' j9 U$ r; K2 g# y; U: t5 y9 r6 f
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine: ]) ]6 f/ d, @8 `. W. @
make && make install
: Y* a9 h/ L  l) A四.添加规则% W6 e4 o6 N: A# r5 @( V/ \% d
! R, R6 {+ i7 X/ K& Y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 Z/ L! ^7 r' N: h) @8 R" h
6 Q0 i7 @( T7 K6 ^- E) `2 E$ g
1.下载OWASP规则:4 x# _* B0 R4 ^! l# z( F

4 B: x* [: G! n8 P. Ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 s! x+ v/ y1 V/ ]" ~' o1 ~8 j7 G" ^

  T1 a! t% j" t9 Rmv owasp-modsecurity-crs /opt/tengine/conf// ]( J, K) t" b- J( h2 S
1 |3 A' E- d) R
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 t' A" f# {1 G! Q; B. r4 d
2.启用OWASP规则:
5 u4 @4 {$ n! h& M2 ^5 W8 R5 b& o, H9 c. R# n) I; n6 {) N1 \
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% X9 z' F+ R7 R& e

/ M$ r6 C3 E; y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 ^# Q8 y. f6 S. J9 _7 ?( V
) l2 c9 N$ R" z; V% {# @
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 P7 Z! F4 `+ H' G( p- f6 i

: F5 H0 [9 y" oInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% p% J4 ~  E  E5 f1 I8 e; u5 X3 jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ l4 _; g. ~) K& ]/ o' G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 X: f% P5 {! z" R7 x* D6 m) j/ v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
# Y6 I0 Q+ d; FInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
  D% N9 ]& ~# ~/ _+ hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ L: M8 D3 O- z6 O8 o! k
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 _% T. A) R/ o. m4 E
五.配置nginx- g; J% l; A# c# \( z" M

$ H- w1 C! u# Q0 c0 T在需要启用modsecurity的主机的location下面加入下面两行即可:
/ f7 [. [' l' E1 Z  ?
4 M/ g$ }5 Q5 R6 KModSecurityEnabled on;  ; {5 ?! R$ e5 k: n' i+ O
ModSecurityConfig modsecurity.conf;4 K1 l' R) Q" D. }  t2 w
下面是两个示例配置,php虚拟主机:
9 g- C8 ~! [% L& M; j
. J+ y( A7 S% H6 T! x, \  }" K* Hserver {3 l  C8 \0 [% z8 D% \! R% T
      listen      80;1 B# l% x! l4 n9 c- @! j9 u& [
      server_name 52os.net www.52os.net;/ B7 y4 Z  E. \. d: v# W# f
     
( D" v* [+ {6 v- n/ @* Q      location ~ \.php$ {
" o: @! L: H% _      ModSecurityEnabled on;  . `' |' P6 N7 w: B
      ModSecurityConfig modsecurity.conf;) W& W6 z0 f3 ~" o) i/ `7 q  A

' Z: S9 u! e) f% d      root /web/wordpress;
9 x0 Q$ K, W% p. P# h      index index.php index.html index.htm;1 M7 l& ~5 P* r6 ?3 n) h. q
  ! F! `) y" j( I5 U" n! y5 _
      fastcgi_pass   127.0.0.1:9000;
' P+ P* j* E% o, c7 B      fastcgi_index  index.php;
0 |5 j8 P, X' `( N0 M. B      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" q0 n3 ~6 y$ e  {4 E$ m
      include        fastcgi_params;  [; e+ V$ ]9 v1 d& e/ M6 ?
      }
$ R: I+ g, Z# M1 [! k  }$ i. y& N2 y) X9 L/ T8 Z# }% M6 d3 V
upstream负载均衡:" Z* n0 C) x# Q* o) I. L4 s# r2 P

2 u. t9 v9 p" ]4 M. K! ^upstream 52os.net {. K4 Z! n, J# Z: A
    server 192.168.1.100:8080;
3 j  k; @4 I) y8 v, ]    server 192.168.1.101:8080 backup;& r3 S) i, x) ^& c6 ^( p; j
}' F5 I; m. V5 p1 b' C
9 E; W7 f+ Y' N2 w( @
server {
$ e$ x7 Q& h6 K, ?/ [" Zlisten 80;- g2 }# ?! Z3 u. F
server_name 52os.net www.52os.net;4 j; [  B7 O, Q5 l5 k0 W

6 D9 d7 h9 D8 b6 ?$ O8 M$ e+ tlocation / {
0 o1 r. B6 [0 V  T    ModSecurityEnabled on;  
8 D4 E9 C  m# e7 o    ModSecurityConfig modsecurity.conf;  $ Q" R3 [- U# d

, q, a: e* ~; R4 |/ c4 T        proxy_pass http://online;6 O. j2 Z6 E3 b; H" x% \- n. }
        proxy_redirect         off;) `9 W; o$ o! N: T* w( h
        proxy_set_header Host $host;7 E. h* F2 V% e: h
        proxy_set_header X-Real-IP $remote_addr;, z1 L. Q+ \: U4 ?$ T2 s
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;1 F: v% l* E! T, q' i; D! @0 G( }
    }. M0 i* ?8 `  i# o% z: B
}
( M+ D  P/ o3 A) D六.测试9 Z1 V8 n/ M7 P! t! e+ W2 k( k
  Z% E6 M* N% E# K! w! {# y, ?5 N
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& V1 M1 ^0 d; |+ R

8 b2 J% @5 t. }# w4 g1 c5 t+ G<?php
. ?9 X2 s+ O" u$ [8 Z    phpinfo();    / c5 ]. o0 U7 F0 n/ b- O
?>
" ]+ \, \  n7 W( r; b( C在浏览器中访问:$ l5 N4 U- H: c  b- |3 o

& d" ?, L$ _8 U8 Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 k, r/ p/ Y0 U- Y( B; ?http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- `  L0 S: Y' ?' [& E9 O7 y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
/ _' f8 K8 [" N& N说明sql注入和xss已经被过滤了
4 X9 @  P( p1 z# @+ N
. F* N/ m7 U2 _+ o; V8 D七、安装过程中排错9 B# T: @$ x7 {4 G. K6 P0 H; f! E

! {+ ^! B, K) T. @( x) W1.缺少APXS会报错0 y3 B2 ?% l4 l
) Z, \( X+ {1 V
configure: looking for Apache module support via DSO through APXS4 G" H  P3 d8 {
configure: error: couldn't find APXS
) k$ e; o8 C8 Mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 h" |) s  ]3 y3 Z$ N7 Y" m+ W
解决方法:
# G7 ~% p5 J4 t3 m) u/ R8 S5 f( C! k; s3 {, C
yum install httpd-devel
' ~" E& U) `* ^, d* N2.没有pcre  O9 `, H# M$ _6 [% t

1 |# |7 h% a; [7 }, i# w6 Q& hconfigure: *** pcre library not found.& E- \: e' g$ L9 T9 |; r( ~
configure: error: pcre library is required
* I  w4 V9 I$ b, t# g/ X' j) W解决方法:1 H2 Q! n7 K- E7 @: L+ H
0 C; ^2 B& f+ `: e) {+ y6 ]. ]
yum install pcre pcre-devel( w+ Y9 `- X3 p1 t8 e8 c: ~& `
3.没有libxml21 C' c* R( ^9 _  ~0 u/ r$ d

5 b% d9 [* R% |! T1 _) o/ \
* h9 v' u& R. H' t) gconfigure: *** xml library not found.
: B3 }9 j" [, r* econfigure: error: libxml2 is required, N0 U  |! j6 s3 s
解决方法:! n  y/ N7 s) _, @' J2 E6 Z" E! X8 E
2 p: i8 r# X# p% u, Q
yum install  libxml2 libxml2-devel! x7 B8 ]! {) T& P0 k  s
4.执行 /opt/tengine/sbin/nginx -m 时有警告( f, A, f! c, T% H& y

& [9 k6 V- s+ I* Y& rTengine version: Tengine/2.1.0 (nginx/1.6.2)! U+ S! \4 \, q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* E6 V2 {5 h, }# k# k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ W+ v) J1 U) H0 I( O' X2 m- @9 d2 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* a. V; y- c4 n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 E4 O) W& L+ z/ f/ |& o
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 d  [4 V4 y9 ~0 L$ l" `0 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ A( G1 w4 K) n, z7 E# Z& u3 s4 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ `- H8 i: ]& \- a* v, ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ ?5 m# M) m5 ^2 f. g
解决方法,移除低版本的APR (1.3.9)
+ Q! k; f/ a; K5 G1 v+ Q7 w1 ?) Z* m2 j3 D7 a+ |% [) B* T) w1 c& U
yum remove apr9 X: ?: q6 Y7 y7 s
5.Error.log中有: Audit log: Failed to lock global mutex
" Y3 k: j* ?. x& M- s* H1 O: _9 A
. S% s; i: t# u. b3 h' r  e2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 [4 S, ]# E' r( p: c" Yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: R4 V$ P$ s0 @9 t; F
解决方法:( p" U! b0 p# g; r" ?: b0 V; n$ U1 M$ v
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 b& s  y# A6 L4 |

: `; q: m/ p" c# t5 H3 L+ @SecAuditLogDirMode 0777
7 F8 b8 c6 ~3 e2 X% uSecAuditLogFileMode 0550. J6 N4 o# ~/ @8 v; E( O2 _
SecAuditLogStorageDir /var/log/modsecurity
: N" B/ h6 t2 e) c% USecAuditLogType Concurrent
  E' o) O& m" _( A参考文章:
& y# V8 E% w' j! ^) Qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- w5 w3 |* N2 w: a$ whttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-16 22:50 , Processed in 0.053699 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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