找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13148|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( |/ d9 t8 q2 u% I$ T
  q& s3 o/ o$ F% f2 U一.准备工作. c) k6 @! v* Z2 m0 ^# P2 R
' S' ~& m" W% C9 E* m2 F# i
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.02 ~3 U# S) p7 J% C- a& d2 v' \

$ o# C6 a, J7 ?! m0 [. Xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ g4 o" ?- }, K0 [* K" i
+ r; G! L2 A4 h7 I
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 m* _- Q5 k  e
/ {% k' I8 Z5 k$ ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! U" G2 M9 c/ I$ ], u' j) P; T0 N: f9 i0 _1 E9 s# p7 }! K: I
依赖关系:: e) q$ j0 U- n# J) n  B4 x
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:2 ?; o6 A& J$ e/ F2 D

: \  r# Q9 |$ L" k6 @yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel& L* \$ i4 ?3 D- b1 K! V* f
modsecurty依赖的包:pcre httpd-devel libxml2 apr
& D( t& a2 r8 _& `1 d
, z6 M4 a9 l: W* i  O$ Myum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel( H  S: I$ y* x- ]! D9 x! S5 R
二.启用standalone模块并编译2 z8 g) G, E8 S* N0 ?& X$ f& }1 p
+ f' p4 Z) V" A/ L  `, U. Y" d2 T
下载modsecurity for nginx 解压,进入解压后目录执行:
8 X8 E: I3 w( z# `$ c0 B/ v  O
% C: n5 }$ B* c" f4 g2 ?./autogen.sh
) Z- P" f9 s3 F4 @) i8 \./configure --enable-standalone-module --disable-mlogc
3 |! W" K8 x7 |make & j% J# i  S& k; q& C
三.nginx添加modsecurity模块# _0 R1 y' q! u) H; J
' g' q* A8 O* M/ s" |: {
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. Q) Y2 Q; Q  x/ u
, w8 e5 a$ O3 {# s6 y6 ]) m9 j
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine* c- d2 j/ G# N  b, i0 `+ y/ T, ~
make && make install. V* N: o- y& o# [
四.添加规则
1 `3 f# Q4 M( u) `; j7 u0 s2 }* L$ I: o1 S
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 i9 J* `6 P6 h" L
) V! ?: d; o; h5 y; u1.下载OWASP规则:" N/ {! c( r6 A" c2 ]2 ~: \

9 l7 u/ E( @: F) A1 L+ Wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- f" J; L$ K' a# \! G* c

, ?! ^; v0 t6 n1 _mv owasp-modsecurity-crs /opt/tengine/conf/- h, P& x( O) @+ e/ e1 J
4 H7 a# l, [# d, ?' H# e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf/ |/ `8 a  k3 @6 s
2.启用OWASP规则:
3 Q4 j" z( P; m% p2 n$ K4 p3 K# c2 ^2 y7 o1 W+ H( ]& C
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 o& m+ X# [, y: E4 F3 ]2 [4 f) C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* u' J( X+ V* q4 K+ C4 Q, ]

( q* A0 y5 {4 J2 s, E* Aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& K+ N! s4 |2 o

  }! Q6 x2 r9 |& uInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ f+ A2 K% d3 n) @: F& t% FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 S8 ~1 x1 b5 V# K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 X6 T/ [  g" ?/ g. g  DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 L4 d9 N" ]% z& W2 |$ Z# X# t2 v
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
+ E- d5 T# N7 ~1 m( p- DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ L" c" ~* i4 e' X+ [Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 h+ o- S2 k0 |% C
五.配置nginx
, _, j' j' U) q$ o% }- c& k# S/ b5 {  B& W
在需要启用modsecurity的主机的location下面加入下面两行即可:- h" L, k/ l5 S) Y/ e
( I2 e. z) ~* F% ?
ModSecurityEnabled on;  
- u. I$ K! C2 |. j- jModSecurityConfig modsecurity.conf;; O3 F8 n4 f: O* d
下面是两个示例配置,php虚拟主机:2 ]) S# S/ C7 a3 b. Y
) N% A' W" y! u! \
server {
5 I* ~7 E) v% U) D* ~' k- H3 V: I      listen      80;
' j* I& l# \* S$ j- S3 E      server_name 52os.net www.52os.net;0 t6 w/ c6 g9 F
     7 q5 D( m3 \, X8 q: u- j
      location ~ \.php$ {8 R# D. X/ ^8 G# O& }: Y, ]
      ModSecurityEnabled on;  - ?. y' n4 B/ T& j
      ModSecurityConfig modsecurity.conf;5 h! L8 J6 f) F, _3 q8 v! }! t7 J* C

