找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11716|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( H  l$ i- V( I6 U/ u, S

& f  `$ F  w/ H* B7 P5 f一.准备工作' |* x  r7 Q" }& [& I1 C( h

: S) _& d3 K9 p1 E6 ?( n" ]" W系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& Y# l4 M4 E6 g1 r/ M3 _8 i4 _. d- W1 [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% ~: u6 A6 }: m: M" h' }& l! Z- x! m2 {5 V
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 e: |! F( g1 f6 `1 O7 K5 S
' ?; I# ~, y$ Y% Q8 {  X$ W) ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- g' R$ k: o) G, I, a) k& F) Z! Y- ^; n% `' H* W7 g4 W  f: w
依赖关系:/ v1 g# \4 c- t4 `
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* \2 B# J# R  M2 D9 N
1 A/ p8 `8 W2 B1 L2 \' i' J
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
9 Y/ d  m9 v) {1 Pmodsecurty依赖的包:pcre httpd-devel libxml2 apr
7 v6 M$ @  J4 X, e2 t+ D
* Z5 d. b: }  Y$ O& C  L! ]yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel/ C2 M6 x$ b' i  C4 v4 \( r
二.启用standalone模块并编译: U; N6 Z! Z( n1 N7 x
5 U$ ^8 _' d6 E7 N
下载modsecurity for nginx 解压,进入解压后目录执行:
: y8 y6 ^1 P& U; p, G( H  `# [0 }
./autogen.sh, a& _. f; r0 H$ O1 A
./configure --enable-standalone-module --disable-mlogc3 P; s1 B- F* A, s; I
make
: s8 `& R. k. y( Y5 T, b* F4 t三.nginx添加modsecurity模块4 V) x5 Z7 W: n- ~

- D, B: x6 Y1 h; a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 I, H- C& }  G1 ]9 x
6 O! Y6 V6 M. r/ g: c- {: Q1 n) i./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! B( K- N% P, c& k; q0 amake && make install
4 _+ m& e0 S9 O! }6 q( u$ M; w& o四.添加规则: M! |/ p1 f+ u8 g+ H& p8 w
# B: ]+ q! F& j/ Z# K6 M; g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 O9 U% U9 T1 {! i7 o$ n- _+ E; i3 c0 k2 A
1.下载OWASP规则:
( `& Z7 p: B  m. H' {, k! L# t9 M( L1 E; z! B* ?% S3 H
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 u7 Y/ b( r: m$ \

: \5 K/ P( o6 h* O. K& U% zmv owasp-modsecurity-crs /opt/tengine/conf/, e# M0 z- R9 v+ v' I3 O

5 K+ @/ q( b1 R& y6 }2 Y$ d7 hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf6 Q- p4 `, i$ f: ?' j9 x$ D# |
2.启用OWASP规则:
# O/ U9 ]9 g, Y" }9 ^9 o
! e# ?2 U9 t) B1 i' i& t" O# ^9 y! i复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ t! Y/ `1 `7 n* l/ a' b) B& ?
6 ^9 ]" k* ?1 l) b" `
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" F* {% f" T1 G3 ?8 H' t
- J! q8 X& \1 D3 K/ B% jowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ y, T4 o' W: n* p$ j8 A8 e: b, l: H

( s+ I7 I  j3 j. M; dInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
2 X- P3 b; E8 s' ?. W- \1 |: n: NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf7 k2 C6 G( Y/ }. m6 }. I: p+ h1 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 Z% X4 n- I: N4 S5 a  KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( {% L. g2 C# z, [0 `. }' BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% p/ j, j5 w9 ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ |1 E$ [0 g: n, h2 R8 j6 `Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 I7 p3 n4 H6 q. C+ H6 \2 b五.配置nginx6 |5 j6 n/ F3 \7 C
8 v# n. |7 F5 R$ ]* f
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ \. ]: O$ \6 @! v( |% G3 s
2 g, r/ Q$ x9 f' C( _ModSecurityEnabled on;  ' e" d' C# B* @; z; [
ModSecurityConfig modsecurity.conf;! ]6 X- M9 F+ D+ H3 v2 F: k# ?2 {
下面是两个示例配置,php虚拟主机:
3 w( ]5 _5 v/ ~2 x; P, {8 t) g3 Y. ~5 z1 ?
server {
! {( g) d% J; B' V1 {5 K      listen      80;
; Q9 t+ v) h) R% Q. D      server_name 52os.net www.52os.net;  L) B6 ]1 {$ ?
     1 R/ c* a  }" G8 t
      location ~ \.php$ {/ s( Z7 v7 ?5 g2 O0 C
      ModSecurityEnabled on;  
+ |' X  |, t8 U# s  `( Y3 g/ o      ModSecurityConfig modsecurity.conf;) J7 g/ e5 B, M6 _* x
  d/ _1 Y2 j  G: e! w( V
      root /web/wordpress;  S+ @$ l5 X$ H8 ~
      index index.php index.html index.htm;1 V  O+ a# O1 Z
    O  r! B0 f8 X
      fastcgi_pass   127.0.0.1:9000;* K: N* n, P, d- t9 W0 G
      fastcgi_index  index.php;
5 t9 n8 Q; z. A7 P+ z, J      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" T9 H. f, I" X( Z& B$ Z5 L
      include        fastcgi_params;/ c0 \( a- N) @& S; q
      }( t# X  c) Y; |& f/ D
  }
# `  [$ H# k& x& L5 D8 J$ `upstream负载均衡:
2 T( c5 I6 Y2 j4 ~' D
5 Z/ `, F2 H) O- G8 kupstream 52os.net {# ]3 E5 }6 b/ P
    server 192.168.1.100:8080;  F- a2 k, M6 n" E! q
    server 192.168.1.101:8080 backup;1 y! o9 ~/ r( x% s
}; u/ Q/ `+ a% l, N0 V

5 ^1 R6 q; I" h* P0 A. tserver {8 _7 F6 F2 f' @  o6 ~1 s
listen 80;
* X. u3 s; [1 Q' r2 m2 o' V. Hserver_name 52os.net www.52os.net;
8 b$ F* p) q, [! A
: Z; ^& t! j0 Mlocation / {4 `; h& }- r0 j' b
    ModSecurityEnabled on;  
9 z- g6 K4 U* Y: ?2 U9 \7 ?. Y8 }    ModSecurityConfig modsecurity.conf;  
; S# {6 s7 C* K9 Y9 A1 B# n, L
9 l1 {: q4 J  n" ^! {        proxy_pass http://online;0 ~4 f9 g% p2 ?. j
        proxy_redirect         off;
( C* L* o( G5 k2 y        proxy_set_header Host $host;6 ~0 j' m/ d: @, u8 K) P( }
        proxy_set_header X-Real-IP $remote_addr;
+ R- D; b, a  r: @- e        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) u6 |% E- H; w$ X! C    }
+ k" \5 {$ _; D* P}
. T; p* j- g: G3 x' ~- f6 D六.测试
7 f. m& H6 y( s6 D$ Y
. C, l% W6 [# n' L$ j: T我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ z' m, j* D5 o" T$ L, I

4 ^$ W9 C' W6 h; q- n+ `2 U) w<?php
( |: b3 P$ N0 y& R+ j, Z0 a4 D2 l    phpinfo();   
" [* k) _+ S% ^# y+ @1 `?>
, b7 T3 d9 O4 K( \9 _& a在浏览器中访问:6 k; b) G4 G. {6 }

2 M7 C5 u' H. l% [5 y8 chttp://www.52os.net/phpinfo.php?id=1 正常显示。; o$ W6 p: ]7 l) X/ G
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。1 \3 t3 E1 Q$ u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
: h& h( ?1 \* x% V6 m说明sql注入和xss已经被过滤了
% G* R: u" Z' i6 c9 ^
9 J7 G5 s) L( V  S0 |七、安装过程中排错4 g$ `0 p% d+ l0 o- ^; A, L; B$ B
* S- s( ~" T" J( h. h- L$ A3 s
1.缺少APXS会报错
- C9 A: |. y1 J0 E6 P0 `; {% O6 D$ \7 i, m0 w: R
configure: looking for Apache module support via DSO through APXS
. J$ F: G9 {; C/ L4 bconfigure: error: couldn't find APXS7 a: S% w- c* \% Q5 O
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 x$ O2 S" B3 ?8 ]7 u$ W
解决方法:
5 \. I  z+ [- i5 Q  x# F1 h/ Y& r6 Y8 ?
yum install httpd-devel3 E3 P6 Z4 w8 @
2.没有pcre
: \9 t2 O8 A; ]* W2 y
5 {' A  D7 I; Pconfigure: *** pcre library not found.# P6 u8 }; F7 Q. u
configure: error: pcre library is required% u$ N$ B1 d, ?& V3 q
解决方法:
9 W  o; x% m% c5 i2 y4 W) c/ o5 F& ~9 z5 L( B. b
yum install pcre pcre-devel
4 L- D. ~  s$ w8 D" o7 J) ^7 b3.没有libxml2
4 `/ `& X, i' V' G1 }% g" z4 o8 P- K  \
6 F( |, k) W' _# l4 I( q
configure: *** xml library not found.
, j" H# t( h4 I. m" O8 G6 I" Zconfigure: error: libxml2 is required( s3 K+ h1 y( K) h, y3 ?2 g0 H
解决方法:
0 }6 _0 p" E; L. U
! _( x: }" O; p* j' k4 iyum install  libxml2 libxml2-devel
2 H2 \8 k( h" ^" z4.执行 /opt/tengine/sbin/nginx -m 时有警告
& C: d4 F+ M+ `1 Q
; s$ Q: z" {$ m0 F( x. D% eTengine version: Tengine/2.1.0 (nginx/1.6.2)) j5 m5 ]! X. Y0 o( x$ E! v
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ c& i( h4 s$ s' b+ _0 |: U6 h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; s3 t) F* @. }3 b
( c% q2 g( R$ g0 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 R2 X5 \/ o8 p" C4 I. h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 S0 [5 P* `. C' `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 m. y" o! a8 M) B+ ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! [  f" G  b9 W: V* l9 `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
9 h: v' d% n8 z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 W# j2 P1 f- `解决方法,移除低版本的APR (1.3.9)
& |( y' G5 [8 ?4 ^* `
3 e( |* ^$ D/ v/ u! }yum remove apr
( }7 i7 O* G# k2 S5.Error.log中有: Audit log: Failed to lock global mutex
! m# D( |8 l1 z( J
" u8 ?1 R5 j- E! _4 Q: t" ]) `2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
* U+ F# E% l. d( u" c0 I4 ^3 u  lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, H9 j, I/ K/ m# {- O4 N解决方法:
6 k- m3 W+ v$ o7 ?0 b编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% o, v* K- A; P' m; W
8 W: M; t) ^& A- l  S3 ?SecAuditLogDirMode 0777
: R6 X; ~5 @% N- q; r% gSecAuditLogFileMode 0550
6 [7 r4 A, D- U+ lSecAuditLogStorageDir /var/log/modsecurity
/ N) c+ e6 B  Y* ?SecAuditLogType Concurrent
- Y- Q0 _- p! g4 f$ ]! j' u2 Q参考文章:; d% l( w4 Y' {+ t$ A# b3 ?
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 w- `  o$ L, R+ C
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-8 08:25 , Processed in 0.057877 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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