|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; y* c" N+ s3 p7 f
9 `; m/ L# x$ C* V一.准备工作
3 y& C$ c8 g f/ M6 ]! K' a; E* E, D
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 D7 z6 F- n4 {; N# ]7 ]+ ^5 T" s2 X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 |0 o1 i& j$ W* y$ x& w9 a
1 S5 K' H% H9 r9 s8 O3 S
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; i) U# p V4 d$ S+ b, ~7 q8 t+ b+ k( T8 b V. H& c7 v
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs7 x. j; F" t/ j8 @
+ q! C/ i) I0 h- D) r" ~# {依赖关系:
- J/ `3 y. @% Q9 Xtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:0 V& `5 [. G1 q
P0 z; o" r& W0 {" Y5 syum install zlib zlib-devel openssl openssl-devel pcre pcre-devel# B6 C0 r1 N$ h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 B; w9 y* H6 O, J, @
; U9 d! |$ d4 L: q6 iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel0 g7 V7 n$ t6 R x0 ~( h0 G& N0 e- w
二.启用standalone模块并编译# r! ?3 M9 Z3 n9 P. l: S
+ z; f0 T) P# x下载modsecurity for nginx 解压,进入解压后目录执行:
}, X: k4 x n8 U8 i. d6 O$ c. C: N: L
./autogen.sh
( j z. o( }1 |5 m1 @./configure --enable-standalone-module --disable-mlogc, b6 q+ a- D |0 Q
make
) z- Q. [% w+ s) g& |9 D: P; X8 m3 n三.nginx添加modsecurity模块
: l+ ` o3 p7 }8 t( `' C; I' y6 e. I+ Z+ @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 t" S4 z4 q/ R! I+ Y7 q
5 T9 c2 L6 {8 v6 ?0 g; M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine( @% k) _4 _$ }% i; G
make && make install, e7 m# }8 m; A" v$ I
四.添加规则7 g: }/ v4 y2 C9 Z& b0 x
/ z- \9 B1 T$ v8 S( _) Q* [ O! Xmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。; t* O8 N6 u: K% X# m2 `
" w- M- C' {) F1.下载OWASP规则:( y& Q' `% @& J! ?0 ^( m
& P5 Q; H7 I1 M8 n. igit clone https://github.com/SpiderLabs/owasp-modsecurity-crs, f+ [4 G$ i& ~7 w1 s" i
w+ j2 W7 S& E B
mv owasp-modsecurity-crs /opt/tengine/conf/
- V ^+ {8 y- ~0 K$ n4 k
) x4 X6 P4 m9 Z8 q8 |! K+ kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; I7 ^0 u- j8 S$ U$ [! N
2.启用OWASP规则:
& Z1 B9 A* c6 l, d; W. y7 B! |$ r
4 T, M, ]3 z) [9 ?) u' Z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' O) v |5 C9 R0 E( F0 N6 p6 K, E7 q/ {2 b. w9 h/ t
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
3 r* _4 {. V- x: {' f- i$ G) I& ?( U# N7 Q# o+ V! _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# s. y2 A9 R/ `, B/ ^; b6 Z! c& k8 d$ S0 S3 S
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 x" Z0 f8 u1 d) d0 ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' \7 J9 {# z# C3 _8 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf ^+ k8 }. H, ^8 i' V! l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 g' N) I: Z; W8 s! K% v! w" m3 z+ g2 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 J k1 k: Y4 f! I% t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* q; V- C* b) h- j- JInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
+ Q1 {$ T) n: S! E五.配置nginx
$ a+ d4 U1 G# k z% e
6 ?) h' Y' }- h6 o" S在需要启用modsecurity的主机的location下面加入下面两行即可:
/ {& x/ F. B: S0 \
$ F/ k( P8 p1 g6 pModSecurityEnabled on; ! l( }- a* @/ A% [: ^5 v
ModSecurityConfig modsecurity.conf;
: Z& f2 {) a; |& g# X$ |下面是两个示例配置,php虚拟主机:
/ y( g( K# E) {5 x0 e% a; x
. n$ ]4 J! P. {! }server {0 n3 o( [4 [% L$ t
listen 80;
/ C4 q N! T' a. U0 ^ server_name 52os.net www.52os.net;
/ S' @8 ^2 z. Q 8 ^# `5 E X, k' L9 [: w R
location ~ \.php$ {3 a6 X# z& R& X& u+ B
ModSecurityEnabled on;
! @0 J+ \, ?- O ModSecurityConfig modsecurity.conf;
! k3 l) s: B. `, I" }3 z
" \* w. E6 I d, _ root /web/wordpress;8 q+ h0 Z2 H, `& R7 V! X
index index.php index.html index.htm;
' u" A0 h$ |6 g z Y! P K + B, B- U" M- `. v B, T
fastcgi_pass 127.0.0.1:9000;- `) e# E: j0 u3 V" d8 h" j
fastcgi_index index.php;
9 R+ ~% \5 t: P7 y h- p& f+ m fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& T2 {* i2 b3 Z) _( ~- p
include fastcgi_params;
2 S/ j! @- s3 ?( |% X- g C }
) `- o$ |- z) e( m, G) u* Q& z# _) e }
$ d6 ~- [4 z$ ]& G# p( Bupstream负载均衡:
3 ^1 P, v8 m; {/ d+ i5 V3 O5 t3 k' O" X% e7 [+ }, P# n0 D7 H
upstream 52os.net {9 X1 L! u! X% ]( R! a& c
server 192.168.1.100:8080;
& |# d; c/ G( y ^% v4 O. g server 192.168.1.101:8080 backup;5 j7 D% Z7 m. Q
}1 x8 y- [7 Y7 h0 l6 G) x
' l; H2 U8 D' j! \7 Z; M8 J4 E8 |server {" o4 `) c" |% u# t7 y* o) z* y
listen 80;
8 w5 V9 z7 c, M3 w& G5 ~server_name 52os.net www.52os.net;
$ N# e! Y: |! R, b0 d. }& w! g; J9 k2 ~
location / {) K, _7 m$ ?) s" y: o' ]
ModSecurityEnabled on;
/ Y4 f# A, a1 H! G0 z' W$ \# y0 V ModSecurityConfig modsecurity.conf;
% A, r& _" C9 T+ [4 H7 s* K! E( Q7 S/ w
proxy_pass http://online;7 ^8 ?: c$ q, c: d, ]
proxy_redirect off;
: Z: H4 ]6 a; f) I' K3 A8 I proxy_set_header Host $host;
" @! r1 U1 x0 e; J proxy_set_header X-Real-IP $remote_addr;% ~/ {* ~( L9 R V
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;% v8 e$ s- Z# O( S" y% S
}- G5 [7 j8 B% M& n
}0 ]& h/ A0 k* i# |
六.测试+ B( m0 X/ S8 K1 K
) f4 t( B: k2 a2 j3 \
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:% P/ F, p+ F4 E
* s! ?; K6 r8 H. g" T<?php
. ]4 g9 Z1 E# o4 | phpinfo(); + Z% b" N1 D; v8 T+ _
?># Q# Y3 v9 \8 a: w
在浏览器中访问:
% Q! q& i5 a0 @. m3 A9 G7 z0 c' G7 b! h" U' k7 f8 j( R
http://www.52os.net/phpinfo.php?id=1 正常显示。
# P# a4 @0 L' ?+ I4 ]9 Hhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& m5 U) x! T" g( f
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
' v! ~- Y+ \) H; ^& ]说明sql注入和xss已经被过滤了; r$ O# a2 E9 N4 N" M
) P! p7 Z: e9 }. D+ x5 m; M七、安装过程中排错/ `4 ^ r2 h) G8 z4 ]7 ?2 T
9 ^( s$ V+ t8 K( {: k1 ?9 X# `
1.缺少APXS会报错, o- u" i( ` {/ M
/ ?2 y- `+ u( u1 x; d4 }" l
configure: looking for Apache module support via DSO through APXS
9 l6 L; f& B- e! U3 N: Yconfigure: error: couldn't find APXS& S' c+ X9 @/ v' S
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
* T: V$ B+ R% ?0 G I- y解决方法:
% ^/ |* ]0 e' P# d3 m
( a# b- R+ ~( E: m/ Syum install httpd-devel
* T- I* _% \% w5 {; e5 g2.没有pcre3 w% }$ S0 P+ K8 z3 c
2 ]8 l; e9 K- p N
configure: *** pcre library not found.
# I3 I1 s7 B/ N" M" u3 hconfigure: error: pcre library is required
+ {$ m) d+ E G7 a5 j9 F解决方法:
# ^: F6 v4 y" H W: p
9 Y; }: ^* h a: ?+ N; Xyum install pcre pcre-devel& B c6 U. v& g/ k4 q( X
3.没有libxml2% ~1 k) L2 J2 i: R) S
P! k( w, q' N7 W
) f+ p4 |+ C5 @configure: *** xml library not found., j w! T) }6 V3 H2 J7 e5 L
configure: error: libxml2 is required
3 }7 z$ b" z* t+ ~解决方法:
! [ z8 X' I8 q3 W: X5 a9 V; J# Q) f. z$ n. }- ~1 y( |
yum install libxml2 libxml2-devel
% j: _! T* d& A& L2 u. P/ @; Z) m/ z2 S4.执行 /opt/tengine/sbin/nginx -m 时有警告
% n0 I9 V2 [; w; ^' l6 u
' Q* H2 y* U5 H! f3 _Tengine version: Tengine/2.1.0 (nginx/1.6.2)0 V( K9 \; m2 f# I
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 u, [) j' W9 V4 G7 M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ l- L" u8 `5 t6 P: _' B' N& [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; ?" T7 M2 \1 W# Z- M; a; R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
7 t" J6 \$ t7 S$ g& i2 z2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ j+ @, O* L$ G2 d, T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# |% X$ q1 L3 z A" o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" P' Z6 N& ^* A. t2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 h5 `2 D6 d0 { g解决方法,移除低版本的APR (1.3.9)
8 U9 T2 E' e( s1 s5 Q( \
; {1 Y9 i2 s/ e+ X, G7 n* t) H8 Ryum remove apr; f. Q- G, Y( v8 F* \- |$ \
5.Error.log中有: Audit log: Failed to lock global mutex
5 x6 |# a2 d9 y' e. G6 w8 c0 H$ f( A' v) M ?( x9 h6 A+ u+ S( \9 i
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 I# B* C" f3 r+ m- r$ C e3 lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
( w. P2 \+ I; D2 v& Y- {+ H解决方法:
/ v- n! U$ l9 s4 q编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- t. V3 b2 A% z G% C2 S, M
# u& b( E* l/ L t; j4 c
SecAuditLogDirMode 0777 N( F/ u6 A; d. G* k- P: s! I
SecAuditLogFileMode 0550; _! |' ]' d3 L4 o. l' o( x R6 R
SecAuditLogStorageDir /var/log/modsecurity3 x/ t" i! `* t9 w3 z' {: R
SecAuditLogType Concurrent
( T* C) t# s/ n# r参考文章:
' l+ z$ A6 s1 x; ?https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
+ W6 q* f* W5 b8 t6 Chttp://drops.wooyun.org/tips/2614 |
|