找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12293|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# t6 S6 A& x% B8 J0 O3 X
7 Q8 Q( D; A6 [4 N一.准备工作
- }5 E3 E3 b. b( u1 ]( E( t6 x5 \; L/ S3 f+ K8 q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0  z+ v* o: t9 u# [# j5 }3 h
1 c  d& B4 V* ~2 X1 U3 s; H" \
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
; ~7 D1 [1 \& h2 O* L
. {# A# j- M) U$ W  \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, `2 Q# Q  Z1 K( e% K: B" J9 G2 [, Y
8 j5 t) L6 h) D# C2 m. @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
* t) ]) N0 ^# M, w& Q$ t$ B" N, s& ]9 U0 y( [5 O
依赖关系:
% V0 E. q  f, \. m$ |tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:- G1 t; E* y8 z1 A/ z1 S- }
5 J  M" p0 q1 `9 Y# Z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 l1 x' N$ J. t; x4 b. l  J+ O% gmodsecurty依赖的包:pcre httpd-devel libxml2 apr
& z, D9 G5 s( G# i. u2 l  C: h1 ]6 o+ `1 X. C
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 U6 `4 U3 ^7 J* U$ \) {8 [6 Z8 T$ k
二.启用standalone模块并编译
9 T$ f0 i! H- o) _% Z: g( u
9 \. {# e5 {2 b2 C7 K下载modsecurity for nginx 解压,进入解压后目录执行:+ b6 t: k' E3 t! k8 U9 M. V
1 w" ]- {+ w2 G7 M8 n+ N
./autogen.sh
) H/ O8 g. e1 w$ |) o8 h# F6 J./configure --enable-standalone-module --disable-mlogc
# z% n. N7 S" |4 Amake 3 T( f; L, `4 B' \
三.nginx添加modsecurity模块
5 R0 r5 \0 z2 v' H
5 z$ {# Z% A; t' V& A2 Z3 C/ m' J在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 v# L& _8 f6 X% N  Z/ p( Y3 p
8 X" j' B8 Z0 L+ r1 i4 ]$ W0 W+ \" e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
% w$ W! `- g3 q. F  e' e5 Q! ?make && make install: ?6 w: T7 O  V# p- }8 r
四.添加规则
1 s, z1 Y3 T. ^8 t; s) U
' W% u0 }' V( \- C, ]7 F1 {: c7 i  |1 `modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# J; k2 `! Z0 v. e
0 f! J# y" k, n6 k# D) I6 W; f  F1.下载OWASP规则:
% u1 E3 G  N+ K% i2 I1 i% w7 |8 S" a9 S; Z5 J1 T  y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs* R) I2 q/ ?8 F+ e. @( i. f5 s) V

& Z# v8 v. e. hmv owasp-modsecurity-crs /opt/tengine/conf/
3 y& D$ ]! a% c& Q2 U( K: @' F0 G8 a' X: \- O% N! U
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% ?2 h0 {* }, |  _. z7 \% {
2.启用OWASP规则:
& ~, G9 r, |5 E$ Y8 X
) |( @6 s' g' r2 x& d$ m3 {: ~8 O* P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 ^+ j" a0 ]; a3 ]3 W( Z' a, I: _( C, b7 K# |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 F' @. r+ B, H' ?* L8 J, K. v* i$ `

" q' C$ o2 ^% W% o3 ^owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' j, H! ~/ \4 |4 `

9 R1 o, b3 t, ^- D, {! FInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
, ]* M$ q% t' J1 Y4 ?, r  y* mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 }7 A( K2 D8 |% N8 AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf9 B9 J6 j1 H4 w" u% r, J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. b- E# x  ?0 ^8 a# @. sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 w6 C& ~. ?9 I( cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; R' d% Z# m" UInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ {4 H: z$ ^' b5 t4 l6 ]" x五.配置nginx* s+ K% l9 u7 d

" M/ z3 r; J( i6 i  x1 }在需要启用modsecurity的主机的location下面加入下面两行即可:4 ^3 i' f1 o) v2 W; B

, v# q- x2 U: z& aModSecurityEnabled on;  
3 t8 {, \8 p* m5 I6 }7 ?& D, SModSecurityConfig modsecurity.conf;
* M: Z+ @- O8 J2 Y- T: r6 p  A/ F下面是两个示例配置,php虚拟主机:
2 f% v; n# R  y" r  C% [
* F- J6 z- `& P. l7 b# Z$ v. Bserver {
* w: \8 e" g  L) M" {      listen      80;
, ]6 f& C+ V1 S9 b1 ?      server_name 52os.net www.52os.net;
. O* M4 |" N( w4 \9 X5 I     
/ Q6 l" m6 X" \- {1 K/ i$ P) O7 s      location ~ \.php$ {
0 |, v8 _3 U! M$ z4 u5 p7 v* K5 v      ModSecurityEnabled on;  ; z2 \' y; Z7 W. b- B7 I) C/ I
      ModSecurityConfig modsecurity.conf;) ^& J8 c& E: |+ v
7 O" D& O. t$ v" y7 D/ u
      root /web/wordpress;
2 X( A5 i) P6 O/ F/ }3 {- L      index index.php index.html index.htm;
0 l$ _- _6 j* p5 @; J+ q1 B1 N  
( W2 Z8 ?) N6 q2 B      fastcgi_pass   127.0.0.1:9000;
% ?  ?  A" q% n; x      fastcgi_index  index.php;6 y6 n8 r1 f. C. o, i
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;# H  [6 N7 l7 B- ]* Q  z6 g3 q2 c
      include        fastcgi_params;0 n" B) r. P; q+ p# }% T" K9 |
      }; R4 f( v) x2 A: b6 k0 n' z
  }
& Q# Z! O4 F+ H# H$ E9 Iupstream负载均衡:
! B' X1 [2 D" Q* \: t# K$ l, A: K5 ~% [3 A6 [
upstream 52os.net {
. R8 n: s7 Q. p9 E    server 192.168.1.100:8080;
! k( r0 L% Y1 W: Y    server 192.168.1.101:8080 backup;
' |7 U: \+ |$ f  @! m# h( y}- n% E4 i$ r) Y" S7 I/ R( k

. N. r- X, G8 ]) Y7 d& A4 Userver {
  [5 c1 U9 X" h% ^listen 80;
- B9 g  Z9 b1 ^/ |3 k4 @1 Jserver_name 52os.net www.52os.net;
0 [  o# k, l" k; |5 N5 d& p
2 L. B+ H7 D5 M5 K2 G4 z* k9 Q' zlocation / {8 `" q: @+ o2 o' }
    ModSecurityEnabled on;  5 g' W. R3 L1 I0 L- k
    ModSecurityConfig modsecurity.conf;  7 a( K2 g! ?5 J9 F, R# o
4 \: @) D0 B: D. s" E/ [1 O
        proxy_pass http://online;
. E( P5 x4 d  U% {        proxy_redirect         off;% j* O6 z1 }+ j0 @: P
        proxy_set_header Host $host;$ h+ @. r. I# ^6 U5 P5 k4 A  j
        proxy_set_header X-Real-IP $remote_addr;
' b% k( i% c+ v/ R' G2 w        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
6 J$ }; J# q/ ~3 [7 M* Q1 b. k    }
  G& e8 |) q6 `' ?}9 q! E: I$ v& D6 a) m: @
六.测试
( y. N/ S/ q; D( c
! L; o" p7 _* B" @- c$ Z! `我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 O8 ]% Z+ v. E* A+ N

5 ]6 F. ^; v4 g: E. a, t7 t2 [4 L<?php) q4 i! L3 K7 {, _5 D8 d
    phpinfo();   
0 C7 f# G* A+ p8 M2 n?>
' E3 d) \6 Q+ j0 g5 t在浏览器中访问:1 Z. i  d( o3 C: O2 R6 N% e
; b& h3 X- }( w3 P) p8 L+ W& Y5 O
http://www.52os.net/phpinfo.php?id=1 正常显示。* d9 `: z0 W8 k6 ^2 W  G4 t) U
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
. ]8 y" g$ c& }7 M' dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。0 o$ z2 ]  V; u# J  y
说明sql注入和xss已经被过滤了8 Z4 S6 d8 m, ^* _& j* ?) `8 ~

4 B# f* e& x; ?6 ^- m七、安装过程中排错6 A0 @/ C; L4 i5 E
1 b" m, [  L8 L& f7 H
1.缺少APXS会报错
) c8 C1 T, f) m; e) X% N' T, X! \, z& X2 a3 H& h
configure: looking for Apache module support via DSO through APXS
2 z8 M% l7 U4 e5 Vconfigure: error: couldn't find APXS* ^5 X( l& r/ x7 y9 [5 [
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  ]$ f" L, ~" z) `; U解决方法:- @' J5 [. k6 W: J/ M/ k

