找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12477|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。5 p4 c8 m1 G! s. ]7 f
0 ?& I6 L. T) L* d0 t9 O& {
一.准备工作
" z' Y9 k& F  W9 v$ @& _0 c" k: l- l- D2 f2 @: i
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 j, b' G* y5 A2 C4 X4 p
) v% P( x* B6 d" i- m+ U( Z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; R# n. O3 i% z1 `3 t, p6 j: d* W9 E
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( J# W) l  G; w& i' d5 N8 [6 A& y7 \/ R4 N
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& i4 `5 {% t: @: t. d
  W* M- x3 c! v+ q- Q2 F依赖关系:/ Z) i( P( i+ F$ u1 S0 l2 ~' ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:- \0 S9 H3 j: G( S/ T! V4 E4 b

; _% q& l$ ]. F. L% B# S5 gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
  i$ L" G9 ^" ]modsecurty依赖的包:pcre httpd-devel libxml2 apr
  u& z# L7 ~, N) l5 e
" L" v  |: U8 m8 t0 syum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel2 _1 U* |4 H- ~1 y1 k$ p& c
二.启用standalone模块并编译
& `5 ?9 N) n$ x3 ]9 L8 d- x: V% O" v' t2 D, ~( l# H
下载modsecurity for nginx 解压,进入解压后目录执行:
) Q' Q, s" L3 f) C& r) i! G1 l4 K/ N, l
./autogen.sh
  {" A! k" e* Z" S! M7 _4 ?) N./configure --enable-standalone-module --disable-mlogc3 \- R2 }5 D$ t
make 9 D. ]8 O4 P. o0 i5 p
三.nginx添加modsecurity模块, E+ g) ~0 d6 f5 A/ x8 q9 c5 M; j) U
9 G: U& ^, K9 f+ o) u) N( I, a' G
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
, i" w$ r2 ~0 t& K" P: I" _! `
/ r. ~% \! b7 l4 P4 x/ n./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine" _, r4 f  p8 M2 v& B
make && make install/ H6 b) u) e+ y7 q
四.添加规则0 u5 u/ s5 o2 d7 e. E3 s; n% Z

5 j5 y* A+ G7 o1 ~/ w9 e( ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 o. a8 U3 B! H- P- }3 V% i& @& c
! x! W, P# p5 g/ F1.下载OWASP规则:1 [% H" k% a' w: U3 C5 B: o3 _5 _

6 L# N/ a& ^2 m. k, Egit clone https://github.com/SpiderLabs/owasp-modsecurity-crs  j& F! J( I2 p1 M

' q/ v9 a, x& M- Z- i: D. a- [mv owasp-modsecurity-crs /opt/tengine/conf/) T4 t" b) ]- V- B
; k! ~) A8 q7 _- s9 q" ?
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
/ B8 G. i+ V8 Z+ A2.启用OWASP规则:' a0 G) a/ s+ p& J

