找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11531|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! j" t! K' |! K
9 B! P1 Z! E; y. Z6 [( F
一.准备工作
$ W+ g7 K6 O- x" [) n
) I0 f$ J5 T0 C4 j系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# v/ z& \& B1 v& t) g% N! W& O
4 n" C8 u5 v! g% o3 r6 }
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 E; o4 T5 b3 _+ ~3 `

5 _1 x7 c+ m* I: Q: a& W7 h) L( smodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 `( t% k3 B( U; U+ x7 q% d9 ?/ S9 I3 u9 Z7 E/ @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 T# M! m: l% y/ G+ m. ^( T2 \  }/ j/ `
依赖关系:
5 ]* W( B: B& I' i2 g/ X) ~$ Atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 D$ e' Y( V* @" I! y" l$ s2 z
  z" T2 N& z2 \4 }1 F+ N5 B3 g
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
7 m# n: j5 L" [' u/ ?- U8 Dmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ Q0 l- }! t& f$ p. R, W
/ O3 L' {+ R' Q) b( n, }" ?yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel9 a7 X; c4 g: E) F7 r; g/ V  J& K# G6 m
二.启用standalone模块并编译5 M7 }1 O* t# _' p# ~; J

: f$ X9 A- |' t7 N3 b2 E# L下载modsecurity for nginx 解压,进入解压后目录执行:
) V# I1 U4 f; M& i5 K$ E) H) G1 N# X$ l: p' L5 R1 r# c+ b3 L, X
./autogen.sh+ j9 |2 h9 H! R9 Q  n# [+ Z& E3 U
./configure --enable-standalone-module --disable-mlogc# v4 ]: C3 {! `3 N& ^
make   t6 P4 [0 }- T8 u5 ?* ?
三.nginx添加modsecurity模块
. |7 z% J* ]- i- U* h& _# m- Y8 s, Z+ m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& ^6 [  `: G  K8 k# H) X. k$ q! \$ [: T: `5 j
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  w5 \' Q3 X2 _) ]9 `
make && make install
  c# }- {1 U( p/ B$ O9 z四.添加规则! j4 ]! z) u9 z1 ^( n! @/ L# L

