找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 13043|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。: n4 R; b* f: O

4 V. |, S" x- |/ B; Y一.准备工作
6 ^4 e/ X9 Q( A. G. j, E0 ~
" w) y- M8 @1 n/ r8 d( c系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
' ?4 ?2 M2 J# r: i6 N1 m! m0 z7 r. M6 J0 M
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" H( E0 j4 d( e3 n- T7 N! G& E
) y6 ]* o9 q8 J, Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' J7 ]6 Q: n# j" O& l: G- w
3 i# c" `: F( G+ IOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- \: _% D/ Q' w% @; i* h& c# a. q1 l. o
依赖关系:
# v6 ~$ Z! y( U8 ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 }" s6 n" Q; J: ^  d4 D9 {% D  q# }8 c. q: m
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
) Z' {" |5 z1 A: l* t: zmodsecurty依赖的包:pcre httpd-devel libxml2 apr4 z' n2 R# f: H# [( T
0 I/ B, j9 G. b
yum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel- x0 \4 D9 F; B; ~. C0 d
二.启用standalone模块并编译
& N8 N+ }  G+ `- n3 o" {) b9 y: ?9 t0 w
下载modsecurity for nginx 解压,进入解压后目录执行:. [1 [& g' l. H4 R* ^, i% r: O

. [8 R. a3 i3 w./autogen.sh! M) K2 U  r! C/ S6 K
./configure --enable-standalone-module --disable-mlogc
. A% O4 {/ B: kmake
7 N$ [9 e" l; r) K( j% T三.nginx添加modsecurity模块
* n# H- S1 A/ r1 m  p& W3 `+ [) S4 ^( k( v- L
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 a3 u8 n- y/ E* i+ k5 s
& s( _. I5 ~6 r$ Z
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine, \  h* L$ v8 x
make && make install
! Y" R; X  T. S; N0 }四.添加规则% S9 I: q$ p! m: E8 }; {: g4 |1 `4 g
; o$ f9 n  T* x  N0 X+ p
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& X7 I0 Y6 Q8 U8 |
( Y) d) P3 n, {# X, w$ o* G1.下载OWASP规则:* e7 g0 u7 j) O9 Y% Z' a5 t2 ?2 d
/ {1 `, s/ l9 s1 g
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& u* I+ G! g; X- v+ v' X* i/ h& a  p  m
mv owasp-modsecurity-crs /opt/tengine/conf/
. m& \$ f2 X1 S* i" g, P5 Z- Y" S! H& g% t* ?  z/ R
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* v1 Z, u, I4 C! _- P3 ?/ d2.启用OWASP规则:% J% ?9 E1 y8 n! h6 p7 @% U, J

: f6 W- i6 e% S- d7 q复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。9 @5 K/ M/ N/ h$ B0 y( g
: Q8 ~7 e9 g' i' C4 ~. K, s8 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ n: @6 n7 r; X! D3 ?# T! y' @& s

# ]. f8 ?( }" b& U* Powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% k0 f+ k+ K# u7 R3 b% X7 J; J% P! A

/ F9 G. B+ M0 M; L, DInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ c* W& H" @3 @! [5 h/ r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) I7 J5 C0 Q6 b  y+ v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
0 {' ]: N5 n" `  y% E( P- }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' A4 o$ P2 X5 r% {) d. N, ]6 |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 w& y9 _9 B, H% U0 h8 e. h! j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* v, b/ F# ?8 ]3 B! H
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ r) ]$ H) n. q6 o( T# V5 H五.配置nginx
& t) x( f1 c2 E9 b8 m. K0 w8 G: W. v. @; ]. u& b) S
在需要启用modsecurity的主机的location下面加入下面两行即可:* f1 i3 Q9 N' {  N7 i# r7 {1 @
9 ^+ ]8 `% ~/ r. s. j9 W( X
ModSecurityEnabled on;  
3 f& }1 n" R2 u  A+ v: rModSecurityConfig modsecurity.conf;
8 I6 P/ k/ ~2 x$ Q7 z下面是两个示例配置,php虚拟主机:
8 |3 F, x0 ^+ v$ ~4 v% t/ {' P& I  x( h# R3 k! U  R
server {
8 z. `  v7 K5 B0 r* G% L$ a& z      listen      80;% z" C5 _5 m2 D& W4 W& U
      server_name 52os.net www.52os.net;
