找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12934|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ W3 Q$ s9 N5 T" i5 K2 Q5 ]/ h! n" n% q! `
一.准备工作0 O. }/ n* `9 P) n9 T2 f. l8 z
4 z( |- Z; ?4 c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
% R6 {6 d' `6 c% o' N. H( w0 e; C' f8 R+ H# n! g
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 ]3 ]9 k, Z+ ~. w; c' o% ^
4 _  x* Z$ U  h, P$ b- C2 A+ o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# O# I$ L# ?6 W2 S# F
; C! o- j, U( b, O/ Z; x5 X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 M" ^9 H8 Q; I- ]5 T& a: W
* I$ f$ }" Q7 `+ g+ R依赖关系:5 C+ A: Q2 X$ ^' U$ n
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:0 h" R7 q+ l! q8 r

3 }; |) O8 J" }yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel. V5 Q! L2 b, l) \3 P8 c
modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 a; q$ W/ _" }
4 z- \/ R: f% t5 z5 h7 P4 dyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel  }/ M2 o8 R$ L5 K3 y! }) R9 D, \
二.启用standalone模块并编译* l6 t1 W: k) {0 D( G# ^
, |$ p6 @* N# P4 i, H
下载modsecurity for nginx 解压,进入解压后目录执行:% Y$ U# a" }# [2 w$ M
# N( x! Y/ n2 B8 H4 d3 s+ v
./autogen.sh
6 M6 v' T; ~6 b; b; ^./configure --enable-standalone-module --disable-mlogc
  d! |6 ]9 |" e  G; lmake
5 I8 |% v, }: v; ~+ j  P! k三.nginx添加modsecurity模块! G  k; v$ T9 T/ t0 `1 y) E) V1 ~

$ o9 [5 W: U# @5 n# V4 K在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 c9 r: m9 J2 z  _
1 v* J) W# r& h. X; L& A./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine5 B2 U# d6 f& J4 |$ K2 n! {( g
make && make install5 i$ {0 b6 ]* M) p& j
四.添加规则5 w, Z, B: C- k9 u0 z' n' Q" U

% o- _3 E: H9 O1 ^2 Q+ k. @8 Fmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。. M) F, F/ x/ E4 b5 a
' G$ _. i/ g9 h* @
1.下载OWASP规则:# t* n, |1 l5 m
3 A, I% A1 s/ ~
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# Y3 f" V& H# K# k' E, ?! z
3 H+ e1 d& W- Q! y$ Lmv owasp-modsecurity-crs /opt/tengine/conf/
# g2 B4 |; m$ W
* L9 \! W( N  I% F$ ^! s5 V/ jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' g) J/ g# ?& k' X1 s, e( z
2.启用OWASP规则:
# Y) |& H8 W4 h) P2 l, r
( Z7 K% Q1 w/ P; ~% R. \- Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" k) }3 x4 |7 l$ P( N( U" ?
; x2 C) x7 y9 V* t. e' S编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* ~! |& X+ _3 I9 o* w- r

7 ~5 |1 N9 d9 oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 [: m/ |. {; u& q  g2 t" t* j7 m/ D% A$ b7 C5 j, q
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
$ m9 Y# X1 M; J4 ~% kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" W' K- B5 e# H* _/ f) b: ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ M% b2 r5 D' G- `' K* gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ b1 v) E- x# N! \: L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 I, ~+ j- Y  i; m" xInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
/ c) F/ S7 B% P6 o7 NInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
& o9 j+ @3 K( M/ i' E五.配置nginx
2 X4 M# [; c% J% c" W+ k$ `' i3 H3 H! |
在需要启用modsecurity的主机的location下面加入下面两行即可:6 \0 J$ t8 B* N# ?

$ A& h9 ]0 n9 b+ m% ~+ r# \ModSecurityEnabled on;  8 }7 G2 R* Y5 O( y* H
ModSecurityConfig modsecurity.conf;
6 m+ N/ L5 X6 ]4 w6 H6 p, C下面是两个示例配置,php虚拟主机:
  W0 m7 l6 m; C. i8 B$ \; a
