找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12580|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 _/ G5 ?' G5 e6 R/ @( x' {! ~& w9 O' J- @& t9 Z/ `; @' @; M
一.准备工作0 C$ G3 ?1 F% v' @& i' k% t
, |$ _1 w# E$ A# e/ P$ y8 _4 Z8 U
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 f4 _2 D+ s1 H; F$ w' I! t
  }4 V) T7 [+ c& }
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
  Q2 B; O! M$ W$ [$ T6 e2 o. _- Z1 h# s% G) W7 Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* I5 z+ x  G' P. P
8 @( ^0 Y8 e2 e* l; N4 _/ b# d/ gOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ \$ J: v" k5 W
1 d' @' q* B1 X. s0 P/ U' g6 F依赖关系:
5 s4 l, q. C' i& w, _% Ftengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( b! i4 A" B  {2 J

. q+ d- _+ Z# l9 W1 D& z. N9 jyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel+ C- l+ Z& _& J! Y
modsecurty依赖的包:pcre httpd-devel libxml2 apr0 z4 Z( c1 }9 A) z$ [! Q2 j# H* z6 q! A
2 V7 c6 a+ k' E# d$ U6 k
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 @  g  R+ q8 n, d* U+ i- M+ x
二.启用standalone模块并编译
5 _, k+ H4 r% S7 d( n
: O9 \- a8 z+ }. Z下载modsecurity for nginx 解压,进入解压后目录执行:! m  W! @. _' Y+ V

2 S- b6 K& k- X6 M3 V* j./autogen.sh' I, m* p. L) N8 P3 M" C
./configure --enable-standalone-module --disable-mlogc
! {/ C; F- O/ o# }4 w( F. w; Emake 6 k% D* w3 Y# _7 J0 ^0 C, g) s
三.nginx添加modsecurity模块
* F6 F' f% ^# K& D8 B
! a) H' j! c7 u+ ?! u在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:/ Q7 P* u9 {" D  r

' u& ]& e; f' A' S; h5 J( R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
+ U) ~+ F, ?' @9 h5 ~make && make install; u! i& ^2 G% V8 c; e
四.添加规则
% h3 \: t2 j  c3 B, }4 b, X
+ [# `+ W. p0 u$ G) Cmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 y' x" \6 U5 Z
, p  U7 F+ B# y/ I+ t4 q5 Q1.下载OWASP规则:
' O& w. Q9 H# G% x
8 J) X+ R+ A& Y; A1 Igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
% u: y0 Z" T/ @/ c# I6 x; a  t
+ B, f6 P1 Z8 W3 Z5 ~$ kmv owasp-modsecurity-crs /opt/tengine/conf/
. r' \) ]; e: i/ F( C0 \
( D0 n+ Y  `% P# K: {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 P0 T% Q" }$ y) D" \
2.启用OWASP规则:
$ O/ _# j  O) W4 |7 D
- ?6 h( d3 n' L9 R$ w复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
  h2 S. G3 m/ v0 f5 _1 V
4 I" o- Y* a) @2 D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 d) e! A- C* g1 p
1 j5 _9 \/ z' S& s+ R2 yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: j. Z7 D- D4 _8 I" X  R# o( F0 W( S  o* i( z2 Z! g2 D8 j6 M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 s2 M# ?2 ]8 E: B1 s# s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ a2 N- w9 s- U8 YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 l. A' N$ }+ m' [$ o. GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
9 s  x  c  F8 D# zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, G% h3 |7 S, K+ k- n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* y# @2 u" e; }7 J7 p
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf  `/ I4 R8 Q, j6 d+ b
五.配置nginx
6 _6 _3 N" `# B+ i+ C( k: E1 l& F/ Q: t
在需要启用modsecurity的主机的location下面加入下面两行即可:+ m9 I% N$ o" q, r; i0 g0 S+ l9 d
; n$ F$ Y: R7 T6 S3 o9 O
ModSecurityEnabled on;  
$ N1 G2 [- g1 @) p2 `9 @2 gModSecurityConfig modsecurity.conf;. S. S7 e, \# T) K+ @& c
下面是两个示例配置,php虚拟主机:, v* Q; [' r( L+ [) _; m4 j

; |; c0 U- a9 ^server {
* s" T; X4 |2 j      listen      80;" K! {1 l' k6 X" A* j$ A- t& m
      server_name 52os.net www.52os.net;' n) @2 S% R. U
     
* N) m# @7 C9 q, T6 s* `      location ~ \.php$ {
- b  L1 @& J3 g) |- o      ModSecurityEnabled on;  
5 X2 P  s3 z1 j9 |* Z! c      ModSecurityConfig modsecurity.conf;
/ p- K) ?: v. W& |& C
4 G" @* X# W# a7 a7 h% S8 t. q- u      root /web/wordpress;
) J) t2 _5 O) E, t8 Y; j      index index.php index.html index.htm;
& t3 s/ `, g' K2 C2 {  
& ^1 q; R# K: t, o4 S      fastcgi_pass   127.0.0.1:9000;0 I; I6 b4 d% u1 \
      fastcgi_index  index.php;
, b  ^, H- W9 ?3 Y7 P      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;( s6 P% `5 b' s, ~9 P" j
      include        fastcgi_params;
