找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12546|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
, [% f: H, E/ O3 P/ ?% r; i4 D* k1 e) [  [+ W  L
一.准备工作
4 u2 e/ u2 q' y5 w5 x
8 f! f0 u, B3 B0 ?3 N) V系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. V+ ]* d+ |9 D6 B1 A" z8 A9 v7 m* G
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- S$ Q" o0 r- m& i0 Q  B  u( n

5 S1 W6 }7 {8 y5 j% W" |modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ T" j1 V: U: _
" I$ q7 \9 A' x, c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- X* M- B5 L5 Z' R* L
* [# q) ~) x6 \3 X3 o0 c依赖关系:1 ?! G( K1 {! A6 ~! w
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; @- Z1 ~, o6 C8 Y/ s. Y
. e% \* ?+ P- @6 x$ n& r; qyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel# t" i" U  \9 O% s
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 A/ o( g4 a) Y, [
! `- n# j  H$ z" k  U) lyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel) o3 F0 k$ I/ R5 E, {" Y5 v
二.启用standalone模块并编译: n+ k1 l' |$ |& s0 a

: E8 e8 d# j( N: H. q/ w9 P/ P下载modsecurity for nginx 解压,进入解压后目录执行:
, V! v' O' w  W' l8 \1 I
4 D+ B, P# ~8 L- Z- P/ R./autogen.sh
9 s# w5 q  B( Q7 h6 z! o./configure --enable-standalone-module --disable-mlogc+ Z  p* L0 i: [$ ]$ v
make * B0 j% ~% P# g4 F& n* g& v
三.nginx添加modsecurity模块
0 T4 n. F  e9 F
$ [! i2 C6 z% z, l( @在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
! Y5 p' {% N! _$ R4 R9 @5 H# ?  T. c  l$ ]3 f0 V8 V
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine- m! L* v' |3 C9 ~; v+ U
make && make install# L; d% P# }6 d% V) |+ j
四.添加规则
" R9 i; t0 |( |% ^2 Z0 f6 |1 x. q; U  W' y
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
, [3 L0 p, p! K8 ~, a' K3 U. y+ t1 l3 w& N4 D" @& q6 d( g
1.下载OWASP规则:
6 b- H) k- R' D; g! G; N
/ d$ q. ^5 ~% [/ r! r. ~! q' ]git clone https://github.com/SpiderLabs/owasp-modsecurity-crs& {' M. g6 ~' {% q8 U; i

9 X) S! d% m  c4 ^  tmv owasp-modsecurity-crs /opt/tengine/conf/- |8 b& p; G6 H7 o5 Q: A5 v+ O5 D
7 R. B* W; {: g  d- ?" F
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 S4 ^; J/ a$ y8 z# }
2.启用OWASP规则:
7 q% B- r& l" D* b( E: b* H  ^$ ?
4 ~3 z5 @0 j" ~& g7 L复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 d8 a8 e% g. x7 T% G. x) w; a
2 ]& T/ A6 x; G* F3 v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* d  W5 p4 D; i" z! @% i: e
" U* O# \: w0 I9 u1 s3 vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 B, P: M8 ]6 h' H  M; z* z! _! D1 R# l, w' M
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 _; \# g& h( B+ L4 @. q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& q* e% G; ^% N1 [4 B2 o9 ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ x: J% M" W2 E  E* E) uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 s, V& v; ]1 u& j& g7 B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 N. H6 y+ O* fInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ d, T! [5 l+ V7 V0 i8 r- Q' i
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf  D8 V" D4 @5 \- _3 a( t
五.配置nginx, m7 V  x7 T4 H9 R
) Y7 O/ E& ^( M: e
在需要启用modsecurity的主机的location下面加入下面两行即可:
. f+ I* a( F  |$ R  B, _, M6 N  ~' r
ModSecurityEnabled on;  3 q; v0 v2 {$ \2 y% q  o
ModSecurityConfig modsecurity.conf;4 M5 P! J4 `+ C0 i2 T& i
下面是两个示例配置,php虚拟主机:5 P4 y- ^  u! Y( f5 _% w  z) ~

1 q' w4 w( x6 userver {
7 a- W. N2 `% h: S      listen      80;
" x. T# w, X1 ^2 {; o3 F      server_name 52os.net www.52os.net;( N8 G( x: I) b# K: V
     3 m. O2 i0 {2 `
      location ~ \.php$ {
' `7 z! P7 B* @  z* G4 Z2 n      ModSecurityEnabled on;  
6 R2 I3 @/ h1 r2 X+ v      ModSecurityConfig modsecurity.conf;$ Y+ E( v1 k9 U6 k

9 D' n6 F3 b' m& j" h      root /web/wordpress;2 Q2 j# X) q1 ^- j8 S. s  f; L
      index index.php index.html index.htm;: Z9 E+ Y& Q& Z) n  o/ N5 P
  
- M2 g- o; D  p( Q7 L' I' e6 F      fastcgi_pass   127.0.0.1:9000;
" {% {8 C2 A* t9 ?' k      fastcgi_index  index.php;
. s2 c, ]1 O' u" B$ L, I2 x      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 Z6 U; D% M* Q: q8 `# ]/ t
      include        fastcgi_params;" v+ d! c1 T! D6 ~( J
      }
! x7 A+ Z4 d. \& s( w, r, H  }
1 p2 G0 h4 U4 \/ O1 Qupstream负载均衡:1 z3 q5 V; z, R9 q0 b$ S

2 @, L& O" O% @  K/ oupstream 52os.net {
1 ]+ T, w8 U0 @7 m9 ?. S3 M    server 192.168.1.100:8080;
3 V) [7 e) H4 y    server 192.168.1.101:8080 backup;( c! I8 o; ~* o* s" L
}) `4 j8 H2 b) U* u+ {9 g8 \: Z. Z- o: H

* ?. O. s/ M, n1 v, Wserver {
/ i( T' m* d3 O2 }1 W! \) W- |listen 80;
2 d' F3 R3 p# N; d$ |6 r8 Aserver_name 52os.net www.52os.net;/ p8 F4 D  C. S% y. S/ n; D

- J" s! H6 u/ W  plocation / {5 C  p$ c6 P8 W
    ModSecurityEnabled on;  
$ T) R# r  K# |8 H: Z+ c    ModSecurityConfig modsecurity.conf;  ) n% W. Z$ {. i5 P9 ~3 s/ `

