找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11838|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 ], J/ Y' ~: N/ \  P8 _3 x4 p7 f
一.准备工作/ b9 p0 m  B: m6 A4 `

" |$ ^5 ~* Q% K6 @' \" K2 l: g系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 d8 A/ ?. s( l2 B2 d( r" I5 a/ }2 |; @. o) u
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 X- G) Y$ c9 V! b( N! ^: b2 K) b, x3 e9 G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 H$ a1 [3 T0 v. L% Y: m2 _8 H" {& u$ M
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 P9 `" w% J  ^) I8 A, p1 G' t1 y8 b1 \( U) }$ X
依赖关系:2 c0 ]+ D& p$ Y$ b+ g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 k  J& N1 }8 K+ Y& T1 ]7 h+ c  {, x1 k+ L* Z
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' E, U% V5 B3 q. H8 z; rmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( `' @7 m4 J# {+ Y# j
0 p2 d: i1 D9 \! D6 b1 U# r! e! i! Byum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 [) ?& }+ c' Q- p$ i" T
二.启用standalone模块并编译5 ]1 }# f  Q/ X  J( G* _
& x0 ^: \- f6 l$ P; d& R
下载modsecurity for nginx 解压,进入解压后目录执行:
7 k& ?0 h: c; {" O1 u3 ~1 G* R# y4 Y8 g: d6 J* b0 g
./autogen.sh
2 W! U- e" h# P/ o./configure --enable-standalone-module --disable-mlogc
3 U& w, }! a5 z6 q' imake ; w: v4 e, w( d" f( s/ `
三.nginx添加modsecurity模块
) b* A; ]. E, \7 s: P: r( f7 K! J7 ?8 ~, ?/ `7 ]% N
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
1 n' v4 O: D: d$ U  z$ s8 X3 [' C
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
5 ~' \+ V# x& {& E  [6 R- \/ Kmake && make install4 S: r9 d$ ^5 ?
四.添加规则/ X1 z; @0 D" @; j
! y# u0 Q# H- h% |7 ^
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: L/ P  h' F/ ^+ ?: z8 D/ `
+ [- L6 `3 m( D4 h; s# @2 z& W1.下载OWASP规则:1 n: o4 ?9 W" m

3 g+ h; G5 }/ `9 \4 m# Ogit clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 u8 f5 _$ E/ o+ Q2 c; H" O

& w# V  h, c6 g1 v' P! p* dmv owasp-modsecurity-crs /opt/tengine/conf/- R& h6 p. L4 \# j# u
- G. [. |! L* H) Y/ U3 x( l
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. w9 s- s) D+ a- }' X, h# ]5 f) N
2.启用OWASP规则:& @1 x, f8 e; m' c% N3 @/ r
( g; ~0 \( C" y  _9 k) E/ L
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' a4 U0 q6 ]# ]# p. t6 E
% H) S9 V( V$ f5 Y8 \编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 W- t8 d! a2 a7 x( z
/ i# W4 q7 n- F- j1 _4 b
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! I+ C8 t2 k4 Z7 Q. O
* R5 A& R/ x; C* \
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
; e$ b3 ?4 x) p+ V% h' ]5 @, CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! n. |8 ?9 |. w7 e* F2 u0 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( Q+ n6 `0 |  m. q0 d2 i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf9 R  f  R+ ]  z4 Z: z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ Q3 y  f+ Q8 R8 s) M9 N+ ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
2 x* ~% u& \- `8 I2 nInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% [) m3 `  M& w$ u; X$ ?
五.配置nginx
# T5 o0 \3 _% g) k% L, r) Y
4 i( C- x$ K% b, T在需要启用modsecurity的主机的location下面加入下面两行即可:
4 R/ e! h1 c# F& v% I- {- ^$ J. a: O
- i  B  s/ a5 ^0 [7 H! \) P  Y* @ModSecurityEnabled on;  ' a# r' Z4 q# o  c  m) M% b* L
ModSecurityConfig modsecurity.conf;1 X4 X- j( W) ]/ E; v! H# p) X/ ~
下面是两个示例配置,php虚拟主机:5 E+ S1 ?7 w. F; E6 ^) y4 _) s

, u- ^+ {: _( F. z' Dserver {
8 K. I# R/ ~5 N' w# C& h      listen      80;( J1 @2 B4 q* ?% [$ \
      server_name 52os.net www.52os.net;
; E! X6 W) K6 i  [     
. g0 E, `* s& X$ r, f      location ~ \.php$ {
! w) \% K4 A; v* p, v+ g  X8 ^      ModSecurityEnabled on;  ; L  k% \+ W2 K/ |% K
      ModSecurityConfig modsecurity.conf;
: x3 x; k4 M) ^$ n
. k0 C4 i5 |- `! X4 R) S      root /web/wordpress;0 r) k* R) D3 F6 t8 ~6 J+ k# }2 q
      index index.php index.html index.htm;
