找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13090|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% \; }7 p1 I0 K9 [+ a
- Y, z: b2 S, e) f2 m一.准备工作6 Y; M: y9 O9 Q3 T: @3 I. E
9 h  M8 a6 U& K
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ B" t1 ?! i. B9 g+ F, a6 g& U4 H4 K
5 r! @* V! c  g. ~# v' Z  t
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& J( V; d0 J  |& K% B9 H

' N+ a7 |( Q# ~8 O. `- p3 d6 nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 R1 U# Y; F4 U9 j* R; [

% t" x3 |: Q7 g: X9 ^0 FOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! M* y' {' B7 h0 D6 w9 C9 z
9 e: i5 l1 m# v3 {2 m
依赖关系:
7 x: y% E5 B& e5 G8 ?" \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* a" B$ d1 e' w1 m$ ~! Q

$ P9 Z. T+ h) B5 Q1 fyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel8 r* b! s9 V/ |0 W, c7 y9 P! f
modsecurty依赖的包:pcre httpd-devel libxml2 apr$ O$ H: r0 Z9 g

2 q; ^- c" ^) M  ?) ?yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 E0 I2 `; e6 e) N+ O
二.启用standalone模块并编译
3 Y5 F* l" }# }. E6 z4 [8 A
3 H+ Y3 |, K1 U/ W; f# L1 T. F下载modsecurity for nginx 解压,进入解压后目录执行:) G$ |- c0 ~, d# r. v8 \1 n* f2 R
' k6 u$ X' f& O9 b
./autogen.sh. U# G/ U/ k: D3 m
./configure --enable-standalone-module --disable-mlogc
2 X" a9 S6 j: [( @" c3 b, l3 @4 Hmake
! B6 q, }& n% a& [5 f$ x6 Z三.nginx添加modsecurity模块
, M. e& |- ]) E: Y' e6 @1 W
  E: S" v+ l: \  l% n在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:' m$ O. j( U/ ~  q

