找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11868|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。% t( Q5 y. l. l) ], w. \( B9 {
4 x; s! V* [% ~8 a$ @5 n! `( ?
一.准备工作$ W5 I" b1 V. r

! d% }5 W0 C7 v/ M( g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ R1 `& p! T4 J0 V+ t. t8 X( _! @3 G
' b2 T/ e' T( F8 S  y
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz  c) W3 c; ^# B5 F4 U

; l8 m2 q) k: Y9 `: a, B3 l+ V1 Z3 ~2 a0 \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ c/ x% E! Q6 a' Q$ T3 [. \3 s. X$ S9 Z1 y! V) R: }
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 Z7 @* w+ M) n7 _4 G
, a- A3 f0 |4 N1 |( F
依赖关系:) p% g; K3 I) N$ W
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ Z+ r5 y$ f9 `( Q3 o  r1 b6 e; J1 |* \2 A" D
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel  O' E8 Q, y: @" f8 V' D% M! h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( A+ q- ]$ q6 Z' h; C& l: U% ~0 S
0 j: Z0 z: o3 P. zyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 n" q5 `! J1 Z% z; K9 \* C
二.启用standalone模块并编译& _0 b4 v8 ^. `
$ Q+ w" I, k9 s2 X1 ?3 B
下载modsecurity for nginx 解压,进入解压后目录执行:
* r; N6 d  o$ ]( ]7 ]( Z8 M: m: x8 i
./autogen.sh
! _! U: w& e( j./configure --enable-standalone-module --disable-mlogc
" N, d  |. w* N$ V5 ~. hmake
1 v7 g& e- a, u4 F& A三.nginx添加modsecurity模块9 W' a4 f+ E( R
' e% d1 f. {6 H  N% G& w$ P# Z  Q0 \
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( O# K' r+ ^* F. F2 c
5 F! z& T# ]. e+ u$ h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! _( n* l7 ?1 u6 F% [0 X  q2 ]make && make install5 O; o$ S% `* G8 @% G1 H- V
四.添加规则
  N$ b; `8 D7 P1 B; k1 B/ {% h
, `2 I. F/ g' Hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! F. r' p. k' Z% _* z
, A9 \$ v; ~. J$ q0 ?1.下载OWASP规则:
7 g3 i" k* D% x- V' L
5 G) G: s8 |$ y# Kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( H/ _5 i8 L, L. C  M8 O. @5 C0 K) e. z3 V# @
mv owasp-modsecurity-crs /opt/tengine/conf/% a% ~' c  W; V& w. }
, R  N" j( _4 I$ V8 g# v
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. G5 S* u/ M8 L- h: {9 Z
2.启用OWASP规则:
! X4 B9 ^& w0 E7 U. G# a) a% l" e% c3 _! ^+ k' m4 N( I
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 F  [% H/ k' Z8 q1 Q
) W) `5 u6 R+ g$ o! d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 F& m; s8 f, v5 R  v
4 S0 u6 V% ~6 |/ s9 Cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! L  {9 M) I! d- J; B  _3 V  M7 Y- T3 g/ b" Z! X6 t4 g' f
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 f* [& \% Y2 `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: V4 V" s$ o, j, E3 h' n
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 B% l/ A& K0 v% }! I5 w) M& R% QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 z3 C8 j0 t! V" [! @1 r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 Y. n$ K( \# E& LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) l" i9 x! ]+ l/ CInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! Q) M; M( L  c2 v) J. C& o
五.配置nginx
. c* _) E/ h! ^+ D7 d1 r# u: k% x
在需要启用modsecurity的主机的location下面加入下面两行即可:
2 p+ c1 B& ]+ o2 [& W* u$ u6 m. l2 P( R% B/ P# G3 E3 h( J& y
ModSecurityEnabled on;  
) ~. ^2 \- `5 p* C9 t$ P0 j% YModSecurityConfig modsecurity.conf;
" S9 ^9 X# j2 Q! c# ?6 u下面是两个示例配置,php虚拟主机:
& A, E' M: a- j) Z1 k6 u
+ n+ N( U8 u2 L& x# H0 |  rserver {0 P0 L9 K* _' T6 K$ X4 Y2 ?
      listen      80;; P  |9 {  ]! i3 G  u
      server_name 52os.net www.52os.net;
% Q, p( ^! D, ^     
7 e; S- {9 Y8 D! S6 _( \  r! C8 @      location ~ \.php$ {5 \' E+ ?% a/ M' W
      ModSecurityEnabled on;  : T7 ?! i8 e, |
      ModSecurityConfig modsecurity.conf;2 S7 p1 I/ N$ U
7 O" ~5 P; K% H3 E7 M4 F- t7 `
      root /web/wordpress;. k1 A5 r9 e# j6 |7 Y0 V5 |
      index index.php index.html index.htm;" R8 j3 ~2 }1 _+ U1 {+ [
  # [+ k2 |& n9 L
      fastcgi_pass   127.0.0.1:9000;3 h. }) [* ?6 I& M5 G2 Y
      fastcgi_index  index.php;+ U% o) Y' l. J8 U+ I* w% v0 `
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;" X& L" e4 L+ r6 e' a  D7 m
      include        fastcgi_params;0 K) Z1 k; n! j& P5 b
      }
  f: b$ a% P7 w4 ~' q: l" k$ ^  }7 V6 Z0 `8 b  G4 e+ z) R