. z5 i; g$ M, Z4 R$ u. C8 n) s  8 |- Y# [" g) @! }' L' q
      fastcgi_pass   127.0.0.1:9000;+ A7 ~$ R7 i% V3 Y/ F$ }& q
      fastcgi_index  index.php;
. d6 u9 n  q' X6 R      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;  l' `7 w/ o2 c* ]3 p7 J
      include        fastcgi_params;# i* _& {& Y$ G0 M8 D) C( A9 K
      }- }7 n  M2 O8 t* A8 x
  }( d: h# }( @! ]$ v
upstream负载均衡:
% v2 g' V( g6 m% }+ @+ p) U0 w8 e  }+ Q- D6 p/ N1 \' j- W" K3 J# _' `
upstream 52os.net {
5 ~: I" B$ ?# [* |    server 192.168.1.100:8080;; p1 ?+ a' E+ H( Q
    server 192.168.1.101:8080 backup;
) q/ N, H& L3 I5 d( ^3 ^. O* s4 ?}) Q: l2 T8 W7 S) d& o$ ^# v! o
" g7 b4 D( E% y0 t0 T) X
server {
0 [5 i# y: {$ u5 A" C4 A2 klisten 80;
% d+ c$ Z6 _( w; e- ]- Mserver_name 52os.net www.52os.net;
5 ?' j0 n) t3 E* T5 d+ B: S( r7 K; G0 l/ ^/ _3 R" H
location / {
: }5 |+ F6 q$ t2 a9 |    ModSecurityEnabled on;  
$ N) i7 M( Y- p: i    ModSecurityConfig modsecurity.conf;  3 u! f/ L# d  H% c$ B+ f

6 ~" x& s4 k# a4 [/ D        proxy_pass http://online;
$ \0 U) J9 r, v        proxy_redirect         off;' X: Z& G( [! u+ R7 P
        proxy_set_header Host $host;1 _( x+ c$ O" r+ p8 s
        proxy_set_header X-Real-IP $remote_addr;
- I& b  n5 W5 o0 |% n7 Z        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;7 v; Z! g- r! ?: W- ^; l
    }
, H$ q  ?7 k$ s}
) [5 M& Y  W2 h( n1 h六.测试9 W/ a7 L0 X, z8 S/ g$ N

* G; x8 p+ Z3 v) z3 {- u  l我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# m, F$ x4 r( X
! V4 w) M5 b9 d  ~0 b/ z, w. \
<?php
; C+ o3 t) `) c5 y    phpinfo();   
, `# w" ~$ K6 n( [2 A1 }. H?>
* R# Z. B5 a/ x" D5 a) D在浏览器中访问:
$ U2 r7 U+ O1 m  {
, h' w8 y1 }- N  w5 Z3 q: }1 f8 bhttp://www.52os.net/phpinfo.php?id=1 正常显示。6 B( K2 O0 X6 C2 v0 r
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。- l: e) J, c" p$ }
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。) i+ f3 V5 ^1 N8 \* g! ?; k
说明sql注入和xss已经被过滤了
$ H# F% k4 b( v3 o
$ v3 s$ d8 h3 J2 c4 h) D七、安装过程中排错
2 I; x- o# F3 ?( d9 x. v; n- _2 ^' c7 n7 h0 S
1.缺少APXS会报错$ c8 ]9 Z& H2 T8 L* j5 m

