找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11953|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。" }* x0 O+ D: S1 k1 Z5 q
) L, J9 x1 P' k! D' g
一.准备工作
. u8 ?* V3 z+ T5 f- Y% g, k) ~6 U* |/ e$ X9 {1 u, X* Y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
9 n" C+ |: N, X
3 Y  J0 l; z; K6 k" n5 n' C! ttengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. _2 j0 i" s" ]+ g6 b) [: s# s9 ^
! o  t, s$ Y- A8 s1 e% ?7 _modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% G# a4 U0 U/ _5 P! o+ B, h7 \. @9 v1 V# m) K
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# Z- L; F5 s6 H% O* L: e! o. O% {: {
7 j. D& ]: T5 D( t. Z, p依赖关系:& F/ d" c9 I6 {2 g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:+ y. ^5 T* J8 Z( J9 W" c

9 u5 Z  x+ g; ~. wyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
2 H8 |# D% `/ |# Emodsecurty依赖的包:pcre httpd-devel libxml2 apr
; w( h- f7 P0 A+ v  X
( b' v6 e2 S, A6 k  j5 W2 Fyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 T, b- m  d, ?0 X6 Q3 t+ C* T/ @二.启用standalone模块并编译
! B3 a! d% Q9 A# N
; b+ L- P4 @- Y# m  y6 l% ?下载modsecurity for nginx 解压,进入解压后目录执行:
5 `# `$ t# w& d: _$ I
# Y; y  X4 y9 q9 n8 a( k./autogen.sh, V% h2 R# T9 O2 v3 b/ ?# f5 ]
./configure --enable-standalone-module --disable-mlogc& Z7 C# O% j) B1 H. [0 l
make
% X7 }2 j3 b5 b/ M$ Q1 k- v4 }三.nginx添加modsecurity模块$ C; U2 d* R7 d& y
7 r* W6 b6 b6 {2 p
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" G/ w5 M$ u0 H
/ }. C4 a  x* w' m/ m4 c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine/ O+ _7 X. s+ Y; Q  x
make && make install' H. s( \( H5 U* K7 O1 L
四.添加规则% {. U, D) H, a2 Y+ ]) A7 x
! t6 w7 Z# s4 j/ B1 n- Q
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! n. M+ Q) h4 G. l. U  L5 ?$ ~; w
. b3 G5 I) G: P  n8 t7 H9 M
1.下载OWASP规则:4 \& G( f8 ^4 X" f
( k6 p- g- m! D' m" l6 m- x& n( c
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! C; k9 s4 _7 Q' U& P. S9 l  N+ L* p; T. @- ]1 ~+ _# \6 S3 |4 k+ n
mv owasp-modsecurity-crs /opt/tengine/conf/% J, Y' t! q! S' @0 w) f1 V

9 _6 O% x; F6 L* v/ {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
! Z. O' M% D& t, S3 {2 @* z2 @, _6 Y2.启用OWASP规则:  J/ s' O1 W) H( `. K6 W
  ?& n8 P9 R4 e& _2 n9 A  R1 Z' S6 L
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 A8 Z) R% f6 L; Z7 m; d+ {* ^2 O1 F
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ K0 Y/ D9 ^" E8 P
* Q8 c) X6 U- x- V8 ]
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
4 b# E3 _( a, A; A" [) n/ n2 _# d. p9 W- }  R6 W
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& G2 ]  v4 l+ B5 V: M3 {* }" \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 y/ c' h% y- C& ?$ a6 u% [4 |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ D- T; Q9 K# |$ z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 P; v+ P( i% y/ B% \! F# N; d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 e7 |* M3 s# H# K* zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
/ \7 {* u& b6 tInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 s! K& K- V( y) {5 \8 Q五.配置nginx
9 w2 a0 i* m' W8 i. J
+ \2 c+ J) H3 M3 `/ t' \6 m1 Z在需要启用modsecurity的主机的location下面加入下面两行即可:
. H8 Z4 f! N) Z/ A
0 A0 o8 t0 c, P8 GModSecurityEnabled on;  
/ j4 e: B' I. h- S* N2 r& LModSecurityConfig modsecurity.conf;/ i! w) g' u% |1 }5 b' }
下面是两个示例配置,php虚拟主机:
, u: y4 I2 d4 e6 g# p' u! l! ^- b0 f+ l
server {
/ F6 ~$ D4 }$ h3 d! ?* y      listen      80;
3 ]$ p6 _+ \, h2 e5 P      server_name 52os.net www.52os.net;8 G# \9 `2 A3 I) }2 R
     
