找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13165|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: r. H# E4 B, v. p: P3 y5 x8 a" p
一.准备工作, t. X# _' U" g- V. F
! x7 p+ s8 [8 A  {& c' @
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' a6 r' U) [# X5 J# L
- m+ v. v2 C: Y" h, g
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% J2 M: c% C. q4 Q

6 C+ Y* M6 X* S1 K0 \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 b0 v7 `( O; H  f5 v( _$ ~  Z% Q1 p
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 O( {( D8 N3 N/ g  Y. N2 z1 v
1 Z% M/ g- G# m6 w" e依赖关系:# v& e3 b+ e* ]( E! U6 y$ [
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 p2 r' d! s& u6 ~
0 ]& Q+ E5 F9 [
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel7 K7 e( V! f* w, v
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- x  j8 i0 I& _1 x3 k- u, l
. ~, o" X. ?/ P8 d9 xyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 |* w9 k: c+ {, Z1 W. [' \: z
二.启用standalone模块并编译
( _2 _! t" \4 q$ f
# Y7 R7 R- g# Y5 B$ F: v$ O' ^下载modsecurity for nginx 解压,进入解压后目录执行:6 Q) a- G( N- A0 j- S+ _" q& k7 p
! q+ D; a. t! U7 q
./autogen.sh: z3 s! W1 [# K; ?
./configure --enable-standalone-module --disable-mlogc
# M% g' ?7 Z' Z' m* Nmake + ~! C% p: m* Z# H/ _
三.nginx添加modsecurity模块" m$ O  L1 _6 O# i, R
+ M/ I( S9 D! C4 K$ k) @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% w9 D$ B2 @0 G) H1 }8 c* C6 |: f5 e, h. V
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! n" e; W3 ^$ h5 Fmake && make install  j5 d8 y1 X. l0 G) t1 y5 V' k
四.添加规则0 P5 u4 E  ?; R5 }
$ p! ]+ B  f$ ~* T. L$ k
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: l% G# R. Q! H* K

# Q) W8 H8 x, ~' o5 j8 n1.下载OWASP规则:. n6 s+ [* |. _

0 M2 _( L! E" |5 n4 m& Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 l( n9 W3 [4 e3 g* w  o' _$ @: F; r9 C) g7 D% a
mv owasp-modsecurity-crs /opt/tengine/conf/4 w; E: U5 I; a% F
# i5 o3 H+ K7 _! _; Z6 u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 ^; s/ A/ x/ \; ]( V3 q' l2.启用OWASP规则:
7 B6 c' [9 t# h% {; g
! `8 h# K/ a5 B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; u$ f9 n3 r4 Q
- q) I) q3 U: l; _
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% o7 Z1 y/ V+ \# Q" \. I1 L+ D2 a
/ e% N9 n# \  E0 f7 y2 U' {
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' Z8 g- d: o1 b, p4 B! {! r, q9 M* t3 Y) }5 T4 v# n' l. I
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 i: c! V5 p( Q7 G7 g$ m# d3 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 h5 N  i, g2 O) e4 Q' h/ C3 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf3 _3 u* x& z  _# h! C8 [+ n$ V
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# I% _' Y; D, j) R3 \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; b; C; g9 L" @2 Q  |4 \  v8 a1 T$ eInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf& K" O: e1 q6 B2 l+ m
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
6 o" J7 Q4 D( S, S' ]五.配置nginx$ \$ H- p0 B  I

1 j7 i/ U' O* s* Z1 k4 i2 b在需要启用modsecurity的主机的location下面加入下面两行即可:
. D/ j) X) K6 t/ B* V2 ]" F5 h
$ m" f$ ]4 K  ^$ T0 [8 cModSecurityEnabled on;  
9 r) _9 W; K* Z9 d, n5 c7 v! X  gModSecurityConfig modsecurity.conf;! k2 Y- w9 u# O  k- D5 A3 \
下面是两个示例配置,php虚拟主机:
/ V/ V2 j& I! R% c( t0 G- a5 t7 c( X* E& j
server {. J  U) y% A3 E! P: Q/ M1 O/ M; {: I
      listen      80;) [& R  U% j1 w( ~
      server_name 52os.net www.52os.net;
. g+ n, o. O% p% W6 q1 v$ o     
; E- S' v5 k3 v  q2 }      location ~ \.php$ {
* {" Y4 W& m, Y! t( x$ L" S: }  P      ModSecurityEnabled on;  
( P/ N  v) X( }/ ~. o( c      ModSecurityConfig modsecurity.conf;3 F  ?3 y$ X; _: l0 H  G6 k

( H  b1 i$ b4 t      root /web/wordpress;
) X) e. ?5 p4 w      index index.php index.html index.htm;
9 `& n/ b2 B  m& D$ r7 {/ c  
: ^' G# w+ V0 ], h: }      fastcgi_pass   127.0.0.1:9000;5 e: F* o( L' u6 q' k
      fastcgi_index  index.php;
7 B5 C9 {2 h# T. U& W1 `4 S      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
& Y$ O8 m4 N" x) q% `# ]9 k1 F      include        fastcgi_params;
4 ^7 C+ y2 w6 Z3 V9 F1 K6 y4 u      }
3 p6 C& a- u) B+ _  U7 t' D2 o  }
1 N% y' }  w# y! {9 T8 u* }& {upstream负载均衡:
2 @; p  T) F; {( _0 }0 e1 A7 @/ i6 m: x8 m( v" q
upstream 52os.net {
5 C; q% M9 I- N) b; z( X. R    server 192.168.1.100:8080;6 X) r) ]. r, T2 r) G3 d
    server 192.168.1.101:8080 backup;
