找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12141|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
  v4 Q! b4 n7 z! R( M$ v& ~
1 z7 F" A& a  Z( \4 @& ]# M% Y一.准备工作8 [& }4 n8 G* f8 Z& @$ `. Q
! C+ [" V6 J. U. r/ Q
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; Z- W# ~$ z4 g! o. `

1 o+ x% t) m5 z# E, K8 b% L: ?5 d! \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; e2 O  H' W* d
- C, p6 g1 F$ w- i& N, ^8 P9 [
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 W# [1 G2 @/ F/ E/ A( @* m

1 q) b, b5 s& M* f- H% ]OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 z; B& \$ j8 i/ o( R5 g/ r" J; I9 D" t0 a# \( c6 e
依赖关系:
0 M/ e. j0 A% ?7 rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
  y, S1 I# J, H, y  M$ I) k
- y9 J+ D" j! ?  Gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel4 W* L* J+ ^& k0 q
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 M, @+ z2 b& v9 ^) ~) p
0 ?1 y! F$ t* X# [9 K6 V5 Oyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& Q$ }$ Q2 |2 _1 Y7 |& n) E( ]/ v" Q二.启用standalone模块并编译
! _5 G" k( a& h( k; V; r% D; V7 i( {( N
下载modsecurity for nginx 解压,进入解压后目录执行:9 |( v7 b% ]3 `' A$ v  q8 s
6 B; M  [# y7 N+ W5 X# a  E
./autogen.sh/ G9 L" I, i6 s9 s7 U
./configure --enable-standalone-module --disable-mlogc
/ L5 n% O7 a& Z$ Pmake
+ u; J6 |% m# s& N0 }! R8 l三.nginx添加modsecurity模块
; ^/ C6 b& I; I0 V  L& Y( S$ L; z6 P: D
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
- l6 i* q3 {5 L3 b- A
8 L, {. \3 m& r. |* a9 b& u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine1 S0 k. T5 H; Q# D+ y1 l
make && make install
- l( X* F7 s0 c9 \* ^! V3 r) ^; Q四.添加规则
& h+ B: S" Y1 u$ w
6 C# s4 c, c9 ~" i# D" qmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 V2 \6 Z9 G3 ]+ K: b) [' _

; Z( R6 t( E6 L0 W7 O: s1.下载OWASP规则:
) M1 m- p/ ]  |) @1 S* J4 E5 m# B* X" v% D
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
5 w1 E. o! Q: `, R3 }$ @
$ C- o: @4 c, e8 Gmv owasp-modsecurity-crs /opt/tengine/conf/
, K, K- ?# X( u0 |9 o2 S5 p: o$ K  P2 t4 r- P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# R4 n* A+ {8 R3 I% s
2.启用OWASP规则:
8 r9 ?  q: Q' D& E1 F2 t4 l+ t: C  p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。2 ]# W0 y' m- k; q9 u6 U4 r
/ z. p  X7 m2 c9 o7 E$ x6 n0 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 C9 ]% D) J8 O2 e
4 o: r& O+ y& _# X8 U* towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' o* t! b% h" f. o! }/ T: P
- L6 H, c' w, u. D1 cInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. ~/ `2 L* w$ \; E6 k3 W- {. ~$ ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf: }4 z5 C7 Q/ i! K" m# t) W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 T) j% n, `: z8 U/ m2 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% o( L  x6 i3 S! n3 y% ?4 gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ t4 `: _  U& `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf1 l: `% h) P% L! C# Y/ D
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
* P* {" n) ^+ b" x6 |3 x五.配置nginx1 w, O9 t8 g: K; I* k" t7 D5 a

2 x  \0 L" T  d& ?在需要启用modsecurity的主机的location下面加入下面两行即可:$ ]& p; W) R" d; o" Q: d
6 I2 P3 K6 L: x( Y% `% A3 _
ModSecurityEnabled on;  
/ ]% M, }! f, Q) I9 ~. R1 Y' tModSecurityConfig modsecurity.conf;% ]7 S- q, r' O" X
下面是两个示例配置,php虚拟主机:
( Q% w* P, `; C# s6 X5 q+ l! M+ Q) ]' z# D
server {* w; l! j1 a4 z: j* w: w) {
      listen      80;0 s! Q$ ?+ x3 \+ K* l% R
      server_name 52os.net www.52os.net;3 U1 G2 V! O, Q; M6 T
     
. h# Q2 |# N+ \& c  ^- h& B( `7 y      location ~ \.php$ {
: b! A; P: G% \! o) b2 S# P      ModSecurityEnabled on;  ) J4 [$ y+ S5 i2 J/ P7 u1 f8 `  ?
      ModSecurityConfig modsecurity.conf;6 y2 `, e6 }, G+ ~; K

, R2 \8 _) E5 N6 g      root /web/wordpress;6 F* c! f% R* c
      index index.php index.html index.htm;# g$ z" X; G0 o  i7 j$ P7 D) l
  
3 i) I5 ^3 V& X; Z2 N3 H      fastcgi_pass   127.0.0.1:9000;
7 Y* p! F/ v' Q; \0 F      fastcgi_index  index.php;+ q/ a; R  e9 I5 {. ~  b
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;! @0 S  w! ~! j% E- _, g
      include        fastcgi_params;
3 l( C+ H, B& J4 t/ E+ Y- w      }' i, v- d! M" P# f7 ?+ S. \
  }; O* Q- a$ p( [2 T3 q4 `" |+ o- |( s
upstream负载均衡:* |$ A- U3 M6 w# ]" {9 A
) @% C9 n5 S+ l0 H  R
upstream 52os.net {
5 @; e( P) F, s& ?7 f; u; J    server 192.168.1.100:8080;
" t. W+ Q* @* @    server 192.168.1.101:8080 backup;
4 |! w" _7 E2 c6 W, D" O}, r/ s3 w2 N" p; E$ D2 e
  z9 E0 r6 P% z
server {  W% T$ C2 N9 l* z( C/ z
listen 80;
& p+ |" v6 Q* C* {, m" Gserver_name 52os.net www.52os.net;" X/ E+ `) Z+ _4 I+ ]; B

% _: e) J# \/ mlocation / {5 F# a- c* t2 d+ I! R9 [0 p1 k
    ModSecurityEnabled on;  " G3 K7 S* J2 s( b
    ModSecurityConfig modsecurity.conf;  
2 c  U7 W) f9 \6 S# R+ a
) e2 K! H9 D0 }+ H$ g' a$ `  t: {# h        proxy_pass http://online;
3 N7 y7 J$ n, ?9 C; f7 r        proxy_redirect         off;
( @1 M/ m/ P3 I0 D        proxy_set_header Host $host;
6 D/ c! o* x; m0 t4 C        proxy_set_header X-Real-IP $remote_addr;
5 z8 a5 C# h* c4 |$ I        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;# z3 h3 h2 r8 M- h3 {7 W
    }
