找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13005|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! q3 m0 _+ I7 m! A) d+ \
3 F* G, {+ {  H7 Z
一.准备工作
6 j" x5 ]- D) l+ g. V2 _& K- I  P. G
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) L4 x: h3 K# T# N% K6 U9 s

+ O: e+ N8 ?: j8 Wtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
( T% e' }' `8 H% O3 a0 _& H) I, d' L- J' Y/ l
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz  U& K  }! {2 s) t0 A: Z& V3 g5 T! E
/ v* L0 ^# M4 e  G* c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. w; c* I  }; w  f7 \
0 T! N0 @( ]' [% R
依赖关系:( W/ s5 ~( a& ]. n- J& s4 m; I, t" N
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. Q( E9 h  m! ?- [

; m1 K; C8 K, Y( b) S3 I, M4 }( Yyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
" d& ~  M5 }6 H2 Jmodsecurty依赖的包:pcre httpd-devel libxml2 apr% l4 c+ K/ P6 b) s" q
% [% f/ S4 U+ v/ t) J
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel4 X1 T/ V- J9 Y& ^
二.启用standalone模块并编译2 J% P: t9 a- t9 E7 E2 v7 ^
  l& Y. F  N# i+ s* H
下载modsecurity for nginx 解压,进入解压后目录执行:) d2 b$ }2 f  l

9 p  y# z7 ]4 C# V0 `& K* P6 A./autogen.sh
1 L' w! S  G7 T; j+ W./configure --enable-standalone-module --disable-mlogc2 V% J- C$ B4 ]0 G2 O) o
make
1 w. J4 X2 N' V- |三.nginx添加modsecurity模块3 ]0 Y; M! U! j- `- K2 k/ t6 r

8 q* H- g: ~9 j6 K* D在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
' |2 Y) e8 s, i$ \
3 H1 ~; F. }2 R8 q* G, l3 I./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
! B. ?8 I; }0 q& X  o$ m  G) d, Nmake && make install
' ^: t8 f. Q+ r& u& D2 o+ H四.添加规则
% G# ^/ Y4 E" {" y: P7 A* _7 N% a; `
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" ~7 j# b$ w- t3 \" R# Z, x+ q7 v" [% L3 ~, p
1.下载OWASP规则:
+ h, Z- d" E4 J7 B0 J/ `4 A
2 q. u! A3 L5 s$ J/ ?9 l* T0 A. Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
+ m+ H  o" u: ~. |4 g
) V8 h( Z! a& m1 t( ?( omv owasp-modsecurity-crs /opt/tengine/conf/
- E3 Q. R( h- }' c$ E$ N2 {2 c. [( a* y3 g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 ~5 O0 m+ J6 h, j% R$ i2.启用OWASP规则:1 |& X! N3 [$ G% \' _& }+ Y
( b* [' c9 U; c' \4 G! C5 U/ u! o
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
$ J, b) E1 Y% y+ K3 c% K8 J! I- _" q3 r
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 h8 c6 n/ b. j4 e% C/ B* Q
9 l! f$ Y  |# }" n* B4 qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: ]5 D' J! J, w" Q# ]. M) S
5 @& `3 v7 s* q1 @" D+ v' rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" ?( N6 e, \  F3 V' E- e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf6 T* G) e) g9 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 @. Z: ~+ {0 K) s& l, x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 U+ a& |+ f) T9 pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 z: h: z- w+ uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf  G. ?0 m& |' ~8 v5 S; h
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" N% ~) `+ I6 m! ]
五.配置nginx7 a3 Z9 Z5 i( _3 _9 Y  y: r
& S' T2 D/ ]. O% ]* G
在需要启用modsecurity的主机的location下面加入下面两行即可:
4 j. G9 [0 k3 W7 c9 s, X- f( P2 M
5 W2 j: d3 l! r- Z$ N+ ]% a1 R3 j- HModSecurityEnabled on;  + C4 l$ ~+ O% k6 u6 b
ModSecurityConfig modsecurity.conf;6 e6 b: K3 _; c- M
下面是两个示例配置,php虚拟主机:
8 C7 a, C( e, Q, M/ [+ M7 x
2 p5 c  J% v5 L( X- D9 ^( y+ n0 @server {1 I( X( B0 N' z" m7 ]2 u
      listen      80;
, H% `  O( }% T- U% B4 ?/ X) I      server_name 52os.net www.52os.net;- e# ?% {  Z+ |# e( M/ E6 d
     
  H$ u1 U* g2 Q* {      location ~ \.php$ {
* k2 d. H3 q4 @7 j9 [" ^      ModSecurityEnabled on;  & M! h  ~: u% u
      ModSecurityConfig modsecurity.conf;
" ?7 H% }2 e$ e
  v/ c- ]( S! C; Q, X. Y! ^      root /web/wordpress;
5 i2 I4 N  w) p2 U; C, i% B, P6 A      index index.php index.html index.htm;
4 a5 R+ y7 Q' B) l, w  9 J! y8 P+ T* P; n; l9 a
      fastcgi_pass   127.0.0.1:9000;
( i9 Z3 F# V0 f" ~- J; ~$ g      fastcgi_index  index.php;
( Q- h; B" d6 k4 ]$ h4 t2 e! I      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ d# }' p" T$ L9 `4 \
      include        fastcgi_params;
( z' Y. A1 {) N; L6 L3 }/ B- E      }
. Q/ g6 r2 g: P/ p3 R( U/ D7 A  }0 g4 G, d- n) R
upstream负载均衡:- l2 {; e$ t" i! j

, R0 |% A( }" pupstream 52os.net {
) R% U6 U+ a: G" V/ \: K# D    server 192.168.1.100:8080;0 d0 K; W  J& i
    server 192.168.1.101:8080 backup;  G& n' B% M/ Y
}
% p0 a% I3 \1 j5 j( R) J
. f7 g% I8 w0 Q6 F2 P8 Dserver {
+ J# J8 \) d) v6 W- t: o! u, Olisten 80;" Z1 S: f' d$ S5 b) Y9 Y
server_name 52os.net www.52os.net;0 _7 N  A% z' r; w

% a) O' `9 ~& }! Z9 Ylocation / {
2 l7 C+ {) u2 Q9 q    ModSecurityEnabled on;  
& h( K; v3 L4 G0 I% `) s2 \    ModSecurityConfig modsecurity.conf;  - O% p4 h+ o3 ^% z! h* z
7 J4 ?) ?( h. c
        proxy_pass http://online;
6 O1 U# l# t7 Q/ l& C9 ]% d        proxy_redirect         off;4 [6 W% L# y- M
        proxy_set_header Host $host;) v8 Z; ]' B3 ^
        proxy_set_header X-Real-IP $remote_addr;5 p# }$ T3 s7 ~& t' S
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
- I. z3 F1 `+ Z; x; A4 P: j; e' ^    }( e; q) f$ F4 }
}
1 U2 q2 ?, P5 `" O$ Y) \六.测试- _( W: a; _; D0 N
( u# ]% ^+ x3 z% Z' h
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) T( c7 D3 ~9 O. j! a7 I8 b4 e2 G3 }
" T. `0 G% ?) n( S) q7 _3 F<?php" m9 m& M- B  T& v. j3 f
    phpinfo();   
: c) m7 y# z- A! h5 Y?>
$ S8 H% k) a  F& _8 ]9 k$ s在浏览器中访问:
* w  u# v( I: |3 K  l. ~
" C" E/ J$ y) G& ~http://www.52os.net/phpinfo.php?id=1 正常显示。. a6 {- \: I9 ?" m& ?
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。6 R" v7 P6 d" w8 \3 U( e5 a
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。6 A. S( O( @# v+ L  i! B
说明sql注入和xss已经被过滤了# ^" @% h, S2 e

+ B" n. b0 V. O# [) ~. H; [七、安装过程中排错
) P" E- b8 q8 R* F% V
2 K* |% N) x2 j7 j. Z# @1 A' {& J1.缺少APXS会报错
6 o5 L6 r) j4 |, A. J4 P4 B0 \' O- x2 N5 U0 K* l$ f: x( }$ [
configure: looking for Apache module support via DSO through APXS. B( @  |4 _% U: l9 Y, D" O7 T
configure: error: couldn't find APXS5 L) F: ~7 j9 ~7 q
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) Y  l4 I4 {+ H6 r9 l解决方法:
3 I% I! k2 _3 @  p4 ~% J5 u% K! U, }
yum install httpd-devel
* C: H; }* Q+ \3 X9 Z( S: m2.没有pcre
( Y! m; W2 f: Y9 k. b2 P  H7 t( F7 g+ U% R" z. ^
configure: *** pcre library not found.
9 x5 c- K& r7 iconfigure: error: pcre library is required
5 B# C" H! A6 Q! e解决方法:4 R7 [# E" a& z7 o5 Q; p2 R9 N3 O2 F

. R7 D5 D; ~1 L  W+ C% ~) n0 K8 Yyum install pcre pcre-devel
- ~1 v) @" {/ k+ _' F7 x2 E* _9 b3.没有libxml21 ~4 s7 d$ ]% ?
1 Y, n# J9 @9 R. t" ?

8 E0 I, ~2 ~8 Rconfigure: *** xml library not found.
% c0 ^! D' i' \# }% A- rconfigure: error: libxml2 is required) D4 o7 l9 S5 c  R6 p" w
解决方法:
. s2 Y+ L; ?! I9 b6 ?1 S8 t+ w2 x, H8 D, e$ x1 j& E1 P
yum install  libxml2 libxml2-devel
. w. L3 _- x* X5 M+ J. H1 H4.执行 /opt/tengine/sbin/nginx -m 时有警告) r& |; K$ G" Z# k* K

: A2 e( Y0 ?6 Z- z0 ZTengine version: Tengine/2.1.0 (nginx/1.6.2)
) _0 ~! }$ f! V" dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 ]0 M, w% N0 Z* R8 i
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
2 c$ |3 ~! F, e: G& h& C9 R( |( U+ [* W# [* }% `, D" c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% q: N' v% F) D, L7 [) w' @3 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"* a. M% A; d9 L& M
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! k6 j. z+ @8 {2 M! N8 {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# x: ~( e8 y4 n' ?0 K8 O" t/ \  m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"- o+ m; e, e% k7 ~* r( y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
& A' @- t6 T! X9 X( J解决方法,移除低版本的APR (1.3.9)
' {5 d0 X/ g# H- @2 l6 F& K( h  a0 ~5 B. D* k
yum remove apr
# |7 x: `' Z: p! ]9 z4 p5.Error.log中有: Audit log: Failed to lock global mutex
) c7 b& f9 y! K; \% ?
. j$ D# E6 S2 I. O, `% ?1 C) {2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
8 {1 R& \4 J/ u# Q) Rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ N3 M% @0 ?- z( K, v( t解决方法:
! V) V1 r; i5 u% y( O1 p编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" u' Y0 k* I' o
1 a' ]. T9 g6 \$ o8 t! X- ]' b
SecAuditLogDirMode 0777! I5 I8 b" m1 g0 S2 f! }
SecAuditLogFileMode 0550
; P  d( r8 ^* i. ^+ A" TSecAuditLogStorageDir /var/log/modsecurity
, B. S' P$ J  ^/ @% TSecAuditLogType Concurrent5 s. x" X) B9 u3 J% V
参考文章:9 L! n. Q0 G+ y: d  T
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX6 g* D1 Y: r! A) ^
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-3 00:20 , Processed in 0.066402 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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