|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! f' M# h7 u. v6 D4 m
% l! N) y; n) x7 x4 B- M
一.准备工作
' u( N4 w! U) q. w& z
( p' o4 T `+ k$ }6 {6 @系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0% o# D: F, ]; J7 p% i7 X& p
5 V' D/ @ S! e) I* f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% x" c4 M7 X2 s( l
6 |* N$ f+ r( {0 u
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' r% |. l. G; B& l/ c
. \+ u0 h. m! c% t& F6 BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 u: y& Q& Z6 {9 z" U1 U, }/ I! H. Y5 w! B
依赖关系:
: ^! Z' x$ e5 h; M" R% a5 G' \tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:; P0 x' C" R0 U0 T
* F5 U# }4 K0 f. g% g7 ?
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
6 `3 m4 w7 |, R. ?$ S% T( zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
/ ?: k- |6 v: k* \+ R8 N& A5 V$ N
6 j' o4 a, D) J( y. [yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
" u! Y- i: \! z: [二.启用standalone模块并编译 W! I) K' q( \
; a& A+ o# G& ]
下载modsecurity for nginx 解压,进入解压后目录执行:* r6 j3 p" U: j, h: R
5 m! F: A' i, p
./autogen.sh
' L5 t' U/ f: \0 Y./configure --enable-standalone-module --disable-mlogc
% C }# U0 H; h) M7 n+ nmake
. F9 k# a& x7 O9 n! T" T4 d1 U. o6 y三.nginx添加modsecurity模块/ Q) \+ t5 T0 _. x
5 s% _2 u# J0 C4 E- J% |! s在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& J1 N9 Q7 O9 N( N- W; S
* Y9 i. f. N q4 b, {2 M
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine1 O" b& k7 |) U; c
make && make install
6 s9 D( E6 t2 h4 p" ^0 R四.添加规则. V3 U, F0 i/ X6 P0 }. w
" d6 d: p( T! c3 E- Q3 S) P( Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 l( f4 w5 L' B; ?+ l
/ J2 n" G9 K9 M' ^* i1 O1.下载OWASP规则:( \2 h; b% p0 i# k) m' z. h
) Y# k; E, M( G$ r3 Tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs, I0 F' i1 K# Y# Z( l3 V: r
( I. t. T% } L* D* u" I2 ]0 Pmv owasp-modsecurity-crs /opt/tengine/conf/
" n1 w: B# z% x% s9 T# X8 |& G
& f) g& D9 ~! f% x. d# j6 u; ^cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf; P$ t+ P. b, J; Q' }5 R+ ~/ J8 Q" q- }
2.启用OWASP规则:
2 V1 J2 x5 r0 ^. R
, U0 O% |. L0 A6 ~7 @* F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
: j; P8 d8 ^2 g0 U2 \- a3 M2 o0 _3 F2 M( D0 s6 F) O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 h8 W, r& \8 H( ^3 S
; {+ l0 l- T) q# C3 howasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 x* ~! x. }6 D; H$ @/ T2 Q A6 J, A9 `; X; x: f3 H
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 M- I; t* r) x2 n* Q, O3 rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# {1 E8 B# y/ Q& W( E2 ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf! c! h$ G0 C) ^. o9 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 S& Z2 q" @( L( tInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 R l1 L/ k- G) h3 H' o2 q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 v8 r# I3 Y" s8 x. |8 Z* z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 I0 N* S4 z* X1 c; I1 [' f' t- X五.配置nginx- Q; t0 g: @# O7 _4 }% f
. {- w B+ |* a) k在需要启用modsecurity的主机的location下面加入下面两行即可:
' E8 ]9 s$ I" v/ \- g. p# G8 B- q! \
ModSecurityEnabled on;
2 W2 r. I! f& ?7 F* ^+ d, h3 CModSecurityConfig modsecurity.conf;
8 a2 t: _& L$ z0 ?/ B3 O3 c下面是两个示例配置,php虚拟主机:" B' e# C; r/ l2 W
* t1 m0 w5 y( I, _+ p4 W$ A( {- v* a- F
server {
0 y3 N$ i/ E& |8 Z% i3 S2 u2 @$ C0 U listen 80;+ r; z7 u+ c H$ u3 `7 P* e* U2 L2 ]
server_name 52os.net www.52os.net;
* @, }# c1 W, a
7 u( J B1 C% H. b3 E5 u% A location ~ \.php$ {
s7 F0 P8 V- Q+ z# @ ModSecurityEnabled on; . A8 t; h& a7 o/ K8 C
ModSecurityConfig modsecurity.conf;1 e/ U& b: M+ y7 ~5 t$ E
V) O+ v' E0 i" D! q) ^4 C4 q root /web/wordpress;8 u8 H1 R: a# {3 ]* ^0 y3 d
index index.php index.html index.htm;
6 F: R+ C! f8 K* b * q7 L% H: T d( H8 \9 R& k7 |
fastcgi_pass 127.0.0.1:9000;
( `7 E8 _ E. B8 L5 \: e0 N& h2 N fastcgi_index index.php;9 C3 F, H- p1 ~2 @* t8 l
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 t6 _$ ^; ]# y) K C include fastcgi_params;
9 H3 D1 X. B6 A& c# ~- `& @# v }
9 ]1 y1 V8 h0 W1 d' i }
# X9 {; Q2 Q/ q3 ^upstream负载均衡:
* D& z9 r" q' P+ r4 u
" u! s1 f! m/ O- ?& \/ k5 A! D% h- y' Hupstream 52os.net {
$ i9 A) F3 `6 ^5 O F/ B' I server 192.168.1.100:8080;
2 {- ?8 K% n V6 p H server 192.168.1.101:8080 backup;- k+ w& q+ q1 H% k8 c; U
}* |) Y/ z! D+ E3 W% S( T
; |# c, O" m' G. dserver {
6 g+ }; @) {) q g5 \, `listen 80;
, l3 x" T' m, F9 r- \server_name 52os.net www.52os.net;
% ^6 p- r! t$ N# {! l, N3 d
?! u" x1 X1 d2 Dlocation / {# u) }, u7 j! `
ModSecurityEnabled on; ! @" `" c, k% ^9 D0 {) z
ModSecurityConfig modsecurity.conf; 9 I4 G0 {9 `7 E6 q) m
. s( s3 w, _! H7 I+ y proxy_pass http://online;
' k* Z) X$ j$ P* u* a3 ~8 A" [ proxy_redirect off;
" M; @/ T. p: Y9 X0 j" C1 Z! y. v proxy_set_header Host $host;: b+ b% L% e. g" z4 P
proxy_set_header X-Real-IP $remote_addr;, Q; w( j, y& L) L
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
; u7 z! M0 P& q- h }
; P+ W( b$ x: i- i( m0 K% k6 T9 E. i% O& n}
3 L$ t/ _& L4 ~/ ]六.测试( x. q+ V, m f! {% m
0 @) t% y/ N" h5 c- Q7 }5 |' B$ [
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
4 G' @; Z( R: h" K! P
, k8 @2 r: v* Q1 t2 t% T6 e<?php# X1 X$ E- T+ k7 }% ]
phpinfo();
6 i* I- ?5 b& H2 F; B8 j?>8 _ M3 T1 H% E! U8 T) z
在浏览器中访问:
6 a2 L" `3 e% v% F( j" O9 a$ p
5 F0 r" ~& W5 M) l& M3 Whttp://www.52os.net/phpinfo.php?id=1 正常显示。
. _, x' M( d- |, T8 h& Bhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。* B ?! x" _* q3 r9 u" |5 t
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
0 h( M3 a6 E+ n* r; e说明sql注入和xss已经被过滤了) I$ ^0 C& z* X5 @. m. W" H" W8 G
* B1 _0 ^) m8 R y7 Y/ s a* `七、安装过程中排错
7 ^8 l3 s. u. Z" i) ~. O5 m
1 [$ a3 U4 {0 B! z8 L4 k1.缺少APXS会报错% W, Q$ N8 _8 I1 P. t2 F! O
! l. i& R, }. n& `6 H# h4 X
configure: looking for Apache module support via DSO through APXS. D v. E( U* _
configure: error: couldn't find APXS
, O% z d- `% s* F/ h+ C: Uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
0 W. n/ u* Z- x解决方法:1 S0 G3 t) y% f, w) O+ P
/ \' E7 {/ s; {+ k( m M
yum install httpd-devel' }+ B. s& z7 S+ l
2.没有pcre
6 ?: K1 y5 q, v! E0 f+ r+ ~5 n& C0 Y2 X6 }* Z! h3 S/ m
configure: *** pcre library not found.
! t( ?; Z0 R, n: V: \, yconfigure: error: pcre library is required, M" S; z4 @7 u7 r- p- H3 {
解决方法:2 c- e6 T. w1 E( c9 g9 L
& {1 T6 h, p- L' ?yum install pcre pcre-devel1 s% a, P% X1 b1 K: ]) ]
3.没有libxml24 Z1 v" W9 b& k0 f8 {% T. U) J
% q" b }$ H! |0 w
! U5 \8 c+ @7 k2 Q% a8 q* A- cconfigure: *** xml library not found.. P, j! ^) j8 G
configure: error: libxml2 is required- k$ e& j; S# v+ [; j
解决方法:
' |2 H! t! ~* R/ f. u: f( K
, G6 y& h: i0 h, u2 l6 Ayum install libxml2 libxml2-devel
# Y+ f2 w/ U+ m4 y6 c" f4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 m# Y1 a1 o, \, w7 ~
& \2 g4 u1 Y x% |2 MTengine version: Tengine/2.1.0 (nginx/1.6.2)
7 @( `4 Q9 \0 f! P9 p: w9 dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
3 y! Y4 w5 B, ?, x0 H" u原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ u+ V; t* V1 U3 k0 D7 K; U" K$ s
3 R* {$ m E( `( {( s0 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
7 t3 z- b/ I8 D& r7 s4 q/ T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( B; R# q9 p6 b1 o4 D0 \9 L
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
$ z% M& I c5 Y2 [5 r' e: S6 Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" W( t. g" a* d/ O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": A" g! I9 s& A. D& W
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! J3 {# t2 G+ v) c5 {
解决方法,移除低版本的APR (1.3.9)
% ?; J, J+ E b9 G, z+ ]0 v6 V8 V4 `, H
yum remove apr j$ J8 ~* v0 O% P" G# a" \
5.Error.log中有: Audit log: Failed to lock global mutex- Q# T2 t. |# v8 e
6 V9 m3 D. L5 G' P
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 2 F. w* H' k7 c. {
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 u0 {& {3 l2 H
解决方法:% M4 X$ Q. R( u8 U2 _1 Q3 M
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 x7 d( k, U7 u6 D
$ B! C7 |+ [1 T$ q ]SecAuditLogDirMode 0777
, w) K. k, ~3 g0 W* T8 vSecAuditLogFileMode 0550
! K& v# D4 t/ x+ `4 pSecAuditLogStorageDir /var/log/modsecurity
9 m2 B7 e1 m0 RSecAuditLogType Concurrent# t& d% Y# ?6 D+ p( u
参考文章:( O0 L, @8 t' j1 a
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX2 l, ] H+ t1 D, e
http://drops.wooyun.org/tips/2614 |
|