|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. x* v* d9 p( s
A f- A% v& i0 c0 @9 O' {一.准备工作
" x2 K8 H7 y; @ g, q9 Q( P
8 j& D* x$ j( f+ i8 f! K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 w7 Z1 o% L0 E" q/ ~4 T1 i( x
- X0 R5 d( E9 U7 p. g3 ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
' W6 ?1 V7 r& a6 V+ W% k3 K& I; n2 b* b4 }' I- z9 }, i
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. @: m" D0 K7 n3 \' h" y: y
* Z& f/ e6 a& Z/ cOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: h; R3 f# ~/ i0 Z+ K p) D
# I4 H+ J7 P3 J: ~ y4 H
依赖关系:$ m& b1 l' I8 u8 ?8 \' ]1 V. C
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ U: O$ p5 W2 _0 z8 g( B, p) s, p7 P2 @7 z- r. n5 a
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
; {7 ^/ x0 @, h# A) I. \; umodsecurty依赖的包:pcre httpd-devel libxml2 apr! w5 o. ?% z2 H( x3 u- V' ? r) A
. m8 a5 O$ D8 q4 n, R/ L/ Iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) }! d; ]. J7 L) a7 a- g1 g) b二.启用standalone模块并编译+ X* {9 O( h1 N2 Y' O9 z& X3 o
4 s" ?0 o- M* w, d1 y
下载modsecurity for nginx 解压,进入解压后目录执行:
& ?# O/ m) {# V8 E# s- s6 E6 w5 [$ ]
R2 Y3 Z3 D5 s7 [./autogen.sh
+ m' M; i6 M: s4 L./configure --enable-standalone-module --disable-mlogc* {4 d4 z$ d2 N/ w
make 6 a( y8 E0 Q$ ^+ W& g+ A- i' M5 {
三.nginx添加modsecurity模块, W+ ?7 i! H( B e6 h# O; P
7 d4 N+ _5 Q1 h |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 i& T. a+ ]$ Q5 q# q$ q
" p0 v* `/ U% Z8 O! O+ H
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine. p+ G1 `$ \7 j) M' h( O; z
make && make install( L4 v1 j: X' k: F5 G1 j5 K2 Y" d
四.添加规则
: d! t. _: F {. L
2 q; Z+ C* D: y- Amodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' r: o$ S" N9 P. K7 e6 S3 o6 d! n
% o: I2 \% m6 f* ^1.下载OWASP规则:
7 ?- n$ X ]( A% `) I' T6 B
$ A) J0 M6 Z5 [' I1 Ggit clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 j8 `: K* q2 R
: f O- B1 [; z/ X" \0 Ymv owasp-modsecurity-crs /opt/tengine/conf/
* L' i2 L7 K: P' t2 G0 R2 A# d+ s `* E* h
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
( j; N9 \3 f" H+ B4 O; b2.启用OWASP规则:7 k4 c' Z6 G3 W
# ]+ W J+ W8 }0 Q! G" P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: ]: k( a( e. b. P4 t/ w% S, V& Q" d6 {* Y6 X5 H; ?. h+ o2 w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on2 G) _& T# K( J! ^
1 a0 R8 j0 C( v6 z9 D% X. \
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, P( q" U! I" S8 H/ j% w1 G3 T
9 A0 ?7 a& i+ G8 |. rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 }- u S* A3 S/ o$ g. J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 D; M0 T. l% y7 z" tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf& v2 z6 r3 \4 ~/ E! r9 B# m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ c/ l0 o4 g, S- C* D1 F( i, d. zInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) ?6 R$ K: B$ n9 LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf* ~2 \& ]0 T& W: ]' r- A
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- x, _ K; J, d. k
五.配置nginx& S% l. `& o1 H6 w1 S
* h* H$ ?2 U D6 s3 ? ?3 Z) V4 b在需要启用modsecurity的主机的location下面加入下面两行即可:5 K8 d7 v9 m. H( q* e. e
F2 b+ Z9 A9 _" MModSecurityEnabled on; / q% |" r0 f* P$ b1 C d. `
ModSecurityConfig modsecurity.conf;
- f2 K) o1 _5 ~ @5 @1 ~, B- e" N- C+ A下面是两个示例配置,php虚拟主机:
: e$ u) E6 E6 z9 q \- W m0 t r7 f! ?$ f+ U6 l( f' C6 d
server {( z l1 B1 y* k: }9 P( z& E9 _
listen 80;
4 S5 Z: R& ]$ a% _ N7 h8 d# z server_name 52os.net www.52os.net;/ J$ P' ]: v' M8 z3 e! }
& W! [9 s. e6 G& X
location ~ \.php$ {( e$ b- f0 T' b5 U' F0 J2 a
ModSecurityEnabled on;
7 v# d3 m: P; X% r8 I( n ModSecurityConfig modsecurity.conf;& n. W1 G4 e% t, G( T
: Y# `& z% P! b& w1 M, S! ~ root /web/wordpress;
2 v8 u4 X4 u% `/ v: r" C index index.php index.html index.htm;
, l. y* J/ ^/ E5 j: D& W3 ]3 s# n ; o: e u; `, R7 S2 x
fastcgi_pass 127.0.0.1:9000;
2 a+ b8 \% j4 i5 N4 _. V: {# Q- t fastcgi_index index.php;2 W" i( {8 N1 v3 [% h
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;% u, U2 n5 S% j" _( ]3 i! K+ [
include fastcgi_params;% @, m8 z: t7 \4 D' U
}" _2 Y2 |6 P; e2 m( b w. L( T7 W+ R5 e6 E
}
( Q- t" J2 y* I7 a% o pupstream负载均衡:
7 n8 f8 U0 e: ?8 j8 a. r4 |1 H7 o! x
: F0 v* ^" W. B2 X5 m6 D8 wupstream 52os.net {
0 j. c y7 |, ^1 W/ Q6 z) w% v server 192.168.1.100:8080;. _- x% ^ @1 a/ K
server 192.168.1.101:8080 backup;( |- V: _ t: E9 ~
}
B- A9 r- C% @* {8 o' @' K3 G! F. d4 u R7 \
server {0 z) [5 Z; W- i" {$ Z9 }% e: k/ T7 X
listen 80;
: Z3 U+ f/ g# M0 g* o' Dserver_name 52os.net www.52os.net;) P6 H8 n2 C" m0 F& I. E
) a4 r# b3 @& p3 S0 C& R& slocation / {
( w' U9 h$ `# A! m, z0 z. N) X ModSecurityEnabled on;
0 z+ p' Q" M/ v) L- w ModSecurityConfig modsecurity.conf;
6 q* F$ |7 M) M7 y
' K& c2 d& ^8 s7 F- H6 Z proxy_pass http://online;- D* I: ^1 c ^" Y
proxy_redirect off;
1 h* c# L( V- p$ T1 @; S6 A; c proxy_set_header Host $host;
3 R$ H4 R: @. E proxy_set_header X-Real-IP $remote_addr;
5 F4 ^. I- X9 z3 |5 y# S8 Z proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
# @9 H& C9 L: I q8 r" t }
! [& x+ n; J6 w% t! s}
6 U' o: h1 ?$ U7 Z六.测试, x) ?. K# r% v! b) P0 ]
0 m9 Y9 t- z- O3 }! u2 s; L- a u
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( I6 A7 N5 w6 \+ L7 B3 i" @4 o
/ a3 ]: s+ ?7 F7 D8 l0 ~+ l<?php& g3 U) {" Q7 G4 y; Y
phpinfo(); 9 e# |+ x5 R- F' C4 c
?>& _* P* X% x# [4 y! h8 W6 D
在浏览器中访问:
0 p7 l7 ^2 z, f& p: e% P7 x: Q
6 A7 \- t) b. G _5 ^http://www.52os.net/phpinfo.php?id=1 正常显示。
. R! n, }! @/ L; V5 K) H" Thttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。. y0 D ?; y, }- @: v( W0 z: t
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 G8 I1 N" H: h4 t3 a- b3 c
说明sql注入和xss已经被过滤了
5 Z, H& R* u# F1 z6 J
- ]% H* Y) l! X" X七、安装过程中排错, m4 N5 a" p4 ]( o) M1 R
. T7 U, A3 L& Q' u1.缺少APXS会报错, ?: y( Q6 V) X2 r3 ~; P) W( k- @
, R: J3 z U% fconfigure: looking for Apache module support via DSO through APXS. q* ]4 {- s" `
configure: error: couldn't find APXS! T) b! Q- Y+ r' [- B; p, ^8 o& ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
X: u, u0 k9 v8 U& y5 W解决方法:
. D. l2 n. \* l1 r$ ^1 a/ b& b
- e: N$ L% h9 `6 X3 Nyum install httpd-devel8 h0 l+ L$ x r4 n3 o; h$ R. A
2.没有pcre8 b( r2 o: q& B. O
* G* J1 R. w0 L, V1 o1 N. ~
configure: *** pcre library not found.. X+ V! r8 ?& i- w) Y2 B1 d! ^
configure: error: pcre library is required
: g( t E, X7 C( i# v# G$ |解决方法:
# y% B# P# I: b7 D3 `8 S' \
4 R5 W/ ~% a2 |$ K$ ^yum install pcre pcre-devel1 C3 @- c4 c' l& y+ o: K
3.没有libxml2
% i: ~ a& ]1 m$ I6 e# m4 h! |- i. V3 q4 b# {8 N# O* e
4 M/ T( Z, X1 ?; {. `7 J
configure: *** xml library not found.
y) J5 G4 w: u0 t& Z9 Wconfigure: error: libxml2 is required# B3 ~0 B) g& V- k! l! k
解决方法:6 Z: i; P4 m! e7 c5 o6 w8 v
4 _1 L8 ?) }3 `/ ?9 ?' L1 uyum install libxml2 libxml2-devel
% ~/ p" _: \ p3 j b4.执行 /opt/tengine/sbin/nginx -m 时有警告; M% |3 d6 H, K! v
6 G1 I) v" p5 o/ y3 G( ?6 f
Tengine version: Tengine/2.1.0 (nginx/1.6.2)# j! c, [/ n0 z3 Q! i
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. q8 C" E# t6 o' `9 z) X" t原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& r/ e S3 n! v
' V* f$ s1 M6 o& \% |# R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
% b! ^8 o$ b- w) P# d4 N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"- s) d, @0 Y- \: b
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- m7 `+ G- J0 C( T% r5 I
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 @2 @) K) ?" I- }. V, j" c4 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": N5 y" p q4 A. [
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., I' W& Q2 b$ N. E( t$ m4 k( X4 @
解决方法,移除低版本的APR (1.3.9)7 T& G4 _7 d; D2 Y3 r" O% m. y
9 h7 K3 y0 T" g1 |1 O- y/ Fyum remove apr( p0 d7 R; s) N# g( P
5.Error.log中有: Audit log: Failed to lock global mutex. t. o+ B9 {+ l+ [. k# o) K, V5 N
) _7 O9 Y9 U3 _+ F3 |- @
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
1 Y- O% X% [5 y! b# h+ i% O! ~$ qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; w+ E9 r6 w% S; l6 H+ L# e, i解决方法:: Q0 H& \% S3 a. [
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 R0 M6 y0 R3 M, P& {# L
& A: \: y% ~3 {$ p3 M- u9 O8 [, uSecAuditLogDirMode 0777
3 }: ^6 ]* R; T9 _4 D9 NSecAuditLogFileMode 0550
L- X" B6 g+ s$ o8 }$ ASecAuditLogStorageDir /var/log/modsecurity
& {0 }+ I; @. \1 U& x) `1 U0 JSecAuditLogType Concurrent
/ x' B: t% C! z6 E* c9 {参考文章:( R# K6 \. E: H* q$ O
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& _# J* D0 G, }. m$ ]7 S/ R6 qhttp://drops.wooyun.org/tips/2614 |
|