5 ^1 Q+ I( |  W9 _# nconfigure: looking for Apache module support via DSO through APXS1 _, x- @' p) x8 f- J
configure: error: couldn't find APXS$ J/ f, H+ j3 {- m$ i
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% p4 v( E! s, R" e! b解决方法:5 q* s1 M9 \' i0 a1 V- z* m

6 v+ v( a4 ?% d& Vyum install httpd-devel1 p1 R9 ^1 k5 o+ N7 ^) C6 n
2.没有pcre  K( ^& V% d3 o0 Y* b* S* K. t6 L

0 r* {1 m1 |' p9 a8 X( C4 o/ Mconfigure: *** pcre library not found.
, C: C) }4 K4 T: c3 |9 Mconfigure: error: pcre library is required
+ Y, W, ^$ b; `( \; i解决方法:- i& F* }/ j+ F& q
+ M8 [& w( }" q- X: w
yum install pcre pcre-devel- `6 t/ B; j; }
3.没有libxml24 o- a2 A; D% L0 }$ G% c) n! K
7 a. h6 \) Z6 l6 d' v+ y/ l

* v) W, a: ?/ Q, hconfigure: *** xml library not found.
* J$ o# ~% G) Y6 K$ Z: r  Tconfigure: error: libxml2 is required
3 n5 P. C; q3 J! V6 l- r) f7 Q解决方法:* I3 j, t- P# r/ a8 x/ s: V7 R- e
% ~# L) @0 i+ @. Q& F8 E
yum install  libxml2 libxml2-devel
( Q  O9 t4 P- s5 d; D2 S4.执行 /opt/tengine/sbin/nginx -m 时有警告3 Z2 _" c& \4 c4 T6 A: A* z9 X+ p% P

! l% h+ S1 k4 u8 }) ?$ p# WTengine version: Tengine/2.1.0 (nginx/1.6.2)
, N, F& [$ N7 X" O) T' X; Q$ b3 Fnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ C$ Q+ g  k' J' k1 v' u/ Z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
3 R6 I; e  ]5 W5 z- K& y" x' M* E. v6 m& ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 ?1 Q6 K/ O( g2 e- g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 H. G( k1 p1 C$ T& J& n9 D" ?# P3 u
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) a1 t7 h; H7 Q  K9 L2 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
' p8 v. E" @4 @% h; n  t. n* d0 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 k8 ]) w1 `2 b8 C9 S2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ Q; }& U; f' ]$ n+ ^! p
解决方法,移除低版本的APR (1.3.9)
, u! Y2 m% X. ?; G$ C( a
3 U$ u7 }2 }9 Ryum remove apr& R! P; \# C" C/ d5 W  z% I7 H0 U( A% n! e
5.Error.log中有: Audit log: Failed to lock global mutex
$ Y# y1 N# u6 Z( ^& T1 D# A) y; T9 E) i( @; a
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
6 ~8 b( Y  d4 O; ^3 |' |global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 H; L. J6 G1 E& B1 a
解决方法:& v( Z, U; |6 i, }) I3 a
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* b8 E5 Q. M+ o0 U% b3 q$ W% ^
* V5 l% P5 m" P' QSecAuditLogDirMode 07773 k( O7 Z2 H* y$ \
SecAuditLogFileMode 05503 L  S/ `2 K% C2 A: Z" O
SecAuditLogStorageDir /var/log/modsecurity
& A- t1 l. K# `$ Z+ x9 VSecAuditLogType Concurrent7 }. {- ~/ R' k, O
参考文章:, u9 a1 X: y$ f( X/ u
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& F5 p4 N" G  i6 V* V) Bhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-17 21:00 , Processed in 0.070125 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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