找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12242|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' k" [; v& _8 ?6 R% Z7 Q8 g0 ?

  C: J: `1 w+ r2 G# ]$ u5 s# c! Q一.准备工作
! U$ U8 U# b- j/ ^5 L
; c& [4 _1 @8 @$ K  R5 P! Y6 s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% O. l8 x3 F0 g* K
6 V+ m2 B3 b. F$ g  F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& |+ U9 }6 Q/ R8 E* i+ q) @. V8 U8 |& i2 ~8 g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 [3 c9 N2 l; K6 x+ l
% I6 D! i3 v* Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs) I+ j, l2 p. |8 h3 r
( _5 B8 ?$ ^4 a& t0 }( N  v8 D* d
依赖关系:
0 M5 s; f* j3 \# H, V! l% V+ Y- l" Etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 p& y* o5 J, B- J0 S( C$ H( `$ ]$ P! A" A+ H* L8 ^
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel: G' ~3 I& S6 C
modsecurty依赖的包:pcre httpd-devel libxml2 apr; A4 J, Q1 T; |# w4 v1 I2 y
, X4 C! q1 u# F6 U8 y! K- ~& g
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
9 H+ n; L0 l& c6 n二.启用standalone模块并编译9 ?. b+ D! G. o$ \5 n: N# n2 a" I/ R4 ?
7 g, A7 J$ @+ f( q
下载modsecurity for nginx 解压,进入解压后目录执行:
' x' G& A' Z* M3 b4 k# H* X
; h  p" H7 [7 r4 Z./autogen.sh3 I! \) @: s/ V- {1 Q" u
./configure --enable-standalone-module --disable-mlogc( z, ?, ~. x2 Y! [$ E, P/ S
make + j# c7 G' v+ S
三.nginx添加modsecurity模块
/ {: n( l5 ?$ `. w4 F2 `8 `
( V1 j  r' |% g3 }# V  [1 \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" w% F0 i6 N  L) w0 t- C& u
2 g6 V+ X8 ?# @8 m- ?. v6 W./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine4 N7 Y- c  x7 ^! X- j) f- w
make && make install
* Q/ b, Z( u% @+ t/ }1 r, ^四.添加规则
2 @4 H0 Z- `/ B; z: V- X7 G# s& I7 ]# O( K6 a. D4 G" w, j& a
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ P5 W  i0 A" r$ H

- R8 J! K) `, q  L2 S/ W8 q$ P) O( R1.下载OWASP规则:; @  V) ~. X  r6 ^' y+ O1 Y

7 }; b+ V) A" f6 T" f, x5 z8 Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ x+ q/ j) s! R  I% u: A& ^( n( L& B* j( a9 M. _2 B* w4 k2 L" P
mv owasp-modsecurity-crs /opt/tengine/conf/
" Q8 ~, |5 u( Y( N& S9 R: W0 h( H) O' _8 U& V! j3 j* W
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& D: A/ I3 Q; j9 N; f& f2.启用OWASP规则:- Y/ s( e  W; O: {

. l# R# |+ @% `复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* D# }( J& t1 N/ s' P  l/ l
4 o1 M, j/ c/ w* F: ?8 y4 t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. S: m/ Z; _0 d; u! J+ u
& S3 W" X3 O" ^% o* r9 Gowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ D$ _/ j. H9 i7 D3 W) B. _' S
* @2 b' x( O7 H. w2 N: z
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 x3 ], M+ P8 Y+ i+ ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; z5 O! A: l- k0 _, l: }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( @* W+ ]" r: `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 j, K: X0 v7 {& Q* e+ V4 e9 e- i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
2 l6 D. O0 d3 i: o* yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; O* Y& |- w7 `( m3 F& l! Z) `* |
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 I' D) k# {# K) M! {五.配置nginx
& |7 L# o- z3 b' w$ [) V# W( N& _! s8 w* k/ M+ h" s+ C
在需要启用modsecurity的主机的location下面加入下面两行即可:) l" D8 g, u2 }8 U8 B# L4 w3 \
0 _3 a1 _, }: s) y
ModSecurityEnabled on;  2 ]5 `& E7 i6 u8 B7 X  B+ w& U2 ^
ModSecurityConfig modsecurity.conf;
! `5 w% I( ~- h4 g, G( q- h% n下面是两个示例配置,php虚拟主机:& `4 E# e/ N: J. G9 m. ?& x1 k

" m, I) j. G; p$ `) Kserver {4 I# ^  j+ B0 s2 X  E- E
      listen      80;$ E/ |/ d1 |/ x# T# N! v7 j
      server_name 52os.net www.52os.net;
8 G1 E) B7 j" b" J     
; g. d" V' }. Y) O6 @- L0 v1 A      location ~ \.php$ {( @# e' i0 o, \- {
      ModSecurityEnabled on;  & T! P, F- a+ ~5 Y9 ?
      ModSecurityConfig modsecurity.conf;
4 ^4 S) s) J/ J! f/ I' C6 q/ ]- Q- [2 ~+ x! M2 Z5 @6 g! [& l! C. i8 H  r
      root /web/wordpress;
