找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13167|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 }  F: V  c+ W% E
3 k' U/ @6 p/ o" W. i
一.准备工作
! P# y# g. _  A8 e7 e* q
& i/ I0 o; B( p2 m; m% O3 J, X4 O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- g7 S( E6 |% q6 Y
0 U7 E/ ?" ]6 @: X' a% _
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* t" R: O7 i& T% j1 Z# u+ T/ ~0 N( `3 e1 ]- W
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' q) X2 T* H& k

3 i- o; B- j) l  m0 b  HOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
  h6 p* x* i0 c
# O$ I  q" M" k- v9 [' u# E& f6 U依赖关系:
: ^1 @& h& l8 d& h' u  ]" Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:$ W& r" x: ]) T: B6 @6 J

* ]: |4 N" J" Z# T- d2 S3 o/ yyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel5 T0 Y5 Y; O2 a1 A. L/ [* q
modsecurty依赖的包:pcre httpd-devel libxml2 apr: e7 \# \% c1 \6 _) X% ^0 N" }

9 ~7 u& w$ f4 D; ?: m: W2 r. Vyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
1 D7 X% j" Y0 m8 P$ s) Z二.启用standalone模块并编译
8 p7 l9 X# K0 A% \; k2 ^* r3 {! K1 W4 L' p
下载modsecurity for nginx 解压,进入解压后目录执行:, |6 f) ^7 O0 ], i
* T5 i- N9 y) n0 k: z
./autogen.sh6 m! z  A4 Q6 r# L: o8 ~% J7 ^
./configure --enable-standalone-module --disable-mlogc
0 {1 z; x* o: E: g) }make
9 o' q8 A( G; B三.nginx添加modsecurity模块
8 B/ @6 Y+ e- s% F( o8 o( P
( d! W( p( n0 j7 p/ y) ]: I在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 K% o, x# H& v1 k9 j4 S5 e; a5 ]+ ]

7 U% O, t( E9 K5 e+ N$ M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine7 Y0 }$ S/ {' l) U
make && make install& }# @0 k* }) o! w9 \% T, Q
四.添加规则
( }5 I: {6 |" D! z$ s9 W/ l& H( l9 |7 ~3 `% a/ g: N
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
) {) J/ J  q3 @& F7 s& j2 a/ z. x7 b3 b) w
1.下载OWASP规则:; a7 ]+ Z- O/ @0 v2 x/ ~2 K. D
* p/ z  Y: L! L( N
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
% o! z& f- Q% ~: W3 b4 p" n5 [: V2 g9 p9 i
mv owasp-modsecurity-crs /opt/tengine/conf/# l2 ~2 H% ]/ D% @( @2 D

, P9 C- b1 E% Wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf7 y) r4 B$ r0 Y, k' K! C9 x
2.启用OWASP规则:
8 U' ^; L+ b* B1 ~9 K
( {  N1 T. `  Q& W3 n) s# t. I复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 J  p  Q3 O2 ~8 A3 Q0 a7 r$ T+ K% f+ N( i7 l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% D5 T' x( w8 i
. E5 I, ]9 k0 j* u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。' s) G- X, ]% U) V) m

6 y- t) d- x2 n9 G, Y& yInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
$ b% K( L* N  Q1 zInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 v- q3 G0 |3 ^- B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" c* j$ Z6 q" ?) e8 o- F; m: ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 n! R2 [6 q+ |# }2 z3 ?0 Z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) t9 T& ^4 H! n! T5 }( ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 R. M- Z5 p1 \4 U- V7 [- Y$ f
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% m# C6 p# m7 G7 e& H5 h
五.配置nginx
* E& o! J4 X5 v8 l  o. s- b  C4 Z$ D0 E" j/ P
在需要启用modsecurity的主机的location下面加入下面两行即可:0 ?$ u! u" a# u" p; U% L
' O5 ]" S9 F* v. E" _2 G
ModSecurityEnabled on;  
5 Z6 `! a! F3 T6 V& \- r- OModSecurityConfig modsecurity.conf;
0 [, H, Q- x; Z7 r7 a4 s% I' c5 v下面是两个示例配置,php虚拟主机:6 Z3 t$ z! F4 A, Q7 n, u

/ p( k- |+ s0 J+ H4 X" v1 Nserver {# O" a. z* E& r
      listen      80;
* j, ^: ~  |6 l; g+ f0 E. r      server_name 52os.net www.52os.net;  M' q* P1 b" i# K) ~: u, v
       P2 x" w0 e- K+ Y4 l
      location ~ \.php$ {
' t. G$ o& P3 j0 W  |8 _) u5 q' K      ModSecurityEnabled on;  : h$ q0 r" }3 j" l
      ModSecurityConfig modsecurity.conf;& S7 U! _: y6 _9 L. R
1 l0 Q  w$ ]7 h8 P
      root /web/wordpress;0 R3 g/ O* G. T$ J
      index index.php index.html index.htm;
9 A# g+ M, m6 t; `" i1 a  , a  x7 G; _: f' z1 V4 C& P
      fastcgi_pass   127.0.0.1:9000;0 b+ D2 }4 T5 r" p9 ?) x. d
      fastcgi_index  index.php;. U3 D3 }; p- L2 ~7 i  J
      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
/ Y+ A! ~+ N- r, e' \      include        fastcgi_params;' }( Z1 r  q5 c. w6 j  {. u) z
      }$ {: \# ^  K! }& ~6 J
  }) L$ r; y0 ^) s: s- k