7 f' v) g: R* V4 s     8 {( V2 g8 I  v9 h! b2 E
      location ~ \.php$ {' j: ?- g) u0 t% ]  n5 ]  Z
      ModSecurityEnabled on;  
, X. a" y' A4 s/ @/ v& Q      ModSecurityConfig modsecurity.conf;0 N$ Q0 k" q) r* Y

5 {" H( q' [& v: |/ c; z7 F4 T      root /web/wordpress;. f& }5 w% z; t' S
      index index.php index.html index.htm;
3 a, c, F5 _! h# ^4 a3 k  
& e1 n( {5 H: V# j6 G8 G      fastcgi_pass   127.0.0.1:9000;! p& ^2 a% v4 M! w' G
      fastcgi_index  index.php;
# C) }9 s7 p; x      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
% I5 C, K$ c2 r/ m- z8 ]- u      include        fastcgi_params;
: m8 R% K$ t3 a/ _0 N" {. B3 b      }
+ k. C  I/ Q) E  }0 ~# B. L5 p0 F1 g/ U( l$ h
upstream负载均衡:( M  \2 `! g3 r2 y1 j
. `5 g$ O$ u9 E$ y" F
upstream 52os.net {
# Z) n# C" m0 T$ @    server 192.168.1.100:8080;5 b7 d* _/ D: L6 |
    server 192.168.1.101:8080 backup;: r1 x; ~% Z) ]+ n6 W* C* R
}
4 D+ z" e, {% g2 c* T- H; h# B
0 O( i' Z& G% {) x4 \server {
1 _* _# f$ a% `, ylisten 80;
$ K" i1 W& m4 L! w0 H- T4 `( Zserver_name 52os.net www.52os.net;
# I* _; d" F. H: A' k- e  ^
7 t; T$ }4 s0 t3 e" d8 |6 ~6 H, Llocation / {' _" }1 `' g* J  Y. w) Y, k7 J
    ModSecurityEnabled on;  * G+ y) {5 z# b
    ModSecurityConfig modsecurity.conf;  
5 p, Y* v0 L7 J. c5 W* M0 m8 ~( a; O( O2 H2 f" v
        proxy_pass http://online;& f# x* L! X; r
        proxy_redirect         off;2 L: R4 ^# _' V* H5 ~$ F
        proxy_set_header Host $host;" r! B6 G0 F- f! ^0 v9 O& f
        proxy_set_header X-Real-IP $remote_addr;
$ f% T- `$ _, F3 ?7 r7 q  Y        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;: b: ~5 W1 z% Y) y( _! W
    }
