|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 {- A2 V9 Q0 F% f$ O; t8 D( b# {" K7 }
一.准备工作
9 |# R1 ~0 l4 o& x2 ^, _$ R- U6 p8 Z( r( h; z2 K* |! F
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ K, ]+ j: H+ p( `0 r( m
. |* l& k9 S- R2 vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
3 c2 a' @ M b) h# z, W; f7 t- }+ Z
6 P/ {8 P) L' z5 S2 p, amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ X$ q; Y$ W" t
6 a* x6 Z1 b5 N% F1 \. c* BOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ [; o x5 }1 m5 ~* t% G
) M+ Z6 c+ D0 I" ?0 V/ y
依赖关系:
+ Y% [ w+ e% Gtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( w. ^' d' N- L
, J, ]; }9 l5 ~2 O+ V# G4 @yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 J/ X* f; A' o7 B% G/ O" c$ M0 X8 C1 V
modsecurty依赖的包:pcre httpd-devel libxml2 apr" b! Q" @, @' i* `' L
7 ^% q+ q! e ~5 U* ~yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 g' Q, s- E9 ~0 @; R% t& J1 L
二.启用standalone模块并编译
. Y/ v" M, F2 X' J( o) }, V9 v3 V- S6 N3 W- N9 g& g6 l2 w2 i/ ]
下载modsecurity for nginx 解压,进入解压后目录执行:% I+ i7 e# e' n: v& Z: u
, [" Y$ S: q, I4 s./autogen.sh/ }; Q8 y/ S2 ?2 S
./configure --enable-standalone-module --disable-mlogc! Y! n F4 Z- N5 h
make
8 o8 j( W7 E6 i1 f( S9 e三.nginx添加modsecurity模块
* ^: g# Q$ ~1 \4 L- Z) }9 ~1 F
* W0 q: m7 t6 B) j1 J: m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:) |& I. s' k+ V1 M% B y+ N5 a3 j9 a
! X/ y0 O9 b0 ^, P
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
; I6 T+ y, x$ Q* {- F- U6 wmake && make install: j$ f1 U' u5 Q* b
四.添加规则
' r' a0 K9 a$ \+ G' Z) j7 h. d! y- x
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。8 k, {- p& D+ y2 @5 H
, G k6 H, r. j9 g; E6 b
1.下载OWASP规则:) h5 \) b r/ @# K: }% y
3 ?) f$ m% w" i5 [& N0 E6 J" wgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 d: O, W6 g; m
7 q* R" f% m) d smv owasp-modsecurity-crs /opt/tengine/conf/
: \# c1 j8 [% b( i+ ^
& B% l( J% j. S5 [3 S9 o3 zcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' c& s& b/ d7 H4 Q3 _+ `2.启用OWASP规则:
2 o0 K: A9 y$ r& K& d
: X6 v: V( r; e( G5 j复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。 b; h+ ^. W5 { f6 w
& y+ I8 x6 `) J3 _: i. P8 }4 `& p编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( J' |' i4 n3 d q
; l- k! z0 r! Y( @
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 x& m, v1 d0 V- Q$ P( g
& W2 u* _: ~% R9 |, rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, t( j5 N5 E9 [8 v" b5 m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- V$ {3 H6 r9 `2 n& NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf X" o, R' K9 Z/ ]) {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ c T+ |( ?% E; K; J) m7 dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# v2 N8 F8 n/ A8 JInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 q0 y7 A. u0 N( HInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
H5 `; n& `. p5 T4 K5 Q五.配置nginx" i9 r( e. J# i- ~
0 E j) ~, ~9 ?* U; I+ ~
在需要启用modsecurity的主机的location下面加入下面两行即可:8 o3 Z: ^7 B0 N) P! |6 h% c
8 i# I6 q8 P# FModSecurityEnabled on;
# N* {: B7 @9 |; x4 AModSecurityConfig modsecurity.conf;( K; D6 [4 R2 F3 Z7 D
下面是两个示例配置,php虚拟主机:
}+ n; a, b6 z
2 Z; W$ p Y& j0 I8 o& i. Vserver {
# ~/ |( J. U$ g0 l9 a. a: f) h listen 80;
' C) X% ~1 L$ ^3 |5 H: f* Y server_name 52os.net www.52os.net;
4 V8 ]. q6 j/ b1 F4 } + J* ]- I- G) Q" N$ U I
location ~ \.php$ {' C3 R- J, v( K4 t" K
ModSecurityEnabled on; . \ D9 h; Q! c8 Y9 {
ModSecurityConfig modsecurity.conf;1 U- {4 i) X. V+ i) w6 J
9 s7 J+ V3 b% g' v1 N0 B X
root /web/wordpress;8 L% E* Y {) p8 f2 m2 X
index index.php index.html index.htm;, g8 [# m0 ~/ ~ j% R% D8 T% k
) g( K5 L% j+ d0 t fastcgi_pass 127.0.0.1:9000;) R( y$ j( X- L4 V, q& @
fastcgi_index index.php;6 @, ^ Y, r$ W0 ]" @
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
* `! Y2 Y" o1 g" x# v0 s include fastcgi_params;8 V& [3 C- |4 y# n
}
" e+ h, U& ^$ e3 z2 L }0 w. _+ G/ q5 z5 [' F' E. m8 P w/ d
upstream负载均衡:
( c% n5 M$ u" z0 x' k, N2 m9 F0 ^& Y4 n
upstream 52os.net {; @9 N" V6 d! W% n8 m+ N
server 192.168.1.100:8080;
: S+ U! V9 L' P2 T" s server 192.168.1.101:8080 backup;
4 ~% N! U9 D* U# F6 h}
3 o7 n% W8 f8 x4 |! ]& a' N* b' c9 x2 B
server {& w( Q* M# H& m: m2 B2 W
listen 80;
" @0 K8 r/ B; }& w8 j; |9 Kserver_name 52os.net www.52os.net;8 d# z- H" j6 P: t6 L
/ |8 b4 [0 l$ R- F O/ j7 Qlocation / {
, I, t2 E1 V2 Y4 n/ m+ u4 L ModSecurityEnabled on;
; ?* V: }' `% W( x& F ModSecurityConfig modsecurity.conf;
9 F, r! h) _+ I" T$ n2 z; o9 ]2 ^' T6 d
proxy_pass http://online;* F4 C. E; k2 M8 j6 c
proxy_redirect off;0 M! D* A5 a E. e' d; ]- M
proxy_set_header Host $host;
p& e( h9 d% e' o J# c proxy_set_header X-Real-IP $remote_addr;( J+ s, E8 ]. b4 ]' N2 b8 V2 x3 m
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 @% R& n9 x, ~* k! j( y" m
}" d$ h5 `! d& ^/ f# y* N/ Q2 h
}
; K/ g4 M. f, X六.测试
. Z. Q" f5 i: |2 m3 ?1 n1 f* @7 n" ^2 F# z c1 i( C' D4 T
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% w% b% H' w! A/ ], o% E3 k0 [0 @5 k+ ?2 t4 d/ F! J% Y, v
<?php% K& R3 ?; U" v) F; Q3 N9 ~
phpinfo(); 9 P9 _7 P i+ F( v% r9 u0 h
?>( t$ F6 _3 q( @6 K* X1 m3 B
在浏览器中访问:
3 S( ]( C+ M6 Z5 |& A
$ v6 j- Z: Y+ p* m( q6 ^. Zhttp://www.52os.net/phpinfo.php?id=1 正常显示。
( L' q5 }# ~/ k5 N2 Y( H1 ~http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。4 J3 n- [. c5 s$ m" r$ T( n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。/ q7 f( \! r) e7 X7 q" o2 d
说明sql注入和xss已经被过滤了
8 l+ ?- I! M+ y6 a* H* I
; Q. q- u1 r6 z: p: r5 U0 m七、安装过程中排错; N. {% T* ]# d: y2 ~! [3 G0 Q
& ~* ]3 F0 ]% O: |& H, t! v1.缺少APXS会报错% c5 ?' r% r/ `* `4 I
! _1 N* @; K5 E1 x$ n, dconfigure: looking for Apache module support via DSO through APXS/ B4 a0 ^- T1 w L2 q! n
configure: error: couldn't find APXS
* P# ]- X- M! `% j1 E% o5 H3 o0 y+ Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& e+ _+ f4 ~) k5 P m ?$ ]/ i
解决方法:
v' q" I6 Y) I+ s* z+ v$ G2 |7 E9 A! m+ `( `3 d
yum install httpd-devel$ y0 z n# g' i. e4 d7 x
2.没有pcre
) `. i" K- R- J& a0 `4 Z+ y
- ^7 ^$ T' r; \" m: Z [# \1 P# Qconfigure: *** pcre library not found.
- ], @( ^; z) U2 k+ `% lconfigure: error: pcre library is required, A" \( n$ ^% p2 }! r9 E/ H
解决方法:
- E* H1 x, [9 f1 w2 h9 \6 C2 Z# X7 Y5 W5 C
yum install pcre pcre-devel
3 ~8 C! L+ n: Z! H& G3.没有libxml20 e; K" \9 U* Y7 |( F0 t" \% Y
4 {" Q$ w& H$ R" z: ?
# V+ Q& @9 G8 D% u( hconfigure: *** xml library not found.
; c* I. K g$ }$ c$ k+ j, ~7 oconfigure: error: libxml2 is required
9 V* U" z4 U5 y6 @: [' S/ x解决方法:
2 g' [# B+ ?7 f# t o: _6 Q9 x
" g, Z3 q1 b1 I3 |1 Dyum install libxml2 libxml2-devel* D' Z; {% i; ^
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( D. R* E, ?4 j" { e `% Z" A
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ s9 q6 n6 V, A% d. Y snginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 e' q) B8 ^) \) M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; t9 Q/ k, [# |3 G1 ]8 j. T T2 N2 c
& B$ G& p! o7 o! \$ a# v* F* l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., X7 r# j j" x7 n, G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"$ l3 n6 N* q. m. |
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! N5 ~# [; g: [* Y& m) z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 y# M% P, U W! g# [* W$ }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
& i/ g8 ~3 q2 s- Y- f* ~) H2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2 u, @! G0 T6 z" P解决方法,移除低版本的APR (1.3.9)
. X. k m' i: t$ N! c* x0 I/ l* ?6 W3 Y; m1 z# |$ s; ~9 o6 a) g( s
yum remove apr3 X/ C, w' S) ]' A. q8 S
5.Error.log中有: Audit log: Failed to lock global mutex6 s7 j- K1 J5 e) s5 X
& [1 [; t2 }5 T2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 0 S( N% P5 P$ r5 q2 a
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 J7 a7 N/ R8 n. V1 ]- t" t
解决方法:
5 X& |1 z+ a0 |3 `- J9 s* I& ]8 k- U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( ] @ o; l: F. H8 |6 K& ~2 ?$ P' k/ T; \
SecAuditLogDirMode 0777
+ f3 T0 K. J6 ~* y3 u! iSecAuditLogFileMode 0550; v% j! J+ q. Y2 p) t
SecAuditLogStorageDir /var/log/modsecurity& p. e, h$ }2 P' x* [8 m5 e
SecAuditLogType Concurrent1 r8 Q% x" E- m8 Q6 z
参考文章:
! t( X; c) N5 c% A- n9 {6 p. X; zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
_# i1 [9 J- f# bhttp://drops.wooyun.org/tips/2614 |
|