找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11282|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 T- j/ U% }, ^" W9 {6 J; I( I# K1 [- G8 G0 l' L7 n
一.准备工作0 W9 S2 S& M$ {% F  d6 a) P

& M( X- U  H7 x6 j$ |! ~/ [' H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 \6 V. l: D+ |6 o4 }& X6 x2 U) \6 a( |7 b
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 `3 X  W# }9 w9 [4 E2 ~) ]( H  u* v3 N9 \& }
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  C* v2 z% C% x, y

/ Y1 |$ b  J6 V1 V  F% }& yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& b; X8 |8 E. U  m7 j( {! u5 }* e0 X% y
依赖关系:
! L9 R8 e- e& A0 ^tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' H4 u3 z2 V7 m/ y

' r: i% m( }- \0 q8 D  Z  ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
$ y. E1 l4 n" F7 Tmodsecurty依赖的包:pcre httpd-devel libxml2 apr
1 H; L0 i: c: N( c0 s' {0 D  z: C+ l; N* B2 h
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. D8 q' F7 z- e' ^4 `二.启用standalone模块并编译9 U$ ]5 y" \3 s$ E' @& o8 N

3 w$ o3 b; z/ U7 ^下载modsecurity for nginx 解压,进入解压后目录执行:
5 \! u$ p( R2 G7 J4 ^* ^3 l; |2 P* Y' o% f. }/ S
./autogen.sh
& k/ l# A3 a/ O./configure --enable-standalone-module --disable-mlogc
3 a  I% O7 r+ N; Kmake
' Z6 o- Y  m0 G3 E) ?6 P三.nginx添加modsecurity模块
7 }5 Y! h7 C# r
5 n1 _  N. W+ U  O& H% \, ~1 c0 Q0 g在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) H8 }+ Y  Y( @5 z6 X! J
4 ~) \3 b+ w) a' R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
2 ]$ T1 x' P) I; s7 Q* ^& U* `make && make install# a) Q( C- P/ w) j  F  A& M
四.添加规则
! j# j1 {7 c* F3 X7 l
) q: c% k% w4 k. g7 I0 [8 i, Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- @7 O8 n7 {  Z* ]9 a. A  M' j% j

2 f, L# h& h) Y& R! r% o8 f4 J1.下载OWASP规则:
3 h9 _8 D4 x: L3 x
! K# y$ B& {; W8 M9 N+ ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' E: g( S" F4 E* s
; A. l. [: `; E
mv owasp-modsecurity-crs /opt/tengine/conf/: [1 d3 x. c: g! j4 I
* x+ B  i+ f' T
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 Y% [' S: S/ N* G& b( t+ R  I% ~
2.启用OWASP规则:
4 @5 g2 f  P9 O9 _: Y. q
( ^, U# G* r7 o! U复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 ?/ [6 E% s$ {' d9 K4 Q
' D7 c2 T6 F! Y9 g编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 _  p7 m! M& ^+ N2 v' h1 K- H( f: K* S+ f
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; }% P* D" K) m/ h9 R

5 _+ ^( c3 z  i; V4 B5 @3 i  ~Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf  |$ m( k- F8 {: |0 G7 y/ j) j4 {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! m- s  q4 W3 V3 z5 N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 t5 ?; B" P9 [5 z! J6 x4 \
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' E5 d; A2 ^8 O, \  c! WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ n9 k) H$ ~+ y4 [' z2 N. s, r' h
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  w1 v$ x( M8 Y  D
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 E. f8 Z- Y0 N1 n# ]& }: k五.配置nginx0 D# a& g# Y* \; g: R
8 K1 _& _  T; Z, y6 g' U
在需要启用modsecurity的主机的location下面加入下面两行即可:+ c+ s- B! z" Y* c+ n

) _+ S- w  r4 w- N) ?( h1 bModSecurityEnabled on;  
: W2 M% C8 q, X7 O0 s  T  H! NModSecurityConfig modsecurity.conf;0 o! S6 W$ V( e8 s
下面是两个示例配置,php虚拟主机:9 U9 ^1 p4 H/ Q$ s" @2 m
( r, q5 p6 J/ E$ J, f
server {8 u  g& ~9 d; b" G
      listen      80;
( o2 k" L& i6 @. g      server_name 52os.net www.52os.net;: t+ g" M: Y4 V8 \* L- `
     % D8 [4 F9 h0 ?3 T" N$ b+ b- _# z! F
      location ~ \.php$ {- {, g. M$ B7 }
      ModSecurityEnabled on;  
6 M6 H1 X& v4 N# H7 H% G: O& R' T      ModSecurityConfig modsecurity.conf;+ A' Y/ r' ]+ R- r* H# [6 m  ^  c
0 [& N3 f/ d# g+ L7 g! t
      root /web/wordpress;. g- O, ]% U) T& w8 j( w. K
      index index.php index.html index.htm;
( }) h4 @) v: P* N% W, ^  ; ]- S/ a2 p3 Q1 q/ e0 R
      fastcgi_pass   127.0.0.1:9000;
; n0 E9 w2 R$ P, p* W1 M, |8 y# f      fastcgi_index  index.php;
2 B8 G8 F+ x- k4 K8 j5 W      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. v8 m! I6 Y6 B; `" a; j, V' q' L
      include        fastcgi_params;" H# d1 A3 ?0 n+ C3 k9 q% C1 `
      }
