找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 10438|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 Y6 a3 M2 N# U- D+ H1 Q

  c% |, F/ @9 v: b2 k一.准备工作
6 h0 H7 d$ j( S6 C
! B  d# Q# ?: v# _- W2 C. q- x1 g9 a' W系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 r+ x% w, E8 s0 U, w& w( W+ d
1 X5 N+ n6 U, l4 e5 c" o" C# ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 N. z. _* |0 J) g2 m& o

( ~& x5 I7 S, _4 L0 `# E3 Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 [; Q* b+ _% H0 ~! [' h( X6 M1 O0 a/ x8 _3 F# T; t* h# c" Z1 c3 X- A
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ [0 O0 P, n# X! b8 ]" X
" Q* d3 H  D; O( f6 G
依赖关系:  D. S" y+ P! N2 o. c8 D6 @% p/ ]
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# l1 G+ w9 @$ c5 ]0 C+ H& g% |3 J$ U$ h! R! z) y3 E
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel- O0 Q+ [7 q3 y/ F. l3 I
modsecurty依赖的包:pcre httpd-devel libxml2 apr% C& s1 s' k0 E/ I( P" k* e

& {, P  `/ e* ~' q9 wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel' o* Z* o2 x$ r- o
二.启用standalone模块并编译
3 L; E* q/ I% x% x/ y9 R/ M+ I) D$ D3 J! Z2 B8 \) k% l
下载modsecurity for nginx 解压,进入解压后目录执行:# M% V7 t* G4 k3 ]2 d0 g

+ E' S+ P: }% H! ^" m; U./autogen.sh
6 w1 R4 ~) t: I! t./configure --enable-standalone-module --disable-mlogc
  Q* ~; Y0 ]: n0 gmake * g: Z/ z* ]- U: }8 V. C% U
三.nginx添加modsecurity模块2 S/ m7 V/ g8 T" T/ ^$ V4 {
/ x/ c2 g6 G) d4 |) r& ]: ^
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 f% G7 I3 a: S$ |  k5 q. ~) a* G: T4 e- S) `* b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
5 `" g' Z9 e7 fmake && make install
4 r/ g- e/ ?5 H0 s+ c四.添加规则2 F) o8 `- n/ u
& y! [' f9 j- D, A
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 V( o$ w: D8 S, k- K7 E# I+ D9 L. b
1.下载OWASP规则:
' g# N4 u8 b: |$ P" n2 b; u, `! h6 ~3 \. H- V% X. Y- k
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; `& N0 }% x& V$ B* N& x/ z1 N
* S6 {" E1 W2 B5 J6 i( Q
mv owasp-modsecurity-crs /opt/tengine/conf/
9 K1 Z( B8 d1 g/ y* W+ ^
* Q/ O( H$ T8 Ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 B3 p& c. L0 G. n3 ^2.启用OWASP规则:1 |1 U3 u! [9 c5 D( L4 P
0 n% w6 K0 v4 {8 V) u1 I$ D
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ P) S2 p5 k5 P4 s

5 \& I3 l3 h2 M$ {/ b: g' k编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 X" ?# Z* f# \/ C/ p6 v
) G( y: V( L( c: o, H; F
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。  T0 W$ K/ m6 A# M3 }

, {; T9 g2 d4 h9 d$ ]# L* K; T9 FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
% F) z0 V+ F6 d* Y" IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf, c+ T7 n" A7 ?" M4 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf: {8 `- m! c7 P8 S$ N! L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf  f; o9 y& M% v0 Z5 ?1 p
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* D1 A+ h% D1 O; n& wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
  M9 a9 x4 Y) ~# BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 T( N3 L" U* z5 q- t
五.配置nginx" T4 a5 P/ M5 a. }. X3 f: F

1 t! W3 J* \' M0 d在需要启用modsecurity的主机的location下面加入下面两行即可:0 P- s3 r: G5 Z2 I