( N7 l( t/ Q$ ?" c" V: Y}3 W, r8 c: h- e7 ^, ]: b
六.测试7 Z$ T$ r  l: y: Y: ], m* J
9 x/ g8 J: H& B  {4 j
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ t/ H. c, E5 E, @" _6 ?

3 w$ b5 l; p: K  F! C<?php
3 D( r4 P, n$ z! ~( I* M" s" G" V6 C    phpinfo();    ; z; K. j" C6 ^& p5 I
?>5 {4 t* P5 ?' M, i
在浏览器中访问:8 G/ \+ }- t' ~% |' ?- c( K

' y& b8 q+ ^* X) ~http://www.52os.net/phpinfo.php?id=1 正常显示。6 I. W2 E7 g' O3 P1 _7 j+ V7 G7 V
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
0 e8 G$ P( M) n$ G: D9 f- l+ _: Ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
" n0 r, {/ @1 W9 n说明sql注入和xss已经被过滤了/ g5 R7 \; n$ S( X# }

) _9 f8 P% y- Q$ n7 i4 |# m; I七、安装过程中排错: R. C3 j8 y9 e  {- u5 w/ @4 }

: K$ W  w3 }$ D6 M  g( P1.缺少APXS会报错, B4 Y/ o5 n# _6 _8 B) `( f; B) H) ^
. \& H' P8 b. ^) x; u1 `
configure: looking for Apache module support via DSO through APXS
5 \6 g% q' m3 D: h- F) zconfigure: error: couldn't find APXS
. [8 w/ C+ |6 e7 n6 q. ?apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
  N# Q5 ?  Z+ L" A  ~3 w0 k解决方法:" z$ D9 B, K  E- i7 v* K. ]8 @
7 q' J/ [. S9 `* [
yum install httpd-devel
+ F/ n' R- k6 i, L& v2.没有pcre
1 j( c6 k6 _6 k# O; I2 D
) i- |! |' l$ I4 V% c, @: `, Y' _4 l) tconfigure: *** pcre library not found.
7 t" p! H- P2 _4 T3 v9 A) Lconfigure: error: pcre library is required7 _# g( `8 h  v0 M
解决方法:: x$ a' t7 b% X  H

1 e$ Y% F" V0 n5 S7 }  m! W3 @4 K. U4 Lyum install pcre pcre-devel6 D' s. \: i( i. a9 A# p
3.没有libxml2/ E% r3 U! X9 g

3 d# [% C2 ~& O& v* U7 ?9 G' e& Y: E, Y4 R: c
configure: *** xml library not found.( l6 v# Z0 K! p5 k1 B, k
configure: error: libxml2 is required
3 Q4 B* z' l& @* G4 y# |5 z解决方法:) o/ F5 n$ {% p( t
; T7 Q4 U$ a# P5 `) R
yum install  libxml2 libxml2-devel3 m( t/ m% I: T, ^
4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ B* t5 {% ^; L7 }) b, k# O/ k1 ^1 C) a: F! d2 ?* R$ B' N7 a
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
) s$ O% `  z9 h. c" `8 T/ Unginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 s8 `5 C! {% v9 K! l! }原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 d) }$ t6 _3 y
* r1 U2 O  r3 [' A& k9 s( ~, X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
  R' ?# A1 M2 c+ E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
1 d# h3 q' m* m. l% b, F% E: m* Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 Q  p5 b. T. a% X# m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 R- X# L6 g6 j3 _8 d& `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"5 s+ T+ H1 e6 q% Y
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.  o: B' f1 Y; d0 D
解决方法,移除低版本的APR (1.3.9)
  L- a5 o$ R! K! O5 a9 ]% p1 A8 _8 y
yum remove apr1 |  z% j% ^9 m8 t! k7 a
5.Error.log中有: Audit log: Failed to lock global mutex( w: M7 @3 l% A5 O5 q6 X

9 v1 ~" T. E1 G7 a2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
+ M$ c! w4 W" Q3 `/ ?global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
$ S" D! J) L# K- @( H5 ~解决方法:1 h0 M1 ]3 a5 P/ n; _4 \/ K( @
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
: e0 b3 R3 I6 B3 s& d6 L  v* k0 N! y6 X6 Z5 g
SecAuditLogDirMode 0777
# L' S) l: V, G" o+ F6 pSecAuditLogFileMode 0550
4 w& H; s9 y1 D. x7 C! ~SecAuditLogStorageDir /var/log/modsecurity
; t: ?& ?1 p' d: aSecAuditLogType Concurrent) p% l$ X6 c' h8 B- {' K- p
参考文章:  g, D6 U5 e6 b. A# F
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& z9 r7 ?7 ~0 V. n
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-11-7 16:54 , Processed in 0.064954 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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