|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! G! z' o# T1 M" N3 \2 r5 q+ Z3 X8 q+ U2 ~; R
一.准备工作
6 W: A# S- s `! G& x* a$ Y! S8 `" `5 |" {6 j _( o7 n8 `
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 V* |4 M- X) F! ]* `* w5 j% q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 U7 Z" D G+ r1 Q1 \: l! c0 L! X7 a: b+ \' C2 S
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ |! h5 s9 U) W' |$ ]8 d" K' }" U4 k& s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
4 g9 b. c+ D& e8 k+ E. a, r0 A. R5 L
依赖关系:- [6 c/ h- k3 z" O3 _
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:) W, ]6 i/ o1 v
9 J. R0 Q) W# c5 Q; T; P- gyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 n: _* Z# u" x, R) P, Gmodsecurty依赖的包:pcre httpd-devel libxml2 apr2 Y' B4 a& o+ N6 ?
: |6 F: n! |! \3 p3 H$ x3 [# P5 s
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel. R7 o* e; X3 F5 O. t# J6 [4 J7 X
二.启用standalone模块并编译6 Z% ?4 k8 E7 C1 T2 {* x
- k9 _5 k( I- w, _, R$ o9 B# R
下载modsecurity for nginx 解压,进入解压后目录执行:( Y6 |( J6 O7 I. v/ q) {" p. \
, Q7 v1 @4 i1 G! d
./autogen.sh
3 x. n$ ~8 ]8 y" K& Q& f) _./configure --enable-standalone-module --disable-mlogc0 Q- m+ A% j& {% x7 W7 l" h6 `
make
8 m$ I% L( ~4 h: L$ A2 B* Q三.nginx添加modsecurity模块
- a' Y9 E8 t) \# _6 z) I9 _; }& B' ?4 F) F0 Y) R. K0 }) S0 r
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 G }3 |' B# ?7 b& V' ~
5 ] v* H/ ]1 ~./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
, \6 J9 D F. Q4 F0 p- R+ ~make && make install
# k( ]/ j/ l+ x" M四.添加规则
' A1 o: K: @3 v+ o/ m, E
& R9 }2 S+ ^, }: c* l" p- y0 emodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( M v4 H9 H! f" Z
% _, A7 K _/ v1 s* `1.下载OWASP规则:
3 T8 g! M7 x- H7 |2 D6 ]+ G& D3 m+ W8 ^& {( M/ O
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* M9 z" R/ j4 `8 e: h# a4 P7 P! V2 ~9 l9 y4 k
mv owasp-modsecurity-crs /opt/tengine/conf/" q" l( z* H( i1 \& e" Q' q8 o
+ X9 u# _. v% Q/ L# m! K
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 c h2 e' S Y2.启用OWASP规则:
; Q" d* p- v+ H0 R# w6 p: U
8 ]* g; v* R+ N! H复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 ?& R5 ?0 h R9 K
, P) z) T; V: w j& k# w1 Y" J! Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 r- Z( I7 h5 Q# U; U
/ V, K3 ^, h6 J5 dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
5 i% h$ _* O. }) j1 F B/ I* X( T: m
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 @* X/ [4 u5 rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' B) Y. |: b6 n# e0 lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; O$ C5 w, w. X6 X1 Z) J0 e
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf& q. I4 U5 T* _+ q% k; p6 ?' m; e
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. Z% c4 W1 ] [% q0 O0 ?7 ^' R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" ?5 X& j) H7 F, {& [) [% N1 @Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 O2 C! t+ w" X* r8 a/ A五.配置nginx- V6 K7 c8 p8 b+ w
7 M z" q. t6 h# k
在需要启用modsecurity的主机的location下面加入下面两行即可:% X0 ]& i! E0 |9 I
0 s _# _0 v, E( `' \/ ^ModSecurityEnabled on; # m. k3 L) z+ k: D; t6 E' D
ModSecurityConfig modsecurity.conf;% M! b- K) x1 K/ j
下面是两个示例配置,php虚拟主机:
$ p; |. t% I" x: Z3 ?& m [, r5 U2 ^2 L, y9 u+ D2 l( B$ i
server {
. o3 M; w! \( |; L$ H& @# K; X listen 80;
: o0 @8 O; F( @! b. a @; V server_name 52os.net www.52os.net;
2 G! w/ N4 ^' I6 A # H0 v7 ]; U' \+ v. M
location ~ \.php$ {* j# ]( R' Z! W4 k* w" n
ModSecurityEnabled on; 6 \9 ~7 @' C, f! n, e" \& W
ModSecurityConfig modsecurity.conf;
4 X% u: E! S8 p3 B6 d" S
7 z4 O' G+ [* E( U% I9 ^8 J root /web/wordpress;
8 u5 u0 s9 P4 n( a index index.php index.html index.htm;
/ @" N" S5 `7 V7 z3 P' p 9 A- X: J, k, \ A$ o4 }1 W
fastcgi_pass 127.0.0.1:9000;) o; M+ j1 g, s; V
fastcgi_index index.php;7 S5 o% `7 f2 n I0 N: p) y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
; k: T# U% v$ V4 o/ r include fastcgi_params;: P" d2 Z" F7 t- J* z6 C
}
% n L& k( d' T+ B/ y7 j, f }: A+ H# ?" ]# x; r/ T1 {* G4 _+ R" I* S
upstream负载均衡:
( Z9 K# V$ G# s7 h$ |7 y5 r
( F( C; s" K- C5 @& G0 |upstream 52os.net {
/ \3 X; a$ H$ F& x: N server 192.168.1.100:8080;, v m8 \% G# B4 N
server 192.168.1.101:8080 backup;
0 C7 O% X" ]* J. e& J}) E9 X1 y# `7 ? z5 n5 `$ D( B% H
% O7 k' Z, _( Z2 ^ T9 \# \server {
8 K% j+ s4 J1 P. ilisten 80;
6 L$ `* p- R9 [/ [- ^/ ^3 @( ] Zserver_name 52os.net www.52os.net;
! R) H; _' k5 g( B4 n% [& U7 a- j: o! F7 W$ w$ K2 w9 M/ n
location / {
) y) r* z% U9 ?, P5 z" J ModSecurityEnabled on;
3 x- u" m! F) P" g6 g# t, W ModSecurityConfig modsecurity.conf;
6 A) }: z( \9 a2 z; S( n% V4 e' g5 b5 X4 P
proxy_pass http://online;
5 j' [& I1 ~2 \) l; e proxy_redirect off;4 ?* U$ ]& o* b5 [) B$ x; f
proxy_set_header Host $host;
: J7 y- g9 m! o3 M2 }" r3 h proxy_set_header X-Real-IP $remote_addr;
7 w7 P {/ t4 N proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 G* J9 ^0 B+ u3 P% v) N( Y
}- B$ f1 ~& M/ }% h
}6 \6 _1 P- F# g4 N0 `9 _
六.测试
6 z, J* X/ x2 m
7 v: o5 N0 N, T# c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 j7 E6 c: s2 b7 i& u
6 Q9 H6 u9 Y# a% _3 p( i
<?php
1 G6 g6 ]: ~. I( Y+ e phpinfo();
$ y9 E o$ T! H7 d. ?/ o?>9 Z( \2 i1 y F
在浏览器中访问:
/ C" E, s* t' y) n! z: o
; e% Q5 {' D" fhttp://www.52os.net/phpinfo.php?id=1 正常显示。) n5 {2 ^- b4 B9 y7 q. r
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。: n$ `" U, o& Q" N \
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. B3 m' U8 U- E0 M: }
说明sql注入和xss已经被过滤了
' x8 G" x4 C9 V9 i2 \! X5 M
. Z/ L3 \; E1 a& J, l: E! X. k七、安装过程中排错
* Q+ J1 u' c" Z6 {5 i: Q8 |) U
, c, z, T! o s+ }$ A3 r: {1.缺少APXS会报错/ I: P; [0 K9 h" H) l8 {2 q
$ r' T# j$ a* ]. y9 r/ }
configure: looking for Apache module support via DSO through APXS
) U+ G. V- ^8 ?- n/ c" z9 mconfigure: error: couldn't find APXS( r0 X8 {. J/ @9 E0 U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。 b# o: b+ L. j8 M8 t% r
解决方法:* ^: k4 e( T& c8 p
. A3 v3 n7 h7 ]& }
yum install httpd-devel3 c! j w, l# p" O( ~
2.没有pcre
. U0 R: H& Q2 u5 J2 ~7 K, [ R5 g. ~ L; Q6 ^ t
configure: *** pcre library not found.
S/ } c$ E; t' xconfigure: error: pcre library is required
8 b7 L' N8 t1 g6 O8 U" W$ B: r解决方法:' S! I& {# ^# N t4 A7 o5 m
( i/ U; e! z$ b; r8 q7 {" x
yum install pcre pcre-devel
7 h9 N1 Y0 j) y% c( d' e- {3.没有libxml2* R# B: {8 |0 T- S
9 A# `4 }2 b& i, S C' t6 N! d1 Q$ B
6 P+ Y8 q) x6 H7 X+ _9 J$ X
configure: *** xml library not found.
; V, \8 p$ E( @' N, b$ `% Fconfigure: error: libxml2 is required5 ?1 c8 c* O4 @; k9 k( n
解决方法:! b' _# `4 _9 G7 I+ @" i9 G
& v0 N: w. H+ }. w6 |3 _; hyum install libxml2 libxml2-devel5 C8 g/ h5 c5 _* L3 g) K
4.执行 /opt/tengine/sbin/nginx -m 时有警告: c1 M3 a1 E' r, [5 c
/ L ] P% r+ F, N. e6 M. o: v* fTengine version: Tengine/2.1.0 (nginx/1.6.2)9 z! S. c8 b) @
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 D8 Y3 \) E; w
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 p* k4 r) `, e; i; [: c% ^
( z5 ]3 T% c3 R% q9 O! A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( \/ j) e7 u9 ~: a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 M0 m0 r& z8 l4 r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 |1 _& f* i% g4 n/ U& A; _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
1 c: `' B g3 R9 ]4 r( H6 C8 f" A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"9 h) K3 l4 S; O! a5 J+ ?1 V5 s4 L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 Y8 m, I! ]2 d9 [0 O; S- L
解决方法,移除低版本的APR (1.3.9)
4 Y. u" i! {! \0 y' l
( q7 p- M8 t: G# p7 c6 ?6 e$ ?yum remove apr g0 \3 i2 _ W; C( `- e, a
5.Error.log中有: Audit log: Failed to lock global mutex/ T/ b$ J' k% N3 P
, _, o: S, d. ]3 g2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
0 @! h a, R+ W/ C$ Dglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% ?: q9 D1 L7 C/ r( q5 n解决方法:; m# }6 c9 o5 i* e( N) M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:# R" m5 U) ^5 p9 C- t( }* m
4 ?9 ^1 _" M: z, f; g
SecAuditLogDirMode 0777: c7 c% J7 B4 ~
SecAuditLogFileMode 0550
% J! t# U9 J, `* t8 @SecAuditLogStorageDir /var/log/modsecurity9 H% u: R; }2 |: r4 I9 d9 F
SecAuditLogType Concurrent
$ r8 m# n7 G% o+ o参考文章:
J% W8 A0 {( { h- N4 G5 U/ ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 L; o8 V; @2 L. dhttp://drops.wooyun.org/tips/2614 |
|