upstream负载均衡:
1 E0 A) a8 g0 U( b$ C4 [5 P
  `, O" c. z- U: O0 {upstream 52os.net {
" {6 L4 Y6 W- j5 T0 p% x    server 192.168.1.100:8080;+ h+ ~2 p5 p# I# D/ v7 U
    server 192.168.1.101:8080 backup;
% E  p9 `8 B) I: P, b  \; f' j9 l}$ Q" p5 Y4 {0 n* r, b* m! f5 X5 b

  z' V% g, c' Gserver {
! K: N- \- V* s' I& klisten 80;/ ]& R( C9 _5 t+ {7 q0 s
server_name 52os.net www.52os.net;* i* k/ `+ W8 T/ o
1 N4 a; {, ?& }, L- R( B
location / {5 F2 ]  X0 _7 D
    ModSecurityEnabled on;  
6 b. V# v( X! V' K) b' ?# }    ModSecurityConfig modsecurity.conf;  
; _1 p+ Y+ w3 U# T7 A
9 ]* P3 }) y" d9 G        proxy_pass http://online;- h* \2 M/ a& w2 y
        proxy_redirect         off;
6 U: O/ ~( n! o9 @  G        proxy_set_header Host $host;
* Q: f0 b. l% S' K3 M) A        proxy_set_header X-Real-IP $remote_addr;5 d' o$ T( \8 @) T3 d4 _- w  s
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
, F* Y* e! Y) A: |6 d* s, d. v    }' k; w8 a* e6 J: A. E7 _' S
}$ Y' d- q$ X1 Q  _/ \% `: J, S! J4 @
六.测试
7 M7 p( @# W, B' Z3 K, D; o
1 N6 v: V0 J7 M我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* x3 B& e8 R4 D* D+ q( W  {. d7 S6 R- W5 Z& v8 l/ Y
<?php$ B3 E% U" k* s1 V! m# V
    phpinfo();   
# B- v+ A( B# `% v6 P1 O7 [  x; t?>6 U, K7 ~( l1 G( p" b
在浏览器中访问:
9 j+ T- p8 q+ ~2 A( Q; J# x0 c1 }( H) J9 w
http://www.52os.net/phpinfo.php?id=1 正常显示。( }; T5 ~  q/ Q) Z7 T
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。3 e$ D& f* U1 M
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。' r2 v  `. `+ G( V
说明sql注入和xss已经被过滤了
% m" T5 L3 d& \% F. L4 x# i' q$ L  Z  r& r) d3 p1 F- H) |
七、安装过程中排错
: U* m; ]* Z, W: e9 |& L2 x) _" B4 S9 X, N7 e+ y( Z& h) q5 F
1.缺少APXS会报错2 T, Z$ U- V5 r' ]$ X

* e  s: p- f8 M" }configure: looking for Apache module support via DSO through APXS
! g$ n, q! U& d( i6 Gconfigure: error: couldn't find APXS
- P* D2 J2 q2 M. V/ ?apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& O" a0 e: {4 j. W) j& f8 |% I
解决方法:4 ]& I9 |. V$ d0 b) y
, y7 Q! s7 P9 \" P! c
yum install httpd-devel
6 R% f, }' ]$ F) |* o/ x4 l2.没有pcre1 S5 W3 a* k; `' E5 U
  ^. j9 V9 K9 f6 I0 k# Q# s
configure: *** pcre library not found.; t6 |0 X5 g$ I0 T- |
configure: error: pcre library is required. O' l9 u: o6 L) k6 b5 L! p2 K
解决方法:
; P& ?0 V& J9 f4 m/ b/ V4 _5 n
% N2 i1 C0 f" H; iyum install pcre pcre-devel/ i+ i) D+ H/ @7 k6 w, W
3.没有libxml2
  ^+ ]( w- G! w
5 B: P- R- x7 y8 `# C. l& G9 S; Z
8 ^# K, e8 e8 a/ p) F  \/ mconfigure: *** xml library not found.( p8 ~1 Y: O: k! h( h+ g* H
configure: error: libxml2 is required
4 i, E# y0 Q) ]: K5 m9 j$ R解决方法:+ u7 I6 i  {# A: Y* Y  k
0 f7 B: S  T" y( @7 ]6 H& L
yum install  libxml2 libxml2-devel3 q+ ?. m2 x3 e) M2 w
4.执行 /opt/tengine/sbin/nginx -m 时有警告! ^9 u% E1 B9 J- u

0 H1 b: F2 p1 s/ r& I0 Z, D( c& mTengine version: Tengine/2.1.0 (nginx/1.6.2)
& N5 h+ q; M$ a  l0 m" hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 Q5 R' V5 L6 C原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" v" H! h" L% P6 Y+ L  g# B
* n  J  `5 f) }: s% x9 N; q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! C$ y* S) K3 [& b7 T8 I4 `  T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"' u4 r: B. P- S
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ {- H1 O+ H5 d0 N9 l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". d  G* A' F% o& ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% M' ^9 s0 C; x# Y5 D; I
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 c& R: e9 S9 t解决方法,移除低版本的APR (1.3.9)
8 v3 ?# s& B0 s! x" Y/ B# S! f2 C/ R5 ?
yum remove apr# o. E8 D' h8 ~5 J& Q* ^
5.Error.log中有: Audit log: Failed to lock global mutex
: x( X/ X" q, V! u  ~9 f9 [) S5 K& z6 \& O: b# H+ K  [6 K
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
  d( E+ X. i$ x' ?- N8 ~5 I3 Rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& e8 c( ]7 H6 h3 c! {解决方法:: }' l1 ], [# |+ {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 z& |* U8 r& T4 v

  z/ }8 O1 t5 E4 r( [$ J% QSecAuditLogDirMode 07777 q1 g" |+ G2 e8 w
SecAuditLogFileMode 0550" D% V. Z' O2 r( h1 ?
SecAuditLogStorageDir /var/log/modsecurity
+ E( \, ^5 |' m% ~. ~! jSecAuditLogType Concurrent' N3 e2 x+ P4 F9 o
参考文章:) T; a0 z% ~" e- X% Y& s4 o
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 X+ S) `& v7 G" a4 l9 ~) F& u
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-20 22:03 , Processed in 0.056672 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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