|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: J9 X x2 D i
+ _5 u/ ~5 H. I5 e* a一.准备工作
* d9 p C4 [3 }: P5 S
0 q7 u6 H( Q1 o5 o9 K. T系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0! @+ u( L3 B9 i* f
9 i& r; }' `: _: D! D9 t
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
9 L/ d: f! }- a& B3 Q) W) w& s M
! U# l8 N- z* k, ~; Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz3 B F1 n# N4 G" a- F9 V/ Q
+ Y" v* B& @$ ?% p9 yOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: I j8 h! x. D! k+ Z f! a! ?% p; T1 Y9 O4 s, [
依赖关系:
! Y' A6 X4 k. Y) |5 \' etengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
0 r, M, p3 b! r9 m( u8 @& Z9 E8 u! l% L0 Z% V" A$ U: i3 m
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel+ `. x. Z3 x9 ^/ y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
- l8 i1 n- W. r) i8 d0 T$ ^9 z/ D. |, u6 `5 \2 K
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
; o( O. E, I2 n, T二.启用standalone模块并编译8 G+ @( \1 }7 f
9 a5 ]% W" Z6 N* ]: h5 t0 Z下载modsecurity for nginx 解压,进入解压后目录执行: {# S- G. H0 [3 N
1 r, F$ X. I* k5 F/ i6 F./autogen.sh9 [# U( V7 g: X9 J9 l7 q$ D
./configure --enable-standalone-module --disable-mlogc
; z, p" c. u2 [* w+ qmake
1 z& _3 l8 _# f; y. s+ P& N三.nginx添加modsecurity模块
! s1 n- X7 J" ^" W4 ^2 r
; g+ T: o4 T9 Q2 R' ]" G6 |在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 M u2 x+ v* ?% x# {% t7 n, o
. P. J0 r. Z4 Q ^4 h1 S./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& i& M' U0 ~' Zmake && make install
, @6 N- q! d2 A L5 ]四.添加规则
# M9 X' E6 ]+ V% J. w+ |* N( @! F. m# E9 D
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- Z3 @3 A8 C7 m% w2 w: m0 {4 u) M; j0 Q6 v2 I, j4 g- v
1.下载OWASP规则:
" H1 Q1 t6 y3 X6 x4 Z- l
2 G n4 r* {% o; _git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. a9 o8 h" b- e. N" n5 S) k
- Y& K. j* |( W! P4 j, Xmv owasp-modsecurity-crs /opt/tengine/conf/& I' {, d! U3 a- W, @
% E1 p$ q) B/ P: r4 rcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 }, o3 v7 B; \2.启用OWASP规则:
& a. u4 ~5 H' u B8 a. B( t$ p# e5 s/ E# r" t1 E U
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 }+ R, y6 y+ R2 I
3 d$ c& [; L6 q* ^& M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( X4 C. Z* l# z- p; d } s1 S9 \" z6 u: |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 {3 [1 j7 J. H
: N9 p. i& e3 w1 S! t' E: kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf2 o. d& P* @6 d4 Q" Z8 M0 V, ^% j
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf" _7 J5 F* M: H& t
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( {* f6 @# C( T+ h# X+ tInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
m3 Y0 ]# _1 @2 Z0 b4 f1 m( cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
6 D: L+ Y% }( _* y$ ]' |; v: @: n. q( v9 aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" k! j# l8 A* m0 Z; fInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 C, i% ]1 v( h% n/ x0 ~4 F五.配置nginx
# C. W3 ~5 ^9 ~+ u
; H2 m! l% r* K在需要启用modsecurity的主机的location下面加入下面两行即可:; i0 T- b+ z( ?6 O# b
+ g+ K9 n, k: H: _8 E- ]5 k7 m
ModSecurityEnabled on; $ C# K" \! J! L2 Z$ s7 r9 `- w' \6 {
ModSecurityConfig modsecurity.conf; Q% v" K7 r9 L( a
下面是两个示例配置,php虚拟主机:! Q- E6 c8 A; |2 E
- c: o2 C# N/ @
server {
/ ` D8 i! Z! D: w6 ^9 V \3 H listen 80;2 }/ o5 {# `9 y8 }" ^7 S
server_name 52os.net www.52os.net;; ?& T+ x+ M; ], L. N, M; h
8 I' o- X4 @. B! g' b) M' b location ~ \.php$ {
6 r5 S3 l6 ?0 n3 |7 W ModSecurityEnabled on;
R: ~" }7 i: K! c& E% E ModSecurityConfig modsecurity.conf;/ j6 a. w" f }! W; ^. J
* j! `2 @- _6 O# a: t k root /web/wordpress;
6 ~3 J5 y$ J# v; ^8 n index index.php index.html index.htm;# T0 L; j5 k* m q
: l6 q$ I2 K( h6 l f7 c fastcgi_pass 127.0.0.1:9000;5 S: `) b0 `+ X' q: K5 T" B5 w1 N
fastcgi_index index.php;
" j, F* m% o0 U0 P fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& J' j F, z2 s1 ^' F include fastcgi_params;: f. u$ [) M( u8 p
}0 \$ h( O- t# G
}" x. Z! H+ o b2 p
upstream负载均衡:4 c+ O4 r; K( Y
. P" i% w" m* {/ b4 ^
upstream 52os.net {
/ S5 o- r- ?! B n5 c7 g server 192.168.1.100:8080;" P4 a2 ], G/ z7 W) C# X
server 192.168.1.101:8080 backup;0 j/ x* q- m* w6 n# u
}
3 g- ` d0 z( B
1 t% ^" _! |7 U. ?/ |server {
, d% Y, P& F! S% elisten 80;* p) P {0 F2 h
server_name 52os.net www.52os.net;4 `6 z- T1 P8 I+ f3 i0 V) n- u
, p9 j1 [' R1 Zlocation / {( ^: F7 ~( W5 [, h. z1 l- B2 E- u# K
ModSecurityEnabled on; - k2 M; Y y8 f* @+ `! l9 ]% j* L
ModSecurityConfig modsecurity.conf; 0 C9 g3 M) ?9 }* N) n
$ b3 y, \. M3 L proxy_pass http://online;4 i# J' J$ c) [0 n, |0 R, a
proxy_redirect off;+ _8 K- S0 P# V ]3 r, W5 [
proxy_set_header Host $host;$ ?3 x- G. n6 ]0 [4 C/ ?
proxy_set_header X-Real-IP $remote_addr;$ t5 F2 c6 A* h& F6 I7 x1 _
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
: Y; H! n+ A- }5 F }% s. g- ?- ]- Z. l; O6 H" n
}
1 p: J4 _$ U( Z- x, W/ ~; t7 A& E六.测试: u& \" o1 R; a. y" i
N) F- H- O$ ?9 g1 W; `6 g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" d3 i- L( Y# o% j& G& G$ }! Z6 G* @2 C9 b0 B4 p
<?php
/ K o2 M# J* c0 F! l phpinfo();
5 k/ M$ w7 O2 g! G?>% \: P2 F, n# q: i
在浏览器中访问:, J; T7 Z* R. ?8 p; M0 u* \
$ [2 \7 x* F( Y, T; \, q, Z4 ^
http://www.52os.net/phpinfo.php?id=1 正常显示。- C0 k: I6 j) W3 Q( K
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
% J) m( K- R% Z4 M4 ?( nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
V) u4 {" `9 V+ P9 H& }7 O说明sql注入和xss已经被过滤了
( g9 `* @3 {# L3 y' K
. S8 b; ]- d. s+ o( _2 w! W9 R七、安装过程中排错; F. K% \6 E9 S3 H0 k
; ?% f! h, M8 L$ W# D2 Y
1.缺少APXS会报错' e! g! G: L0 h0 Q5 h) G
* R2 M/ T- V' vconfigure: looking for Apache module support via DSO through APXS
& V8 e/ g, x5 ], y+ d. ]configure: error: couldn't find APXS
; W0 s8 e/ h) ~" K6 ?& Fapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ I& c: [- ^% A6 f1 G3 |
解决方法:- c0 P' u9 p1 c& x
+ V1 ~0 v/ e* u4 k: U* B
yum install httpd-devel, B7 L. i$ p1 G" |( b
2.没有pcre
; q, \* i2 Z; d+ ?/ r/ @1 H; X7 t$ o3 y! _! m- ]) }. S' k f
configure: *** pcre library not found./ Y$ D& b! Z% P
configure: error: pcre library is required" a0 W5 k2 k8 P
解决方法:1 g, c" p) W/ r* C
0 y9 G [. S5 W
yum install pcre pcre-devel0 D0 l& q# V W; u" Y1 W, b
3.没有libxml2. o' B6 Y5 s$ O! n7 p
. ~( W/ e1 J: |2 S+ s5 M& Y2 ]$ o8 h" |( R3 b' h
configure: *** xml library not found.
9 ^9 j3 X6 \8 c2 `; dconfigure: error: libxml2 is required" A9 n" b1 ~. e3 K( _3 l
解决方法:* \# H4 u2 h. G3 e+ @) p7 ~8 i/ @. e
! f: ]: Z: _: q- G' c$ v2 @yum install libxml2 libxml2-devel" n( r5 W8 b$ s
4.执行 /opt/tengine/sbin/nginx -m 时有警告) X7 j) s9 m& X: ~0 t# U
2 A" m: ]) t& t. J+ cTengine version: Tengine/2.1.0 (nginx/1.6.2)
1 M8 ? V, t; Pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 E; N& }1 [9 \( h' @原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 R2 \, E0 y8 u0 g4 L
+ B; P9 y6 i- Y" N% {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 y' {: m: U7 @$ u; ?8 l3 S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"/ i) {% q9 L8 l( S+ K: @8 v6 H
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!- ~5 F# M% @$ Q! J' s) w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ ~) D% A" c8 Q& Z3 y5 f2 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' O% _$ J( ]! l- h
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; w6 }: R" Y2 S$ \
解决方法,移除低版本的APR (1.3.9)
4 G9 j5 e6 Y" F5 ?; X6 b" [3 S( M+ f O. h; i8 _
yum remove apr8 _) |/ j) l1 g; K: s
5.Error.log中有: Audit log: Failed to lock global mutex
/ s: N, ~$ G; n. M5 [% [
$ n( u9 }! r) v7 e2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* d7 ?" N# Z1 N z% d7 P2 Rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' }/ s5 u6 q& ]- C9 F
解决方法:) a% A z+ W. e. M, @
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 T5 X, K& A9 k$ A5 P; F
3 {1 A- B* P/ j xSecAuditLogDirMode 0777, |$ B( ~2 I8 {5 b, B0 S; F& H; A
SecAuditLogFileMode 0550
4 w+ Z; C1 D M, k7 _4 b' h, M, USecAuditLogStorageDir /var/log/modsecurity
# a) k* X) P6 s; ySecAuditLogType Concurrent
* U( u+ X7 O- V参考文章:
3 J: K3 C0 v8 Y. ^4 B1 chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
4 M' Y9 Z5 H0 p. m: }4 y3 R$ Rhttp://drops.wooyun.org/tips/2614 |
|