|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ [* v+ Q. ?9 C) P5 v+ t# O4 f: L5 Y0 Z ]1 \ u2 \* |% z
一.准备工作1 u4 j" }$ A; c4 e6 p' q
$ L, a6 F2 C% k) W3 H- {
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0. G! x" b: m" E P* W! @6 [
/ E# R. }0 |, I- s5 G/ i
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( }8 p* w* n! b/ o+ Z2 s4 [
$ J) B3 G1 X2 _( G7 c
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ u. g; G/ b8 y. G1 _ R/ U
2 E1 F* Q& e) YOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
" o3 {! ~' ?' G; N7 |) B: u
( V" V+ C* e1 L- Z% j7 A依赖关系:- S) t- L. ]. R
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" H: v% q' y4 ~$ [: P9 {+ B& p, J
; d x+ {% O6 z( g% V ~( n2 V
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) Z' K; b j5 U
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 u: `+ ]) q, V
& K, B# r) i2 |7 s- M4 t+ T5 e
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* Z/ C+ ?8 i0 F2 v" P' |$ S
二.启用standalone模块并编译
6 b7 R8 X* M: ?4 Z, @! z% Y* X$ T8 c9 b/ o+ c9 R" B. \1 Z
下载modsecurity for nginx 解压,进入解压后目录执行:: t+ y% d& [5 G6 W! n
: a8 E! r6 o6 d1 P./autogen.sh
- ~' `& ^( z- c./configure --enable-standalone-module --disable-mlogc
" ~; [% ~; M7 |make + J! g y) x/ Q2 L& n& z
三.nginx添加modsecurity模块7 j3 A& L0 J) h
\) [: T$ o% Y& h3 x% n在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* S4 R$ F# U% W( l. X9 F. M9 u/ m H2 }3 {: \0 C1 ^
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine/ r2 A; Q/ A9 G" ]
make && make install6 [$ i1 I: D3 O! l
四.添加规则
, Y! G/ R2 ?4 f# U
! Y/ g, J6 V8 E U Wmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# V# \, x5 z. |& z% p( |+ q, |6 h# k7 p8 {# J
1.下载OWASP规则:
5 X* w A7 K! K+ d5 [' N* T' a+ z' m& @" C9 P( B% G% Z+ |
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs' Z4 a# t( S$ q& g' Z2 j, K5 \1 ] t
2 z6 Y4 Q* G4 C, G: c
mv owasp-modsecurity-crs /opt/tengine/conf/
: n6 ~- ?* j8 X @
9 G# K! J% f1 V) icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 x) C" z- Z, p% _2.启用OWASP规则:+ r$ u& u$ x5 ]! M5 _% [
2 @: x' J0 z8 j7 K- Q/ y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! k$ U6 ^+ Q# W3 N* K9 z( B$ w3 V6 j
3 H( R/ A0 Y# e4 e2 U! ]9 X编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 V4 _) E3 i5 w: {. ^2 Y+ l$ a+ P5 {, u# V& i
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。+ D$ N4 K I7 V# J. a' L
5 |$ n8 |1 C+ M/ i8 F" ~
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# g3 p- a0 H% u9 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; @1 z k! t/ l v! v/ K' s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& j$ V2 s4 Z9 d& K2 S9 AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
% v" @" @! A3 K4 e0 }Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- \! W1 W# T+ j" p$ gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
( K9 l e; q) n y6 zInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# z* C8 @9 F+ {. d9 u
五.配置nginx: E; Q3 d' y/ I1 ?( ^
2 ~+ o; b, j- W: d: L0 h在需要启用modsecurity的主机的location下面加入下面两行即可:8 x( N3 l; g' k# O( N) n) `' @; L
2 O Q2 ^4 B8 Z. p3 f0 ?ModSecurityEnabled on; ' x( q( _- ?9 R4 |
ModSecurityConfig modsecurity.conf;
: f* }/ k7 w" c" E$ l下面是两个示例配置,php虚拟主机:& ?. l# c: c) h* ~" W' d8 a9 j
* x* v5 \8 v3 N, \0 c G) {! S) g6 H
server {
: ~+ v @9 h7 C. Y, t6 v listen 80;, K* ]3 X) g0 S5 Y" v* \7 `
server_name 52os.net www.52os.net;# g$ w4 X8 k7 P7 [2 v4 f
. E) I/ Z/ A0 ]8 ^4 T
location ~ \.php$ {5 U# J& h! V: d( A% {- Q
ModSecurityEnabled on;
1 m: Z. K# I# B, l6 o, O* k! L ModSecurityConfig modsecurity.conf;
1 R5 z* W$ b; F( j6 X6 ^, ]/ J# Y3 c: ~& Q
root /web/wordpress;, l* t7 B4 X& m8 t, h5 M( J
index index.php index.html index.htm;. W" o3 ~5 Y! A: f `6 M5 q
. i( o0 e; X% @4 X# C
fastcgi_pass 127.0.0.1:9000;( x% g: `9 l! w5 P
fastcgi_index index.php;' V$ l- L* v. s/ N
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;. D7 K* w+ ?# v
include fastcgi_params;
+ h% R' s/ Z- V9 W# Y' O }
/ M9 z( G* V0 [4 L4 k( } }
' N2 w3 b+ D, Wupstream负载均衡:/ g) ~9 ]1 K H) R4 a4 l
, v6 A& J) N* Y3 v4 r
upstream 52os.net {- J5 M" Z# k$ j! g3 G+ ^
server 192.168.1.100:8080;
. x) l2 v# |- d# ]9 \ server 192.168.1.101:8080 backup;1 ?& E! k0 i6 V2 v
}
' t, Z! f( B0 ~/ E# f R1 T7 n; _
" f" i ?' g; ~/ F8 N5 Y, Nserver {& r% S) _- z( l) h
listen 80;
8 T+ V8 Y2 R6 @1 \server_name 52os.net www.52os.net;
( |# U4 W, h; \+ V @( Y
; L) l' H6 s" a0 }0 ? Y: Mlocation / {
1 b) T: {' b7 d) S ModSecurityEnabled on; ( l0 x/ ~9 H9 ?5 c$ C2 S% ]5 a
ModSecurityConfig modsecurity.conf; 1 ~) @9 \0 F$ g2 o3 ^
9 {! M9 ?$ i* I9 c7 m/ C9 o proxy_pass http://online;" A6 M2 h! T4 v* S( g3 ?
proxy_redirect off;
0 v- n: o3 S, F" F0 I" p/ p2 z proxy_set_header Host $host;' O9 _7 K4 t# G: B" h
proxy_set_header X-Real-IP $remote_addr;
- e8 ?0 z4 \3 X1 m. C4 \ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;! k9 ?8 V4 B, R' W2 P5 w
}2 y. c/ v8 P8 {! \. A4 V9 o
}" P8 f- V* a& _# y- |# I& E- `
六.测试
' ~8 X7 p7 B9 e- J: d% L
! D0 }7 O. X" U) u# `我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( G H* u5 E. m) y+ n4 S
" D1 W" I- X7 l7 c<?php
7 ]/ Z. d* K, i" N, Q9 \- K& [ phpinfo();
& i7 h9 N' p/ F" S?>
+ i j: h1 s& D在浏览器中访问:
9 ^7 T$ `7 s5 J; c, M2 U. ?1 A8 `/ y$ f' t- I! }& f7 x* e
http://www.52os.net/phpinfo.php?id=1 正常显示。+ C7 J# h, o. W( ~
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 ~- b/ \) a' i1 X" L
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) d0 w6 w3 c. S) [. T
说明sql注入和xss已经被过滤了
! u: W- H) X# F. F/ O9 H
4 J% t( ? F6 z2 t8 \% d& H1 y' G: V七、安装过程中排错0 w5 q5 z' P. s
' b( H- `& W4 r" H1.缺少APXS会报错1 b2 U: e) ^2 E% u% M9 o: x
' C( o$ g2 a; ^* z: oconfigure: looking for Apache module support via DSO through APXS
/ Z5 z0 Y) ^& s# W7 cconfigure: error: couldn't find APXS7 d. S: e( t) R+ p) U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# v: ?' l1 ^/ u解决方法:7 s) P$ J3 E5 N7 M( J& D
( D3 @9 {4 l* E: i1 N, Y; Ryum install httpd-devel
/ @1 L) v* b" i( T9 O2.没有pcre
( U1 L$ q1 E5 G8 A: ^. N2 k( [# c! K E7 u
configure: *** pcre library not found.
3 T, ^+ u) |4 S3 U3 }configure: error: pcre library is required
1 [4 `6 h& p+ g解决方法:0 g, M* j. R3 h# P* P' \
3 E `$ n4 X+ K) B# Xyum install pcre pcre-devel# Y0 e! s: I( X/ [, v
3.没有libxml2
, W2 M( b" I( E/ O2 U! d7 _$ T$ J* s
& a( R: z0 m8 H% h& kconfigure: *** xml library not found.
7 k; L& P. N. \* ~9 a" [4 N7 Zconfigure: error: libxml2 is required
5 _( Q( p, Z1 g& A( L解决方法:
$ L& @/ J$ e7 {; i4 N) i% J4 W7 S
: u- a* q+ i/ \ o, Q5 j# Zyum install libxml2 libxml2-devel1 \+ f1 t6 U6 v! r- K, f( v6 d
4.执行 /opt/tengine/sbin/nginx -m 时有警告
% R; I. b4 ^: g. o3 u6 ~- a: q6 x- O) G: f' k
Tengine version: Tengine/2.1.0 (nginx/1.6.2)1 u9 G5 X; H! h. X1 v, N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- U, @7 D2 Z) t: Z+ p& U# W% a6 b原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ \: H* w3 T- s7 I$ I' Z7 K' d4 z
6 p, l+ F, p8 b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.# V9 g( \; V% k' I: c3 ?2 o
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 z8 a* }/ M8 w- \2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, o. j! k, w5 d/ b% z. k2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. Y+ W# |, I( J) U0 ^9 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 f) G9 P! T9 W6 H5 H$ ]( X2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
* \$ ?4 b/ Y1 k1 J2 B- o解决方法,移除低版本的APR (1.3.9)
' A9 @- O, d# R$ V, j9 u
. M% G, k7 n; r( S5 a7 xyum remove apr) `3 F& z: I) U/ V* A- l3 w3 |( c1 q
5.Error.log中有: Audit log: Failed to lock global mutex# h& G6 O \6 ^% L% a
; X7 ]0 f" {- t/ ]
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
" O. P4 v1 o+ [0 ]$ Lglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]8 d) Z! Z0 n" L
解决方法:' T1 e1 R4 q" ]9 K# T: W5 I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:2 L6 g7 S' \' B+ s& z* @$ ?/ u% `2 [
n. ^ y! v3 v. q( M! \SecAuditLogDirMode 0777
2 H& x3 B8 N- k# ISecAuditLogFileMode 0550& A4 B' h8 ^5 M4 F2 y v& T
SecAuditLogStorageDir /var/log/modsecurity3 Z. E5 ?9 R% u& o; w, U
SecAuditLogType Concurrent7 [. x$ j( Y. c) a! ^7 N
参考文章:+ Q; ]! }5 D- K9 F% d: J# M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
( S) B8 u4 [' s; ahttp://drops.wooyun.org/tips/2614 |
|