( a6 ?  _( I( |" w2 P4 b" ?      location ~ \.php$ {- f5 A: u( V9 w5 U/ B* ?/ @
      ModSecurityEnabled on;  : t% H+ G+ p( X# Z, W
      ModSecurityConfig modsecurity.conf;3 C" ~, y: c- c* b3 G% u3 h

0 W3 h2 v' Q+ G0 `      root /web/wordpress;
3 i, X1 M2 w" a  O1 j, Q% a      index index.php index.html index.htm;* E4 z& V, M+ W9 r$ R' x
  9 b6 x- e* n1 \+ F: y0 |$ c$ d
      fastcgi_pass   127.0.0.1:9000;
( {  T9 q1 j( l: C* t0 X/ w      fastcgi_index  index.php;
6 F4 [$ `- ~) [: x) A      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
1 c3 l) M" Z! g6 e) s3 p      include        fastcgi_params;( O2 {5 }) m2 _( A
      }: S* o+ X9 n& j0 F$ p% ~. u2 ]0 F- @9 I
  }8 Y+ |, F) J8 H: R7 }. M  D
upstream负载均衡:
9 ~1 y4 ], T( {  s# d7 M: m5 G& U
8 V" {0 ^# N% O3 e% G) j" F2 {/ Pupstream 52os.net {! ?0 o/ f' j, a! G! [6 ?
    server 192.168.1.100:8080;
0 u3 M3 ]" _5 C& }6 o    server 192.168.1.101:8080 backup;6 `3 a- R5 ~7 L- s: k
}' R' t7 N* V1 I! o" i

+ c# c0 l: z% \2 Q( d5 C" }4 V, ^. S6 userver {
% C8 F8 ~' G+ `; m) O. |listen 80;
1 {9 @) l: S) m; B) V8 G! _server_name 52os.net www.52os.net;
7 W7 b- R( [3 a9 d$ w, d1 m1 z' m  Q) f8 m$ C* T
location / {
* n0 m4 |. f8 U    ModSecurityEnabled on;  & n: ]7 y1 D% s" o! _3 L, e# K
    ModSecurityConfig modsecurity.conf;  4 N) \# Z4 d2 \" y" ?

: D% j3 ]3 ^7 |9 Z        proxy_pass http://online;
5 _* k0 b" D2 i% J        proxy_redirect         off;
$ y1 _* P7 K2 s; \& C( T        proxy_set_header Host $host;! H1 B% K7 ^+ t
        proxy_set_header X-Real-IP $remote_addr;+ u( x+ s, u0 }6 d' b3 g5 [" U
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;' ~6 y# a0 j; S/ A  s/ T- l6 U. J, q
    }6 z3 W/ Y: I3 ?4 ^* ~. S2 |
}1 Z# h' I: A3 P) x/ A$ F
六.测试& ]3 g, x6 q5 B. G- E2 \; f( w( m
( c5 ^3 H  D2 C* r: D9 ]& R
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# L6 \! k4 b6 e8 F' W$ }

) M9 y3 i' S% {: T<?php; E$ N! G% Y) l, y, C
    phpinfo();    " I, I+ z( V" P6 A# X
?>
' G) q7 F- S1 m& N: B; q* m6 b4 G在浏览器中访问:
1 }+ `; B* V/ e1 X' ^  r/ w* \' X% e$ G  v3 F, ~
http://www.52os.net/phpinfo.php?id=1 正常显示。3 E0 k- k- C& J$ l2 C, p
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
+ o) y- q3 Y& T% n) C+ L! p1 uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
$ h) H! F0 y" D& ?说明sql注入和xss已经被过滤了
! B( u" X( G6 a2 u% }& v: F7 c* p, \# `
七、安装过程中排错& ~5 s# [& o& j  c

/ w" W8 i% a- W& w5 H- z. S1.缺少APXS会报错
/ w( _2 w! O, ~' t
0 k5 @; i% a( e8 {1 F' ^configure: looking for Apache module support via DSO through APXS
& W$ B' ?8 m; S) `/ D3 econfigure: error: couldn't find APXS% C3 h1 W8 x. @# c' p3 o
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。1 T/ z" j) m. w; B
解决方法:
( w# f' x8 R/ s6 Q8 J0 B" u* W: Q2 y* _; G2 _1 o  r: j- q
yum install httpd-devel2 n- ?) F3 F. [- _7 t
2.没有pcre
7 w4 U# D6 g9 Q# `, @& @2 S) X% ~& H9 `+ \% r
configure: *** pcre library not found.
2 ^; y+ a0 r' i" j# C) j& \+ ?configure: error: pcre library is required
9 ~0 y" X* o. _: @解决方法:
) c3 ^% R0 {4 Y! f  c  ]7 I
" T1 P' ?5 ]: x8 f) u4 q. u7 q( nyum install pcre pcre-devel
( H0 f7 Z7 V& R( r3 R3.没有libxml2! ^# M  ^" t4 m, f
( s: s' g( W* V+ v

6 ?  X0 m3 i2 I5 a* Dconfigure: *** xml library not found.$ Z% A( [+ ]* Y9 h4 r
configure: error: libxml2 is required6 z$ K" y3 o0 y" q* [
解决方法:
# N) c+ o/ B3 }. v9 {9 b, z1 H% s- W! u
yum install  libxml2 libxml2-devel
& \. Y# {) e3 W: r" {1 C& w* V) G4.执行 /opt/tengine/sbin/nginx -m 时有警告
# r  h. X1 L& J  G& g8 T. J& h* D6 S% L- Q1 _- m& O3 i4 A- D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
3 m5 G! U) P; U/ O' inginx: [warn] ModSecurity: Loaded APR do not match with compiled!! S. `/ ^5 w5 ~' m  x$ s: j
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ w7 l/ g5 S7 ?4 M3 y$ Z7 o
5 i# {) E4 s1 i, `' b& ~1 \$ m7 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 ~% G1 ^8 K9 {. J2 d5 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"5 Q4 g, z6 b5 l" g- j
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! h1 U- W3 T+ @5 L* d' d
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
4 p- j  j2 r. }0 h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
* v% u4 ]" C' e* w4 b: K2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! R* Z3 m; U8 \
解决方法,移除低版本的APR (1.3.9)4 A2 |0 X+ X. K) Y9 Z
: u7 T# T# z  |3 J# B. I, j  X' H
yum remove apr
& ^( ~' H% G4 E5.Error.log中有: Audit log: Failed to lock global mutex  h  Q% |7 v0 O

- f; X' g/ ?5 S9 F6 N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     7 }0 m2 Y" ?: @+ ^7 Z4 C' }
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 q& I8 m  P5 S  q
解决方法:
4 R8 ?- v* T$ H' ?# D8 _) u编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* w7 {' ?0 z# m+ u4 `

' K3 m& O# X$ S1 KSecAuditLogDirMode 0777
0 Z# y9 p( f  Z8 a' _/ p8 BSecAuditLogFileMode 05502 u$ g6 t' L' c3 z
SecAuditLogStorageDir /var/log/modsecurity
, d# e1 }5 T! KSecAuditLogType Concurrent) f8 h" ~) }. w" r* @2 Y
参考文章:3 b1 R* _; p- M; m) ]8 _1 d( ?
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# @; C) v( s+ L% P1 ^http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-7-29 15:14 , Processed in 0.068717 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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