找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12974|回复: 0

nginx配合modsecurity实现WAF功能

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

9 y1 s+ _5 j; [( @, b3 v5 {% j8 Z3 H" R一.准备工作
& O+ q# b, q$ @4 a# H+ R" g
. N) ^# r% V3 n# Z$ J" @  j5 Q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# V0 l% [- r5 J' F0 l% m. X; n3 q# P( R3 U7 _* T2 I
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; z$ i# U6 U1 ^' ^/ ^! p5 c

& T- {1 y, ~: |- v, qmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  x9 E) O+ |0 g6 i, X3 I8 m

. T+ E$ K- q, O8 C$ dOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% R9 B& r( o9 g: O

' E3 k8 E- t, L% K依赖关系:/ y+ C2 T  a, a& D$ Q6 c9 t' F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
8 F7 m4 D7 b4 e+ o, g' L
2 N$ u' s5 ?9 b* p' j, W. P- Ayum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
4 F! w% l7 l4 t* V% X; L/ hmodsecurty依赖的包:pcre httpd-devel libxml2 apr) l& }" j0 S9 f3 n. t$ [) ^* Y

/ _6 F9 o" v* g2 O& Kyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
6 F8 Y1 w  @( S! j+ Y0 M0 W二.启用standalone模块并编译
- E, Z' y3 z0 P* k/ x
2 E& p) _; R7 @* o9 v+ ?5 U下载modsecurity for nginx 解压,进入解压后目录执行:! \# Z. J6 c! s3 m  {: G  X
# S" K0 U- ^) B- Z! e
./autogen.sh% D) C3 k2 H, z. A) C- b
./configure --enable-standalone-module --disable-mlogc
- B& f  }. @4 @make * ]# V; d: O# o1 u3 G( O8 D
三.nginx添加modsecurity模块4 J4 z6 S) F) j& r1 x
* D3 C; W6 |  Q
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:0 h) ]' F- C  h2 I2 P( B# P( h
5 o& A! g6 C6 T
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
( E2 e# `+ \5 c# Z1 o! Lmake && make install" P- a; E' u5 v& ]
四.添加规则
) s8 L! C0 U* s, a. K- I1 P: g; n# M4 Z( {( L2 _5 M6 \9 t
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. a+ ?# @% i. D# [( v
! d+ \% d- x* ?( c* q, h; @5 N  }1.下载OWASP规则:
, S- i. z% [, c3 h) t! E# m- p  F
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" q* E8 y4 o+ x- ^  S5 O, D: b: n1 [
4 f' t; L8 t- |1 _2 Emv owasp-modsecurity-crs /opt/tengine/conf/
; l6 y3 \# s; i9 {" ]# F: `
8 ?& a. a$ c# \: e4 qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf9 P; U- C* I: h, S
2.启用OWASP规则:
1 u3 F5 J" L0 T: v4 @5 t) A. q5 E4 a, R9 E6 P9 T" y3 X6 E3 @
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 t8 Y0 T7 l1 m2 |) A. S
5 h5 `  L6 k0 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
& F8 |/ Y; L% [  I& b% l. @$ Y5 Z
  I# f( ^, m5 Y: O* oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% b) c3 G' v& |: [
+ @1 ~7 K7 V9 w. x0 r3 E) K
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
. d. k0 m0 f+ @( q" [& wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) y; @9 p1 R2 M9 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
# H+ {0 v$ R; J6 r- |0 q) K( U" e0 \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, n; c6 j* q" H# _- {. RInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 p4 u* k8 {0 h  c- b: u6 `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) j% l9 Q( i  i  d, x' xInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
% U' T# c+ K! C# ~4 l五.配置nginx
$ \+ a" h! r, G, j4 _! p
7 T0 e  @0 N# g) M& V( H( R在需要启用modsecurity的主机的location下面加入下面两行即可:2 L6 A& v6 p0 z8 P
  ]: H2 p7 R* _$ P: u) @
