|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# n+ u5 X, u+ i: @1 Z4 H$ C% I: r
一.准备工作
6 K9 g. ^& e- Q/ s- V2 R4 E& Q, G2 d4 y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.02 S& Q) V( O% R) j' r
7 v2 @3 A0 D/ `
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ u! n7 v* }4 y0 ^6 R
2 w, ^# r* c) T$ H) ~; [modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# t2 q+ R5 [3 U' N0 F- Q& n
, M, P# k2 o, C1 H$ lOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 U3 ~1 A- ~! r& N4 S; A/ s8 @
9 e- j0 l+ F U5 A7 x+ s$ g% s7 d依赖关系:
, F6 o6 s: }8 U) Mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) y1 } h* ~) {% {. s9 p: V+ @4 g' s6 C! s1 Z
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
7 _/ X. G. }/ @$ i4 Q; c8 bmodsecurty依赖的包:pcre httpd-devel libxml2 apr
5 d+ `/ Q' I% [+ y6 t
1 A X$ I! {5 myum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# P2 @* A5 C0 |! P: B5 O5 u二.启用standalone模块并编译
I9 p$ J) U* P& e, b" p$ T" C& b5 h* R. K; z
下载modsecurity for nginx 解压,进入解压后目录执行:* W# [1 d L- O4 N; K5 s
; ?1 ?" a& _6 S/ v4 B. \
./autogen.sh
* k; U/ m2 s# D. V2 j' k./configure --enable-standalone-module --disable-mlogc
6 z( T- c4 ~8 E% n+ x+ omake
; x6 a+ d, K7 `三.nginx添加modsecurity模块
# _+ p% s% P0 h8 ], H/ G4 x* `( B q7 s5 v4 S* M) j; k! K
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
) t, x5 [5 r- p. h- W. }
; y) Y( z% H/ r" i1 p./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
/ ]; n9 ]' _6 mmake && make install; M' f2 c/ ~6 r/ w- K/ _
四.添加规则0 w# K7 {+ Z1 W- W; v2 e
, M4 p" F, {% U! w& P
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 o* g/ p% ^- X: D# ?8 {6 g9 z. D8 u1 |3 k
1.下载OWASP规则:7 X, \. J, V9 j
" L: g1 i6 R' J& j4 \3 o
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# ^ O0 J* R2 Q# ]. I- T$ S' |( a- M$ v$ k
mv owasp-modsecurity-crs /opt/tengine/conf/
; l2 j6 r8 T6 c7 h7 V" K; R9 J" D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ ?* w& l; i. w+ a' `" i" d2.启用OWASP规则:$ U8 e9 a* M5 l0 t: Z! R- o
1 U9 I* v9 Y4 ]3 G* j( Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
f6 E* V) ^1 o7 S" Y' U) u7 `8 c7 n1 a: P, q( ]. C1 C9 d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: w& K( ~1 B7 [6 X9 F
- l7 B. ^1 y b7 v8 o" ^0 Y% F% oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' ]" u1 _3 j V+ i8 u# R& M; u, k% v7 R( Y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 Q) W& c) J& l* }3 D7 `( dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf4 h* e4 V$ m, V9 q9 C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ \- x$ E. _% e* B0 w w4 X! y G; m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
+ C0 j' y+ p4 ]6 Y! m) R6 T7 wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
* z9 N4 ^. ?7 ?9 z/ C, c$ WInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* Z" X5 n) v# S, F# J O6 RInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. d( j: R& g! E6 |/ y+ p五.配置nginx9 o& V* E, L3 c. I! ]9 z w
& e( b/ `" o# O% }! `0 a
在需要启用modsecurity的主机的location下面加入下面两行即可:8 ~" H: D9 {/ s4 m% |8 }" v! o
Z x7 L3 ^1 l! [# bModSecurityEnabled on; % ]/ `( g. g; u8 V, q
ModSecurityConfig modsecurity.conf;
2 J7 e. d8 d% t! g# l( }下面是两个示例配置,php虚拟主机:
4 c6 o: ?4 {6 g/ \4 C& n; o1 m* R' c3 b8 _! H
server {
; D( R- j' v9 i5 m6 u% f listen 80;/ J, a/ X f& s' o3 N& T+ R( V6 d
server_name 52os.net www.52os.net;* |) [- y" O- v- A/ b* e! d r, U
9 A. f. m6 ?/ j3 Q/ x
location ~ \.php$ {
' r% v. D* @( f ModSecurityEnabled on; - W" H1 f' e' h- D: F; U6 ~
ModSecurityConfig modsecurity.conf;1 f' h0 n, G% v7 A
# R/ `: {7 d# s$ }( n0 Z$ e
root /web/wordpress;
( c* g- y# b% n& u% E) c index index.php index.html index.htm;0 _; w6 w/ R3 y4 ^6 k
_9 G- h A3 {2 j7 s1 Z2 B- [ fastcgi_pass 127.0.0.1:9000;9 z" h" p5 x( }9 A- X2 {( E
fastcgi_index index.php;- A+ P, A1 q) j6 ?8 v G
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; h: Q# R* N ]
include fastcgi_params;
# t! y7 P% m* I, W9 |5 f6 R) u }
2 n) m. @" F3 v( F: M }) ?& Z; Q4 w: V
upstream负载均衡:
; G/ @4 D3 n% J4 d M9 v
) z' |1 o; c: Y8 a, B8 w5 N' U1 _& ?' zupstream 52os.net {0 F! W3 U% S% a: e" z& g
server 192.168.1.100:8080;" Y2 @( K$ Z( l3 K6 A! h
server 192.168.1.101:8080 backup;! \. F7 y7 `3 d* [
}" Y4 r6 Z: [& J% ]0 s+ m" K
( y0 h' z- W4 {" v3 D O
server {
/ K4 G8 g5 c3 ~3 E1 Z9 h3 Flisten 80;" H7 C. O' s7 h( n2 S# o; A& l' D
server_name 52os.net www.52os.net;
4 \" M1 U+ I5 q( b$ x9 x- j
8 D+ K2 f4 K& o% Zlocation / {+ n) g, J, `0 c( U* O
ModSecurityEnabled on; # A. n$ }& w1 ~! a( @% G
ModSecurityConfig modsecurity.conf; ) @9 f! [/ z6 d* A' K5 B7 |: l
# t: u1 U& u, D* o8 h( \
proxy_pass http://online;
0 X. c2 d; \/ r proxy_redirect off;
; m# Y8 h5 d+ H% L! R proxy_set_header Host $host;
/ r! ^: {* K8 O# j. M; U$ a ^ proxy_set_header X-Real-IP $remote_addr;8 {7 X0 U% E5 ^( E+ Q" t) ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3 L/ n6 n8 Z; g }4 P, F& A5 d5 e" U
}
l2 `) r: V) P2 n$ K2 F六.测试9 c! S- ]0 j u& Y
* w& l: ~* N( V) _
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; o+ z6 g; c! w5 S+ T8 X+ t' s
3 g0 {' d, G8 K* Y1 i3 h/ H% Z
<?php
" o) e" `6 m* F$ y7 Z phpinfo();
8 }% S% d! `, `$ W1 K }?>+ `, H1 R X# L4 G
在浏览器中访问:# r1 t9 [3 n% Q0 {" c
. o3 T2 d- L. H T
http://www.52os.net/phpinfo.php?id=1 正常显示。
! B. E7 S8 G/ s- Shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: o# C" W6 ^* Z. G! chttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。1 m9 ~/ B8 ^$ k2 x3 s: U y
说明sql注入和xss已经被过滤了! C( c' J9 d# \) \0 z! U- f1 z/ x
) H1 p5 H" h, Q2 e3 x. R0 {
七、安装过程中排错5 y6 Y2 h" e, D, | Z
$ R' T$ _3 f8 d9 {4 ]7 j6 w8 J" ?1.缺少APXS会报错
7 i) C* X% ], m& h- n4 p: n4 `0 Q, ^+ Y7 B r) l6 ^& u9 U6 u
configure: looking for Apache module support via DSO through APXS* e( e! z6 N V( i4 I; Z# t- r/ \
configure: error: couldn't find APXS
T8 i& Q/ A( s/ C3 o/ Oapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; a+ }0 d2 L( L: }解决方法:/ b% t" G4 o4 M& d( Y Q5 Z- Z8 a1 Y
1 r2 v' u( w! [yum install httpd-devel
b* A+ T+ s3 W- P% |" y- h# W! e3 |2.没有pcre/ o- U& O4 o E# ?1 J( p8 |
1 I' O7 d9 t$ r; L/ G
configure: *** pcre library not found.
0 N/ H+ q5 z& Tconfigure: error: pcre library is required; x( `2 s& j- P: y- C9 x
解决方法:
, W' ~7 | M$ }, ^; P& _! ]" k. Q/ y7 m# j- V2 y! Q3 ?: H+ k
yum install pcre pcre-devel
/ K7 U2 }% u X' B3.没有libxml2
; f: S/ u8 a, a( B( L, C* T; o: O
! n5 S8 q6 w1 C
5 i- L4 N9 @: p/ y& m3 A! tconfigure: *** xml library not found.( C, \2 y K1 w( b
configure: error: libxml2 is required: p# @# G3 _; E- M% m" d/ b
解决方法:
4 U. w, F+ V3 A) u6 P E1 B0 q9 f$ F! Q
yum install libxml2 libxml2-devel2 h2 {. ~4 S2 U0 @: E( h7 U3 o: S8 R
4.执行 /opt/tengine/sbin/nginx -m 时有警告5 B: t2 a/ X9 C a, A @' r7 N
F, O; Y6 Y$ M! G9 T; I, J4 A4 B
Tengine version: Tengine/2.1.0 (nginx/1.6.2), i, e) G9 `9 [
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
6 ^1 ~0 c1 E! x4 x5 q; p* G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
7 a1 c, t) X8 x/ N9 |
/ I2 N" L, b. g3 m. c2 v2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
Q4 n* d1 s5 J1 Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
' d1 J8 v. R: d0 T/ E% K7 R2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
# c1 Z2 g+ P7 W6 V7 m" |) l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 X- x. T' ?7 z( T% x7 l, w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! E, ? H; D4 r4 U- O- D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 Q i- g- M( W解决方法,移除低版本的APR (1.3.9)4 Z/ v7 q0 v- Q. l8 P
& m, e! T( k0 a7 T5 ~/ S7 I" Byum remove apr3 a* T( [2 F4 o. X3 B# B- S
5.Error.log中有: Audit log: Failed to lock global mutex
6 P( S) _1 ~$ U+ D% K# x. W: f3 o! \* h: W: l: D; K4 p2 v9 G
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 3 h. j( h \1 ?/ c. V- {1 G
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]' n+ d$ Q+ S- `8 E( E
解决方法:
0 x3 \) B. l- N9 ]4 H编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:9 s: t1 L; ]& l- Q; _0 K/ }
5 R. u. Z# _! ^2 n- {$ K
SecAuditLogDirMode 0777
. Z. o T/ V/ E( Q" kSecAuditLogFileMode 05500 W/ a) n* Y0 P
SecAuditLogStorageDir /var/log/modsecurity
0 t, x& M7 g1 q. j4 z+ {0 W3 Q: GSecAuditLogType Concurrent2 s8 Q5 f' b+ D( T+ W' w5 l& o
参考文章:
& W/ _9 J% D9 ghttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) b( w/ y- X3 {8 U B4 }, ?1 nhttp://drops.wooyun.org/tips/2614 |
|