|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' j' R- p" x1 u6 C$ z- F/ n$ A
% K" D& W$ o5 ]6 Y" b一.准备工作
( D: z1 {4 M3 ~5 V0 o4 Z4 E5 w( X7 s: s$ v% k2 C( g0 i
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( z8 D; x; {$ U4 G" f* D$ n( t2 Z% q, n8 ~* _! x; `8 o! w0 i/ G" f5 p
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ T( r! {$ o: @& y
, U. E& W$ ~6 V# y1 Q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
7 I/ t0 Q2 K8 D& K# {' B- [
% z+ u0 k5 I& r4 L4 X. NOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 [) G+ R" g* U4 t3 k
0 W3 U/ s" |) A! A. a依赖关系:
5 O9 t6 D, ], o' l5 _0 s0 ctengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
6 `/ D. I: M; I+ \4 O% ~! n$ k( B* m* s7 o# g
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' y- r9 @/ Y6 x. @- X- I) cmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( D: a I9 v5 f
! h. B& W( C% F6 b4 P1 ]* gyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel8 Q+ N- m( K0 V7 R* C7 Q0 m
二.启用standalone模块并编译1 P5 F4 X: x1 N$ E6 g8 S. _
% Y" y% {# G/ p) ~
下载modsecurity for nginx 解压,进入解压后目录执行:
9 Z& w5 u' l0 y3 |2 R$ y* J+ E4 G+ ^& N& P9 |( G
./autogen.sh
! Z8 d$ e; N0 E( \& h./configure --enable-standalone-module --disable-mlogc+ S/ H# o6 p/ }* _9 f
make
7 Q% L$ `7 ?7 a三.nginx添加modsecurity模块
/ j5 t% x9 M! E. J% B; n p! S" j8 y; @6 F5 X& I# C4 ?2 a+ w
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) R- i, c: a/ b& M7 [0 `6 J9 S& X5 E T$ k0 @
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& O- D$ N8 x6 qmake && make install" E- e: H7 w# `& D
四.添加规则
: U" Z9 W+ a3 I# B: E- I, T6 p0 T* b* g* M& \
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 S$ G* B4 T; o, F* I' n4 C$ z4 `+ p, s
1.下载OWASP规则:
! _) B4 d% j6 O K8 H3 i
) H* m; X; o, F5 W/ U+ I5 hgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# y9 U; _% {* q5 q/ x- U% h8 I* K, D
mv owasp-modsecurity-crs /opt/tengine/conf/, a' f' i' B- R' y8 R, n
n/ {) m6 K- ?6 Q) j, ^# vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" a( n7 J! j3 V9 |( _' S
2.启用OWASP规则:, l0 i3 Z1 B( a' q, a9 y- v
5 l4 B$ R0 m& I- @+ j- ^
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
6 R6 s B' I2 _- t0 N$ W# l9 K9 I/ m x3 E8 M& B
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
q" {2 S2 Q4 n4 n& X4 B) ?' q4 H0 m
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" Y* M G8 l: t0 e3 d# B7 n
2 n \% }! q2 _
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 Q: \/ t- S/ v2 M' d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* p0 C) q3 H7 T4 g; F; \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ i6 C j4 R+ ]; ?% ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' l) @) K6 E1 V; j6 q# Z! y$ M
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 G6 K5 a, L/ t0 S' A2 x+ vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% ^0 S8 |; J& u3 R o" ZInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! }9 O0 e/ x6 {# Z4 B
五.配置nginx
+ a% H) v! G) K% t. y7 Q. }
* Y/ P9 ~. c: x* c$ o& x% b5 [' C在需要启用modsecurity的主机的location下面加入下面两行即可:
$ [% n t+ e7 M' r2 |/ v, d! v0 w" ^ P& M( Z) w
ModSecurityEnabled on;
9 {, E% H3 p, p) v9 A( l5 JModSecurityConfig modsecurity.conf;
! [/ L* O+ a9 T- ]9 Y6 }( X下面是两个示例配置,php虚拟主机:( `' y& S+ q7 |) a
1 D5 G9 V, \3 U0 E# ~! b1 x" T
server {! k6 p' w8 ^7 [( K$ W3 d- D7 v: @
listen 80;
' T& ~) j; E# I1 W server_name 52os.net www.52os.net;
/ ?! j; C( w/ t4 Q$ q( ~% m: R) c) `
/ N) f3 h/ s* V) i location ~ \.php$ {: X' A h, e# Z* U' M; X1 ~
ModSecurityEnabled on;
+ W" T; Q, G$ X/ q5 B: h2 d ModSecurityConfig modsecurity.conf;& d, Q" B/ _8 c) ?6 ]2 X
b! g3 S4 l u1 [ root /web/wordpress;1 D) F, k; W! k& Z$ Z
index index.php index.html index.htm;" c7 L, e5 b9 t4 d; V5 S
+ C4 W7 N+ \- `# p3 ?" E' ]. Q
fastcgi_pass 127.0.0.1:9000;
2 i( R* y/ w" h: H+ [! m. Y fastcgi_index index.php;
% Q: R5 w9 l; h# W, \0 M* m fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
( J$ Q* f" E7 T8 T, S% n# z include fastcgi_params;
8 C" i# C2 v- u }# b1 U+ Z6 p$ ^: G( G0 u
}2 m- ~* K2 C+ U9 j( p! y
upstream负载均衡:
! n9 L, J j$ @; n; \7 V8 `) B+ w, j; J; D
upstream 52os.net {* R2 v& ?" ^0 b7 M% X& H! ?6 r, C
server 192.168.1.100:8080;
, c5 v* B* X' P M, y. Q0 a3 q server 192.168.1.101:8080 backup;8 n* a+ j6 n( l# @! u; n' \
}8 w6 `+ T k% X- ~/ ]
7 W9 r% w# r+ v% ` gserver {
; u: u N% i0 F `2 Z* d" S% o6 vlisten 80;" s2 ^4 [8 @3 {, J K _2 j
server_name 52os.net www.52os.net;; }; }0 G: H/ D+ T! e' ]* y
% {" `- S$ R4 U! D [; Rlocation / {! G* J! Z! a" Y( ^% N3 }& G$ M' s
ModSecurityEnabled on;
& n# d$ c4 o- J( p ModSecurityConfig modsecurity.conf;
$ L) L& B- X( a! y1 ]* |& j) V/ I5 f6 ]+ E" S
proxy_pass http://online;5 I) @( N$ D! \3 T: k/ ]. o: h
proxy_redirect off;
, P7 F0 b! A. F$ e proxy_set_header Host $host;
9 g2 i* ^: u" ? proxy_set_header X-Real-IP $remote_addr;5 b+ v J- [0 s& ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 S* z% Z' f( H' ^: B
}% c: P6 v$ R$ Y; f
}1 p% Z! x8 H- h
六.测试
8 T, |8 M4 d* Z @7 r7 w6 |9 u( b' |, G5 ^1 u2 n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% L4 r. {" D* r$ k( d- ~5 s9 A/ g
6 Q9 R- f9 @0 C9 g, w+ q<?php4 G; _# \6 X' U, c; J* J
phpinfo();
9 G1 E: e7 d: C# F0 @?>& e- |6 K+ E8 {& `) e+ _5 I
在浏览器中访问:
2 a i% Q. M6 ]! F3 m: j3 j1 v& [
http://www.52os.net/phpinfo.php?id=1 正常显示。: z3 X7 k* P( j4 a
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. ]: }2 N, I: ^. T0 ^http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% F |6 i8 P6 g8 n2 T; b- X
说明sql注入和xss已经被过滤了3 P8 \) d; [2 j9 i' R1 F1 x! M7 o
9 _' O/ I/ h6 ]3 |7 H# q, k七、安装过程中排错
* \& a- `/ d. z) P2 B
0 K+ S$ S8 q8 x6 V- w* Q7 ]$ K) M) z1.缺少APXS会报错
- u) o1 M4 [- Q( h0 j i3 b8 n7 U1 s( }: C w6 t' F! f' D
configure: looking for Apache module support via DSO through APXS9 z6 I- X8 P3 N& S* E' ~( \
configure: error: couldn't find APXS* z, J) ~& z1 p. z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
2 {2 A) E4 k; n1 l1 Z7 | v解决方法:4 ^" @1 ?+ m5 r; \ @
7 I( [! r$ r! s7 t uyum install httpd-devel4 a: D( ?# i( R; U
2.没有pcre
5 @0 i' D u; ?7 D# o" [7 R
2 m, a) Z* v! s( qconfigure: *** pcre library not found.# D @5 o7 a" Z6 y: S3 X3 j1 Z
configure: error: pcre library is required t. i( E Q. Z
解决方法:
, U0 R; U: D2 y0 }# U$ P
. E3 |. C* _- nyum install pcre pcre-devel, L1 a2 q; }0 F3 M5 w0 r
3.没有libxml2- [- j# p. O3 S* O/ |
+ J3 w X m" | { B1 U& a
2 Q& `& c2 p% Econfigure: *** xml library not found.( q$ h8 T! `( I `
configure: error: libxml2 is required5 n+ e6 r+ {) \: Z" w
解决方法:+ @5 a% V/ O$ I: E; e5 a) F* L
1 h& e8 C$ t, W6 Q7 u
yum install libxml2 libxml2-devel g+ p6 d- |- X j; ?
4.执行 /opt/tengine/sbin/nginx -m 时有警告
x z N8 u: ]" Z( Q6 W+ _
8 s8 f' l1 ~; `) RTengine version: Tengine/2.1.0 (nginx/1.6.2)+ y' Y+ n9 L5 B" N; I* ^
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; W' F% \8 _, A* y3 F' Q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) l+ b1 A/ F: f+ I" _+ L' [* O' u% Q) I3 V
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. R7 @- M+ b& K0 C1 n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 g' m4 a2 B* m# T' ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& h3 I1 j& M- Z$ `' \ t4 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05". _; N7 ~* d. e& _: b* w Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 \6 j2 ^- W- _+ P
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) k5 I. S3 D/ w5 r5 u解决方法,移除低版本的APR (1.3.9)4 `( K+ }7 Y7 z# o6 p
; _8 i1 X3 i) y6 ~5 Hyum remove apr
) X: D# a; M, a, Q* {5.Error.log中有: Audit log: Failed to lock global mutex
0 Q+ [' ~; }! `/ i/ [
2 ?2 m0 \+ A @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
" z3 E- d! ~6 Q: ]# W S( rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
& S, x4 P9 r; B解决方法:* y& E9 M5 z( q7 p7 v% q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
/ h, R) [* ]4 M7 D
2 d* l) E! C+ iSecAuditLogDirMode 0777$ E$ m, ]1 n* x' A3 X3 Q
SecAuditLogFileMode 0550+ Y/ c& @/ k! z- ?$ N. z5 K) j
SecAuditLogStorageDir /var/log/modsecurity
* D0 a) C! K+ R4 f6 ^! B7 l1 d8 v1 Q1 zSecAuditLogType Concurrent
; \# V) H& y) ?& E7 @# G' H参考文章:9 I/ T! W) D- m) A% l% ]
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, s- A0 I6 T+ B. z# y
http://drops.wooyun.org/tips/2614 |
|