|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
: i; G4 P5 q0 c, ~4 n# ]
# C/ {* [: N/ y: W0 {2 E/ W一.准备工作
) n H+ s7 V' _9 y& e% t8 O- I5 h8 P( M$ q9 H) S$ H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! M+ p! O* y. j! k6 v z
$ v$ \& E/ d$ i% u, O+ rtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 k0 M; m/ ~: y! B* k! E" T- x4 u
5 U* b* H- l B3 D4 f- R. Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
4 O& t7 y# J3 r5 l3 [. l, e q& l- t, B4 H
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 B' ?0 v% S; c9 s- [/ W
! X+ N* H9 | j' Y- I" f2 q* \ T
依赖关系:
( J( T0 y# @1 s/ I% h0 c; M, z: Ntengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( V. @ R2 v. r5 P. b$ d1 T; n. p
6 T ]& h% k0 N+ G3 S% H& t5 {yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
% ~7 }1 m4 |- H$ l0 `2 b2 B/ q; d- Zmodsecurty依赖的包:pcre httpd-devel libxml2 apr3 ?( F# Z/ I( Q5 w
3 ~/ O9 ?' o1 [yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel) o+ m7 @( r6 L, b( f; ?4 V
二.启用standalone模块并编译
( U; t3 P7 H: P; d8 l0 q7 i& P7 i V9 S5 d
下载modsecurity for nginx 解压,进入解压后目录执行:3 s- F7 q# D! i
* R0 X# L! k& Z./autogen.sh$ i2 _" V) P0 T, G5 n/ R. u2 h
./configure --enable-standalone-module --disable-mlogc
0 C, w$ H* q) O* o& N. Umake
1 M, R0 Z% }* D6 L( a' A/ p三.nginx添加modsecurity模块
( Y3 I) `" l, K; t" B+ i
( X( u, ~5 A3 I5 k w0 `在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 Z5 ^- s: i! N- k9 k1 F
3 a9 K4 m* M; \5 \./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine, ]7 u* v) W; [$ ?
make && make install0 h. l9 _, a3 a% e% Z
四.添加规则
0 r8 l- |! p/ X2 X: J' Y* ~1 X- h2 I) M! E" n) q- Y# a( b, o
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: ]9 j& v1 I) B! |. j
4 H" e f$ j0 X: ^
1.下载OWASP规则:& {3 H% a/ L; V3 j& M: O
" c! z2 D0 K z) E. H" \. cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 z' o5 {% Z. a' `4 N; x3 v
; X) T3 M7 g! s3 A0 ?1 k) l6 Xmv owasp-modsecurity-crs /opt/tengine/conf/. g# N1 ~$ C3 K5 M. g D: ?$ d; r
, O- P% Z/ f5 F- P( o1 w6 Icd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) M5 f: y# U% ~: `9 Q4 P! O \2.启用OWASP规则:& H8 |! l1 p8 x @/ O$ n! Y# F
0 D* E% s" o, x) L
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 w% S# D3 X7 s2 ^: A
6 d2 a8 H; s3 P2 c& y; W) b编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 }: C% |1 I8 d! R4 ^6 p! p
* U! g. \, f+ O
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 ^5 G, U2 v/ L8 l
9 J* S; g9 Y9 g3 Z, B+ L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 F2 \' [1 J V Y- W l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf) }+ y! ?1 E2 V0 Y+ F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
. I. k; M. D1 A1 b, `- q9 b# aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' ?5 q# t' P; s @% ]Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# [, w( X! ]; ?( Q6 ?2 r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) B2 o9 ^' A; p, k# l5 ~. i
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 n# S& X) }5 l% E" f五.配置nginx
3 @7 n0 I. ^8 M0 F9 O4 H6 X I* x2 A& u, H
在需要启用modsecurity的主机的location下面加入下面两行即可:
. g+ v+ ]- k1 Q
v7 d+ N r3 V/ B. N6 dModSecurityEnabled on;
# W; Q" }/ q- {5 @8 W; F& y. N+ aModSecurityConfig modsecurity.conf;
8 \5 _: r h$ {% j' B* ^3 c下面是两个示例配置,php虚拟主机:4 F3 q0 T4 G* Q' Z+ I
8 ]- Y$ K5 H* D$ i! `8 r
server {
7 L& V- u6 Z2 \$ B! O( M0 r listen 80;4 @' q8 V) W* s2 T9 P8 U4 h
server_name 52os.net www.52os.net;3 @( R- V- x6 e$ `; h( j6 Q& N
6 M3 k6 }% g3 n3 T% S
location ~ \.php$ {, P& Z$ g/ b% I0 `& E
ModSecurityEnabled on; ) i( I3 W: N( r. l# X1 v
ModSecurityConfig modsecurity.conf;0 m0 |( o; F( h. b( u0 T- U
& i& l' \9 J; o% t0 S% ? root /web/wordpress;4 s& G& W9 ]* o" O
index index.php index.html index.htm;% ]. v7 W! q6 c+ R& E+ S" E
+ D! K- C7 m0 K6 u' M0 W
fastcgi_pass 127.0.0.1:9000;
' @ \; e2 p& A' F6 K fastcgi_index index.php;
4 W' D3 m) n+ A fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
; u# `. F' z ^% z& o include fastcgi_params; h! d+ k$ }8 B9 ~% u' l' Z6 ~
}. N: S4 G1 F2 V5 h1 b( ~
}
) o4 b/ Y; |( P2 L" {* ^& }upstream负载均衡:$ k9 L' e' i, W, B: S9 a( a7 `
2 }% q; m5 p5 f* F9 V! ^1 tupstream 52os.net {* m c, I9 V$ B: ~
server 192.168.1.100:8080;% h, |+ }7 U7 U8 T- a" m8 @
server 192.168.1.101:8080 backup;
5 |: S8 E) P& N* E* G' }# d2 n}7 S9 ~7 V9 n7 ^
' v1 d7 K @. g9 X3 {server {
3 D; s0 Y/ O2 G4 h3 ilisten 80;* W/ j- K6 l+ X2 m5 Y
server_name 52os.net www.52os.net;/ S1 }, ~: E8 u8 k
# g4 ?4 r$ [1 u9 klocation / { _% e1 q; E* M
ModSecurityEnabled on; # v1 h% D% l6 F
ModSecurityConfig modsecurity.conf; 1 e! z% h) H9 P
. y; M( T, C3 l3 h" e3 P* Q0 h proxy_pass http://online;
2 V F8 a. L9 _% L& V proxy_redirect off;
( a' u+ k) R6 j3 \- S/ e- Z) w! l proxy_set_header Host $host;# Z* ]) g; j) a; g* r; p9 P
proxy_set_header X-Real-IP $remote_addr;
0 P! o5 m: I% M; Y- Y proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
. U2 o; r: C7 D: P$ K' Y7 K }4 H8 {6 [7 |, x
}5 k- y5 b( _. Q- H
六.测试( a/ w9 m% V# \5 w( L8 X% R, T. y
$ V/ ^2 P; E: \- l) [' A我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& o( i6 [3 U) T; ]4 e9 i/ {7 L. m: e$ _
<?php
( R5 Z' V4 A3 j' p! j- O phpinfo(); / a9 k2 ?$ r- [! S& x* H& K( e
?># ^" G0 N6 \, Y ^' J6 X5 Y e- \
在浏览器中访问:" u% I% {% r% M, x M
9 s+ h _& W& [3 B0 P8 Xhttp://www.52os.net/phpinfo.php?id=1 正常显示。
4 K, E+ `+ x0 d' @+ lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 T& @ e* V- }3 w6 q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 b. X: z2 y5 E6 h, f2 D6 ]( K说明sql注入和xss已经被过滤了4 i1 h7 U1 ~. ^" x2 x' z2 t
, M, x! e- e4 P1 i2 i七、安装过程中排错; Y5 n( E5 @" b# v* p* V' ?
4 H/ Q0 ^5 O4 S1.缺少APXS会报错
# m4 V" J! K6 W) O( Y% O$ W1 _
; t9 E1 d, r$ U; T* Z9 iconfigure: looking for Apache module support via DSO through APXS, {2 K2 X( U0 F4 @) W) ^6 A( n
configure: error: couldn't find APXS2 \; `0 `- b# m+ j- B I
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* y7 |- B s, p% U) z/ g4 `5 r/ y- X
解决方法:
: {' h: k9 B% l/ O
: P7 _" q3 }& ~3 K4 hyum install httpd-devel
1 O7 a( e: |8 N; | ]& Q# q2.没有pcre+ ^6 O7 r3 h' d& S. z1 p
4 d# [% M1 j3 D+ b( p; s. n+ vconfigure: *** pcre library not found.
9 t3 q5 W$ C. e5 |- ^configure: error: pcre library is required
: ]' g& a H0 D( {7 o E. D$ p解决方法:+ \: f% N- E& L$ ]0 \6 W# u* h$ x* W
% c/ R/ A: P! g0 [) P! Lyum install pcre pcre-devel
) n1 _, o+ ]; ?$ b2 ] N, ~6 ^$ K- S3.没有libxml2 D! L8 u* o* x' x3 ]
' m5 y& W T# b. K: n
' r$ u; ^9 @) p) S- {
configure: *** xml library not found., K1 G& m( e8 S" S
configure: error: libxml2 is required8 T, o' h. r7 U1 M7 {% W
解决方法:6 N. M( ~7 n' X! F
& j3 p" f n1 v7 M/ G% |! }: L
yum install libxml2 libxml2-devel
' [9 I7 i$ z- ?; b) U4.执行 /opt/tengine/sbin/nginx -m 时有警告+ }* d( g' ^8 l, p* o/ H- U7 j
& ^- o: g2 P3 [* Q3 x! F/ v. zTengine version: Tengine/2.1.0 (nginx/1.6.2)5 d! ~8 g' r6 b6 Q& B
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
9 S: o) L) C0 B$ P原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 F2 ]; n/ R5 r. E. j2 F+ J R( U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured./ c# O2 h4 A: R5 y# Z. f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" f* d7 ?3 r* d8 ^3 N+ g7 K+ d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 `) H% ]9 {6 X' L" D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# C: Q! a% k7 n4 u: v" K1 |: G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"; l$ r$ J$ L+ \9 b% X
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ }, M/ u) H: s3 z
解决方法,移除低版本的APR (1.3.9)
' d; \2 }# A: Z; D; _( A! _7 x' ? N6 a
yum remove apr4 L. k9 \+ q6 C' S$ y3 {( ]5 Q D
5.Error.log中有: Audit log: Failed to lock global mutex6 ^9 A# t$ a* y1 |
; {) x2 o/ h5 `2 ~; l- R2 z$ G2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock " l- G0 b5 D! I. k
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 W8 u/ ^% U6 y# A
解决方法:
& @/ b+ P* A3 s( @- n8 w编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. ~ @+ O3 o/ P' Q F; G9 L
; g0 d* r) {6 ~8 J; Q! [) G
SecAuditLogDirMode 0777
% O" `6 c% b, R' c# PSecAuditLogFileMode 05503 p+ n2 H$ u9 o0 [# e) ~
SecAuditLogStorageDir /var/log/modsecurity
; {& i" S2 ~ N: a& ~; mSecAuditLogType Concurrent+ c' t( M( y5 V! j6 V2 B
参考文章:) w& K2 v% J" b
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: f5 e1 P: Q; @5 h* W$ p- Shttp://drops.wooyun.org/tips/2614 |
|