找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13111|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 a# E: O4 w' ^4 Z( U- k" U* y
% l6 v5 l* ~/ Y2 `, C. p( P- C一.准备工作
6 O) R1 s* N' R( B! O% S
+ y: v) f, w4 H系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- J0 f" ?3 c4 u$ z4 t' z

' u9 L& l5 i: ]tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz/ j6 C  [1 J' f* n

2 j  {+ Q' e1 {4 ?  Y! Xmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. {. J; n) I! Z

8 u1 Z6 h2 A' G& Z3 ROWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: e6 \1 Q' i1 u0 ]5 V  J' q  ^
# s% f4 ~0 N" x6 |& O
依赖关系:  ^: M+ x* k* n8 J
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 N# ?% c# u( E% S. |- p+ y

- Q; d. u1 c. ^yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
6 m$ C9 E7 g. f; Y. v2 q: qmodsecurty依赖的包:pcre httpd-devel libxml2 apr
/ N$ R5 Q4 _/ v; e" S0 i, N" p. d) U( M6 k' T
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel6 T& f! N1 X9 c5 Z7 K+ k) C, ?
二.启用standalone模块并编译9 @- q9 f% _* |+ o' N6 X

$ y4 o! r0 x3 b; b, m9 C4 }8 Q下载modsecurity for nginx 解压,进入解压后目录执行:
! T6 e! n4 e$ q+ R6 O" ]
" p$ t" O* P0 H./autogen.sh2 t" P3 J3 l, x' ?( `5 r
./configure --enable-standalone-module --disable-mlogc! G6 c1 N. {- [! z6 B) C
make
0 j6 W- S8 f4 |" x& U+ t8 c三.nginx添加modsecurity模块
9 p3 U0 Q6 B4 P1 O/ [2 ~3 ~$ ?* Z; s) w2 c0 ^+ l  X' r. t$ g
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ m( E8 G  k) b( _/ S
: i) o' ?6 x$ U" ?' H& F$ i
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine- x- e% _6 `  \: Z
make && make install
; V5 y) Y3 d; m四.添加规则
! [4 q: E. Q; B: ?% t8 j/ J4 U) W! j8 X( i
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 p" b$ i' L% |" X; A  X# A* J) I

6 W8 y8 T2 [8 \# [; g4 P# q1.下载OWASP规则:
& K3 \) W+ Q: r$ D
9 l6 G2 S) t" E7 H4 q( o$ F/ jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 y/ o5 l- E8 n- p1 V6 F' E; ~# w$ u3 p4 j3 Q4 A( u% `9 q8 B
mv owasp-modsecurity-crs /opt/tengine/conf/
) J& D5 k; |) J* y, n7 ~- T: M
1 L* b2 r4 o" f% v, L/ ]cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% |' ]9 y) c# S
2.启用OWASP规则:/ j0 I8 E4 d& G6 u

+ m* C" Z6 H# R  Z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. G9 E9 n; _2 U7 Q, o
6 D. J3 v) O8 V' R* {& k( e# i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ b) `, X6 \* o8 ?& W

, a5 w7 N5 v. M+ ?" r: ~$ o, Kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 ^4 w; {& M. x4 P; g

