找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13137|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  t( l$ B! E/ O6 H+ q! j, k9 n; l0 }  |4 K) E& F# ?
一.准备工作
0 }2 |2 |1 T5 [" I5 p5 [' r
. l+ _' M0 ~2 W4 ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ }  h2 Z6 Z# W; J& C8 O6 t- }! C1 C
* W' l7 ~% U' L4 R8 ~# F; ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 L  U+ [4 T8 r' e4 |/ i4 b: V$ g* X0 ?3 E9 i  {- T# z6 m  [/ ^
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ b+ e2 [4 K' R  g# T; N0 K4 R, s  A! A' l
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ M5 g  G! F$ T2 f) ^2 C* P* z& ~+ ~
依赖关系:& A0 p4 V- K' b1 G
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 K( {! O, G9 W/ x4 I4 Z3 a. L, F% _6 Y/ P; H9 m: Y
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel# H9 u7 B$ [# i: }% r3 Y4 m0 @
modsecurty依赖的包:pcre httpd-devel libxml2 apr
1 K5 ]/ Q7 ]! g! g3 H6 n' w7 v6 K6 x; D+ d+ W
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel$ \( j. l- C1 f3 J2 o
二.启用standalone模块并编译
' e9 ]! Q+ c, {* w1 ]3 e7 r3 m0 D( ]- s- C4 h: C7 r! O
下载modsecurity for nginx 解压,进入解压后目录执行:0 O& o) v9 T6 [" L0 E- \) w
! w# w6 P/ l9 H4 O; s
./autogen.sh
: L4 _, e. K, }5 ~9 ]./configure --enable-standalone-module --disable-mlogc
' J  z  n- s' @5 B5 ]4 u% F: }make
1 a9 c' ?' Z! G$ q' b- s三.nginx添加modsecurity模块( ?. Z( u- G! p2 i' u
0 X4 C" N- |0 I- j
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 T- |2 i2 Z9 h- N! Y( Z
1 u: f4 |3 E+ q! t1 Z! e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine9 P' T! ~" H8 [* l' N
make && make install
" q. |% z2 p: a5 X四.添加规则. V: S6 y$ a* B4 z/ u
4 c. L5 {$ I6 m
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- K* M8 @: [5 }" M* O( j: Q, s
/ ^9 p5 F! s' y6 ^* {
1.下载OWASP规则:! h' s! j. x9 X% d' Q

; X$ y& j: s2 D& R/ Z( Qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. ~- S% F+ _6 ]5 t4 }5 Z2 T% N9 S; ]; {" C0 a
mv owasp-modsecurity-crs /opt/tengine/conf/- R' ]# ?' X: ?( ^2 _: u: `
9 I8 s8 d: V; I' a6 K; y6 {
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf- j( C* G; t  X" G
2.启用OWASP规则:
3 K% z3 f% _; D% ~! H
; H/ v( r. j$ c8 v( o复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( |! D$ d/ Y, j% t$ ?* ?
% J1 e; {$ F+ _3 j2 B4 m0 N
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- @3 o& @) J: }- `: o9 y& Y$ k! }% f. j# G! \
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& c& W% g3 Z9 v; U$ F, i3 o
* q  g( Q( r& q( G
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ q; K; `- K  R4 X/ z' m$ o
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; d2 c1 M! A- h$ y+ M4 Z3 c' _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) l+ g8 l5 J! @) qInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' Y1 y( Z5 o* @, M& U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 o" K7 m) Q! xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 [' \2 B2 b' l0 g/ @: [# h* _Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, i0 w. l2 s0 o+ E" G
五.配置nginx
/ O9 V5 r4 v, O+ k' k
! Z7 G2 t6 [' h2 {, }在需要启用modsecurity的主机的location下面加入下面两行即可:  ^/ J) F' J3 M( W

" ?) y- D" ?& V& f% RModSecurityEnabled on;  
7 }6 i5 w5 ^3 @$ MModSecurityConfig modsecurity.conf;
% W8 v' h# G/ z% h, l+ q" r1 ^下面是两个示例配置,php虚拟主机:$ N3 ]* \9 }7 X  c# c9 }* j' r

+ p2 d7 }; I( R# o! N; l  I5 G0 Sserver {% G$ V4 ^' l  I7 x! h
      listen      80;, \1 |3 d4 }' _" p6 U
      server_name 52os.net www.52os.net;% i; {; n) i4 j2 ?2 i8 E: L. e
     $ C8 W5 `. f  o0 D8 T
      location ~ \.php$ {
/ d- @1 z+ R5 C. m7 @      ModSecurityEnabled on;  3 `; q$ M! C9 c/ N" R! F- Q! q
      ModSecurityConfig modsecurity.conf;
, _/ a7 z. a, o: a. l/ v3 J; H: G/ ]3 |2 g( t( l/ q
      root /web/wordpress;# d/ y/ p! i) F+ ~" C4 n
      index index.php index.html index.htm;9 G" c: I& |  t3 g3 v/ m' K
  
7 ~3 B; N2 A# C1 k2 a/ l! @7 z9 e3 ]      fastcgi_pass   127.0.0.1:9000;5 {) s3 V4 ?  V" {1 t& }
      fastcgi_index  index.php;
' w) Q; [/ u0 Z. I. g      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ Y* M5 E2 h  O1 @7 O
      include        fastcgi_params;+ M, v1 j" x9 v
      }/ w& u" h2 `9 _& `
  }6 L" ]$ h4 p& W" q
upstream负载均衡:8 d5 W( H- A+ C7 ~

( p2 W) z' q% Q' Z$ _; nupstream 52os.net {: Z% d4 a1 a5 y3 a3 o
    server 192.168.1.100:8080;1 Z9 R0 z" F1 e0 R* K
    server 192.168.1.101:8080 backup;
) n9 E4 S/ @$ U) p# J3 q/ s}
) ~; R1 l/ E, b6 n7 D7 f6 Z3 a
$ Z6 k: \' |8 T5 fserver {
+ L# @) a7 f* O- R2 A  b5 @4 x$ H" qlisten 80;' w# t! O; H0 v. G0 i9 t
server_name 52os.net www.52os.net;
  r1 v; T+ n- b9 y. d6 ^& C
. c" ^9 J# |* `, r7 Z- n$ M' ilocation / {
  \+ [7 B% R* \* k0 M    ModSecurityEnabled on;  
4 |& T9 V; |- J: J+ T, I    ModSecurityConfig modsecurity.conf;  . c$ Q3 z+ \; j# c% R
# v3 M6 n: z# w- v7 |
        proxy_pass http://online;
" _% c" Q% P) T  ?' [$ I        proxy_redirect         off;- I& j& r! U% q, L* |& \0 P
        proxy_set_header Host $host;
5 t6 q5 w- ~0 f% i        proxy_set_header X-Real-IP $remote_addr;
( u4 v: P6 k. o1 a( f        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;  Z! _+ U5 s4 w% s: X
    }
1 Z; u' w- x2 W2 A9 J}7 R5 K; {& K! o' L) }
六.测试
9 U' ~: n% g2 ?1 c2 q$ e& j5 c; u% N5 _; h) g" c. y8 w4 n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ r# @1 t. _9 \: h! V% @4 ~
( _' ]1 @; e8 s# R- }# c% r<?php
9 j. b6 r/ {$ v: i    phpinfo();    - w) s: C. ^& @9 k0 m
?># o& {# A0 r4 m$ ]
在浏览器中访问:8 h, ]5 S! H' u# O7 c& o$ W

1 O+ n$ c& ]  d# H4 u4 O0 j1 w: `http://www.52os.net/phpinfo.php?id=1 正常显示。
, j( m" `' A, l. ?$ v1 ^! k$ Shttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
/ k& X1 ~& M! k, B( ^5 Shttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。& V, ?) C8 y& g
说明sql注入和xss已经被过滤了' A  q# Y/ A' J4 c9 }0 x
. }$ ?8 \- d1 T' G: S
七、安装过程中排错
) S9 f& F1 S: C; S" e$ X$ q, M2 W  A9 F7 `
1.缺少APXS会报错
! t9 c) ^/ D; K5 U4 }: ^
) f; Y5 g5 `# n1 s% fconfigure: looking for Apache module support via DSO through APXS
* n. Y% p' {- u' H# Wconfigure: error: couldn't find APXS4 Y) |  E1 t$ E9 D% Q
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( h. N4 D  }% x" D
解决方法:
9 r* m, w/ ~" a2 }! z8 o4 l* p: K2 S$ D4 ?; j4 m" h4 l. }& C
yum install httpd-devel
7 A  U& ^# c6 n0 a6 E9 W9 k5 ~2.没有pcre# N4 X( P3 V2 K7 q. l

) U. `. n; t. l$ i5 r5 bconfigure: *** pcre library not found.0 y( Q, N1 {" R' y! s0 t' z: G  h
configure: error: pcre library is required
& {- h8 F* W$ ]6 K解决方法:5 o9 i" v3 k5 i% D4 V  o5 y6 `

: r+ X7 {+ e; Yyum install pcre pcre-devel/ S* J4 Q) i) o1 x2 S" r
3.没有libxml2: `! G9 h' V  Y* W  D
0 z7 q, Y6 O$ o. q; E
( b3 I( o$ E; N1 G* K3 D
configure: *** xml library not found.
& \' |1 P6 m) i  G5 Jconfigure: error: libxml2 is required
# o# ]: x/ O) o' v解决方法:
, F8 x  O6 I( K* g  X  Q/ |0 d0 q0 \3 {4 R, k. P5 i' g- x$ {
yum install  libxml2 libxml2-devel0 o/ k* v' h% t. u- Z
4.执行 /opt/tengine/sbin/nginx -m 时有警告0 _, W/ a! y* b3 N
5 I$ v. L8 O6 M% ?7 }' w5 t
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( H1 v1 W# n. e" n: @
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 I; o1 E' [3 A6 r" B5 r
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& Z/ F9 p$ {" {( o' g0 v

- a4 i" j# \# c. C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 Q5 b6 B( P- N6 r- r0 ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
6 c* d! x( J) h3 J/ f- r, P4 Y0 B2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& U9 D7 M/ I+ k4 T" b9 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% s. [  A% V& K( S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"  H* _, P8 y- J2 N4 r
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
& X' `" n7 [% s: \( D! @" f4 i解决方法,移除低版本的APR (1.3.9)2 ?( {# `# K9 o' b9 c
# |6 p7 E4 n9 s2 q- Z+ M9 x2 Q) X0 q
yum remove apr
9 W( ]$ s3 N5 i9 Q; u3 P5.Error.log中有: Audit log: Failed to lock global mutex- d* S0 |* Z3 u

6 ~* _0 \0 X. D9 V/ a+ P2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
' Y, _5 t* [" J# F7 G7 u# |# u; N- vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% W  V. l. e. X- ~/ V解决方法:
; Y; ]% R# r0 l* t/ R4 U# k2 k* c, v编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* L4 q7 L0 \( X

1 g6 n6 j5 }5 M" a  WSecAuditLogDirMode 0777
7 T. s$ X* G, `& nSecAuditLogFileMode 05507 D0 R2 B+ c- ]' J- ^
SecAuditLogStorageDir /var/log/modsecurity
& I4 D9 @9 |3 ~9 g0 f; w4 U. y( HSecAuditLogType Concurrent
( P  o. ?' p+ P4 ^( w! h参考文章:: c6 e/ B' i6 d. ~$ S
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ s7 \7 ~0 R0 W/ A4 V5 J3 q
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-18 14:59 , Processed in 0.070780 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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