; N4 b* P8 @8 u' g! c8 J  }
7 _+ `0 Y- M( p' M6 T: [% Zupstream负载均衡:% T! l  d( e# r9 ~! R1 J4 u
1 g5 W- K# P4 F1 |4 g
upstream 52os.net {& v1 P' M& J. C4 `, `8 N
    server 192.168.1.100:8080;
" W6 A: Z( G& I0 J    server 192.168.1.101:8080 backup;
( q6 C; N1 P" B/ p3 S}' T8 l7 K0 N3 Y$ L2 e5 X

% m! }/ C  g) Oserver {
  I7 D9 w! g! jlisten 80;% E* u. O* l9 `7 t  g( ?- G% o+ d
server_name 52os.net www.52os.net;8 H4 d0 c5 E. t( c! d6 A# a

9 C( H* T1 T: F: U4 |3 q+ b' c% U) u9 i0 Plocation / {% F3 q8 v$ k8 ]7 a0 X
    ModSecurityEnabled on;  
6 Q( @+ ~/ h2 H. a    ModSecurityConfig modsecurity.conf;  
5 f. H* o& {+ p, c8 ^8 S. |1 P$ Q
        proxy_pass http://online;  u: r; }' ?2 @/ H) V, x) b" M
        proxy_redirect         off;
  D, P- U6 Q& h/ ~& o        proxy_set_header Host $host;# \3 u! I) L2 q1 H) A5 C
        proxy_set_header X-Real-IP $remote_addr;
; F% s" E, P+ p0 l' R$ t        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;& |; I4 V! J6 k- ]( m) G3 o& U
    }/ K/ x, b5 E, q7 S6 r
}. i0 L9 y, @# j8 e& L; J* q5 D
六.测试
* L; \  J: ]- ]/ u9 B+ [5 ~5 {% i' f! e
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 P3 m5 j# ~: Y, p/ T
. E5 O. w" o# H+ p% ]
<?php
% u& M/ d- t5 m3 ?    phpinfo();    9 m% Y+ v0 @' m, S4 }
?>
% d: i+ g" z7 _+ U- f在浏览器中访问:
9 p$ s$ B- {* Y% G% N, [; ^1 W+ }, b) c& e9 x9 d
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ w4 Q/ T' t8 r. U; ]  N! yhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。7 o+ @/ x" g( ]$ e, y% B
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
- u6 Z- H! s6 M9 h+ A2 X说明sql注入和xss已经被过滤了/ v6 d( ?, b, a
( {* O& g0 L  |! Y8 h3 C, H
七、安装过程中排错5 ]$ ]' U8 v% H" V) |* M

2 i7 ~; ?5 h8 J. _  V1.缺少APXS会报错
/ t; k2 ^1 ^; m- n3 M+ x6 L, T3 ~* r( H8 d2 V/ g/ y2 Q; L1 _
configure: looking for Apache module support via DSO through APXS  p' Q* k9 k! W8 i6 s7 j: g
configure: error: couldn't find APXS! l* _: m2 k' a8 u: d
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. p" ~& T+ e1 ]0 F7 `3 F
解决方法:
6 P# y5 K; d0 Q; e# `, O6 e" n4 e/ ?8 Q& p2 V* Y* r% k  @0 a
yum install httpd-devel
) K: @3 c* c% J3 U8 ]! D2.没有pcre
. P4 @3 [& N$ p& j
" j  g, Z3 [2 V0 b$ C7 U2 u4 rconfigure: *** pcre library not found.
9 E& u5 @7 |5 z' jconfigure: error: pcre library is required
: C- Z- f# G5 T+ B+ U! }解决方法:
2 q& {8 G! j& C0 J: l0 C5 R+ L8 t! y# ?  S- m! }9 X9 r
yum install pcre pcre-devel
2 N2 ?+ F2 c, Z9 K$ _) O3.没有libxml2
$ v* S9 d1 W& T5 k
; U  L( N. J: A2 }; T% ?1 I' g! u( C# R1 k7 j, G
configure: *** xml library not found.& g4 P- x, i( m5 @9 x' g
configure: error: libxml2 is required
4 g( t/ n, P5 x/ o4 y" {解决方法:
% H6 ~  {# x5 F- o
5 K* o1 p8 {6 ^yum install  libxml2 libxml2-devel
! ^0 y( W1 i, E! {6 g- G  x4.执行 /opt/tengine/sbin/nginx -m 时有警告
; z' l% Z3 u* k* d9 x
* t1 t5 Z1 m3 A  d5 k- nTengine version: Tengine/2.1.0 (nginx/1.6.2)% ]3 P( W/ ?, v0 B: R+ E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
7 |: m! y( s2 X& ~) O8 l原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 ^& k/ D& N. T! a0 j: o
1 z0 R2 X' \0 r- J( ?8 x3 A2 F/ `7 y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 s& b" y0 Q. P4 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
7 y$ g% t$ y5 j" j: l2 }2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 R0 u0 u3 L; Z) y9 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 s3 a# i. ?; m  g' A2 C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ c# o, W& Z4 I4 l
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 R, z8 @4 q9 L+ c9 C" R/ h% v
解决方法,移除低版本的APR (1.3.9)$ o+ J. [* t) L' r8 o/ x
1 V+ R4 _' ?- S6 j$ m( M" _
yum remove apr7 n) _! L# ~( N; @
5.Error.log中有: Audit log: Failed to lock global mutex
/ X+ z- l, h" ^8 r/ K% B7 J( F( g8 U3 }. a5 \: p* d0 S- d6 q6 ]3 o
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     / Z3 h) d' Q) k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* r( r% ]0 k- h7 V解决方法:
/ L" |! z( G& V$ Q3 B, k/ x编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' T! ?' U: T' Y3 B6 w# C
) p8 B7 ^) s* j( r7 L; @; `SecAuditLogDirMode 0777. p% e, }; ~8 g) G- J, v
SecAuditLogFileMode 0550
3 u1 }) [' J: C& w  j+ BSecAuditLogStorageDir /var/log/modsecurity
) r: @- G" S4 D( A# ]- ?SecAuditLogType Concurrent
  [; m. D% U  j$ p$ K参考文章:
; r6 U" z8 p. _9 K7 b% jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, T2 g/ Y" x7 K# S4 l8 X, |  Chttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-6 05:00 , Processed in 0.181305 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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