$ }6 [4 D- s( o- {/ {5 x复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ V9 |* A1 b( M5 s7 n) f. q, ^
6 A1 v- i' h7 {编辑modsecurity.conf 文件,将SecRuleEngine设置为 on5 X& A2 a% t: Q  j
) l& U' a/ m3 ~& a* K; ?. Y3 o: o
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
& Q, K$ T" P  P, v& z' B" W- l6 m0 f: B' A. G: x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf  Z. j! d! {# T. b- t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 ^8 U( Y, `1 D$ b* v' ~% c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 J& R# e! c5 |# R2 o/ V! C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& Q& H6 g  N9 x; W
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 f2 ]6 Y; C! l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) e$ Z$ @" ?0 h
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& |4 ]0 _0 M0 F2 Q* m0 w, {
五.配置nginx
5 M% R# X) d: @, w
2 F4 S  }4 h6 x3 ]0 {8 U* d( k在需要启用modsecurity的主机的location下面加入下面两行即可:6 f# J' k$ b8 F' v

5 p6 y# H/ f# Y, {ModSecurityEnabled on;  
" m3 |0 g8 b% ]: e$ U  Y, eModSecurityConfig modsecurity.conf;3 i0 e1 t; x8 W! Q) z9 J
下面是两个示例配置,php虚拟主机:
! j. J; t6 P, X- l- U' b9 _. g5 _( z: k* P' h" G2 H* y6 Q
server {  g  p1 [4 w4 k1 e7 l$ h3 j' c  K
      listen      80;
2 k  L) g# a0 }2 e! H6 D7 h7 U      server_name 52os.net www.52os.net;
' U( o: O7 u/ v) \     ! _# Z- G& F+ b% z8 @+ v( O) ~7 T
      location ~ \.php$ {7 Z4 V# r+ `& B
      ModSecurityEnabled on;  2 ^0 O2 r  w: Q) l' O
      ModSecurityConfig modsecurity.conf;% o. \+ `5 K" D& \. G
# [9 @0 m0 G; g* M2 t2 X0 l
      root /web/wordpress;; N" z& R; C! @$ \) x: X2 e
      index index.php index.html index.htm;  r* a( S/ Y# k% c. |8 ?/ S: Q
  % k# C1 M; ?0 ^
      fastcgi_pass   127.0.0.1:9000;
& d4 `/ q# p. \, O7 ]$ t" g- }      fastcgi_index  index.php;' l6 j# p4 m# N8 T
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
1 M6 v" j2 T. j# [! D% v      include        fastcgi_params;
; E6 G* v8 K" c% a( q$ y      }# O( F) T4 l1 F4 {# |, T5 V( W* k
  }1 O, d2 u' @1 |* g
upstream负载均衡:( d  Z8 N* {) \4 I0 i) f% N8 ]8 ?

0 }% b: Z, W) tupstream 52os.net {, Z0 i3 B& k6 l) N$ l' @4 d4 I
    server 192.168.1.100:8080;6 B2 R& R' Z$ L1 g1 Z/ P
    server 192.168.1.101:8080 backup;