/ O- B0 {6 i: Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& G  E' C, o0 X
0 x! ^$ a" z) y- o% `% ~# i7 x+ g
1.下载OWASP规则:
% h* ^2 `1 L: H& k
3 H, o) m( s  o$ r# ^) xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) Z2 o, K+ l) _: i' j( d0 a$ k2 Q3 T: T% P) p$ E
mv owasp-modsecurity-crs /opt/tengine/conf/  m& Q* M/ ]4 T' b/ w
: V# s: Z5 e/ c. n6 w/ i+ J4 c, c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 x0 A8 |- z/ V% u8 a/ W3 ]  F
2.启用OWASP规则:
: D2 L- t' n9 \, J6 O" v( C5 F4 r& {, D
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% f2 N3 K4 E* }
; k) l: L( }2 k8 i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' c6 B- D) [9 w1 D+ f

& `! t5 l6 G7 v3 q/ D& c9 Howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, d( R$ q6 q* H
! g$ c8 o4 \# N9 z+ t# X9 q1 s; {
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
! k: G5 u& v' j# J4 G* P+ tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ \4 P8 m& o' c$ m7 a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ ~: L' J; ~" _) v/ t  F( cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf: a) f8 L/ l3 B- J4 h" [1 R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 E+ A2 }1 i2 R: n, {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! G- C" _1 @# `0 `! a* J, M! n
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
) k( @; X( e! C4 n5 `- {五.配置nginx
$ r7 Y+ K: G- g1 H
0 c/ H  e+ @+ u; \3 e9 _8 z  U# |) U  u在需要启用modsecurity的主机的location下面加入下面两行即可:
1 O' R# |1 @" j9 N* a
6 R" D! o# u; ~7 R$ `* EModSecurityEnabled on;  
  I0 M8 k8 R4 Y& pModSecurityConfig modsecurity.conf;' o2 j2 Y3 K% W. ~: `  w( X* D
下面是两个示例配置,php虚拟主机:
, i( y3 P1 z2 i9 m, J: h) f& v
$ O$ X4 |: l+ C7 u0 Gserver {
  d0 a. d: Y+ Q) k4 w% I* E4 E1 j      listen      80;
" u5 i  i9 O) B/ A+ ~& B      server_name 52os.net www.52os.net;' G5 s5 l, w& R! p+ r
     
1 G: H- F5 N6 f4 p! A1 D- \      location ~ \.php$ {7 Z, D7 m4 U% D/ u% x
      ModSecurityEnabled on;  
2 K7 @# s! J1 S4 C& A, N+ k$ l      ModSecurityConfig modsecurity.conf;
5 b' v( b0 c" K# t( N1 F
( W& I  I/ H3 X3 x7 c* Z! r1 Y- b      root /web/wordpress;! v& v* g% E. e/ t' X
      index index.php index.html index.htm;
" c+ b. s2 v. p* L  ( s+ b$ B( I  \- p+ e
      fastcgi_pass   127.0.0.1:9000;2 X0 ]% ^8 W% p6 d4 \, ^
      fastcgi_index  index.php;
/ k% l8 m* H' y& L8 Y8 v* c/ p      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;$ r4 o, Z: X- P: M; [8 O
      include        fastcgi_params;. I5 t$ `- `2 a: z7 H% l
      }. B, R1 ]: L1 M* y: w
  }
" z, f4 {5 [' w/ n9 b  ^6 _4 ?8 R8 jupstream负载均衡:
2 l% e2 E* N; h! ?& E: Q7 r( p
$ k& X: e. u+ }2 y& c6 Supstream 52os.net {9 o$ j; e/ D8 p" A3 z8 \
    server 192.168.1.100:8080;
0 x0 P4 O( W8 y! Q7 j9 i! N    server 192.168.1.101:8080 backup;6 O2 R8 p' p7 N) M3 H) }
}
& l( [; R9 W9 n( Q* b+ h' |" {" m; K0 i
server {8 D1 G6 c! b( g6 B9 g; O& [
listen 80;! Y9 g) V# O4 O7 {% D+ b
server_name 52os.net www.52os.net;
1 e  U7 p2 c7 c4 ~8 G# G2 t, s* _# k
5 U% [( W, }, j; h; t2 Llocation / {: l+ Y9 O4 j" }" Q- ^# W8 q: i, N& G
    ModSecurityEnabled on;  ! O% G" _# B# w
    ModSecurityConfig modsecurity.conf;  ' {% i: y* U5 o2 X

0 u! D: P9 u5 O5 l) c8 F" W        proxy_pass http://online;. L3 N6 ^' j" |$ m
        proxy_redirect         off;+ P; p* x& t+ C3 l
        proxy_set_header Host $host;! z& ?1 n7 C6 J: t
        proxy_set_header X-Real-IP $remote_addr;
: C! W' w9 T9 c; |: y' D5 E: J        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;0 F# f! ~6 i# D2 P
    }* S6 i8 |; L2 c9 V' e3 u
}/ C! `% o( @5 Y( ?* m7 X9 {
六.测试
2 I1 R% ?. Q) `' M& h8 I3 F' ], k; m4 i- a  d+ P5 N" r
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 C/ g8 A, Z5 g, n

2 {: C# O4 u1 h' G9 G% k<?php/ M$ F" ^' k4 `1 A1 o6 H
    phpinfo();   
2 Q5 T6 N, h: }6 M1 T?>
$ G. @4 t" u8 |  r) R4 P1 @$ ?( {在浏览器中访问:
. b5 N* R% \8 c4 I/ y; r
1 [  p7 D# p& F0 t% U6 qhttp://www.52os.net/phpinfo.php?id=1 正常显示。5 d2 b7 g; [) M8 t- }
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
# t, V% t/ e$ g" R3 @+ o7 X: ?http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
1 e" Q7 _( u: U) u2 I说明sql注入和xss已经被过滤了0 G4 I2 S" ~9 Z( Y# G6 v
" C; v, W$ Z: u
七、安装过程中排错" H, b. \5 z  u* R) @0 ?4 j( B

1 [: _" w' w" }8 h; W/ r0 W1.缺少APXS会报错8 p$ J0 x. _+ O

$ P, u5 U. G2 {7 R3 Jconfigure: looking for Apache module support via DSO through APXS- v+ P2 x- m* n: H9 N- y
configure: error: couldn't find APXS
  x. _* `. b# @4 `+ }apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。9 g. ~. [# v4 N5 K. C  I% c2 h
解决方法:! G$ U2 k1 R0 q; W( j( H
/ ^$ A: }, A) u( b5 j% d, H! E
yum install httpd-devel
" P" g+ ~2 z5 L- E0 c, P; H2.没有pcre" k% K3 }! D8 d( K+ W
' j) r) q* o; R8 n/ E: S5 H- n
configure: *** pcre library not found.! Q# \4 x/ ?. O' O+ P" i. c  v
configure: error: pcre library is required
5 v7 S2 U1 [2 w* \1 {解决方法:" \% D8 y! y) V3 w# J' D

7 ?6 T, X* r% Syum install pcre pcre-devel/ e5 r6 T# J1 \, _' o
3.没有libxml2
7 d4 V/ Q5 j! K5 q' h- f- L1 F" Q  K* n5 G, @8 P. p0 D
; a  T, N3 u3 S2 \; \8 `0 u
configure: *** xml library not found.
6 j! J( z1 w: e0 ]3 t0 ~configure: error: libxml2 is required, z1 j! L! S0 c* I4 I
解决方法:2 I5 f2 g$ C" i$ W- w

7 O4 T5 \( K5 b$ Oyum install  libxml2 libxml2-devel- H8 A. H3 @% z3 X, P
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ r% E  F( @' ^2 C
; V. {9 u/ f3 h+ z' r( dTengine version: Tengine/2.1.0 (nginx/1.6.2)
! J$ T6 R7 B' Q3 ~nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. H+ c2 Y) a$ y8 v原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ B7 S0 t, ~+ W, H7 }  P
& G/ n3 B, x' v# h7 s$ e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* e# }! B2 K+ k5 a$ I; ~  u9 F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"3 W0 ]# W4 ~  C3 B4 r
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! `* }8 Y/ l$ c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# T# _6 K9 a( W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"* h  a. \  k( X7 b
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# r2 v0 n0 A0 T5 _4 K
解决方法,移除低版本的APR (1.3.9)( {, {( r, Z* @7 n& k5 D) n- w3 ^
, f2 C/ ]7 }) Y
yum remove apr- G5 _, v! A& H* l# X; j2 h6 D6 B
5.Error.log中有: Audit log: Failed to lock global mutex4 B. N+ Z, \) v& a* W

3 X3 ?' e% K) \8 q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
0 h: Q' s; E1 w- x0 i7 Mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# ^7 s$ d; w! c; K; u解决方法:; U+ _4 b/ s% m4 F2 D
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 o. B, j9 ^+ T/ T% }( ^# @/ K% H. C9 X' U+ a( d% q
SecAuditLogDirMode 0777
5 J; T8 T4 U( d2 b  eSecAuditLogFileMode 0550
) c) ?/ J) |* W2 F; C6 F+ ?SecAuditLogStorageDir /var/log/modsecurity
4 J* _6 h; ]; C. vSecAuditLogType Concurrent
* H; k3 J. m( l; A参考文章:
4 G2 k$ Z2 l7 k, Y# Q0 k( x' Yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
" c9 A  q2 B0 U! ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-25 09:51 , Processed in 0.061902 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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