找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13185|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 M0 _, d' q1 _+ P! }5 f4 m# |& @7 t$ @) L' j1 }& R+ d% [6 Q1 d$ w
一.准备工作
, J1 Q* Q6 ^- M& u: l4 y  n
7 p7 ]( K7 q- s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" x2 K( Z- R! s7 v' s/ z
9 I. [' D; e1 T! o/ ?
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 S) x) [5 P) d" X5 ]- E6 x
. v9 C: B" o% m4 \! b8 c+ ^& l3 Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ f! j- \* ^3 m5 W
7 H5 z  n( g+ T) p' M. q9 G
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; y4 Z  x9 V+ j( y# H" k% X
( g  y8 @: \% \( I! U: e依赖关系:
7 Y  [4 m0 Q3 Z+ q$ W% p; p- Y6 Htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 w! l7 F  ^5 _7 X. @$ G
4 @1 F+ q) z: G% Z* a: V
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
5 x  ?% M' W. |modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ w+ c- I* p1 D; U- K$ j9 G! i* q3 _/ C
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel7 U7 U4 J+ Z3 {* W4 Q! G
二.启用standalone模块并编译
9 E: g0 ~7 @2 a  e! l; T$ y/ r( f, `9 P% W) `/ b
下载modsecurity for nginx 解压,进入解压后目录执行:
! }- n4 o0 p. l  |. P0 X
6 [. e. L& l- p; L% ?8 b./autogen.sh# X/ q2 Y0 F5 j9 c
./configure --enable-standalone-module --disable-mlogc
( Q7 U6 J0 k" ?' g% ^4 g8 V2 h8 ~make ( `% ]! v0 y4 ~- \. o7 t4 e, x+ X
三.nginx添加modsecurity模块
$ ]+ y  J# c% d4 w3 J6 z5 z" y: g* L: }/ B' B' L
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
" Q0 L! {' M0 u9 q+ t
% Q6 i: ?9 T( R) m& i+ H$ L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
& ?: N% r/ ^7 B9 e  A7 Imake && make install
3 ^$ o0 g# l# X# P9 f- B" t6 {四.添加规则
2 a% W& ^( c# Q% K2 c, |; F* Z. @3 M7 ?
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。# K' k  R2 r  V( P: m- J. L
! p0 v2 V: D+ Q; W6 _- v0 ~
1.下载OWASP规则:
% u1 _0 ?9 R5 B' M  L* E4 F! J  D% G
2 R' E6 x# X$ k5 B0 j7 F* y6 d& X8 Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs4 k$ q. ^. T3 j- _
! B6 c( u5 G2 j; y
mv owasp-modsecurity-crs /opt/tengine/conf/  @+ _5 T5 l- L

! ]( A9 {8 M: Y- Rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: v5 ]) z8 J, O
2.启用OWASP规则:
6 o. r% X# v) Q9 D' W7 H) }( ?" i6 B4 K4 v3 ^/ y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
& @' ]9 s1 G( E* s8 T2 m- j6 L
  D! [; p; f0 v( ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 I5 F1 L' Q  u4 ]
! @$ A" [- l9 Y/ D: t8 I& y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- v/ g0 |) V* k% T8 f: x( J% o1 G
6 @# @5 r+ M& I- e) P# w/ c* pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
5 m% K& j1 i+ _( r# gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 o9 Q& ?5 D) o, S4 tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf1 r& ^, k  g7 r/ a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" {4 S. Q  a/ Q+ ^+ W$ W0 w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; {" B$ P8 h' {4 s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 E( q; J& n4 j2 H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" t% x1 c, @4 x8 c  r1 s
五.配置nginx. p* Z, @+ ^; Y: X

4 h/ ~  k- a; n' _6 C& Y在需要启用modsecurity的主机的location下面加入下面两行即可:
! n8 [' ^# P' D! [6 }" X' i
! I& {. Q; M% r; O$ IModSecurityEnabled on;  8 `: C7 G0 }) C! k" d7 q3 X9 Q4 d
ModSecurityConfig modsecurity.conf;
0 q' q" l: M6 E0 n$ c5 z下面是两个示例配置,php虚拟主机:2 A  |2 K& a, o: N" {1 p+ J

6 z- A4 {1 N5 H. x- b' _" userver {
6 I3 y  K8 g9 c3 W) U0 _      listen      80;& x# I% t, T( G2 }( p4 J, A& D9 I* A# p
      server_name 52os.net www.52os.net;
6 j* }7 n+ W, y  q     7 m$ m8 n4 y% H9 [8 k( N) Y
      location ~ \.php$ {7 Q/ i7 u1 j$ p( _+ }" h9 C
      ModSecurityEnabled on;  " \$ I$ ~) h. m. p! a$ E/ @) w
      ModSecurityConfig modsecurity.conf;8 ]% k3 X1 h1 F! P) }" i

' L7 ]# Q. h& G6 R      root /web/wordpress;
+ m8 C- e$ B2 a! B7 P      index index.php index.html index.htm;& n. D0 f8 i7 Y2 W( p9 o
  9 `% `% M9 O0 [: F/ G4 `; x5 |
      fastcgi_pass   127.0.0.1:9000;
( k9 S+ J  ~% _1 D( O8 G: B& `% c  X, n      fastcgi_index  index.php;8 M! M* Q  s, w: ~) Y$ i7 t
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;) ], h: v9 a& }
      include        fastcgi_params;2 O: j- n$ ~2 {; `& Z
      }
+ z0 f9 C. H0 D0 c# x, Z  ]  M  }( A; T. o* J- o4 W3 m  X- D
upstream负载均衡:+ p/ b" l4 w5 t, h7 n
2 F9 e6 A4 E# E3 m9 D; M
upstream 52os.net {
/ k0 R" w. i! K) S" P    server 192.168.1.100:8080;8 V" h. m$ h( X. C  S
    server 192.168.1.101:8080 backup;
  X. j0 B: p7 D+ l% v8 I2 A}
5 U" @" R* l. L! T/ i+ _1 t: j" ~; n3 r5 O
server {+ k( |8 l! }; N8 Z2 |
listen 80;3 X+ K8 u5 m' \) L* ]0 Q6 {! `1 X
server_name 52os.net www.52os.net;
7 Q+ S, A* J4 G3 H/ {# S
8 ^( G/ P8 A9 [: Plocation / {
; |& K% y* |- b- O' Q# C8 r' j2 M; Y    ModSecurityEnabled on;  
7 j0 `2 l& w- W' {; i    ModSecurityConfig modsecurity.conf;  & ~6 ^; h4 o/ F( P" G1 ?( _

- i/ @% ]3 k( `5 t) ~; V; m        proxy_pass http://online;
4 C0 ^* q9 h4 b3 ^* j7 w' n        proxy_redirect         off;; K8 L: J& F& R+ b4 I) T
        proxy_set_header Host $host;* s" u! B. _( `/ F) `  x! p
        proxy_set_header X-Real-IP $remote_addr;
8 Q- ^9 M3 {* x* r5 I        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
( v$ F# V# W+ W9 b4 f    }5 B* q$ o4 a: ^: e9 w/ g" n
}+ S, \8 j% Z3 E+ ^1 X' ~' C
六.测试3 W+ M- G$ [0 ~" g( I# Z: \
8 O8 }+ @2 l2 o8 [
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
. z8 m& O4 C, `5 \
3 s( N+ H  k" y, F* M<?php% N6 [- n% o8 ^
    phpinfo();    / N8 i( K: L1 b- T9 V7 u# o
?>
1 L% X) e/ A) W& t9 k在浏览器中访问:% X3 \& M  x6 a% s

$ ~8 @: N0 E% u0 bhttp://www.52os.net/phpinfo.php?id=1 正常显示。/ M6 r& E- {" v) U
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
8 {! E. t/ |$ x) O$ r9 L2 S! X" z& hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。1 w; t+ K. W& h5 U7 P
说明sql注入和xss已经被过滤了! T  _* U* W* R
2 i5 F6 s" i) _( y8 f, k
七、安装过程中排错  J9 s  ~# s! l: r4 e, P
* a9 u" h0 \0 b* S( E2 \, [% u
1.缺少APXS会报错
: [* Q' a  |( e2 L2 L/ `8 A/ [% j  l8 z- n0 i; T( ^* U
configure: looking for Apache module support via DSO through APXS
+ w. ~* L! I/ @! g5 ]( cconfigure: error: couldn't find APXS
2 s& X2 S% F0 S6 Mapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
) Z, N: H: B. L0 ~* {3 M解决方法:
# a4 N% Y! u+ I7 Z
9 Z+ f' h, b' i- z" Oyum install httpd-devel" {0 F( M$ h+ a+ [+ J& P7 ?
2.没有pcre
1 N6 e3 X' u* r: N5 `
: y+ m; `$ {8 u; u) j$ xconfigure: *** pcre library not found.
, |3 m4 \$ x" ]5 X0 Yconfigure: error: pcre library is required
0 f/ `' G, [& i* s/ `) y+ r+ G# w解决方法:) t- ]* G- K: K7 c

7 J7 R) Q' {1 z& _5 f# ]yum install pcre pcre-devel0 }, e2 U- ]& I  \  i- c
3.没有libxml29 s" Y+ o  q5 w" q
, T- u- ?% C. e9 @. c' L" m+ J
) C+ i  [6 k0 x6 ]
configure: *** xml library not found.
3 M& t% k- \" K5 Lconfigure: error: libxml2 is required/ \  o7 Z9 Z4 x* W: \) R4 y; Z5 K5 I
解决方法:
7 H6 ~1 d1 |% v5 l. ?3 f6 w9 B" H5 e8 E% w
yum install  libxml2 libxml2-devel6 w* P% m2 X8 {, v& A; @7 x
4.执行 /opt/tengine/sbin/nginx -m 时有警告; k) {4 Y1 \6 N6 |& g9 N
$ j5 K/ ]9 l1 T5 k' I
Tengine version: Tengine/2.1.0 (nginx/1.6.2)* W/ I" Y& J4 O8 A3 `0 d3 ?% _
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ O9 C) ?0 F6 y4 R4 f/ o' N1 D原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ j2 W3 [; F9 P# ~6 S) G# X3 A
; O$ j  E7 x( v" u$ A" X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.) R* y$ b- q) _8 K1 `3 z  b( t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
% n( B% a2 J  ?* @2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: E8 v" ?* y7 ~4 @$ |1 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* D0 @# N, z' r# H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 s( I1 H& B1 @3 }# u2 r; ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
; ], N9 ]" [6 b6 r8 w$ H" d解决方法,移除低版本的APR (1.3.9)
" D: x. ?9 d/ p! N* k6 c" h# k6 q$ D8 T
yum remove apr
: ?' H$ D* M6 N$ G1 f7 r4 P( V5 k% z5.Error.log中有: Audit log: Failed to lock global mutex- y' ]& K9 C  F/ u) a0 h0 J) t3 U

5 q8 l- U/ l4 C. ?+ G6 q7 Z" ]2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
. C, _6 [7 O4 r# bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]% \- l! q( z. W6 H# a1 f! n
解决方法:
, W- `2 Q. }! ^3 W8 d9 x3 n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' Z: P( K4 B+ n; b2 ^) l6 R2 \
! N/ E! L1 J: ISecAuditLogDirMode 07779 h9 _3 ?5 d! C6 d7 H7 w
SecAuditLogFileMode 0550
0 E8 ^. P% O, n. q' |SecAuditLogStorageDir /var/log/modsecurity
0 ~$ ^* d( ~# Q9 r) q$ v1 tSecAuditLogType Concurrent9 h1 O2 E3 S8 @2 O6 F3 s
参考文章:3 |% I4 {# h1 s7 o& F$ m: T" m
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% z; e6 P2 q8 \. E. H& _2 f+ ^. Shttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-22 10:09 , Processed in 0.075718 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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