找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11265|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# ]% @. U: {1 I% }
/ j9 f- M5 Q& o' F
一.准备工作0 \% Z, A" H$ p& x) r
2 l3 u9 v+ ?/ ]
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
- t5 @, a% B  o% I5 O8 j$ F3 X; P# M' Z4 s: o
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% o$ Q7 E/ t2 S3 `  b. \. S
, u9 Z/ S" V& h+ E
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 `8 A+ W6 }( Z/ U0 j$ H( @; v
# w2 k/ U) K3 V3 ]+ E! }+ B- U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ w& T. k: O# w7 C
2 K- X  k/ n9 j' c依赖关系:( L! {$ L1 t' s! b
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:- ~* J. _$ a0 X1 @. Y, a* T

. S; [. v2 l+ R% E9 [6 Ryum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel; h/ b2 l' F- z
modsecurty依赖的包:pcre httpd-devel libxml2 apr4 V$ Z* K: q; \$ u

4 R2 K8 D9 Q% e  x+ d' D) [yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
. L. H+ g/ g# _; T二.启用standalone模块并编译
4 e5 K6 H+ d& S% G# e) ?3 p
" ?- W2 [1 b: h% I下载modsecurity for nginx 解压,进入解压后目录执行:
* n- \# V8 j; ]7 P8 U1 ^; n; Z5 d$ p/ g+ w
./autogen.sh
) s5 H- a% r" v; J& |2 P9 f: v./configure --enable-standalone-module --disable-mlogc- B( a* G# M5 a  c; a# Q6 k; m
make
5 `2 Y& L  \9 W+ M4 k三.nginx添加modsecurity模块3 u  U/ {! x- ^1 M( H( {* ^" ]
' U) ]; _4 T$ m7 U2 q) X
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# w# {) C: C$ _1 w
  H$ R& w# [" a6 T, o
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine3 c; Z- X/ u6 c# L) ?
make && make install" o$ b  r4 y" c0 P3 ^
四.添加规则! N  S. u1 q+ S# u/ @

4 K! ^4 [! z: f- ], R( B8 n% ^7 ]modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 v: I+ `* W. V9 y8 |: l( h( [

2 e) W6 o" p/ `& P1.下载OWASP规则:
( P' T. N9 r  K8 l
- l* T! g* A) P2 y- i5 s$ x5 f" sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 E7 E; _3 A7 R. v: o' l. S

7 ]- W! t9 A. G' F3 Smv owasp-modsecurity-crs /opt/tengine/conf/
# U' T. P7 D4 I2 z) z. R  I
3 p! k0 y8 U( X  {9 zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" [' G1 U; z2 D3 D; a  ?: K2.启用OWASP规则:
* p4 _! [9 R; i* G( }$ d7 s$ B# W; j* ~: F& \  w) V6 j
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: ]7 d7 ^7 t6 p6 [# i5 N% X2 K+ c

$ X- A# p+ O( G! l+ j编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ \! b% v5 T" X" }( {4 y, A; h
8 F$ w. L5 j- x7 t* L" `# C, oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
  D8 z9 T. V4 G# y7 |$ a% X2 a2 E8 y  I1 L! }
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- o7 {. B3 d2 l5 \4 W5 _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 Q6 Q* q% F" @! C2 w; G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 }% z/ K7 x. ?" f  ]- r+ T/ t# ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- q, e" t  W7 y& t: z; U
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 s0 v0 e8 e' D$ C) _9 f* ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. L' X/ ]! {  ?# ^. }/ nInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 L3 `: i* R& B' r五.配置nginx9 f# F3 U6 e7 a' z7 T- x0 ~" e

5 ~: c9 x& W' x8 t) h1 m/ H5 }; o8 _在需要启用modsecurity的主机的location下面加入下面两行即可:
2 r" ~& i# Y9 P2 j1 ?$ l
* \- E" S. l3 j5 X+ s: U* ?* f1 gModSecurityEnabled on;  - e  d' R4 z* N' A
ModSecurityConfig modsecurity.conf;# Y& y- P$ q/ a( z" E
下面是两个示例配置,php虚拟主机:
* l5 t: n- B2 h0 w  n) ]
! ?1 m/ Z8 Z) i0 Vserver {% H/ H3 v% g& T) J9 x* W. ]
      listen      80;4 ^- A1 b' P! V; Z0 I" O) q- R
      server_name 52os.net www.52os.net;
$ c: |" l) e, p5 e! P0 o     ' W6 w( n  C% S' C. B
      location ~ \.php$ {
9 _0 A- @# L% P3 Q      ModSecurityEnabled on;  
: X0 s4 V0 T. o- F* D      ModSecurityConfig modsecurity.conf;$ t; H) e6 t7 `" O# f& ?
3 u9 T7 U0 Q* n1 }7 j2 v: F
      root /web/wordpress;
# `6 c4 F( N' f      index index.php index.html index.htm;
: J. K/ K& v1 \( M" V  O7 w6 y  * u3 l/ Y. K/ U% u# a( J; W9 x
      fastcgi_pass   127.0.0.1:9000;
9 n! v+ T" q3 t6 d. ^$ Q9 v; K7 P& v      fastcgi_index  index.php;
9 P4 q1 C6 O: K1 S3 _      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
, `6 X1 |& I) I) g      include        fastcgi_params;# v/ i+ v+ O) L
      }& }" [* ^2 T. N# @! f
  }
6 P2 f9 y2 x5 R# |& ?% Wupstream负载均衡:
1 q) y, d' t6 B. e$ u) [
5 J6 x4 w' q: V. [3 _4 e; Uupstream 52os.net {
1 `; L$ U3 r7 J; t! Y2 g' o    server 192.168.1.100:8080;
) i1 U3 x" G, d1 @( R1 F    server 192.168.1.101:8080 backup;: K! P8 w/ O1 V0 D
}
( k. o" {5 M& W0 H$ H* V% z2 Y5 x1 V& V6 C
server {
  m* q& X( ~' A7 W# ~listen 80;
9 ?4 y, g/ b* j" z3 y; bserver_name 52os.net www.52os.net;! c% s1 M# l1 O4 ]1 e

6 E$ v* U; F& `- Q4 t, P, l4 ?location / {
' L9 _7 W' b; ]0 C: q, S+ `    ModSecurityEnabled on;  
; K/ X& i4 y& A    ModSecurityConfig modsecurity.conf;  / \/ |0 M3 C/ h( L

1 ^' A# R3 b+ j! ]        proxy_pass http://online;1 B( V3 M1 |7 s* l- [3 M: r5 u; }
        proxy_redirect         off;
9 l9 Z' C" r$ B6 C# u4 L9 m        proxy_set_header Host $host;4 J. {  a2 a# ?7 x  j
        proxy_set_header X-Real-IP $remote_addr;# J: N% D/ y2 \( l2 R3 M
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;4 |5 ^' \7 k/ o8 e  ^
    }2 ]" @; _4 M' `% f3 D" [
}% ?( K1 B0 \; A$ i3 P1 h; B% y
六.测试: B8 O6 \! b* l

) V8 d' X2 _9 W$ G% L& p我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- k; E8 T& S5 j5 G7 ?# j$ g( s$ U; O3 m8 E) `2 c
<?php
9 I- Y7 s! O% k$ w    phpinfo();    3 s# r# B8 o6 E
?>
! h! k1 @) x0 Y+ @* m  n" s在浏览器中访问:
' B# k/ z' i6 C! F0 q  f/ I, j6 l+ C2 }9 q0 Y( U9 w& ^  Z
http://www.52os.net/phpinfo.php?id=1 正常显示。) \3 ?( Q0 v% a  t; Q  l+ O: E
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。. l/ M; c1 S$ ~' q1 L+ C4 j7 k4 Y6 q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
, X8 ^: g; q3 h2 l" _; h1 B( K0 _! W说明sql注入和xss已经被过滤了
4 r4 r. b2 g, g1 U% D9 n2 K  z) r, j3 G( j3 l
七、安装过程中排错
0 a! |: d7 q) L) K% C, P/ U4 Z# T& m4 e6 o( z. a: ~2 G
1.缺少APXS会报错
) K! s' X# Q  k& r- T1 p5 M& _9 O! {1 C% Y; m* k. [4 l
configure: looking for Apache module support via DSO through APXS
3 b% `# ^0 ]( v( j( x0 kconfigure: error: couldn't find APXS8 K  R" x" J/ L5 N
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。% D  p- E# C# T* d2 y
解决方法:
1 S; D, A# c6 {" [8 M6 Z- w' q$ U7 T$ A/ n3 g- Q& ]
yum install httpd-devel3 t* q( F7 [2 ?6 h( L8 M
2.没有pcre
+ A; c; j9 r3 W0 J6 I
) ?; T+ E. G/ _# z0 m/ Tconfigure: *** pcre library not found.- x; W7 V- Z1 ^9 s8 j) }
configure: error: pcre library is required7 y( n2 ?3 m8 i2 g# H/ r* `1 i
解决方法:
/ N3 |& A; t( J0 W, B/ e# ^" _5 i# o! s
yum install pcre pcre-devel
" F$ G- j6 F! Z1 {3.没有libxml2
9 e' k1 ]; {, K+ u+ N8 B+ |' d% T5 I  A8 @

; ^2 G3 V% R3 r# ~3 jconfigure: *** xml library not found.$ S1 k+ Y0 K3 [% B% z
configure: error: libxml2 is required
6 E2 f* v- n7 Y: \- `- {# x解决方法:9 {  v2 Y( U) L8 c9 s

1 h" s) ^8 L7 B! I' y% tyum install  libxml2 libxml2-devel
1 _5 P2 E1 D5 K! N4.执行 /opt/tengine/sbin/nginx -m 时有警告
, I5 _' p* g7 f5 f* F
1 `  d2 J; W. f2 @: @7 K$ ZTengine version: Tengine/2.1.0 (nginx/1.6.2)/ ^" y: {4 F9 h! O9 H, |: O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ @! u( d% v) Z% O" r原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log3 i% ~) j5 [! @3 A6 z
5 Q- v& e+ `* z! _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.+ x& I: T% c' U! f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"- T3 s$ U3 e: L+ s( q# K+ |# J: |
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 t) K% w' ]) p  q8 f' l, S2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# O; c: }4 k! i3 v$ e5 T! R: f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"2 b" \9 f  |3 N6 Y+ p% v
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ T6 n) j8 p" j3 J
解决方法,移除低版本的APR (1.3.9)
# x2 _' Z/ M2 V
) N! m7 N9 P; Hyum remove apr
$ W, N3 b/ q; q" a) C1 u5.Error.log中有: Audit log: Failed to lock global mutex3 f$ _& L$ P( U3 V7 w5 Z  w1 m2 }3 ^

7 }, X5 U5 Z$ I2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
% f3 i) j' W7 `% V# L0 tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 c0 p' ]- c/ {2 P7 f5 O
解决方法:- T: i' g* J0 a2 A) k# p7 n
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' ~: V) f+ F! c' l5 m! _

, V1 d  n) _; f0 J7 L) ^SecAuditLogDirMode 0777
0 l- q' ~7 ^1 PSecAuditLogFileMode 0550( c4 C1 q# O3 w% s# Y
SecAuditLogStorageDir /var/log/modsecurity9 d8 m0 t7 \  V1 j9 I! G
SecAuditLogType Concurrent) A7 l8 q7 g3 I& T8 P
参考文章:
, [1 b. M, G' W. T3 M2 Q9 Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; h1 h3 \5 l* n! o' _
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-4 08:50 , Processed in 0.122083 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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