找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12950|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" h* z$ x/ W2 E1 J1 S: C
" r6 E. L7 L- B1 \
一.准备工作# {! {0 r7 @- C' \5 y3 ~  u* \
3 [+ T6 o( X+ t7 Q' ]  l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 A7 x* ^5 P! ~
: g$ d. B2 r6 g9 V  j5 D5 T
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% f2 I- _& |$ x- p( k8 `- J8 z: e$ @2 Q4 P/ _1 P/ _
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* b9 b/ @1 K8 Q; i- y0 G
9 M8 V7 ~/ D& v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( |. n# P- M. L; N; f

7 L# g+ W+ i: O; D1 E依赖关系:: p, _, w% F" q2 D
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ S: `8 x, J4 O7 M9 V) A$ k* ^
$ Z4 }# _/ ^( G$ \. z/ X, dyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel/ e/ y! `, w5 L. `, j. q
modsecurty依赖的包:pcre httpd-devel libxml2 apr
9 a" f- c7 W/ s1 [8 Y# W$ M9 C' J# @1 B. [+ Z! C9 G, V' d( |
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel, u% N0 x5 B. u& L2 h, X3 S
二.启用standalone模块并编译, ~9 K$ l! p& z" w5 w
7 z4 @6 a+ p" u$ |2 R
下载modsecurity for nginx 解压,进入解压后目录执行:. r$ H* t2 n( e. W# j' z; R/ V

% e8 o! @$ I7 {7 p1 o./autogen.sh
! r( C1 z& Z2 r% n* l8 W' ^./configure --enable-standalone-module --disable-mlogc, z# ?* L; c' T6 V. x% d
make 8 \3 q0 J5 N' X& L6 l8 W
三.nginx添加modsecurity模块
, j! s, z) F7 l$ I) p
- g& q8 U; \2 I0 B在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 F: l. a2 v6 }" |
% r) d2 d3 G3 T1 P: J, F
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
- @; M0 L$ H6 P. D0 fmake && make install0 s. X$ K! I0 T
四.添加规则& G, T# x# `% @. i( f- Z- R% h) z
" Y) Z2 E6 K; j1 u- d
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# W9 l4 v. j6 S9 p  z

# b7 L& B# l1 X. h. B: R1.下载OWASP规则:9 {, U# Q6 K4 i; D

: B' H" D% O. j, ^. hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs& ^2 N7 ?6 H/ J, m1 @9 O3 m2 D0 O