ModSecurityEnabled on;  
! c9 j' s% X) c- d" DModSecurityConfig modsecurity.conf;
# _6 q! P1 o  R: I0 I- I下面是两个示例配置,php虚拟主机:
+ G% L8 \) [  A$ E9 `* D( v. K2 L+ i! B# Z; c  r" s) l! W
server {
- r8 S* e' G5 u9 [( F      listen      80;9 C8 }1 [% P$ J
      server_name 52os.net www.52os.net;' v7 R8 d! f7 h
     2 y: l4 B9 H5 ]# N5 {0 T- B
      location ~ \.php$ {
! `& {" M6 U$ c# w" o. |0 r3 \( v- f      ModSecurityEnabled on;  4 `- v5 j: {! i- j7 M, ?
      ModSecurityConfig modsecurity.conf;$ B: m+ I4 q4 g* `* q2 g5 W( {
# \  P6 Z7 A( G, b' P
      root /web/wordpress;5 j, f( {1 L1 [
      index index.php index.html index.htm;# \9 \9 r6 r; ]9 }
  , }7 G) Y) o/ E* Q7 Q2 {: I4 G
      fastcgi_pass   127.0.0.1:9000;& q/ G1 @; V; C1 J3 o% }$ O
      fastcgi_index  index.php;* I& L  U1 T; @7 z9 I
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;: C& u. v1 j) y" i4 r; s
      include        fastcgi_params;
: v! R( e. p) e* S4 \      }
1 b/ a& I* _9 }9 ^  r  }
+ ]1 c* u1 J+ K& P. X7 `upstream负载均衡:
# A) L# G1 x  I# b4 G5 Z; b" u' Q& U! q/ `1 x" ^  _
upstream 52os.net {% @7 x* M8 [5 i7 _" Z
    server 192.168.1.100:8080;4 M1 j/ c7 I& W7 _' i- t
    server 192.168.1.101:8080 backup;- C& Z6 g$ p# r3 s6 _# W( X
}
: }5 H$ K4 G  z8 W
( {7 q4 @. w9 b) \server {
* w% ?: F" a5 B3 {" ^listen 80;6 I( b# H; o# Q, r. o# H# u
server_name 52os.net www.52os.net;
+ Z* j* |, ^$ I+ N9 I* G7 {. C. p* G. m
location / {- |6 L+ ?& V3 e
    ModSecurityEnabled on;  ' \# d8 g/ p6 j
    ModSecurityConfig modsecurity.conf;  
4 R! T6 M) Z7 T9 ?6 M
5 K- W  I2 t5 Y" d) {- u: \8 Y! r        proxy_pass http://online;  ?8 \. Q. W! s. s$ d
        proxy_redirect         off;
3 T* P2 [; p5 O        proxy_set_header Host $host;
& ]& V3 W1 m, s4 Z: `& e+ y: `% G        proxy_set_header X-Real-IP $remote_addr;2 y( r1 t. N" U7 {% c
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
) a; G% g+ K# w& D    }6 \+ T% C8 S: l& ^7 F7 c/ @! P
}2 `& K* z! I/ a
六.测试& i# `9 E6 t: I4 C

" T; R7 Y& Z, [4 {5 e我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 H( b! G, Q0 A! J% [1 L. k  B7 J' i

) `8 E  ]) v8 K2 j/ E, s<?php
7 o; ?  @) A1 A; Q6 g1 Z1 h- d, f    phpinfo();   
  M- F0 y! Z+ ?" q! o; V?>
8 z2 }  e( f# L. Q2 m在浏览器中访问:) l, |2 R) U6 z8 ^

$ ?# p4 p- }& I# H4 e4 Jhttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 i! @: W- h; _9 g# Z! I0 C( [http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
5 W- B$ t& q0 k7 |http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。/ `* \: W' `) Y. f" K/ Q
说明sql注入和xss已经被过滤了
' h" ^/ t# v) a% ~; S1 }: t: s4 G3 a5 a$ x; g/ {1 E
七、安装过程中排错
# K, ]/ ?4 K' P% B: d" x
- i4 a. j, m7 F9 n1.缺少APXS会报错! n2 h) _" ?/ J- z! g

1 t: \1 P' ~& Y4 X5 y; P9 J* wconfigure: looking for Apache module support via DSO through APXS
& W, w( k( I, ~* H( z1 gconfigure: error: couldn't find APXS
6 }5 L! Q. G' y, N, e) \apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 L) }  u6 m# u, M5 {1 A
解决方法:* |. f% x: F4 B# j8 ~, N

8 j; p3 ?- F4 z$ @8 Hyum install httpd-devel( k1 }$ A8 ?6 ?, T
2.没有pcre* B3 ~8 f  @. O7 r9 U, Z

5 _; ~; d! o" d  K* }configure: *** pcre library not found.! \: v2 _7 K. I* \$ `
configure: error: pcre library is required
; u9 G$ r* p+ d6 W解决方法:
1 o; c; n3 @' P# ^& L
, S8 e$ D* b& l9 I: t8 Fyum install pcre pcre-devel! }9 j/ U' I( Q8 v- G: j5 t
3.没有libxml2' }1 L1 k! ]" S

" e- ~4 ~5 n4 A/ a
+ i3 l6 |2 F' z' m1 V' xconfigure: *** xml library not found., ~6 {; ^0 V/ i7 H( [
configure: error: libxml2 is required
# f2 j  O2 [8 x- C6 I  {解决方法:
3 q) T5 ?7 I2 `' T/ W4 _+ }
  U4 C$ U8 u. myum install  libxml2 libxml2-devel0 C, B; x& L- _3 ?& P2 m) E
4.执行 /opt/tengine/sbin/nginx -m 时有警告/ |* M! `* a; e* d+ X
# b8 k/ m/ `' U! L7 C
Tengine version: Tengine/2.1.0 (nginx/1.6.2): c1 e1 }8 i3 y, l. V
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( c: M5 ?9 I, z9 W# t& ~: @
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- N, S9 \  ]! ^, o
; I1 P7 h8 G# \2 n* @, m$ C5 G% n2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% T! J7 [. @$ [+ q: Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
, _& M- m: k( L* X" Q" L$ S; c2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" R. q9 e3 l; A$ c4 B0 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; b, B& b. E: M% k) R' j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"" t5 R4 [4 b6 {$ ^1 N. z
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; }. W) h8 Q2 ]2 W) Q: E解决方法,移除低版本的APR (1.3.9)
- B( v" B- K) o' g2 Y0 j
! k; b8 x  C4 ^1 n. ^yum remove apr: }& a6 y; m' ^% X1 E
5.Error.log中有: Audit log: Failed to lock global mutex
- d+ q' b4 z. ^" x# u6 z* I/ m' q, T4 s
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
$ N6 c. F3 H  k$ Rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, i/ f* J0 R  O0 N解决方法:6 X1 f0 l) y# ]* {. e4 _4 G$ ~
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ c  ?: H+ D5 Z9 p0 p$ r6 M
" K7 d: c6 p: |( i% lSecAuditLogDirMode 07773 Y& F, X8 k- D; ]1 ^$ z; B9 V
SecAuditLogFileMode 05509 k5 y3 J6 j- W8 n9 e4 f9 s
SecAuditLogStorageDir /var/log/modsecurity' c" X) R6 o: E4 _
SecAuditLogType Concurrent
, H; Q% Y8 Z! D+ T7 t6 ?6 ^参考文章:
0 P+ R, }% k) J. S% }# I4 chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
2 d# F, U& Y' S4 fhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-1 01:58 , Processed in 0.068530 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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