. u6 Y! [  I. s: c( ^& c./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine# _0 t! H9 G9 p: D: ]( s% F
make && make install
0 j) c; q( J+ H- u% W四.添加规则& N$ n2 K/ L! m$ t" u" y

+ A( p; \5 W( M7 B/ Pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: g. }  Q" H/ E. B
9 K! V' C0 s0 F6 _0 N- `1.下载OWASP规则:
+ i0 J, v  \  O- s# |! `; g6 W9 v8 h! q% I  i- [/ r/ c5 U
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs& Z, F- Q( ^9 ?1 a& _. L9 U
6 @4 }9 ?% ]9 G
mv owasp-modsecurity-crs /opt/tengine/conf/
$ |( H& K: E! d8 k9 B7 m3 F/ C4 m+ b( e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 g8 b6 Y) A5 J0 N2 R; `; ^& p; ^5 N2.启用OWASP规则:5 g8 U) G9 o( E: f: z7 i# [) `4 T
. p: ^) {4 }! l/ j& Q9 `! }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- Z, C9 {, r1 _
% [( K) t$ D% [* u( w+ y! u% F. F编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# V7 i/ u$ x. t* A
: l4 R8 D$ C* J4 R& U' W7 j9 u& f% b+ |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 y4 H: e% g* H7 D) O3 v; j

  f( R& F/ E3 }* `- N7 TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 K- l( J* u" S1 E) F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ v' {2 I& `8 D7 y/ LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf# {3 k3 K7 |+ R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 R) U. z- Q, Z; [2 D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 X& u6 O& \. E, V3 a) K1 f1 v! `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& D2 \5 N3 E5 e7 D( S! a* Q  JInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf5 K1 h2 i' ^" O) u: w: r+ s
五.配置nginx, _4 n, w3 D1 X; R8 q1 {/ o1 n6 s! ?
- p6 Z. c8 V$ O/ y  \0 d4 ?/ T" E( ?
在需要启用modsecurity的主机的location下面加入下面两行即可:, m  r# m4 d9 h" L& j

0 u/ a) n" B" I4 w2 VModSecurityEnabled on;  
3 {1 U/ J0 g# x5 H, {, D, k% E6 B- C) vModSecurityConfig modsecurity.conf;5 `5 @& B3 A( ]. C: t. \0 w) u
下面是两个示例配置,php虚拟主机:8 o% u! D' Y3 O0 C2 B

/ W: Y0 o$ `. v7 [server {9 w4 H: g# C- p  B$ `, z! `
      listen      80;5 B1 o: [0 E1 B4 g
      server_name 52os.net www.52os.net;
% b' @8 i4 O; ~7 Y     
; i$ l: {3 H3 f0 B$ a  a, o5 v; `      location ~ \.php$ {: z$ {5 v8 g- w- m  u( i+ z
      ModSecurityEnabled on;  $ E5 `# W3 Q% G7 S7 C/ v: K( {: b% `
      ModSecurityConfig modsecurity.conf;. ]4 @5 q% p8 ?
" Q1 w' s( Y7 ?2 x
      root /web/wordpress;
& d* o6 o6 ~; x' S7 H3 Y# G# j8 ?; K8 h      index index.php index.html index.htm;
% s7 }# I9 @1 K  3 G$ f9 ?- q0 b& C6 m0 y
      fastcgi_pass   127.0.0.1:9000;! L" ~- @) F; Q6 X
      fastcgi_index  index.php;
/ H/ I8 S5 k5 h7 g5 t; P      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
8 p" E. a" Z5 p( q      include        fastcgi_params;. F6 ^+ K' f  E  c
      }! z1 `1 A' K! C9 `/ F+ s/ V
  }
6 d" A* R$ F  F4 U/ @upstream负载均衡:3 ]; n; K' [/ Y

, F! _. b5 z* H# X  F% H( d4 rupstream 52os.net {) P5 A* O. U7 h; q- I) w+ |
    server 192.168.1.100:8080;+ T3 S8 {9 t& S1 M
    server 192.168.1.101:8080 backup;
  r$ i0 W, b0 Y2 {6 ^3 g, I}
1 U: ?4 A+ h% A6 ?
) [% f, c; b; r, `( [server {
6 Q2 c+ y8 \: E! [8 e4 llisten 80;/ Q( j. {+ T  a2 a  r' q  w7 C. f
server_name 52os.net www.52os.net;
: `4 x; E3 o9 R+ c; N" ^( {  C# G6 o1 e( [8 K* q+ g
location / {
( a6 f- |4 E, q0 O# |: {    ModSecurityEnabled on;  ' }. @3 V) {9 ~$ P
    ModSecurityConfig modsecurity.conf;  & H8 f0 u# {* c+ n; ]- l) [0 C

8 `, n6 p, N5 x" l        proxy_pass http://online;$ I* Z( k# Q7 ~6 \. k
        proxy_redirect         off;
! K8 {6 S8 q+ v: H- R        proxy_set_header Host $host;' ^2 V% u8 l& y& S& Z6 }; K
        proxy_set_header X-Real-IP $remote_addr;& w8 B( _1 X5 L
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;5 l  ?; I' o" {* Y) C) z
    }9 f9 s. A- v+ R, C5 D; s+ b
}1 \  v% U' j6 \. F, q* U9 }
六.测试
* w9 Z6 q/ E7 Q$ _( J, m9 P
2 V- ^( g: n  `% H% H6 a5 o9 U0 i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 m$ t: t2 a! a" ?
+ K! I  X& h+ c( |6 z, \9 x
<?php0 |) [5 w1 {2 N# R. g1 b- `
    phpinfo();   
; [9 k2 c9 g! L7 ^% L" {?>
- }7 l1 F* x" D. Z9 ~) a; J在浏览器中访问:
% ?+ F: e: n- V! }1 M7 a
) m# ]' I4 O& h+ X0 Rhttp://www.52os.net/phpinfo.php?id=1 正常显示。
* X% X# {5 T; V% @: j: X& |  z" Hhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
( P$ J. n6 s8 ?4 T6 J7 |3 F& qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。" Y- N& I5 D7 v8 V
说明sql注入和xss已经被过滤了
# D9 ?! P( s5 r5 u  a5 V& T; Y7 }% Q$ m
七、安装过程中排错  q0 ?4 t4 {3 z, a
% ^! b( {! u9 x; c
1.缺少APXS会报错, z) F& a: R$ E$ g: [+ w# J

/ y( e8 |% w+ y* J% W% Tconfigure: looking for Apache module support via DSO through APXS7 H+ s( J/ s( U* f& t3 M  a
configure: error: couldn't find APXS: s, l5 l9 @8 X" S6 G) M5 U: y
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ F2 U$ m& I7 h解决方法:
5 H0 R: \& X! ~; }& ^+ {+ }9 n& @' F0 z3 m2 r
yum install httpd-devel
- D2 I$ C; @2 z2.没有pcre/ ?% E% n$ ]- @( h1 P# ?  N
. t3 A/ X$ f: ]  g
configure: *** pcre library not found.+ E, ^4 c" J7 H7 o
configure: error: pcre library is required
: I3 Q+ O& E) S; j1 t% q& E  E解决方法:) v+ b1 I) P" x

# A/ j' b1 v; E( m8 W" Z$ x* myum install pcre pcre-devel- y  [; B8 v% f1 |3 T/ g) @
3.没有libxml2
1 |% e$ o6 F4 J: U9 f+ u$ S5 X4 s

5 u, m. _8 l* h6 I' J3 gconfigure: *** xml library not found.2 h5 g) q. [6 v% v' v9 J8 X/ Y
configure: error: libxml2 is required
3 x- G; U: F. r. o解决方法:
6 j% u$ u5 l  {, m& T1 S. z5 o3 j6 `9 k: s1 y0 d& A& b! ~
yum install  libxml2 libxml2-devel- {" E9 o7 H' ?# G
4.执行 /opt/tengine/sbin/nginx -m 时有警告
1 n8 X- v% ~+ Q! v* v
  H- h% v/ |, U* b4 DTengine version: Tengine/2.1.0 (nginx/1.6.2)6 b" }) R- i+ v* P
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 r$ Q  U: I9 m: x9 `  T' R3 c  M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 p3 D# c/ O3 `- b7 t' j& }2 q  o& c  I: L2 J  x8 C: a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ z0 H" S- D6 Q: j' J  _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 h0 e4 k; r+ W( e% E/ P5 |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" c1 M. T+ z- u! h7 _; A9 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"% W; j' z3 n  u5 S! j( f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, i) \1 w- u7 d2 r* X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ a) @1 r9 m- _2 {" n; c
解决方法,移除低版本的APR (1.3.9)
9 p. k4 M2 I6 k/ v$ G# l, l: }, W$ E
yum remove apr
7 Y2 b) {* g2 L& @/ w5.Error.log中有: Audit log: Failed to lock global mutex% G; u0 G8 O+ J) u" p' J

- ?( D, y4 w2 U" ^) F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
$ \/ _  Z7 s8 m7 k# a2 Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 B* l7 }6 }/ D7 C5 h0 A
解决方法:
' h# m/ r) y2 {) w1 R编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
) W8 I- R' p+ z' t/ c
+ P& l3 b3 B& j* MSecAuditLogDirMode 0777
- R+ h4 U1 _( D$ x9 K3 QSecAuditLogFileMode 05504 B9 L, R9 K2 m
SecAuditLogStorageDir /var/log/modsecurity3 O: T( N  f; |. J+ v" e
SecAuditLogType Concurrent
" N. M3 R" V2 n: B/ ?参考文章:
& U+ R8 t+ d" f# fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 n# g6 |5 y" M7 X
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-13 09:49 , Processed in 0.083203 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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