找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11364|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 D+ `5 L3 r* X9 y
- f  {* U8 `* g8 Y0 s
一.准备工作
; s$ W1 Y0 p$ J
" V2 o2 U0 i6 f) a8 d  A4 Y- @: Y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 K+ w) I2 ^# n8 \6 _5 b7 U
8 E. a) @: C) j2 y; k8 _* m
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# y% i  T* Q; P& h

( d5 s. I/ T. _modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; c0 R* ?% r4 V
. T* E+ |3 \" h! a! n7 L3 DOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 Y9 c9 g1 g6 C# f3 y0 \
; \* s0 o8 ~; {" @  x# B: s( m, n2 j) Q
依赖关系:3 v- K3 I4 O0 M& l4 a% M0 M7 q0 l
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( C8 w/ A$ H' r3 F: ^) Y# p8 H! v; f4 G, h1 _
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel# ^! r8 `7 h" g
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# H/ k5 i2 ]7 |% V% O1 Z. L1 `8 E; `2 T& f( [9 Q2 I
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
/ y& {( G* N7 H$ _. t" F二.启用standalone模块并编译
& K0 n7 M/ r, I. e9 `. h. R: ~+ W  Q; p) {
下载modsecurity for nginx 解压,进入解压后目录执行:
, k9 ~- W) V/ A; ^6 N( s
" Y3 r* z8 }( {' \- `! z1 p, T./autogen.sh) r" K8 V" |4 A2 B3 l6 V
./configure --enable-standalone-module --disable-mlogc0 T! o: v* D7 J9 ~/ F/ \
make
3 d5 R, [/ f. t7 }. ~1 k2 Z& Z. f三.nginx添加modsecurity模块' t' o2 ?2 A' L4 x. w, K/ R. R
+ ]' U; M: Q# I
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, D( M9 V- g: e2 \
$ |: Z9 F  y& ]3 V+ B/ x+ J1 W+ ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  N. S. @  P. S& w; a# t9 ^( Qmake && make install
) Q) S; q( s  N3 Z+ F' \四.添加规则4 A$ A% Z7 K& C# [/ s7 I

+ s) a  R  p1 E# V3 Gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
3 k" P0 C7 D. o9 l5 N# U* \9 K) O, B( v1 \
1.下载OWASP规则:5 l: i. t8 M$ _
7 `9 H7 {5 a$ t# B; O- c
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# i+ }- V; m+ j/ q- b

# t; d7 m/ o+ v$ gmv owasp-modsecurity-crs /opt/tengine/conf/7 N7 l2 J! m, ^; T' q: q
9 P+ }. l" [7 |. i+ t1 g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 Q7 u. Y& R' ?5 P- i
2.启用OWASP规则:
9 s# s) d7 A# q2 m& ^8 V/ p' M
' L' x6 {+ t& C9 I* B4 Y0 g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 s3 v3 p. Q+ t+ n! H3 z, B, h" u% d' o  a9 ^1 }5 ?7 }2 |# ^8 D& t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
9 W3 G3 A) Q. U1 T0 Z! @6 I/ z; I2 P# a3 S9 n7 S/ Q& d- B
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ D& t0 l; {! l1 L+ u4 O. H1 H5 B

6 m" z4 `# ^/ Z+ Y2 V5 T7 OInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 _" V' u. y. P' a) E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' a+ X5 ^8 X" e& Q( z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( e2 F5 N. E: z) ?; f: E' ?* oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ |  T6 L0 L" oInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' N0 g# S; K& L7 m7 h' GInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: L, Q( ?6 c  I* O1 A) zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
9 }4 i) O  M! G5 L五.配置nginx
3 \* D/ A' \$ P9 Y. [
; p( S% |% a, P. ]3 ~0 F在需要启用modsecurity的主机的location下面加入下面两行即可:1 t4 Z  V, k. G' `! z7 Z$ R# {1 f( U
; m5 c5 \; p( s3 d
ModSecurityEnabled on;  
% j& L. s* \5 C  y& |ModSecurityConfig modsecurity.conf;
* {3 r. c5 b' H* N下面是两个示例配置,php虚拟主机:: j- i$ H* A8 L' _2 D9 Q. e/ ~

- ]' I# l5 Y3 h+ A/ {+ L. E# Aserver {" \* f! f: J4 A5 ~
      listen      80;  A3 m% r/ B4 c0 Z# S, L
      server_name 52os.net www.52os.net;0 U/ h' ~7 ]6 E4 \, N
     # p# ^9 a* E. C9 `
      location ~ \.php$ {4 @; I3 T+ l5 g8 T! @% L. Z/ ]
      ModSecurityEnabled on;  $ u3 c" A& g0 i* c5 w  C
      ModSecurityConfig modsecurity.conf;2 \* e3 j/ g' \5 D  G7 j
3 q2 I  d1 J6 D( d. P* r; \
      root /web/wordpress;
! b) Q8 |- Z: V" a" I3 R( s0 L      index index.php index.html index.htm;. n' V4 d6 C8 M/ q5 v/ y
  
4 g0 [, q$ {& m! M/ ~* T9 Q; c4 M/ P      fastcgi_pass   127.0.0.1:9000;
4 L* p4 `& p! O      fastcgi_index  index.php;; |/ t8 |# \* U! ~4 S6 d& ]
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
% ~1 |8 [; A; x: N% C7 w* B5 N: j/ h$ J      include        fastcgi_params;
' Q7 k2 {6 i6 X. D" D# f      }# d% P7 M  r' L( G$ }5 [
  }
4 i% \# z' D; k7 y; F; P& Eupstream负载均衡:6 y( O6 j) W4 ^

2 p7 T0 C! p1 S+ p4 B+ N6 p, o1 zupstream 52os.net {
$ u+ s* c) n& O, Z2 M! Q+ N    server 192.168.1.100:8080;1 C3 B& ?( g0 O6 y
    server 192.168.1.101:8080 backup;
. U# L  r4 ?6 M# y}
, g# g5 I' O7 v% Q; u$ z
9 x/ V- @: x" E# v* zserver {6 g( m# V, k* j/ O3 O1 ?, E- A
listen 80;
0 T2 C3 e3 Z7 T3 vserver_name 52os.net www.52os.net;
/ `1 }& j: o; G. @' R7 X5 Z' B, x1 `, z' G& k  F: [
location / {
" P# n3 {+ ]& g3 B- J# _    ModSecurityEnabled on;  $ H7 P* p( J/ W( }2 H( F# K
    ModSecurityConfig modsecurity.conf;  
1 @, e/ o( V2 P% b; x, h0 p1 Z7 @1 g) B1 ~
        proxy_pass http://online;: V0 Y/ L7 B7 l: `" R
        proxy_redirect         off;
) k6 x# x- L- p        proxy_set_header Host $host;
+ h, s( q( X: y; `1 z3 d        proxy_set_header X-Real-IP $remote_addr;0 H% Q: d& ^$ O. N9 a
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" P$ Q  Q* o3 S. V& F% g2 y- w  i    }
+ o/ e) P# |  w: }" _}# ]+ w# Y& i6 H; a0 r
六.测试& ?3 N( |$ {9 a; Q. P& p% _
/ R- o* C' d( |1 q2 I
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 K1 U- S0 d- o1 j! O

' l( s# w# A/ I# y/ E" t( n8 O  a4 m<?php0 v1 x& Z4 O6 a; F
    phpinfo();   
1 E5 D) Z: o' p+ T3 [?>
6 A# V& u& D' d1 M/ Z  p在浏览器中访问:
" v1 |# w. Z. P. A8 H8 [9 X0 g' ~
7 K+ s% j4 b2 G+ i% b( q! lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
2 Y) A& D1 q/ g+ _9 uhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
% `9 K! d- v" A( fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
! h; |* ]! j1 h* P) S, @# _& _, ^说明sql注入和xss已经被过滤了& ?5 Q' S0 C- B" k% C+ F( l  i
# ?: l" `  C! K) k
七、安装过程中排错2 p0 Y# S) R$ I9 I1 f

9 X% T& i) l" z3 y/ v1.缺少APXS会报错
$ N' E) n5 s3 H- {
! z3 L6 a1 R" u5 g+ X" q6 qconfigure: looking for Apache module support via DSO through APXS% T" p' D: A5 [) z
configure: error: couldn't find APXS
  p" B/ D7 c$ q/ l: K* Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* H' o2 J) v% q/ P+ l
解决方法:  g4 {2 G/ X/ H4 c  R
4 r: g% C9 g* T/ C1 _
yum install httpd-devel
3 \3 i% Y  f6 _2.没有pcre1 n% a; G8 e$ r4 F

9 y" ]# l/ O- I/ Zconfigure: *** pcre library not found.7 K2 E. [$ L" s) W: s" d! B; E
configure: error: pcre library is required
3 I0 y+ X& k1 g. m5 }& X解决方法:
  w6 J2 v1 O' ?0 w
