找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13096|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: ]1 |8 L; @) M+ b/ f( k- k6 ?0 [3 D2 Y- X
一.准备工作2 v7 M" i: c  z

5 B* a; e1 `9 L, q, `' X1 A# ^8 O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 y. z$ u5 P+ `0 y: x* a( i$ z8 y, }5 ~/ @+ ^2 k$ o  G
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ f* D- g( f5 o& Y( N  h4 ^/ Y
3 C) |! @5 z& ^' N3 q+ `2 s) pmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  _9 D+ V0 k4 b: x4 q4 X

' T6 F& c$ J1 y- Q4 |5 e: H. `OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ s7 N* _; G! r; \( l5 o
$ q( v. m/ g6 a# l" A" D依赖关系:
6 u( D$ M- a, K. Y  [- o0 f! G) ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% l9 g4 i: E$ r& B4 B
; b/ w) q& F; g, o4 u* \
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel! k: `" G  J' F9 F9 d
modsecurty依赖的包:pcre httpd-devel libxml2 apr6 t6 A, [6 A* g" ^# D1 S4 J
! V, B, R) D* p$ x1 w# N* e7 p
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
5 P( Y- m+ ?% B* y7 C& k二.启用standalone模块并编译! T6 C3 V$ t  p$ p! {0 L8 m
& |- I) Y; u* G3 Z/ m- P
下载modsecurity for nginx 解压,进入解压后目录执行:
' O7 _6 _7 S; p2 D" ^
0 f& g; h8 y8 E4 D! |3 P! \./autogen.sh
/ I* e+ r/ m/ ~4 R) P( [./configure --enable-standalone-module --disable-mlogc
, i# o% F- R6 \% s$ Vmake ( U# |6 K7 H# u# V/ v& {
三.nginx添加modsecurity模块
' N4 E$ |* ~- w+ `5 P( i# |, G5 i4 f6 q$ @. N% T( K% O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ ]& Z& d8 P" X$ _4 q

5 a7 N7 a8 p. T5 D" h, K./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
$ |9 L" R2 Z0 H$ q. o% _. v8 emake && make install
5 s9 P: D5 i; g: ~0 A四.添加规则$ Z! q& p2 `+ Z& k; s% H/ D: R

3 k) }( n. k( [( r4 Xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ V0 U$ N- I  V. P, g

. w$ U. Y, l) Q1.下载OWASP规则:( I+ i% _- Y7 ^' z$ m0 Z. D9 `
1 w) c, Q& K, M2 ]5 l6 h' h: m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
  o$ h. H* `+ z4 u- _& |! t( |- X
mv owasp-modsecurity-crs /opt/tengine/conf/- y0 |; V+ L, O

) t6 _" t- Y! R3 Y0 D: k+ Gcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 R' O0 ?" ?, s6 Q* ?% b2.启用OWASP规则:0 Z8 Q+ C6 d5 ~7 k8 d) j
7 a- n) j8 M9 u. n5 U% m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 T/ h3 \, f& W9 h  P/ {2 P( C7 l/ ~- E( B
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% `+ G3 c  |& Y+ c: X% {4 ~$ k, F7 V  ^) ~; T
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 F8 ], `2 t) Y1 J

6 Z, }$ _1 i; ^3 k% [8 h1 b# WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ L6 C4 o; q/ ?! f* QInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& O# [1 j- w! l5 H% l. r" ?6 P* p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' ^- N/ \1 O+ Q) |3 B: r% C) W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( z. {4 \' C0 m7 r) J* F8 tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 p3 F7 P0 B/ ^2 m6 D  w; V8 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" U  e# M* \9 F/ f
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 M2 Z$ {- u8 U1 Z1 s- _
五.配置nginx
- ?( I3 r% r6 Q. d. u7 w
! ^3 o8 o6 I8 S在需要启用modsecurity的主机的location下面加入下面两行即可:9 y# l+ Q' ^: h4 G) I: t  X* N

2 p8 n. l3 m/ H; l3 [ModSecurityEnabled on;  
# K) e2 M+ L' P0 m2 M0 T& R/ aModSecurityConfig modsecurity.conf;
0 H+ N* Z5 \! |4 T. @& C' b- }. P8 g下面是两个示例配置,php虚拟主机:2 p  V  L7 B# I: q8 T
3 k. W: }0 E4 `. r$ b9 y% v% W$ t
server {: u4 D( u% r0 R" n0 r3 |. z, k1 D
      listen      80;& t7 @8 e; g4 U* m
      server_name 52os.net www.52os.net;& }! w6 \/ G- d) J
     1 w. Y, f2 {: M$ z2 p
      location ~ \.php$ {' V- U" W+ G" @* T; A) j8 l
      ModSecurityEnabled on;  
$ K: {& H! ?+ V/ y; H      ModSecurityConfig modsecurity.conf;
: y  Q3 R- P% m% ]. I% n
& ?& i. J' u* K! D* n      root /web/wordpress;% `. _* ~' Q! G7 l' m
      index index.php index.html index.htm;% f! c: G. c/ `! n' V& a
  9 _1 [$ B0 ?& L/ l: [
      fastcgi_pass   127.0.0.1:9000;/ v; X! C2 ]0 P" W2 t! {
      fastcgi_index  index.php;
& `7 ?2 M: V; N% O      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;* t3 j3 `! W3 C! E& @) i
      include        fastcgi_params;
0 d+ O! `% C& I! A: x( u      }8 `% p/ s% q6 ~% p0 y
  }
! }9 c. V. {3 I+ q" g9 `upstream负载均衡:
" s6 W3 Y5 n1 s' c8 S
+ I6 w4 f' `9 r% M+ T& Zupstream 52os.net {
: `, ?  v. k7 ~; b% m    server 192.168.1.100:8080;
% T7 _+ d% \+ }) p8 i/ Z0 z5 j/ ]    server 192.168.1.101:8080 backup;
" @% l6 v9 {% w" Z: f}
6 X. k9 H  _& D" g; q; P" d8 R4 B) [; v8 A1 f, t0 w
server {/ S+ D& b  Z5 M5 G' H& P/ b- O
listen 80;& ~- ]# x" a' m' k/ O3 \8 E( J- U
server_name 52os.net www.52os.net;0 t, R) P# C" l* K5 g6 X- a

& w$ b* x# b. b! Z, Glocation / {( t" T# U5 ]) i1 L( ^
    ModSecurityEnabled on;  
+ p3 c% ?8 V' M- r    ModSecurityConfig modsecurity.conf;  
; V: S9 N& u% X3 j1 g) M( y* d7 J1 k! H" K
        proxy_pass http://online;: s7 P7 V: O" I, I6 f- T9 C
        proxy_redirect         off;6 y) s& u1 m$ h7 o  N$ M) T- J
        proxy_set_header Host $host;7 N% i) E! d/ V- n1 Z" v0 h! W7 s9 y
        proxy_set_header X-Real-IP $remote_addr;, }# [. X6 h) J) j! G
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
: _3 G% M; T$ F" `    }
* ]" P0 O: J2 A. E0 y}
0 f& g# B- ~0 @. u六.测试
0 X: e( [8 `- h' _' y) G! M
; P  J1 o2 q; h  n1 c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; z2 r) [, Y4 l1 U+ Z$ b

/ y; ~9 A  O5 Y# r2 L<?php
, e! O  M( \$ l, Z# m% _    phpinfo();   
. q/ T% |2 s8 \2 Y( v$ W' d?>
8 ]& @' g% k& R3 K在浏览器中访问:) n) M& ^, @& N7 s* y  a3 G; t; x

& c" B! c. w! U7 y- J+ K1 x0 x0 Xhttp://www.52os.net/phpinfo.php?id=1 正常显示。# O5 m+ N/ W1 W$ L7 X
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
8 s0 X% b4 Z  E  H, p/ Y- {http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
' p- C7 K: {9 k8 h, v2 F. E/ T说明sql注入和xss已经被过滤了: {4 Q* c2 c$ ~2 S4 R9 ?: r+ S+ [
$ S1 @( H- J3 w/ p
七、安装过程中排错4 v) @2 K( }( T1 G" S
3 j) r$ }  f6 t/ o3 T, ]: X9 C/ z
1.缺少APXS会报错
1 c3 }/ g7 O9 [- B5 o% v$ J3 _  u# a: O" h7 u9 ^8 G
configure: looking for Apache module support via DSO through APXS
. `: ^+ a! V; }6 y1 ~$ Cconfigure: error: couldn't find APXS
. S' l0 \$ z8 w- p0 z* Wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 l2 p" L4 k  I解决方法:& f' `+ k, B" A  F

0 ~/ R+ [2 j* K4 ^5 ]yum install httpd-devel% Y! _) g! Q9 h
2.没有pcre% L/ l- M9 i/ f2 Y3 F6 m

6 v2 }+ p; n& rconfigure: *** pcre library not found.7 R' {" d% N1 ?' w
configure: error: pcre library is required$ p8 d1 o) d" R& G4 U5 h6 f
解决方法:
/ \# n3 t$ M0 X4 A" q
. ~: f7 a7 T2 Iyum install pcre pcre-devel
5 x7 W% a0 J5 {* P, r+ _3.没有libxml2
, f) u. u  R5 R! A( T9 g9 Z! f% O3 @  l
( m& c4 x/ j3 ^4 M/ Z4 L
configure: *** xml library not found.
# ]7 t! \  c8 S$ V1 p4 Jconfigure: error: libxml2 is required
9 e' ^, |* E3 L. Q解决方法:  W: V9 j- X5 U. N5 @
6 `" ?2 {/ q% Y
yum install  libxml2 libxml2-devel
* G' ?8 o+ Z, o9 P, `: c3 @; G4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 s2 @- f  {8 {/ D$ N4 B* b( E% |& T' k
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( n, V6 P$ z; `* b7 w6 Znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* Q( \4 h" K( ^! n原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 W4 U+ P# d% U4 T. U% V: c3 {! u  h7 `9 \. P: S: L  K6 _7 Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 @& u% M6 F" D0 D1 Z  w/ a2 S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"' U2 j9 E" A/ v6 B" i3 P% p; Y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ J8 p6 s1 p5 Q, [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
  e: n+ Z, Q5 g4 W& x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& B* B" Y- R8 k3 r, x3 o
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
0 E& Y( I, x3 ?; w% _: K1 [- R解决方法,移除低版本的APR (1.3.9)
5 T. V- X; A- l/ W4 Q8 e3 ]& m* F7 \# r! ^
yum remove apr( Z. z9 }# c! S- i
5.Error.log中有: Audit log: Failed to lock global mutex
3 C1 B; q9 J. N) n6 H. L; D5 m9 ]% E
' H0 h$ _1 E  S/ n0 b' p3 m2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( s/ u! n7 v) }/ Jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 M% a* V3 L/ S* e! ?* ^& \* [3 t7 K( ~
解决方法:
% n5 `- H$ J7 Z* U) p9 Q) v; a编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% D8 F: s: M  i7 ?4 O$ X- _. H* {3 T, ~3 J# \: [. g' s4 M
SecAuditLogDirMode 0777
/ r& ], T0 y) F# O: ASecAuditLogFileMode 0550! L( {* }' f9 t' q8 w
SecAuditLogStorageDir /var/log/modsecurity
# R, i9 h/ D" G$ w2 q8 gSecAuditLogType Concurrent9 n6 ?3 v' X: q+ d( \
参考文章:
# e  R  i3 G# n, L0 ~https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; `/ P5 Q0 @/ R/ @0 x: f6 s  I/ w
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-14 05:05 , Processed in 0.044156 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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