8 ^! N% d8 q6 ?$ @- X2 C2 O}: A/ h% N2 I! z( V) `6 s- z! q

9 Y, r7 a5 u- h. x# K: n, h7 Vserver {
- a1 |8 _  B* q# Blisten 80;
0 r* o3 |8 d8 O5 h8 `server_name 52os.net www.52os.net;" w3 f0 W. i- z( x6 s2 e8 k2 U

- A) o8 ^- U, W! H  v  xlocation / {
7 Y: ?! _; P! x7 Q; Q    ModSecurityEnabled on;  
0 C' N9 _: h: ?; J4 I9 ]5 k5 r    ModSecurityConfig modsecurity.conf;  
+ Y( F$ w$ C2 v! [6 i
# R# A) q" Q6 Q9 j9 e& {' l7 M! o& l        proxy_pass http://online;
) X. ~& F7 b; V. x% d1 f8 c2 p        proxy_redirect         off;
% k" n8 u6 v; Y/ W. W8 U' d        proxy_set_header Host $host;" y% O" F- f1 ~5 ]4 @" c5 u! k9 R# X
        proxy_set_header X-Real-IP $remote_addr;* B. o/ `+ U( r' P$ h% Z
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;( W0 K# z3 X- C+ V
    }- ?  D) y/ {' W! m  J. O+ Z7 w( j0 V
}
# h2 R$ R. I6 [3 S- e- C六.测试" a5 w( g* F( ^

" ]+ Z) f/ ~7 o$ b' J- e% y- A/ G我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
0 r0 L. t" R7 q8 T/ L- k
. C$ v- W4 @( E1 @9 k<?php
0 F, o; w9 }) A. d% ^! v4 d3 ]    phpinfo();    ( f" t- m  |! u( P
?>
" Z) t' s) B5 ?: j/ C( I在浏览器中访问:7 H( W9 R3 ?" \% e& L) H
: m4 R3 {. y  ]. F( b& P
http://www.52os.net/phpinfo.php?id=1 正常显示。" L3 A; u4 z; r$ j/ T* a
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
5 n; K7 z8 m7 u1 s* Rhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。8 }6 [- J  h$ Y+ L7 _* {5 J6 O
说明sql注入和xss已经被过滤了
) D8 R4 t  x- M  D; C# f& O- O$ ^! A, n% a2 x8 W6 T+ L% B
七、安装过程中排错% \5 _" b) t1 h8 V
7 z& d0 f: `2 f% j
1.缺少APXS会报错& o; P% V4 n$ g8 F4 |* `# b6 w+ `

8 r: u$ b1 F5 d& aconfigure: looking for Apache module support via DSO through APXS
6 l6 k: L2 }& Z1 Dconfigure: error: couldn't find APXS8 N( P' A8 |# x) ^, q& C  f5 x
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) S1 F7 S) P( B2 b+ c, Y解决方法:
% o+ O+ Y7 [( j/ k; B" `
2 H9 H3 A0 A2 Jyum install httpd-devel/ b) C  H5 [& W8 D. a. F
2.没有pcre9 i' W: Z  {, Q0 m  Y
3 B: r! \$ `1 u
configure: *** pcre library not found.8 r' b, ~5 F! G: s/ o3 H6 Q
configure: error: pcre library is required
! u1 M; g' E3 J# j3 o解决方法:  I; X  A+ ~, N
9 ]; o5 V7 N9 a0 R! e
yum install pcre pcre-devel5 l6 c3 A# {% G6 Y, l" B
3.没有libxml2
8 P5 L6 f2 _  Y7 L+ y
+ `5 Z) h( [* T  z2 X9 E
8 ]$ R7 o0 h5 h" G+ z3 Bconfigure: *** xml library not found.
4 T0 V. r1 k, o% h0 Y6 Y7 |configure: error: libxml2 is required
5 h% |" D) g7 y1 D' j解决方法:
* J; y) y  @# P; r  x0 C/ w9 P  W. X' q! Y- M7 @! |/ B4 m4 y& q+ {
yum install  libxml2 libxml2-devel
9 N7 r3 X' a: ~2 V4.执行 /opt/tengine/sbin/nginx -m 时有警告7 t4 G9 c" }, R& \  i

0 `2 q7 B" A  @  f' i% ~Tengine version: Tengine/2.1.0 (nginx/1.6.2)4 H" ]" X, n1 L- A3 Q5 F6 U0 H
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ x$ s- }, M2 P7 S2 a( f* r原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: _( C2 S$ _0 [- X' w* D( @) o. E: u/ j
/ _$ g# D" Y9 \& P: d1 D8 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. q4 x, t1 ~5 x5 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 @& I% F  c; x: T% ?7 a
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! f7 @0 t9 U6 E# g* C4 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ T1 {6 r; j' c% C. V# V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) b! p: F0 A' W7 N+ N# \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
& h1 [" X2 K/ L  w" k解决方法,移除低版本的APR (1.3.9)' I9 N$ Q( z% B# I! r4 q! S
/ q. J( f) L! |5 v! W
yum remove apr
. `+ b1 J( s! `) [, N5.Error.log中有: Audit log: Failed to lock global mutex
+ v5 _  G+ L  l8 c/ D* G$ W0 s( W/ M5 m7 f9 ?
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 B% s, l7 Q( O* y2 [, P, _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) Q+ Y7 L" y& ?2 M: p
解决方法:* ?+ o4 s: ?! ~; X' E' a: A
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. l: m- V# ~$ Z4 s( x( Z
$ k( t+ s# _7 s# F7 ^! \
SecAuditLogDirMode 07776 m% y1 l2 E; O: h3 A5 V
SecAuditLogFileMode 0550% o% a8 Q% q$ o' p' q" b( g4 m! j6 z
SecAuditLogStorageDir /var/log/modsecurity1 m: {* U8 @- }; t& d( b( c  h" J
SecAuditLogType Concurrent1 M$ d$ }+ _  B
参考文章:" [) u0 D% }7 J1 @3 H
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
  p; a! _% S$ C& O& Ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-20 15:50 , Processed in 0.048375 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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