找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12281|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
" U; r5 _* m1 E
/ q/ \' V9 I# g* F一.准备工作
6 {6 y4 Z1 v' y4 B' P+ V' A, J" ?# a) r
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: s- l$ C! Z9 I9 y( R

6 K9 H  D+ u+ {! I- ^tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 u% x* S- D7 e, g* \$ O' A0 L) {, V4 b: Q4 ^+ L
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
! _! M9 a" T9 q$ o& {7 _0 P% p& n2 h. L* m
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 u9 }1 ~& l% j- I" ]: [4 t. \, y+ z3 `  S: g( O/ A
依赖关系:
+ x( \, r  f+ ?4 n& Ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
% t  w. C$ Q) s' ]% m( g  Q  {. r, s7 h3 ?6 Q3 G
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel2 K9 e4 G( [$ p1 L" p
modsecurty依赖的包:pcre httpd-devel libxml2 apr% ^) o3 G* e8 U2 z5 k: K0 s

  U( x. A: i) g+ y) syum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel5 N7 M4 L7 G7 v9 F
二.启用standalone模块并编译
3 d$ A+ z" e! D3 h7 }* q4 B
) g, u7 z5 A: S  u* B; x下载modsecurity for nginx 解压,进入解压后目录执行:
5 V) \( V& p" c; G  s* E3 u3 x
( @8 A3 r7 v# @; }  e( A1 e./autogen.sh/ b+ y4 `6 v& p- @
./configure --enable-standalone-module --disable-mlogc4 x) ^( n9 U1 h% N5 X. U6 E2 T
make   s  R# a1 U- u. y7 S9 Z
三.nginx添加modsecurity模块" z; x' J9 d& y/ r' G

: E1 }" ?. C! f! X" L  ^在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 y5 J7 C% c! A9 C+ i3 R
0 r: w( o9 G/ H1 b3 J, ~. L! P0 m: U
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine+ x2 s5 g7 @, E' z4 J0 J6 W! M; Y" L
make && make install* b& S/ L9 E. {  ]
四.添加规则* Q' O0 m7 T* e' F. D

" [0 E" m3 g8 t/ M  F# ~0 k9 }modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! k: `( n6 V1 z- [! ?! _! v: c2 Q, l( k4 i3 ^/ ^6 f6 C
1.下载OWASP规则:
% `, R8 ~8 n* N- s6 {) y& ?0 k; X/ _% f8 O# h, m  V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
: r; _9 Z$ [2 `6 t4 t* K: u) f% S6 t, J  t0 \" Q4 F1 o
mv owasp-modsecurity-crs /opt/tengine/conf/* {* O: K2 o: M% R1 N
( s( C8 x6 P& M7 o8 }9 M$ c
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 A2 _9 p, B# |/ y+ q5 b2.启用OWASP规则:
( t/ J0 D8 m8 D' [" J7 _
9 U8 L! N1 }0 Z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。# n" t( ]4 n# j7 K7 @
/ V3 E2 b: ~' A  i8 D2 }. A* C
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 r9 R- k, i% v$ r6 B) H0 W& t

5 ^0 w6 H+ D  k4 M: kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% b) ?1 X4 t6 K/ _0 S: }7 o$ G6 H* R% b& V3 O+ ~# e3 ~
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" P; _3 ]& _  ~  z% DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf# W% K0 I& c5 o3 P7 C2 z+ F3 z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ {) r0 A. K- |5 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 p' o, P) W$ U; d
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& R5 p* w6 P  E' a6 B& }" s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf7 x& Z) c3 f6 W5 F- U+ c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; }) p: m* [) e0 b( I# T
五.配置nginx
( B2 m6 T" {( F! S) G
- r, w- d+ Q# a1 k0 e5 e* h3 ^在需要启用modsecurity的主机的location下面加入下面两行即可:
" c" _0 N0 ~- a# I
3 l! d  g, |: l. ~- u4 uModSecurityEnabled on;  - ]! J) Z8 U& T+ b
ModSecurityConfig modsecurity.conf;7 y! Z4 D- Z1 w: x4 A
下面是两个示例配置,php虚拟主机:4 f" i4 e( E) ~

8 p7 h1 p. R& H2 b, V5 P3 P0 G  Lserver {
8 y) e3 Q4 h$ d5 v) G8 [4 R) ?. g      listen      80;
$ L" a2 H4 [! ~' c/ @      server_name 52os.net www.52os.net;6 D9 d& L2 ^( Q( L. ~
     
6 p, r8 W6 w9 L* m" K" e0 m2 b      location ~ \.php$ {
* `; [5 \4 \& k! W. u& H      ModSecurityEnabled on;  + x0 R) }% w. z' E- X" e8 }- M
      ModSecurityConfig modsecurity.conf;
8 ?) o  r% b( g( L* V2 s& o  H) L- O$ h  V1 l7 I( v+ g
      root /web/wordpress;
! [) ]$ g5 @" F, ^      index index.php index.html index.htm;
1 K! [4 U) N( O6 _5 \  
% k$ _2 g8 j4 \' r$ ]      fastcgi_pass   127.0.0.1:9000;+ _) V/ K! S% Z& w( r1 S$ c7 L9 g
      fastcgi_index  index.php;
+ w' V" w( E4 |      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
( w- p' L6 @0 b4 j% a      include        fastcgi_params;
8 v; v8 r" r+ ]2 }3 b      }
6 _0 h6 b, i9 C5 F! M" |  }
; f3 s0 U. V& F$ G0 o1 z! xupstream负载均衡:' ]. m0 \  W5 b% ~1 p
- I& P/ o2 n. W4 S- t
upstream 52os.net {  L8 [- V% j+ a4 @
    server 192.168.1.100:8080;
  T( m( h4 O4 H+ i) j    server 192.168.1.101:8080 backup;, m/ k* ^6 L5 m$ [
}+ w* \5 J& V3 F! O9 _
. o( T2 K2 k" o( P! p" A! n' K6 R/ S
server {
  }6 Q- z" J# V; W2 Z2 [; Qlisten 80;
; Z( Y& y" _2 e( o: B% Yserver_name 52os.net www.52os.net;8 ]# D# r4 B- B6 H% {
- k) C+ s  p7 G+ L( `
location / {/ t+ s; ~- b2 b6 @# S- o8 d
    ModSecurityEnabled on;  4 W% Q3 C$ j) U5 e8 X" T; q! V
    ModSecurityConfig modsecurity.conf;  
% J. x/ `% i8 w* W! ?6 N5 L, G7 J2 i6 p  ^: Y
        proxy_pass http://online;4 k7 z( z+ M; ]! k+ J
        proxy_redirect         off;
7 O. P' M# {: u' c0 W        proxy_set_header Host $host;
- Q# m+ e! n$ k# t3 b6 L* l        proxy_set_header X-Real-IP $remote_addr;# b0 a/ q$ t/ Y, f5 t  s, b$ O( ~2 Z
        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;' r% f& _; L; Z
    }; {; x# n, a- n
}
8 {) n  c' |9 [& K5 f; l) k六.测试
7 \: v# O2 }% @7 K$ ]9 H
6 l2 E$ g3 G' s/ p4 u! ~8 _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:+ G- X  k* J, f& ^4 X

0 ]. S' h4 e! k# u<?php
# S1 o! O' F6 E: N    phpinfo();   
, A8 |5 ~9 o6 ~  a" A?>
! M6 }) F4 B3 E/ o$ c8 h在浏览器中访问:
; R9 Z) j; J0 P3 d# ?
- e; ]- O" x' ~5 E5 X: ?- Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。3 l% T/ J7 e2 |9 ?
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。8 @9 E2 Z' ]  h( l
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
  K# d. i' u& C, y' B$ [( k说明sql注入和xss已经被过滤了
9 N5 M# x9 Z  p6 {( Z1 y  J
% Z' t  R& l3 t: X七、安装过程中排错
5 e2 y5 D+ B$ {0 t; o; p! z, t/ y7 u: }/ M9 C
1.缺少APXS会报错9 e0 D8 e) J0 M5 d
4 w3 k* r4 ^) a) o4 P
configure: looking for Apache module support via DSO through APXS
# i, W8 K9 q1 {( x  fconfigure: error: couldn't find APXS0 a, @  p1 b- N" w+ C1 c
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 l' u. y) [0 s
解决方法:
4 w5 a  \& N9 K2 N0 j
' N; z4 d) z/ t' H& [0 gyum install httpd-devel
. t# t1 G3 A$ k/ k; E2.没有pcre
( q: V: M6 N. a' b
2 _1 C; q  v6 Q0 A2 C! O. h3 _configure: *** pcre library not found.
) G( Q  p9 i- s2 g8 t; Tconfigure: error: pcre library is required3 Q5 m2 R9 t# |
解决方法:
+ c7 B2 `& m7 n- z8 Y$ f# ^1 @* {8 Q
yum install pcre pcre-devel  g$ k5 O% I7 `' E" O+ B
3.没有libxml2# T4 ~+ c  ?. M3 H" N
' X# o4 `( w* D" X

. ~' s6 p8 N: B2 Oconfigure: *** xml library not found.( y( [: v& P/ |# U. N
configure: error: libxml2 is required: \! T7 v) F/ m4 p
解决方法:- ^7 s# n1 I" G+ ]2 D, a% Z; l
" Z9 r( ~+ s6 K( j7 Y9 G7 z! }
yum install  libxml2 libxml2-devel
( r0 H: B, z8 v1 ^; v0 f4.执行 /opt/tengine/sbin/nginx -m 时有警告5 F0 M6 B2 X; _( s% ~9 ?! k

' n0 X- [- O$ s+ x- kTengine version: Tengine/2.1.0 (nginx/1.6.2)
: r: _& `4 U7 C, a" Jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 Z+ z& ^7 C/ ^/ S; X8 b4 L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ B# N0 B. S% u. ^( C" g4 O
/ I* f; r! b- H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 _( _4 E3 g' _; ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
3 O! G( W+ U, A! Q- J2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!  K6 p. k2 o% Z% x- c- _% o, f" {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
/ {) d" n/ Q$ t- Q& ?8 V5 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 x8 p. d4 t8 Z* b7 s9 W. ?& M
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." Z. k0 ~. c8 H  a- W
解决方法,移除低版本的APR (1.3.9)0 i: Q; B; J8 I

" M5 ^+ D' y+ c0 x% [8 c) Ayum remove apr! J6 F2 j. \2 R7 J- J/ {
5.Error.log中有: Audit log: Failed to lock global mutex4 y2 I9 D% H; e( y: k, k5 @

. n# F6 s' T7 g" L# p; w2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     * a& |6 w3 B, V) E3 K
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' ], B1 I, i- M0 U, v' K, \解决方法:
" w( O& h7 ^8 D0 s8 t编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
* z) S% @5 d1 ^% ?: z# M% D' B( ~4 C" _8 ~/ o# B$ H
SecAuditLogDirMode 07775 f& u2 a. E* C% O' b9 t& s+ e2 f
SecAuditLogFileMode 05505 k+ s8 `5 O. h3 ]% }8 ~2 X
SecAuditLogStorageDir /var/log/modsecurity
! Q, t4 ^- E6 i5 p8 |6 D3 ]SecAuditLogType Concurrent
# ^/ Z+ E' ]5 W0 c0 J参考文章:
9 o/ l/ q* G, \& j4 r' ]2 V/ _https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX/ U4 Z9 p' J5 B7 z& h
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-8-22 09:49 , Processed in 0.070482 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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