6 f2 Z6 `8 N2 ^$ S3 _$ I3 d0 _& ^}
  Z8 w0 v2 P; ~& a  R# C六.测试6 ]5 D' \8 z7 ^- ], {1 I+ h; r

, w, t! t3 @, u* G, i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 ?6 Q$ g8 n/ |: B
' ^) w: K( s/ C, R; O! l( p2 c<?php3 Q; E7 ?+ v, c4 n) f5 k6 ~" P  B2 F
    phpinfo();    * \8 s/ C8 E$ l+ G6 f
?>2 k' z0 ?# y# x" `  V( Q+ ^
在浏览器中访问:
" a% b& p0 m; ?( c+ f5 ?6 c1 i9 c8 Q% H. K7 S9 n
http://www.52os.net/phpinfo.php?id=1 正常显示。3 x8 {4 S% l9 g! d5 h, \. E
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。2 `" f! o7 Q; A2 i, g6 F1 P
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
& {* L7 o, X4 L0 I8 B/ M5 o2 d2 X$ b说明sql注入和xss已经被过滤了+ f9 W1 Y1 e% L! O2 Y+ q; C
, D* x9 J/ A) D" U
七、安装过程中排错
( r1 y5 p" M5 O4 Y1 G1 B$ P; u* M" a0 `% u- X+ M! g$ h
1.缺少APXS会报错5 d; q+ \: Y, W& y

, h. K1 A- q9 T1 |configure: looking for Apache module support via DSO through APXS+ {% |# G+ A; a' U3 n# `
configure: error: couldn't find APXS$ M3 L# P8 K, o3 F5 C, v3 M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! [1 D! T8 z" A9 `/ ~" b
解决方法:; @+ A. e2 x3 M  q/ O6 n5 k

* n( D/ T$ D; d6 Y5 P! j7 jyum install httpd-devel
7 X4 F- A4 {0 A" M2.没有pcre
, K4 B* d/ m/ m: d: {6 M: K2 ^  Q1 y
configure: *** pcre library not found./ m1 I  i& n! x) |$ _# n. W6 s
configure: error: pcre library is required
- ~! k6 N/ S$ K解决方法:7 x8 }# ^: }; O: ^( s% y6 w
$ {4 h% ]3 C4 F/ t
yum install pcre pcre-devel
* {/ G& p$ {: g7 K( N$ C3.没有libxml2
* Q- J! E( K% b3 y, X( k3 {3 }6 a' d3 a  ~9 C4 e; T

, R2 _, ?8 ~3 ]9 z. t0 b2 Nconfigure: *** xml library not found.
* q0 j- w) l" M; @configure: error: libxml2 is required
, w: r. O1 n0 G解决方法:
! ~5 Z& w7 L) |% z7 G7 j5 i9 P( Q! \2 w% @3 D! j! R
yum install  libxml2 libxml2-devel! }5 @- T6 D( a0 y, U% d
4.执行 /opt/tengine/sbin/nginx -m 时有警告: t4 I& s. R' f5 C
3 ^) R& j4 `% l! Q! f# o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
( i: R. C1 }1 f. J  _nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
  @8 ~- w' s- w- {6 ]% }% K原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( g( B8 {3 e  \- T' |* m3 [7 q$ `+ b

* k: z2 {9 N, J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ N2 |- E$ P) ?0 Y; K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
& w) i/ a$ W, X. C6 |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!, V) o+ H0 S* a! c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 b) _1 G! c; G# P- n$ ~) q6 c9 V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 c6 Q) ^4 s" N8 b4 n: ?
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.& z+ c4 H7 V3 D. _! m
解决方法,移除低版本的APR (1.3.9)
5 h: B$ C9 H2 b# O$ Q' o3 e8 G9 W
6 {# l7 p& c& B) X( iyum remove apr- M4 Y/ k' V7 T% c- D. ]4 P
5.Error.log中有: Audit log: Failed to lock global mutex3 d- M$ p1 Y( L7 f$ v

3 U' Q, G9 C+ C  N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
( ]2 q' ]2 n( r) vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* o5 y/ M' M8 }# |; t3 c解决方法:
: Y9 q$ H& a; I编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" ?9 ]8 y: D2 P+ }6 _( D4 `$ R( e3 J* Q" X& N: U
SecAuditLogDirMode 0777
2 a, ]! ?# l. i& ~- k; i  SSecAuditLogFileMode 05509 Y8 X6 k5 n4 Y% G: [0 {
SecAuditLogStorageDir /var/log/modsecurity4 p, H6 H  Q# p
SecAuditLogType Concurrent
8 y( T! B  l; {' U$ M" x参考文章:& @3 L4 B; m; Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& ~4 g. {: u$ k1 H
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-13 01:15 , Processed in 0.099574 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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