|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. Z0 P+ @' N+ k; T7 G4 ?/ w
" {! ~2 A. [) u: W9 ]8 h一.准备工作# X! j: Y' t" g, L. ^5 Q0 P( ?; C: {
' o/ Y V+ B+ Y6 a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ V0 W: M+ r% s) w) ^. h0 Y) m2 u2 F
( P$ O1 h# u3 U4 w3 f, p( `tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. ?0 x) h' M% c. I1 m3 J! l, M1 p4 C* F8 r
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz7 W# D7 C7 E8 o( f8 P/ y: Q; D
% F: `7 W* Q aOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs* a- S+ d) n! r8 s0 S1 w
) ~0 O( ~6 o) o. f- f; S, u依赖关系:
4 E3 a9 K% S7 |- ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. v' _! S1 s: F& a e( r
- O7 a: z& T8 b3 X. Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel. J) p5 |8 }8 R5 I) e5 X: a: n
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 Q: g2 O. F) F. F; m( E3 O. t v$ {. O7 j
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% v' R( ?( ^" L& u3 A* }二.启用standalone模块并编译
; W) k9 G2 G- ^9 p: X1 f
& \1 d3 Y3 v# O; o4 m3 N下载modsecurity for nginx 解压,进入解压后目录执行:! ^- P. A u4 T! Z& h
/ P/ m0 R8 X& k3 W! O" ?./autogen.sh
4 [) y/ _# d, e' n0 @9 X5 K* U) l./configure --enable-standalone-module --disable-mlogc
; Z- U) W% I, o4 bmake
" } |! M' f0 O9 n9 q1 `3 {5 e. G三.nginx添加modsecurity模块
+ x. R K8 t8 A! {# L* P) b0 J4 Q% }# |$ c+ N1 `- e( N
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:; m% x/ A: N; R' s
; }4 h ]3 _ q: E./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 C9 {' F1 ` ?7 B( l) t6 u" Dmake && make install
; }% k6 Y% G& }- w9 j1 o四.添加规则0 e" C/ j: ~0 k/ E+ \
: k( ]5 ^* Z$ F& g9 I1 T6 }
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 |0 A1 g0 |9 G* R1 J9 C% [4 z6 k( p" |
1.下载OWASP规则:3 ]( F8 r, h) s# ^) P7 J
1 I H, }- ~: ^9 V/ k
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 }4 c* k5 g' _. h9 P
0 Z ?8 F' Z* N# j; t0 j8 B2 ]
mv owasp-modsecurity-crs /opt/tengine/conf/
# r% n6 ^! N0 r9 E6 i0 \0 M; j3 h) U8 d9 F7 i, A* p) v
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 ?4 ^/ w* A0 V4 w2.启用OWASP规则:/ }9 `" U0 m& Q# l# N2 N6 V l U
; g) |. R- l- J* i, ~% x5 s, O
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。+ _, F$ `$ l1 h: o, \0 y
( m1 i2 P& n. ^! k. e" Y7 ?2 A: q编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ |9 `: p& }2 r
6 F' F% g+ ^/ Y/ I% _0 ^. towasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! f5 [% Z: ~. c
0 j3 q8 a3 a7 u0 M } A" rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) d0 H6 f1 u+ f# E" b. J
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf' }1 C2 B& b) m2 `
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ y" [ A& W1 M& h6 JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 w+ c# b! I& B$ `) c) E/ T! XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf W; W4 j: @" `9 {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# F3 H- e$ G- c4 K% v- O
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
7 B3 ^+ b( j0 x五.配置nginx
4 x1 Y# |- o) o" Q7 t3 Q( B# f/ |7 u
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ Q- ]% G" _: r4 @7 M! w( E8 u! i4 Q2 a" ^, S4 a: G1 Y
ModSecurityEnabled on;
* S0 k9 B8 |# m lModSecurityConfig modsecurity.conf;
$ ^9 G! h$ ?& {2 F- [9 R( o' k下面是两个示例配置,php虚拟主机:& V! {8 z' Q* |1 j! b) J5 i P/ g
/ {: ?& z+ V, Eserver {
9 @5 z- G6 h, K q1 x listen 80;
7 Y& h4 o' C5 r; r! G c server_name 52os.net www.52os.net;% h9 R! L; M8 T) }# F0 ~. ?
~6 h# L! U4 T+ p7 |
location ~ \.php$ {
/ x3 P( f7 n/ ?. B; k% \$ Q ModSecurityEnabled on; : b* S( K1 I& n5 T2 D
ModSecurityConfig modsecurity.conf;8 _4 s* `2 i+ V0 K% R# s
& z' U( S4 B# H4 U' V6 j
root /web/wordpress;
8 u6 b& I: |2 f- k* _ index index.php index.html index.htm;
( T2 Z' E$ l/ M6 a2 }0 e% B 2 D- W4 r1 {% ~0 H
fastcgi_pass 127.0.0.1:9000;
0 Y! h3 M& L9 e4 F: e; ^ fastcgi_index index.php;$ u/ X2 O. j! A: p8 ]7 \7 d
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;8 }( ?$ W" O9 x% U# a
include fastcgi_params;
/ C. t2 D" g# c1 x }7 P/ M, S) g$ J4 H& V: ]
}# L* E& O$ [$ H9 v- E) ~( ]
upstream负载均衡:
* r4 ]4 I3 j* E* t
1 Q$ I) H4 M. j6 f5 g! ?$ v/ lupstream 52os.net {" h. _$ q9 J% a- R) P' Q' Z
server 192.168.1.100:8080;
% ^- x Y! o! X& A8 U server 192.168.1.101:8080 backup;& c) G% P8 j8 x$ ~ t% `! N
}
% o$ o! F6 @. c. ~- n! H/ U8 @, G! G
server {4 q- l- {( d: X6 x; G
listen 80;- ~1 b+ u( u; R1 _( C* H" r6 {; w
server_name 52os.net www.52os.net;
+ J$ t' f1 N0 a2 o4 f; I8 \. u0 O+ U9 @) z8 Q0 y d, s+ ~' j3 y4 t5 b' E
location / {
8 p" w) q8 v0 N! v! d* N( U ModSecurityEnabled on;
# U( X# M8 D" f) l$ }4 ^, e1 B ModSecurityConfig modsecurity.conf; 6 v$ J8 W. q. }- @& `
7 ^1 [* D" L8 J5 _6 b0 p proxy_pass http://online;+ L0 X9 l$ o- ~" g6 @5 {
proxy_redirect off;; v# j$ p* f& Q
proxy_set_header Host $host;
3 e" v% h$ J T5 a+ \( W W proxy_set_header X-Real-IP $remote_addr;
( ^; D: R9 x( ?: u; x% ~ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;0 Y ~' `, [, h$ ]7 t
}
4 Y0 _( n, ^: J% u' }}
7 q- P1 _7 F! w% ~* N六.测试2 i+ [ m1 i: x( K
& }- r1 B8 K4 @) i+ I; I: t D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:: v0 e6 j& [3 L0 q+ e0 U5 \
4 l4 D; P: `1 }# r% S. @3 t, Q<?php
/ |' U* G: M3 P& ^' V! t phpinfo();
# S8 E4 P w/ x6 p2 l?>
W% d# T1 ^* g' G2 W2 A在浏览器中访问:" I9 R% L$ z0 Y- N
+ ^' ~2 ]; T* ~; bhttp://www.52os.net/phpinfo.php?id=1 正常显示。
6 E* {6 a0 ` ?6 Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. x6 `" f* N# Ahttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。* v7 y# v" R& m7 z& D- |1 L1 {0 C! a
说明sql注入和xss已经被过滤了- t* d* C) H9 _+ c
; h4 o3 A% g5 e3 I七、安装过程中排错
" D. G a' T/ J
" s# D/ h: G2 Q" L7 ^$ ]. M# `1.缺少APXS会报错
9 W2 n8 w. U& v2 U7 S8 c. s
8 W5 K# r5 F# V' M; F7 Z6 }configure: looking for Apache module support via DSO through APXS
O4 R- t7 i4 `1 _ ]7 Mconfigure: error: couldn't find APXS
/ [2 N9 B& n: Q' x& lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# Y& J0 X. C, M" D- ~# `
解决方法:
% P3 r- y2 v0 M& i4 M* g* E* c& v; s. b5 ^" M" Z4 j+ Y
yum install httpd-devel# s5 f/ c" q# n* i* V) n
2.没有pcre
4 c2 L/ P: x+ y( l
6 |7 |; w6 i, L7 j. ^2 Y$ kconfigure: *** pcre library not found.9 P( m4 N4 p5 i' R0 z+ f
configure: error: pcre library is required1 e: `, J9 C9 c) P% V. @* F' u
解决方法:
9 ]$ z/ X; |! j2 q
4 f! v6 i. n3 j: Lyum install pcre pcre-devel: \! ?4 i0 n A4 b
3.没有libxml2
/ @& I) Q( H9 d, m3 O* L. H1 K2 o s/ U; b) f
) H" G0 O$ I L4 i/ e' C0 ]1 Z/ wconfigure: *** xml library not found.. w/ a8 s( d& e! d N0 [
configure: error: libxml2 is required
; G& X% D7 x% X* \# k9 P6 k解决方法:+ H L9 o- P* R9 u) E" ]
% `. j# T/ k1 r) ^4 p9 Nyum install libxml2 libxml2-devel
" j! ?" y, o9 }4 J! K+ a4.执行 /opt/tengine/sbin/nginx -m 时有警告6 {9 ~! Z9 B- e0 \9 \# Z3 s
$ j' j( K) b4 A/ C5 Y) A
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! H* u& N3 Z3 A2 k" J- y& n3 v& tnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
2 o0 A' G0 E7 v. m. A原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
0 X, b* d7 Y; y5 }# m! }" p, l/ H6 ?$ R# e+ \# }' w+ }& I$ L3 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
) N. A) }: n5 |# b4 }7 x! p/ W$ y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"4 B$ P2 h# S$ O" O) d4 N7 Z
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 s# V) R$ a, [5 z+ S6 s5 Q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 P8 F6 `/ }3 X" {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; Q( a9 u0 U+ u2 I- k( L& I# x0 D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.( X) R% R8 h, i% x/ Z) K! V9 A
解决方法,移除低版本的APR (1.3.9)% E: v( {. L4 N
+ Q: z7 Y9 t' O6 A4 oyum remove apr3 a$ o3 F, z2 G8 i+ r C
5.Error.log中有: Audit log: Failed to lock global mutex, @' G3 G( y2 l( e
: e. J( n) R/ C3 }6 b2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ' J7 f# G+ h" L% n! C9 n) A, C
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 K5 N1 d9 Y" l! n
解决方法:! O3 @' j' m# j7 y! J4 D5 ?$ O
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 |- [. e' u7 {3 N8 r+ S& H! R e9 y- j, |6 @! ]7 M: C5 G
SecAuditLogDirMode 07775 C# i+ C9 q: s- J& ]
SecAuditLogFileMode 0550
% A( e6 I* }$ Y! ]& _ f3 \- }$ YSecAuditLogStorageDir /var/log/modsecurity
, B! L9 p3 Q+ w2 s1 o* TSecAuditLogType Concurrent/ A$ h( s3 S# a/ q
参考文章:9 M \. u6 ?: M4 x& G
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 _4 l8 D8 C2 I: ]2 z2 n" ~" C: }
http://drops.wooyun.org/tips/2614 |
|