找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12623|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# j8 r+ v( @+ b) j$ r3 _4 ^
, e7 h. T% m1 `& s( O4 d0 S
一.准备工作
$ g3 e, J$ V' r# L& Z
' `7 l" O7 b' f9 y3 F- k系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.07 v' D# S, a. ]( U
6 O5 Q1 w& e6 E& c( r) q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- H) _% R* `0 ^4 x- Y* o6 ~* g, c6 \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. W. O& ?) t( m+ _8 U& K, q4 D
' e. X. _& U& S% ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 {1 n/ `1 |' r6 a& E
7 t: b1 c. }/ A' t3 s依赖关系:
* ?1 `: w$ Q3 Y9 E4 i- _tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, {# }. J9 k7 `
# [+ g" i8 a2 f7 G! j
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 Q; K& b5 o) Q6 _5 b9 M# n
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 M+ j0 j4 o: u
' e$ ~! b7 a+ k: O, C  C% g1 fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel& x1 D4 ]" `( I+ c
二.启用standalone模块并编译
7 U  p) h4 @4 S0 w, s7 Z! i
% O1 r: H; u4 v& L: y' M& p下载modsecurity for nginx 解压,进入解压后目录执行:
% f/ [2 U+ n) K- U9 X3 _8 J7 \9 S% ]0 l* m* h
./autogen.sh
' d& ?% E& D$ P! W9 o( b! L./configure --enable-standalone-module --disable-mlogc6 a6 ~; |. |' I7 \0 [; c7 E
make
5 @/ `& p& C0 v/ D' Q( Q/ D三.nginx添加modsecurity模块8 Y: \# K/ T& u  r$ p4 t, H

8 Z) |- b" I, A4 p' J8 k1 I在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) o2 z. l& G# r' t. }
# r* g7 Y8 x% F8 I6 T2 K
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
  v! v8 {; ?. y4 D4 Fmake && make install0 r) B; F2 b" ^1 E6 u
四.添加规则  r3 S9 R$ q. s5 i0 i
; W* C  l/ J( t4 }: Q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! F* X4 J" X9 T$ ~( N" a/ V

+ Z6 u. c1 v# a. @, c. h7 |1.下载OWASP规则:1 T+ n3 u$ P4 ~3 c4 K
2 e2 _3 U5 Y  b: r" Y; Y8 K2 c! w1 H
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs* }& d5 a, p. w
: w; p& O  @% v' W
mv owasp-modsecurity-crs /opt/tengine/conf/6 V4 m* O" q. k: n" H- B( Z* l

% r, y' d, V# Z7 t+ \: dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; b! v: F! Z* i- y4 I  Q0 T  C5 P2.启用OWASP规则:
+ p- F+ W8 c3 v  t4 C
. a# f  z6 A  d4 W* P; q3 F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 a8 O0 R# m1 ~: S
8 F: r: p& ]- `& t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' a" j- h8 w( H: ?- g4 u: l$ O8 e1 L$ @
6 q  U& ~) b/ b0 `4 ]
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
8 a6 f! e) T# `# W/ |3 |" C4 O& j. P
* ?$ Q  d" ?: ^; rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 B# t, F: M/ R# E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' N5 T" T* Y. w9 NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ T# Z$ C5 t0 NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' v& f& p3 W0 V, a1 f
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 T% E, h% _2 \* [/ b- w, |! lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" G$ _, t9 M5 |5 [/ vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 ]( @* r: X' D/ {- K3 e$ w+ F! X
五.配置nginx
5 F' K  G1 ~0 ^, m( `1 v5 ]; i. b+ x* b5 H' A
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ }8 h7 l8 b+ E$ x7 ?% a1 u, L+ H/ z/ A* F" Z5 W( s7 c/ a0 N
ModSecurityEnabled on;  
; D/ h; T& I0 kModSecurityConfig modsecurity.conf;+ q/ ?, l- V" ]8 V# V6 R/ h* @
下面是两个示例配置,php虚拟主机:4 y! t) x4 Q! W
$ a6 _* J1 r; `0 Q6 @
server {; X+ k' E, G8 S4 g# M! F
      listen      80;& W: Y6 J* A& Q" H
      server_name 52os.net www.52os.net;
" h& B' m. T7 ~5 y     - u+ B" @0 q' X1 D) B" O+ A# h
      location ~ \.php$ {# @9 g% K7 x9 u$ k2 Z. M$ b
      ModSecurityEnabled on;  ) ^$ d( X! m$ Y6 @
      ModSecurityConfig modsecurity.conf;" C' ], [- }! |8 c! o# b

( e/ b" z+ J: P5 a) S, Z" p- k      root /web/wordpress;
; t; P; i# R' H. f( B0 Y      index index.php index.html index.htm;$ K/ c9 h, q! z- Y* T4 G0 _6 c
  
  U8 _5 J% q6 e9 [      fastcgi_pass   127.0.0.1:9000;
9 u& P( C4 O. v' J# S# u) y      fastcgi_index  index.php;
; c4 ~& f3 o% M/ N      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;, C" k* b4 m! m! H' m# j. F4 D
      include        fastcgi_params;/ a- ]) ]1 K* ^5 r
      }- U7 J* ?' S: k! F; t3 l% d' w# X+ `
  }
) K" D0 E( I, c% L1 D5 \upstream负载均衡:8 |1 b* X/ w0 A; b) T7 O! a

2 j6 p. n" b2 y! o3 V) w0 W% h9 g+ wupstream 52os.net {
4 w, C; `. e' |8 c6 h& A2 v    server 192.168.1.100:8080;
* D& L- o; G1 z& V' Y! B    server 192.168.1.101:8080 backup;3 H; o% A+ y6 z. `" m8 ]# G, S
}; U- A, ]# B& K- F