( z( {6 o% Q+ ^2 H$ `: R* o1 ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ E1 b4 a9 C) lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( K5 R1 \* g' Z; c/ J- k4 v: F7 k2 iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf2 t3 ?/ R& X( i5 U1 U% h4 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 b" ]2 ^/ I' c0 u! Q: ^; h
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. G5 Q0 S$ a6 D& L1 u' x
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ g5 [- ~, D4 X3 x" o- Z2 r) ~Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* |0 C4 T7 \6 U
五.配置nginx+ B0 j/ |! L& G$ z; K
" a# {7 V6 h7 F0 ^2 c6 s8 F4 Q$ R
在需要启用modsecurity的主机的location下面加入下面两行即可:2 x; [% j0 U! M- M/ {0 r
1 R6 w+ ^* Z. O6 O* I, x" e
ModSecurityEnabled on;  
  h0 Q$ q, c. q. f  j4 J  @+ iModSecurityConfig modsecurity.conf;, b9 y# R" E; Z% E
下面是两个示例配置,php虚拟主机:
: ?7 Y" _1 a: m
  T' [  t# }, p5 w4 O. cserver {+ R! ^- ?5 i9 n5 E$ z
      listen      80;
* Z+ ~1 L' u. x, z! c% h      server_name 52os.net www.52os.net;, ?& }6 C+ F, C+ A3 P
     
5 Z" n' r$ Q- C      location ~ \.php$ {9 ]4 _- l; h/ D& t3 j5 h3 ~3 P: ?
      ModSecurityEnabled on;  $ T. s$ o9 R1 b. _" K
      ModSecurityConfig modsecurity.conf;
5 l% q, t" }8 N$ \, h) J+ O3 Z7 w: U! u& A2 D! r) H- k2 C$ R, ~  B
      root /web/wordpress;. v) X/ C( Y9 L- d; y0 J. d0 I  T7 t
      index index.php index.html index.htm;
8 w( d; m  Z! Q$ Y# M  
  G1 ?' i% p0 l( T+ Q* `      fastcgi_pass   127.0.0.1:9000;8 E. Z8 e+ L! c
      fastcgi_index  index.php;
3 s# a! E  a/ v- C5 j9 N4 D( R      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;+ a& B% ]) S" O, {- H! p, H; X
      include        fastcgi_params;7 O7 ~8 [/ F5 ?) E3 u
      }. W/ Y, Y# S; j. `8 P, `2 e
  }2 F5 }0 K4 C: V  `' K1 M
upstream负载均衡:+ s, z2 m- V, T6 a8 s

% [& b/ a/ }% k5 nupstream 52os.net {
* [8 x2 H( D4 ^, u    server 192.168.1.100:8080;
2 ^; F" }! _; g( D' }5 b2 q& J    server 192.168.1.101:8080 backup;
2 o1 S$ R8 o0 `6 S}
( ^' D) f& C+ M9 ^; M' I
3 @2 l5 ~# q$ F0 {- O* nserver {
! ]7 Q, L# g6 e, L5 O0 Wlisten 80;
8 H) s6 m* J+ u; u5 A( dserver_name 52os.net www.52os.net;
0 y( ~* A; C" k. J, a, f3 V) h) F# q, C% h: G
location / {
5 \. E5 s" A3 H6 w; o6 f( [1 s% }    ModSecurityEnabled on;  2 c6 V, B/ m; j5 A1 d2 O- B
    ModSecurityConfig modsecurity.conf;  
; {% r) u8 z, J. c1 Z9 ]1 d; d7 G: m% k1 i' L
        proxy_pass http://online;# Q; l7 k/ }' y8 W3 y
        proxy_redirect         off;
3 [+ H! q/ {0 o4 H8 h6 r        proxy_set_header Host $host;
! V0 T; a( j0 t        proxy_set_header X-Real-IP $remote_addr;
4 f1 {9 K$ L& n0 B1 C        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;
5 }" j0 N" O' Q; W' D9 ]9 y# B    }
" a; M  o5 ?( |/ B$ I}6 a2 V- O" \" ^! ?  H/ t& r/ L, a
六.测试1 f* X* V7 \$ J) C9 A: P* m5 `
; {# J5 C2 I! H( n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 }. Z" V1 C) v( p+ Z
  S: M2 [, d; M7 K( o  q0 I& w1 I
<?php
) ~2 P3 n5 l' O% z: M5 ?    phpinfo();    4 X8 Q& Z$ |  ~# X3 Z
?>9 q% h) c7 W( B3 X$ G% C) H* L
在浏览器中访问:4 r+ Q4 m, V6 ]0 x
% f3 J! t" A; [. f4 z+ [, t
http://www.52os.net/phpinfo.php?id=1 正常显示。2 V0 k; Q1 l# I4 I
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
" {; K. L; ^4 m/ U$ D' Zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。( [$ x$ L$ W9 O) J  a
说明sql注入和xss已经被过滤了
5 X3 b8 z6 ?7 h. C* o) ?3 b& _; a* a
七、安装过程中排错8 w( Q* [; ^5 X% c0 Q4 L. X2 o  i4 }

# j- M) `/ i, G5 Z7 {: O* C3 P7 v2 a) [1.缺少APXS会报错4 t  O: J3 F* [& |
' g% S) C# H' e, V6 p
configure: looking for Apache module support via DSO through APXS& F9 W6 L) C% r4 Y
configure: error: couldn't find APXS% s" h& z7 N9 P  @' ~5 n* P4 J- {/ R
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! k) B0 Y' ?  v  S* @1 H
解决方法:. `$ p( ^' a6 z
* [% z/ h* s( U
yum install httpd-devel. t% T5 \6 q2 _8 F
2.没有pcre
# r" y" E# y4 r0 x1 m
" t4 a: x5 \! _, w' _configure: *** pcre library not found." b- {  p( \+ j$ a$ d
configure: error: pcre library is required
0 Q' q+ x( d  y$ |解决方法:* `9 c; d, ~' P& y
$ V' G2 g8 l/ y0 L8 E0 m! n
yum install pcre pcre-devel
( u6 j8 |' w0 s2 Q5 ^, U& P3.没有libxml23 x8 H; X  o* M; y9 `6 S
' [# x$ O- S/ H* W+ J, j

, c! ?7 j! {8 D" |% ?: {! y* H* Tconfigure: *** xml library not found., |/ d1 D' Y7 y3 \2 w( V, X
configure: error: libxml2 is required2 y. _9 @2 e! k4 j$ S% k$ e
解决方法:+ |6 }" f- k( l, ?5 p( Q

1 \* |3 `0 F. byum install  libxml2 libxml2-devel
9 L: I3 A& i' [) \5 B4.执行 /opt/tengine/sbin/nginx -m 时有警告: P5 Q$ i* T3 [3 b
% B, e: c' t3 F! u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)  k5 ^: f: a& H# u; {6 z
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!; f$ a6 A& d, \' f" v7 X7 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log" f2 g8 z8 y" R- }# V! F- T5 [3 h7 n

6 G  j7 S" k0 t: x3 p" H9 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 \- Z' ~& W  Z* k' ~; G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
& x! T( d7 t; D2 y. w( P* ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!. i0 t  O+ j+ u1 _8 p: }) e4 G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- P$ M$ F1 b( Q$ Q5 e/ S& N3 a4 y3 W. c  u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% ~6 {; Z! P, |3 v. a
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ I( Q1 B) @' g! \
解决方法,移除低版本的APR (1.3.9)
/ _! K' @, E; R1 u0 C+ D7 P* K* N6 z% G; k, ~7 h6 c
yum remove apr
; W5 L% K# |8 }, q- `7 P7 g  }, ~5.Error.log中有: Audit log: Failed to lock global mutex
9 S6 T5 ?0 c2 W( `" B4 x
3 k7 S, t  L1 j& j) o1 C2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     . M" n' r$ v/ D- b; H
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]$ O% C" v, {6 `
解决方法:
. r$ f* S/ T# R8 v* _' q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; }: r( c) ?& s, V$ q' O/ |# [9 z# A5 |8 n& ^' q
SecAuditLogDirMode 0777
0 k* p" Y/ `! \9 qSecAuditLogFileMode 0550
, ^  ^; p4 S* `. ^1 A. |, N0 n8 ASecAuditLogStorageDir /var/log/modsecurity
0 `( C; a0 D$ P* D. F" x. ]SecAuditLogType Concurrent' P* I& N: y7 c. [
参考文章:+ r" g5 {3 l4 q& G" {( \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ L& v) Z6 ~$ k) G0 V( Z% Thttp://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-15 21:28 , Processed in 0.062872 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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