|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。. F$ C0 W2 W9 u- x# c
! a% _/ {3 [: T! [4 D3 ~1 Q
一.准备工作
& j9 F) j4 C" v4 ?/ H+ ^6 U8 I0 ~7 F- W% z+ J2 x
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 o0 I; z; o' |4 a; c6 ?. k6 C* A9 K- z! M2 ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz \" G' m5 m& V/ W) I, ]
3 A# b# a4 l2 a5 }& o
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( v4 U9 G- A% Q$ c" o" U& I
! I3 i( ^8 c; l2 j2 VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. K7 [ e& r6 t+ o! p9 `: P3 ~1 y+ g; i" B
依赖关系:
/ @0 ^( \3 i' s! c% q, mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 ] J, Y/ z; Q) o# w
9 h4 p! H" M$ W/ vyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel& u! ~/ j; `2 t- D8 r! t: D! N
modsecurty依赖的包:pcre httpd-devel libxml2 apr B) S+ {* p( Q- L
' ?' D m3 Y" r9 s; h6 i
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
8 T: T3 O7 a, S) l二.启用standalone模块并编译% Y+ m0 i/ }2 e G* `% E' C, R- s
! q% D6 j7 P2 U! e" A! ~
下载modsecurity for nginx 解压,进入解压后目录执行:
x, ]2 u0 y) z6 T. v6 S$ l7 ~% F+ X8 ]. q5 [, n! S/ ]' ?/ B* `5 r
./autogen.sh
9 I! s" G7 ?) i6 D./configure --enable-standalone-module --disable-mlogc1 P4 [8 R5 f7 s1 i
make 8 i: J- n5 [4 A4 ]
三.nginx添加modsecurity模块% C, e% y- p+ q! v# O
( ]1 H9 |! I8 {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! b3 D( W, {' O9 g6 P7 J+ t3 K7 \
) H. K) b f: l3 u% h4 k4 t- Z( x./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine+ ^. p" t: a6 V5 L0 p& h
make && make install
% T# c( G7 e8 ^% M: f四.添加规则3 [, M; J; Q) e4 c+ \9 B1 {& F
6 \& {& p! w4 p) _+ p3 E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。& o3 l" H- F6 s. F7 ~
2 |# _1 I/ c8 j" |+ L) B
1.下载OWASP规则:7 _+ g' |9 \9 j% b! |# T: A
+ m6 G* o) A5 k1 l2 L# x+ J( b
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
- J, M& @* B: J. H- S# `# h
) D0 x: _9 T. u/ m& Lmv owasp-modsecurity-crs /opt/tengine/conf/- w' X5 [+ `. m, Y! R3 |. z/ a8 h
2 ]; L# j3 _. `* M/ n( ?, Zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
, u: {3 R6 E- h4 T4 f# P2.启用OWASP规则:8 W) n1 Y2 q9 d, G" [; v
' c" @) B; {7 k) ^% y- z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。7 }& F0 i# x4 [; {8 y4 L
5 |3 M* i$ K: c* G/ ~- v1 B编辑modsecurity.conf 文件,将SecRuleEngine设置为 on }' T2 l* J2 I6 ?8 ]6 q8 q2 w
5 y% b3 q3 ]$ x; |# w1 ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。, l3 S# k$ J, r; @6 e) O
4 a$ P* c8 a' u% C- KInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. v4 L0 U+ V; N) T( t( r
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) F. s) c9 }/ X, a; ]5 d# sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 x7 A4 T, _% _! l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- N* v* @5 d; T7 B$ c D
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. G: b/ s" w. nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf v6 g Q/ U |
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# K6 V' L( C' ?" {
五.配置nginx1 e# Y$ z8 _$ o3 t# ^
, W7 r L5 `& N4 b& I/ o在需要启用modsecurity的主机的location下面加入下面两行即可:
: p# C8 Q% l: O$ ] q% X! W4 a. u/ f a, l; W# K
ModSecurityEnabled on; ) V. B1 k! S+ ^4 X) t0 }3 M
ModSecurityConfig modsecurity.conf;
1 U9 X" S5 n L4 _0 Y/ V8 O; p: a- C下面是两个示例配置,php虚拟主机:& H$ @/ g/ a, `. d
: \; q) R# S, Vserver {
0 {: x! W9 l, r0 J$ o) h listen 80;, Z+ S. t& C( r6 Q5 a
server_name 52os.net www.52os.net;9 ]2 _0 Q- I, G$ t# P
% f) C+ V2 D& L1 I+ g9 X- X
location ~ \.php$ {1 Y) j% L, p H/ H" l
ModSecurityEnabled on;
+ {1 S& O! Y7 o' `9 ~ ModSecurityConfig modsecurity.conf;3 c/ b: Z t ?4 k1 `* n3 A
9 U: h* [9 ^1 |! H2 ^1 i" J5 j
root /web/wordpress;8 u- C6 B: k- b6 V8 z2 Z m6 s
index index.php index.html index.htm;
* z, Y. c2 J. l, h+ D( m . P$ a) F* u6 ?$ q
fastcgi_pass 127.0.0.1:9000;
* w& B# t0 m* D4 { fastcgi_index index.php;
. `" z L( ?' D$ Q$ {9 Y" Z; y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# b1 H7 F+ k9 j/ g0 B
include fastcgi_params;5 c, X. s, T: W% N5 X; O2 _
}
& W, ?% e& |0 j4 m: s$ x y. P1 V }$ O& N4 ]% X/ l' N# o3 A
upstream负载均衡:; y; C% R) B% l
/ G- S! s" B8 T
upstream 52os.net {
9 ~* a5 S; \" j% I# V/ O% D N server 192.168.1.100:8080;! P5 V& J; u2 M( K, M& B( C
server 192.168.1.101:8080 backup;
7 D5 r5 }7 A, ?5 @}/ X* w/ t7 k: B
9 q- p+ i6 f; j% W7 M- Y
server {
1 e' f1 k4 M3 W- U0 C' nlisten 80;
4 |2 D5 F0 U2 T- G/ Eserver_name 52os.net www.52os.net;* }- V' Y/ b' K k8 r9 ^
: Z1 V/ F, @, v8 Y8 o
location / {$ `+ O' O: R& I H( V: K) z8 J `, w a
ModSecurityEnabled on; , e9 \9 ]' M* i# `; L
ModSecurityConfig modsecurity.conf;
$ a! M G+ r1 s" }, }. C8 ~3 Q8 u9 Z, e( s9 @4 p
proxy_pass http://online; a- ]" [& h$ u
proxy_redirect off;
5 G/ V" w) \! V: o7 ]9 ^+ \ proxy_set_header Host $host;9 v. T; q, E; Q+ w. w2 P0 N
proxy_set_header X-Real-IP $remote_addr;# d6 {% d( x! m8 ^9 ^: }) F
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
x8 l8 `0 z0 |" y* h: I1 _+ _ }, x% e5 o' H8 _4 D
}
5 L. E" @3 a |/ [) L* p六.测试2 M. Z4 ?) g1 m# k
" Q3 h8 L6 z' b8 \1 K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为: ^3 F! B- i) |, @# G2 f$ Z' x
3 Q# V M% {: z! Y& q# ^7 I
<?php
6 \; J u0 q' f2 q, V phpinfo();
: C- e5 O1 Q4 J0 M?>9 X! b( ^* Y' Q! p. q, N
在浏览器中访问:7 s# q( d2 s7 r9 u5 c6 t
: L- b: {4 d2 n1 I& w
http://www.52os.net/phpinfo.php?id=1 正常显示。
+ B7 r9 y5 {0 ?' b5 O. U2 T+ [http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
! F' t+ Z# m. i# F2 V7 f7 \3 I" \- Jhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
0 M( r t2 o+ Y# P- R说明sql注入和xss已经被过滤了& R) q/ B/ x, c9 X
% n$ u0 u2 g. A七、安装过程中排错* c" m8 n8 t3 z _' j& Y0 |
- \3 V2 ^& _5 H* p9 A$ w$ E
1.缺少APXS会报错) O. p. G! y$ M/ _& j( K6 m" T
0 ~2 b3 G( i# G& `+ N+ T
configure: looking for Apache module support via DSO through APXS
+ t' r4 u- g G# ?: lconfigure: error: couldn't find APXS
. ~ x+ z/ U( i$ e! B% L4 Qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ _/ m! C2 e4 ~% M F0 [解决方法:
1 O; a7 Z- j' y: _% C/ t0 T% K
yum install httpd-devel
1 i8 s0 r- D6 `( e) Q2.没有pcre% D) G- Q. }" z# ~
; l$ j+ ]( }; nconfigure: *** pcre library not found.
1 H1 o& p+ H+ Z& U+ M6 ?' W+ v5 ~ tconfigure: error: pcre library is required9 X9 a8 u' H# @4 _) W' H! c, M
解决方法:9 W$ d# o- {# i) O# C% e
; G8 b! P% B7 r9 ~( `6 X# G; b* a, [; x% B
yum install pcre pcre-devel
4 f% ^5 ]3 K! `8 h3.没有libxml2
% m b$ W3 V: [3 g
* {+ v# M9 p: ^3 I. \
9 T. }9 y: c6 lconfigure: *** xml library not found.
6 d5 j* \9 I# f q8 [6 a& zconfigure: error: libxml2 is required5 m8 _6 G V" Z% N
解决方法:
$ Y/ Z8 R% K' J; C. S; e8 P2 e X% q }
yum install libxml2 libxml2-devel) ]7 N: r9 u' E% Y) `, n
4.执行 /opt/tengine/sbin/nginx -m 时有警告9 G) J' X7 V; Z/ N5 U- Y# U
# k) W9 G5 v& d8 kTengine version: Tengine/2.1.0 (nginx/1.6.2)- }' L( K2 F/ _
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! {4 h" j) {' a/ h2 n0 Z9 ^原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log) v! u& \, A1 e) N! ]
' O/ N, k- w# @+ R8 [# \" W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ X3 h, r0 i+ r% J) p* F2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
* u5 \8 }" B: Q% }: {% R* @9 P- S2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 g7 `7 P1 K6 y3 N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' T- ~- N% S2 g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ S) J' z! l6 k2 I& G
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.5 n& G; s. W. y3 z4 @5 k; I
解决方法,移除低版本的APR (1.3.9)/ `2 J, u v& w4 L1 P
2 f* J5 s5 a5 R/ X8 j0 W' N! k* W' U
yum remove apr, D* d; x9 {! ~3 ]
5.Error.log中有: Audit log: Failed to lock global mutex, z. E0 u. V! y$ m9 L
' z: @+ ?2 P% [9 D1 E, `
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. ]% v2 h( }. V oglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# g, n0 w9 C! X/ g解决方法:% p: I6 e' _' p
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* |5 p; n' Z- f
0 g% \7 q+ K9 W) v8 h9 v8 X" x) A* ]# jSecAuditLogDirMode 0777
0 X% U% P/ @" E& @8 o) uSecAuditLogFileMode 0550' C+ d; Q5 @8 I4 G8 j6 I! P
SecAuditLogStorageDir /var/log/modsecurity# P+ Q U% I. ~
SecAuditLogType Concurrent3 i( ?( p9 R- I; {+ E% w
参考文章:
% r2 P* L( {9 V0 V* H) uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 w' d6 I% A1 |8 u. r7 t
http://drops.wooyun.org/tips/2614 |
|