" x. F4 f4 v8 E6 B- ?server {3 V7 w4 i# [6 a, ~
listen 80;
8 V3 q( s/ I0 k+ Q, @server_name 52os.net www.52os.net;9 j% l3 \5 C& ?; F5 a( W9 ]
$ B4 @4 z3 \! g; i: b- N
location / {( t3 B" J' Y4 f! q/ @
    ModSecurityEnabled on;  ; F8 K# a0 ~# g
    ModSecurityConfig modsecurity.conf;  + \' Y5 K0 P* r7 X
: o4 M3 ~6 Y. J( i' x- M6 e: u
        proxy_pass http://online;
" S' ]- V7 ~! z: y  I        proxy_redirect         off;
6 y, q8 I1 ~, N; N6 m        proxy_set_header Host $host;( j( J- O7 O# ?
        proxy_set_header X-Real-IP $remote_addr;" H: E, {9 [/ U
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;; M* Q, ?) w" ~6 H* E8 d$ H
    }1 x3 L% e3 b  g; j" k# H
}
& b$ D, [: ^6 E3 w  M: Q+ X六.测试& ^3 x9 Z1 D7 H$ V+ r4 R
' X2 ^% ~* C- A5 i, L6 w
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' V, Q+ s2 H# [$ C! q* h5 F2 g4 m
7 ]8 E0 q) G* i7 i<?php- J& @, h9 F% N5 ^& H2 x! s% y1 @$ J
    phpinfo();    * L' {$ u- G8 u0 ^
?>% F' a9 u9 J5 ^3 h- i) r
在浏览器中访问:* i/ M* k$ `+ _0 r* X( j  K% S

, P; d( g3 g0 Z0 ~( ghttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 s! W% ^* Y0 \+ s1 b% N5 b" D$ xhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 ]0 J, U- n! L* [" E, m( N
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。) E( ~& V( ^  V5 l
说明sql注入和xss已经被过滤了5 u9 H; T% ?8 ]$ V

# X0 m* S" _* _; X/ s七、安装过程中排错
0 C% i0 a2 f* v3 p2 {
9 ~; Q+ g" r, U1 P  q0 N1.缺少APXS会报错
! A4 c# @5 g4 D9 }% J
3 H& ^) B) K. E1 F# C, [4 v1 pconfigure: looking for Apache module support via DSO through APXS* Z$ E) A, ~+ u1 C( g
configure: error: couldn't find APXS
' |& n% C4 b) y3 M' P$ @: uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; x% |- v& X/ U, D# i6 k( C
解决方法:" u' G" q  U" m: e5 m& P- G
+ D; p' w9 l5 W' c+ f+ P+ X
yum install httpd-devel
+ {% K0 T) j1 x- ^2.没有pcre5 `- ]. c! c7 [; F1 I7 G. _

, }/ n  m9 o+ ~  l( v% L0 O  `configure: *** pcre library not found.8 H: e& b6 J/ H* i
configure: error: pcre library is required2 C6 ~/ b- b2 R1 J) c9 o# `) u
解决方法:: K3 u1 k* |, C8 U3 K
# t0 T  X/ \' J% }8 U% x& l
yum install pcre pcre-devel
  f$ H4 P4 @( U3.没有libxml22 r7 M. H2 I5 G7 L" m5 s

0 Q- L5 N1 e6 \- B; D- P0 G$ _3 n) H' V
configure: *** xml library not found." K- |" T4 x. O' ~
configure: error: libxml2 is required
  |7 B* R# `: Z. }: K% n0 W解决方法:2 v8 e0 V" Q; O. S; d
0 v2 i7 T8 p( q3 Y; D3 U
yum install  libxml2 libxml2-devel
: P" d# U% B( d* }9 n  R/ f4.执行 /opt/tengine/sbin/nginx -m 时有警告# W, ?8 ^/ B9 w! g0 L

  p6 M) @: a9 R! BTengine version: Tengine/2.1.0 (nginx/1.6.2)
2 [) L- _* l3 I4 Wnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ i% ?* ?# Z. _, Z6 s& X* [原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 D, E: f1 Q/ B
' z/ A& q3 u6 a+ g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' S& L- K% M% z' j. i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"4 D0 }' q9 E; O9 _% f' `
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
# r- n$ O: N% A  V( M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; _& c0 i% E5 M+ ?) m) c! p5 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
1 b! W5 A7 y! A6 F  O" ^2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 Y- T( S* ^: N解决方法,移除低版本的APR (1.3.9)
! h7 P6 ~; {- m8 w& v7 i
8 J; Z0 J( c/ K+ y. U9 _yum remove apr
  h+ ?$ ^- M& X0 l8 r! t# X% u" x7 ~5.Error.log中有: Audit log: Failed to lock global mutex  r) Z& I- ^. V. _* J0 h" E
9 r8 q. A. O5 n5 [% f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     & D  q4 Q) ^0 H* r
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& o6 J! n* J9 w/ n/ |3 a6 E
解决方法:
' c' u( [( D! t! U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 l1 ~% D0 J& G7 d3 k( V5 ]7 B2 W0 w! U; B0 w$ B5 U# v
SecAuditLogDirMode 0777
0 E8 E) |  r  b( E, hSecAuditLogFileMode 0550
3 x4 ^5 c0 I+ zSecAuditLogStorageDir /var/log/modsecurity
# O5 r: b/ |3 ASecAuditLogType Concurrent
) ~; v: ~: Z' R/ [) p参考文章:- C- D% z" Z8 c' }7 L. H
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) N8 K( ^& z5 E: e, n* s6 z
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-23 18:12 , Processed in 0.079545 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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