|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: z1 |: M, B3 N6 C: Y7 c3 _1 G2 H' |' g0 Z0 T. p9 J: R
一.准备工作5 x$ i: n5 _6 U a) L$ {& z8 O, ^
4 { q5 ^. V; y m7 @! m+ I系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.04 G$ X: s" c, _3 ]' q2 O; h
# {! C7 p3 k$ l! E" z- J1 V
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz1 u" T+ M) L+ M; E0 B$ b
& l* h1 r, n- R, I( p
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz" ?- V0 K) e) w7 f- m
! f+ L0 q- A. w# f/ r
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! d1 H, z% {1 `
' S6 J: r2 ^* x依赖关系:
- e q1 T; I$ f8 I$ Atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 Y. {; y# \8 u) S. t. d
: T, A5 b# ?2 K" o
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
; E8 Y% Z, `7 @5 H& b" Z9 Zmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. Y% e$ y; b* @9 s& [( m
( h6 U' V* G$ uyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
( j: Z1 s* U5 z5 A0 {2 ]二.启用standalone模块并编译
. A; d1 Z o7 [
! _% _. W) {6 V3 x( D- x下载modsecurity for nginx 解压,进入解压后目录执行:
8 y) O8 P' z1 J" R4 ^+ i f! s5 Z7 k. Q$ r p
./autogen.sh
8 h8 ?6 k! g, ]* {) _4 v./configure --enable-standalone-module --disable-mlogc: j, _- k# Z: B, @0 ^
make ! @8 e: `8 f+ T' q% a5 R% O
三.nginx添加modsecurity模块
6 ?5 b+ V2 S5 D' _4 Y
, `; F: F9 R$ u! g! F在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块: S; Y3 m$ W; k3 C
: Y1 B3 R* x T, |
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine. R8 j& B1 a3 N% k
make && make install
9 P' Y, B3 ^6 q/ n. W四.添加规则6 o2 l+ d. c4 b# y
5 X7 c# l4 i A' b6 ~9 w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。- J$ M" v" a" G4 b4 @1 q
o2 t O, i' r) j/ I1.下载OWASP规则:5 N8 s H: V, f' h' q+ x
; ]/ k; }+ a/ l2 g& X
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ {( m( U8 w J/ s; j9 e3 J% _9 S% ]8 V
mv owasp-modsecurity-crs /opt/tengine/conf/1 E$ H# T; _3 {" N
2 U' W& [+ G, T- m9 v
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, Q: o8 T6 g3 i; ]% F! C
2.启用OWASP规则:% G% O$ h5 d- w9 p$ T! m
; i9 o+ l- p" v) J' B复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 ?4 a5 W0 {6 p. p; g- a" C
; ^/ F5 F" Y$ ^
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ \4 Y5 t& A5 E6 u$ r8 u O+ a( o9 v& p% [. h
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。5 W1 z1 {; s, x* j+ ] }
2 t* R; Q/ p, u" X% I5 I! ^
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf& Y6 {& D9 _3 N" m& t* L
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 z/ d# G+ a- e3 hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
% E2 e0 e5 I4 O O, \$ X0 |& K" eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; y; ?' F% F) j1 N* @4 e6 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf+ y! l9 _' {% G
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( h+ F; ^" K. {3 v/ V( l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
, t1 T0 r% w6 w8 I" N3 R# x五.配置nginx C* f- T9 g0 H3 N
: z3 I+ E- s* O& L, y
在需要启用modsecurity的主机的location下面加入下面两行即可:# R0 D% @, u& F+ M* S+ T b
7 g9 z! @' [% e7 I7 ^2 D! s ?, X% ]
ModSecurityEnabled on; ; p; S% t8 ^- \; S% w
ModSecurityConfig modsecurity.conf;
- }8 ^# S; t! ?0 S: {0 i下面是两个示例配置,php虚拟主机:$ d& w, G3 _7 N- d! H9 s
8 w9 x; t3 K. k1 z: p# X" V
server {
0 d: G' t% `8 i$ u7 \% A listen 80;
6 u; e$ w* ^' C server_name 52os.net www.52os.net;% Q6 B; U. b- c$ R
- ]8 A% ?3 ~# L4 @3 E5 z7 s location ~ \.php$ {
; }- d$ F ^3 u; V ModSecurityEnabled on;
+ E4 w9 t( V8 {+ l* c, L3 x# } ModSecurityConfig modsecurity.conf;
) b% H& B4 S" o+ R, e6 g8 j3 V9 y$ e0 b" q* T4 h# @
root /web/wordpress;
# x" Q* u$ @0 X) B* e index index.php index.html index.htm;
' y: i3 C- G# q6 V q! t, I" T 5 A, \2 C, i$ R* }& v
fastcgi_pass 127.0.0.1:9000;- ]4 {& U7 A" m
fastcgi_index index.php;
9 O3 E5 s# {. d3 G* L fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;8 e/ d7 d$ s1 W& t' U3 u9 X
include fastcgi_params;
/ F: B/ C/ L1 t3 j! f, A. o% _ }+ B+ }+ ^6 D7 j# d
}
3 c6 N2 W6 Y" s+ q5 i7 _' Z+ U! cupstream负载均衡:
, d; k) d: Z0 A) I: n9 X
) F9 F# R0 j* w6 \6 h- wupstream 52os.net {2 o8 ~2 H& z% i. H0 v, ?: r0 @2 ?
server 192.168.1.100:8080;
& w( Y' _9 D6 b' q server 192.168.1.101:8080 backup;
5 X3 V7 v+ z8 J3 F/ ]}
% L- _* X3 [7 [ }2 Q: U
4 y( w# S' N& K h5 n) mserver {% [0 L U7 @, s9 }
listen 80;
% `6 c8 X( S' t0 B& H. kserver_name 52os.net www.52os.net;
4 B3 w' ~( O+ V! F+ l' a3 H" V3 f8 Y6 c3 B
location / {
3 S* S( H' e& [* J% d+ A ModSecurityEnabled on; $ @$ {2 L( ]* U1 ` c# G& w$ Q
ModSecurityConfig modsecurity.conf;
. d9 \5 d. ^% p; a9 W' U/ c) `. m9 e8 }1 k1 _' B7 k
proxy_pass http://online;" R. S9 K+ z) _; a# D: w6 q
proxy_redirect off;5 D2 g, E9 w3 Z
proxy_set_header Host $host;# \; c. e' H: l9 s
proxy_set_header X-Real-IP $remote_addr;
* t- G( b1 W; \4 |$ B proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 x! ?4 H. ]1 _: {7 G+ i
}
* k' \" d+ g. }1 I$ o, I7 N}' u: a4 m: k% k1 _- j) H4 ?
六.测试
; ~( F- X# y; ~! E1 t* Z
; ]" ^! P3 w. c5 T( }+ g我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) i9 q- o0 I' }6 a+ X+ v
$ N! ~0 \1 e/ l7 m( u& W! G<?php
! a9 u: ], d* {: C# _0 \ f phpinfo();
1 R1 A3 D0 e. c$ j+ a M0 t?>! ^5 ^- M4 t2 z3 Q+ ^& N- t2 B( `
在浏览器中访问:5 L* [ b1 p/ c' O% E0 W8 Z
2 k: Q u! O; ?2 ~http://www.52os.net/phpinfo.php?id=1 正常显示。
; W3 z6 s2 }' ahttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# f- R- B" a9 A+ f$ ^. Fhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ h2 m5 @: \, d1 V, ~/ m/ G说明sql注入和xss已经被过滤了
2 Z/ }( ~3 H& H6 {* l0 M" P, }! o: K5 y, d: s6 A5 n4 I
七、安装过程中排错$ ~" E+ ]( P4 r
) J2 C% B( K" z5 I
1.缺少APXS会报错
! J z( [. z7 D
# k6 K9 F& P! M% `configure: looking for Apache module support via DSO through APXS
: |& X8 E3 }. q/ S- Y$ rconfigure: error: couldn't find APXS/ W' U. C1 d2 x! g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! E7 e0 F6 p" C7 \* I
解决方法:% o4 S1 p1 x6 P9 z9 i7 {+ H! |
. s2 x& [1 l4 A4 i9 R5 G
yum install httpd-devel
9 i7 h+ L, o8 Z' P: z& f2.没有pcre
" t" w$ A P0 }$ `+ G2 c4 r/ t
8 S) N. `# U1 B+ Z; C* s( J! fconfigure: *** pcre library not found.
0 q4 _& B. t2 jconfigure: error: pcre library is required
# }: L# i+ o0 J# H/ {1 Z解决方法:
" y1 A- `- m6 b! M6 v7 _! q2 d% f# D4 k' v( e
yum install pcre pcre-devel6 b5 f4 p F; N) M# Y
3.没有libxml27 f# I) Z. Y- j, W
: s! ]) m; q9 W$ T
' R' K9 I+ ~( z! v
configure: *** xml library not found.6 {/ G- M2 y) {3 B) b" c& K% K
configure: error: libxml2 is required
; A- R- u% e! a* ^# Z解决方法:
$ o- A# k6 a4 O- l3 Y/ S
: [2 R1 y$ e9 Z5 B" R5 p! r7 r# Ayum install libxml2 libxml2-devel+ x2 M* l' `& R$ R* [
4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 ` ~1 q& }" h2 x3 f, y7 k6 ^# M: F2 M3 h) q! u
Tengine version: Tengine/2.1.0 (nginx/1.6.2)# x5 ?! k" T) s! v1 o
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!; p ?) z7 _& g. R
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ I( T8 c+ F# F! _& `+ R& `
$ u6 }2 _; ^( K1 S2 ^9 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; i7 l/ @) r/ w7 ?4 ~3 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
' F2 d% S9 e z/ j7 ]- `2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
, K( B R# x- j4 N2 |; c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 u1 v2 s* s4 U2 v! _: w
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
: B" z9 Y! A& }- g" v- h9 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On. a3 X6 d- r/ ]4 O% \1 ^ [
解决方法,移除低版本的APR (1.3.9)
8 v% S' b' W* f# E- ~7 R& y( ?
4 y$ B3 y1 j3 ^8 i5 G3 D7 tyum remove apr& b7 q# O5 x5 J; D& {
5.Error.log中有: Audit log: Failed to lock global mutex
; _8 g. a" _& u- ^' B) E. y& f$ f& n4 U
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 1 _7 v" X- j; @9 M# v
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]* P3 R, ~- Q! j9 S! g/ _
解决方法:
. k, m8 H. s* w3 {3 y& j编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ g7 |+ w, h5 G/ O h
C' N$ j& b/ s: Q; vSecAuditLogDirMode 0777- W5 L' @3 Z) K8 O
SecAuditLogFileMode 0550
$ l3 X# V7 r& ]" MSecAuditLogStorageDir /var/log/modsecurity6 E: [5 L0 O4 q& t; x: u
SecAuditLogType Concurrent
# c) |* q8 N( V- O* {参考文章:- U- i, {9 ^( u7 d9 `! e
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
+ G& p( h4 \3 q& hhttp://drops.wooyun.org/tips/2614 |
|