: A* w$ ]* S( a7 O4 G2 kserver {/ W% v- O2 a$ c8 z% Y( C* X
      listen      80;% P/ K2 x) k8 q" f1 \" u3 }/ T" k
      server_name 52os.net www.52os.net;
7 \9 W/ H3 z" l. y2 C& v$ f     6 S) u5 Y  H4 `+ P5 y3 M
      location ~ \.php$ {
7 @6 ?! v+ C5 n# O      ModSecurityEnabled on;  . t, o1 I* V% T( Z' E; T
      ModSecurityConfig modsecurity.conf;9 q7 W% V0 s, G" G5 J% ~# R

. D" q4 [" R0 d& J4 f& p      root /web/wordpress;7 y0 f: p2 A" h" x; }
      index index.php index.html index.htm;
/ |6 [2 O8 H# j3 }% @: R  , ]) I! Y- k; d4 @
      fastcgi_pass   127.0.0.1:9000;( d' W% G" y+ l0 {! J: @- z
      fastcgi_index  index.php;
" `0 A% H- v9 k3 o5 b      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: X1 F1 u: L7 y3 m% B! c& N6 }. D! T: w      include        fastcgi_params;# ~  s' Y& J" H  u0 v2 {
      }
2 O0 R% R' l8 ?) l8 m  }
8 W) R1 j- |$ {1 R, Lupstream负载均衡:
" g3 {* S1 N/ p6 i  ~* \# Z0 }) y) @7 {# |+ g& V! f$ a1 Q
upstream 52os.net {
  U% K9 ?/ N% Z' {    server 192.168.1.100:8080;
% \6 T  E  \( ]1 R  q+ P! M  P    server 192.168.1.101:8080 backup;% R4 K9 H5 U! D7 R6 a; K
}1 N' L, O* g9 q- `" S

- F$ M+ i# i5 l4 e+ \1 zserver {$ j! O5 k( V' z: A) b; l
listen 80;9 x+ k( q  t9 M) a
server_name 52os.net www.52os.net;
; [3 d" D7 X6 J5 a1 K+ N7 y4 f! M# @2 p5 K) B* j* ~  w# P' o
location / {5 ~, B7 ]# A7 a
    ModSecurityEnabled on;  
: x7 |  F6 v% f8 L, J    ModSecurityConfig modsecurity.conf;  
; B1 O4 p( @5 M% C1 R, Y
( t. |. u. |9 F1 [7 H, R+ F        proxy_pass http://online;
5 X0 _) F9 r5 F& p' @# O+ U6 c        proxy_redirect         off;
; f, p4 m1 Z6 d+ Z# }* X. ^6 ~        proxy_set_header Host $host;0 K2 E% A: J' d% R
        proxy_set_header X-Real-IP $remote_addr;, @( G1 `3 S) y
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( `2 J3 P9 X3 J0 f  u
    }
' J" o9 l. V$ K) n! z/ m7 {}- b5 N3 p) G3 X9 m8 |  a$ ~
六.测试
$ x) c7 k8 E& i
0 {5 W- m3 x% ]  @* O5 ^我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. H) @% B2 ]0 i' E+ {5 ^7 P( y& M

& r; X1 G/ O* T<?php
. J0 k* G& |+ i1 Z) Q* H* I$ O7 U    phpinfo();   
: }0 K# b- _1 k/ `4 r3 u1 Z?>
6 y6 o3 _& e' W. j' M, l# R+ `在浏览器中访问:
5 Q9 E. J  H: c$ n& l! u5 I% r2 f( m9 \1 Y3 h
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ y+ ~4 E3 F5 _. W. V% g' a, Khttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 c* j$ m7 u# m2 r3 N/ H* {
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
+ Q( ?6 k! j5 |% s7 l说明sql注入和xss已经被过滤了7 B, c! M$ {- j) V6 l
5 X9 ~( z$ n0 ^- F: o  c" V: I4 d
七、安装过程中排错
$ t! R5 ~8 H/ p" V9 l
$ Z/ w7 b& I+ k* E' c" a1.缺少APXS会报错/ L7 F1 ?. L% }* h7 S9 H
/ p0 D! _' D4 k6 T, Y7 @; I6 o0 d! S
configure: looking for Apache module support via DSO through APXS8 g( Q" x6 W2 l& i! K  x" i" }
configure: error: couldn't find APXS; H  l, r( L) H! f- @6 a- T* `
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& P  X* Y. j5 Q( o, S
解决方法:
; S- A; T  Z% p3 C
) P; ~. C& _( r' F$ B" P" }! T9 ]/ t, nyum install httpd-devel
+ N% n/ p  k1 k: `0 _" z2.没有pcre
) {, a0 [0 ^% ]& c# {# ]5 m) ]) b, u% Y4 |! n- N* S$ f# E
configure: *** pcre library not found.; V) u* K# @. O8 ^  M- U% k
configure: error: pcre library is required
0 h9 q6 |5 ?: x* K$ K解决方法:
$ c7 Z( t) @$ h4 [0 F# K! S# w9 ]
% f! u* u6 x( D9 R7 g4 Wyum install pcre pcre-devel+ J$ M+ e3 m; F' s8 e7 a1 ~2 }
3.没有libxml2
  d0 s1 h  h3 ]/ Z1 p% B
$ V( c$ @* I- O0 R& q
' t% F6 j8 [4 p, x/ R' nconfigure: *** xml library not found.) H# v) Y( {- K; J; [6 W4 Z, Z
configure: error: libxml2 is required( {/ _. F, S- |% Y, \) m  [
解决方法:) S" L9 ]3 u' h# X

0 k% K- v4 [" k7 k4 p+ d" Z3 A; syum install  libxml2 libxml2-devel) S& |* N- z2 P* a1 D( E: [
4.执行 /opt/tengine/sbin/nginx -m 时有警告! q- l9 O+ s4 c" u7 ?- [
# C4 K/ T1 c; M* n' Z8 W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 s; D' V! q: ^4 J, ]
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: ^, \/ Q) M6 t, F3 L
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log  F6 l& g& L: z) T/ c' s

. o( S! ~  a7 ]$ l- G9 Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 \. b9 O, E, s4 p1 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"; \7 R; u' {3 N) C* Y: p! Y1 J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ b; x7 r5 O" h1 e" g8 I' e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05") T2 q( f# b* G/ \6 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 v; W# ]0 o' v( q1 c$ y/ e* f: c- a
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.0 a' r6 Q/ X# ?: _6 B. H) j
解决方法,移除低版本的APR (1.3.9)
% x( u, B' q9 H. [- A# G2 v: H, K% A- R0 C& x1 R; Y) |
yum remove apr& w7 T4 T, G1 A8 R! Z
5.Error.log中有: Audit log: Failed to lock global mutex
" z- o! ^: A: ~' d) u8 ~. V  z9 A5 C7 c+ y+ W7 O. z1 \
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 a, C# F' V' u! e$ e0 k  cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* \2 e! j8 ~( X( c; ]+ M5 ~( ]
解决方法:
4 F  p, L& T6 b  p- \编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 L9 G  j8 I+ n6 E% r# R) l7 d

4 s8 M/ `3 X% U7 XSecAuditLogDirMode 0777
* \6 @! r  ^$ R4 \0 B5 K9 @8 gSecAuditLogFileMode 0550( G# G  K. g, a3 H4 d, y6 k* E! F
SecAuditLogStorageDir /var/log/modsecurity
9 z& H) i! H/ l4 a6 iSecAuditLogType Concurrent
$ {. g% X7 F( \4 M0 r* u5 p参考文章:9 b" q9 ~7 `9 x
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& K+ }6 u9 n& X% m+ V
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-27 16:35 , Processed in 0.077764 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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