- l2 G4 a- m" Rmv owasp-modsecurity-crs /opt/tengine/conf/
! a9 J% ?; H' L* I# A
2 b9 W4 x- x# H/ P  k( F( o, R3 [0 @) |7 zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- Q: h% V: X. H4 ]# O2.启用OWASP规则:# N8 }' r  j6 K3 B% [! d
: Q! Y% T$ O% c; _% u: x* Y/ V9 `
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 Q- R( _1 S; j- ]) c
2 F! G! F$ f& C3 b2 G: d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! F" O: h- N  ^7 Y+ I" {0 D
$ [+ P! F" x. u9 }# ]1 K* @9 x! oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 F) x3 w& s: j* x" g) R' u3 W0 f5 O

, ?4 B# u8 n  AInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 E% }. K) i7 Q0 E4 I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 e2 x( N. _1 L8 F* g. k/ _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; ]" L# D  K; \, Z+ v1 z" c: n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( y' L& E4 g* H" Y" [6 g
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) A8 h* |; v, {% oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% C7 f2 w% d* M* u; i; k3 m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 f0 \9 B8 s! s. F: D/ O五.配置nginx( B+ i7 a* w7 A: m  g/ z0 w
( S! T, H% b8 F4 ?: J
在需要启用modsecurity的主机的location下面加入下面两行即可:. Q4 b+ b1 Z4 K6 q: H5 T' R' ^+ |
9 {" I* H1 H$ d5 A$ }* i( S  W
ModSecurityEnabled on;  
' g1 y' M2 V) eModSecurityConfig modsecurity.conf;8 Q( X0 w) m: u7 H5 M0 F+ c9 z
下面是两个示例配置,php虚拟主机:
2 p9 n8 D) k+ h
4 m/ w8 ?/ `' t6 cserver {
  ^$ Y! w9 \0 b      listen      80;% c  I; i" `7 _! L7 Z( B
      server_name 52os.net www.52os.net;' \& R& N; p/ @, N3 v- N) d
     " i% i$ j: Q$ o# f! {' b. k
      location ~ \.php$ {/ m  X( b+ g7 ^" P
      ModSecurityEnabled on;  
' z; X5 y, `- C5 F: V2 L      ModSecurityConfig modsecurity.conf;3 X& C. b$ E1 T5 \2 S( Z
2 A' R) `2 H" R- D/ L; Z: I6 x# v
      root /web/wordpress;
8 d6 M8 I3 F3 Z0 c! ^# t; `7 Q      index index.php index.html index.htm;
7 k  o& e* L& t/ w  
: H+ P! u2 V8 v, r9 n  Y      fastcgi_pass   127.0.0.1:9000;
, B/ ~* S0 C8 n- E3 T      fastcgi_index  index.php;
7 @( k$ {- c4 K      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;$ q) a( Z# s' z* g  ~
      include        fastcgi_params;
/ y# u1 x8 |% x& t8 D! P% B1 x      }
# ?: R- S" v6 D2 F8 Y* }" m  }; g' `% k2 _/ a
upstream负载均衡:
3 r) U* F' ]# F3 {5 F3 U+ @# F- H
8 W  D* r# p; g$ S/ t+ Mupstream 52os.net {0 V/ j" N# t0 F% S& J  M
    server 192.168.1.100:8080;
3 I5 [1 a9 b' l( X( U2 O9 v7 U    server 192.168.1.101:8080 backup;2 N# q7 H( s  E6 H+ F: R* C( J) Z
}
3 V' i& [8 x4 _$ d: W& n/ E4 }0 F8 I; z9 r- y9 `
server {
$ D6 \' C; y$ I6 O/ \listen 80;
5 [( y9 N0 G9 j9 j1 ?3 z8 c6 Userver_name 52os.net www.52os.net;
3 B! |( K8 r( G9 H. n9 _6 u9 e! {: }# c3 V; r4 k, x
location / {
8 q+ i* D3 ]( i# I% }    ModSecurityEnabled on;  1 x+ ~5 e9 l3 E& x( M! |8 B
    ModSecurityConfig modsecurity.conf;  
4 C& e$ ^  O: M: s/ {3 d
' E; X, B  m6 T0 c  g! f7 c9 h6 q        proxy_pass http://online;0 B% H$ V, a9 c5 L/ e4 S& x, `
        proxy_redirect         off;* n7 e" N) Y' U5 P2 \* R# X
        proxy_set_header Host $host;
; ^: @# u5 k) D( j        proxy_set_header X-Real-IP $remote_addr;
' i2 \2 ~& b; h$ O5 ^' n# Q        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
4 S; y: \' u) ?7 r# u' }; E( I" t3 ~7 k    }
+ G1 c! z: A0 n3 F8 Z}
4 q5 U$ B! I) r" C* r& {六.测试
9 R' y- C) x3 ]: P
- M/ S# P1 s7 K! c0 f5 ~/ J- K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 s) i; S8 [% I( Z8 |; {% S8 {8 _- L; d; D: U7 Z; z  `
<?php
$ u+ p7 ?6 C! Z% n% z8 c    phpinfo();    ; O1 g5 I, X5 B% h2 S
?>
$ c( r4 d( g; T7 u8 b在浏览器中访问:0 W6 r/ c3 t) x3 T/ w

4 h7 b( l6 V+ o; R; Zhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% S6 u: t( W: K$ Q2 L9 V4 |3 Ihttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。: r5 F5 e+ c$ p' |
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! N8 e: g" t# U1 I  L% L+ ^# M% V说明sql注入和xss已经被过滤了
" S# w- ], C; P) k6 w: a$ m
! s3 @; y4 d) |! v3 d+ L七、安装过程中排错/ ^) `8 [( \6 Y$ ^6 S' x+ \8 S

8 g, N* \' @' W+ e$ ~# M1.缺少APXS会报错  G! x- |' d% P5 R: [6 H+ H

/ j6 [+ v& K( K  U+ Hconfigure: looking for Apache module support via DSO through APXS% I/ {" m: V/ |& t. U+ L, V" @
configure: error: couldn't find APXS) m; V6 A$ r& Q& R
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- d' q, |  v) o5 t6 E
解决方法:
) c3 v% I+ Y( c' C+ v$ K
5 }. m4 F; Q; x/ }4 g2 F. k6 }5 Vyum install httpd-devel# F$ R' w8 d; v) X* `# w' }% X7 ^
2.没有pcre
0 e/ D" w! p8 \9 g( i
" q$ Y3 r! U2 b5 @" xconfigure: *** pcre library not found.
( E) ]9 |9 |0 A" |9 b6 wconfigure: error: pcre library is required
8 @2 b5 F3 X8 X/ z" F" a, ~解决方法:% I+ y3 y' T! J0 _  y6 d
) c4 _, x6 |0 q4 O/ ^, x
yum install pcre pcre-devel. i- ?" W7 b/ P- m
3.没有libxml2
  F# E, j: q6 h7 s2 `' X: \
; @+ J* N7 I. S& A4 a9 y
4 `" e- A: S  F% S4 @% ~configure: *** xml library not found., [: K( F7 H6 c/ v" U4 m
configure: error: libxml2 is required
7 w% w; O; G7 g! }, p$ }解决方法:
$ _2 W( t# s" X: p! `" n. g
/ [1 h3 h- z# uyum install  libxml2 libxml2-devel1 `; P, u$ d& j2 E9 ]4 T2 i
4.执行 /opt/tengine/sbin/nginx -m 时有警告: V/ Q& o. P3 B6 L! B
6 u+ K" R2 T$ H; Y% }9 V
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
- C' [0 D, R: |6 l. j8 J3 l6 x+ u2 dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!% j& g& p: A3 I5 W
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# ~! |1 e" s, f# y  R, \
3 F$ }' J$ Z2 C. I* U+ g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ R+ J# E* b; l1 U* B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"1 t) v2 s" {! o9 X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!  r- b: `" N: S, k* z$ T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 p! b' u" _# o4 N/ N& @( ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. o0 c5 s+ I% Q# B) f8 Q) h2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ k7 Q! q  V5 e" K/ [1 y" C  |
解决方法,移除低版本的APR (1.3.9)
  E0 x% t8 e' K0 m- G# g7 G8 ^8 K2 w' l" z
yum remove apr
; R# z  ]$ }. ^  V* t5.Error.log中有: Audit log: Failed to lock global mutex
3 _* T- c4 \7 N5 U& k) L8 r9 s' ^5 A! b  j, n/ ^6 R+ [# r
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 |1 H' `0 L) s
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 t! S, i4 I6 s+ X. B1 @解决方法:
/ O( o1 E" e3 k1 q' u' N* h编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" ?  n; g- J6 m
& M# y- u2 v& A, q8 `6 }
SecAuditLogDirMode 0777! S) h8 m" ?7 z& Z6 z3 \$ Y
SecAuditLogFileMode 0550) ?. e8 L+ C, s' L! r/ R/ L( I. s
SecAuditLogStorageDir /var/log/modsecurity
9 }! Z" R0 H/ U+ XSecAuditLogType Concurrent  [4 X/ W- _9 I& O. V
参考文章:1 d$ ~  y# z" Y6 k5 }
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( M  L; b: p. n( Y2 f
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-29 10:53 , Processed in 0.051605 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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