找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11993|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. [1 b$ {5 }' T; K' ?1 m8 j+ l2 H
0 G2 M7 ]$ \2 L0 J3 V% i5 L3 w2 r一.准备工作- m  Q& S- e( _8 s) Z+ t% K( V
8 L. n# H, o; Q/ w
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
2 r$ L! I# |# n: w/ x
' e, B; u6 I( `$ ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. H) F4 ^, J% @7 m( U
- p  G9 O" w* t% S
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
* ]! V7 E) x1 Q" c$ \9 p, }( d* F6 }/ G1 J/ c7 @3 V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) ~# N5 J1 N3 ^) H6 X
( S. g) M6 r; S- H0 h! m依赖关系:
* a6 F! g5 h+ w! v! _tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; x  i8 a2 U8 `" {: l: e" I- ^; z
! }0 Q5 h' u+ byum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel, Q: V) r7 c. z
modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 Q- G, w: x8 A" C
1 }" T  u) U/ I! ], L  E  t% ]yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 ?1 A' c9 [1 s9 L
二.启用standalone模块并编译$ W7 P2 Z; b/ c2 B4 W

! \  b$ m5 b9 t下载modsecurity for nginx 解压,进入解压后目录执行:1 ^# [9 l( |$ Z$ [# [

1 D( S# d& }( I# A./autogen.sh, v1 K. J& [' P2 h- E
./configure --enable-standalone-module --disable-mlogc# t; S( z" J  z3 r4 b- `
make 0 |0 J& i- a  y/ O4 _
三.nginx添加modsecurity模块! v& t( U2 F% N8 o
$ K' h3 j- n+ x% u
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
. T% Y/ I$ e0 U, U1 D# S- \& r# s0 h8 u5 |. n, p
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine0 J' K  E% K& s
make && make install. V& K) L$ g# v7 W: }. _0 E
四.添加规则
3 ?- y3 m, V# {) ?* }9 k
" j' G: q$ u0 |* hmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 g' _# V; y( e0 V1 C

& S/ p- {3 }' t4 D! K! I5 Q0 f% {1.下载OWASP规则:
3 Q$ `4 d: P* l- H; j/ ^$ ~3 }( c; o& x6 `6 }
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs# I8 e, M% Z! L; F% k
* \! P" L1 m4 F
mv owasp-modsecurity-crs /opt/tengine/conf/9 ~: b1 E8 d4 U) y9 r
/ E) R8 `  y. n4 |) c' f
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf  q; d6 A4 R7 {) t, j! a2 i
2.启用OWASP规则:
) ^5 N3 G; T, i; k  |+ R9 |3 K$ \" b( A
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! M9 h6 e. U8 `, @2 T! d
2 z6 \: ^$ M! U) P( [
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 O, j  y' ~$ p' ^- C+ G9 F
5 R- ]7 ~2 J' K
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; p; O: N2 l1 G3 K2 ~& V

$ V; M' K5 u4 ~( l$ MInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 h; V6 }1 S1 V1 ^$ ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. T) \, i5 X$ V* ?$ FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! s; s$ q1 j+ [$ X8 a1 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" {! i: T5 \* {+ y" Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' G0 O1 O4 c. q9 p; l! ?* hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ z6 j* j5 y$ \. PInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf4 P$ Q; q+ G4 H  g
五.配置nginx
# _* [9 P+ [- h1 d! ~9 A$ G9 U. y# u3 u9 b
在需要启用modsecurity的主机的location下面加入下面两行即可:/ r' {1 Q- b' Y9 L; ~

, W5 ?! Z) G% o$ A6 w: I% Y2 W& hModSecurityEnabled on;  
$ S7 o% {& x6 F0 W! tModSecurityConfig modsecurity.conf;
7 s% s! x, K. x- C- d+ g- X' ]3 t下面是两个示例配置,php虚拟主机:
; Y+ e( X+ u  ~2 R- J
& M" f/ J6 h3 M2 m. @server {0 R# Y9 J' q0 a) P/ X
      listen      80;/ D3 Q/ H1 A2 p3 X
      server_name 52os.net www.52os.net;
4 P. ~6 z0 a# H3 ?! u+ F     
2 O4 v9 O5 w3 w. I      location ~ \.php$ {, C9 Q  c+ C2 c, u! d- D& q# I  E2 S
      ModSecurityEnabled on;  $ i; b  @7 q( \( x0 m! W8 P1 Z
      ModSecurityConfig modsecurity.conf;
. y' {0 A! l1 u3 @5 C$ @; I0 _' S- M  C% J" f. F. ?" }6 F- a+ v
      root /web/wordpress;
( t0 S% h/ S* N; r# u0 d* }8 C2 v      index index.php index.html index.htm;3 p4 M4 G' M' k# Y8 G+ d
  
) I  u2 ]# w$ G  x. l9 b4 w      fastcgi_pass   127.0.0.1:9000;
1 ?% z+ E; w: q      fastcgi_index  index.php;" I$ j( r3 u+ B, S# y' b  E0 Z
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
  x( K/ W1 P8 I3 z0 m      include        fastcgi_params;
. I! b* q! }6 b) T      }& j. L  k/ k" \; T0 M& x
  }! A0 V  V# s9 d. j' d- \7 d3 x# X
upstream负载均衡:
& |3 ?4 n8 `5 f0 R
  y1 `& ]' g/ jupstream 52os.net {
