|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 e9 A/ w$ n7 F$ P. Q9 Q* l$ }! b
9 [( ~" j7 Z$ }/ {& @
一.准备工作* f( U! b9 K4 y$ Y
. ~# g# M b4 L/ Z+ I. x
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 y. g# u1 l, h/ i% E4 Q
8 R3 ~. t, Y: ^/ G7 `! _7 m! Otengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 ~9 {% x1 I8 ]- x0 S0 o5 K
" Q! ?6 B' H/ d% `( D, [1 k3 `modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 O( U" _+ }6 {1 E
* d q3 N3 y' `5 L N" B
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ m, k; f0 n7 E3 [8 J/ O* x
- C& h8 M8 c6 { F依赖关系:* U& Z# N1 _, [% R# {" H& l
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" d0 H4 Y T/ a+ Y! |
/ g; a' Y x4 c8 }: W+ ^/ @yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel; U' [9 ]* m9 Y/ D, |+ l
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; [+ I4 U+ t2 i0 j: p) t; ^; K% Q; m$ c, m" f/ c
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel. }' d3 [& `" N7 H8 p
二.启用standalone模块并编译+ R& P4 H* x0 B6 N/ [2 C- `
+ b7 V; z h; i
下载modsecurity for nginx 解压,进入解压后目录执行:3 p! \% b- S9 y
( ^3 O4 t% J9 T& }1 R* k$ g- G./autogen.sh
2 T5 u8 J6 {- E& I+ [, V% }./configure --enable-standalone-module --disable-mlogc
; N% R. H, J. ^$ u( mmake
9 W2 t' b9 `( t4 J# B9 Z三.nginx添加modsecurity模块 t- K8 u4 f/ u# w9 ?# j t
% ?* I4 r6 M+ _6 S( b+ E# Y% R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 I$ w/ U9 B7 S, _% P) `, v- ~; o0 o0 m8 G2 R' a4 @/ \! O# b7 } G, Z
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# i9 m2 ]. X9 ~5 P8 ~' \make && make install
0 L; k- \8 g& x3 g) l1 _) w0 o四.添加规则. f/ }- o; p' w& n' n6 Y2 A
; p' F5 ?6 b4 L
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ p3 H* Z! Y. B$ ? ?/ A5 {
: ?0 a4 W& d2 v9 }# h+ U1.下载OWASP规则:
: \; N4 k) B4 N
' p v9 ~; L* H5 W( Rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs: ?- S9 J6 J: R6 I
! q* Q" G5 y. A) ~+ cmv owasp-modsecurity-crs /opt/tengine/conf/
& ~4 `$ e1 G B* \1 p* Y+ b4 P' T6 D7 s( R7 S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 U/ L- a0 G5 N6 f0 b
2.启用OWASP规则:
, Z7 ^' z: L+ W9 l# ] e" r8 e2 a
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ y5 }! ?% f. B6 o, U. `) W
1 x4 h, P2 [6 t* ?7 k/ ~" @编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) q c# s1 j0 u0 E
_+ d( G3 I/ K% d* O( A4 xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ S) H$ e0 U( [; L u
9 x3 a7 f4 ~8 I" J
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* [! r1 Q+ c' ]! s% KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ H! \8 W1 u$ j& F: B2 N8 I0 `! yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. ?7 a/ C& ^3 D' NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
A0 U; e) m) v" a5 W" S/ L! l1 d0 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# p, B" {" a/ G% Q! _
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
1 B+ Q. L8 j1 m9 z- d# \% JInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( C+ N- J% a- @
五.配置nginx
i5 x$ O$ X2 p$ H* p0 a H
/ u7 J5 ?+ W) j- ]5 W& Z在需要启用modsecurity的主机的location下面加入下面两行即可:
) @2 J. n/ g9 B4 ~3 ~' I5 H& `! y. B: v& Y( r
ModSecurityEnabled on;
[- [7 ^1 I" ^4 v$ {5 QModSecurityConfig modsecurity.conf;
6 |3 H4 S: s/ h m* ~: `, {7 ~下面是两个示例配置,php虚拟主机:4 g* {# v% P; ?8 L/ @
9 T- U7 L9 R' d8 X$ bserver {: M0 d2 J. s5 G9 l6 K. \; K5 V
listen 80;
& q2 {/ l$ C2 F8 V) [ server_name 52os.net www.52os.net;& h/ A, n6 W/ e
/ V6 P2 L9 U, |' g: S location ~ \.php$ {
1 j4 u0 v, C( U- K3 t. B% i* x2 p+ f ModSecurityEnabled on;
2 x. @' T6 z. I ModSecurityConfig modsecurity.conf;
3 g. z9 J; R4 [8 t- p: _$ w! ^! J6 g. O' m, b
root /web/wordpress;
/ X+ C+ o4 m' s6 O index index.php index.html index.htm;
; ^9 X7 ]3 H) a% K4 _: }
6 K/ [, ]" P. G! l fastcgi_pass 127.0.0.1:9000;
: S! M* c& v. P fastcgi_index index.php;
v& I, L, a) c. f+ R fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 N# {4 z& |! b9 G$ I6 x include fastcgi_params; e1 _8 V. [ K& d
}
) S, M/ V& d3 |, a' ` }# `/ S( @" J- ]- P
upstream负载均衡:
! v2 w8 f. C* D
% n, M, {8 o: A9 oupstream 52os.net {
1 h) a: s& u0 p server 192.168.1.100:8080;
( c( d7 l% y/ L; |1 V3 w server 192.168.1.101:8080 backup;
5 l/ Z# ]+ K- G/ Y' B}
: L! k2 @ J- M+ m. N! y8 C9 C# N( G: D! p9 o
server {
0 L' w, f _% E* W6 z3 y& X0 d' olisten 80;
* P. k+ R. ]) v- T/ @) F' {server_name 52os.net www.52os.net;
/ R' I8 z* }- ^: E2 m& Q
. q; _' ?' [+ P* w: alocation / {0 e3 J# D, M2 e; ?+ a6 l# i J
ModSecurityEnabled on; ! h1 t" k; r! U4 _: n
ModSecurityConfig modsecurity.conf; - o* @7 L7 |8 ?; I S
) u- |9 f3 I4 D: O" L
proxy_pass http://online;+ c9 N( R; N' l4 `9 L
proxy_redirect off;. R$ m" q) o- L5 x) a
proxy_set_header Host $host;5 a1 J/ u N5 M& y! ]
proxy_set_header X-Real-IP $remote_addr; I7 R. \; Z7 [0 W4 Z* A) v( W
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;% _- d: J. w1 q( _& ]$ h/ R1 {" A. b
}( U! E+ s( g" _4 l
}
) e0 O' s/ _* V% ~+ ]六.测试
/ Q9 a$ s/ J; [$ F) J3 H" v8 q% q1 t& y" @) N
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 V1 L1 J0 B+ x7 H
J& J8 g/ h" Z3 j) x9 l<?php
; g0 P. ? ?8 W/ Q/ ~3 ~2 y4 Z phpinfo();
7 y( ]3 O3 i6 L?>
$ Y+ L O4 Y* T7 O% E+ {( j在浏览器中访问:. E! A' R7 H* w& J) [/ i
( Q- ?, R; |. w+ \6 F! M! Y/ F' L" k
http://www.52os.net/phpinfo.php?id=1 正常显示。
- E# @+ V/ h* T7 ~) m$ B6 Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
8 s6 |4 y" H$ Y( C$ e3 \http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' r6 l! R9 d6 z
说明sql注入和xss已经被过滤了
" s. J9 ?; j9 ^( t$ t$ }7 f1 ~' |, T, Y1 O& q% ?4 M
七、安装过程中排错
% v: n$ H0 M( X; [5 [- K9 s+ v: ]# V) Q( m9 F2 Z
1.缺少APXS会报错( e$ B2 {+ J/ X/ g& e9 K# Z
( \: r# E1 _# [
configure: looking for Apache module support via DSO through APXS7 i" m2 m' \/ l2 o) u6 Q
configure: error: couldn't find APXS
: P {" d6 E# t# vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ [% z. J4 w5 `" L解决方法:3 j2 R5 K# d& G
! v/ I" w; _! s$ i" w( r1 z3 N
yum install httpd-devel
* J1 V9 ^; F2 w( y/ X2.没有pcre" d& Q+ v+ @+ x/ D" Y
( v- c6 \0 ^7 ?/ |5 K: b) j
configure: *** pcre library not found.
) |- W% c( C# }; @' d3 y! g% Y ^5 Nconfigure: error: pcre library is required
" J U' H4 z# T- t' M解决方法:
$ g h1 u2 Y. N- B3 x, G0 P( n! ?& M" T
yum install pcre pcre-devel
" _, d1 R$ E" G3.没有libxml2
& V* l% h2 I# P1 K$ Z; C
+ Q9 `4 _' o# ?8 m2 ]& W
: Z: O9 ~, \6 o9 Q Lconfigure: *** xml library not found.( W, x$ `! ]* @! z) ^8 m8 P
configure: error: libxml2 is required
, j& z$ R2 V/ X. E解决方法:% B8 r& \4 G% q6 T/ m
0 l/ b9 e$ |+ Myum install libxml2 libxml2-devel) f- e S( q1 p$ @/ `8 W
4.执行 /opt/tengine/sbin/nginx -m 时有警告$ W. s) Q ^/ w* B! ^# M" q+ U$ h
* z/ \! V" h9 N" wTengine version: Tengine/2.1.0 (nginx/1.6.2)
- M% i, k# O9 y% D) J6 b. onginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' h3 N" ~' _# i+ {9 s7 k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log5 S( n9 i( [) d9 u1 E
6 i% w# Z0 T) Q! E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( X' G6 f# Q/ J" G% w) ~( l o" h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
, c, C+ i/ S' R% u7 N" i2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!& x, m) s' i) y/ l4 K, w9 K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"- y5 c i/ o( D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
7 o! H2 C) |0 F3 V2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On." T* r* l2 w5 \( d8 ^3 Z
解决方法,移除低版本的APR (1.3.9)
$ L/ r/ A+ g- t8 P3 y+ c# Y% `7 @% @2 N# F/ j
yum remove apr$ K! O4 Q& z. U+ L. z0 @( t
5.Error.log中有: Audit log: Failed to lock global mutex
; W' g: k9 X' W7 }! l
4 U' e) j- p3 p8 r) J2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
- V! y/ _/ V7 @2 d2 z2 K Lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"] B5 e6 @2 e: ] S% ~/ O* i* d' V
解决方法:
+ I7 x5 Q- Q6 Y5 K' g0 Y编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 W. v' a# L9 P7 G E
( x: y M x2 o C: S. _: PSecAuditLogDirMode 0777
' @! O: s$ E; p: A# G Z3 x+ LSecAuditLogFileMode 0550
) q7 S( G) D! Y$ u, F8 J7 QSecAuditLogStorageDir /var/log/modsecurity# q7 j1 X; N; N$ N# `# Z+ _
SecAuditLogType Concurrent# r, N1 H7 L, ?( J
参考文章:8 a6 m9 h+ S, L; J2 a- w$ f9 g
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! x0 R/ A2 d: V. C/ Y& R8 A
http://drops.wooyun.org/tips/2614 |
|