4 J) M$ U3 j) Z$ p3 [3 V5 g/ C      }4 r! e! ~8 g# |& i* o! w
  }
2 i* v+ Z) _) Kupstream负载均衡:
3 m2 F8 q- y% @, z5 l
9 ~% `# E" h! o  J3 Wupstream 52os.net {
4 a4 ]. f$ @- `7 V( h8 F! Q    server 192.168.1.100:8080;$ s$ R0 i4 I4 S0 ?3 ~9 Z& _
    server 192.168.1.101:8080 backup;
, ~. h/ x; T, A* m}! M  }2 V1 a4 V! E/ ?/ E( K7 v
' B4 d' M+ R9 z4 x9 y6 t& B
server {
9 m* Q4 I# ?3 Z6 ~! O( J2 ylisten 80;
' |! i+ d7 }4 {4 ?/ \server_name 52os.net www.52os.net;
( V" h1 d* [0 C' T% i% P) P5 b! D8 R& P4 y2 j
location / {' y: T! G2 i( z+ G  g7 x
    ModSecurityEnabled on;  
: G/ \& B; q( a( Z9 f    ModSecurityConfig modsecurity.conf;  
5 v+ Z; r8 s. d( {, m1 J% H( W0 n
  Z' Z9 u4 }- f        proxy_pass http://online;
9 W5 i* @- q: |( N" b  n        proxy_redirect         off;
! O7 k$ C7 H0 \8 s  R( O        proxy_set_header Host $host;
0 F* A" W9 e/ M3 _8 y        proxy_set_header X-Real-IP $remote_addr;
( u+ A4 F1 p& j) p        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
- O2 m( _' f6 P5 P3 y4 s9 Z0 D( j    }2 ~: M7 O  x# H' P. D
}
9 {9 |5 G" p7 l% A; V6 \- n六.测试1 v3 L% l. ^6 A2 G5 b  c

1 D# e' _1 n0 F我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 ~7 z- D8 R0 k7 [9 t8 F
7 u# B; }- L. ]4 U; ]8 E( r<?php5 o/ p4 x. ^( ~8 @( C+ m) }: y
    phpinfo();   
' p1 S7 E1 [1 T# |?>& h: `7 K0 s3 i9 ^5 u; M
在浏览器中访问:
# c, B! m3 k' w/ E) y2 {* f+ j
; N7 M9 e% A' a; i5 Q2 S% {http://www.52os.net/phpinfo.php?id=1 正常显示。" h6 |. ?- D+ v* Z2 p
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。* U5 _$ P: G' ~- `# b0 g2 [
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
4 t  E" V4 O( B3 N说明sql注入和xss已经被过滤了
+ m3 ?" }1 ^2 I6 @/ a
- ^+ A) m) H  `) l: u2 Z七、安装过程中排错' W$ U* o) J! V" Z/ O9 E
9 h4 H6 i9 L' I
1.缺少APXS会报错
4 \' j8 l5 K) [) A/ n+ O  `* P9 t
" ]( s" K* B9 B& T% ]3 ?configure: looking for Apache module support via DSO through APXS
/ ~5 N! t, s; Q4 ?# G0 k$ ^configure: error: couldn't find APXS
5 V; e5 z3 I# ~- kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& [4 c8 t4 C' s# I# @
解决方法:& H7 Z* L0 h- I' z9 y
+ k# l% B; d( F7 H$ u8 c
yum install httpd-devel% g3 j3 A8 R; B* {" x
2.没有pcre4 g! }$ ~' F$ }3 ^6 v, I

! d* S  R# D! z5 V; S2 qconfigure: *** pcre library not found.6 Z3 p% M. K2 d1 x
configure: error: pcre library is required
! a; K, @* h7 Q0 v解决方法:- W0 w  I& c4 [: z/ {3 \

+ [  n/ p" n/ l" m, ryum install pcre pcre-devel
3 w5 t( z+ p% l! N4 G* f9 o8 A3.没有libxml2
7 {4 ~" r# @$ U" A0 ]% z
5 i( Q0 ^0 Q, G
. E6 {: A0 ~1 {4 G* Rconfigure: *** xml library not found.* ^& x$ O, P9 @' C1 D( @8 z
configure: error: libxml2 is required: S" @- E8 n3 m' I+ Q3 F
解决方法:
  R! C+ z. A  }' b6 H# C
  A, X8 K- j- O7 h7 l. xyum install  libxml2 libxml2-devel5 {* D" J- o' W# w* [/ @" g
4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 }3 g6 G) X+ y  y+ C& t$ _% q& ~
( C9 T3 g5 V3 K6 l  F$ l; I. BTengine version: Tengine/2.1.0 (nginx/1.6.2)5 U* R! l$ h! G+ u1 O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!# t; s, t! M; K; g! @5 w) j
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* b0 o# d# k; g

# D# J0 G+ T( J1 F, ?* a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 [- M5 I8 b0 B8 s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"3 F, }# a: {; a9 H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) V% `/ \/ Q& _! f: ]4 k4 a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* |/ {- @2 Z: P5 J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ ?( D, h6 k# k' N+ U' u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: K; S5 C* D! ~( Q: e7 u6 k解决方法,移除低版本的APR (1.3.9)5 i" w- }* {9 S5 E

/ Z/ p& e/ [6 |+ l+ b0 ?- S! Gyum remove apr% ^; f' ^, ~! v9 K" b+ g
5.Error.log中有: Audit log: Failed to lock global mutex
& ~- ?1 s+ E$ L# ]& J+ K* Y7 d# B- o& ?% E- D
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
+ S, Z5 y# N* t) T/ Yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 z. J$ @' {. h" \- ^. T; r, O解决方法:4 g  B) t. |1 O9 Q( ^8 `
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
: O. G1 G! {3 ?2 M5 J2 w) K/ ]1 C9 |( y& M
SecAuditLogDirMode 0777# V* y; B' n- f' Z6 u
SecAuditLogFileMode 0550
: p8 x! V, l9 H% E8 h( a8 PSecAuditLogStorageDir /var/log/modsecurity1 T4 h% g6 Q% V1 x
SecAuditLogType Concurrent+ x, p; a. C' y0 ]) O* i
参考文章:& s& D: s3 A8 I
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX* R3 V% u) l, d1 a8 Y5 f, Y1 ]" u' G
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-19 12:52 , Processed in 0.073318 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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