找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13107|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 B5 ^1 H2 B7 }4 C; N! O3 t2 F. B$ p; g' y/ n
一.准备工作% m1 t: k0 X) S$ |7 W  t9 s
) F3 [/ h2 ^8 [, }: z) P: k1 ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; P5 v! h  z3 L2 u, M* i. J) l5 i0 _5 g3 q' E! @* i
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 p6 m7 K2 y% }0 n9 D9 S
+ s& ]" x" Q+ a$ Q+ E9 i: l/ o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
5 t2 S& q4 W# I" s8 L& g  ]: @0 r% D2 v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: u3 p6 ^4 T9 Z+ V
6 ?* W% A3 c5 I依赖关系:
, A- [) j5 T, y# y8 F# f* n# Wtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 |! Y; ~6 J: [
3 Q: p7 P& }9 h2 Y( X8 u* q/ i
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
, h% S8 Z* ?) y5 P$ e% ~modsecurty依赖的包:pcre httpd-devel libxml2 apr
0 U( b$ B9 a3 X% Q+ s4 q1 u$ q: k
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel0 E5 P" r* [! h% u
二.启用standalone模块并编译
( l2 r1 t3 [# N3 Y* u1 U7 r' W$ O
0 ~) k. q( u9 }3 y1 i0 k下载modsecurity for nginx 解压,进入解压后目录执行:) q1 G5 Y) }3 a: e# [; _0 {
+ b4 o8 r+ x9 t) b. f. r; L
./autogen.sh
2 Z! F; C1 l9 [./configure --enable-standalone-module --disable-mlogc( q4 c9 p2 E4 Q% O" m
make
$ F0 ]- U7 j9 \7 Y三.nginx添加modsecurity模块" R" o5 J8 h, ]+ d  c& H* u$ r3 x5 f5 e
, l$ ~! T/ [4 Y6 R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# R/ e1 {! H7 B* B

) K% ~4 k! H, i./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine8 i- |  g7 T9 \3 s6 U
make && make install' ]& I" \+ P. [
四.添加规则0 ]# q( b( |, _2 O3 x2 H% u" z5 T
  v7 O, _+ h6 M
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& b' P! s- p9 i$ r

' m4 [# |  T1 G# p: ~3 p( \1.下载OWASP规则:; I/ u( F& e" l
0 d3 b3 |  o1 L% W! p; Y7 Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# Z" d; N) [$ R2 o. p
  p' S  |; O) O1 g  `) j
mv owasp-modsecurity-crs /opt/tengine/conf/6 j5 P' S; w( V

& P- k" [% o" W9 }. w! V; ~cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- [7 ^+ Z- A0 i2.启用OWASP规则:, l2 H% b2 A* Z

0 X2 U- x) O9 V5 F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* K* O. W. `  r4 e
4 ]! x; m# s* w( s0 U+ C! k5 Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 v$ W/ E7 l' a9 R9 K1 @; H
( W+ H, H4 ]! s) J' gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
, V' ]3 k8 P' y+ g! T" d
8 |, M" K, `2 \4 k$ fInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ b, c" G& {$ `3 d8 V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ Q) T2 }. k8 H4 GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ ~6 E' }0 l* wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( L. r4 G, @7 A: e/ }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( a% ]4 h* j9 I/ [0 q; A8 v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 Y( g6 ~0 l) V$ e. j# `Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( p7 F; U7 G( V# [) g
五.配置nginx
) R$ @0 F9 w! G2 S" K! p- ~7 R$ |+ E' G$ y* P2 L  j
在需要启用modsecurity的主机的location下面加入下面两行即可:, n$ d+ `+ G6 R# m

) @8 U% r, T) ~; G' l4 T# iModSecurityEnabled on;  
0 K( [# ]& t) l  E" U! _% M# A; ZModSecurityConfig modsecurity.conf;
5 N$ w. N1 ?& h" K, ~9 F. k下面是两个示例配置,php虚拟主机:1 d  z4 v' q# Y- E

" A# Q  l" m; x1 B# Q% cserver {
6 ?4 s8 z' Z2 O      listen      80;. o; z* P3 `" m  F8 M
      server_name 52os.net www.52os.net;
