|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。- Y% g5 l0 q( E& o& H
9 J" V- H2 n5 B6 M4 m
一.准备工作2 Z% T1 R+ x1 O
) E2 t. N3 i' v1 E! x, m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ d. o3 ^8 c& {0 I4 J' l- B3 p* g
, [& Y# F6 A4 f' I# x+ ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 K) A1 G; k6 v: n9 o/ y* ~$ n# l2 p
& p( z Z& q# m7 Q1 A- p, L+ z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' U" j( [/ Y- }
7 n1 ^6 p4 l, X9 E0 H- T% V0 F$ ZOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, Y9 k3 G$ V' j0 h2 n7 T, C
9 B4 d3 ^2 Y5 }' a& [3 k依赖关系:8 ?- i% n# C$ P
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 H7 ~: U& a5 `/ z2 O$ R ~8 R Q: g p3 w- V) k# {. S
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ H7 w8 x; N9 _) U
modsecurty依赖的包:pcre httpd-devel libxml2 apr' _ A, c, @/ O
8 o* o: O4 @+ |2 Z- U1 }
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
, D4 t6 S& P" x. v y$ j7 h. e二.启用standalone模块并编译- i; D% A9 a3 P$ p3 e, m6 p. |" H
( o2 T2 q6 e% s! p6 i下载modsecurity for nginx 解压,进入解压后目录执行:
7 U% I- `3 l1 h' ~+ `0 X
% `, h, q. s4 Y z./autogen.sh
( R. x& s4 t, B./configure --enable-standalone-module --disable-mlogc
5 K( B9 y' z" @' j( E3 Amake / y$ l* Z4 F; o4 y$ w! t9 |' X
三.nginx添加modsecurity模块
( m" f/ E* R( z' \+ w+ ?& q) d/ | ?( Y, ?
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) w( ^8 {+ J, h0 ?" R$ E+ Q+ Q
4 Q# q6 a+ k9 r3 w- u% T2 ]: D0 R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# l; p- f3 G5 g3 ?$ \2 h* Kmake && make install. _$ ^& A7 B# ?& a
四.添加规则8 `3 u Q, y4 E4 S$ ?# R5 N. F/ w
/ n/ b3 g, N+ t; j- A) `6 u5 Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& T. o+ M; |9 H7 ~. X" G/ L( S4 @& ~" S$ ~8 P$ Y, x
1.下载OWASP规则:
5 A L9 L( {- b1 A: T- ?9 J7 F8 q6 g y9 f5 y6 w
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs! Y7 c0 c. ~9 t/ l8 u2 S( B
& s5 @0 I. p8 k6 {2 e; k# U% s
mv owasp-modsecurity-crs /opt/tengine/conf/7 {1 l! ]5 Y4 ?' Y9 w7 J
/ Q. q" o/ T5 d6 G0 B& ~6 K- Q% pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 L* G! ~% E! v+ H/ q6 F2.启用OWASP规则:' q8 T# W, b2 C8 n$ W
, s2 i, \3 a4 [ {$ P, p复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 w4 r4 i/ ^' I2 Z) G
' d: z" Y0 W \( k' p编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
, ]1 v! {5 y" P" K9 l) x0 Z/ F) G
; A$ d+ m e( O2 c, ^owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) @# w, Z6 B& E2 V. f) H
5 G, T* j8 ?5 x7 g! o5 s6 j+ TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# v9 t9 T) D! l) W0 [8 ]% R. j, g2 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf9 h# [" N' L( M7 U1 z% c, W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 E4 V9 L6 x* [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ k: _$ `. v( m9 m; t5 D4 @
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 v% O- T+ \1 `1 W' T$ Z- u: NInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! |3 Q* H! H( o% t! _
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 B' @/ K4 I/ G0 N5 D/ N五.配置nginx; Q+ A5 X* O, y" e$ q0 J2 x* i7 o
0 Z* Y, \* M/ r( g! K5 Q) I
在需要启用modsecurity的主机的location下面加入下面两行即可:) a L+ ~5 j( u3 c
) n- d6 {8 u$ a5 m5 g, nModSecurityEnabled on; 0 t# I# C+ ]1 C( t. o
ModSecurityConfig modsecurity.conf;9 e9 Q' D0 \9 d! a+ Q; O
下面是两个示例配置,php虚拟主机:( ?0 x' `% G0 b
) L+ t, Z7 Z$ y8 L5 E6 R/ ^
server {
+ C4 E, b' Y; T; D) q/ { listen 80;
7 s* Z2 S/ @/ e+ e$ s5 f! u7 r server_name 52os.net www.52os.net;
I# f' _# q/ l2 e $ b W C) E3 Q' F" i
location ~ \.php$ {: \6 ^1 A! R: u8 t# H4 Z' u, b
ModSecurityEnabled on;
/ j: f( t0 H" s ModSecurityConfig modsecurity.conf;( w- s$ y, Q3 ~
' M8 [# h8 c- A% ^% I% g% x
root /web/wordpress;: A) q* A k$ X/ o8 \. m
index index.php index.html index.htm;4 t; M P- t0 B, J
# J! ~7 \. f! V fastcgi_pass 127.0.0.1:9000;4 ]0 p6 z6 _2 a
fastcgi_index index.php;( R# a6 S( e3 o" n2 H1 L, n% j
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& a3 O: [, r, Y- Y& g+ J, X include fastcgi_params; t8 c) V: ?4 j" g5 X5 k; G
}' x) d O! Z5 |2 I+ E, `
}- U* F: U% y: B- b% P U
upstream负载均衡:5 \- |& F) d9 G* ]1 T a
" F2 V7 B9 V4 u# D: a( X& Wupstream 52os.net {! l" @) D* A6 u
server 192.168.1.100:8080;/ b) T# A- N) e7 y: T9 g) h$ t& P1 S
server 192.168.1.101:8080 backup;0 f+ h Z4 L# R0 \- P
}
9 a6 z% o0 A) H" h4 [
& @$ J0 f, K) t& g& vserver {; Q: L! O. m, K$ V
listen 80;9 @" L8 l3 B( Q7 v3 E& I" R
server_name 52os.net www.52os.net;% G: e! K+ e/ N7 [; F
9 P i( z' p' Q0 U
location / {
/ V n. p; V& f% c ModSecurityEnabled on;
6 m/ q7 A3 h @2 D ModSecurityConfig modsecurity.conf;
, z$ B$ B% O) V# {0 N1 B) ]# D& _% V" G% }: q( f R
proxy_pass http://online;
, Y3 C& F( ^/ }, B proxy_redirect off;
( T! A' O1 e- h% a proxy_set_header Host $host;
* a# m+ E p" b m proxy_set_header X-Real-IP $remote_addr;$ p) n& G4 p; h' c0 g' [# t
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! b b" k" k3 u2 H" |% @ }
4 {; k# e: R: h}
) c" d: B, K$ i六.测试
, @8 E" r6 y( Y9 G
# I" U/ q* x1 D0 [0 I我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:' J' k& v. q; |) f
% h2 a& ~4 L; E5 |! n* @
<?php
% [* _ o$ X H$ W% e% m phpinfo();
% t" v1 P( U; V: z1 R?>
# ^7 E& P; d3 a1 Y" q! A在浏览器中访问:
# t A5 j* J' H
% _0 t0 c2 I* v1 I; O# L% d8 Bhttp://www.52os.net/phpinfo.php?id=1 正常显示。6 w- @1 l- Z$ I# h8 b0 ]
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。; L) u8 ~$ Y6 p' i; H' c
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
% v- ^$ S4 W F, N, k7 [说明sql注入和xss已经被过滤了4 | l" ?) j+ {2 [# m) k
0 r/ i y/ W5 i: O- `0 N% T
七、安装过程中排错
$ H* l# J _, u
) e- [3 R l$ e2 T, i/ q1.缺少APXS会报错& o. j- ~& \2 v; }6 U2 e" g
n1 v5 R0 e! Y3 Wconfigure: looking for Apache module support via DSO through APXS
8 t: q5 N* l" R4 ?7 b7 r& ?configure: error: couldn't find APXS
3 y+ G p8 q* a/ B: I- @apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( {% z' ]" q" w
解决方法:4 X% |8 B8 l3 I7 L* d
; w, j& N# t# B A' {* W/ @* ?yum install httpd-devel
% R+ `- I2 G( z* o l2.没有pcre2 x \" E5 @5 u) P- w. P' x
' n: Z* D2 x1 W" w; {configure: *** pcre library not found.
2 H, V% Q; P; fconfigure: error: pcre library is required
) |) z" y2 ]; N! x O* [' u解决方法:6 \' m; O- ^' ]& T' e) m
4 S/ _; F- ^" o4 m0 C/ a/ H' T
yum install pcre pcre-devel" |; ]# b% j" u$ B- y9 i$ U$ ?
3.没有libxml2* ~6 W; V; a: K8 Z! d
6 X: U# R9 ?5 T; g% T& b! V0 Z
7 O S! ?2 v& u" [configure: *** xml library not found.% ]9 V' I4 j6 C; L5 v
configure: error: libxml2 is required9 j) U7 u! V+ H
解决方法:
( n% U' H) f) _0 b/ S* E5 U6 }3 l
* ^3 e1 Z2 V$ C! J7 Gyum install libxml2 libxml2-devel
; g" K2 I, s& h4.执行 /opt/tengine/sbin/nginx -m 时有警告6 q9 u; M; U: B) _/ P5 x
# p0 _% i# G4 K# e8 ITengine version: Tengine/2.1.0 (nginx/1.6.2)3 Z- s3 x; G5 e. ?: r" [. E
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ R$ O7 C3 W4 g% M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( D5 I% O5 N) z+ D, A a2 I
% K' l$ T& ^% P" r; A! M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
" o" V8 c: h4 J: r$ f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
5 O. _+ c$ g6 C) D) Y% N2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 z7 N# M& ]9 e1 `# Y" J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
- T) O n" |( S$ ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 Z! s% A. I8 m3 L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
, h |8 C. h v1 m, C. x解决方法,移除低版本的APR (1.3.9) I+ I# w, P3 a0 w9 h8 w
5 Y* \9 m7 R) ^& V- U5 X" ^$ Vyum remove apr7 F0 b8 _+ n4 @0 v# _. T
5.Error.log中有: Audit log: Failed to lock global mutex
- C+ t) w* c4 ?2 v( D6 F8 C5 g, D& R# v, l1 N4 v
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
0 S: g! X8 [ z8 \/ t: v0 `6 mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) ?0 z) ]/ s6 w1 B解决方法:
3 K; e6 {6 x4 y% Q' |6 F1 e$ ?编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:$ o5 P% K; H& W* `
! Q; B0 \$ c4 Z, m4 Q2 M+ \: rSecAuditLogDirMode 07773 p, k. v. ]& }0 \8 u
SecAuditLogFileMode 0550
/ f6 ?" Z6 ~- i3 K- {5 z6 hSecAuditLogStorageDir /var/log/modsecurity
( A8 R+ j, ?% U, w Q4 d1 V3 T1 jSecAuditLogType Concurrent; q$ \+ N, w8 Q* J% v# f
参考文章:
- m% @3 d2 p; |$ E9 nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 l& o7 \! d: V% E! Z+ Ehttp://drops.wooyun.org/tips/2614 |
|