|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 l: Q. E0 \8 i6 P* q) {) N8 o+ P+ G+ x, L$ h# ?
一.准备工作+ C1 @! c; t) B6 |/ Q
6 f2 X( @* V; x( o& [ e! [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ K- ?' t1 r/ Q! o" B" N7 a
% F' M. e# s N% i: Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 G/ h) N$ e9 J# {3 Z
0 x' b- Y# D s7 Y( l' Mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz% }$ A* i% g$ i" ^1 H. a' a' N
5 j$ @& l# P/ L! `; q3 D+ j: E' eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
7 ]6 U$ C4 S, {. S* y* ^
" L* X2 j# b; u依赖关系:
5 n. j$ T4 }$ x+ s0 C4 Z& Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( G9 L7 X% @: h
/ @. j* {7 q$ i
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel$ j( s4 `& Y3 t& v& _! r0 N
modsecurty依赖的包:pcre httpd-devel libxml2 apr
7 X1 k" e8 H7 P% i
( D3 N9 q+ ~" f# T) hyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) C7 @* y [, E# J: L
二.启用standalone模块并编译6 g& M! o, m# G- G) U
! _6 u7 ~5 p) T7 O
下载modsecurity for nginx 解压,进入解压后目录执行:7 c6 x! z4 X2 s1 V+ J P
3 v+ Y8 o" S( Q2 e./autogen.sh8 t: B7 Q. v: a C6 }
./configure --enable-standalone-module --disable-mlogc
* p4 R* y& J( f6 Smake
8 g/ r; ~" ]$ W; M, _7 {4 x8 Z. B: f三.nginx添加modsecurity模块
. J: C& ]0 U) q
3 {2 v1 a) Y/ a B4 w1 {在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 J8 g+ E% l; u+ J
- J- M8 z J; {' x, c3 R/ Z& ~./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 t6 U3 l/ p7 [3 A! d# t% [
make && make install3 S( ~% B; L" L( O$ v+ n2 {
四.添加规则2 b; V1 ]/ {* P# k1 G6 x
5 Y8 ~6 Y8 c, e r U
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ y0 I1 Z# L: b2 \
* p3 t0 N8 k. ^1 ~1.下载OWASP规则:# D- t6 P+ g" q2 y- r
9 h8 w0 D& E+ O M
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs. y: G( O7 Y8 i% h# ^
0 {) D' I, W& K6 Qmv owasp-modsecurity-crs /opt/tengine/conf/4 G E% {, T- h% d
4 z. o3 G# M% Jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ }" f) u. N/ Q: T: w% u! m4 I2.启用OWASP规则:
7 a: @- L, a1 S- M( s' o# K$ {2 G
, W. k* e: d; M7 T复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 d2 h3 g. {1 d2 `0 k
' G2 ^1 z, O) l编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 j6 m* P% z. H8 W% U
& N( {& }" C! yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
0 ^1 ]0 r' q. _1 q+ D
1 W# a- `; i+ CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 Y0 |- N# m! ~9 LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ F% l/ n) y; T* |4 J: CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ q( @7 d" I4 _. R* oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
! N' f' M" H1 M c5 o( DInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf2 p& z& J' J2 S( U7 b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf- m' z+ s' k$ Y5 ^1 `
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( H( ]! b) [& p$ u5 M
五.配置nginx
$ S* z( W, [# T5 _2 d# R9 l
0 k9 H" ?* Q3 B在需要启用modsecurity的主机的location下面加入下面两行即可:
4 x5 b. u8 O% ~5 b8 g+ L5 ?. m; G+ R' N7 ^* _
ModSecurityEnabled on; 4 F3 \% j4 u/ E1 j X9 v, P
ModSecurityConfig modsecurity.conf;
+ E6 r* W% ~( \" h, U. n下面是两个示例配置,php虚拟主机: r8 z# f; x& z: L
0 F, r% U9 _3 y) _# qserver {
+ b) N. ]* f& K* h9 N listen 80;
J5 S) u+ M( b! n5 w$ Y% D0 K server_name 52os.net www.52os.net;9 c5 J, ^. P1 X1 u7 I
' h! N, @3 y/ h+ Y
location ~ \.php$ {
9 @4 k% G( y+ U. r ModSecurityEnabled on;
: Y3 y9 o: t' M( D ModSecurityConfig modsecurity.conf;
& v- M$ w1 z+ T+ L
' p& e* V. }$ }1 ] root /web/wordpress;
# c* f7 e' ~2 w index index.php index.html index.htm;
" A$ h5 o" O, y& b( g$ S , x7 A5 P( |5 X. c6 g
fastcgi_pass 127.0.0.1:9000;: r! ] O$ H0 B; D" F
fastcgi_index index.php;
' K2 w+ o6 L* d2 ?6 O fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
# o: h: g% A8 i. t9 P6 W. A' O include fastcgi_params;$ C& l1 u: G: @) V4 s7 A- {! W
}& z+ H) ]+ ?5 m: h/ x: M
}
' a0 K: ~1 k, D! J2 p) i! ?1 Lupstream负载均衡:8 X$ R7 M U- y$ D) n6 R
: c; \6 ~8 J7 {# _upstream 52os.net {: A7 j% l6 V9 V% J0 M
server 192.168.1.100:8080;4 m& _. b; [6 c6 i& O) l4 i
server 192.168.1.101:8080 backup;) G3 R% Y. l) M3 K( r
}
' L! e0 S4 L9 _+ d* F* J5 D
( T# e) A- Q1 L! S1 F: J; N2 M: mserver {
' r* U- _/ Q4 A5 Flisten 80;
! \; h% \8 e$ jserver_name 52os.net www.52os.net;5 B; \ e. h0 \ M0 M7 q* B7 ^
) @6 ^) e) w. l- W9 ilocation / {
3 x& u7 _/ U* _7 a- [ ModSecurityEnabled on;
6 J. H" a6 V1 l ModSecurityConfig modsecurity.conf;
% b2 V1 }- d2 W/ I
4 `! d- e$ M0 m ~9 v proxy_pass http://online;
0 o1 P$ z6 L$ T2 F6 Z proxy_redirect off;
`! p. ]% ^% d proxy_set_header Host $host;
4 u8 s& C% l& s8 X8 x' N proxy_set_header X-Real-IP $remote_addr;
- I) i" M8 ~! f8 T/ ]7 x, y% C proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
) R. @; c5 J8 p& y }
# T' k9 E3 M" d x6 K}7 | X5 n: G \$ l" e& d+ ^
六.测试
9 s% W) h, U% a3 e9 [5 J3 O3 H1 f1 a9 B( _( W$ U
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% c8 Y0 Z8 }* M4 k) b" ~: P( W& l3 e( `% {$ c. Q- Y: g3 E6 V7 x
<?php
" \- P# o, t% I phpinfo(); : {$ e+ H! w& e# @
?>
8 ?8 I9 X9 s: w4 }. X在浏览器中访问:
7 x N! e% c$ V& W4 y2 e( W0 S3 }# v7 D# n
http://www.52os.net/phpinfo.php?id=1 正常显示。: @. Y( V! Y; Y/ z4 Z, @5 }
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。! A0 Q) {* H* ^ L7 p3 _
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。3 l# S% V6 T+ u
说明sql注入和xss已经被过滤了
( U/ p( M3 J, s, r" N3 N, v" Z* ?% Y8 @) Y" F3 s) J
七、安装过程中排错+ d& n1 j/ E6 c& M+ A) W3 u
( }7 _, g" j+ L3 |% c# G; D
1.缺少APXS会报错; q" m ]0 R6 u" ~. \ J5 @, S
" ]) f, e8 p/ i2 J; x4 s
configure: looking for Apache module support via DSO through APXS
4 p) x. b& Z5 w3 a- F, X$ _configure: error: couldn't find APXS
/ L) K n6 ?, B% l- iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" G; a. B1 Y1 L1 B: g
解决方法:
1 g- L W+ B0 C5 k: Y% F' y+ [
0 y6 s! j l& | y) U+ }, f6 Zyum install httpd-devel) Q- j I: l/ J6 ]
2.没有pcre" _1 X8 F5 v: j; f# C7 U
; z! i [# W( S' o1 f) ^& S! Q* N) lconfigure: *** pcre library not found.) t3 b2 [8 J7 }
configure: error: pcre library is required8 j6 K: z+ ]& [; x
解决方法:: `6 X) J U7 e# \# v/ z8 n9 b2 {
* }5 P/ O# H; s4 q( @: m, u/ a; }yum install pcre pcre-devel
: Y7 N) l0 N& T3.没有libxml2
( F- E4 P+ W, \, G4 @
) G$ c4 N1 f$ U0 D2 f8 W2 E; D5 p( {8 q( u; e$ T
configure: *** xml library not found.- k( j* S6 Y1 }# j2 P7 k8 {2 `1 A
configure: error: libxml2 is required
* F- Z5 B# Y% o. U6 Y" W解决方法:$ {( r$ j/ [3 ?1 W( `9 A" Z1 _
4 a. C2 a% V. c8 syum install libxml2 libxml2-devel
, X" q! }% P# G) s- c1 `( J$ G/ `4.执行 /opt/tengine/sbin/nginx -m 时有警告, F/ r5 E* D7 h( L
# g' b4 U3 n" B) |9 q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ e# P9 B8 F. c K, J9 ^8 x
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# O2 |4 R4 K3 B: u原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 j# V6 v; K9 P/ |5 d2 o! l$ s/ u/ s, P: [: s. Y- X7 J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% o$ K3 F& c8 E. _& P6 D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"2 m: H* ]& v& H. }
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
/ Y0 Q" ?& L, J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
" u, P3 S4 V# R1 l6 _" b; }4 u2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
# C3 e+ C9 x* k @4 A0 \' ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# b" D Z& F ]8 r4 A& V2 W8 [解决方法,移除低版本的APR (1.3.9)
; l4 {. ~9 @+ I: S: g; S1 \- j) b. g4 |9 w! F9 y( t
yum remove apr/ Z" M3 W: a+ d1 {6 {2 x* ~
5.Error.log中有: Audit log: Failed to lock global mutex
3 X6 d5 ^ }( }* J
8 A+ G6 k+ m, C8 q) w, T1 Q K% D8 U- b+ z2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 Q8 N" }. t) d) C, F
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]" ?$ d5 ?! E1 Q j
解决方法:5 g8 h/ k8 s' A7 \9 X3 h
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" b" ]. u' [( u
$ K1 i/ _# M9 q4 N5 r
SecAuditLogDirMode 07772 O/ q, U. w+ _) p8 v0 @6 a
SecAuditLogFileMode 05503 i* i' d7 d1 t' X+ u9 P
SecAuditLogStorageDir /var/log/modsecurity' k4 O' x, A% |* C/ R
SecAuditLogType Concurrent' x9 T& l, l. t( Q9 T+ r& @6 W
参考文章: E1 o% O% F! o8 q% D/ B4 B8 j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 p4 z# |3 ]% ?5 N- qhttp://drops.wooyun.org/tips/2614 |
|