|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
8 S0 s% D; n% y
/ b+ p% y4 b* r7 Y; Z( t一.准备工作9 [+ O& W. @5 t9 m
@* E( g! Y. [+ q1 K5 N
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( W# x, v5 o+ j! B$ i2 W) S
. C; ]; l" `; R9 g& D$ Ktengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 m2 u- Z8 \/ r
% z5 c% u! x, K+ F" e: q$ [1 w& \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 Y) s% Q" A( U2 x
4 k5 f0 }! m( P& eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 c$ x6 e5 v# x! B7 @9 f
' S6 ^; F$ | A: Y9 i: y依赖关系:
# w0 _6 p3 p, o8 Atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* b* t0 D) X0 k% p% O) M2 e1 s9 C* m: X1 E" g" \
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 }3 l$ y. w9 r% D+ c+ {modsecurty依赖的包:pcre httpd-devel libxml2 apr0 _$ `" T- J8 H$ N
) ^! o$ b, A# a
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
# h$ d; W- W$ I6 Q$ c二.启用standalone模块并编译1 W6 ^) s, H! M3 `
! e% s) ]0 L+ C/ E4 j9 O7 x
下载modsecurity for nginx 解压,进入解压后目录执行:
v- e t5 Q5 ~- r7 F1 j+ G) d3 j3 M0 k: K( L2 L5 A
./autogen.sh4 y$ k( s+ ]% n" h/ x5 d
./configure --enable-standalone-module --disable-mlogc# _6 x: M& r- ~4 h' W: z$ X- K
make
0 O( Q* D4 l, e5 r# A# Z三.nginx添加modsecurity模块
3 w/ P) a9 L/ g/ u- A5 |- Q! E8 ?9 m$ R- y3 ^+ H
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 U/ z: w# A8 a7 n {0 L
7 D! Z; x |: w8 F
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# S# Z- j6 u+ N5 ]
make && make install
6 B0 U3 O. J4 v1 ~ ~- J四.添加规则
5 b! a u6 Z6 X( j' X
; o& e3 W. }* d# Q2 b% i0 _modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, Z8 K' t/ _/ u# Q) X6 ^# @
$ w3 q O2 v$ }) `1 v% V* A
1.下载OWASP规则:/ \; u. v8 q7 n: k) ?
& n' l9 Y! r Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# J, y4 R$ m- N. y7 o1 z g) A, p0 R% x
mv owasp-modsecurity-crs /opt/tengine/conf/" M) _) t: e1 s; f2 B
( x& @9 f. W, x! A# L5 E# Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* }* D. X6 o0 n: z. f
2.启用OWASP规则:& ^+ q' h( r8 P
* n& \7 b3 P. E2 e- G1 e. L2 v
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 k- c6 @ R, H [+ R( t: n, }0 T, c" A+ c8 d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
4 \) e8 t' D& c3 e1 r: Z; D( g; e5 i$ _0 @ J5 N6 d$ D
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。% O( V3 \& i! Y# \" ]+ V5 C
8 q l, b1 G/ r& k- u: SInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
1 p+ ^$ H# u% q1 w% UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf- I$ S; p' Y6 M9 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
~# C6 M" s3 b( C9 f. GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf# j z1 A- x& |8 B: I
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
( I- E+ _7 G7 w" kInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
+ D. B$ e! e) qInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf( F* e; Z7 W2 N4 p. k' r I
五.配置nginx! x% o0 O8 \/ s( `9 D: \, v8 |
* \' `' ^8 \& V在需要启用modsecurity的主机的location下面加入下面两行即可:' \! e% W4 C8 x# k, ~
+ L1 l( @5 p- |8 J6 O, kModSecurityEnabled on;
2 f; T2 P$ v4 n% K! u5 N* ~ModSecurityConfig modsecurity.conf;( _% E3 t/ Z1 o9 E: I
下面是两个示例配置,php虚拟主机:( }5 l0 ]6 ?4 h9 u4 E7 \3 k; e2 s
+ q& k- V* d$ f9 t; S6 c
server {
: S; v2 V0 f# h- T2 S& w listen 80;3 g5 N8 m4 d3 m$ ?, y& U3 Z
server_name 52os.net www.52os.net;# Y) @) E, H8 ^4 I
" m; f' _( E8 L) p5 l location ~ \.php$ {/ P0 L6 M. Y1 d! B
ModSecurityEnabled on; \8 R8 ^0 j/ _
ModSecurityConfig modsecurity.conf;3 P/ J: l& _, H) ?9 Y1 @$ h9 A4 m
6 g5 l+ S! \% s% q4 | root /web/wordpress;3 ]% U- [: Q& ~1 b, Z
index index.php index.html index.htm;/ O5 O4 v+ P4 h g
* B, a9 k# `2 u' y S fastcgi_pass 127.0.0.1:9000;
- X! V; o* ^% G! V7 t$ o3 N fastcgi_index index.php;
& O' K! }" N' A- d9 T fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;7 I! J+ [2 b7 `6 n7 ]5 Y, x
include fastcgi_params;
+ d1 E6 ~8 r- ?+ r o$ k }" ^; l( J0 x/ {, B1 V
}) e% D' W' L c& [. F6 k6 {9 Y
upstream负载均衡:7 v( r7 S7 r5 s2 e/ ]& m
h: g+ h/ @/ eupstream 52os.net {6 b" p' h2 I1 w. B" E8 s! K
server 192.168.1.100:8080;
/ m5 l0 X% m9 b& s0 d1 r- X server 192.168.1.101:8080 backup;. g2 Z6 w5 e6 q0 c* Z9 H3 @, y1 i
}
1 T0 k! {+ e/ f3 u2 E: ?6 _8 N# O- f9 R+ t: O& _
server {
# [! \8 x/ \ a7 Ilisten 80;. i) j# A: u+ l) w, p! A. }
server_name 52os.net www.52os.net;
; ~( ?- z0 J, z4 g$ a( t& u# h: s6 I! M. ?/ t- L! s' w8 G( ]
location / {0 m# L: X+ S( O A
ModSecurityEnabled on;
1 C9 B) N" [7 v) h8 p t ModSecurityConfig modsecurity.conf;
! T( |+ t' C* \' E6 `# A" P( R+ n. ?" P' I
proxy_pass http://online;
5 y& C* U1 {2 O( T( e* c9 y proxy_redirect off;( Y* {6 ~( Q& K' @. Y
proxy_set_header Host $host;
1 I, J/ I# Z5 {* x) c6 } proxy_set_header X-Real-IP $remote_addr;
, j# W5 g. R2 [% z" x- b! s3 i proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
& ?& k: C( c) J @1 X }$ h, m8 w$ q2 H, R" O+ w \+ Z* G* U
}
, X8 O C) t; j# I. @& [# \六.测试; U1 [: z# e# A" s7 `$ o
! T5 _, [) m, M$ T9 f: N我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 P/ ]- ]2 O& I `5 U' H
& S4 c' [1 Y8 R
<?php! T( l3 x' o" s: r3 n) e) [
phpinfo(); m9 o6 D: c# K( w {
?>
7 o, u# w' D+ ~" b( J在浏览器中访问:
! J5 C4 @" x+ P, G# i
7 \% |, `- J4 S0 C3 {http://www.52os.net/phpinfo.php?id=1 正常显示。
* Y; t+ j3 e+ X! C( O; |3 k7 Xhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
) k% p) V+ A* r: w; Thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 G5 ]% f7 F- m, ]/ ^说明sql注入和xss已经被过滤了+ |+ ?0 k( ?0 ]
- B' l% B, J6 x$ u. x# X: }
七、安装过程中排错
! b, R# u$ u9 N5 z' _5 S
) n% q& L# w2 R1.缺少APXS会报错
( K" _% C3 @) U' w7 b, F' q- |8 L8 D- s/ L
configure: looking for Apache module support via DSO through APXS
/ t. F9 x% \3 T rconfigure: error: couldn't find APXS6 a, {1 U, d6 m
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% b4 h8 }2 P j) d0 z解决方法:
. G" l$ R T4 y, ^8 Y d- k2 |9 Y
yum install httpd-devel
2 e5 u5 B) D- m) r- F" K+ V! b2.没有pcre4 S1 s" n* X: w
/ V2 j0 W d1 |! h. i. D7 `+ Mconfigure: *** pcre library not found.
4 B- ?& R# B2 ^8 \5 [configure: error: pcre library is required
) V( x# j* C: Y/ w8 l |$ d9 F2 G; \* T解决方法:
" E# Z* \4 K4 x# z* ]$ |4 D( a( x7 G5 T6 q5 g
yum install pcre pcre-devel4 \2 c- d1 z# h s5 A# g
3.没有libxml26 J9 E2 H" @+ o& X8 N8 }1 o1 E
5 r( [) ]& h K, l7 }! ]5 R
$ b+ I8 S3 [1 f2 t8 K& Y5 I
configure: *** xml library not found.. @4 x( [; y& P- v
configure: error: libxml2 is required
" z- @+ X2 R& e4 \/ b3 k' o解决方法:
" ^6 R+ o/ `$ v9 K5 T6 A( J% G( w8 V7 N5 l5 j! w+ }3 ^: Q$ X4 i* z
yum install libxml2 libxml2-devel8 Z: o; v: E/ ?' x
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" z9 T e% c$ f! k/ f; M9 L
. g* B# Z+ H! M, |% pTengine version: Tengine/2.1.0 (nginx/1.6.2)
/ E8 P$ v( A5 I8 ^5 ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( s, {9 _+ q2 K0 g! l原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" }! F% n c! U
7 T0 ` t, Z! ]6 g: ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.% s& ]! I$ r4 e, ` |' t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"+ ]1 c. c9 [& L3 S. l8 t2 ]
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* ~( O u8 O' g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# f4 s7 ?1 g4 y( M4 [% W/ z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 H/ @/ ^# y* N4 q0 @2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.: |* @7 ^7 m* h6 q6 I6 U5 A
解决方法,移除低版本的APR (1.3.9)
- R5 L0 ?; T5 {7 N/ k& B* S8 w$ I; y9 D9 z9 K5 B
yum remove apr) Z9 h4 z/ Q* J) Q2 R* A" j8 t" j
5.Error.log中有: Audit log: Failed to lock global mutex
, V" \; o& D6 N1 P3 e$ w
$ J+ L* P, }# X3 [' f* Z$ m& X% H2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ' p5 [. T* z, L% K5 g
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ c) e/ Z2 e( ]# m# m3 v
解决方法:
. D! U* c! |2 C" f4 F# K# y' `编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ Q2 ~3 K2 i( B0 v2 ]2 y9 A- N G0 a
) I( @/ Q# E V0 VSecAuditLogDirMode 07777 y: T0 ~+ [" _# G/ h6 [' K, H/ b+ D
SecAuditLogFileMode 0550! Y0 m. a1 |* [% W- C9 C
SecAuditLogStorageDir /var/log/modsecurity( l: \ @: r H- |1 n4 z. N
SecAuditLogType Concurrent; u2 F$ l: f; ^8 M' P4 L0 I
参考文章:7 S1 W( m1 e2 g5 y5 u
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ M+ u! o8 O: S a' @0 Jhttp://drops.wooyun.org/tips/2614 |
|