|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。( M# K( k: g1 _: b
% [2 V) u) l/ l; U+ {. W* Q' P+ ?
一.准备工作0 z4 m5 {8 { e- e6 ]: A
/ F9 y. V$ D J9 g; i系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0" \+ P' e5 O. n9 x! ^# i W5 m
% d4 P0 [! h6 D2 q- X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, N5 A5 C x; w" K' g* O
; ~- A4 ^+ x' D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: Y3 I2 q9 r& h$ A, u; E6 O5 j
$ z4 M' g6 l$ X1 j; SOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs3 h: v5 ~7 T$ ?3 e
9 E$ H; J3 K% @依赖关系:; X! v7 n# D( y% o: k- P H
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# g& i- z' B: X4 E5 ?" ?9 u
- F W$ P6 V$ i6 K' p6 A. T& P( Jyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel% O$ S0 k0 }* D2 D0 m6 s; x, P5 n
modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ y3 F- l( C$ W; ^8 v1 s' s5 b- X$ D# ]- K( v% O4 g4 H
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel6 X' w% q2 d( n1 r& f
二.启用standalone模块并编译& J1 o) J$ S6 T7 }! {; K
9 F; ^ X% O1 o8 v, T
下载modsecurity for nginx 解压,进入解压后目录执行:+ K- C) T9 ?+ V9 ~
/ u! q T9 J: I5 x! e
./autogen.sh7 U7 Y8 U: \# V ]
./configure --enable-standalone-module --disable-mlogc
: g5 }6 x+ E/ i* _make
5 Q) ]% K( ]2 `. A4 m三.nginx添加modsecurity模块& V* l5 Z. m- G
2 z, T1 m2 R) y# M7 \在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 c0 k" X, E8 I/ X
1 X% V! R( K$ Q0 l+ {) H./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine* n# q7 N- ], |" g0 H
make && make install
. T2 Z) g/ W5 l( c& \/ ~四.添加规则/ b' z+ I9 a8 u6 k+ L
- }. H) D1 c0 B6 m7 Rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! e1 z2 a; u5 f( w$ H8 [8 x' v7 G
. ~: e+ {3 k4 H7 ^5 z6 @1.下载OWASP规则:
( t1 `7 \* g% m$ P+ U- u9 V; N; G! N/ ^! L
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" B9 a) Z G+ `5 }+ b
9 c8 r3 o6 y, c+ D6 fmv owasp-modsecurity-crs /opt/tengine/conf/
! \2 r# K0 Q) ~. B
' t8 {' _: E( L( pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 V/ Z. n& J. k0 C) a4 K2.启用OWASP规则:3 r+ ^ H- N% ^/ c7 U
, L# d; l. v4 t5 H" A
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% l4 |2 ^9 M$ @5 d" A/ `
9 ^* |; A) C ]$ E) Q. H8 g6 s编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" D6 r. d+ {2 i3 B" g7 Z: J
% h+ r: k1 k5 i5 Xowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# g. C5 e, i$ h2 l' W
' J+ e4 Q2 r8 H9 f- c5 S. EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 w0 S- n: B$ f3 w% |) oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 j4 {# o2 v/ X6 eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
+ i& t% C7 m: f: o; pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
- i( i6 p( O& l- CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf$ H" Y9 W' [/ R2 H+ e
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# }" q( ~0 t. p% r+ U) e9 }! J$ K1 D
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 w) _5 @- w* s6 z u" F6 k- v
五.配置nginx
1 z# w: ?. ^, D, c/ ~2 f, L) D2 ?% H3 y7 h
在需要启用modsecurity的主机的location下面加入下面两行即可:& n0 X- J" b% f' a- `
; ?+ ?: r& h) W- Z4 w0 l( w5 d
ModSecurityEnabled on; 1 d4 j- r/ d* A9 i- D
ModSecurityConfig modsecurity.conf;; |7 }( w4 K0 s5 H5 c
下面是两个示例配置,php虚拟主机:
! c, N8 q6 L( i" I; l# P0 |
: I0 D; z4 B) W# d" f6 B$ |1 dserver {$ ~. N( }4 @/ W* ^% k# a: {+ @7 c
listen 80;
9 F9 V. J5 s0 t2 X1 T1 |9 u7 r server_name 52os.net www.52os.net;
( `2 C9 p" H6 M1 b- w ' w. k7 E! @7 `& \; ^8 B5 n
location ~ \.php$ {4 m# V/ K1 q! Y1 F# \+ `% ]: W' r
ModSecurityEnabled on;
( z9 P+ m9 @- f; [ ModSecurityConfig modsecurity.conf;
5 I; r4 \5 t0 o2 q; U3 u8 R, }* g" A
root /web/wordpress;
/ j+ s0 \" ~# P; _6 x- D- |. b index index.php index.html index.htm;
# r! w. C6 X3 V: p # s1 n* }5 U. W7 _ r% J
fastcgi_pass 127.0.0.1:9000;
# q- a+ J2 u, y fastcgi_index index.php;
/ _5 w/ j0 W. P8 p9 A0 E- ?" a fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
, [2 {9 A! {' d/ ]- i& K4 @ include fastcgi_params;
+ k* {5 A8 V) k, S5 z/ y }
6 R" b8 B/ M; F$ E7 Q/ G- Z6 N }5 L4 b9 Z% F: I9 }$ P
upstream负载均衡:
+ B; U$ S2 _7 l3 d# R
! i) E. W5 n8 M* X# d8 i" Xupstream 52os.net {; N; i/ n' }2 _1 B1 ]# t
server 192.168.1.100:8080;
7 a; D6 k8 `! T7 {3 [ server 192.168.1.101:8080 backup;
2 S) G, c' T* T- i}
% B% n. W% g+ }# f+ d3 F/ Z% K! Y9 ?
server {
6 w1 S( z3 ~3 X1 Y+ jlisten 80;
5 u8 A$ X2 [" L, q4 n$ |5 aserver_name 52os.net www.52os.net;* S' B: F/ \: @4 M/ ^. u) z' `. J
4 _0 j2 Z% l7 g4 x' z. U0 N g- ulocation / {: L1 H' F/ A* K$ Z
ModSecurityEnabled on;
; K; T2 q% M6 b# X6 Y ModSecurityConfig modsecurity.conf;
. ^& P& w, v- b* m& {, U1 E1 b- G; R5 u# y% b: K" ?+ I
proxy_pass http://online;
' Z# P) b0 q$ H" K$ w* W9 V proxy_redirect off;
3 K" S( o8 {* h proxy_set_header Host $host;
$ K) ~! n$ [) Z proxy_set_header X-Real-IP $remote_addr;
. s" [, I0 e4 D) G4 d9 U, y) O proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;7 q+ f( ^: r* t
}, I8 f/ T6 X8 ^! T
}: O& U; R* a1 E$ r9 z% x+ Z1 E
六.测试
) a3 U% D1 G" h& s/ }5 }! |+ ~6 \4 O9 t
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; j& O1 Z) a6 C/ R' `- O
6 L# V0 Z% }$ g9 o7 R. I) K) d' }1 W5 I
<?php
5 L, ]8 u, s( D4 K phpinfo();
) G) ~" P7 Z& B6 m/ N$ e?>0 Z& h3 Y5 \5 A4 _& ]5 D# c$ ]4 t
在浏览器中访问:
; w3 l) i) ~9 T! K; H# P- ^) ^0 o+ k7 a5 _* p3 f
http://www.52os.net/phpinfo.php?id=1 正常显示。
- K' [. d0 V9 q: j" Yhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
, _" {$ |1 J8 Y/ t2 i! Z6 vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
6 d# B% u7 E) a# v+ h9 }' C. S说明sql注入和xss已经被过滤了
/ I, F! ]2 J4 o% k+ Q% _5 \
9 ^$ Y* E0 b5 {! h$ E3 q6 k* k- R7 Y" N$ u七、安装过程中排错) R( [2 H& C4 K, Y0 F, d' Y8 ~4 ?1 I
2 k; [# {6 `/ {" }* |0 h! P
1.缺少APXS会报错
3 z' ~7 @# M4 E I# W1 ?* i# ?+ X4 P; U* J! p) O( e$ \
configure: looking for Apache module support via DSO through APXS
* j2 P, B4 N, z. g: o |' Q3 P' Pconfigure: error: couldn't find APXS& n1 l) m! T; q/ G: r
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) s* W$ l7 j L% O' R* h& e: v
解决方法:* q, K- i4 D, y! }/ p5 Q5 V
3 K; u, x& o* E0 W$ ^0 [% v) L7 A2 Qyum install httpd-devel& d: `7 u! E1 b t
2.没有pcre8 i" N' ?; `5 z
9 E3 s( G( d( `$ t: t. |. Q$ Fconfigure: *** pcre library not found." I! K5 d- Q; Z8 z0 H
configure: error: pcre library is required
7 K3 Y o/ S' s4 e, D解决方法:
1 _7 Y6 T- e) Y& ? C, \8 t; \: A. D7 k
yum install pcre pcre-devel
+ g$ i" P2 z( j- w- Y; X9 z3.没有libxml2 U4 j% [1 y# p- Q. c8 B0 p
4 q) r" b/ o' \0 C
r& X: Z6 {5 R( j5 ^3 L0 ?$ n
configure: *** xml library not found.6 Z9 T6 a5 `3 v7 q' W, a7 @7 B
configure: error: libxml2 is required
. M. ?" ~! a4 G解决方法:
) v. |$ W6 _. G$ b2 `" S! k2 S- B. k3 F3 F
yum install libxml2 libxml2-devel; k8 f! b0 Y, J( P
4.执行 /opt/tengine/sbin/nginx -m 时有警告" L: F6 t4 p+ P" J# Y, E
+ G* I* Y) t; KTengine version: Tengine/2.1.0 (nginx/1.6.2), p" _3 f1 m) S' ^% K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 ? o$ u$ Y9 S% {9 U5 h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 f! Q0 @0 Z2 ]! t. k+ [
8 m' I( x; @$ c" H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.- X) `- ^, {+ F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
6 i5 k* L d6 H+ c r2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- z) i" m& u; _* b" P+ g" F, U% Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. H- O: Z# @& ^* M. h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ n6 y9 ?# W8 R6 Y3 a1 |# H2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( Z5 G9 K' E: ~9 B2 T6 E* U/ s8 N解决方法,移除低版本的APR (1.3.9)* e% ]+ @' _. B% d. |
* K( k, ^' _5 }9 E- Eyum remove apr0 z2 N' l( ?3 }7 ^
5.Error.log中有: Audit log: Failed to lock global mutex
6 D# I7 {" M% M2 J! h) h: n% E# ^, x4 h
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
3 B4 ^3 a1 A- C0 \4 g0 Xglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& |* o& B) C; H: O* _1 b# s, ]' U
解决方法:4 w0 N: w# I# x; r. l, f1 E" ?
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. Z4 q( v7 \7 q$ i, A4 w7 n
( [7 ~. T5 `1 U/ d$ Z! G
SecAuditLogDirMode 07779 [0 B. \& v5 n H9 B0 F% u$ {2 r, Y
SecAuditLogFileMode 0550+ V6 O8 G1 C* D) _# J
SecAuditLogStorageDir /var/log/modsecurity0 ?! c1 w0 e m
SecAuditLogType Concurrent
6 n6 `/ z: L: z! Q. R参考文章:
4 K' R& N6 l+ b {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 c, x! j8 v4 _) L$ q; ?: m
http://drops.wooyun.org/tips/2614 |
|