8 j4 x4 e& d. k# l" s        proxy_pass http://online;: m7 e' T/ G9 v# m$ ?& L8 W
        proxy_redirect         off;
( x8 D. f' Q6 H& n% K! j        proxy_set_header Host $host;
6 ]' s1 |& e% t0 R        proxy_set_header X-Real-IP $remote_addr;1 G) G' O& q) _' W
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( ^; l8 x, h; L9 s2 P8 U3 m$ g
    }
2 @/ y$ ]( z: y- d! U2 e9 E5 Y}
# k& ]: P, @" B; z" U8 P. j7 L1 o六.测试& p, ^( ~( s1 C" b& {" Y0 A

* d8 c3 b- C" ~7 K5 e) z! x3 t, A; B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ J3 j8 z1 Y1 B9 j* d, H! }* P. k

( L: [7 _. A+ ~9 L4 f<?php
6 e9 ]5 q% }0 @  v    phpinfo();   
8 ~+ B- g) Q. c. `4 {' ]?>0 A' p  y( d; A7 R5 `$ }
在浏览器中访问:
0 k5 Q" E; V7 L+ u6 U. o8 ~* R; I8 {. I" E* [% N$ h; y" t
http://www.52os.net/phpinfo.php?id=1 正常显示。* o; Z* p+ I% L( b8 u
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。# K! L5 h% q5 \8 L7 l
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
' C$ T5 r1 G( ^) T4 t  o- E说明sql注入和xss已经被过滤了
% z+ x4 b" I# u# d/ x( R+ e# E( }$ P6 ]4 V
七、安装过程中排错9 `$ Z' x  _+ Z2 w* L6 [
. f# C9 C3 O9 E5 g& j1 m0 E. Z
1.缺少APXS会报错+ o2 a/ L3 ~6 ]/ Z- H" @

# B; D" o8 w) Y; l; vconfigure: looking for Apache module support via DSO through APXS& m" q  Z7 f. V$ b$ I% Z- K
configure: error: couldn't find APXS: ]1 ~- @. c4 n& F6 n
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: q) {/ M- [1 \1 H& h; z" V) _9 P解决方法:1 y: |% y' X: S8 k  D/ @1 m4 N
8 Z3 [3 ~% C4 k5 H2 f
yum install httpd-devel6 ]0 j& C' I4 i
2.没有pcre
  F' _# x, h6 S8 E7 e' q2 y' a; F, h: ^. A
configure: *** pcre library not found.% I1 f1 G4 z2 O8 K
configure: error: pcre library is required- r( `* `! V" K2 e( b: R- o
解决方法:/ W* x+ |: l1 r; Z0 n# D9 w
) n0 e: [6 ~$ P$ f9 i6 V
yum install pcre pcre-devel+ E, s% f( T" ]) d& Z
3.没有libxml2
& X; k3 V. U# D% O
% ^- @' [% M* I& F# @4 X+ w+ Q! ?7 I' e; N( V
configure: *** xml library not found.
4 v2 X. ~) ]( o/ Z( f0 dconfigure: error: libxml2 is required
% e+ V& s  C4 W, [解决方法:/ \+ t/ F! ~9 F8 c5 c: z
# \3 S! T  ]% R9 c5 z7 C5 v; O
yum install  libxml2 libxml2-devel
- @6 C3 F  Y/ J& ]4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ B# e  S, c7 U% b0 n/ H  M" T% @$ Y0 l
Tengine version: Tengine/2.1.0 (nginx/1.6.2)) O0 s3 s, q4 K9 s
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' K! Z& R1 D6 T& m原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log* R* v, R" h: J" B4 g: t

* q" O# \& Y( o7 Q) `) N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ f8 Q% }/ i4 Y2 h9 u: i% x- Q' g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9": q2 H- a) I& [5 K
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 F3 l4 o9 y2 z. ^2 D7 Z( |" X* P5 _2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 X+ ^( X4 }5 [2 ?! Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 y5 d+ u. s$ r6 n" W) Y4 k2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. S2 {. ~7 E- X4 a. |
解决方法,移除低版本的APR (1.3.9)
! S3 ~+ l/ d9 T; c! @4 P4 C  o2 @8 A- R3 _
yum remove apr0 i( [* Y0 d' a" B5 l2 b! G) o
5.Error.log中有: Audit log: Failed to lock global mutex
% Y9 `6 l/ f! F5 P5 _
) w# `* E$ J6 F, F- N$ N/ t2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% I- c2 F) T4 v. B- R8 bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 Z& W3 o. h0 f
解决方法:
0 o. F5 s* @# j# F0 g2 @5 j编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ ^# A$ S  I" b6 V
* R5 d& M) I1 U$ g8 Z# j( P1 i
SecAuditLogDirMode 0777
" E  ?$ r* `7 D6 i6 `$ e, F2 aSecAuditLogFileMode 05501 J7 n: |8 Q. ]* a8 x4 Y4 L8 A5 E4 \
SecAuditLogStorageDir /var/log/modsecurity2 L1 L( O% a! H& j4 a, \/ w
SecAuditLogType Concurrent/ Y% n$ c) S" L1 k  S$ `; H
参考文章:
) q2 D# I) r8 L3 lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. s# C# [( v5 q$ \+ ^4 T+ G  ^
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-15 18:14 , Processed in 0.067418 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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