找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13339|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% _" S" Q/ t. D' s$ ^6 M0 r4 b4 N! i/ Z; P1 g: c4 M  Y2 M6 l
一.准备工作
% u) g" i& U. U; j2 ~0 _8 \3 K
: w, ?9 w+ ~9 Q3 |系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  J: i1 H2 S1 F* K6 J7 X
' F1 n$ M( a: h8 M, L) N6 @* S
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" C, u1 k5 A1 R. N7 T' S4 X$ S5 r
* B5 m7 u6 s( d& E0 lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& M9 o4 Z+ d1 n0 p
1 i, T! A7 \' Z& w. S( A$ G
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 t6 Y$ a3 c% ?& c  b3 w& z- b

" c/ p5 B+ r5 U依赖关系:' ]0 ~  J* \6 `7 L5 \1 P( R
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& @) r! y' N0 ^' c8 v& ]+ I! u9 i$ A$ z$ Q: S
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' c8 R1 c4 p& \% @! p: _/ }# b: _
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 l( q( J0 L6 s% u# [8 b% v) m
- }  M1 W4 y) u; R
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
4 {  p* |" ]1 A2 {% b- s) A3 V二.启用standalone模块并编译
* y+ x$ K7 G" D/ v9 P  v
; F7 o: B; F, V' J7 p9 ]' E& Y5 L下载modsecurity for nginx 解压,进入解压后目录执行:: ]  L* L! M: ]. [. f

; [+ W- ?# ?9 j8 L- t, g- v$ Y./autogen.sh
; _; l6 z4 ^) f5 L* \1 M! |# q./configure --enable-standalone-module --disable-mlogc
1 S2 W- |4 Q: q5 t0 d" amake
7 z' g7 o' B& z* W7 }& {7 v( l7 b三.nginx添加modsecurity模块
$ u# O8 B% V/ Y" _2 G* ?; E. U$ i. C) O8 x2 V
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
# L! o! t+ Q2 J" L' _9 u0 _7 S' B. W% q$ B) c& ]3 T
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
4 J5 X' z9 v9 c  _  W! C/ dmake && make install
! E6 V' z5 `( l# [- [) C  A, J$ c四.添加规则
3 \1 z6 c4 p# e% l. P: S! X' ^# {7 P  P4 _& j+ Z1 P  J
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。( z% h: e- z) z3 z
& X; |$ x# `/ y0 c" Q
1.下载OWASP规则:+ p. K) A: x/ P& S; E5 S, N5 u
$ ]* v  F* a% E* m4 N* T
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' y+ @9 _& p9 ?$ `' ~' e/ [7 N
9 y( I2 u: e- O( l5 N& u
mv owasp-modsecurity-crs /opt/tengine/conf/" f+ U# g1 q: G+ B) B, S( h# S
8 i8 [! F$ J( N. C
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ p& F! f8 O" _5 z3 G* s5 i; G& g) ]. z) K
2.启用OWASP规则:( g2 p: t# z( ~# e" ?
6 g7 f1 }3 a& `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 K5 g7 G2 l4 d; C$ j4 Y
. x  B! g, l* a5 G; O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 \3 w& Y% W1 F" z$ ]7 {4 o  E3 F# t4 V; ~/ l
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 c3 G5 Z0 L, y
( G4 Z# u1 v/ [& B% U7 Q+ lInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 S( P5 r$ }: M) o* \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! v% p; H5 \% i! w+ w5 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- m) J0 O" F3 t8 D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' h& I9 x- m! L' u. P7 i6 B+ `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 F7 R* `, N/ A. s# iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 t! A+ m% ?& R4 D- x  LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 H& Z( n5 j2 q+ @. [五.配置nginx
: l: t/ H1 b& M) p" f! ^5 S( D4 U2 q' H8 G( i. D* K
在需要启用modsecurity的主机的location下面加入下面两行即可:
5 s9 L6 A% D6 w( P3 t7 [: |0 a: q4 w* p2 k# W3 {3 p/ e
ModSecurityEnabled on;  0 c0 @2 g" Z8 W% p0 M
ModSecurityConfig modsecurity.conf;
( y0 m6 I4 J* Z4 p  Q, M7 F下面是两个示例配置,php虚拟主机:
8 m1 G1 {5 \! \2 W8 m1 _; q- f0 a
2 z5 F! z& }( y, c$ Bserver {# k. V0 @; J5 U
      listen      80;& [) L$ _4 C, l& C( _: W2 g- Z
      server_name 52os.net www.52os.net;  b" i' ]8 u' {8 h; O
     7 f6 ?: ^' S8 w2 m: w
      location ~ \.php$ {$ a% f* B1 `' _. \+ H& a
      ModSecurityEnabled on;  
0 L# p+ M$ }* Z9 K8 d8 f0 `/ ~+ C4 R      ModSecurityConfig modsecurity.conf;0 A1 G: V0 N# h0 F  w3 C6 V, K% I
; P6 _9 [2 [4 W/ B( Y- P" N
      root /web/wordpress;
& z# ^5 f& x9 v: G% E% w      index index.php index.html index.htm;- {( Y) w2 S& j2 q
  7 H) }1 w# K# i. R" M/ Y+ P
      fastcgi_pass   127.0.0.1:9000;
/ I4 {0 \8 M, ?1 U7 t# g      fastcgi_index  index.php;
1 o+ }& [5 K( Y1 J1 \) @, Z      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;, I8 Z0 j; L- t
      include        fastcgi_params;  t, T/ `' M& I. P
      }% _' x1 Q( C4 o( G
  }
( d1 @: C- s2 B5 g  ]) Fupstream负载均衡:
  U" X- e5 H- F
) E4 T$ s% s* G& V" a1 d% zupstream 52os.net {9 u7 f* ^; E$ _8 h6 g2 P
    server 192.168.1.100:8080;! `4 X% p( s2 ], T% j
    server 192.168.1.101:8080 backup;( h' V9 q) v9 c0 M9 D9 v
}
$ s$ F3 j+ m8 Y# F; A1 N6 e" V2 b/ W! @$ t: ^4 ]% L
server {
7 Y& J4 P8 G8 K2 W5 {& _listen 80;4 {- V5 g3 |4 E4 `: \; }- S3 Q1 Z
server_name 52os.net www.52os.net;
8 a5 x; B0 `# j8 S/ X' o) D# `
0 [; D3 \& Y* u; Slocation / {3 c$ [! n, r4 X6 U7 U& t1 ^, ~
    ModSecurityEnabled on;  
5 A) f2 o! d/ O  Z2 o    ModSecurityConfig modsecurity.conf;  3 E+ F& T) S! ~: ?; v
, x% j1 u5 z6 v" D% j5 V# Z
        proxy_pass http://online;' {& U. p1 O) I+ M
        proxy_redirect         off;
' G* z) h, T" q# Y* ?  L        proxy_set_header Host $host;
  J8 W2 G3 W( R1 B+ Q        proxy_set_header X-Real-IP $remote_addr;! g# Y3 @( G, t
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;5 i3 v- a8 g( x( Y9 W
    }- T  I) T2 R1 R9 I, n
}% q* C3 g9 Z: V/ a$ p4 @# M
六.测试6 F& |) w+ o+ P1 Y, M( i& D& w

" Y5 x0 f& ~, n  n; o' q7 R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" J9 C4 Z8 C( Q7 b& w$ l9 F6 N
! M" \3 d2 V  N* y<?php' B5 ~# r4 }+ P  u; K, h8 \
    phpinfo();    4 X; M& Q6 {' j) W. j
?>
' z  ~; V1 }8 K" \9 R" J1 V0 y3 b% ~" U在浏览器中访问:7 y# z( U) q4 W) l2 P' ~8 ^0 r

9 ?. q+ [( \3 y/ Nhttp://www.52os.net/phpinfo.php?id=1 正常显示。2 g$ B. |) B* q& [5 S' Z+ R8 Y
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
; ^) j& w5 V/ @; E0 X0 Phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。/ H/ ]9 g" W$ F% D3 j4 K- v6 V) v
说明sql注入和xss已经被过滤了
9 w- F/ b6 n) j# K8 F6 c7 g* M: f9 _( I% |# c( c$ y% v
七、安装过程中排错
, X. K3 ]7 P  u) w% q
: G: D: h7 u% E+ w: b- C. ?$ }1.缺少APXS会报错
, y; S0 x) ^4 M
5 e3 u/ G1 I1 \! J1 K$ Yconfigure: looking for Apache module support via DSO through APXS
& k: h7 \1 Z# _$ g/ Wconfigure: error: couldn't find APXS, }( G2 I$ R# X, R
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。  O& N. E$ n! Y' o4 x7 t1 j
解决方法:5 I; a: V- u: Z( A

/ y: N5 h6 V: n# e. R: kyum install httpd-devel
, F+ R7 f% O; v/ t& C2.没有pcre
$ G8 n" y3 D; h( m  W* F# L7 e3 ?. v  o7 m4 h" F
configure: *** pcre library not found.
7 P8 H5 u# n$ Z& N  I: Tconfigure: error: pcre library is required+ s" R! c0 M% Q0 F4 z  G8 y
解决方法:
6 A( i) b& d6 `
1 G# h+ r# w5 i" r' N" F9 H9 Z& \yum install pcre pcre-devel* {: v4 N9 X! h# S" f
3.没有libxml2
  R7 |$ ?; o; t; b: H* t' @1 i* K- E( T: o/ `; |; s9 k
6 {# G' D5 s' u) e0 O8 u4 Q% s
configure: *** xml library not found.# t$ R3 ]$ |9 o0 I
configure: error: libxml2 is required
! s+ S! ?, S, y# J; F8 x5 q解决方法:
- T  u6 }: x: e# |* U* e. h
% n" m; \* y7 t  }7 Y6 b; tyum install  libxml2 libxml2-devel# z) V2 n( r1 {
4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 K. q6 p3 i9 Q! l! Q+ _8 f! D) m0 B
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
# Z. ]( c$ ~! C2 Q3 h- f! t% y" [4 |/ unginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ o) P) w, \, O# c- A6 R0 k
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 \& z6 ]$ i4 [$ [# L. l# h/ q. j4 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( P+ @, _* Y# R* Y. j1 g2 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
/ A0 D3 j% ]% p3 Z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!/ U# H" d; n# Z2 \/ z  o' i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ {5 q1 ?6 d4 Y- u- L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ _2 F: v# ]8 i) e7 e2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( O* a: Q7 _( M8 x) h0 |
解决方法,移除低版本的APR (1.3.9)  Z+ g) l. U1 |$ W9 L5 T. W
- c+ P: l, M* u3 P
yum remove apr5 M  ~" w. ?6 V- g) X
5.Error.log中有: Audit log: Failed to lock global mutex% P5 K/ o; o0 _9 }

- Y1 Y2 U. u, F/ w# W& J2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ( Q9 e& _9 G- ?+ o( Z0 `+ K& a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 w  T4 ]6 k+ ~
解决方法:
! T5 [$ ?6 |2 @+ J1 i编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
# I- Z6 J# B, O, i( x
+ s" |! Z- j  I2 i! H$ gSecAuditLogDirMode 07772 l/ f$ B/ Q7 F" x$ s  M. [; z
SecAuditLogFileMode 0550
. |% g7 M' V- _4 j2 i, @SecAuditLogStorageDir /var/log/modsecurity
1 j1 A: |) h; ]4 mSecAuditLogType Concurrent
( ?; y8 `0 X3 g7 m参考文章:- I7 S5 R# L' d: J. w5 U1 w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( z6 e8 o. l3 X/ `- J3 R8 b
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-12-5 01:06 , Processed in 0.076317 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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