|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 @/ n( w' m! U/ n$ e. ~
' a; M) h7 G8 N& n$ c. i一.准备工作
; w. C1 c6 n- b: U
( {" n1 o. s# @ H C4 w# Y& R系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 E; t+ h/ f) Q$ }
5 ~" j; d( L3 O" }) H* Jtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz# p* _# d2 p$ K6 v7 _0 L: s5 `
4 l+ h* Z2 n1 L! I# j2 u/ x1 a
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ y$ \; O& q5 ]( V+ a% l/ C* J2 z( a! ?4 H8 b0 n8 u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
+ [# V' x: N0 p& I+ P S/ p. i+ @
' s3 f* F, l) h/ h% t/ I$ a8 D依赖关系:
, O t+ h! l8 u# C( _& U- @; htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# p5 q4 Z- U) A Z) i9 X$ t8 y/ x
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
8 i/ a. `4 k9 m! smodsecurty依赖的包:pcre httpd-devel libxml2 apr
7 c! e$ e- X% h" ]2 u! `9 t1 ]' T$ u" x- ^( ^# Q, X4 T, S" E, [! r) W
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel3 y& M& ]' k+ k/ B
二.启用standalone模块并编译( D3 e: l3 b' e
" i" e$ ~0 F! R( _
下载modsecurity for nginx 解压,进入解压后目录执行:
$ U9 ?! P% J4 W
, b7 F5 F; {- _7 G) y" D./autogen.sh
. u/ N c( t4 T: g( y. N./configure --enable-standalone-module --disable-mlogc
) m1 H+ Q- N: Z- L% N( v* smake 3 X. v8 x/ g3 q: u. o
三.nginx添加modsecurity模块
9 j) ~% v V1 k! W9 ^+ B
8 Z1 n6 L! _/ a在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 F- ^; }( A$ E
$ q" B ?% s& |. n+ `- M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine5 }; z9 r8 n1 j2 \3 `. e
make && make install
3 i" c( {+ o& a% A1 V四.添加规则" B: ]- r$ Z) L
/ q' K. [! `; j5 m
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% O+ r" p. k! m, U4 T, [1 k! g: r3 H
2 Y' x6 [6 G w0 i' {4 K- m1.下载OWASP规则:" x5 W, `5 ]$ g& g4 \0 {( T
" i) T0 I' R9 A9 f: P2 X8 ^! Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ @2 H% @6 u |$ z# h. _ J: @/ T
2 c, I- y$ \: Z; q- T% ?# _mv owasp-modsecurity-crs /opt/tengine/conf/
& g# @) }3 l4 T# T# g- N
4 C, G0 F: q, t- @6 y) f0 o0 U8 fcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf2 t, Q$ D. F7 @/ @4 J6 V/ M3 Q5 f
2.启用OWASP规则:
0 y- B: y8 f/ D, }* z: K) [4 ]
+ Z1 Z! L7 y) e; C3 r0 ~4 B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
! j5 T7 d/ b) t, u/ }- @8 S; H; F/ L# r a1 ^$ p- P
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on' U$ |! c: p' ?% U* Y% T1 n3 M
' K0 N- x& k4 ]owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! W, @; v4 s* E# k8 g) i$ {+ y$ Y# G9 @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% C1 u+ M# } ?% {! v2 d& W
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- ^& c( z7 ]+ T% RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf d7 ?5 e9 ]5 z. U7 S ?/ O( k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! ?" o! b& z& `( l8 [* w3 u% T8 ?
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
& {" q+ k$ {) h1 E6 A1 sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 m# u3 E( l1 F- _( ?6 ?! Y5 kInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; p& U, s9 J! ?五.配置nginx+ t9 p, D o' |( D
7 r/ @1 c( O5 k( C* o* `5 B
在需要启用modsecurity的主机的location下面加入下面两行即可:
: B1 U0 ]8 T9 h! M; O
' L. P' G, [7 h! E3 M) bModSecurityEnabled on;
' C: E) [+ O2 E$ SModSecurityConfig modsecurity.conf;
( f- r( z- L& @: I& b下面是两个示例配置,php虚拟主机:
7 T; G! l, _ O" o I% H1 Z- T; n& M9 o, y- @
server {
s# y* l; g' o* _ listen 80;
0 Q3 R) k/ ^9 \3 |8 K7 Q8 a server_name 52os.net www.52os.net;
" q) g2 w' F a1 m: K
* r0 l, l0 Z% t location ~ \.php$ {- n8 q1 _# W5 `2 o4 T) e+ x+ V
ModSecurityEnabled on; ?8 n$ B2 _, f3 }- a
ModSecurityConfig modsecurity.conf;
$ F3 Q- ~# C% B% p7 o7 i/ l+ q5 j3 r9 Y) X
root /web/wordpress;
" z; o/ G$ d; O; { J$ ` index index.php index.html index.htm;+ V2 Q, P4 v/ R$ J: j- F
+ t; m+ K7 l5 E0 a
fastcgi_pass 127.0.0.1:9000;
# m$ X+ g9 |8 P5 y, B0 } fastcgi_index index.php;
; J$ A L' ^/ o* [& H+ V fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
: Y" U7 V6 h7 R2 V4 T include fastcgi_params;" D& ~; q4 [' c' _* D
}
# o: Q( r) W9 T, t: h }
- j5 W8 I- j- A6 e; C8 e7 D fupstream负载均衡:, ]; I# [8 ?5 ]
: {9 J. [0 q: p$ y. K" @9 t) uupstream 52os.net {
0 R' G5 r) k3 b* u, I server 192.168.1.100:8080;
n: W7 s( u( A8 V server 192.168.1.101:8080 backup;' n. q" g/ U2 p5 T
}5 {; f1 I8 S6 a( ]
. T9 Z' D3 R7 J; d
server {
$ E t: Y! n# G# s4 ~- ?. Nlisten 80;4 @) e% K( V9 O7 H% d
server_name 52os.net www.52os.net;
4 O" O: D" Q; y; h1 R* |- u1 t2 I9 ^, x: Y7 G, r
location / {
- x @9 O, k3 t6 }4 D$ B5 h ModSecurityEnabled on; 5 l6 l+ ]4 ?/ v
ModSecurityConfig modsecurity.conf;
( O3 W* Q2 ]) _0 A& O; H& V, {; I; y0 O' m2 G2 m
proxy_pass http://online;9 F& s4 U( p) \" S# V
proxy_redirect off;
, Z0 n7 Q9 ?2 I+ Y5 ? proxy_set_header Host $host;) m4 m1 [, h7 c& b; d( u% D7 F
proxy_set_header X-Real-IP $remote_addr;% B2 ]7 C) e# v
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. o4 R5 c2 t! d4 ~0 d }
% P4 x7 R S; h9 G& C# O+ M4 Q}
T/ L$ @, C2 i' N! A. w* D六.测试
; z) B$ h4 g" G& W& y M5 o# p3 T1 s* g. m, F
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
5 J& x+ b/ q$ b" O
) N9 f- F4 b9 `) b A) G<?php
3 M9 y7 b( {; c+ v$ q( B phpinfo();
- ^1 |5 O4 s" G6 f?>0 O+ q$ J, G8 r) _- y6 U
在浏览器中访问:
) A8 w7 U+ U# ~/ `9 s
# P+ A3 h0 Y P7 A9 x0 r5 K) t( Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ y8 P9 q3 H$ ]/ O6 uhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 C+ ~+ d/ l2 o* \% [% vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' {1 o6 ]/ W/ U, ]( Z6 Q
说明sql注入和xss已经被过滤了) @* r$ I9 V9 k2 Y7 E) J
& n# ?# z' i/ N
七、安装过程中排错
q' N1 g. ^% a$ d# S
8 l7 ]4 }8 m& [$ {1.缺少APXS会报错6 Y- k- O) [1 _" p$ [; m
5 G+ y% N3 m6 ?, E$ e6 B+ M
configure: looking for Apache module support via DSO through APXS
, N; ~1 i8 ^. t) j; L6 T. Rconfigure: error: couldn't find APXS
! Z6 N/ z8 G5 @, Napxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" e7 D+ P2 y7 c- A' E, o, X解决方法:+ |) S$ J; e- f
+ e. o) D& Q* a4 o9 }- {yum install httpd-devel
6 R2 K8 ?1 z6 `! K" d$ F) }2.没有pcre+ u8 V9 A" e* a( t! p+ Z; H6 l3 L
7 U) t# j2 r6 h$ r5 n. p- wconfigure: *** pcre library not found.; R8 I7 |: F: p
configure: error: pcre library is required. c2 Q+ f6 \: s
解决方法:4 j4 Z( ]- S7 _/ C# ?. C
( t E" h6 u9 f- C" @& Pyum install pcre pcre-devel) T. u" B( @7 ]! h, w
3.没有libxml2* a" D7 [$ h2 w+ x. `
" U$ C w2 e/ G6 G8 X/ A6 M# b7 G7 w0 v8 j
configure: *** xml library not found.0 {5 ~8 @* M' e
configure: error: libxml2 is required
+ C4 j9 d [" j' W解决方法:
, l. b1 b+ r4 y" m
* a: Q6 ^! C9 \1 wyum install libxml2 libxml2-devel
; y, M: e* M1 _6 H7 r, _: \' |4 _/ S4.执行 /opt/tengine/sbin/nginx -m 时有警告
, h4 w. K A: E. h! k; D3 v+ m0 _$ E2 _" d3 m6 N' U1 y2 ~
Tengine version: Tengine/2.1.0 (nginx/1.6.2)! V1 H. b9 k; c) f+ ^7 N; U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 n0 D7 q M' m1 I* C原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
# U3 [/ Z9 ?6 ~% C; Q' X0 `! d5 K( O6 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' l' u1 T6 b+ A) _9 }5 q6 k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
) Z) Z( w K, d/ _9 r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" C& r! U9 n# [1 g b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
) Q6 k# w- f2 B4 Z8 X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"0 }: `& Y. G1 O/ R+ b
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ k! x( ]! @6 n! G$ D2 }; \; b
解决方法,移除低版本的APR (1.3.9). h* ^" N" _$ p8 ^* J) d/ Z4 j
3 {/ D( a9 u- F9 f4 J/ v
yum remove apr" W0 m4 A( x1 @) v
5.Error.log中有: Audit log: Failed to lock global mutex- [, D0 {7 a( z& Y, `$ n
3 V0 N- o; v( a: [' \
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
- I% V% e7 Q& ^+ A# `. z: b7 Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 l1 k1 @* \" B9 H, {! D$ @解决方法:0 K* A- c* e+ f. _# X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 X( r. r3 S/ ?( f' v: @7 K5 X
5 s: b# ?* Z+ L4 I4 P2 [5 ~- xSecAuditLogDirMode 0777! [9 c5 b) B' W1 Z5 H! O7 \
SecAuditLogFileMode 0550
# v" d4 U: @: o3 F' l( BSecAuditLogStorageDir /var/log/modsecurity2 G2 }3 z3 u; z3 ~
SecAuditLogType Concurrent
( K% v: l& y+ o参考文章:" G! P: g+ O" s
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& ~$ n6 i" h6 {# a9 Jhttp://drops.wooyun.org/tips/2614 |
|