找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12157|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' h. [/ B) t* T  B( L3 x: a
. c. g- C3 c; |' E5 }' @7 q
一.准备工作
. [' N2 e- i* h8 Z& {4 t: H; B/ V: r' s, Y+ y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 h; ]/ S8 s  v5 ?& P4 a9 @, Q6 l4 G4 I- G: e) q: e
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ J; {2 r2 ^' ~9 D8 P9 i* v, s% a. q9 o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, v$ K0 c6 V* C( @( u% H2 r0 R
1 |% o0 v; w1 h% V. S4 ^" ^9 T
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 C9 D; d2 a; q1 N2 h' A( c) D4 U& Q( P# h+ p9 r6 B: S' t0 o
依赖关系:5 y' h" O9 S& |& E- c
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 _% t# p' k6 t, r: `& _% n* A( R
( B0 y8 ^8 s$ G' B9 S4 T8 {4 Zyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel8 }7 H" O& }6 \' I
modsecurty依赖的包:pcre httpd-devel libxml2 apr% m9 ^5 }8 A' f+ @
- U* [& U3 p3 N7 ]3 ]. B9 @7 A
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel8 j% e3 k3 V$ e; G" T
二.启用standalone模块并编译
4 h: e0 S1 g: x8 W
% z9 J# z& T7 q下载modsecurity for nginx 解压,进入解压后目录执行:
) p( U* J! s7 A! P6 W& c1 D! ]$ k# d& M: q# h0 J
./autogen.sh
/ n- W6 ~1 i7 E# S1 L. H./configure --enable-standalone-module --disable-mlogc
( r: O+ {" l0 w! mmake 3 `$ _" \$ Q+ {: a# \  ]# r# [/ U9 H
三.nginx添加modsecurity模块
$ m$ c- o+ f4 n5 B5 \0 X2 j' r( I) A$ T
% z. m. n: X% [! q! c2 G在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 \1 P1 Z+ ?! F" X; R( C( o* R0 N( A6 b2 F, s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine! q, l( R) h0 b
make && make install
0 D5 C$ {7 T$ E. N四.添加规则
" p/ @- y, ^& q! `0 C
6 @* A* p, g( n' O1 z$ P, Imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
6 c. O5 x' d) T7 \
' Y- z( e5 L# R: s- Z: w1.下载OWASP规则:
% j5 j) ]) @% I2 @3 K
( K; u% F" h, d6 X) N- i9 U( Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs! F' B. b% X( e% r! ^
/ x' U# u# I% g8 o# W
mv owasp-modsecurity-crs /opt/tengine/conf/
. c$ u5 }2 f' M2 g" ?9 z5 M
1 P1 A* f& Q9 q( pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* D. X: _2 e) B; }. v- K+ p+ w3 w* {" r2.启用OWASP规则:
9 \7 N/ H8 X1 s, {! S6 k9 h$ G6 p# `7 Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。) g7 {3 y$ f. E+ r  i* p, M$ T; X3 w

/ D4 D+ ?) a! `/ K, s编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ s4 y* z& `. `' ]
! b& {7 o5 K! |) c& Mowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
2 ^- Z3 x( ~3 Z. ^0 D. d) y/ t
4 v  `( o" Q, T8 P, n! z( R1 b7 SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; l( p$ Y+ Y7 H2 i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( h, m/ k" D' j) l* _: v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- b0 X. u" J# ?0 JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf" K: Z7 j( D$ I! m9 W6 Z9 v, P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& e+ d7 F9 [1 W6 \' mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& n- l; G5 ^4 P! eInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 h, d* j) {/ r! ?
五.配置nginx* o* ^$ q+ `% A0 }5 X% u5 r5 o

& T5 s4 e' s$ [2 e在需要启用modsecurity的主机的location下面加入下面两行即可:
5 f9 V* v7 Q" O; y# ]) J7 \
9 C, ~; A1 k2 |" gModSecurityEnabled on;  1 q( m, q- a7 q, v/ X. k
ModSecurityConfig modsecurity.conf;$ W" R) d& [4 Y# X
下面是两个示例配置,php虚拟主机:4 \" B: i7 G1 W6 b

: x! U3 G% g' B1 o8 I2 Zserver {
: [' z- e+ `# _8 n1 p( l2 D      listen      80;& l5 }* `# ]! N9 w5 S. e# p
      server_name 52os.net www.52os.net;' a7 {) f2 o9 p1 l; ^& C4 l
     4 t2 D$ L! a4 L* g# x
      location ~ \.php$ {7 \: A  c. }; g) j2 j* D
      ModSecurityEnabled on;  
* j: |( c6 b8 @% v: M8 \7 r( k      ModSecurityConfig modsecurity.conf;
' q1 l4 [' h$ o: S: g2 I3 c% y/ u( c
% N; l( H+ y+ @2 N- b" d" l  B" Y      root /web/wordpress;
" [5 M2 Z  }. c# Q) t- S      index index.php index.html index.htm;
! \- B' l* w3 ^0 U  
; c: Q* @% O1 p" p+ \/ l1 J: ]      fastcgi_pass   127.0.0.1:9000;
. U; J3 J; c9 o4 x! Q8 |5 b& r      fastcgi_index  index.php;
9 _& d8 o$ l6 p# L) B      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
: x0 {* T1 i8 r* f      include        fastcgi_params;
/ p1 }" e& N  ^  O' Y9 E2 ]- e; t      }
# S) U, |4 t. Y5 t9 Y- f  }
: O4 Z- c3 {& I0 {7 q4 p% Vupstream负载均衡:
. d( }) \8 i0 Z5 n, f! C% g) S" H: Y+ }  z
upstream 52os.net {
+ b( ~/ A% X, g- ^    server 192.168.1.100:8080;
7 K! |: M; Y9 W  I- {    server 192.168.1.101:8080 backup;( z0 ^1 I" T& e
}# P. S: H; J0 V6 H( ]- y
3 p4 B2 a0 b  v0 o; |
server {2 H9 f& n" ?0 ~' [& A/ I- O% X9 Y* J' d
listen 80;
1 |0 k. ~1 q1 W& p9 pserver_name 52os.net www.52os.net;- X3 D) Y/ Z7 P3 v; E; Y3 s* c
2 ?6 }+ A  V; b
location / {8 e! d' |% d1 ^. K, q
    ModSecurityEnabled on;  
- r9 A0 S' S+ g; b. O5 \( m; m    ModSecurityConfig modsecurity.conf;  + ^& n* ?; o  |! e2 `
( |, K; W5 G* \2 b) b
        proxy_pass http://online;& [( X1 M6 b* ]1 x/ n7 F' @0 O
        proxy_redirect         off;0 ~& E1 r6 ]% d) p0 ?  G# W& z
        proxy_set_header Host $host;( g  A3 Y- a$ R+ Q2 Z% V
        proxy_set_header X-Real-IP $remote_addr;' W$ X8 S: X" h0 Y
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
7 E) }1 T6 `  t8 ]    }
7 H) z; T8 A% k3 z}% p1 P( c0 z! y% b) G0 _$ C& T
六.测试
. \8 ]* p; c8 T9 p8 F9 ~" {; _. P1 l. W/ J7 d9 h( e0 n6 c
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
  X- W+ O! ]4 U0 X- p6 v) |: J5 N6 ]" }/ H3 u/ t
<?php. k4 z4 L; `9 c9 r" S/ [8 M
    phpinfo();   
, H$ [) T/ l6 k& ]  G?>
$ u& F! ^! v- O在浏览器中访问:
6 L; W% P& W/ x
4 x0 Q, J7 v) p- y' Lhttp://www.52os.net/phpinfo.php?id=1 正常显示。4 U* r; N- ^+ k$ ?; d
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
- v8 P" g3 U0 ]4 H9 c0 W! I* xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
" |+ A) p4 C$ B, o4 d说明sql注入和xss已经被过滤了
: [9 A( V, L: v/ i
1 E' d+ f/ N3 S! i/ `$ p七、安装过程中排错- q( V7 W3 A$ {8 |# X! [0 @% b  L* Z
9 S3 Z8 l# q3 W
1.缺少APXS会报错& U# c* Z" }9 p

' k5 q7 R: h6 _% B& _configure: looking for Apache module support via DSO through APXS+ |1 J6 S7 Q" r  j8 Y& Q/ o, O
configure: error: couldn't find APXS
6 S; h3 G! P5 h7 papxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 g9 y/ \$ m( F/ B1 A( H. a- L
解决方法:
$ [2 V9 l, H6 X- j* Y  R% s/ N2 M9 ~: g% m( z( R2 J& ~  \1 c# |0 M: S$ `
yum install httpd-devel
3 Z( L! U! l( o+ C8 U2.没有pcre
7 L  F. @3 r; z4 v! t
% Z9 ]9 y! u: [) Z2 w. m, lconfigure: *** pcre library not found.
- C1 Q' e# R9 ~configure: error: pcre library is required% B" [7 m+ g$ v9 Z; _3 @2 B
解决方法:
  X! p6 E* K9 Q  [# u5 v% S
# `$ e, A/ L/ uyum install pcre pcre-devel7 P/ K6 @+ i- K9 k/ P
3.没有libxml2
- V5 w1 n, \9 p. R1 k7 ^& C' S: T0 b3 |' T8 `4 b
  n( j7 D$ n' ~- i2 r
configure: *** xml library not found.4 }% y6 C: y7 R" b
configure: error: libxml2 is required# t- X& s# P: u/ A
解决方法:' Q2 I2 O2 J' k# ^, s0 `% k

+ q# ^2 j  n! \, b; wyum install  libxml2 libxml2-devel
/ O' P5 O& L+ S4.执行 /opt/tengine/sbin/nginx -m 时有警告2 c( a9 [$ M1 A, q4 s4 Z7 z
* ]  X( A0 x6 o% k0 R" ~; x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
; m; d+ `3 G9 m- z9 }nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 R+ q. F4 i. `4 j, a$ p  N1 z2 i$ f
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 x; t( S  o* a# _& k: F4 ~
  X* D+ s; q: j8 e6 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
/ f1 N# k, r6 o4 s. ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
5 N; k( G) e( |& F2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- p+ H' y$ @) c+ B5 ^( ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 A- `3 [; |- \1 H. Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: C6 K1 w4 P# q4 \2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
/ q& p$ S" x5 {: P: S, ]/ t. P0 d解决方法,移除低版本的APR (1.3.9)! L5 Z+ r' I; F' m: ~0 x
8 y5 c# K( {. B6 s- F) [
yum remove apr
+ G% g/ g" f0 z% T5.Error.log中有: Audit log: Failed to lock global mutex
9 @, E4 Q9 D; F! `% [* y7 ^- L2 n/ A& F( P' o2 D! Q8 E+ }
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     ; j# n% z! Z) d0 t
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* O2 k! ~- {2 h" K$ L/ K
解决方法:
; _. y. e5 m% @& \; q! P- N. ?编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:! J9 T+ G3 C, B- d2 b2 Z$ n8 e
. ~" z$ Z0 Z  {+ w# k% \
SecAuditLogDirMode 0777- }- O% q% g, v5 q: F( u) p: T
SecAuditLogFileMode 0550
& q  w$ B- U9 K1 J- e4 s, aSecAuditLogStorageDir /var/log/modsecurity2 D) I$ w  f/ B9 e) C
SecAuditLogType Concurrent
1 d; H7 F- ~& C参考文章:) A: b8 Z9 {; o% Q
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( v% K$ K: y/ q$ j& Ihttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-14 18:37 , Processed in 0.047466 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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