找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 11387|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" x/ c! k1 y$ Y4 x! ~, O! X  {% P; f
一.准备工作) R5 Z, C( @5 E1 p; J: c$ l1 T

) ]; l9 P' Q2 A. L  f系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) }# T; I, q) i1 h; i$ Y8 _
& X5 p9 T( u7 p* }$ p3 l- d" r
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz4 c0 c1 k0 e" f% ^7 V9 W- ]

6 ?7 C$ i) o9 Lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. I9 g. D' K" B$ P7 U3 Y
/ u" v1 E( n3 ?- }- J3 u& j6 R& W
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 W0 P, [% }3 n) Z* Z6 f& L& k6 ^, k! M; H
依赖关系:6 D+ ?: |3 F. A$ v+ I6 c
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ c& ?2 R; h% T" K. C
2 l. y: b1 s% Z- R1 E. P
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
' u0 f+ r9 h4 ~3 f( C. Qmodsecurty依赖的包:pcre httpd-devel libxml2 apr4 ?1 O) D9 {3 F- O( U" s

# g' x) v7 d7 N- f) ^: _yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel. R* R) [; t& e- K& b
二.启用standalone模块并编译& U8 X& j, k9 r4 u  b* Y

% s& |. O3 q( R9 E8 t下载modsecurity for nginx 解压,进入解压后目录执行:, p  Z7 K( Y; ~

4 |2 z" y" C* l; g./autogen.sh
) j, f& j, g- w./configure --enable-standalone-module --disable-mlogc" m0 I. w- t* b5 f% h. G/ B5 N
make # w; O1 x7 |8 }8 Y
三.nginx添加modsecurity模块5 U2 L1 f' W, C" g$ N
( X. w: M3 n# ]4 C  r: n" P9 v0 |- l6 {
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
3 H* d' H+ `* l; Y6 T4 z2 w; U
, d: v6 H/ m+ D. S6 e# B./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine  R9 |. ?- f9 m3 Y# h! d" g
make && make install
9 U# n/ R/ Y7 ?% z; z+ k7 |- x四.添加规则' C3 a" {! q* F) _8 @# R
7 v/ F* z/ Z! D  e
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 d% Q4 k% B4 Z4 `; ?" w  R+ @5 g
; S8 P: ~. ]. `2 \3 D6 s1.下载OWASP规则:
$ m/ s7 I! h, J# |! m4 N
4 t5 Z; v  H. L3 m: dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 s5 `8 P: x0 o! j( }3 t4 _
$ b( }9 @' t0 G/ v
mv owasp-modsecurity-crs /opt/tengine/conf/5 m# X: K" G5 M

: [. J' E0 o8 @! ?, Z/ q8 e. ^cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" Q* e' E8 d8 V8 Z  g: ~7 x
2.启用OWASP规则:5 f7 p  d( b+ `1 }) p# _" Z0 P' {
$ I9 l2 j7 @# ^$ l
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
7 ^% w7 C& S7 z. ~
! l3 f+ d/ O3 d- n% i2 W编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ ~& X) v0 q' v
2 o0 p1 G1 c( p- a& q5 b6 d/ ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; Y+ F* l( f) a0 K( C* r* j  ]  g- y& C0 T% m
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- Q: D, H" l/ rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
: k  L4 A2 e& U2 Q  wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% Z! `3 B- z: ~8 X" u, I1 `) M/ GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
  c( k9 p5 g0 q2 c9 IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 _: O" S/ B$ O$ EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ u2 P7 y, E7 l7 W/ \1 o8 T& ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! h2 I6 p! J/ g
五.配置nginx( o+ Z" D+ w8 @; d% L$ i
+ }3 k9 C" q7 l4 q
在需要启用modsecurity的主机的location下面加入下面两行即可:# p3 I; i5 d7 R8 g! T# S8 N
$ n; g1 Z: @5 x3 z4 ]0 a
ModSecurityEnabled on;  5 }% k7 [/ K: R8 K9 E  O5 V; H
ModSecurityConfig modsecurity.conf;
3 V( ]; M" ]) z- a5 M4 G下面是两个示例配置,php虚拟主机:8 R9 D3 e) x4 [

1 x; L; q7 z+ V6 pserver {
3 _  i; m. k8 o& v( S. W  E- w      listen      80;
8 }0 I/ ]/ e; L6 A% R- Z      server_name 52os.net www.52os.net;
$ l0 k+ ?+ A, m2 w     
3 v6 Z; Z! |' v3 {      location ~ \.php$ {4 I0 u  l* f. A& ]
      ModSecurityEnabled on;  6 d6 I4 _* l  l. Y
      ModSecurityConfig modsecurity.conf;
$ g- y; F2 |1 V4 |5 |& M8 Q2 H: b9 m3 M: c
      root /web/wordpress;  f4 c8 H& N4 J8 q/ i# j
      index index.php index.html index.htm;
( y; W, p! s# n! }; T; E  , q1 \* R% S0 \% a* z" p. k
      fastcgi_pass   127.0.0.1:9000;) ]+ T, `6 _* X# g) a9 d, \
      fastcgi_index  index.php;
# Q* o2 F; u3 {  t# S# X4 g      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;. [  |- L5 F  b
      include        fastcgi_params;8 V$ r" [7 c8 F2 k
      }5 l$ R9 a# ?# q' c
  }( Y& ^5 ]! P$ U2 |, ]
upstream负载均衡:
& B2 W$ w8 P5 U7 u) F  i2 o* b( x6 x
5 C2 E5 q. b& a+ H  F4 V( Q! nupstream 52os.net {8 D0 V; F' i1 p# v8 D9 G
    server 192.168.1.100:8080;
5 D  o9 H# K+ U4 [+ r( r    server 192.168.1.101:8080 backup;
7 l$ ~5 ~3 x5 ]0 }) i}- w) i* P( ^: [$ r( ?2 P+ e! `! c  v

) u: v. k7 O0 o/ B/ C" Oserver {$ X2 U, m. V+ m  D
listen 80;6 p  Z# c5 W( o5 |
server_name 52os.net www.52os.net;
( X6 _' p" L" z! p  r" g
) k6 ~$ L# i. @8 N8 y+ O( ilocation / {- k5 ~9 A9 {$ }, s- T1 d- s
    ModSecurityEnabled on;  