8 h; e/ ~/ b  @& p      root /web/wordpress;8 J8 k' H$ l( o" k: q6 F
      index index.php index.html index.htm;6 e* r5 T' c* L: X. V5 C
  : y7 h# G# [9 x2 g' O+ z7 |
      fastcgi_pass   127.0.0.1:9000;- R9 y  Z* R6 ]- X5 j4 W& R1 t; D) `
      fastcgi_index  index.php;5 G+ M8 w9 P8 z0 K1 y
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
7 M+ ~# W$ G  o4 }! D1 }      include        fastcgi_params;
* x1 H; b$ m% \2 o' G      }  M+ k1 Y# R, ]* I# b) e$ ^) w
  }* O7 J$ m, A) h2 u, ]
upstream负载均衡:/ u) T) z& k6 q
; h9 S( Z) N2 I9 J2 s! O+ E: g
upstream 52os.net {
/ T8 M/ Y. E- M1 X8 m    server 192.168.1.100:8080;
1 h& d7 ^, [9 m    server 192.168.1.101:8080 backup;
: n: n( O1 s' u* e! q}6 ]0 S% d6 ?# d" ?4 u3 ^

& U# U* h4 E- y9 h. E. bserver {
. _# z( a& V, }" r9 E; ^! V3 [# Nlisten 80;
5 H; u0 I4 `( Iserver_name 52os.net www.52os.net;
( `. O5 @3 t" _* J; V
! o8 }: _& H; C2 flocation / {& i$ }% |5 h" O* x  v2 m. A
    ModSecurityEnabled on;  & m- F5 E: j# K" K% `- N" r
    ModSecurityConfig modsecurity.conf;  
: q  W( w) e6 Z* R& e" r3 W$ t2 R# v6 _5 ^5 k
        proxy_pass http://online;; _0 [3 Z3 p0 _
        proxy_redirect         off;
& o6 _  }, e7 \" E. B; t+ V        proxy_set_header Host $host;4 S3 a6 t  p6 D$ N
        proxy_set_header X-Real-IP $remote_addr;
1 ?2 J6 k; }4 ~6 p! u        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
" k/ B1 W# O' H. b/ E; l8 N    }3 r  j6 |! m. K% @( L, U4 a
}
% `9 L4 V" h  @, \& M- y六.测试
! m( A, t/ S  u& e* w' j  N) O6 ?* w  P0 L! R' X! f* M! d
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' T* o' v$ o! L- f; |& m7 V9 L/ m& P
<?php1 N) F4 g& z% z8 V. e# Q
    phpinfo();   
  V' m8 {6 x( P; n' b  x, X5 g& a" s?>  C" A/ B; _' q+ Y, a# ^
在浏览器中访问:1 K- G0 F9 ?. m# E) M' Z

) f% ]0 Z' c6 a4 e& nhttp://www.52os.net/phpinfo.php?id=1 正常显示。' @: v$ W: K. n- m7 B
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
1 s( w  a* |0 X3 N* d* fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。! t. \4 E' o4 H
说明sql注入和xss已经被过滤了
3 _. ~7 N6 t8 j7 J. @/ o0 g9 ~! o6 Y6 Q# s
七、安装过程中排错
& l0 y1 }+ h6 B! d0 K7 b
# Z. c5 ]) H  W* Y  Y1.缺少APXS会报错
( b2 ~& o; ]" }6 _% @+ Q; z! q3 {  _1 r4 |& T! x) z
configure: looking for Apache module support via DSO through APXS8 V6 I8 T' }3 W6 |) E% M! k0 R" d
configure: error: couldn't find APXS
2 _" T2 `9 i4 t& B1 Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# @0 z2 n# [4 I$ s8 p7 H* h& X
解决方法:% n" B, h' O& x+ |- [1 }- W" J

" g$ V' J7 L5 }# T" ]yum install httpd-devel/ _5 }  M4 X  F+ s9 f4 ^
2.没有pcre
: S' [% n3 C; ^- s" o( u) ~: }; w9 T' r) Q0 A
configure: *** pcre library not found.
' j/ e- d$ u1 ~3 _8 Qconfigure: error: pcre library is required& R' N# N/ w* Y% e% F; c0 V+ T
解决方法:2 X# {. T. f  _+ I

6 D5 u2 p2 X  ~2 hyum install pcre pcre-devel
7 u' Y9 v8 r& o5 W2 y. o! T/ u3.没有libxml2
% `) W5 I8 A- W
8 P2 h5 v0 I5 ~9 `+ H; Y& L" {: M5 i* `! R- R8 A8 O9 ~! ^1 e! @; o
configure: *** xml library not found.9 i; I  Z- n8 C" w
configure: error: libxml2 is required% Q! h( ^; I5 @* i' |. U
解决方法:
6 h& s$ @9 B$ u; h. @9 w+ r; R) ^( n9 _6 L
yum install  libxml2 libxml2-devel
6 s: J% Q" _+ d2 j3 p4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 b3 Y2 i7 f* j$ q$ _; F! i' g; q' x/ L1 v9 T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
  S. }/ |) e2 [+ jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!. `! \0 B' W; A. u# r
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 K+ l- A( X) S$ `) C) h& ~

  y5 D& w6 S4 v: A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& a) M* K! V9 U( M1 ~: i4 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"# i/ P  F. v6 P$ G, b: V# J
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 T% t9 q! m1 C: J- p" w* _1 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- E. P5 d1 }4 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 B4 p4 l$ a) }& f1 f; W
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ U9 @/ D: O# U, V7 a
解决方法,移除低版本的APR (1.3.9)
. i& o; ^; L1 r) u6 R  X  f4 X  ~  M! T3 F; J+ g! m, g4 ~
yum remove apr3 T( v4 z* u* n& s+ S. J
5.Error.log中有: Audit log: Failed to lock global mutex
3 I- n0 ~* D, x- B! d; j
5 ]: q7 `  X/ Y; b7 J2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
; c- c+ Q0 A/ @7 J6 t2 Nglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& I  U& n# n% d$ i- ^4 J7 M
解决方法:6 d* M; T; v/ x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; l9 \! J/ k2 M+ {! W. n, ^; o' O  G) p8 g
SecAuditLogDirMode 0777  Z2 X( Z6 q: k; @4 H
SecAuditLogFileMode 0550$ ~, H$ l1 }/ Y( q  Q: d5 l
SecAuditLogStorageDir /var/log/modsecurity
0 d" Y9 h% o( Z6 ]+ ESecAuditLogType Concurrent
) ?$ _, u6 e: l8 g参考文章:
# \  Z' O$ |  R- ~https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 W2 I# D% N& q4 uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-19 05:03 , Processed in 0.067335 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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