1 }# I9 f/ C6 f4 ]     8 ^/ [& A6 U3 }" X' Q: F1 }: V1 H
      location ~ \.php$ {
8 _% k1 {9 s% k$ W( l6 Y' B! A      ModSecurityEnabled on;  
- N! S  l6 u9 D, c      ModSecurityConfig modsecurity.conf;
: K4 I( U/ r! R/ u- q/ k/ W( a) \! H% t% W8 O2 W
      root /web/wordpress;
/ c, W9 ^: r( a2 _/ {$ i      index index.php index.html index.htm;& I4 A- \3 ^) {8 s5 b0 E4 i
  ; A# c) g4 p( K( D' f2 }7 Y' T
      fastcgi_pass   127.0.0.1:9000;4 `- B4 o* T+ `! @# |! I$ \7 f% y( T
      fastcgi_index  index.php;
5 [# O: c. z/ a% X( k) r% v      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;; J' z6 Z) |. w- N
      include        fastcgi_params;
7 j) f7 ^+ u! G6 H& t. E! t/ E      }8 D; Q" `' o9 q# x
  }: T: h+ \5 H7 |9 y# O& Y2 |
upstream负载均衡:' {/ `6 ~$ |& c$ E9 J

$ m9 p* g) c9 b9 m8 q; b: e4 |! B( \upstream 52os.net {7 U) p( P' E9 j3 T6 q0 x
    server 192.168.1.100:8080;/ K$ B; Z: \, E: y. C* y" V9 i: @" O! N* P
    server 192.168.1.101:8080 backup;
9 j( p' ^! G! ^0 i( C0 k}. x  b' J1 \0 K
. R- k8 h$ p2 t: J
server {6 `) n+ `8 b1 D  x* r+ N
listen 80;- ^+ J0 g9 o7 @2 M8 ?" K" I
server_name 52os.net www.52os.net;! F7 s* n' `9 \
3 i! h5 ]# w) B9 b' _8 X# t# g
location / {
. c5 q8 d/ N: `, f    ModSecurityEnabled on;  . G6 P1 O' g6 J& f- R: \
    ModSecurityConfig modsecurity.conf;  ; z% Y- S6 V2 x8 Y9 }7 a

$ q0 m2 t; P, |3 y) E        proxy_pass http://online;  N( B3 i( G/ p  T) O2 }
        proxy_redirect         off;, E+ ?6 M9 S8 _, U1 i' [
        proxy_set_header Host $host;
% Y/ @" p) B0 T- h* @' e  }        proxy_set_header X-Real-IP $remote_addr;+ o3 a% T% c& s% W
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 z7 E3 W9 |9 S* [! S    }% s4 J3 ?3 k( O4 d
}
; B9 W" A4 Q" v6 L6 ?六.测试
, d3 T6 L0 }7 R3 z. b, j
1 q! C& G! B) l" U我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 h7 n4 U, c$ ]( ?+ h
# z( M, w; m* t, F- w% \4 X# d<?php* |# M; g; m8 M
    phpinfo();    & {2 V" x" s) ]6 _* k$ Z" u3 w" }
?>% W+ d, h, I0 j7 ?2 Z( O
在浏览器中访问:! i  Q8 w. ^! J$ N* a

: |5 g! }; x' ?' chttp://www.52os.net/phpinfo.php?id=1 正常显示。
- _+ U! |) G! ^- ~http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
: P( s6 w! W3 G' \- i& V! W/ fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。6 E8 U* `# I3 Z& r) y2 W
说明sql注入和xss已经被过滤了+ Q1 g1 A+ ~3 R$ |* u

8 ~" M* `2 N7 s/ {/ ^七、安装过程中排错
6 J2 j! |7 Z: ^, _
0 @; Y( u* z' E  r0 P" K4 {8 K1.缺少APXS会报错+ W7 b+ `3 ?3 B% E
7 g0 \$ q* }3 k
configure: looking for Apache module support via DSO through APXS
) A/ j8 c' T7 g2 D" M* N: a1 Pconfigure: error: couldn't find APXS, `8 T4 P: n' q" M# P6 M4 Y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. [' H" E  o6 u- P
解决方法:* i0 u) A6 t4 K  y3 H

2 p0 u3 V9 Q: W7 D+ Yyum install httpd-devel
- L  _5 l3 g! a; {1 W% A2.没有pcre) Y8 y: ?+ V! S2 h
% t8 r1 R& _6 b7 y  y
configure: *** pcre library not found.& ?3 q% O! }% j% `; f) u
configure: error: pcre library is required
5 y. m; k& r) i9 ?! K: T0 Z解决方法:0 r3 ?& Y3 h7 K

2 I* _6 d8 A: h2 W( l; i, f5 lyum install pcre pcre-devel
9 W- ?' u  Q  G: c. u3.没有libxml2
! l- B8 }, X1 I/ b/ o9 Z+ a
9 H& W2 V, d; _$ x  i* q; M* [1 N; X
configure: *** xml library not found.0 Y% o* K2 O7 x3 m
configure: error: libxml2 is required' J1 P3 w4 L# f2 N; e# w  }
解决方法:
( u. c/ W$ N  V2 V$ p0 `5 L& J! R+ z/ [; c) x
yum install  libxml2 libxml2-devel
  D9 T6 v: r; q; n  o1 {& A2 Y5 S4.执行 /opt/tengine/sbin/nginx -m 时有警告. w+ N: l$ F2 J( G0 P2 d
' m* r' z; E- Y& z# H2 F
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( i! R# }; f# j" \3 ^. ]! R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% s% E: y/ `" C9 i& x- k( c原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ ^, D8 g, v( @" o$ C
8 Y! H- F/ R$ o: z. t9 g" b4 M2 m5 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 G2 _6 F6 A( }" I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
( z; C, g- }! A" Z7 N& W2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( P& \1 N( g+ F3 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 ]0 ~' v3 ]: X/ g2 C# k, N( Y- S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 A; s7 F2 u, v8 A
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, U$ C) J5 W0 [6 K% S解决方法,移除低版本的APR (1.3.9)
7 K% E4 z7 \6 G5 Y9 u- j" M& N% ~" F+ A
yum remove apr0 Z( [, S+ u1 j+ s
5.Error.log中有: Audit log: Failed to lock global mutex
8 o6 N* Z  N/ v( x, A+ b1 H; y) Y5 Y4 S7 Q! M  ~8 T9 x
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
9 Z% U$ b* q+ g' g. j8 Vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]/ |7 x7 k2 k9 f& e% a- C' O
解决方法:
1 B5 A% s3 N4 V$ W5 S编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& q. O4 k5 E1 C0 @
+ \5 w, n7 g: i+ s
SecAuditLogDirMode 0777- s3 p8 X- E2 c/ J9 C
SecAuditLogFileMode 0550) |& w7 U0 u/ y. k( f5 y0 h
SecAuditLogStorageDir /var/log/modsecurity
# _7 i6 |3 R! FSecAuditLogType Concurrent5 S# L: m' ]  T( v
参考文章:  O! T: d5 w, ~  {( a. A  u: \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 {# S. y! s! a. Z7 ?http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-15 12:05 , Processed in 0.064100 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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