找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12341|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" t( j9 Y' t0 X3 C- S9 @& N
# n5 `: |: a' R6 M! t一.准备工作
1 n" H) w! e7 w; N
7 b: y6 Y3 }7 G3 f9 `4 ~2 {系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; x/ p) E) q2 N# `- C

/ ]& l! J  Q' Y. q; b" stengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 K! }  W. e1 }" G) a- Y, ~

% B' _: ]: P& P! Umodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ ~- A9 A0 M; K. S' y
* L) B  N  K1 g( S- c" N3 e6 J6 m5 {  a! v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ c' C- P/ \/ `
: ^! Y! a9 Z) U依赖关系:
9 m# |0 R$ B' m- a& Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% _# p8 ^# Y1 F& n
- k( y6 ]4 U8 Gyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
( s1 s* x, I# _/ q6 J1 _2 ^. U+ omodsecurty依赖的包:pcre httpd-devel libxml2 apr, [5 d! b( @$ Y

/ b7 c8 Z/ F" Gyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel  u5 N4 d2 W! E3 g: @" i
二.启用standalone模块并编译$ g! [4 \) _' s
& p' R2 ?1 A9 y+ n
下载modsecurity for nginx 解压,进入解压后目录执行:( V4 s0 i& _6 n  |, V5 e4 O

+ Q  J  r3 p4 L) C( J9 n( S./autogen.sh5 Q' Y9 g; b0 ]# V# e
./configure --enable-standalone-module --disable-mlogc
0 j6 G0 `% W$ b- ^6 t9 x0 L# hmake
, b3 g, T% I) F( }三.nginx添加modsecurity模块4 U4 [/ ~- b+ R$ _) E! W& ], w
+ e- x2 \5 N% C1 r) S- {/ B
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( Q3 _( Z/ k% F+ o4 e8 t$ O
7 C5 p! Q3 A/ K./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ a0 I0 V; r. f4 U7 {0 |  ^+ R  I- t
make && make install
% {* b5 X! {8 ~, {四.添加规则
* |4 O# S% J2 S) w- }2 L5 i" A0 i% {( f5 n
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 T3 s+ D9 V/ k
  d/ `. @5 v, o1.下载OWASP规则:
& w1 d* ?& Z- ?: }! K! j, u2 I# O3 i# M
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 t+ n* V7 n& {1 i0 G
3 Y2 e: ]" n9 M* I4 b. C5 U5 ?9 Dmv owasp-modsecurity-crs /opt/tengine/conf/
. Q2 w1 z! |( j: T  {: t
! _* S2 e# q7 v; T" Ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
5 ^. ~1 c0 P# b2.启用OWASP规则:
1 i& t8 M- c  G' O& `, s
3 _3 W+ f1 U( ~) U; ?' E. S复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* U# f4 c+ o) A3 }5 `  @3 f/ M7 z
. I& G. C2 {8 U# W" Y) y编辑modsecurity.conf 文件,将SecRuleEngine设置为 on0 `. w8 p2 ]8 q2 E% k4 F
6 Y; B: C, E+ p$ f' G( R& q
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- x- O- L: n! ?  D4 W" g0 l& v6 M. a- r, @) W. T" ]
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. N5 r/ Q" y( [" P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% O! k; u3 s9 G: z% f# vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) }8 \! ]9 t1 f  E, i6 [, G/ ^# |  K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% S' _. ~0 B. C! W1 M9 W. UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf0 N& A6 C8 u' H* T7 K$ O
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; }8 Z; V9 I0 s; U- gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf, t& a& T% J8 y" J: m2 P
五.配置nginx
* {: C" ~/ g5 n: h* i# ~
, c+ L# I! t7 `, ^8 g在需要启用modsecurity的主机的location下面加入下面两行即可:6 Z, ?) w3 Q' y7 T" O

) k! p( U3 X/ f  Y0 N& F  }ModSecurityEnabled on;  7 v: W2 ^+ |/ ?6 k9 O. @+ f& Q( k$ `
ModSecurityConfig modsecurity.conf;3 q5 e+ }. j% `9 @8 u9 v  S
下面是两个示例配置,php虚拟主机:5 E- c9 ~0 `- z! E3 Y) Y

# h2 U/ q2 w5 G7 E% [server {
# [- F  q1 O% B$ g      listen      80;
4 }9 D# [9 y1 J. w- C      server_name 52os.net www.52os.net;
: G6 ?5 j5 v; a  z- D     7 j# S% O# F5 ~( y$ W
      location ~ \.php$ {  c; h2 u+ U; ~7 t8 u/ m& V
      ModSecurityEnabled on;    l$ b3 i: B4 P" r' A
      ModSecurityConfig modsecurity.conf;
  q% W7 u1 l5 |* {6 z
$ t  f' t3 l" x      root /web/wordpress;5 D  D4 d% a! p
      index index.php index.html index.htm;
! V# H0 S6 u2 E7 S% Y& h  
( m  Z( ]# _9 \- `: F& {& K3 d      fastcgi_pass   127.0.0.1:9000;# M+ P/ w5 P  C- s% ^* C" d
      fastcgi_index  index.php;1 h' j- p) G3 B) k3 W
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
% O7 h% l/ o+ Z: D      include        fastcgi_params;
7 H6 g0 G  E; z3 P# l' b      }/ Y9 e( a; V$ X1 V
  }
: S8 D- M$ [2 K  p7 A5 p. I, @upstream负载均衡:. K: [, K" w! T

/ R3 L7 P/ P' X  supstream 52os.net {
8 [# v6 H) W* d1 q  _+ g" d. M2 t    server 192.168.1.100:8080;- H' w* X% \. Q; U/ {2 u: k5 `# ~
    server 192.168.1.101:8080 backup;
2 B5 E  ?0 D, c9 _/ _}/ C! D0 k8 Q3 o- u

) E8 d+ L0 y, s9 w5 ^# wserver {
- |* v! I+ o* F, rlisten 80;7 i: \; U/ D# P# S1 w2 m% z
server_name 52os.net www.52os.net;
/ D2 s8 e. O5 c" P( @6 s3 z, e- e3 O! s6 e5 l
location / {
  r) n9 m+ g& D    ModSecurityEnabled on;  
) h* x3 ?) A, U    ModSecurityConfig modsecurity.conf;  1 b: G! h9 ]( j7 g* N3 |
2 \% e) E' C0 H9 I- f0 Z$ [
        proxy_pass http://online;; [3 Y) f' h: p* D4 y) w
        proxy_redirect         off;. ]( h5 n" C9 U, ]. b6 V8 m" A
        proxy_set_header Host $host;
8 [8 s& V1 D* {! W- X' [/ D: ~& s        proxy_set_header X-Real-IP $remote_addr;$ Y1 J0 t9 M. o2 \  o( Q
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( j  R. m- ]! |- p1 l! o) v    }2 ]3 j0 {3 u# V0 I- l
}. N/ L  m# p- v9 I9 {
六.测试/ n) v0 a" Y) f
- [  ~& G/ F' k3 d
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
; R1 [, H- l% Y% S  n/ s$ G
0 h6 A$ ~1 {1 K/ ?6 S1 o  V<?php4 ~" a; s; [; f" f) @+ G# z# n
    phpinfo();    * S) m- O2 G2 {, p( o
?>
2 K4 {% Q$ W9 ~7 H在浏览器中访问:' w/ [  T9 [: T8 h4 ^+ r0 y

' K6 ~% i4 H* N6 ]http://www.52os.net/phpinfo.php?id=1 正常显示。
" W7 A7 B: s; H  Khttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。& d- G) }% z# ?6 z+ W' q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 L0 Q6 [% ?  [; q
说明sql注入和xss已经被过滤了
. J- A8 ?/ v. l: X$ o: }
$ p4 `9 I5 Z; n* b$ R! R( G七、安装过程中排错- h& Y* B* r- o% p0 E
0 F7 \6 G; _& z; I$ _
1.缺少APXS会报错" ^$ p1 W. p( U

8 F' o0 Y) d$ K; B; I1 N- a' t, xconfigure: looking for Apache module support via DSO through APXS& v2 |2 q0 {- `8 F' k! ?9 ~
configure: error: couldn't find APXS( _- L7 C+ I+ T! W  f! t4 d; M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! h. R1 k/ |2 U2 F* @7 D解决方法:
5 L& G0 z- z0 s, r1 F4 Z0 s  c
0 E- S/ b6 D8 @5 k8 Eyum install httpd-devel0 l% j7 k/ g0 g7 {
2.没有pcre# G; I+ c2 W& P) }

. i9 _  Z$ F* f  ~3 bconfigure: *** pcre library not found.
$ K& Q7 \+ g. Fconfigure: error: pcre library is required7 e9 [6 `) S; G% q' E% e* j: l6 |
解决方法:* A: n9 S! _$ `; v
, z: k) [8 v# x  S- U
yum install pcre pcre-devel
+ U# J# x" ?0 u. d" Y. Q  s6 L0 F3 \3.没有libxml2
) f% n: C& E0 `, a
, h$ p' ~$ z  w' k% q5 |7 H0 |4 @& A2 G/ H4 N
configure: *** xml library not found.4 Z3 A4 E0 P& W
configure: error: libxml2 is required; l/ g# ]' L$ N! ?
解决方法:4 ~9 N% A' D; O+ H, _9 I

9 ~/ {+ Z. Y% y, J5 l* E0 N5 Dyum install  libxml2 libxml2-devel
# {0 ~/ U6 F; M0 o( I4.执行 /opt/tengine/sbin/nginx -m 时有警告' E* K- y4 @9 t4 `7 @
% ^  Q8 i. J4 q! M; z+ R1 M
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
4 ?, _) ?' g5 h2 @  enginx: [warn] ModSecurity: Loaded APR do not match with compiled!! C8 y5 ?. W* ^; C4 j  s" |
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( N; J6 M4 O. V0 S

; u8 F5 U9 }/ N. [$ ?, A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
- h: U  S) p( [9 \# z: Q( c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"" g3 r1 ~. B2 U& _. z7 @9 ?, O& v9 U
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# v& [! T5 E% i% x* E& F: X6 w4 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 T5 |+ `% b( m; D. k3 e& z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 @8 x* Q2 l' G. }  |
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' P1 t4 f" Q4 b+ v; ]解决方法,移除低版本的APR (1.3.9)
- M+ o, g! Y6 M- k
" o. c* Q7 W6 S  d( k# hyum remove apr& i7 |/ C4 a' L; |5 J0 e/ `
5.Error.log中有: Audit log: Failed to lock global mutex
" i# m  X. `( @4 e, Q. O: c# k5 d4 w8 ]1 I% R5 _' {8 f$ _
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     % r. X4 }6 M# l3 Z% q& d
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
" z! l8 j4 J5 n! d解决方法:  @& b! z% F& G$ m/ C9 w1 v4 r2 r
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" ~8 ?2 b7 k+ T. Y2 k" D5 J* h! h+ R* i; j" t2 W
SecAuditLogDirMode 0777+ Z! h* `/ N" j4 O
SecAuditLogFileMode 0550- g$ ~' n4 |3 B0 W
SecAuditLogStorageDir /var/log/modsecurity
, V) q! ?1 ?+ m/ U$ tSecAuditLogType Concurrent
" O: Z6 H, c, i# C$ s% ~参考文章:6 m5 I, g! U0 x/ C+ q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; f$ F' a/ {' {: [4 z: O9 \4 qhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-28 23:40 , Processed in 0.079708 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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