找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 9916|回复: 0

nginx配合modsecurity实现WAF功能

[复制链接]
发表于 2017-10-19 16:53:31 | 显示全部楼层 |阅读模式
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 K' J7 G5 S" x' I, Y% i) L* t, |, `

+ o+ I3 d3 n- L一.准备工作* ~8 j4 K" |) }# r" O0 B
* c5 y9 U! m- @7 g9 ~" [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 Q4 }% g; o5 o7 a* `8 _  P
8 R' x: S" i' t" Z; C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
/ z0 J" s8 F7 g/ z0 g7 @- m5 r9 j' V8 x. s! K
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 a6 ]& X! d# a  @% ?' v. A" O3 ?0 V7 |* a' W8 ]5 Z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs% g: }: v& E1 s: G
" D4 U0 f' I  |
依赖关系:
: H6 P( P+ ]. Z% o6 Ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:  C7 ^* {/ c, D) o2 a  `5 f

# s3 D* ]/ e# [! l. Hyum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel
* \, W' h5 R# u1 W0 z4 Xmodsecurty依赖的包:pcre httpd-devel libxml2 apr# ^0 H7 v4 ]" W0 K1 o9 n- D

% M3 t. C8 l2 L3 ~% U& Z  hyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel
% z" M8 B$ k: _# D  O3 j二.启用standalone模块并编译
; |0 d, @# Z  Y9 J8 u& n
+ o2 [8 c) a+ |2 |0 e( B- M下载modsecurity for nginx 解压,进入解压后目录执行:
7 q- T2 K' B2 y4 n" G
& q: g; O" B  e9 k6 `" v6 ?$ L./autogen.sh
- R* l* W. ]( d9 g3 x3 g./configure --enable-standalone-module --disable-mlogc
' I0 H# O% z; M% D) E. imake * r8 L! `6 _* v6 h0 e
三.nginx添加modsecurity模块
' B3 l7 M1 T6 U, c% l9 e% \8 i' W7 \1 B% g6 c; i& d
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 H/ S. ~' [+ D6 ?

( _$ c/ T( x  f+ V7 R5 j$ e% h* N./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine
" |. q7 l+ b( Cmake && make install# _  k2 c# F8 e2 B; @  P
四.添加规则$ c0 w9 a+ w* U; L( ], n* e

# V( `$ z" N  E' emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ a4 Z) y2 b& D, g" e& E& \$ T

& ~9 i: k! Q# ]. ]: g$ i# T1.下载OWASP规则:
* f# V& G5 Q" a$ t
3 X7 \$ y) b, R, Q, P/ zgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs; k: j5 j! h( d, b
0 ^" ]/ z; C& n1 z4 c( c
mv owasp-modsecurity-crs /opt/tengine/conf/
" D( ^* g/ d. D* o9 a* L
7 I8 n6 z; u1 J: [( ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* T2 ^7 W' u0 ~( L, j2.启用OWASP规则:
  x0 A/ w1 F/ ~  y# m
% Z8 w3 N0 M+ H* R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) b' `7 o; t( I8 p# m0 o& W) e; r- ^# h* Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# X6 p0 u6 \: A" v* M1 v; i

- l9 b  ?( F7 F; P' F! Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* H! u3 K; i$ Y3 q; F" s
4 L+ W' x2 `. j' g
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% n; a" s! @) F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ @5 |5 Y! n: {# M$ G& `* WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* D/ {5 c5 w3 d6 Y+ A  l; [) kInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ t6 ^9 X8 ^! C' E7 e
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- |9 Y5 c. @5 X& QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
) [& U) i2 l  S" gInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. j; o; Z* N2 I
五.配置nginx6 b/ `2 z  P! E8 `

% P" H, z/ R4 ~; \2 m在需要启用modsecurity的主机的location下面加入下面两行即可:
: J, T, W$ N$ v0 v7 T
, b( a6 T; O/ I9 M3 ?/ N( I6 yModSecurityEnabled on;  1 x* k7 m) d9 u( M* A5 @- P  F
ModSecurityConfig modsecurity.conf;
# L( d# X2 W% z6 h% e下面是两个示例配置,php虚拟主机:
  }1 _# l" P( G# K" l3 R: x6 k$ _
server {: D" W1 k( Z( O) d8 A$ @" f: J, }
      listen      80;
& m8 ?9 q7 P% l8 Y0 l+ n) S: e; A5 d% ^8 Y      server_name 52os.net www.52os.net;1 c+ m7 ?3 z, k
     
0 `' ^7 l2 H/ _( I0 _/ e      location ~ \.php$ {
* e, R3 [) i. x4 B* A8 s6 {9 E      ModSecurityEnabled on;  
2 Q6 ^9 i9 L5 W6 X4 g* {# R      ModSecurityConfig modsecurity.conf;, }3 F* l, J) y5 @9 Y2 t# Z4 B3 D

- g+ M$ O0 e  b7 v      root /web/wordpress;
* ^& C/ f: g# |( e      index index.php index.html index.htm;
" E' w9 |& s! f( K  
4 d0 R' A& y/ k) T" O5 Z- l      fastcgi_pass   127.0.0.1:9000;
; w4 i. z% S- \" c. b0 K! F      fastcgi_index  index.php;
9 S( h; f, D3 @; K      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;8 `$ K% e! L/ M. K; j
      include        fastcgi_params;
$ E! L- e. y) p# R7 S6 t      }
. d: S" J+ l& |  j) ?, B# X  }
8 {7 A( o+ u* Z% Y: Q. Nupstream负载均衡:
: i: l/ N" k$ s5 B( i! G# K" m: p7 x! a: R, d$ r3 r- _
upstream 52os.net {; y; M- F  \: p6 L$ b8 n6 h+ ^; o
    server 192.168.1.100:8080;
8 S4 G# h7 o7 Q8 v) l    server 192.168.1.101:8080 backup;
4 j1 H( F: B! @/ o$ }3 @}
9 Y% C- M, z! N5 w, q6 \
7 ]3 X7 k$ ?4 g. ?; r/ D' jserver {
! m' o# @0 H  K/ b) T" Jlisten 80;1 H" n' e7 @0 Q0 m3 w. o5 k, l
server_name 52os.net www.52os.net;+ G, p! {4 v. Y2 J5 R( l1 k5 a
# w4 A+ p2 J4 G
location / {
& R( ]7 U8 v2 E    ModSecurityEnabled on;  1 a& b! c7 f; I  j' }
    ModSecurityConfig modsecurity.conf;  ; H; o# p& K& l) Q" f
0 u. G9 p2 k0 O! a0 |; |! z+ z! f/ h
        proxy_pass http://online;( r; Z7 A4 o2 @, G- B; W6 r8 T
        proxy_redirect         off;
$ A/ w5 E1 ?* f1 }. d; D        proxy_set_header Host $host;2 \: ?: C2 H! j5 W" N: s( C1 O
        proxy_set_header X-Real-IP $remote_addr;
2 ?- D+ k: a% p9 }! f/ A' r        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;+ I& q/ q+ x" ]4 m* t
    }
0 b* h& s6 z- s& v3 ?7 j}* P- x5 s0 p9 v, P
六.测试
/ S4 d9 x( x* C! c9 n
, S  W9 G2 e, ~7 X我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) i! t$ W4 R* C2 P

: }+ a, R+ a1 \: Q6 ^<?php1 K7 X" D9 y* s0 b( }# z
    phpinfo();   
, [1 k; u& ?* Z9 @?>" z, H; X1 }3 d# g' s9 u
在浏览器中访问:. g! P" h0 s7 i) y# A2 {
" G3 \) _( C, R. B) h+ ]' d% G0 ~
http://www.52os.net/phpinfo.php?id=1 正常显示。( b: F7 V. C; K( s
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。" P4 S4 I0 Q( R
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。
# S7 x( ^$ B) P; X7 @0 _9 A' t; \说明sql注入和xss已经被过滤了& `! o) n7 b$ G7 k3 g7 @" E/ U

& u. p" c3 J* Y6 L6 ?, W七、安装过程中排错& p, p: O! K+ x% p4 \+ d

1 D: ~4 j5 e5 q; S5 L, E1.缺少APXS会报错8 G. e. \0 C& e

) J6 I8 r! F$ w! g- I; l4 wconfigure: looking for Apache module support via DSO through APXS# [* x  L( D: ~. o
configure: error: couldn't find APXS
% `" F+ v/ ~4 C, Wapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。5 k" A7 J  y7 y# Q: _( a: N, S
解决方法:
2 A8 ?. b; W, o: E' j
0 [3 k& A; p  i/ |yum install httpd-devel
1 p1 ?& t- S) o0 M, p: ^' _2.没有pcre; b  K4 p( ?! A( }
2 p- c$ Q. ?* q# m
configure: *** pcre library not found.: {& C' H% ^; ~
configure: error: pcre library is required, i! c; [" T, G8 }& ?. {3 I0 P
解决方法:
. s/ o! R0 [/ |/ _1 @
8 M5 v/ ]8 C5 [* X0 _1 l( Wyum install pcre pcre-devel
% l' a. D) ?2 e5 U3 p3.没有libxml2# ^- p" w  L6 u; ~

/ V  r9 r7 V6 o8 N
+ y* K7 L! b5 R: ~' b! u( Z: g! hconfigure: *** xml library not found.4 K8 ~  {) `& u' R3 d/ Z
configure: error: libxml2 is required; K7 F0 S4 ~5 ~- c$ o$ G: H4 N
解决方法:( p0 R4 S1 ]' V" J; o; Q+ U) B7 g

% C: i! K# j7 }+ P- a) |yum install  libxml2 libxml2-devel/ g8 i2 d+ H' X+ n7 \2 }+ g
4.执行 /opt/tengine/sbin/nginx -m 时有警告1 k) p9 \5 }/ b# m
: E! i, m. E, @0 _, C+ U6 D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 g; j: p3 H4 H; P( T! F
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 V4 O" g& o- g原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log: A: g% L; O" C4 q

' n' R; x9 u! L, v; a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. u3 n: F& Z3 T1 {) G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"0 ?/ T* x) W3 w1 v/ @2 t
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 o  y. w, L; P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ \2 p" D; D5 E, e. H( Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
1 U7 }( E/ Y! P$ b2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- }) F. x6 I9 H7 H( {' |2 N  _解决方法,移除低版本的APR (1.3.9)
( Z* G0 q( X* j0 a2 M& i! W  C& G4 u& Z4 a
yum remove apr: d$ e$ n$ T  D/ u/ O+ g
5.Error.log中有: Audit log: Failed to lock global mutex; e4 T4 l" `6 b( ~/ Q* ?

6 v& n8 L! F6 P, }" X3 ^3 x! s3 }: z$ u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     
4 N/ u4 d- m* P2 L, ^2 L/ [global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, O- \) A: U9 W/ q4 A7 T) j2 g解决方法:+ z: F' ~: I' q2 O8 x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
# h2 E" `& O( C! P" j, \5 X1 w
& Y+ B" }  w" r) ^; z1 B; X. R+ u3 xSecAuditLogDirMode 0777
9 O( `6 a* q& F0 Q' c; S' D' ^+ l2 YSecAuditLogFileMode 0550
! f# @5 j& [, ^3 u$ ?4 q4 aSecAuditLogStorageDir /var/log/modsecurity
; M& v: [' {. GSecAuditLogType Concurrent6 o4 @* _9 E0 m8 V) I, n2 P
参考文章:
# J. U1 _2 w5 y, S1 X: zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- H) E3 O1 g7 P& ]
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-2-13 03:43 , Processed in 0.061836 second(s), 19 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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