找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12511|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* }8 F) F) o/ ^8 d# O4 f% C8 B% W8 H2 `: n: S; D5 T
一.准备工作
& u$ z, @' x. [: S6 v3 z6 X
. u7 E: l, _- ]' g8 s3 s" m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 Y  f' q' _4 f4 @

5 S4 u: `# q" q  h& J3 C6 t. utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 N$ a7 q- M; H: ?. z+ G  m/ F  N4 x) K5 P# a* O8 I4 N3 b1 M4 e
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz" m3 C: A% U) I# G& I+ G

" L. |. g* y5 z9 a+ v4 cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% o% V- T3 [5 `; e( {

9 v; N' F  y: X依赖关系:( R& d0 P- C' h4 ?$ l: ~3 z# ~3 W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 ]8 z( g7 ^) T2 R

8 a9 `- s8 M& t* X. Y, A- Hyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
; R' X4 W1 u9 A4 q0 Z7 hmodsecurty依赖的包:pcre httpd-devel libxml2 apr6 u8 ^/ p3 d# m, Y( G
2 N0 k- r' M9 R
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 b' n6 L4 S4 _' F( D# G- O- J
二.启用standalone模块并编译
/ A) z! g: ~6 g/ x  K, d5 ]
; X0 h3 e( a6 c- h: y7 |下载modsecurity for nginx 解压,进入解压后目录执行:
- m9 H3 t% |" [$ L% z
* r6 m/ Z4 c* N# G& Q; h9 k./autogen.sh
, ]" X! Z' c" `7 T" ^./configure --enable-standalone-module --disable-mlogc
* P; W2 i6 ]' z5 Y& Amake % A% [0 v; S% |$ z3 k
三.nginx添加modsecurity模块
0 j& T" e/ W8 h9 d# y2 z, c2 j3 Z( w1 ]4 a" w9 \
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# M6 F! f6 A9 H1 \- \( o: f6 }' ?8 N( R

9 S$ o1 j4 l' F8 j! n3 J, @6 L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* ?6 p7 k/ ]* V7 t
make && make install
4 u# a9 R- l0 E/ r( B+ s四.添加规则8 j4 H( i. x3 f0 ]8 C
. O$ m. I7 a5 R# ^* j4 }, `
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& t2 i/ g3 {: d% h/ v: d$ t3 k  d# s2 S, [. R3 I: _. G4 T
1.下载OWASP规则:+ G8 U/ B9 ^% E1 X0 e% F5 m
0 w- M9 r% ?; B& ~
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs; e0 o" u2 l) p2 f* l8 N+ |* w
4 g6 ]& n* i# M( R/ I
mv owasp-modsecurity-crs /opt/tengine/conf/0 m1 r/ h( I' _0 A/ Q5 d
7 h# F, E. {( Z7 f$ g9 l
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; i9 [/ S; f( m: l; \2.启用OWASP规则:
. w' y2 {* V' P) ~% d4 Q3 b2 Y( R7 g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 ?2 u9 }) ]9 k9 ~5 U

; Y7 ]' _, v' B3 B0 i# i% m5 e+ H编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" O" @1 z7 O' H+ K7 w
( o- s" F/ Y) G& A7 k5 ^owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 X( ]% u# K7 k' ?6 j# _9 D. f2 o' V
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 g$ M; B; w. P! v3 f9 W. t2 k+ F2 P% IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- X- ~8 w/ L% X, y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 m) j2 `# [9 \' z' [: iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 E5 D/ g: F. |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf  `& ~  v0 p/ R$ O' Q; E' N5 i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 q  p- V& s9 ^5 m, S+ r
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 l3 z2 ?" z, G  \五.配置nginx; S% G' ^& _4 {0 v6 c

% V. T' J! i+ I( g! [在需要启用modsecurity的主机的location下面加入下面两行即可:1 Q% I5 c/ C) R- V

