找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12794|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) Z. A6 x5 ]4 B$ z4 W! O$ a& y
$ w! V) Q2 l4 l1 s一.准备工作
. D$ Q0 t- N. t
% @! I9 X6 ^! ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.01 K2 d7 X9 M  r

4 `& D  o! P& `+ O5 S$ ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz) ~1 Q& h$ R  q  ~2 V4 Q
! {% s: N" S  H: i: c# c
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ D- b1 ]8 g- M; P: m
, b# C  t, _5 W5 q  Y% }3 e: oOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( b5 J0 O5 ^4 i9 e! w
/ q( I$ E1 l" y# j, s9 Q1 c4 ^0 w
依赖关系:4 J5 m% p$ y7 M$ W# x6 t% i) Q
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  g4 V: D$ d& k' n+ M

" A+ h# }6 M! ]' V2 X+ l" Fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) j, U3 a" N6 B$ R  h+ Umodsecurty依赖的包:pcre httpd-devel libxml2 apr& I, D$ h9 J4 o# w  W

% }1 ]  e, v$ b% @7 }! R/ Uyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel3 g+ \2 z& v  _8 D5 e: e
二.启用standalone模块并编译
. R1 ^7 ]4 x7 @- ~, H: K! p) ~" h6 I0 f3 `9 Y; ^; `: H
下载modsecurity for nginx 解压,进入解压后目录执行:% r1 E- R! J. c# k; \  D- B9 w
) w* J9 u5 z+ t6 [! |( M
./autogen.sh; Z% Z2 G/ {. z: u2 k
./configure --enable-standalone-module --disable-mlogc3 u$ s/ [( }) y( p6 L' w8 H
make % n; V0 C1 C; Z
三.nginx添加modsecurity模块0 L: b: T8 F" h( n* O* T
! w. D8 F: p$ e1 e! n9 J% e
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
  m3 k& k" z2 [7 z3 [* d' S- X
) c- s3 H9 v: L) n& u2 t/ O5 C( g./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
* C7 d7 h1 X: I* x8 Z4 n4 pmake && make install& ]6 E- t7 R2 k3 r6 H6 G2 A
四.添加规则9 S. O- |4 Y. |

9 ~+ A5 ^# Y, W6 M9 ~" L+ n2 Emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% @$ B# e1 {* h( @1 m- r
2 h, Z- L& W- t
1.下载OWASP规则:& t- C# ^( w, D$ a" c$ _

/ ~9 f2 {( d% X; f& G7 P8 M/ q. Hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs% U$ T- Z9 V" n  u
, f# O2 }5 J) V, X
mv owasp-modsecurity-crs /opt/tengine/conf/3 E" k7 V/ f( I$ L5 a; K7 \% _
8 {( F* T1 ~! H* O9 m9 ~8 c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& p4 z) o9 _! c( A% B$ K. k7 m2 T2.启用OWASP规则:  R; F" l5 i2 C# ^" c! z, Y
: L* v* w' H* Z' D! R
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 N0 e/ @* Y# W' `/ e4 @% s
: d* I/ a: e1 `4 E7 n4 l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 U- ^! {8 X" J# {+ ^0 I" I

. e$ G: x( h: ^8 zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' z- }0 G( b$ A$ i

" x0 y6 E! ?( c1 wInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 a+ i* p+ O' ?8 V0 z7 K1 ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 q5 Z* ^0 g8 |$ P& oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 t- L$ G% n; _9 m1 O1 t1 \% e0 fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ Y# z; x  y$ K+ d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 p" C; C& R3 h6 f( I# i
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; S- T8 O, R5 R- LInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# ~3 y5 i0 T/ s8 [" l0 `
五.配置nginx+ Q9 N; G4 j+ _, A3 m- X2 h% M# t
0 n# l% H/ w% T: l( f2 w$ S& g
在需要启用modsecurity的主机的location下面加入下面两行即可:
% x1 Z' ^! {3 e3 L& Q; S, x9 F* S6 q% q5 p9 q  Q* `" H. c( @: ]
ModSecurityEnabled on;  
* z1 D. j+ A: q* v5 lModSecurityConfig modsecurity.conf;% e4 u1 e) |8 I+ X
下面是两个示例配置,php虚拟主机:) Y' K/ j% W1 N! P. [2 \
) S' T) R3 d6 |, h
server {
- L# h; J; A6 |      listen      80;! [" d- `2 o* Q. w
      server_name 52os.net www.52os.net;' l- `% a1 t- R# p
     
: b/ S, l# @: \7 s. I& a# M, J      location ~ \.php$ {
- n1 a* u/ ]+ I# m8 f# o) f, m      ModSecurityEnabled on;  % i) F8 }- m3 o1 ?" t' f( }
      ModSecurityConfig modsecurity.conf;% x' U: _" ~# A2 g
# ]* l5 W, S3 V" K
      root /web/wordpress;( U; }- h8 \7 h5 b1 y# p& R
      index index.php index.html index.htm;
* N% S% K. T3 \" H/ O4 Q  
9 D) m6 o; V/ `% Z      fastcgi_pass   127.0.0.1:9000;
. H; a' C" H4 F6 E9 Z  W      fastcgi_index  index.php;
* G  U3 H! s4 q2 r1 Y6 a      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
- m1 D- R( A& ^      include        fastcgi_params;
& l6 t6 ^! l# @      }
5 T1 V& s. N4 {, U/ p  }0 v+ e* X& M% k
upstream负载均衡:) T& P- J6 {# O4 P% T5 c; w" ]

& u' R6 }/ f; uupstream 52os.net {
# P3 P' P+ [: f; Q4 a0 B    server 192.168.1.100:8080;" [, [! K" N9 s! ~  ^  p
    server 192.168.1.101:8080 backup;
8 x$ z7 z2 @! \0 F8 d4 o& P' ?}
0 }$ {3 N! N+ O3 q
" f4 T% N: x# J: B  Eserver {& z3 F% I% b  y$ v' P( |/ `
listen 80;
) b2 T( m% V# Kserver_name 52os.net www.52os.net;% b# x/ a) W+ [: V
( Y9 b; Z$ M' |3 p7 L
location / {
5 p1 b5 V3 q& p$ c& X    ModSecurityEnabled on;  
0 k) D' J+ \- `/ {8 C( U: G, j    ModSecurityConfig modsecurity.conf;  
9 m; ?% R; Z- I
5 a: D4 p& a* {  O( D        proxy_pass http://online;, M2 l6 V5 X3 ]
        proxy_redirect         off;* |# t# d$ x- C, k: R/ x
        proxy_set_header Host $host;( ~0 M! x0 W) K7 j
        proxy_set_header X-Real-IP $remote_addr;, l& ?) e) L0 W7 N7 }
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;- E+ i+ g9 N2 h- b
    }
+ M" I% }: L: U7 K! k& J}
+ a1 I! [- b+ [. r7 a六.测试: {  X1 k1 U4 z# q3 Z' _4 @

' {6 H% Y' {& Y  f. |& P- |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 V: B( ]  u8 B3 ~  r

9 X- N1 D; L# Y5 j. N( T  t<?php  o( ^# W7 L. R% `% _) A. d8 z
    phpinfo();   
" r- x. N3 t6 C# j% Z- N?>: l; V& G( `8 w; _- O
在浏览器中访问:
' F9 y- G+ E- v+ ^$ D4 d
7 w" S3 C7 m4 C9 b  Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
' B, k6 q* s: E: y( \2 Whttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
  q/ X% n5 Z9 v- jhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
; A% `  L$ b( _% s. M+ L8 S' \说明sql注入和xss已经被过滤了
6 q  i0 e# U' D2 m5 m3 M) ]3 a+ _; [: a6 s4 y( j4 F9 d; R
七、安装过程中排错
4 [# \7 c9 |) d! b& D4 y: K8 R% g9 ]  R& H; r* K
1.缺少APXS会报错% s& \0 }9 k6 W- Q% F

2 ~" S' F0 D$ L' m0 ?configure: looking for Apache module support via DSO through APXS% _$ z9 Y) P$ p% O- Z6 z* L6 ?
configure: error: couldn't find APXS! ^% J! ~4 S/ P% W1 y3 ~' G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) |. j9 P# ^7 P* F1 g7 W& P2 N解决方法:7 m. z* n) S7 g6 ~
' v( p7 R& J. t8 z/ s: b
yum install httpd-devel' z: N( ]% f: [# q
2.没有pcre6 Q6 v  s! _6 i
9 Q6 B% G3 U8 A* |
configure: *** pcre library not found.
1 m0 A5 l  Q! ^8 ^( q  \4 Nconfigure: error: pcre library is required
( ?: K) [: ?' I2 E" Q% r" u% {解决方法:) H7 Z9 t7 O) i6 R

* ~7 U9 ^0 s- l9 a2 Ryum install pcre pcre-devel( _0 G: V( A; H3 G7 y( s: N
3.没有libxml2
# r, Q1 d/ a: ]7 \$ A
4 g0 k+ h8 Z. K9 I0 B3 T  h% |
* S/ a! V( X5 u) Q8 y, L3 nconfigure: *** xml library not found.
, c4 P1 \9 r4 Dconfigure: error: libxml2 is required  o, G2 T; f- l9 g& c' S
解决方法:
0 r5 }8 E" L8 R5 D9 s5 ]) P8 s
yum install  libxml2 libxml2-devel
" u* {& t# w4 T5 C# `. e% K4.执行 /opt/tengine/sbin/nginx -m 时有警告& ?8 [- u! d& o& ]% m

, |0 }  Q0 {- G; U2 e! K5 e6 yTengine version: Tengine/2.1.0 (nginx/1.6.2)
8 U% Z6 u% p3 \6 d' H' ]' F- Fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ F& e# ?# n: b$ f3 b: y. H1 `原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ R  k3 e$ T( Z! J
: @1 L+ B( V6 ^# I5 K4 s: Z) i( ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( H6 O1 `: N( u7 b# R5 ^$ I6 x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9") {5 h! [: D8 u# a6 |: E) V
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! p' B) p- g7 }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": P. I& p( N9 R% ?1 m) v/ G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", }$ Z& l' Z, }8 z" [
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# `9 J) R9 k9 ~- R( N5 W8 G  m6 _3 m
解决方法,移除低版本的APR (1.3.9)
, V5 l4 }' @4 k- q/ D8 [; L
' }  b; o+ \% S9 T2 Yyum remove apr
  b% ~3 P( ?9 s/ |. i, g' P# H) s! h5.Error.log中有: Audit log: Failed to lock global mutex
  R* t: H$ U9 r( i& |3 K! G) L, }. s4 ^9 f& A3 u+ I: ~, i( t6 J
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
3 _) Y" F- ~$ L  @. xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 |% X( {) P/ j2 X解决方法:0 l. u0 G9 k! W# c
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:1 H+ n; E* z( m  z  m6 E1 h3 X6 M

! t! V! s- j# q/ M* GSecAuditLogDirMode 0777
) Z5 t, T7 G) V  R8 }SecAuditLogFileMode 0550
& N" c& l3 Y9 CSecAuditLogStorageDir /var/log/modsecurity
0 L: k7 P$ w8 \' r! z. jSecAuditLogType Concurrent9 k0 I; K# w$ B( e2 k
参考文章:# ?0 }6 b$ @8 s# B0 l
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ m" }& |# ^  W3 Y5 L
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-10-12 08:08 , Processed in 0.049801 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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