upstream负载均衡:
# T8 j" C7 j  u/ g4 e) c; h
( ^, I- g* {+ M* Z, v6 Xupstream 52os.net {
/ U, s* F2 B4 a    server 192.168.1.100:8080;& b" h2 V' V$ o9 q
    server 192.168.1.101:8080 backup;% {: n  p0 `( ], y1 [9 W6 B
}! I! a9 ~# [- l1 g3 E0 V

5 y- Q1 @% m& J8 r; s) vserver {
- z$ S0 _7 e! \: ?$ s6 Rlisten 80;. @7 r" _" l2 v( m) s
server_name 52os.net www.52os.net;
4 b+ I6 r, t8 T3 ]; h; P3 U) y& s4 R# o) B" I% x
location / {/ [3 q  N" X! d
    ModSecurityEnabled on;  
* O6 ]% s7 \1 U) p0 d1 [2 n    ModSecurityConfig modsecurity.conf;  
# g  @, J: U+ K4 y& S
# n9 w5 e! f2 t        proxy_pass http://online;
$ _7 Z& p( ~' |9 E) _8 }3 H- Z! O0 W        proxy_redirect         off;
! Q; A6 y) [5 _& M        proxy_set_header Host $host;
7 Y5 Q- x. w; H! K6 R" }# o        proxy_set_header X-Real-IP $remote_addr;
% N0 R6 B  J( I' Z' \# V        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;% Q( K) @. l) }7 }' x
    }
  ^' d1 |$ l: U- j}
, g) ?5 H' g9 @六.测试
1 h% s9 Q' N$ T0 f7 Q0 C& r
3 ?$ i$ j2 v  x( m' e我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 _# ]9 g; {  u# _4 C

2 m9 X- e! L% X6 a4 Z, E. ~<?php
3 m" d7 Y( _7 R3 j    phpinfo();    " M$ f0 y  g5 ?% i. B
?>
" O, @: e" e- `在浏览器中访问:6 L1 ?  T! @9 l4 c
$ B! y$ ^& I. J
http://www.52os.net/phpinfo.php?id=1 正常显示。1 V( x: U1 q$ \/ E7 e1 x$ W
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。  q% n  L" \; A$ u: c- z
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
6 |) R1 Y: p: ?( o& F说明sql注入和xss已经被过滤了
+ J9 e; w  V* F$ @) l
7 x* x6 d" W/ L' }( I, c七、安装过程中排错$ f" D/ `9 M) I1 D- T

/ |. v3 R+ S" H% y6 m' |/ F1.缺少APXS会报错4 `/ |% Y6 d4 g. C) ~( Z
1 s+ e& [0 i5 ~: J) @5 ^/ ^- r  c
configure: looking for Apache module support via DSO through APXS
9 T, D0 W% p& p8 r9 ]configure: error: couldn't find APXS
/ P& T9 n) u, Y3 iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 H! ~: N% p" V2 Y& n解决方法:
  {% G9 L8 v+ {4 O$ L
$ q7 b: G0 r  O. s2 ?yum install httpd-devel' Z( \4 Q+ B% g( _
2.没有pcre8 d) Y4 N) {8 y. a4 O. j
- I" `) f! w4 B/ X  i
configure: *** pcre library not found.
% c* j4 _5 x1 y5 `3 L2 cconfigure: error: pcre library is required
2 X$ [6 d7 I; ]1 t6 i! Y6 {8 z解决方法:" M  H3 l( Z5 ]

& ?# b5 ]9 G0 I+ o# nyum install pcre pcre-devel) M/ F9 C6 {, V, B  q) P; c
3.没有libxml2
1 ~5 C. p! e/ j, q
& Z# `2 l6 _* _7 q' t5 z7 L+ f- e) ?' f7 {+ ?5 S  N6 y
configure: *** xml library not found.
/ v. ]3 x/ y5 L& Nconfigure: error: libxml2 is required
, g6 }0 [& G' O8 V解决方法:
4 o  W: h+ s/ p' P- r- H  b* j: X7 K! ]- a
yum install  libxml2 libxml2-devel
! w1 C" i8 ?2 m) U' @4 ]  G4.执行 /opt/tengine/sbin/nginx -m 时有警告
; k$ D' _, Z! g! B( w9 W$ X5 E# E
. F4 e  ?% l5 w2 T; D1 h) D% XTengine version: Tengine/2.1.0 (nginx/1.6.2)
' [0 f; f; M: o0 {) c, knginx: [warn] ModSecurity: Loaded APR do not match with compiled!0 L! a9 U  B' h" F. i+ g
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' c0 t5 f- {9 W9 H' r4 g) [
" [0 k* l: |) [- u  r$ Y2 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! f) X! ]" h0 x7 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9". W3 P7 v: L6 A& P+ b' {( ?
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
8 z6 J" K8 u8 S* R; Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"" k% w1 y, g5 n6 o1 S/ Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 d. d- e7 S3 r5 F2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 q* H* w* R5 n/ m- T2 B5 n+ _
解决方法,移除低版本的APR (1.3.9)1 t: Q8 Z- h3 B4 w

( I8 E! \- U- D" C9 o$ m4 Syum remove apr
" |. O; ^2 ~& r7 D5.Error.log中有: Audit log: Failed to lock global mutex  a0 N/ Y8 \% ]* q- A. w# q
; b: G- U+ W6 S$ m9 ?$ G  F* J1 m
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
& w5 [! l9 L  Yglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
: c* f0 X$ \' N解决方法:+ [+ f  w; [" c* s) \; y/ `
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 [2 _0 ^; @7 Y  j2 i2 n# R5 _

, \. B' k: G9 M! oSecAuditLogDirMode 0777) v, i" \. r# d: C: N
SecAuditLogFileMode 0550' ]7 ]" e4 l, X
SecAuditLogStorageDir /var/log/modsecurity  P2 d" z, T; `
SecAuditLogType Concurrent6 ]. S- \6 }& l& D9 x
参考文章:
: Z( R. ]* g% d3 j( chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 u, Y% g% Y7 V: A1 [: l
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-20 16:55 , Processed in 0.097358 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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