: U9 T. d* Q5 I. \ModSecurityEnabled on;    |! V& z% c( d
ModSecurityConfig modsecurity.conf;
0 x) |4 X( A, T  z下面是两个示例配置,php虚拟主机:  \* l" T4 r& @+ a  v3 L
6 s' L4 U' @  |9 W9 L: h  w: z  S
server {( R) V8 S, q, d# A
      listen      80;
. t7 e# Y  |& `2 \6 f$ k      server_name 52os.net www.52os.net;
& u) t( l0 j5 t, u2 Z     2 N' y9 Y7 G2 e: {# a6 c
      location ~ \.php$ {
3 V# T* _7 ~- ?: {+ }1 j8 @3 p  l      ModSecurityEnabled on;  9 q9 N0 C4 ?( f) b8 B6 {$ g7 h
      ModSecurityConfig modsecurity.conf;
/ A5 M. {  o' i# P# ~& G; ^: q$ h" x. I) S
      root /web/wordpress;8 S2 _# I5 X# F: o; n/ j3 |* K, G, T( M$ Q
      index index.php index.html index.htm;  n/ [$ m4 s# J9 H9 T4 N2 F
  
7 e. _2 D& H$ Y! F' b$ T4 {      fastcgi_pass   127.0.0.1:9000;
, l7 n! n( N) s: t, B7 P      fastcgi_index  index.php;
5 f& I, C* A' o9 d3 Z$ J  k      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  l& S& ~- q( A      include        fastcgi_params;
2 d$ k3 {+ S: I+ k: t# r      }: h+ w+ l) X( t; r3 g/ U
  }
& v6 ^% c" s! ^5 qupstream负载均衡:3 d* }& Y1 [4 l8 t
3 ^* D$ r# L7 G: F
upstream 52os.net {# ^0 ?' {7 \9 q! Q) j6 S0 `6 e
    server 192.168.1.100:8080;  k- f9 Y4 [6 Z2 G
    server 192.168.1.101:8080 backup;
, I& @% g. D& s1 X+ t( U! ?}
5 a! [" t3 L9 T/ v' o
4 ?& J2 J' d! l, b7 ~( N3 i. |server {0 r' d* W. r& H  C
listen 80;4 K* K, C- n4 d4 ~% r- A
server_name 52os.net www.52os.net;
1 Y. g# j0 p* g6 h7 R* l! ^+ p, K. U/ t. ]
location / {
% {1 s5 k8 U! h' |    ModSecurityEnabled on;  ; X* }: v+ q- z
    ModSecurityConfig modsecurity.conf;  
$ M' v+ }2 A+ }. s. F2 g0 s
/ B, {- f0 r1 ^        proxy_pass http://online;
) Y& Z0 E' l9 a1 e, O" f& w/ B5 v        proxy_redirect         off;
8 e8 I' p! c2 Z# m        proxy_set_header Host $host;4 H( \2 i. }3 X! T) e# Z
        proxy_set_header X-Real-IP $remote_addr;
6 \3 r: A6 G: c6 V8 t        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
1 i' t6 n' u" \% N8 A    }3 Z8 S2 M* V  _0 o* t
}
9 y& ^" Z+ d8 A& K4 M六.测试
* _# G3 N* R/ X( r8 a* V7 S6 B1 _# Y( }
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
! L7 w  T( [* z
- c. g0 Z( T  ~. m+ a9 i' Z<?php
7 b4 K$ u2 e: Y1 X  d& K    phpinfo();   
- n4 u4 K+ i, ]. I6 \?>
1 C5 _0 ?" y, H; v在浏览器中访问:
( a6 b5 j( D) n, Y4 Z  I# ~9 j
: l- M' y) D) R! Fhttp://www.52os.net/phpinfo.php?id=1 正常显示。
0 ~. N4 F! s* i6 A' uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- T' v/ N# y7 p% c
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
, P0 h4 ^9 H4 I+ U4 g. e3 \# n说明sql注入和xss已经被过滤了
* e3 P! ~8 [$ B- h5 F! G3 A
: E+ B( w0 [' {' g七、安装过程中排错
1 T5 @/ a# Z% Y  B4 x
6 F# i8 g! S6 E: k" c$ k- H" p3 R1.缺少APXS会报错
2 t) R- o/ [- P7 g# X* t3 H% I" y7 g+ K  N; Y' f
configure: looking for Apache module support via DSO through APXS
7 `3 X  q. C; c5 C1 v# }configure: error: couldn't find APXS% E$ M4 C/ H; b; h) t! x
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 h9 z9 e' W8 g1 X- B* u& w4 M) [8 U解决方法:
* q$ F3 o8 V3 G7 p3 K  M
' {: @% `1 I% _' J7 p( p% ^yum install httpd-devel
  j1 p. q3 i, }3 w: D! c9 M' e2.没有pcre- `9 [# W3 j0 b& q

/ g% N& r  H2 s7 I' oconfigure: *** pcre library not found.
8 E' k' u/ X. l: d) O" Nconfigure: error: pcre library is required
3 J$ `; s* C0 _8 X% k7 o' o4 o解决方法:
& W  g' z: i+ I4 t4 z4 @( L+ ]: F6 P  K8 S3 l/ S
yum install pcre pcre-devel
6 W0 Y5 u$ X" T  k5 S5 r+ Q3.没有libxml2
! l' M' K, y) d" b
4 A1 n) T( F5 m8 t- `5 l% b$ y0 j' o( [
configure: *** xml library not found.
, Q) D: d# a! q# `1 H+ \6 b, Q  }configure: error: libxml2 is required
: f! c0 c0 u2 X解决方法:1 Q+ E4 U9 O: C, h, S# U

$ W( ]: ~, l' W3 qyum install  libxml2 libxml2-devel
- A4 @$ {: k* I' J$ V% _/ q  u& m4.执行 /opt/tengine/sbin/nginx -m 时有警告6 l+ G6 T6 m: B0 \) Z1 x" K% ^2 o

+ j" Y, R; n% j: LTengine version: Tengine/2.1.0 (nginx/1.6.2)+ {5 m/ N2 X: Z( Q$ m+ o0 k
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% e1 c% P) E- |$ v- u+ v  x原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log" G7 a; p& U2 t/ q- a- L

) B# |# L5 G7 N/ I6 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. ?) ]2 o, a" {) F6 o- l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
0 }, ]6 K: i' V6 V* S$ v" P+ J2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) o: d) {- h+ S) F2 K2 i, s! U' W! Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
2 I1 m7 m: j7 n8 A( b- [: T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 z( g& _. V2 w9 C8 \
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
1 P7 h8 {( ?4 L+ f6 {: o解决方法,移除低版本的APR (1.3.9)4 ?+ M8 \& ^) q1 b( E! W

7 ]5 v# }  O  n5 a* _yum remove apr7 y7 t/ j) I  ~
5.Error.log中有: Audit log: Failed to lock global mutex
7 `$ I9 k, H- K7 W& [0 `( Y) x7 r. [5 o- P. X4 c/ h  y
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
$ ]! c- P1 ~+ k1 jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 ^& S  w" j& E7 d4 N1 ^, ~' b) M; |
解决方法:
. c! t& |; S. M: P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 p6 S8 ^! J: S0 X$ W5 k5 u: s! D: E& {0 ~! G, P- P. B
SecAuditLogDirMode 07776 A* ~0 q# K2 j: `
SecAuditLogFileMode 0550
, t; |8 \3 L& t" Q- MSecAuditLogStorageDir /var/log/modsecurity
5 ?) m: ]. n- ?# c$ N; `: Z; MSecAuditLogType Concurrent
* d3 I6 }1 r, I" D! y参考文章:9 S, Y2 Z- T$ \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX: f, ?: C( Z; L; x
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-12 12:51 , Processed in 0.073577 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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