3 _, j$ R- Y: N$ Hyum install httpd-devel
1 H0 a6 Q, {/ _2.没有pcre
  g; o/ ?) c3 d4 N
; l. Y" L5 W. Pconfigure: *** pcre library not found.
* T4 T  m8 i7 a7 E) c+ r$ oconfigure: error: pcre library is required! V2 ]9 P: q2 i* L& m  B% T5 I
解决方法:$ ?+ h3 R- n7 B# T- K9 r* H* @0 K% t. V

- A5 q' E: \7 l# ^6 Byum install pcre pcre-devel+ `+ ?: z0 L  G/ c
3.没有libxml2
( l5 D4 N0 B# |
" Y$ G. c5 k4 n: ]: R  b
$ m7 t/ X1 n- N1 _configure: *** xml library not found.
# t" c1 J# q: }; x0 xconfigure: error: libxml2 is required
& v) C1 U+ [( U解决方法:4 D" F  ~# o5 k6 I( @1 I0 {# P0 @: I

; t" A3 N: d6 n$ Z3 }! @' dyum install  libxml2 libxml2-devel
+ `+ e$ K  {! a" n- B& l$ y4.执行 /opt/tengine/sbin/nginx -m 时有警告
6 N3 \% q3 @: Z
. E* n% \, {! rTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ O$ @/ f6 d7 C% X4 X$ u& m6 i1 j5 qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 R4 w* k$ X) V+ w2 C( S( f
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
8 R: `/ \9 j) c+ P: W9 G7 n# N3 ^; r6 x  z0 p1 E1 g3 e$ L2 s9 X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
# D7 T- q$ S8 x8 U# D8 p/ t) Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 W* H6 T9 ?" F4 v5 ]" H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, H& E8 z$ y8 ?: Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; t; @+ p; _/ B# e# z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
. d: j: @9 f7 p8 g( A! X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ z$ o# g% L1 [8 \* y解决方法,移除低版本的APR (1.3.9)
6 q! c  l, ]- W' f3 Z) p6 m( b0 Q6 n+ q7 {) [" K% f8 `
yum remove apr
( l( Z( f3 u8 \" l- ~  K0 J1 v3 f5.Error.log中有: Audit log: Failed to lock global mutex9 p1 |6 k, d; R0 ], j

# @3 _# z( ]6 j8 }2 t. `2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
& Q8 E. u/ Q! J  H" W+ |0 W. p/ Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]  K7 [% L# j1 E2 {6 c
解决方法:
7 c6 J  M# v9 P# [" y6 y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- H, {( |! D. Q) ^2 b# {, S1 }0 h) Q/ i% s" E3 o4 C
SecAuditLogDirMode 07779 \& J. G1 \( w- Q6 e
SecAuditLogFileMode 0550
6 q1 L+ u: f. ]" ~$ s4 ]; t' ?3 QSecAuditLogStorageDir /var/log/modsecurity. @; T$ h" T+ m! I! \0 K
SecAuditLogType Concurrent0 B; g. i* |1 s% [4 v
参考文章:3 C8 s, L3 E: V% f7 o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 D- J1 ^  H6 H2 H: ghttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-24 05:17 , Processed in 0.057216 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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