找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 12414|回复: 0

nginx配合modsecurity实现WAF功能

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

' F6 J$ i, x* h- R3 a2 ?一.准备工作
+ I/ h) l) A6 S$ o2 m" N2 `! |* K8 h
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 D% h5 j6 j6 |4 u% n! L# a" w
' r6 G& h5 Q1 c! f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 ?. V8 L' s, x3 r; p2 m. u. a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: ]- T  S4 W. V# W3 e' f/ l+ Y2 R  Y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
) p4 H, Q. m0 l% _1 s+ B" U9 {5 ?; |. R+ y' o8 ~) l" y
依赖关系:
1 B& \( \( |! ^/ F' @. e# jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
; h9 h0 u1 t7 Y3 G) n4 o% E7 i2 G1 Q* x! m& g3 ^
yum install zlib zlib-devel openssl openssl-devel  pcre pcre-devel3 o1 K) Y: c1 x, W$ B( N+ j
modsecurty依赖的包:pcre httpd-devel libxml2 apr1 K6 W" Z0 Q7 k( @: P

# Y$ f' j  w& f$ a" ?; `. qyum install httpd-devel apr apr-util-devel apr-devel  pcre pcre-devel  libxml2 libxml2-devel# J# _: Y9 A; X: A
二.启用standalone模块并编译
9 Y2 }8 K0 ]) m
! A2 k) n9 ^' M1 [9 V& C下载modsecurity for nginx 解压,进入解压后目录执行:8 U4 \; L5 z2 q& b1 ?: P! j# P

! o. Q& w# o) k! Z$ E./autogen.sh1 \  d) C) \- r& ^& K3 B2 N
./configure --enable-standalone-module --disable-mlogc2 ]6 q* W1 O2 P7 g! P
make
% U1 J' Z8 k& o( {! N三.nginx添加modsecurity模块: v4 X% v0 Z3 B! B

