|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% l' ]' I! O- s1 q- u2 _
& C' `! i5 J2 k; w) Y一.准备工作, m- w8 W; A* G
2 `! B, B9 S$ k5 q% W
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. f9 P6 u5 D. O
) S5 K' }9 Q" q- V% Dtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz0 h& ], w8 K9 |8 ~, S5 a4 o
7 x! K7 o' A% l) S9 s* Vmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz& \" O" I4 l! |" h7 ]5 S
- q- w* H* ]3 _+ w2 r e4 w. Z0 @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 P, a/ X3 }7 z( x( k) k% `5 O
" B6 k$ [! {) s5 E8 I1 T! n依赖关系:" Y& B" k8 C& f8 A: z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ T. Q0 T' y& t; U/ a: D" J! A/ S5 m
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
$ a+ E. b* U i# _) Bmodsecurty依赖的包:pcre httpd-devel libxml2 apr- E4 j$ p: O& c) y/ t
: G, F! n2 ]1 L; M7 n0 ^3 Tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel& h' ?: W/ S7 m k& w1 r' x2 M0 c/ r
二.启用standalone模块并编译
3 \2 T0 O Q: Z8 P9 d- ? P3 ^9 R3 D% F
下载modsecurity for nginx 解压,进入解压后目录执行:
% H: R# u5 D9 ]5 w: A1 c
3 q/ b' o! g+ e, ?; S/ ?& F; E( N, r./autogen.sh" C- f8 a+ y+ P e# c. k' X
./configure --enable-standalone-module --disable-mlogc
" E6 D+ N/ v% f( lmake ! {5 R- ?3 v, B
三.nginx添加modsecurity模块
7 V8 z) P/ A1 g" t3 Y5 u, s
0 X# {! E/ S4 F! \7 J% X% Q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 z! ]4 ?, @) q! G3 [5 ]+ T
6 N" E+ ?2 h: y* @, Z
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine) G7 w! W$ F: k8 ^
make && make install, X$ |- i2 Y q* u v4 g7 s9 t
四.添加规则
! s" d, e* s7 L5 e# N
; O, n( }8 q* \& a$ Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- @& ^9 G3 a+ B% V9 l( p& L
# E3 [# `, ]8 r. n8 r4 m1.下载OWASP规则:
: G' h/ L* D- [8 ^0 M" v- x0 S: S6 u3 y7 Z, f: [8 G, o. g
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ t8 A2 \0 j6 r) d
& K& V) A- r8 M: a) e4 [mv owasp-modsecurity-crs /opt/tengine/conf/
! R8 y- U% U$ d: t8 @) }, i8 J$ T& a+ e0 m" m0 R) L0 |' e
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, z2 a, D6 v1 |4 V/ S) _1 K6 k: N
2.启用OWASP规则:5 `' J5 r- m' s' _1 e4 Q
' X# z5 F: a8 U4 s; J
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
. G4 E/ T( a( ^/ t' F. j
6 u6 E6 W: S; t9 S编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* h. {6 S+ H+ ]7 @
% e8 P ]8 c2 _" T7 n5 W5 A; Cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 c; j7 l6 O, @# f/ n
8 q" |/ a. S$ B/ ]2 b' `Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" E1 o( \+ T0 h) ^
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; X) X& P$ V h( \4 y4 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% U. o3 L/ Q* T8 E" M
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 y4 o: Z- R% o9 z* u% UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- p0 V: l. h6 w3 {; k( D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 ^) n" F8 ]4 ]8 j1 E9 a6 R& ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! k- L# H2 o: v0 W* p7 Y
五.配置nginx: j2 V# y+ t% u; o& J, B6 R$ b0 W6 _8 `
' Y9 l8 m) K' s1 P" t
在需要启用modsecurity的主机的location下面加入下面两行即可:( Z5 K2 ]4 K0 o1 d: P; ^! c
; n6 f) \' @* ]ModSecurityEnabled on;
7 h2 k+ F8 F2 y3 z" s/ s! W1 CModSecurityConfig modsecurity.conf;' o# ~; q" N: H# w
下面是两个示例配置,php虚拟主机:
4 c4 ~6 R. U: [& }% F+ ^) F
3 A* A; ]* I4 t* d# U/ H) rserver {9 z- A5 d3 h% u/ S9 T
listen 80;5 Y: N; b: J3 {. f8 k! o5 V6 ?* N
server_name 52os.net www.52os.net;7 f+ o9 H/ s# ]$ u
, S) i+ R5 L% q
location ~ \.php$ {
" x. ~- `( I/ g8 s& m+ d. f) {1 M: u ModSecurityEnabled on;
" q" J/ Z! O$ X ModSecurityConfig modsecurity.conf;
) L4 N9 z- F# |, u5 R4 C9 @$ t, j0 b
root /web/wordpress;0 a* S! m+ }! j. f5 K8 y7 A
index index.php index.html index.htm;' v# S7 k+ ]% V. [) r
) N r: M# c0 N0 r9 w1 j
fastcgi_pass 127.0.0.1:9000;; Y! O5 K1 h3 z$ j" C
fastcgi_index index.php;
$ Z U4 B& z- x1 Z fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; q& @0 p, _- N( D( @
include fastcgi_params;
" r3 X: ~- X* v$ E. o }3 F. V: K4 e* \) G0 h
}
: d& I. f2 F9 x' e! J, f, Dupstream负载均衡:
/ a7 s% c4 B3 K- e/ V
: }$ M2 [4 h! b, d4 E [upstream 52os.net {
+ D! u2 R! ]- i% l7 Q6 | server 192.168.1.100:8080;( k& ^) Y5 [ ~ F6 l
server 192.168.1.101:8080 backup;
# f2 \- P- n6 }}) y3 h: S6 w U; u/ c& ^' x
! k! I! W: f; vserver {- C# C5 b/ r" b# p. L
listen 80;+ J0 ~1 B% N. }( S3 \* p# E
server_name 52os.net www.52os.net;4 u N4 A7 ~4 I3 X
- Y5 Y) z3 O6 n! d" B0 ylocation / {3 O$ i9 G' s: _0 y
ModSecurityEnabled on; 4 M& r9 C& {. w0 |
ModSecurityConfig modsecurity.conf; * V8 o6 {( U) H; O7 k1 h
4 |' a; l3 u q I* y
proxy_pass http://online;
. s" H2 K( F3 a+ L proxy_redirect off;
" G, K5 c9 ~( G& _- V proxy_set_header Host $host;
5 ~% F7 `+ F W: ? proxy_set_header X-Real-IP $remote_addr;3 T& T! _6 i- [$ O6 ?3 Q' ]6 S' I
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
) p" z5 D; i6 a$ l( Y3 [ }
4 W3 H6 U$ I f H: m( D}; O7 \" C* F. ?4 B% W' t
六.测试
* p, Z3 J9 z; ?; Q: u
: Q* P( r2 |3 y& F5 ~2 v' f我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ Q0 z% y4 d5 T5 U6 h0 d, B% A/ U1 S8 s4 K: e
<?php
0 d d) d. Z/ e# {( N phpinfo();
- X0 F/ h8 U, O7 e$ A?>) x0 h5 m7 R+ E* m* u: e3 ]2 y5 t1 W
在浏览器中访问:; T3 [5 s! [. x) P
, i8 r# R' l. L5 E5 P
http://www.52os.net/phpinfo.php?id=1 正常显示。
% T8 T9 t! f/ ?; u% S+ {6 xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。* v( v$ ] J( G# y' R. ?
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。+ Y2 g+ A5 }/ k. P1 I6 o9 x p
说明sql注入和xss已经被过滤了0 n) p8 Z, _# [, n% @* y
; S8 ~ Z/ C/ r/ h( i* d( ^
七、安装过程中排错) G9 N. J' B4 O+ \; S1 M
J' ~/ @, E Q& I9 O9 p+ ?" K* |1.缺少APXS会报错: \ o& s, g0 V
) v5 v1 @7 F* P+ w/ y% oconfigure: looking for Apache module support via DSO through APXS
% r' e$ p5 b T- Jconfigure: error: couldn't find APXS
V. {- d/ F [, G {- S/ I5 {: capxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ s! {7 M* u* Z& e/ ?# |
解决方法:
3 c) K+ ^0 \/ N' `* r( c+ C1 H7 o- X* f3 N+ W
yum install httpd-devel: b5 @' {. R! L6 s" K
2.没有pcre. A# G8 M) Y; v/ q1 I
3 N: l: J0 n5 j" E+ R0 Iconfigure: *** pcre library not found.
- W! ]3 j$ d3 u- Hconfigure: error: pcre library is required' D) k! ]- y1 j: ^8 A
解决方法:) _/ ]* |% J! d. J- m8 ]
8 K7 n3 h, Q- Y) p8 iyum install pcre pcre-devel
% W, ^# {" ?, x2 [8 B" D2 y3.没有libxml22 H9 D4 E9 k. h( ~
5 Z* H# z$ f, B, N& I* a
1 w8 H' H c! n& D# Y
configure: *** xml library not found.' l$ o( \$ y0 A
configure: error: libxml2 is required
a7 t/ N! d* Q8 s6 N解决方法: T, w7 S) @# P6 z: P, {
7 K1 \: G& J/ L/ K
yum install libxml2 libxml2-devel0 b, x: Y7 Z& @; `) {( z9 R
4.执行 /opt/tengine/sbin/nginx -m 时有警告" d8 T+ D: c5 o
2 @& }/ ^/ r6 ]% d8 |, f D
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
/ K" v2 Y7 q& F# q8 H7 c" @nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" H, A+ n5 Y9 Z4 J4 k3 Y0 }1 D
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 M2 n+ T) A) R- _4 E* [
4 Z+ P1 _, }* V! g$ Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 v% A2 I' x$ e/ E- e2 V2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". ?$ j F' S. l0 g* @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
+ c+ r: s: r/ [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 k* S& B( m, Q6 h/ p- \( w% V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", p& n7 `3 J% k0 X0 s" v
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 N/ k! w) K4 D0 w$ Y$ X解决方法,移除低版本的APR (1.3.9)( A) S2 G+ e! z) d, J! f
/ n; P4 g8 T/ h" C( ^2 ?8 Dyum remove apr* T3 T7 H6 d/ U, j5 v
5.Error.log中有: Audit log: Failed to lock global mutex
. r; a* S+ m3 y. g* R) G) ~' q* G3 v" k& a- K5 p1 b5 ?# A& H
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 @7 @" ]! R* Y# D
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, E K3 R4 T) }8 W# Z, V解决方法:3 f/ ?5 U6 H! h: [2 I; ~; y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 y7 r9 W9 I2 D ]- M0 _
* u4 i( X9 r. c g" B* aSecAuditLogDirMode 07778 D- L" E: v* }" r
SecAuditLogFileMode 0550; N5 d7 O0 G q) K
SecAuditLogStorageDir /var/log/modsecurity
$ G0 j, ^: ~. x0 @& ?5 kSecAuditLogType Concurrent( Z$ B( x( y( s
参考文章:
7 H; y" j8 @( ^https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ C7 r' F: L* X) M; x4 s
http://drops.wooyun.org/tips/2614 |
|