|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# p6 h$ T1 e! u( h% H2 E% R* s3 J) g2 L6 l/ A
一.准备工作, w5 i0 }; o+ H: G# }8 Y
8 C- H+ C {" D: t* l9 @# d
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) C# \( v B4 ?; o
9 B* K' G- K+ I5 T( ptengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- s& i. o+ r3 G( h% l
- B# G' I$ ~1 b% {% I, w3 H _modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ c! P2 s. |3 x8 f9 K2 E3 p \8 d F# k
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
; U0 V3 {) @- N( t9 U9 a# b, K) @- h3 i
2 v/ {. R, J6 g. o% e6 x& m依赖关系:
$ A% [' H' R/ z7 Z1 ptengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: c( i6 S5 N2 N4 V8 d) \
# ^4 t: a6 |2 Yyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 X0 Q8 t3 _! |" R$ p5 _
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! B6 X' L# M/ z" q5 k, a3 x j" @, F3 Y# m7 t$ i
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel9 @" i8 H6 a* \4 F4 a4 k
二.启用standalone模块并编译
# g' v2 _. h; n& c) `7 j% P' V A! S
下载modsecurity for nginx 解压,进入解压后目录执行:
* I% b2 P* d3 O. e& Z, _1 t) L0 ?6 ] T7 c
./autogen.sh
" T4 g0 B- U" n/ \6 n* i. c./configure --enable-standalone-module --disable-mlogc
' b, D& H2 t+ d' L& t' ~3 ~1 F0 Mmake
7 T- r4 _. g' Y) O2 G5 H三.nginx添加modsecurity模块
+ n9 z9 g& u5 e; B6 A# P! h4 |; k% ?) u
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* P( S6 W! J* p/ j# \+ y+ g# s& a! [3 f4 t1 L* r b
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine. ~3 c' \# Z+ w5 j
make && make install
6 H6 e% @& y4 p( W$ a" T9 F四.添加规则
" J% C% `* @/ h6 U F6 a
3 d" |9 R) F6 U7 \: umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% D$ `: H- @' R% d1 i4 T; k5 A
! z2 d/ ]7 X& x1.下载OWASP规则:" c! t( r$ M' U* e0 P( V
1 z1 N! F: @8 ~5 b+ Agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs; B7 Z0 u2 n" C+ o( W
) n A- }. S( W6 o+ X$ C8 Y' T
mv owasp-modsecurity-crs /opt/tengine/conf/
6 n1 k5 C% l8 d" u) z, U7 V% j/ h$ N) ^
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf" g$ k9 S5 ~! H! R- L# |
2.启用OWASP规则:% B2 h& t, a/ |3 J8 {; p( \& Y8 |
) K4 u m9 }. \8 a1 S$ G: R: X7 ]复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) H- ~* W- P4 [% j5 g9 T4 V# x/ _" }: v1 M
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
/ O2 n" k+ ?0 u8 s
6 s* @ h# O- N' m9 \9 Zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- _3 g! I+ e# Q
. ]! k4 V4 I. e) TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- O' _0 V7 V& M( \" O- t* ~) s
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, `% \$ a7 N9 K5 {1 Y9 T8 x* ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf/ w1 e, ^4 W8 S( ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
2 Q3 A6 M) B l. f' P: g3 W8 [Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& O9 K! l' c: K9 ^- C6 G. O
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- k7 _% A0 E1 e* q5 O* M. I* lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# B, j) l* C' k+ `五.配置nginx9 Z5 c7 o% y7 `7 D; D, _9 t* J
! K; c# \/ o" o; S在需要启用modsecurity的主机的location下面加入下面两行即可:! f1 z! T* ]: k; j. q( T0 s
. v& I+ D7 t5 q: C
ModSecurityEnabled on;
3 C9 k$ G& {5 F. {0 bModSecurityConfig modsecurity.conf;3 @: F/ b. v! u1 \
下面是两个示例配置,php虚拟主机:
0 N6 f1 {! i Z: H" M0 v& H
' H/ c$ g* G) \8 b& G* oserver {3 e1 \9 p7 `7 r \
listen 80;
9 x# I- h8 j+ | Q; L( j4 M server_name 52os.net www.52os.net;* F, r/ K/ e, m8 G
6 X4 L4 x" S. g' T* _ location ~ \.php$ {
8 ?# r) [) d# C ModSecurityEnabled on; 6 b" k: K: l- w8 w6 q+ U* `
ModSecurityConfig modsecurity.conf;) W7 |1 D( Q# Y1 \1 P9 f9 G
3 }3 z2 e4 j5 v4 @- D( k root /web/wordpress;' N. }" J: Y I3 s7 a+ F( q
index index.php index.html index.htm;/ @/ ^5 t2 T: C; J
" Q4 u, w' `! {7 I @- z$ d fastcgi_pass 127.0.0.1:9000;
# n' o$ D0 T) c2 X" G fastcgi_index index.php;
+ b1 R2 G( k. q fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' K( `% G" q2 T F% J include fastcgi_params;
7 D8 d- m, i: N, L. i8 Q }
- W! j6 P9 N+ Y3 j* j" q }/ T2 x; s3 { t" U' E3 Z A7 @
upstream负载均衡:! p% w+ V; I3 q
- v5 c& q- I/ ]! C( y( fupstream 52os.net {
$ p) _8 o* O% h7 C, ]5 Z. R server 192.168.1.100:8080;
1 ?5 n- z7 k; g( n server 192.168.1.101:8080 backup;# {5 |9 E& q5 I
}4 D: u- q- L" x4 B. ?, M: ^
# @1 I! \/ B, t5 O. Dserver {
6 R" Y0 ]7 j3 P: Q1 \listen 80;
. b+ C+ X: a0 [8 U* Zserver_name 52os.net www.52os.net;
; y) a1 s# @+ n( B2 w& l' b# P
, s. H8 }5 e& R% Plocation / {, b3 |/ @5 ?, {; W
ModSecurityEnabled on;
: l( Y" C- W2 F8 l5 f$ b& _ ModSecurityConfig modsecurity.conf;
. B P2 B8 h/ N& e7 k# R9 F( R/ d3 N1 B9 I1 c7 s/ w7 J. G
proxy_pass http://online;
2 z4 b9 x6 C; h) N+ C& W- \ proxy_redirect off;/ |8 l: {2 N+ y! d. {3 t
proxy_set_header Host $host;
# Z, S4 [, }. a1 ]$ g proxy_set_header X-Real-IP $remote_addr;
/ s# w/ |& F( B& U9 k1 r4 M proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 j, Q. K) u# b) X$ E/ j8 ` }
% J1 g3 Y/ Y3 F) a% ?4 E}& x/ b6 W5 Q5 ?8 B/ L9 S) \3 B! n
六.测试
$ a" R' w2 d% n H/ r6 V/ o8 \( f0 o' M0 c3 g/ f. W8 V- R
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 ]& E E3 S% W8 T6 u3 E. r* i5 F i2 H3 p
<?php
( ^; s$ B/ l4 o" S/ m: G phpinfo(); & ~# e& T+ W1 Q, c7 I3 x9 ~; g
?>
0 f5 [5 b3 a- ] J7 b* I. M( d在浏览器中访问:; N/ q6 {; g/ E4 a8 K) ]# c2 r
2 s) L' J# i! V4 B
http://www.52os.net/phpinfo.php?id=1 正常显示。# A$ t& Q6 S3 p+ s, T; X
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
# W( A7 b' }6 F/ Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 l% |: \( }3 Z4 h% x& [8 @说明sql注入和xss已经被过滤了
, M1 m' Y# S% }: E- d- C5 P; T: K: }+ F9 x0 z* z
七、安装过程中排错
3 f; f6 f9 x1 r% d+ b- [* l8 o
& @3 ~; o ]8 J1.缺少APXS会报错
% ` |- O3 n" _# p
1 X5 j1 Q9 X! A2 Econfigure: looking for Apache module support via DSO through APXS6 h5 e, F0 \3 L, B8 t
configure: error: couldn't find APXS
0 |% p7 ?+ S6 I. E) \apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
6 f$ ]* v8 s4 O; h) R+ L& s3 B解决方法:
% F9 O! z+ j* L8 A) d t8 r& r- e
) }, L/ T0 i2 Q! E# @5 Y# M- tyum install httpd-devel2 d4 @3 T$ k$ \% R5 k* d7 \+ ^
2.没有pcre+ c" _! @' U) U9 R
( t. G5 t8 C$ y6 A4 l
configure: *** pcre library not found./ I* N3 B- I/ @
configure: error: pcre library is required% u! G: z4 ?: k
解决方法:0 B4 b8 o9 v/ i
+ u8 @' I& C( h) D0 t/ Myum install pcre pcre-devel2 V% ^ d! N4 T$ l
3.没有libxml24 y7 Q7 Q: o0 C; `& m
% Z: }; e. J- J R: u4 V- ^
+ x# d$ d4 G0 w0 s, a' Jconfigure: *** xml library not found.1 _/ b; D( [" ^' p3 W! ^6 x
configure: error: libxml2 is required% d/ X4 ^4 ?* d; s2 z* W
解决方法:" m3 y. n; q" v9 S9 U! b! b
+ }0 K* _# s' j) b; }8 {$ {
yum install libxml2 libxml2-devel
$ e2 {/ }- l% n3 P; }8 |- B4.执行 /opt/tengine/sbin/nginx -m 时有警告) r. k6 R1 G0 H4 j
* }( v9 `- b' }# c8 a* I$ ~
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ L& I/ L7 C4 g8 S
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 N- a& I0 W$ z' y5 v) H% f1 Z& R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% {2 `, O6 _. E9 L( `4 M
+ `4 c' l- W' Q& k7 U2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.2 V9 l& U/ L" B- V2 M/ N8 |4 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
7 I% c- ~: I% [, U0 X2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!8 ]; G/ u& A! `2 E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 d' R. j+ m3 s) ?' `! Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
8 `+ ?. b; q# _" T' _6 l7 Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# m" j, B, s/ x* P9 q
解决方法,移除低版本的APR (1.3.9)3 ^: ]: |5 m; @
! V& S$ y3 q; L+ ~# a& \. g; ]
yum remove apr
6 n. G3 N9 G4 _3 r0 J: e5.Error.log中有: Audit log: Failed to lock global mutex/ G6 v( q. \& d& b9 |. U" f
1 O8 ~% e- G4 g# s+ G/ Q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock " K, D, @) Y# `! Z+ ^+ k# c
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 x$ g {+ T. ]3 v' f! \解决方法:4 Z9 X. b b6 d
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:, C% C9 r( J7 f7 h# T9 [
6 C2 ?3 f# w; U* C" f4 e! R7 G
SecAuditLogDirMode 0777
: r( k3 @' ]% p% v' \! bSecAuditLogFileMode 0550/ w3 K) q+ D. m/ E! N$ u- {6 n
SecAuditLogStorageDir /var/log/modsecurity
/ q, s& {, y4 c2 e4 v4 [SecAuditLogType Concurrent5 I& e1 v7 C y$ _1 g) b
参考文章:
$ I7 J* N8 E8 nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX" s* g+ y# W* @8 j1 m% m a
http://drops.wooyun.org/tips/2614 |
|