% b) V9 c, k; l- Y1 `2 U+ V' N, V* I" a) Z    server 192.168.1.100:8080;% d) @' p' D+ U, U& y: i( y% W6 f
    server 192.168.1.101:8080 backup;
6 X$ t8 E3 W& U}/ d; H4 K/ H; U6 j# F# I# a/ Y
% ~8 W* d+ r$ n
server {
3 l2 H$ j$ r7 p4 s: jlisten 80;
. u  o8 W( B5 N1 g1 n0 v) j9 x# n8 b9 Vserver_name 52os.net www.52os.net;
' Q# A2 W) f3 E8 v/ V3 d) G
2 }3 K* l) {, X. Q3 c* t: K- |! P4 plocation / {
8 e) C9 D$ b( t6 m, P! j    ModSecurityEnabled on;  7 x% A5 E* _* H: s: r
    ModSecurityConfig modsecurity.conf;  . `7 a8 q6 R& L) k) Z$ n% B

; H! B6 G; r% v0 |, W# p        proxy_pass http://online;  S. M) j0 ]; ~3 A
        proxy_redirect         off;
$ U$ f; R) o2 }  ^9 A, x" X" V        proxy_set_header Host $host;
2 t3 z* O6 V: E  e+ J1 Z        proxy_set_header X-Real-IP $remote_addr;
: @* [: Z6 ~6 P) Y' O        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;9 G+ X. R/ h3 N; `5 W
    }% G6 u' Y( g5 w8 ^+ O  F
}& I$ L+ F5 |/ d: F6 D! E: X" d
六.测试' Y5 a: G" u. [) ^2 w

7 o- E. f- t* B  C  e我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) }; }5 Z* P( `) t

' [6 W/ M$ ~- U$ M1 ^<?php: ^! ]4 b  L/ G1 P; G6 Y2 h* y7 F
    phpinfo();   
. d/ ~/ E: K- ]+ e$ e?>/ j3 r+ }7 X7 [% ]. d
在浏览器中访问:
  `+ F6 p1 ]* b: s& |: A- g7 ~/ z+ f* ^9 s* `0 N; Z
http://www.52os.net/phpinfo.php?id=1 正常显示。. X; d+ \) ^; Y  K* c8 A. q% ~
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
+ l+ L5 Q( L- G; f2 N' phttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& }) d! P+ }& o  q9 D/ M3 i说明sql注入和xss已经被过滤了( P$ Z" N1 s( c. ?9 X

4 K8 d( ?0 F' A七、安装过程中排错) N2 g* R" ?; Y, z! t  I

; D# L. b; ]6 q( V1.缺少APXS会报错
2 ], J( _4 ~7 f; `/ N* a! G7 {. e: z2 \/ G; ^8 {- T
configure: looking for Apache module support via DSO through APXS
' n) \, P0 V( F. Rconfigure: error: couldn't find APXS
  p+ f* d. X. g% [8 q4 J1 fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 M4 w7 N) @% T( V3 [$ R解决方法:
& p. G9 I2 L" l3 [/ \
0 ?) P% F6 ?3 n; }. Wyum install httpd-devel, I# t7 r1 U$ f* E
2.没有pcre
" T: T6 X* H1 n! B( `2 l# q: K6 ?9 C$ |4 P
configure: *** pcre library not found.
* ?- l4 }2 r% _0 mconfigure: error: pcre library is required
/ T$ N/ e& d1 J8 n, p8 Q解决方法:
6 o2 d" g+ c& _8 |4 u3 F
+ m7 e  S+ y4 ^yum install pcre pcre-devel( y4 S4 _- N# Q) W% O/ `
3.没有libxml2; s4 W$ a5 _8 R( Q+ E

4 H* c  I0 J! p4 k% m+ @; i6 c! c" s3 a$ Z. h+ B
configure: *** xml library not found.! p% c) _. N: w# z
configure: error: libxml2 is required* c% E: y! s, z. h2 W( h
解决方法:
, Y) v7 x/ x6 P/ r8 P" w7 M- d3 y4 g( R( h
yum install  libxml2 libxml2-devel2 C* M: x  P8 k, G- @: h# M
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ _, G; n" Q: M" B( \. s# J6 \- D- F3 ?- V* W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)6 v' {6 O3 ?# C9 L0 i7 w3 g6 T
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 D% h! ~6 x2 ^' K# J1 P( H
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! w0 m4 H  S7 {! E
' I5 z' j# n% k. l- [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: A2 O; Q( P3 ^, x1 q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
8 D8 F- x9 y' T7 k2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 p% U; n4 \; Z5 p+ h9 _7 j
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 |5 O# x! j5 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# z! G! x$ U3 F" j- i5 M5 ?2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ b& u' @$ ?: S: \; N! v
解决方法,移除低版本的APR (1.3.9)& Q$ Q9 J) m( I! X% j5 ^

" x5 C  [9 m0 y: X. xyum remove apr4 e8 ]$ }, }  b- V+ y' U
5.Error.log中有: Audit log: Failed to lock global mutex9 }8 `3 w: f# q& S& R

+ ], H2 [& h, ^7 {- ~) x2 {2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     1 @" z& l: s, E
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 B3 l0 E1 J7 J! Z3 J
解决方法:
1 I# ~( \: @: H, x' h编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- f: w. @- F% @7 ^9 h) O+ B9 }( t
" q0 `$ U; z. S5 NSecAuditLogDirMode 0777& `7 Y! w1 i9 Y, ]+ Z2 Y; k
SecAuditLogFileMode 0550) m8 D  _6 L9 o* N" N0 o7 u+ [, ~
SecAuditLogStorageDir /var/log/modsecurity, M) B$ T0 a! }4 Y6 }: F
SecAuditLogType Concurrent! q; i$ b- k* E
参考文章:+ T- j5 u& R& _' n; a* I) A6 \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* t  b2 Y  j$ b4 mhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-1 18:21 , Processed in 0.067190 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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