6 `2 t4 N+ H2 I/ L: V7 H2 `* d5 o      index index.php index.html index.htm;
; l; a0 M7 t6 Y( Y1 d  
; J* [; e* W4 F9 J4 q8 t      fastcgi_pass   127.0.0.1:9000;
1 g6 |- q0 p1 o2 U  C      fastcgi_index  index.php;: l# a' r& c& {0 y# n! d5 c/ p: n
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
0 p9 z7 z# V4 [5 V; H5 Z      include        fastcgi_params;
% ^) j4 F- }. I4 q      }
1 u, E6 u5 _; {. s4 k  }; l& q+ y4 k: E' v/ g
upstream负载均衡:8 u' X7 i! t# c
8 {( e2 q- d; \) M
upstream 52os.net {
% x  K% I& C6 Y- D3 C+ q0 i    server 192.168.1.100:8080;
& t0 o$ z" k4 B. t2 j& L0 y    server 192.168.1.101:8080 backup;
; a' j) G7 B/ N' r' h}" W& [* ]) K7 Q0 \# C% s% Z
4 ^, {: j; a6 p  t8 ]* [& S6 U9 L
server {
' p/ j+ S4 g) b7 b5 }' a$ zlisten 80;( L* A+ U* z" I+ G4 H/ i
server_name 52os.net www.52os.net;, |! L- h+ i: Z: a

4 ?- S2 ]7 B+ a; J" m6 n9 o* [& _7 w; j3 }location / {
$ X# g2 S' i0 l, W" f! j    ModSecurityEnabled on;  1 r' i! L9 Q$ w6 {
    ModSecurityConfig modsecurity.conf;  
1 R- s: i4 Q, G9 r+ m7 E' C/ N# _3 B% B* Z7 N3 m
        proxy_pass http://online;
$ c4 {2 r# {) X! f* S3 h! f        proxy_redirect         off;
3 d, ]+ v0 {8 b8 x        proxy_set_header Host $host;$ j7 C0 W7 u6 S8 G
        proxy_set_header X-Real-IP $remote_addr;
7 R3 |( M5 M9 w& l$ T; q; |8 D0 m        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;) |  j2 f' F% E- b2 ?
    }
' N& ^/ G6 \! Q; W  |5 a# ?3 o}  u( K+ i% b5 T. @
六.测试
0 m1 s$ X! k5 d/ K' I
3 A% F- K5 ~  H我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 B# `% u6 `7 {" }+ A

4 P8 a9 w. Q* L/ G/ N) V+ u<?php
7 h  S+ k4 h/ \, q    phpinfo();   
; x% i. E' X: v# V  n* q+ q?>
% z; ]) a8 u' e7 y1 ~, k4 @在浏览器中访问:
0 Y6 x- [' e: `  q+ J/ o9 ^
' ~4 D2 s1 p' Z* lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 S; l. E+ x2 s+ p; ^, ehttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。+ z+ w( E) \1 N' N
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 P6 v' \  x! j$ O1 h
说明sql注入和xss已经被过滤了
0 ~9 k' n. W! b: G
- ]. }" Z6 C9 z+ e七、安装过程中排错1 W. G& w2 ~7 Q( G/ k5 m. q