2 _7 m( n; E" s. B    ModSecurityConfig modsecurity.conf;  4 j5 p  U. L( D3 T
1 h8 q) i2 S5 [; ^3 K+ x
        proxy_pass http://online;
% k+ e0 K; ^5 I$ o& t6 `$ U        proxy_redirect         off;
' l( f  E& Q7 K9 P$ d        proxy_set_header Host $host;2 {1 u4 w  x1 _3 ?
        proxy_set_header X-Real-IP $remote_addr;
' O8 ]! r$ j& @* Z% D! \" a$ d        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
, A( d6 a- ?- e7 }0 t! p9 m) R& T    }
' f5 \: C; O. ]1 V}  P: }; m/ a+ ^4 J0 I0 h* h
六.测试2 d9 p4 H& {! n: v

3 ~$ u0 F5 q- Z9 n# k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:  K6 Q" [2 F( n5 k
6 ^# P7 T/ L! S9 o, G
<?php
' c" V4 `, r& {7 j0 k6 L6 i    phpinfo();    1 G$ s5 _  G" U! r6 z
?>
( V: c  l0 _' u5 C8 d9 j/ Q* _在浏览器中访问:  b, e# A9 E3 s% L$ E; Z/ y/ ~& r

, W9 b& X* u6 s; v( k/ ghttp://www.52os.net/phpinfo.php?id=1 正常显示。1 |, ?: w( g" F! u6 _1 i
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。! ?8 b* V) R5 O& [
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' i- G% A3 `/ Q- U) T) W& @& q
说明sql注入和xss已经被过滤了' q9 p* f* @2 @* Q+ G5 h0 H
) g6 A/ _% o, A  [4 f+ D
七、安装过程中排错2 L9 Y; B' y1 G# e7 g7 f
: h6 i/ u# t6 \- ~% c* N
1.缺少APXS会报错
3 ^0 `+ K, q: n) m) W3 T, ?7 A' \
3 h, m: u8 w5 u, Uconfigure: looking for Apache module support via DSO through APXS0 r* x2 P/ m/ W7 N3 d5 p2 c
configure: error: couldn't find APXS" [0 F+ R6 x2 k: s
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, p9 x. ?$ \3 L+ q2 P2 h4 j解决方法:6 J! o  R# L, a2 J4 ^

6 B2 q' T. N" jyum install httpd-devel" k: q- C6 `7 N
2.没有pcre
+ c0 [' }& B4 ]9 K, i
4 w! R& `4 u, q0 f  Wconfigure: *** pcre library not found.
3 R8 k0 P$ y1 p- |$ b2 g+ f: E7 Uconfigure: error: pcre library is required
! n  \  `* Z. |解决方法:
5 [3 M( L1 Z) z8 R- ~. \+ o2 n' Q' G- c4 z; B# q
yum install pcre pcre-devel
/ |' _$ g, {% Q0 F, a( V& [% ]3.没有libxml2  }2 @( @* k* r- @, `& A

% i. C0 J% \5 \- W: d4 P  b+ c
7 X# c1 ^: C2 X8 G. h. ~! r0 tconfigure: *** xml library not found.& C% U6 E- a0 g
configure: error: libxml2 is required
2 j" T3 \6 v/ M% `解决方法:
2 [" Q! ~* v, _+ }1 y) H% c# Z* q$ g6 i0 O9 u
yum install  libxml2 libxml2-devel
. V5 m  a2 k( {: M$ A; J4.执行 /opt/tengine/sbin/nginx -m 时有警告
  ~  M. ^8 F6 Q( |& H
/ R* Q! m7 T, qTengine version: Tengine/2.1.0 (nginx/1.6.2)
6 G/ j" u, T" k( T$ L. X0 Knginx: [warn] ModSecurity: Loaded APR do not match with compiled!
1 m$ E  d* |* x5 |原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, A* q5 H* N7 A4 ~9 C* @& E
5 M" X" R! y1 F- \5 c+ S" Y  F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, K% I# Q0 c2 H! U8 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 S, S2 K) ]. p2 q8 g- V2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
  a7 b5 N8 @* E. A0 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* v* z5 i$ b; ]3 D+ m& m1 h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 G3 }. u8 Q: J5 @' E8 q6 ?2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 C# ~* a, b& i解决方法,移除低版本的APR (1.3.9)
, E$ A4 H  [% |5 l! n3 l3 h6 N1 u; w) h% X  a
yum remove apr/ N0 Q( r% E. V( ]
5.Error.log中有: Audit log: Failed to lock global mutex
/ _" j' `% r7 l+ T( H' I9 ~
! m, a( o2 S  n; R+ W1 P2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
0 k% {; U: F" ^+ B5 |global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 N* |. V( m9 [( x$ A# [
解决方法:
+ e0 e& P6 H/ \% S) |& d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
! ]$ ^/ ^* ?1 B( B# \' }
) R8 {! C% Q, W2 J6 oSecAuditLogDirMode 0777
/ {# T/ ]2 |* g1 J& Z* X) |+ VSecAuditLogFileMode 0550
, r1 v. \7 W5 q( A( rSecAuditLogStorageDir /var/log/modsecurity4 P4 l, ^6 x9 z* M
SecAuditLogType Concurrent
$ w& w* K  W1 k+ R参考文章:" O& H" M* _  e1 z1 h0 i
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- G) g3 S' H" l1 ]& D5 O& Uhttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-6-14 14:58 , Processed in 0.065852 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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