|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ Y) n$ E" w* y4 u
. o* q- F( C/ }0 k2 o一.准备工作
' [+ Q" `/ V6 K
/ S9 r- A9 n; G$ Q/ k) q3 ^. N系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; T4 V. [0 }/ M9 k+ C) |/ c' H, ?; o( O& p9 p1 j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 I- }! G6 L) P+ K0 |7 y
4 j9 E8 O. G! B0 g9 X% mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ s! S- X- b3 D" a9 Q- |3 I9 Y% e8 e. r1 e8 L5 {3 C% q% U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& d: S5 U7 B) R- \
" u+ A* ~# K, A5 I5 T6 u依赖关系:: X3 U3 C9 h" J5 B8 F
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* H) u* `; G7 ?2 [9 ~1 m5 [$ u- {( H
; ]9 y8 e% Z3 a9 w. u2 _yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- { h! ]' I# G- v/ h
modsecurty依赖的包:pcre httpd-devel libxml2 apr) s( U3 j3 q" H+ r$ M( \. z+ X
- e: h+ c- E, z2 d/ Fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
6 ~' c- v, e1 h! b; y4 i二.启用standalone模块并编译5 ?. L! d( h: Q
- |) O$ y' X" Y9 ?) l9 t下载modsecurity for nginx 解压,进入解压后目录执行:
( {% X* l" x, v4 N5 |
. D$ v* ?+ K$ S) W/ Q& }8 _: e" Z./autogen.sh
( i- E- B% `) }3 T! A./configure --enable-standalone-module --disable-mlogc
! ?. s7 [1 A; c& w! {+ X; Emake
. _. e8 Y) a* O O' @- k; C N三.nginx添加modsecurity模块# L C0 }" ^* {& s
4 L6 r. S% a; n+ }6 C1 m1 [2 a
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 X! W8 ~7 N$ A. h3 F
2 T, q8 j6 H2 d8 F: [, a./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine! w- t% c0 z ?9 Y) e' p3 B O
make && make install |: t! @, ?+ y& Q8 Z. y
四.添加规则) G$ b3 s! n1 k* |' i# @4 A
- @1 ~. y( i9 e$ p. x0 Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
+ }# O [9 f$ }8 Z( _! d) S# X
% J% u# T! Z) a! d1.下载OWASP规则:) @+ w. a; v/ s
8 H( g5 A" B- ?6 J9 J& _
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' o* @5 @ [1 A/ m
9 A0 {& } ?, S8 qmv owasp-modsecurity-crs /opt/tengine/conf/+ u, ^) L5 _- d8 ?6 ]" m& p/ R
% T3 c. t* Z% h+ ]9 P, G& p& @5 {# s
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' O2 ?; |. R6 A( d% `6 G
2.启用OWASP规则:. m; o; |2 y- G! W8 `' }- |6 c
' G; E6 h3 N$ j% I
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 p5 m4 n" S+ M2 y* Z q* N' P9 k
* U! U3 a" z3 {
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 x9 x, r3 n( g, E: Q3 u0 Q
& E3 ?1 o7 a- h) Q% t4 powasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* F/ }" b; D. e; u8 i9 o
* I1 G% _) L, B9 g7 ?( o' jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 R W$ ?! S$ wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( G5 P1 P1 I7 M) a3 u8 {6 JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 Q0 k" C9 H3 K- p4 A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) d* a) m# |' ~4 y! nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# Y$ Y1 t% p! }7 G0 n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% Z: j4 b9 X2 d+ j5 N' r; G: w' s
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 s/ w( c( w* M/ I9 T+ ]9 _+ _) J
五.配置nginx
& B* H) q2 v8 H4 M6 T
) u+ I. b U) Y: X在需要启用modsecurity的主机的location下面加入下面两行即可:
( O& n. Z3 q8 e2 `" t- C2 r1 l* G4 _9 T; |
ModSecurityEnabled on; ! T- Z7 o: `: p9 Z+ W) n
ModSecurityConfig modsecurity.conf;3 w4 E1 ]% R* k: e6 \ k- Z
下面是两个示例配置,php虚拟主机:
4 M! B7 L' u u( E& T
! S- g/ O# f3 ^server {
4 O: G4 a0 ~- ]8 c; c5 C! o! ? listen 80;
2 @. C! }/ l( n server_name 52os.net www.52os.net;
7 Q0 R( U3 T+ }0 i& d ' q9 q* w9 o X$ _
location ~ \.php$ {
& B; T, I& A5 i$ p" K' |+ E ModSecurityEnabled on; ) l5 s( v/ |% [" n7 _
ModSecurityConfig modsecurity.conf;& j& z( H: _& O$ T. Y
% ?. t; d* t+ y root /web/wordpress;- ?5 l( ~' F) Q0 x4 t, \
index index.php index.html index.htm;
- b* J4 x: _0 j9 f, r, x$ `% c 3 E) H! z) Y0 G6 C7 o/ ?
fastcgi_pass 127.0.0.1:9000;! R" r p8 w, H- ?+ b# H- v2 G( c
fastcgi_index index.php;# L0 i" G$ [ m- N+ ^- C5 f
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' ~7 y+ h/ c5 B0 ?, Q include fastcgi_params;
3 [1 g1 z+ b7 Q8 w3 B* u h& r }& \5 P2 K" N/ U& e
}
0 {1 B6 H$ ^6 F; o( @/ h! xupstream负载均衡:
( H9 z5 h' v. B% D2 H5 b
* J8 E1 e7 K. N7 i1 E5 Qupstream 52os.net {# E! F5 G# G! x; V, O
server 192.168.1.100:8080;! I+ B! x9 Z( ] B% j4 v/ N& y; r9 ]
server 192.168.1.101:8080 backup;
5 e8 D* d% C- w! T* E. j. V}1 T8 t* z' w- m# i! k
* d; N% b6 M# s( M+ G& zserver {
3 j' H' ^8 P8 t/ Dlisten 80;
B. f+ k- T2 s4 A$ e/ Fserver_name 52os.net www.52os.net;+ s3 [ J0 L$ Y
: C" ?9 w1 u5 u: @6 N
location / {
! S7 p. C1 Q8 ]9 D" p ModSecurityEnabled on;
! o1 U% J7 [! t# u$ f' O3 H ModSecurityConfig modsecurity.conf; 4 y1 i* a' }( D/ n
% U# x2 e) I3 I3 p2 c: G% D
proxy_pass http://online;! N5 \( [5 K# L+ ?( _% U
proxy_redirect off;
4 }0 F9 x4 S. b8 m+ K! M proxy_set_header Host $host;
( Y/ N4 g) s: K6 B* @ proxy_set_header X-Real-IP $remote_addr;
9 Z. {7 F# n7 i3 o; }) \ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 {( C; v+ Q5 m% D& A! V; ~
}
- g; j N; ]) |& {4 k7 Q. t}
" s1 j( \$ I/ _7 a; F& Q( H) c, ?六.测试
" S( m5 L" y' U( E6 F0 e
8 i% \! |, l" @' V; x9 d我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 x% a2 k4 R5 O @, L( c# \0 Q
; I) _7 ]! ]; j( \6 R! c<?php
5 l- O/ R5 ~* n! L ]/ v |8 ? phpinfo(); ) b j, B" M4 l# X' z+ @
?>" f1 d; k% h. u- J( X
在浏览器中访问:2 z6 e% T$ G$ A: M
0 a1 x/ A5 C5 R" y6 y% w0 Ehttp://www.52os.net/phpinfo.php?id=1 正常显示。
# K' R! h9 N; o6 S3 C' W& F+ chttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! K: L" s& z2 Z- X
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. W6 n9 u5 l9 l, m" S
说明sql注入和xss已经被过滤了
* _; s2 J! e; g' Y. A
) |1 H9 z4 G3 n i% u( V. S七、安装过程中排错2 M, r" `" c# \7 ~7 J' M
, T8 e- s2 ?; o$ c
1.缺少APXS会报错
. k0 H& Q7 V! E- N9 ?# K/ T* W
% G$ M& J$ p. w% ~* fconfigure: looking for Apache module support via DSO through APXS
4 K6 t2 w M# q) N qconfigure: error: couldn't find APXS
. H6 N" J" L1 ~apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& @% g/ s8 j* H* }; i# R$ W* u& x
解决方法:
3 I& o/ n T3 V8 X7 F- p/ h, I1 O# P, q
yum install httpd-devel
" {. ]: f& ?- ]' n: R. o2.没有pcre8 g O) z3 Y/ J" T4 g j
* X, d$ W- }0 b2 w6 q! \0 X" E- y7 P
configure: *** pcre library not found.
6 e! `7 e% V: G" A, D4 N7 lconfigure: error: pcre library is required2 k n, x/ d2 ~! D
解决方法:
s C7 e/ R' l; q- z
% n9 f j- X* Q& v# d9 ryum install pcre pcre-devel
- ?* E! H9 P4 W# K x6 r/ k6 l3.没有libxml2
; q7 ?: D' _. k& B% M
* x: c! L+ G3 D0 g7 i* N
: w$ y- o0 p" a' ^: L. e! Iconfigure: *** xml library not found.: Q' U& e7 I1 N( U+ A/ N+ P7 {
configure: error: libxml2 is required- M4 }3 W5 O5 K8 q
解决方法:) y% E4 ~7 `( Z2 w" _+ s8 @
, }. W. m( g2 [% V
yum install libxml2 libxml2-devel" B' e" C7 T8 F9 B @/ N" i; T
4.执行 /opt/tengine/sbin/nginx -m 时有警告. t4 `% N' i. P+ N
# X. h) S3 e; ~0 J [$ M
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! D! @5 `' h& {# e6 Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; `! p. B- v% r, ]- K" K原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( s1 K. Y/ h& O7 B7 f* n& ^8 [7 n5 H: v" F! M6 y; E! p2 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% `1 V. `4 _, ?" A% F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
2 I: f2 D" X+ e4 B4 |9 O2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!+ r3 y% R* C, h; `: w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 f8 ^4 }1 D8 A" _" q/ b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ e* `. J1 _1 h" X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) R8 l; ^ A3 U9 ?9 R解决方法,移除低版本的APR (1.3.9)0 W" G" n5 h( L& t0 h
& S+ K8 ]" m& N: ~% \yum remove apr, L, S# t% }$ t5 K( P
5.Error.log中有: Audit log: Failed to lock global mutex, F' Z5 A$ k4 A0 S y _1 L
. X6 X }4 O6 m% J6 s$ J6 s2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, I7 ^) K/ Q4 G! k' x/ ~global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 H5 L L3 o8 H- h; ~" k0 y2 H* J
解决方法:2 o; l1 Y8 ?- N* V8 J' o4 {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& O3 P7 t! C( P, H5 P( m
8 [/ E/ j5 P& q2 ]. GSecAuditLogDirMode 0777& i( o+ _! a- o" g( S* P
SecAuditLogFileMode 0550
' A. a, c2 j% c& u$ kSecAuditLogStorageDir /var/log/modsecurity' \8 ~. O( R4 |2 Q) y. R& `
SecAuditLogType Concurrent
+ ?9 G0 l# `' x' `参考文章:
; K, r N6 U. r# A. B2 T( bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX$ t) j2 R4 N6 g2 n# }
http://drops.wooyun.org/tips/2614 |
|