; S' ^; k/ v( R6 Q- Fyum install pcre pcre-devel5 i1 N9 _/ a  V! }2 [4 O
3.没有libxml2- R- \0 g/ s* Y) l4 K4 Y
# s5 e' D7 z! J2 U$ A7 A* `

: z) I6 i6 p/ _" tconfigure: *** xml library not found.
! r7 i$ t( Q* iconfigure: error: libxml2 is required
( |7 a0 q- j- d9 e/ x解决方法:
! J8 ~" ~  j) o$ _+ M# h5 V8 r2 l/ D6 L$ ]4 \- R6 k; h8 E% c6 K  [
yum install  libxml2 libxml2-devel
% c6 r) b5 }+ ^( i3 a. a9 }4.执行 /opt/tengine/sbin/nginx -m 时有警告
: K. o0 P  a! l5 I9 _" Q) s" u1 @0 f- R, I# L7 D' M. d' r8 s5 W( ]
Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 E; q+ L7 D5 m$ E3 s  W5 ~
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& h7 M" s/ v* g; q& i4 w原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 C6 {5 u  w, m: ]& `

9 ]) S% R$ G& H; O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" O. `$ L+ l' \: T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"9 D+ N) L' N2 y, K0 g' X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 d; q. R4 W0 U0 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ P9 f& G5 n) D3 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% ]4 n; {9 m$ j) g3 B- `+ C
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# L6 H0 \9 D+ S) P' I解决方法,移除低版本的APR (1.3.9)
1 J. h+ E; M: |. ~1 j/ e! x7 \0 x
! Q0 Y7 f, `* dyum remove apr: h9 x) I" O7 ^) k& O! B2 w
5.Error.log中有: Audit log: Failed to lock global mutex" ?# _: M# ?; K! p, n

  c) T5 H5 O' g7 R2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock       N) ^$ y1 s9 k$ r- M+ u% l( n7 ]- I
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 ~' G, U# f) M) f% f% w
解决方法:
+ U+ y7 W( A3 W; P. P编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 P8 S$ J4 e; Q- [4 ]# o5 B8 y: g5 S; C2 r
SecAuditLogDirMode 0777
+ a0 v) H5 N. B7 p) j# A7 ^6 \SecAuditLogFileMode 05501 B# g) s' U9 q, \4 l/ w5 T3 Q
SecAuditLogStorageDir /var/log/modsecurity. H- c5 D7 R( I9 `) U
SecAuditLogType Concurrent
+ ~& ^: y! _7 g参考文章:, o+ v- n0 J' h9 A& h2 ]2 Q7 Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
9 ^% E/ m$ C2 H4 [( g. z) ohttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-13 04:27 , Processed in 0.064088 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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