|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: s0 G2 ?2 f- f6 H
% T( |& H2 s6 n8 x一.准备工作
' ?& D$ Q k! _5 W" ^( s9 N4 W \ V4 Z+ h
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 _" K M% F( F; A+ U5 y( E
( P$ e2 u+ o3 [* O4 J: Ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
P) I/ V0 m3 h& m: {9 \4 j: b7 D9 \5 f2 c$ \0 K
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" R" C x6 `$ ~; ~* K+ G- \% g4 X* Y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# ]5 S |0 t) F: k- r3 [
$ [: H3 C+ a: b4 ]- k: k
依赖关系:
5 M3 q& G4 Y% r9 U0 g5 q otengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:. m* D- b$ L& K/ L
( r& k( |. B; k* X+ Y' G' V0 C
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel+ q' R# A: V! z% X5 X3 L
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( j e2 Q) R" ~& Z, O/ ? y$ A+ b y- ]5 [& d
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
3 U# n2 T$ E. { {4 O5 F二.启用standalone模块并编译
" b! Y) N) @ C, {0 W) C, g+ f
O" e9 n1 D/ A1 Z: `$ r# h, I下载modsecurity for nginx 解压,进入解压后目录执行:
; B1 I% h3 p2 |: C$ r5 k6 n% t
./autogen.sh4 [& I# o* }" o$ m- V, @
./configure --enable-standalone-module --disable-mlogc5 f" H1 h# C# m) l( \
make ) E! S" j; p/ ^ B
三.nginx添加modsecurity模块, ~$ B: J. V/ t( J
9 h _, J, J* ^; q3 C
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 S3 h- p- n* m+ ]1 V. x" a5 V: ?
# S$ q, e' x4 z) b4 J./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
, Y7 J) [5 Q: b( {2 ` O5 V: X- D" lmake && make install! E- a3 z: x4 ]; k' ~* e2 K
四.添加规则3 s* _' D a. T5 U
2 I! W- B9 M- B6 k7 g. I4 s' @/ h: qmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- x; ~9 w3 G6 H- p% S7 l; B
5 J5 \% m' g) [9 q9 K; a
1.下载OWASP规则:
/ R" a' S6 F7 x$ g3 @, e1 {# S7 k. ?0 @* P2 h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs( z' R) M( j8 Q; q
0 B$ V% E8 y! cmv owasp-modsecurity-crs /opt/tengine/conf/5 }0 a7 y+ ?$ p
* q. V/ j0 S; R7 L: H( ~
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
* q+ M7 I4 y8 u4 X$ r9 b2.启用OWASP规则:
: x7 F- j( d' x+ C8 ~5 y3 [+ F
) [' a+ K. x4 G, V8 }复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。" D" {9 J. Y( C* O) T( D. ~, w- r6 f
F0 y0 m/ X4 d3 V" N7 W( Q
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ Q7 F4 ~9 J/ @7 F' D$ r
! w+ \1 }7 ]- X; _; nowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% B+ o- Z" V3 p4 i5 i
6 C3 X2 ^# k- f3 pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- k0 {2 f# M% z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
+ D0 Z! Z# m# [+ G1 [% @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" W6 Q# K( ?" u* y! G9 c/ GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
/ ] R0 ~* X+ g7 rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. p+ i; E: ^: d* b1 ZInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ G D; {& j' l; h
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& Z5 U. F" C5 J- s% w* C3 e
五.配置nginx
5 P- Q O+ {4 R: S4 G1 }4 ~9 q/ X
在需要启用modsecurity的主机的location下面加入下面两行即可:
- Y$ I2 `9 a# ?& D8 A4 a/ T" p2 ]! ?
ModSecurityEnabled on; 4 o+ ^- g; l# N# t. z4 r! Z
ModSecurityConfig modsecurity.conf;/ l' X6 L& t% \ ^! R4 c( h
下面是两个示例配置,php虚拟主机:3 H; j- H* [* ^ Q3 W3 G
* u5 p, F* M- U1 D% E; qserver {
0 K# v& }* Y* c$ z, R. B( D S listen 80;. }( B$ x* @' ^" H g9 C ]
server_name 52os.net www.52os.net;! _, i+ r5 A M, c7 r: }
: [: ], @) A1 A2 E$ c. u location ~ \.php$ {
$ O# Q- o4 s" L% J. v" y! l8 Z ModSecurityEnabled on;
$ U$ t/ d$ p9 r, O) C9 T5 L! l ModSecurityConfig modsecurity.conf;
+ S2 L: R8 Y! x2 U$ A
* l3 l* x# b/ O% F! G root /web/wordpress;
! u. ~" F" x: L0 i, ` index index.php index.html index.htm;$ f. y: w$ p1 u
8 W7 v' ]2 |; \& E6 k/ w! ~
fastcgi_pass 127.0.0.1:9000;% x/ r7 [& B* u! F8 q8 \
fastcgi_index index.php;1 r& w8 K: {. M. t3 i7 a- |3 P
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
9 v* q9 }! }4 c; E2 F' h8 r/ v include fastcgi_params;
: u+ l* Y: O8 }( o+ S }
, w4 `% N5 S( k7 {% A }
* W% z. g& A2 l' Q# |upstream负载均衡:
' t1 b, H: j$ l
6 G$ l: ] ?& V2 y* ~2 E( f5 hupstream 52os.net {8 j: C* Y% y6 X
server 192.168.1.100:8080;8 M( @6 ~# O9 [- p, S2 `
server 192.168.1.101:8080 backup;* n8 n: t# X1 H* n$ O
}
) V( F$ N, d- e* t1 N! a4 i$ T) ?2 m4 |# g
server {
9 G. K% x6 z( ?3 L4 b4 U# Hlisten 80;/ ]0 j* J( X. y1 @4 R
server_name 52os.net www.52os.net;
; j/ \. k$ L/ R. U* a* H# }. G) z1 h5 Y6 k/ P& o
location / {
& Z8 {; Z7 Y' P: C- d/ Z$ ^ ModSecurityEnabled on; 9 T: G' S5 i7 c% O
ModSecurityConfig modsecurity.conf;
. {# b7 A I M. c U3 O i1 n* y9 J% Q& |
proxy_pass http://online; A& j4 `. z$ Y# `" X3 T" {5 @! Y
proxy_redirect off;
' W# u9 i+ e, k" @! q* A% u& G' Z proxy_set_header Host $host;
( V4 ~. a4 a1 D4 n proxy_set_header X-Real-IP $remote_addr;
3 c& g9 s# ]: ~$ o Y: Y" A: ?1 R proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 L1 F* n h" r7 E5 J
}! h% U+ P4 H7 q; J2 o/ z
}
% X4 K! Y6 z l+ ^* t, G" t' y8 f六.测试3 C7 \; |1 W y5 S
' N* `% R m9 }. a7 R/ {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# W9 V& }3 H# u+ `6 Q, x
2 U1 d/ Y" k8 Q( F
<?php
Z2 w1 V# {- B; E phpinfo(); 9 g8 o& f! J( K
?>' B+ G7 L6 N+ D# C; N
在浏览器中访问:
* Y. G- J) B" _7 ^3 d q& L$ _% \6 _! [7 h- [! N
http://www.52os.net/phpinfo.php?id=1 正常显示。
$ D9 W, y2 C1 L8 b( jhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
( J) P8 y L. z9 h& F; |; i1 v+ R9 A& F% Ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。* @1 L' J- |/ A% o3 s" V
说明sql注入和xss已经被过滤了+ Y% T; C( K4 J; w
. L: e' V7 I( S
七、安装过程中排错
. h I6 `# V' T* k+ j9 t& W, p% c' B. L4 y4 E
1.缺少APXS会报错4 }: _ h) M( a& Q
2 U( s$ `8 n& Oconfigure: looking for Apache module support via DSO through APXS
" R1 l; y+ Z; }configure: error: couldn't find APXS
" G/ G& S# G* O. Q5 J3 Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" S1 u: s- h& q% m3 P) g& g& R. V
解决方法:
1 [9 I5 f4 n0 m7 k
& ]4 V# U* p+ G0 T- o7 m$ Uyum install httpd-devel$ d; a% j2 I; q7 w
2.没有pcre/ ~5 U% p0 `3 T% @" [) A- R
0 ^$ }& \$ A3 W* T cconfigure: *** pcre library not found.. k- k9 y" C- p& |3 ~ B
configure: error: pcre library is required
- a! {, e& C$ `- d/ y9 _' h9 z5 O解决方法:! F2 ]; B: G& E) e
% M5 W0 ~+ [7 c- K
yum install pcre pcre-devel
|/ U# j5 b- f3.没有libxml2
2 u7 q6 o* a" k% E$ D m* C; Z* n9 @
" ]: v- Y" Z2 j' I$ E) r$ U
configure: *** xml library not found.* Z: b1 {7 O: v: O" I- H
configure: error: libxml2 is required
% }# @( V$ \6 ?, g; D2 D解决方法:9 H" d ]) G1 a0 J4 | v
( A5 B' x0 h5 {) j( O N& Z
yum install libxml2 libxml2-devel9 z1 V9 z! J; f1 T: Q
4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 j* b5 u5 `5 r. i/ v3 a) q. m- Q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
1 { @8 |* W% y: h0 enginx: [warn] ModSecurity: Loaded APR do not match with compiled!) [' a# ^! O* c" ~. ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 `/ T+ m1 |2 ]4 u
/ l( ~" q1 |* j: ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 W Q7 B' t3 u8 w- T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! H2 n1 Z# g+ T
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" ?& }3 R2 S8 F6 d; f7 ^9 m8 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- H% `; Z5 @2 b% y3 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"0 h M8 R- F+ F L0 y! k a
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." g1 ^% ]0 k8 {+ \6 n+ w& u2 [
解决方法,移除低版本的APR (1.3.9)
" w- M% O) M% C' j& M
+ S0 F0 [, }4 I9 _7 dyum remove apr1 t; G$ z, E( r, i, E1 S) x; V
5.Error.log中有: Audit log: Failed to lock global mutex
& @0 O+ K) m# u2 H# Y8 @9 g
' v5 `, `# F: ]2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , _7 G& l1 g: I
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
Y" U. y9 F4 D9 j( U2 p: U解决方法:
- [4 z- H2 s# L7 i2 Z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 |' s4 p! U7 u( {) u: b t
6 u6 t" u" f7 J
SecAuditLogDirMode 0777# { ~) {; _( _( w
SecAuditLogFileMode 0550
Y' d4 a# L( A: c; ^, K$ E) TSecAuditLogStorageDir /var/log/modsecurity
( J+ x9 L7 V3 C5 R- zSecAuditLogType Concurrent
' K* d1 p o' E0 X参考文章:- [# L2 E U0 b3 S: v4 Z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" M _9 M( p3 z& P0 y1 r
http://drops.wooyun.org/tips/2614 |
|