|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 I2 f N b4 h6 e! R; O2 K0 u- B
" y6 A/ U7 Y0 s5 m% B k( a3 J
一.准备工作* a6 E P& S7 H- h4 @7 u8 H
8 ? G) t- l4 W* L( b系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ b5 d& X: a( `6 V. M
1 Q' y* E: L5 c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz( y7 {. s5 [1 f! w Z* d" W3 h" l7 Z6 P
) h$ w: x( a+ R! Z# F" `3 n
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
, K6 A1 ~) @9 t, H5 D: Z& |( F" e1 z/ W
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 u5 y! F$ J/ v" M% @+ j/ p8 R$ l7 A6 k; s- f4 q
依赖关系:! J. J% s& q( q8 k+ i8 K" S" ~; r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 U% ?7 Y0 k3 i% _
$ p, e3 K5 Q; D0 c7 F; d! e
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
( r: b0 d5 E" ]& h4 u8 Pmodsecurty依赖的包:pcre httpd-devel libxml2 apr
$ F4 j \8 r# k$ m9 Z3 [) C( t: s" e) [% V- b! y
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 |; A$ `6 k v
二.启用standalone模块并编译
+ S6 \/ B0 p* F" ?+ V c, y7 O! G! ~' {; d! e$ V
下载modsecurity for nginx 解压,进入解压后目录执行:
$ a& ?9 S8 Q h* y' R* `7 J H# s
./autogen.sh$ z5 T, l% ?! D" z( F
./configure --enable-standalone-module --disable-mlogc
' D/ Z+ T) {6 P1 {- umake
! O7 V: b3 O' N1 W1 W5 D& C- `三.nginx添加modsecurity模块' ]- \% b/ J. i; W2 Z9 A+ r3 z
5 D/ K& C" T! O- M$ L' y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:2 q& {. r5 U% C& U1 i/ |4 ?
4 Z* k1 L0 [" O* d$ G
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
7 ~( ?/ _+ i" M7 ^% r8 qmake && make install+ a( q' u/ ]5 N. t# }
四.添加规则
7 A# e# ^6 @: q0 n1 f
; I/ R4 h" e4 D! X C+ omodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 d( w* R" _, Z9 k" x
9 h5 }1 h% m4 i. I0 {+ | O# [1.下载OWASP规则:$ N: S% Y1 k2 \, m5 F: }$ \3 y
! N" F- `2 _7 g# [
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
2 {. m4 a6 ]& Q$ Y5 y' G5 S+ ]6 O$ |6 y' V7 g- S- l0 Y+ N$ ~# p. ]
mv owasp-modsecurity-crs /opt/tengine/conf/5 ?5 j; H! X% G" I. ]4 O9 l
. _: k5 {4 o- x7 |cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
) O0 P* f6 I% M H: M+ W% X2.启用OWASP规则:
) ]' q: v0 j# ^5 U# i; @% o* \" s4 T( v! W2 I. g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ V6 I+ I0 l8 U) J D8 Y0 [
! a/ ] O2 ?# ~ Z编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ v! B% }) U4 a m1 n
/ x' I0 U" |! y' u. |5 X' ? kowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 I; ]4 l1 u) q; f4 y0 W! ^: Q$ ^+ k9 V* F% M: J- r3 L3 a' K, y
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf7 L8 X7 u) K5 k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
C9 C4 m9 i- k2 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
9 l, y6 O5 t u f+ {3 P5 ^) f* hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
" g9 u4 [7 v$ {$ _. X& SInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* [3 e: b' k7 {- N6 m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. _; {% V8 N- t% W% z! QInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. B6 c- {1 p { ^
五.配置nginx
# b" k2 {8 f' C8 q" \2 ]" G$ X# Z+ B! l) G0 V* Y
在需要启用modsecurity的主机的location下面加入下面两行即可:+ C; j( F. L+ i1 f! Z& A. {8 [0 I
+ }; L2 y3 D9 d2 i' |% YModSecurityEnabled on; % z$ k+ H1 g" C- o( I c
ModSecurityConfig modsecurity.conf;
# z9 Y/ h: t. U0 z3 ?6 Y: ?- ~下面是两个示例配置,php虚拟主机:
# c% t0 k# A" p3 S7 X( `7 i7 K7 V: `" J9 \9 t( ?0 {6 u8 K
server {
4 q `4 a$ B" G( \6 t# F1 n listen 80;( [, s" j' P; c& j/ Z& L3 R' j
server_name 52os.net www.52os.net;
& X1 W" z" J9 e8 s9 T; o9 l8 K- k ) X. Q$ `! X3 S" B5 \' K
location ~ \.php$ {
6 {1 ~: ^! y" Y! g) Y ModSecurityEnabled on;
/ Q. h V* w; m7 o% x) T/ e1 ^) J ModSecurityConfig modsecurity.conf;
/ `) f# Z* c2 a# C y; y3 Z
V: \5 |* t& A4 g root /web/wordpress;( @: N) g- |, A. w2 s5 _7 ]3 `
index index.php index.html index.htm;- [7 k& c3 G: N8 x
. u9 E- T, m- j- q) O6 W2 }% k
fastcgi_pass 127.0.0.1:9000;
% l. o* k% [" K/ z: N0 w/ V fastcgi_index index.php;
+ n! f* y6 k4 K2 ?' T5 g/ Z( m9 K fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
* F% m# S, \$ {' w$ R include fastcgi_params;
2 P5 P1 ?; [5 S# i6 `1 N" \ A }
0 c/ s" w1 c a/ g }
1 U: G2 y- p& H( Z$ }/ S/ j: }* tupstream负载均衡:
! ]" H0 m7 }: L7 @% ~( N
1 B9 b0 h+ y; hupstream 52os.net {
& K* S3 K% }1 l) P( E2 g1 V" R server 192.168.1.100:8080;" { m: P0 {5 c7 D
server 192.168.1.101:8080 backup;. i; e; g% H0 [7 ?+ h* `
}
4 [( s- H3 w' m$ z6 c
5 k6 d" |) H- W9 v1 u0 Wserver {3 n, d: O; E* H" s$ m9 a: n
listen 80;2 c% I) y2 M3 u, M) r
server_name 52os.net www.52os.net;
; L+ q9 p$ n0 T2 t) q, @- ?+ f" z: P# w, C$ X* E
location / {. B( k9 `% S f7 W2 v1 C
ModSecurityEnabled on; : H0 v9 @- E4 _( P8 G# g, \ d+ a
ModSecurityConfig modsecurity.conf; ) L {4 t) Y- ^- L2 X9 F8 C
, E9 R' W3 ~0 h* J9 a9 T
proxy_pass http://online;
0 `/ ^, u- k6 p; D3 t& W9 X proxy_redirect off;
; ]( w. { ?4 f9 n3 c ] proxy_set_header Host $host;' S. {: A3 [4 t# x
proxy_set_header X-Real-IP $remote_addr;
1 G# y4 b' n; N; P3 f( S; P proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;& D8 `4 A- D! }9 z
}
) V- t/ I/ ], o! H}3 n6 k- R3 W5 h! i9 w
六.测试5 C; x/ i4 I- K; p4 h/ X
# `0 T, S1 y K
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
1 {4 p& h# B2 l9 ^% A" ?3 J0 J W6 a: j d
<?php
V* w4 t% L" v3 v6 K2 F phpinfo(); : s' W2 [2 v% {( A9 x5 N( Q. s- J
?>
0 A* d7 C3 O. g在浏览器中访问:
& m% F2 u+ G7 v
% [/ s7 R2 |: B v, c, Mhttp://www.52os.net/phpinfo.php?id=1 正常显示。; W- N" K5 f8 R0 q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' g3 C7 C! p: j7 U, |5 I4 B, H) o
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。 _3 A: K" X+ n: s/ `
说明sql注入和xss已经被过滤了; \2 w! Y# V7 T. g" z6 @' m" a
" }# m; W- B( r+ Y( N七、安装过程中排错
5 I p+ N% P0 C* f, o" A) }% K* K) }5 q( H1 S/ }4 D. F
1.缺少APXS会报错
Z- r0 g5 @! h) @/ Y" b1 A
3 F8 u4 | |1 Xconfigure: looking for Apache module support via DSO through APXS
* w4 ?2 z: z9 b7 T; r, M0 t2 rconfigure: error: couldn't find APXS
; L+ k7 Z, N! t w8 T. w# ?) x2 H t. Kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& V8 O0 a: Z; D9 D7 ?
解决方法:- R! H6 O$ g* |9 C% a4 Y; H
- i. p8 c2 d! }5 p! dyum install httpd-devel
: `7 O5 i3 l! ]. ^# B2.没有pcre
% l+ o. I3 X8 U# }% M4 ?4 F l; I
configure: *** pcre library not found.
+ F- X* ^5 l5 @& ^7 dconfigure: error: pcre library is required- C7 L" n0 m+ Y% l) W! b" }% h
解决方法:
& _: C2 x' E: W, D, w6 B# e5 v
8 F' q+ B# c2 [1 ? Wyum install pcre pcre-devel1 L! N6 k' ?9 U, ]
3.没有libxml2
! ^( j1 v6 n0 m: h1 T$ A
( Q6 t) S( y' V: y# V% T' U
) E" q* J0 m1 {configure: *** xml library not found.. a" p! k2 b$ {3 D* M0 T, N6 C8 x: K
configure: error: libxml2 is required
/ G+ k: X" P( {解决方法:4 A m* d- L+ N# `' i, R( k
2 h; f8 c8 E) H- H! m# N. N) lyum install libxml2 libxml2-devel
. e( H! H# q+ [6 c; r4.执行 /opt/tengine/sbin/nginx -m 时有警告
& s- [$ U D1 K F% w% A2 _# _
& N1 j3 M% F2 i8 o W9 b3 DTengine version: Tengine/2.1.0 (nginx/1.6.2)
2 f4 _8 j6 }: ~" }5 \/ _nginx: [warn] ModSecurity: Loaded APR do not match with compiled!) A! @+ W2 {9 h; N: J2 @
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
( V [+ _5 v+ h+ O. W& o7 D, R# X( X) u, H$ h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 t+ v8 D8 h: H0 T4 l/ Z) w& R9 i9 i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ D2 l: M8 N3 Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# R6 d: V3 i. I( h/ G8 n$ s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 H5 s( a i+ n( J
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"1 r4 S* u+ M0 v; @( p% x
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
' `( W7 L# ~2 w$ E% |解决方法,移除低版本的APR (1.3.9)5 s* b- u2 b2 g' W
" x1 D9 |- q$ f. A, I9 K1 ^0 T# u
yum remove apr# C3 j* A, O4 B1 C2 L" R7 I* W
5.Error.log中有: Audit log: Failed to lock global mutex
$ v* q6 o" G! @3 v/ q6 ^" u1 L- Q( o* [
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; g2 H6 R, @6 Z7 q& I
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
+ u6 u5 L. W' Y% ?% g解决方法:8 X$ B& o% s' l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
$ o# }' v4 E$ s- s5 a7 ^3 |5 i6 L2 F, P
SecAuditLogDirMode 0777
5 k8 q0 e% b& |# j& cSecAuditLogFileMode 0550
# O6 Y& o( Y4 |! ?, j* P% RSecAuditLogStorageDir /var/log/modsecurity
" t" p, _* E& H6 ^! _6 MSecAuditLogType Concurrent# Y1 n5 \" O Y" v' {- O
参考文章:
9 v4 D( N' Q0 khttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 h3 C3 w$ @/ Dhttp://drops.wooyun.org/tips/2614 |
|