找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9220|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 u7 B  x4 ?2 T$ V. \$ b
* J; o' U3 b/ c, s- U一.准备工作
: E2 u" s% t* Z& g: l
; C4 M. `$ r. K9 P0 I系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 p, W& b- t4 v4 n9 u/ d, D6 Q! w) }! I# m" Z6 U1 W
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 t( J! P6 A  ?2 C+ o6 O8 B4 X4 m

" _' D# N& b8 bmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
% K& _6 @& w7 y: n. J4 S
* o1 a& G8 V  [$ J' o7 nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& ?6 S, q  \# [3 ]8 w, W( B* l

" b" J  M- y7 a* `# W依赖关系:
$ j8 v- p5 ^( M$ ^& c2 L/ Dtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
1 |7 w& P% W# W9 {# Y! X6 [  l5 E' F/ A
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel' I6 a" I7 W# E3 w# ?$ T
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% a. C+ p" |. x4 o, I7 M
! \+ \9 Q( `' Wyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
& m* d. A1 ~1 C5 Q二.启用standalone模块并编译
; m& S8 |/ C8 b8 v0 b  _' E. L8 T5 m) l* ?
下载modsecurity for nginx 解压,进入解压后目录执行:6 I& ]& e* W& W0 o2 u

+ z5 A3 d7 s: `: ]( j: I) }3 V! d+ Y./autogen.sh! ~+ \! ?$ o+ W/ R2 T% \
./configure --enable-standalone-module --disable-mlogc* J4 s8 S( d. q
make
0 R; k: B$ a) ^  S! p8 a9 j7 N三.nginx添加modsecurity模块% @$ |& a( L- H9 s8 B) Q
* O/ }% H( _+ d& _3 \: |2 }0 E3 h- U& w
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 s, A2 r7 q. @7 z6 W
, C2 e& ]( N& g6 m" L./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine: N6 Q) T/ ?6 V! e( M& o3 j; g
make && make install/ h* L/ w, q2 b' L
四.添加规则
2 ?, N: i& t* h# U9 y2 r
* M; _: g; j! a1 g1 o7 |9 [$ Nmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ }; w: i% g' B$ H+ M

( R' [6 Q4 m0 f1.下载OWASP规则:: D9 n  A) _+ A  v; f" x# Y

/ g6 p. m3 D0 y& i. @  m1 G6 ]1 f. z4 xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 \' ~9 W( o; ?2 }/ F
; ], K$ F! K. T$ @+ T; y& ~  X6 Umv owasp-modsecurity-crs /opt/tengine/conf/
+ m) m+ x1 t: ^# f; T3 P/ I5 Z  V- @+ ]' O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 c# f" \  @& f
2.启用OWASP规则:
6 E; I1 C- k/ N( N1 L6 e" d7 L( N1 ]+ [  j. e9 A
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 U. q6 ]* y% ]4 j$ v+ k8 F
3 e! C  r! v, O; U2 a
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" f; S1 x: e# S: U
* \1 L8 @& x3 g3 m- |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 D' \: G: L2 x2 O( o3 {- V& x
- d: J: d  G8 k$ r6 k3 |/ u
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 J$ Z: l3 ^, E$ O( k4 b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ {! {9 l; C& m& O4 t. y6 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
) O; M0 n" d7 j$ VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf9 N8 q8 U6 G  C! _, o4 a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf1 A  `' p2 N! {9 d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) V) l7 W3 m% u: U6 v6 h" lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf6 I( D& ?3 ^0 L2 A8 s% i- Q
五.配置nginx
5 b6 c6 `# \  B) L8 r: R; i+ P; _% R
在需要启用modsecurity的主机的location下面加入下面两行即可:
, [2 m  u  T" u1 H9 m- `
) N- a8 V$ a  L! @4 X8 J; YModSecurityEnabled on;  
5 O; ]- u! D; R! X: j( DModSecurityConfig modsecurity.conf;
+ }9 u+ l' }, O; n& f9 r下面是两个示例配置,php虚拟主机:4 `0 q$ L& W# V
4 ?# U: i; o4 h$ ?3 G
server {2 ?" v# |$ `- l  q! n/ S  a
      listen      80;
' ]$ y& {/ r" h! d/ k, M9 D; y      server_name 52os.net www.52os.net;
9 \1 q: I; a) P; C     
& c* G2 y4 F7 K: y9 ]5 [# A      location ~ \.php$ {" g7 M3 Q5 A! X& @/ D3 v3 z
      ModSecurityEnabled on;  
, [; `8 q/ S* V* s, {; X9 @      ModSecurityConfig modsecurity.conf;
6 u& h6 v  n, L  J& O
7 [+ @9 k7 q5 {2 G$ F      root /web/wordpress;
, g1 }7 c$ Q* j* s: L! [, R      index index.php index.html index.htm;. j! m5 E% C" l1 B+ G
  
% a4 a' u8 ?9 t) }- K      fastcgi_pass   127.0.0.1:9000;
4 h  h! k' K9 R+ O- b      fastcgi_index  index.php;
& w7 `# i' w/ A* z! X      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
8 c, C0 O/ {* {6 O/ x+ n  ?9 Q2 ^      include        fastcgi_params;9 Y4 S+ N! d2 f; O+ K. Z% l* h$ N
      }8 M3 f9 T) N4 k- X0 {
  }
& Q8 [( h9 z; i/ {, `: l; Bupstream负载均衡:! X4 l; a3 ~- {. C  c

) \) j4 b) U, t. ~$ O9 [  i: fupstream 52os.net {4 M2 R4 x4 m5 q6 ]( ^
    server 192.168.1.100:8080;
3 a+ I% J: \* T% v4 P    server 192.168.1.101:8080 backup;' w/ O, G& P% Q0 l1 x2 V- `
}( o: v' F' }1 o& [1 b* j2 r
' ~" r3 _8 b: i8 ]
server {
/ X0 O' X0 |$ hlisten 80;/ z+ J/ I* a/ W  p
server_name 52os.net www.52os.net;
) ~& g" G2 X( E" f! U+ ~3 A8 Z. [3 @$ o
location / {: m% D$ |0 @9 `: X
    ModSecurityEnabled on;  
9 H- e, N$ L. A' n6 o* o    ModSecurityConfig modsecurity.conf;  
- u/ o5 c( ~* ]  ]6 H9 L; }$ \* @0 v  f/ u8 d0 n
        proxy_pass http://online;. A3 Z& c. d$ [- a
        proxy_redirect         off;$ F( Y9 ]1 j6 E- i
        proxy_set_header Host $host;& Z5 y7 y$ C7 i7 j0 u7 p
        proxy_set_header X-Real-IP $remote_addr;' b8 P! J- u2 \. Y
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;! d2 f& B  t8 h) I) b  T+ s
    }
2 S2 E6 n2 @5 Q; L6 B& S}
% c2 R8 q  H! B六.测试
9 V8 Q$ C# H6 F
  A1 L5 |" w, T/ }" u我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# {6 N, b9 p- c, V* p6 \

% ]3 r/ H6 k" e9 a5 c+ K<?php! l  E' ?& K9 s0 e# t) @
    phpinfo();    8 \1 Q+ U7 [1 h$ z  i& X; C
?>
) y6 [* b: L3 w5 N! |在浏览器中访问:
9 i' e1 c  i9 W+ l! N) W4 [6 p+ [  W
2 z3 D/ z9 D, y5 ?6 D) Z/ Shttp://www.52os.net/phpinfo.php?id=1 正常显示。
2 a$ V5 A, t4 X2 t( @  U% jhttp://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
  ]6 X& o: G" G9 `* Ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。5 G7 d1 {  E1 e9 G. T% P* a. S; S2 }: Q/ ~
说明sql注入和xss已经被过滤了6 |! j1 V. b, h6 Q# P* O+ m/ o4 J
! i* p4 F; ^6 v& V8 @, E" T' \& |
七、安装过程中排错- g2 {0 }- v! ^7 p1 I

& u1 [7 t3 b+ @% \: Z1.缺少APXS会报错
9 J% `& P, \3 h" |8 A8 K4 a4 f% B. N
configure: looking for Apache module support via DSO through APXS5 B  b0 a) p+ U+ j! z
configure: error: couldn't find APXS+ w! m# C7 h, o# o4 B
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 N. B  M0 z5 i& i; e% W2 w
解决方法:, t' N; k( t$ }' L5 n
' |2 ]/ M+ z$ o; I7 H( I
yum install httpd-devel( j6 i% W8 B' ?; a* M( m4 S3 A
2.没有pcre
  R+ `0 W9 `) s6 x: g( K6 B; J' o/ }
configure: *** pcre library not found.
/ t/ K/ b/ b! g# `, X0 gconfigure: error: pcre library is required
/ g( W6 Y: c/ p. k解决方法:1 n7 b' g1 k1 Q% `4 S5 q
( F+ w; V; D/ o( r+ \* F* m
yum install pcre pcre-devel
$ W8 g9 z; _5 T: j# \3.没有libxml23 b4 ]3 {0 T7 }1 ?8 f4 z
6 O8 @/ r8 w& d
" I6 j0 N% f6 j( h* \
configure: *** xml library not found.8 d) B7 A* B6 r! y4 S
configure: error: libxml2 is required
" \! h$ k, r. S. g解决方法:
2 q" X! g8 c; R1 a& B) q. r) F! D
7 R' F( m) c+ I1 d/ Q# L; [yum install  libxml2 libxml2-devel2 M/ X' ~6 T  Q) {% ]$ m
4.执行 /opt/tengine/sbin/nginx -m 时有警告6 t$ V% S9 g% a5 [2 ~# f
4 v3 R  ^5 f. k6 }
Tengine version: Tengine/2.1.0 (nginx/1.6.2)8 |( _. a% f. n  p, }% x
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: |9 Q5 g+ G* v# d& Y  N
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log  w( z% T6 l- M% {4 d

( @7 o6 I2 p0 Q: R* a- |- E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 s0 ^0 J& e3 j- b0 y/ Y7 E: }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
! a. t% Z0 @" c) A0 q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
1 B8 o6 o' K# j3 Y% f4 C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"! E9 T" P* U3 A$ Q0 d# ^  |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ q8 r& L  c- V- \" }$ ?2 I
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. G3 X# O! o/ B* F
解决方法,移除低版本的APR (1.3.9)# T- K3 @- ]* _9 Y& f

, v+ r. @% G& d6 W2 Lyum remove apr7 n2 O% `" I$ x' b7 j2 ~. V
5.Error.log中有: Audit log: Failed to lock global mutex+ s1 F' `0 ~% k; |* l5 ?, f
3 f7 t; a4 v3 S+ r' y3 c) u
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
/ L+ O% F& s& ^& P4 v6 Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 F# Z- R5 }8 M( |% x6 v
解决方法:
6 k% c% B, u( h5 Z: b" }编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* U/ _% E! n5 X( }2 d7 V3 ]
0 {: D% I. B: Z, w' i2 bSecAuditLogDirMode 0777
! ^$ I9 }0 m/ h' s& _% gSecAuditLogFileMode 0550" D! r+ C  f( }) z4 n' ~. J6 J
SecAuditLogStorageDir /var/log/modsecurity1 [0 R# S& \( l5 D2 I! e$ M' W
SecAuditLogType Concurrent& a1 e3 {/ s. F/ j/ w
参考文章:
$ t0 n) l. O) l5 V- x1 E; I0 ~; H7 bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 S% I. \2 t5 Z. p' w, _
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-11-27 17:47 , Processed in 0.053829 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

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