: K7 j' g  q, Z3 Q}
4 k1 y/ u# D* h0 P% `/ p* T1 `! \4 g5 T
server {- ?& f: q5 {1 U# ]: L4 X' s
listen 80;6 r7 E( q; t) T1 J# M7 l: J0 T
server_name 52os.net www.52os.net;+ S! s" a! `1 ^0 I' H

% @0 s0 n& w; \1 llocation / {
" F! S5 s. i+ ~    ModSecurityEnabled on;  
) n' B' B7 U, G# U    ModSecurityConfig modsecurity.conf;  0 ~- }* Q+ r/ \! F0 p* q2 `5 u& _
4 W( U0 q+ L* q1 @" l' s* y9 D
        proxy_pass http://online;; P/ h; b; y# Y7 f
        proxy_redirect         off;
( `+ A1 v* T, H( m7 k; m; \2 v        proxy_set_header Host $host;& l4 O& `" x  C0 H, }+ J' X
        proxy_set_header X-Real-IP $remote_addr;& D0 {) d( N  M9 G; I, |
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;! A" Z4 }0 I* F/ ^
    }
3 o8 ]" H& }0 q! S, M}
6 g6 a& a# x9 c# K) D六.测试( `) w% F& i# s& _0 O; N: h

; v, y7 u0 d" L/ y8 V& t我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# G8 k# ^& Y* n) L. t8 B1 G
; f1 f& |3 C7 `: ~, ~
<?php: B% H) `  I. E. v. J0 A- x1 o3 E8 P
    phpinfo();    4 J; {% O0 Y8 D
?>" K8 U) Q6 p4 A' P/ ~
在浏览器中访问:
2 X7 E9 W& [4 C7 }' w7 f9 C# D9 @5 o
7 `& p) I; j+ U* J0 ]/ @2 U9 m5 dhttp://www.52os.net/phpinfo.php?id=1 正常显示。% @& H5 l/ Y  x# s. a
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。" \5 H1 J1 |1 P. M0 z2 B6 Y
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。; V& i- q* `! D: ^# h$ @! A- ~! ?0 `
说明sql注入和xss已经被过滤了2 j4 G: @6 e2 g# [) a  c3 Z$ z) `0 R
! H. r. K- c, W5 a% K& s$ C! ^
七、安装过程中排错" i. r5 A; R8 Z+ [% L, X

; c/ a2 P' o' L; Z+ A9 X& M6 C8 C1.缺少APXS会报错# B8 A: @1 c6 h) H/ O# s

/ u. H6 b% V( z! wconfigure: looking for Apache module support via DSO through APXS
6 \0 g+ Y! r; Nconfigure: error: couldn't find APXS+ T! H7 q: }- R  t# `8 \3 P
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  w- j+ R( Z" i- k( b1 ?解决方法:
' B2 w. L, H( l3 s1 d; v# g/ Y, C7 F& W5 p0 p2 [/ [
yum install httpd-devel! n0 A+ o7 P' Y$ `  n. s
2.没有pcre; d) b3 f0 f. `) j, S1 N& I
9 ^9 z# s; O* X/ U3 @9 l( Z/ V
configure: *** pcre library not found.; p, C0 M9 o& T- i6 j
configure: error: pcre library is required  j. E8 O  }. }( h
解决方法:
# A0 q+ k! Q7 c7 `4 ?' ?3 b, {# f# K
yum install pcre pcre-devel
1 p+ w: Q: L* |: }' A$ p6 g" _4 c3.没有libxml28 D2 K% p+ @3 ?% P' v# S
6 b) R7 W6 z* \6 w& Q

! `5 W0 P7 o- f3 B0 G* Nconfigure: *** xml library not found.
% W1 |! J9 i! M9 Z% v& Jconfigure: error: libxml2 is required
% q* m& O0 Y! e5 t/ _解决方法:, @0 H+ [1 S5 [/ e+ ?( U% B
/ I) l6 n: {2 e
yum install  libxml2 libxml2-devel
8 O( M" f' r. x4.执行 /opt/tengine/sbin/nginx -m 时有警告
7 S2 F$ e2 {9 \" z. {* ?) L$ f3 v8 }3 {/ y0 ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)  a3 K, `5 @0 N" h! e# \
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 w. c; w( o  N: u
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log. F7 x* [2 X. V6 s9 S% n

" x  J7 g- U3 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- c' ]) m( }+ S4 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 W/ x" P- ]0 v
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" k- [% T  K, S7 N1 u" b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
; _9 P* |7 ~% T, w0 p( N, O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ Z2 E5 _$ x: I9 P- C2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.0 \. s- A) I& j  o) A; Y& z! u
解决方法,移除低版本的APR (1.3.9)
8 z  i# m4 h! j6 t( `* x
( ?+ G5 L$ d5 M7 U: N# P( ^3 h1 Xyum remove apr
  Q- V# p3 {5 O+ G5.Error.log中有: Audit log: Failed to lock global mutex
0 |) f- w# x3 Q) t! H& u0 m1 J# j5 P" f3 S- w' L( U
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ; _& d9 @2 ^* b. C8 a# U" f
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) e& Z7 m. ?* j) Y- p/ ~0 D
解决方法:/ D) @+ {( U/ b
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 i- K3 z0 E9 J$ p

/ |# ]1 X5 g" E. BSecAuditLogDirMode 07772 y& d6 a- M: L, W
SecAuditLogFileMode 0550
& _7 W: M0 K, U- D. ]$ S4 tSecAuditLogStorageDir /var/log/modsecurity  l; n0 N$ g! {$ o! l3 F
SecAuditLogType Concurrent7 j0 i" v  L- b+ d
参考文章:" m3 i4 Z, D8 J& w% }
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. }3 L* F( g7 l: L
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-9 03:20 , Processed in 0.110538 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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