+ ~* p0 E8 B+ g) S# P在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( r4 z8 C5 L3 {$ x6 K. B8 D
; J1 h% L% v1 |./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/  --prefix=/opt/tengine3 V' b# i' S' P$ `: s
make && make install
: S+ e: \1 u' ?- V四.添加规则
! U  e  Q5 _% x/ _3 @6 S
6 y3 \- I3 e( {' Z7 [: imodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 {" `% U8 l3 C

& @  T# z! r' S/ C" }( H/ d1.下载OWASP规则:
. y/ [' L4 Z$ Z+ a
" @1 \4 S3 x+ l* I) j! c8 hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs( s, g( ]" j+ h" }2 E

: V6 @$ ^& q- ~- q/ Smv owasp-modsecurity-crs /opt/tengine/conf/" S6 s9 u. {# @& j! N
$ }+ J  A0 o- b! |$ M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- G$ g- _6 X% K" w; p2 q! `' \2.启用OWASP规则:3 c% c. f7 o1 H; S7 m, x! F3 R5 K

+ L9 u+ {/ P* R% H8 C: h0 A复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; L3 K! e' B6 `; }
! l( L: Q5 v5 ?) _+ t* v( `编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! b0 P  v' O: M, Y
. R; v2 y" K% ]& a. Y( zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ a, b& R8 A4 T6 w  I2 G

; c9 e% o/ R! t; S- n* C& [Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 \; K+ q" C6 h7 P6 @: v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( \, z" R  I, b2 Z4 }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 Y# b% n, c6 T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 k$ _: I6 j) G6 p0 T$ N! y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; J& S% r( q. a9 m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% u" H2 m6 y7 \- ]Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 t2 E1 N. Y5 f+ u
五.配置nginx0 ]4 A2 F. Q- o& p5 A9 D" o9 N

6 p8 W5 u! X+ }# V# [在需要启用modsecurity的主机的location下面加入下面两行即可:3 a3 |: g( n* \8 ~

. P2 a& o! F' r: W0 C: QModSecurityEnabled on;  
& \1 q+ u# x; L1 u/ W9 ~ModSecurityConfig modsecurity.conf;
0 _% ?4 V! M% g+ y, {7 `下面是两个示例配置,php虚拟主机:
) d; l4 M8 c1 m
2 Y7 @) `5 I8 i# N1 l( Nserver {
! p& b, u  n5 I0 x; N$ J      listen      80;* N& B0 j4 c2 m. K( P
      server_name 52os.net www.52os.net;7 t! M, Q0 o" I6 ?$ {( Q, }: K) M
     
3 L5 q7 R$ X" U; k: N, ?      location ~ \.php$ {
% [5 P! r1 G7 ], R6 I( A! e  ^      ModSecurityEnabled on;  
* C  Z: n$ [) W: \% `8 K( \  _, u$ q      ModSecurityConfig modsecurity.conf;( J2 b9 L+ }2 s) P3 B
2 `& X- S0 \+ F' Z  ~' z' J9 t" h
      root /web/wordpress;7 N! Z+ d& M+ ?% m8 v
      index index.php index.html index.htm;
6 |. o0 E- F& q7 K0 E/ Z0 a  
3 @1 g; {1 c" a' i      fastcgi_pass   127.0.0.1:9000;
9 l2 o$ Z/ t+ e6 Y) |      fastcgi_index  index.php;
4 X+ Z8 S& Z' F! Z. \9 G, u      fastcgi_param  SCRIPT_FILENAME  $Document_root$fastcgi_script_name;
4 S1 }% F# }2 A& O      include        fastcgi_params;
9 Z5 O% `# A$ M- S3 m- j      }
; T' U: U) z# D3 ?- c; Y  ]% D  }( H  C- x- j5 {' ~
upstream负载均衡:
+ r; \# x5 v# K& H) E6 v
* R: L( P+ U: J: Cupstream 52os.net {7 j: Q) f4 B" c8 h, k- T' j
    server 192.168.1.100:8080;
+ o0 F3 H+ C% e* _- X    server 192.168.1.101:8080 backup;$ L8 x( V+ I, w% z
}
7 m0 ]; H7 D) Y3 D7 m8 [5 Q
; M% x& E6 H! y; Jserver {$ c5 ^' k2 ?0 Z) o6 o
listen 80;
) C* v: }# [/ L: {2 aserver_name 52os.net www.52os.net;) A& A, U3 p, k( g

. m8 t$ {8 b+ |# a5 F1 Hlocation / {" `3 A0 I! j8 p( f0 a8 n
    ModSecurityEnabled on;  1 ?, v6 v0 w5 `2 |2 ~
    ModSecurityConfig modsecurity.conf;  
' ]* h; S: s6 ?8 }% t! p1 Z4 P, L4 {2 e- g. x# L
        proxy_pass http://online;
5 x* E( g  b0 J* K. R1 ]8 b        proxy_redirect         off;
" C/ X5 s" J: x  L        proxy_set_header Host $host;
# O6 O. W# j# w9 W        proxy_set_header X-Real-IP $remote_addr;
: W: `- ?' L. z7 R* r/ i9 s        proxy_set_header  X-Forwarded-For $proxy_add_x_forwarded_for;0 M: y7 D: J  H9 c% m6 ]- y
    }
8 n; G$ q/ o1 z6 J, T0 H}
) l' |/ W1 X( _, g六.测试1 [9 D- K* K2 h  |, \+ y

) y% D3 a- X$ I, v我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' Y* B  u! Z# S( @* V
( f7 j( ^7 I+ R0 \/ f<?php0 N* Q, L( \: F/ G' u
    phpinfo();   
# V5 `  Y( |5 e( F3 u?>
: h9 E* m0 p. R1 w# G% d2 j1 B3 e; E在浏览器中访问:8 x2 E0 G3 F  t0 \; k) H
/ k6 j6 o' ?7 |) F' U
http://www.52os.net/phpinfo.php?id=1 正常显示。! P/ A4 v# B! j1 c
http://www.52os.net/phpinfo.php?id=1 and 1=1  返回403。
- w3 h1 b3 ]* k+ r3 qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script>  返回403。: r* [7 z: o) i$ A0 E
说明sql注入和xss已经被过滤了$ a' V2 Q& j* g7 X% N+ g

2 `* j% \' I2 T0 w% U: v" y七、安装过程中排错6 q- K/ P+ s  H+ p: w
( k' Q5 C( e" H3 ~3 M, m: ?1 Y
1.缺少APXS会报错! t7 J! ]# h! |
' g7 y" z$ J; \( s9 I
configure: looking for Apache module support via DSO through APXS
- s: m5 L, p4 S# _3 Nconfigure: error: couldn't find APXS! @( Z8 {. e! z# g; k( r
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。: G* u# ^' q$ z
解决方法:
+ D" w& z1 w; E; c3 U9 V* p2 L/ t* k& ~9 V8 T; L- U& C
yum install httpd-devel
% [0 j; d. I# N4 A2.没有pcre7 Z6 D- K3 M6 M* {$ V
( u% ~. y% n$ v0 g1 y, O0 w
configure: *** pcre library not found.' @# H$ O8 X  n  f; p# S" W$ O' k
configure: error: pcre library is required
- x  i" \) y. ?( j解决方法:; k9 }9 G: n" k8 {' v/ ?9 i; |

7 |( h* Z3 n4 B, hyum install pcre pcre-devel5 o! ]4 ]* u  ]$ `( ]$ [
3.没有libxml2
! }, K6 u  C+ Z& s
4 f) P3 \* ]+ K6 N
+ Q/ ~9 R0 K6 Z' _* Sconfigure: *** xml library not found.
8 C% R& h1 Y( Z  qconfigure: error: libxml2 is required  I$ Q. o5 t- E. _
解决方法:% R' v# A8 K6 j+ R8 b4 j: D

8 H! H4 Q; R0 e* ^- t4 i( Nyum install  libxml2 libxml2-devel
" N" Q7 c1 s* ~$ L. ^. k4.执行 /opt/tengine/sbin/nginx -m 时有警告
, n6 J+ k& N' W9 M* J' D: h  }5 n- \* V4 `
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
8 c7 s) u+ m2 s- \+ @0 _5 |- B2 J# Xnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" p4 E7 g) z' P) I4 f6 `) x4 h7 R9 Z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* l: A2 |' P& N- a0 ]# C: j. o3 C& C( l* I4 T6 U& R1 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 A. m; g' e$ K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded     version="1.3.9"
" @5 X) o3 m; t' s: _) [  Q/ A" p2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; r$ p' E5 O9 o' T% G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- [* p3 W6 N+ k# \* C1 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 U& {" B0 B7 s- S# {1 N
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." j  u  J8 f. J; P; M) a2 @0 F
解决方法,移除低版本的APR (1.3.9)/ ?; w' ^: H9 c4 j5 V
. J9 X4 k+ ?" D, T+ ?2 H
yum remove apr2 j1 D8 f# y2 l- C9 H. l' Z) \
5.Error.log中有: Audit log: Failed to lock global mutex
# ^9 @3 {# L! O1 Z
2 R2 h3 w$ K/ R" M; m2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock     / ?; k, S/ c# M% X/ ^: Q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 \! g2 P9 i* ~+ g2 ~7 c' q
解决方法:: Z, f2 x, {* r6 P2 S( x
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# A* v4 s5 W8 |  ]
7 t2 z/ K/ t3 c6 z. D2 R8 |' H8 {
SecAuditLogDirMode 0777
. v; B# @, d2 ^8 c3 r' ISecAuditLogFileMode 0550- m2 X+ B0 D! }7 W) o7 R
SecAuditLogStorageDir /var/log/modsecurity
' p% p# E0 J) TSecAuditLogType Concurrent
0 M; e$ @: ~8 P: S/ p1 ^5 K8 N8 X, G# j* F参考文章:
4 s2 C; d2 U. Mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX; t! c1 }# j9 }; }
http://drops.wooyun.org/tips/2614
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-9-4 01:42 , Processed in 0.065075 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

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