|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 L. t* C5 P% m3 b2 [
I \* L0 n F! h% O
一.准备工作0 U# w0 ^ L, s' a8 H9 N
# a% _" D( y5 X$ Y ~1 h8 V
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. B9 j8 @6 d7 t$ [" a# w6 m
7 l- K. z0 H* p5 L$ |2 ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
+ u! i5 N7 S3 B* S# ^* M7 s0 d F' ?6 j% M2 R$ D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" q6 t. |1 s% s: F+ O4 |
! s- P* |, q* J* l; x VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 M4 r( r: h) L
6 H: k# }, G6 `2 Y. H( T依赖关系:! r0 H S/ Q6 ~7 \& ? _. {& M
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 A T. W, D, o7 h8 M
1 M; Z( O. ?" kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& v+ w# j. H4 Q& Imodsecurty依赖的包:pcre httpd-devel libxml2 apr
. j9 w7 H9 Q* R B/ [1 W' ~# A) j: e' [5 v1 ^% N
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
( x/ i2 p9 A9 K% ?8 ^; D二.启用standalone模块并编译% y$ r7 A2 a' n* U3 t# M7 j$ i
6 u D. C$ k& | K
下载modsecurity for nginx 解压,进入解压后目录执行:
2 \& a) g \% I: V7 X5 u( v; ]" m, `
./autogen.sh
# ~! c5 r+ y4 E1 L./configure --enable-standalone-module --disable-mlogc# {! j$ M0 \. |9 P" r; t& o
make . v0 p' |# _& i: R8 a
三.nginx添加modsecurity模块5 V6 R, a! X7 z
8 u) H, ]4 ^( r# E9 K2 _
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 W) l5 H7 c9 \
' }5 t" I2 @/ G6 F! g7 J( d./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
. j1 [: t6 J7 Q6 t: j% p, k2 x3 E! @make && make install
2 A# y4 j; d. o% D# D* f四.添加规则
+ B! [3 |4 f4 B y; g, K, W! ]
1 L( H" H& M! }modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。6 U# R3 t; p& V; v0 J: B. K0 G
" O" F ?9 ~, M, r" t1.下载OWASP规则:+ I# P" D1 Y# J# ~1 L6 L P
( E- h4 n1 |4 `/ v! `& h0 @
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs, J2 e( J0 }+ L6 ? z# B; J7 d
1 K6 j0 r/ Q/ T5 g/ N
mv owasp-modsecurity-crs /opt/tengine/conf/
9 B- f, g4 @ D8 @& X3 K
# d$ F/ ]. C: ]% [( M1 g) x' ~6 O0 ycd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
% {* t. M# D" b9 s: p0 ~2.启用OWASP规则:; |, S1 N) \9 {) V1 {% }5 V& @
- |# h i% H/ I& n/ r$ u% |
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。 s; m. ]7 y% M; R6 G' P# n K
# s; Z. O: K2 W. k: u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
. E2 C" ?! @! \4 t8 k5 x& r$ k! L- G8 g% \+ p9 w) ]
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 ]' h; Y: Q# h9 Y, e- _( ?
% p/ N# b" N) ]# Z' `6 pInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! G1 ]' N; J* C R7 P# l0 f; [4 b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 g5 w- a% ?! V3 Z8 u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 @ _3 Y+ z4 Z/ \5 ]5 uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 y2 x3 e& L' H' O4 D& |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" x, ]8 N, i) B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
$ A# `7 @! J# x+ A/ B/ v8 aInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 }! A: y! n- `五.配置nginx, T2 u$ V. Q( J3 o/ r
6 ?3 A+ c' ~7 D9 C) ]
在需要启用modsecurity的主机的location下面加入下面两行即可:" H; D& c* T. X: J
8 g0 U. Q4 } M! x4 L* LModSecurityEnabled on; ( U; u ~! m6 V% `+ d0 o
ModSecurityConfig modsecurity.conf;
, t$ ~9 w. l) t1 M下面是两个示例配置,php虚拟主机:' q4 Z; t$ `+ Z" T6 o# Y n; A
# A. G; _& w. ]3 f
server {; ?2 ?; Q* g2 @8 J5 q
listen 80;
( S- d% e8 g; i server_name 52os.net www.52os.net;- s% T* r5 g( h, E# g+ }
& H1 o) L) S( t3 h
location ~ \.php$ {
' o$ Z+ S" |+ t ModSecurityEnabled on; ) [+ Q2 E! M; t. z# m
ModSecurityConfig modsecurity.conf;& l/ e4 A5 y7 J+ U+ k& I
- |* c$ g* N( d1 X
root /web/wordpress;& r0 Y- B( k; k: \, F) p3 m
index index.php index.html index.htm; K) u% T4 T* l+ E$ p0 B, H# i( W
; Y/ ?$ S. Y2 R& {
fastcgi_pass 127.0.0.1:9000;
0 ]1 ~5 P, Y) T2 u, W9 g fastcgi_index index.php;- X( L3 G4 ?6 C% \
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;9 Q/ W; Q' P4 a, h% n
include fastcgi_params;4 ] a, T: N% w0 X* ]5 v
}
! x8 M" S( t* U }" T1 w( l b7 G( d7 W+ D
upstream负载均衡: ]' J! \: [0 K, I+ Y
9 u( H2 _. H: a0 {& Hupstream 52os.net {
, G4 C7 G- i2 E' `: A server 192.168.1.100:8080;
! X0 |6 T( d w4 ]+ p: _; B server 192.168.1.101:8080 backup;, a) f8 p2 h6 A s7 v7 u
}
# w" b! P! |0 ?* |& _- y5 h5 g/ y9 r
server {
+ H% c. N9 S/ @8 v p# llisten 80;
+ U% [0 z: `4 fserver_name 52os.net www.52os.net;3 y/ ^8 A1 v. s. O4 u4 H/ O
5 ^* X# k$ U9 h
location / {- s! a' a2 d$ _, L5 a' [# d
ModSecurityEnabled on;
' g2 [ P) I3 M7 L8 b ModSecurityConfig modsecurity.conf;
# N) D( x% N2 L1 G! o& E) ?* P& c! ]. M @1 d. F Y* R" k
proxy_pass http://online;
# N. T9 w) O& a) X3 h$ x proxy_redirect off;$ l* d: @! ~* }- i$ X: l
proxy_set_header Host $host;
( }( N' x3 z/ d% d proxy_set_header X-Real-IP $remote_addr;
/ k6 E3 I# T7 q2 l3 X& q! s proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;6 P: c3 M. T y5 W6 C5 s# @0 Y/ w
}
7 I: Z" d1 e8 w, F. P2 S& E6 B}; _: ]' O1 Q/ Z) M ?" r( J
六.测试
5 d" I. a0 t0 @9 S0 O9 B
# \% B m1 W4 s l, o6 U% L我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:2 D7 I8 J5 P4 ]
7 I+ n- o+ W( d; B
<?php
* m7 ~6 ]# l" e, u# h phpinfo();
/ h- J2 ^8 L3 \3 A# o& o- C$ Y, _?>
. G" l! E2 f5 W7 y* J+ w: t! ?; K& d在浏览器中访问:& @1 K2 }5 p6 d# j" u/ R
; |4 `% ~' W/ _* Y3 E, u% @http://www.52os.net/phpinfo.php?id=1 正常显示。
( Y" c0 X' p% d. B) t! zhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
- R3 O0 D1 [& g$ U, khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# L8 q0 W5 z2 s. G% Z3 u8 N说明sql注入和xss已经被过滤了) ?8 Q$ ]! g( L
: H# M" a+ K: |8 Z七、安装过程中排错& s1 | H" e/ U
- a g' O9 _) S7 _( t
1.缺少APXS会报错
! y- \1 H5 a* z' t6 |
. Q9 |* A4 I/ t" Bconfigure: looking for Apache module support via DSO through APXS
" U5 ?' S$ k+ E0 E: T; D! o/ ^configure: error: couldn't find APXS" ~( h5 F% x; ~9 L$ H
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 M% X' l0 T0 k6 r, Z解决方法:% E# k2 O/ j5 ?6 d" [
1 i% M& p& V, I( {0 }
yum install httpd-devel
: C. Z/ a* |4 g( X: I) C2 w2.没有pcre9 h3 h& l8 y( Y
, @7 I, W- Z, D+ q% Mconfigure: *** pcre library not found.& r( [/ [7 G8 e& h. R
configure: error: pcre library is required y4 ^! W5 z/ l6 ?6 ?1 x4 |+ V
解决方法:( ^& E9 q5 M1 x# y! V! g* x
5 J) ?7 a) N! p3 k& {' w! qyum install pcre pcre-devel1 \' f7 M7 S& k1 e) {3 l% a% w
3.没有libxml2) y: w& C. \* M6 i: n: q2 z/ i4 X {
8 z$ l3 T! m% v7 e
3 _" G% j8 D& c, Z& D/ Dconfigure: *** xml library not found.
# Q0 z; H2 |+ W. K* [& aconfigure: error: libxml2 is required
; Z" e9 u+ Y- v, X, `2 N解决方法:. E0 X0 F' |5 j9 ^3 p
! L! I/ z, @. M4 Q. Fyum install libxml2 libxml2-devel9 J: I0 @2 m: ], ~: T. J [
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 C, w6 o; m* h F
! ~% n5 a7 A& D4 @, ^% J
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* @. P: j, L3 T$ E( pnginx: [warn] ModSecurity: Loaded APR do not match with compiled!6 X4 p" U7 @; R5 C+ j D% W
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# ?. l5 F5 l& S: ?5 ` ]4 ?/ |/ _, [+ y+ f5 T: j6 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 M4 m. K2 f; w" v0 ] J% m% L
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 ^2 y8 Y' d& k
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; Y6 v0 r; K) v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# c( P! q* n; G, `% W4 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
' E0 U! l" O3 H! m9 H3 z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
! m" T; S" |) B解决方法,移除低版本的APR (1.3.9)
+ X- f5 z5 @ ~0 [$ ] z# G: e$ m
$ @( M0 k# s1 X9 a, K; Q7 wyum remove apr
7 h7 r5 m6 O* K( \5.Error.log中有: Audit log: Failed to lock global mutex
( S8 k. [7 w) h; y" d/ J) }) W8 A& n% M( L2 g1 l& l
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! A. t! O/ O! O8 t% y4 Bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. ]! u4 R& W7 p; i) J
解决方法:4 q. h3 k; A* W' s* a
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
T6 o$ z9 I/ W d/ N9 r b# F
3 h& X7 g0 j0 GSecAuditLogDirMode 0777
+ `& k" r% }' X2 G- PSecAuditLogFileMode 0550' u Y+ j& g6 B; s
SecAuditLogStorageDir /var/log/modsecurity* w9 M2 X' _# O# v4 O
SecAuditLogType Concurrent
( s( Y* d7 ]" R1 _4 G1 b4 V参考文章:
: ~. a# J3 r. d/ C( y) {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" h4 i; K- {0 c- @1 C: r7 D/ l- ?
http://drops.wooyun.org/tips/2614 |
|