! ~# l% h+ U8 x* u% o9 L8 e1.缺少APXS会报错
) \5 u) }! {+ W/ b% |( v+ z+ }4 ]# x7 Q
configure: looking for Apache module support via DSO through APXS; R* e' B* Z8 x
configure: error: couldn't find APXS$ v  V5 X' `5 C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ V  i6 ^8 Q* [3 ?
解决方法:) @) ^% I% X5 R7 Z' h$ E
0 p! K8 t$ W: o4 i7 J0 c: c9 c
yum install httpd-devel1 O/ Y9 Q. ?7 j: Q. K3 y4 t
2.没有pcre7 {, L; ~  ~7 d8 ^; N% @' k

8 Q' T6 ^7 L' N# D! ^configure: *** pcre library not found.
/ ~3 p, g4 G. T5 K! e. b: mconfigure: error: pcre library is required
( F* ]( G- `1 x' f+ |解决方法:
; b& Z) H+ M2 X# g" x  h
( I0 e0 g7 o. L, z  O4 ?yum install pcre pcre-devel  m" h9 B2 B. O' R: p
3.没有libxml25 l  c) z: S6 e( b
4 W7 e7 C' z6 ?: }7 e/ W3 w8 e3 `
2 g$ q9 f  `9 d% \8 W, m
configure: *** xml library not found.& o6 _: T/ w; G( J2 u% A  X
configure: error: libxml2 is required
2 G" R" i7 _# x% `* i+ m解决方法:6 D# w+ \, P/ d2 u. h8 u
7 O/ J, O" w7 r, f1 K
yum install  libxml2 libxml2-devel9 |/ s, n/ Y7 v! T7 q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 ^8 F4 [4 P  M. e  z( |  L; `" m" f4 R& X; X6 {0 o* K2 j: L' i3 K
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 T% T3 X- K8 V* ?- L% S
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; e9 H* j; Q, J  S2 i原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
- W- z) q" h2 _7 ^
2 C/ ~& X8 D, F9 w/ `' i# L& R+ W  U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.  V& S1 O1 k8 I1 A/ P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9") [4 p7 v2 F) a
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! A* U7 ~. ^  e, g& ^7 G6 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# U9 e, c& M1 h+ W( W2 K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 h  M& {8 b' w4 G) J* t2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." a$ v. V& Z8 [. [; U7 ]8 o( F
解决方法,移除低版本的APR (1.3.9)% U) d6 u; F5 }0 v! A' `

) F$ b: X# M5 X7 d, syum remove apr
4 E$ c3 J& t7 ^$ J5.Error.log中有: Audit log: Failed to lock global mutex6 ?- u4 L/ V, \. B* r2 M

9 u6 h' f$ k+ J/ D( t; R0 j8 R/ u. K( v8 F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % u3 u  f3 K+ W+ d8 @4 H/ m
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 M' w3 |6 Q+ o+ q: z3 Y3 V
解决方法:
' m; P+ y1 \3 |编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' `# G" N2 [+ F) Z2 _0 A1 Z
1 F) G2 `& y$ C: F4 WSecAuditLogDirMode 07772 ?) ?+ x# ]6 A# ?% r
SecAuditLogFileMode 0550
3 i* [" p3 p% Q+ S1 H) r% z: U9 nSecAuditLogStorageDir /var/log/modsecurity
3 ?; g/ g& Y4 B. d* ASecAuditLogType Concurrent
, w4 A1 \7 O$ X/ v参考文章:
' [& G% ~& C& A: F4 Uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 a7 |; H1 G) w' a% fhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-18 19:52 , Processed in 0.070221 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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