找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12024|回复: 0

nginx配合modsecurity实现WAF功能

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

9 x/ F8 R) q; h0 J- U一.准备工作( M* R5 f2 `+ {2 F# r

/ N" N8 m' W" G' a3 W. U* [系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 k0 t8 l5 }+ L7 b
3 s: Y7 G+ [9 ~9 ~+ C! Otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
: [5 m  b/ C. U# `
/ O/ W1 e9 Z$ |  {modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 e9 _$ Q- K  e/ b3 O

% ^. q5 p' Z( a$ d/ m3 x. a% UOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 O" r2 c  D7 c7 I& S' h+ O
7 N- H0 U( Z& x( f* V依赖关系:! N2 Y$ {+ i, F7 T7 z6 T
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 i+ ^7 E; L' q7 D! J
0 p9 z) w: n9 @& m  W
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
8 a' K; I/ v" _9 \  c$ N! mmodsecurty依赖的包:pcre httpd-devel libxml2 apr
4 J! H. g* m1 B% H7 B1 m! t/ c# ^0 ~- H# q, }
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel- j; F' G6 a6 I' O9 I: g1 N
二.启用standalone模块并编译
& L/ ]* Q, Y; X- j& M
" s  B# k/ t; [$ d2 z) \( h下载modsecurity for nginx 解压,进入解压后目录执行:
4 C& c9 b6 M/ j# ^" e
. M' J# o9 p6 c8 D2 m  m./autogen.sh" d9 t4 b! J* Y6 J& t# [
./configure --enable-standalone-module --disable-mlogc! D- U& }$ J; t  o
make ' @" e' _0 A5 \7 ?. w
三.nginx添加modsecurity模块, k1 ?* \( D  z( d; ?. S

7 \- s( M1 T9 @0 R" C在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
  B- s( P; b9 i" T1 d2 j: G- V# a  |* Q. ^. m1 C
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
8 A& U: a0 J- Q2 n  l: P% Emake && make install
) V8 p' z; U( R" }四.添加规则
( h; l7 y& N/ ~% f! K! O) f- v, \% a; G& Q" j
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 T7 I. V5 t. V( b- S  o% B; f% q  j
1.下载OWASP规则:
+ G1 c% U6 [; ^2 Q% }  f/ w
0 m) k& y4 u, ]0 l( |  Ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 M8 _7 H8 ^& H8 N

8 k% l: }. m7 ?9 Q! K/ }mv owasp-modsecurity-crs /opt/tengine/conf/
$ t, S# t; y& _, n: ~6 i  j; B4 `. A" B% ~+ C" i/ Y: K9 L/ }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf$ v- S8 y- e) @3 _
2.启用OWASP规则:' N% {7 H5 r& j

" R! ?1 q2 P+ g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 b) A& z6 @1 x, t" [- ~' m# k/ L
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 ^/ O8 \8 f$ E( d

, X. m% a7 b, B( K5 T6 f) `owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; I' B3 C5 L0 w* q( H$ Q( J6 F& X0 W, |  J7 q# W3 p
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" c+ k; O2 f6 {6 u0 J  [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& S7 o5 \* g7 O! \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! [6 T& L6 }; u; G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( z' @5 {" l/ s. k% sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# n" s9 F6 t  N% d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* ?) a8 l0 {' ^7 vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# m8 }6 q- h! N2 r! a) W五.配置nginx
4 Y. G' U! T8 A4 y% \& t5 h8 E$ J0 F, Q1 G8 @' a  E; i- R
在需要启用modsecurity的主机的location下面加入下面两行即可:! b9 \0 b2 x- C1 z

- H8 j" U1 R( C) m  }1 r- ?; b, CModSecurityEnabled on;  
2 [4 B% Q8 Q: TModSecurityConfig modsecurity.conf;  X0 ^( |) [5 r# ?1 C
下面是两个示例配置,php虚拟主机:
9 b; u3 S$ w5 _3 J/ R7 v- }" K. G
server {
6 {8 z, j! Y( O& Q      listen      80;
2 g. c* Q3 I9 M* l! {      server_name 52os.net www.52os.net;+ e; C2 Z0 U$ S" K& I
     * R( \3 s% Q* W8 H) U1 D
      location ~ \.php$ {
5 e) C5 A& Z( M8 _6 ?      ModSecurityEnabled on;  $ h4 T/ Q$ C1 C! p
      ModSecurityConfig modsecurity.conf;) G" h8 g* _- c( x1 V" {) X6 g

7 V+ O( j3 @$ V$ Z$ z      root /web/wordpress;- l- J, [3 t9 U& J
      index index.php index.html index.htm;
* Y8 ?/ ?" ?; u, M2 I3 r  ' L" F; r( r' U+ \7 m* u; @+ h
      fastcgi_pass   127.0.0.1:9000;0 R. S6 |- k9 h0 X
      fastcgi_index  index.php;
3 m1 L/ v; O( [5 b      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;9 b+ S# e6 Q( s3 ^2 Z# B/ X, C
      include        fastcgi_params;) w2 ^- {, y3 j6 ?5 b  Z
      }' ]2 |. q: ^+ K, _9 n" ^
  }5 g& W8 D6 R: p, d+ Z& ~8 o4 Z
upstream负载均衡:
6 Z1 c; T9 N; P" Z" o- T: w$ }& b* {& X" p
upstream 52os.net {8 A7 T& m& n* Z; a- d
    server 192.168.1.100:8080;% b( B  |0 K! b8 s0 K) R
    server 192.168.1.101:8080 backup;
" l* k1 E2 ?* A9 A0 H- C, D/ t1 p}
7 F7 q+ L% q9 Q' Y) H4 ?0 }. i# Q6 A1 L1 T
server {' Z( E# a; u! L" d' o& M5 z+ R
listen 80;2 s& h, y" m4 \8 @
server_name 52os.net www.52os.net;
/ N# r+ k3 p/ c9 w5 Z! i& P8 k  A& {' E) d3 y
location / {! O7 [% |" X8 w4 `0 g
    ModSecurityEnabled on;  & G: J" p+ j' h4 ]4 u, F
    ModSecurityConfig modsecurity.conf;  , R+ j, S( S! _. a: ~7 `# N

# S- R  J) j  z        proxy_pass http://online;! o) l4 m! Y5 X0 M5 b! T6 \, l4 \
        proxy_redirect         off;4 H, p3 F: ^0 p+ O2 F
        proxy_set_header Host $host;
- V" A) D  ?8 D! Q3 W3 ?        proxy_set_header X-Real-IP $remote_addr;2 P3 e1 Z$ ^5 o' s" }8 a: S
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
8 O% O8 E6 ^5 Y2 f1 u- F    }5 v- J3 C" s- W: B# U
}
  W" _  \; }, B" \  v0 w六.测试
! E' `5 @1 d2 g& P' y4 A) N/ [, j( g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:' M* U, D7 L5 o  W

' V0 A( {2 Z, y9 ?( N1 i- Y<?php
  [2 n0 b4 Q, l    phpinfo();   
. a) e" F' B6 _& S  W?>
0 Z" |1 T6 f6 u在浏览器中访问:! Y1 v7 n2 u$ Q6 g( e+ x

+ j" u- F+ v# Ihttp://www.52os.net/phpinfo.php?id=1 正常显示。
) Z" h1 x8 _$ @; z9 P6 Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。1 I6 J9 U: k% |& U2 {6 }1 J
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。% y, B; A% g8 _6 P# v4 l; m: X
说明sql注入和xss已经被过滤了
5 k9 R) W- i! l3 w0 _  W0 t( j! k" I9 F
七、安装过程中排错& y$ |/ Z/ Q& v
3 y* n" a2 I' t
1.缺少APXS会报错8 ~3 O6 J1 L/ F* b

4 p. j5 O* g. J* P# W% tconfigure: looking for Apache module support via DSO through APXS
+ o" l2 I' Y2 R/ P! b1 Oconfigure: error: couldn't find APXS; P7 x$ d2 w1 A# u7 a1 n
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 }8 B" \7 Q8 x! m+ |( D解决方法:
  e  P, F7 N; X0 Y" m) j2 {: j- F( M: T* W
yum install httpd-devel
2 x7 ~' |6 l) m; S" Z9 Y, S/ z8 K& x2.没有pcre9 x5 J! B& S3 U( D# {/ d

8 N4 i. \$ _" V3 b9 sconfigure: *** pcre library not found.
0 A+ i& Y" u: g7 v8 S! g% Qconfigure: error: pcre library is required3 M$ b. C. x% y# P+ Q
解决方法:& S. @( f, \. z0 D

6 k& E- z$ F( M" e' uyum install pcre pcre-devel
9 ?( I: h& H) Q9 S3.没有libxml2) U/ ]& C+ ?8 v

* ?- g' q) |: D8 _4 H+ Y2 k2 S0 X1 w
configure: *** xml library not found.
7 ~# Q* e9 Y1 _& h2 y+ s+ ~configure: error: libxml2 is required
: o- S/ e: ?5 b+ W2 u解决方法:9 c7 s: U6 E* Q! t6 I5 @. Q
4 V& L$ q- t; D; R% L" [
yum install  libxml2 libxml2-devel
+ Z$ w5 F; {7 T- P- J$ g4.执行 /opt/tengine/sbin/nginx -m 时有警告* O2 l) x. Y& d, v2 e) H

# L) s( p) w: p# t: N, T9 NTengine version: Tengine/2.1.0 (nginx/1.6.2)1 a9 k* Y& {( Q, Z8 W
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 W7 p7 W7 b5 ?5 L" e, A: Y原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 F3 t6 K3 D3 ]4 t& x/ o3 n
) _2 b3 |4 ?4 P* b8 e: f" d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' i2 j! n' O3 ~1 `4 C( m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
2 u0 s4 ]/ K- m( g3 a2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
2 ]+ s! a, j0 L/ W, S3 u8 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% h# O3 y+ h" L" D2 {2 s2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! `/ q1 Y$ }2 \4 e% b
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 n8 _2 A2 n& q; B  g
解决方法,移除低版本的APR (1.3.9)7 B' Q" Y: n+ K( L; [$ I3 ^

) f* v  U. s. F! wyum remove apr, l/ w8 z& j4 Y
5.Error.log中有: Audit log: Failed to lock global mutex' b5 @9 p8 n" k
0 {( A4 \, \$ N1 Z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     # h+ @" ?) x2 p+ H' z: K, F
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]( X4 M% l" l# B9 R; Z
解决方法:
" u1 r; z# Y+ l! z" C+ I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* s& x& K# R1 M
% T8 l- x& e; K- k% {, W+ kSecAuditLogDirMode 0777
- |% n5 C* p6 m5 W: Z' ?: |SecAuditLogFileMode 0550
+ n  t1 Q2 S. B3 XSecAuditLogStorageDir /var/log/modsecurity
! x7 U" H( ?4 G0 |& A6 A' ]/ kSecAuditLogType Concurrent
" [, Q; x- g/ y8 K+ ^! Y! X/ @1 J参考文章:6 A% n& z6 i7 k2 k3 Z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX# t( z' W* X; M2 {, u! L! I( o
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-4 01:02 , Processed in 0.070806 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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