4 r& ~4 {% b9 I* C. U6 J; D; G- uModSecurityEnabled on;  
( s8 ]6 p- N/ P$ d! y: rModSecurityConfig modsecurity.conf;
2 D& ^: I4 q' s( O下面是两个示例配置,php虚拟主机:$ W. W5 j$ `1 w- o

6 M. A$ V9 D0 I" d/ j! {& t+ o2 X$ Qserver {( e* m9 B* E0 A7 T3 A
      listen      80;* }( L' C: p; t) w3 H
      server_name 52os.net www.52os.net;7 ?& D% |* s/ b3 R
       }' ^+ L) t( n
      location ~ \.php$ {
* ~! B( [4 q1 z, `      ModSecurityEnabled on;  
5 ]! ]+ ^9 N/ B% u- T      ModSecurityConfig modsecurity.conf;
3 `& ~7 A' ]( P3 E' j, k0 B4 c5 z7 ^7 ]" P
      root /web/wordpress;
) ]0 P  i/ C9 b      index index.php index.html index.htm;
; L3 o. ~2 K' l7 d; a4 w8 e  
8 i6 V) p1 p. ~- f      fastcgi_pass   127.0.0.1:9000;
, K* A) u) k( N9 y8 G: x      fastcgi_index  index.php;
. G4 y6 `+ l, e7 a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
! ^/ [7 f/ p1 v+ O; f; F; V- i. j      include        fastcgi_params;( ~% v( Q+ u6 @$ l: F( M: S5 f7 E
      }
  x6 V( H8 K& {6 K  }- j: N2 e  J  t+ y" E  N
upstream负载均衡:
; v: ~& X3 N8 F- ~# C. |# q) X! @* S% W  m! I+ ^' ~5 k
upstream 52os.net {# U% x- r6 r9 Y! G: J. n1 k* \
    server 192.168.1.100:8080;6 R" X" r0 S' e4 T# Q; ~" \
    server 192.168.1.101:8080 backup;
) J- j6 \: P( {& V# ]}6 U# [( Z% E8 ~$ i2 E
- P$ x+ `, M) k0 G1 t" j
server {
- X$ Z) c( G$ K+ y' {listen 80;
/ d0 G; G. _1 [+ d* sserver_name 52os.net www.52os.net;% H- [5 V, T* m# @; U* Z
9 p! y% R( l5 w
location / {
& r' G4 U; u2 q    ModSecurityEnabled on;  
: T4 }) p$ i9 Z) D8 O5 f    ModSecurityConfig modsecurity.conf;  
6 F' f/ z% D& r6 X& M* `8 R
" @% f9 z( C; E0 U4 e7 @7 W        proxy_pass http://online;3 W& M% Y4 d# Y4 B+ _
        proxy_redirect         off;
( b. o( B" R$ i- e9 z. R' X        proxy_set_header Host $host;
4 s" x4 o/ u/ y# Y* s        proxy_set_header X-Real-IP $remote_addr;2 n% y4 e5 W- C& O
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; ~' g4 n+ t' n/ y, \2 z4 M7 m
    }
3 \6 l( L/ Z; V& i9 x8 W5 f; w}2 L& f  \% X1 C7 F
六.测试8 R+ d6 u* L: ?( q& x' ]4 p4 ~4 f
" K' ]- f# ?3 _. B: h2 {7 q1 m- n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 c, c2 l0 H$ S, X; o0 o( }0 G
7 k, x, }  C; }6 C. _<?php
; o+ r& k; E+ S$ J    phpinfo();   
( U) ~; l$ i) m+ H. A6 K  M2 ]?># L; E+ W# d4 a
在浏览器中访问:
) u/ D7 ~; v! l6 U% s3 G* N
: Q. {$ i1 E6 h# t4 q) c2 Phttp://www.52os.net/phpinfo.php?id=1 正常显示。3 k% O7 P) b6 C8 E9 a! a' c
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& X" W8 j: h) \# B- v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。% Y& e9 X) {% v% P/ r: H- v* I( B
说明sql注入和xss已经被过滤了
5 A( }" Q/ w5 y) r( J
- X2 M# g( W# e: N4 ]* d1 ?" B) F七、安装过程中排错
1 r+ v: |7 ]1 t$ {" a( d, Z, }, t9 \& s9 \% \
1.缺少APXS会报错
8 ]& M* Z: G7 d) g$ U7 T
2 N1 }7 T; ]. m1 G( f, k' [8 R. Econfigure: looking for Apache module support via DSO through APXS
& g7 W" `4 W# m1 D+ dconfigure: error: couldn't find APXS
) x3 n, p* i" @8 g- a/ b# D! @apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
( _' r( ^5 I3 o5 a- s: I解决方法:0 |  Q7 O2 u& E, N1 V# M
& {; y1 C: L8 F4 X
yum install httpd-devel6 \, A& L$ }0 T. n2 f1 [' F
2.没有pcre8 q, t* p+ v9 z/ P

/ J( F' x/ C+ G" M. _# Dconfigure: *** pcre library not found.& f9 K/ O( [+ a( ^( g2 t' t' e
configure: error: pcre library is required
* a* c+ t" |8 _* y& _& F" Z! a/ b. s1 x3 ^解决方法:4 k. [/ D/ k* {7 e/ v/ X* s

- r' e  u) }% Q1 B* d7 I/ xyum install pcre pcre-devel1 y- k; J+ x1 ]$ E( e
3.没有libxml2* j' ]1 Z  i! a8 ]
7 T9 C1 e/ i. |$ Q3 {
6 D/ [; {: N! h0 E4 i7 `
configure: *** xml library not found.* a8 ?& \# R+ C! W0 ^
configure: error: libxml2 is required
  I$ A2 @( s! e- P% d解决方法:
. w% P* E# `1 E1 L# M1 G( X. V/ _. L# G
yum install  libxml2 libxml2-devel
& e* A1 `* Q  I& V& ~" ?8 _4.执行 /opt/tengine/sbin/nginx -m 时有警告
! W3 T0 F  h8 y2 \1 _' V* Q# J. I& ]2 i
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- e: X8 {' k! ?7 y0 Hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& i6 [$ N9 b* V6 g8 ^4 m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 l$ z/ p% b# v4 X
1 r/ _. U; P4 j( l9 N) P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 r( C) h' P3 ?, ?8 F5 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"! j  v/ s) R5 ?5 s% W8 p7 p4 i( o
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- A! U. i1 W& m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 Q7 U4 s* U$ y; p9 b' |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ z2 j% i/ P- J$ i8 z+ E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 H& n! N9 i) M5 Y2 h1 V
解决方法,移除低版本的APR (1.3.9)
9 e: m; ^' q  }$ [4 z* C! G  Y- P9 }2 B* {9 M" L% ]/ h7 T6 k
yum remove apr
' @# g+ C$ ^% D8 [) n+ t5 T3 l5.Error.log中有: Audit log: Failed to lock global mutex7 U. R9 j, e9 }# H. n
, A+ b* L5 Z; H( X0 U+ m' D- L
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
, h& v6 @- X: n1 N! a+ Y1 dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
0 Y- K2 g) a% |3 B- ]" J  |解决方法:% d3 c. G% n6 L# B& y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* t( `3 m/ O9 T9 Y: T; y
% }+ J: b! _# u- l1 V! B) s$ P1 m( ]) g
SecAuditLogDirMode 0777
% F" S8 s' y) W; f" zSecAuditLogFileMode 0550
3 ?# u0 _# j2 I( x( l- o" d, ISecAuditLogStorageDir /var/log/modsecurity
, d/ @: D$ F4 D2 u& P' O! WSecAuditLogType Concurrent
' D& O; k* }% _* l$ g, }参考文章:
  Z$ q0 @& h: P+ X/ N, _https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 T  Z; N: M( c# y: V' H! T
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-3-30 16:02 , Processed in 0.068933 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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