|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
& m0 ^, ~& L$ \5 x9 ]% k
6 i. j' m f# a7 i1 M! n一.准备工作
W+ y" {9 c' i5 D- K5 ] h6 i' _6 g: u
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
7 ]( D* u* a% o$ ~. P$ Y' z
4 b6 s/ L) u+ x3 `tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 E0 d9 l- o+ t- f" t: g' R6 S7 a- \8 n$ c) _5 u: d1 v1 z
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' H& E5 }) z- @/ k0 m6 N
; \1 m0 y$ C, Q# bOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; `; v$ y# P9 L
" M, m+ v# L# o# J2 L, e+ l依赖关系:# a+ V3 r6 s6 |$ W( y7 Y' ~4 S1 ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:% ?% Q- E/ \% f$ a- }) l3 K
" t* |7 b) J& a% kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel; [! i9 o# x( m
modsecurty依赖的包:pcre httpd-devel libxml2 apr
+ c6 i5 r, i+ \+ T" p9 T3 V6 o" _
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' l" o2 ~2 K1 G& `7 @8 ]1 @二.启用standalone模块并编译! P* h) d' f) T* U! }
: T3 z0 z' u9 c0 Z8 x; G
下载modsecurity for nginx 解压,进入解压后目录执行:
0 @$ H7 X7 t6 }
( e# B" A% \1 B& O$ X./autogen.sh; a/ B! [. t# \/ o* e
./configure --enable-standalone-module --disable-mlogc
' X! C! `3 x+ K$ R7 ~2 n9 R0 g7 t8 k- zmake 6 D5 b9 E7 i A( X, j* F
三.nginx添加modsecurity模块# r: t5 g' m3 _- w, H* |
, N, o$ B3 y$ X( k5 y在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 r, s/ c9 s# y5 }# x& G2 E" w
! Q" Y& R' b. D( y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine0 N- q. {/ l P* {. E! u
make && make install
0 e8 X5 S" `6 M9 x5 _四.添加规则9 o6 N) S- b% Z' L; t
4 Q- O' \/ d) {
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 ]1 T k, m8 I2 P
- G9 `1 M- o, r) g; ^* T: D1.下载OWASP规则:# ~+ N" A b' O$ a
3 a {, v' g$ ^8 \git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 T s( ~' x6 d0 L! B$ @. g9 F% d. q( K2 X/ N, `6 t- D
mv owasp-modsecurity-crs /opt/tengine/conf/# _# X! Z) F: |; b# i
2 N- @: F: T/ ~% q' \* }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 z3 \3 Q# W( t2.启用OWASP规则:' |! h% S% t: ?( r
" d' Q) v# O0 [' `6 i复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。/ Y9 g8 G8 i" C3 O1 L
8 ]& ?2 f+ v; ?9 [; z5 {* R4 I; E
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on9 Y) `1 C% o: F4 E+ X& k
1 x+ ]5 i- X8 B# i$ ?owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% q* i$ U7 \8 k$ c
~/ i8 ~8 W( j# C& I/ ZInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- c1 z* h1 \7 iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% O# @, k6 C" E% I0 S. s$ UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf" _! x$ \5 {$ o" V9 q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
1 q7 S8 ~2 o p3 b2 g6 O% EInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* z3 }' t4 r7 ^. H* |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf0 S2 b& p O2 p" x
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 R* S X, N- v2 j
五.配置nginx
, E# A1 ^' y( |7 w$ `3 V7 [% P5 B T$ S/ B* H" C
在需要启用modsecurity的主机的location下面加入下面两行即可:% d9 Q5 Z8 P2 N6 R$ |# ]
( C; ~+ \. i. `ModSecurityEnabled on;
5 V! s, y+ N9 H. D9 T) S& QModSecurityConfig modsecurity.conf;. ?( p: s5 u: ?6 z" }- A
下面是两个示例配置,php虚拟主机:8 V* h, V- b1 K1 g C; A
& i0 w, o5 x2 {8 R+ [0 l
server {
9 P# h8 A- Q8 Y# `. n' p* X listen 80;
- k M" [" I6 e/ \, h server_name 52os.net www.52os.net;/ _# g* a6 l* x% @
% m1 F' H# E& R) p' k; B4 b( U
location ~ \.php$ {
5 s7 c! M; p0 u- m; r ModSecurityEnabled on;
9 |& u6 `, ?8 S$ D x/ F2 u ModSecurityConfig modsecurity.conf;
! [1 O5 L! W- P5 w: Q
$ \) h- H% y8 j) d( F: P root /web/wordpress;& N& y3 L O: Z7 D( ]$ U
index index.php index.html index.htm;/ {, z" S8 }" h
% `# g9 J: \5 J1 e6 N, W u fastcgi_pass 127.0.0.1:9000;. v! r% v& Y( R8 |4 q
fastcgi_index index.php;% r# J$ z! K3 R E# B, O2 Z
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, J8 f. ]5 y% z" @% k
include fastcgi_params;1 J; c6 v; u2 q
}
# Z8 ` W$ n# {, n2 V" ` } B8 X. A" D) i: W6 c
upstream负载均衡:' f& |; L2 c6 H( t
4 X( \( Z; N+ j) ?upstream 52os.net {* J: |$ F1 ^* y
server 192.168.1.100:8080;
$ ?1 X/ `: k, B& F" x server 192.168.1.101:8080 backup;
. ]; k5 f) {, n- O}+ L9 D& _# H5 l+ O& ^; M9 Q
1 k Z! q. u4 A- V( ]- ?
server {# `2 z0 a- r& g$ h
listen 80;
9 v% E- T; {: i/ O1 d- {" ]server_name 52os.net www.52os.net;
7 I! s$ t; O2 C+ g$ E. V3 F- y6 k% p1 O4 g3 C/ g
location / {8 \4 Q. }" H$ q& h) [
ModSecurityEnabled on; ) k7 M4 d2 g1 i8 q. i9 \
ModSecurityConfig modsecurity.conf; 0 d4 d& M; _6 A
9 N! u% u3 \* p T% u
proxy_pass http://online;
+ C1 z+ Z0 t/ r L* `" m, d% _ proxy_redirect off;
+ B6 i" B3 m7 c: p& B! b proxy_set_header Host $host;
; b# |, [7 s: Q. D. G proxy_set_header X-Real-IP $remote_addr;# ~% ?2 q. j J" B U
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
/ S' I6 n) |4 \; a& F }
4 _8 P# j$ g' h [, m3 T}- K; Q6 ?8 h1 n( u7 _ W6 Y
六.测试
6 U9 u* x+ J- S6 m& v+ X( w0 ^4 e1 P
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) B, {6 `( k4 [; U) n* O" T: G( k5 ~+ c
<?php7 {: }1 J# x% _
phpinfo();
& i$ ~' T+ O6 p?>% x- T0 B5 p: r+ E% f! {
在浏览器中访问:+ j3 v7 R# K7 g* f
' {# v5 |+ n+ Q7 [) S2 H8 G0 a: X, W
http://www.52os.net/phpinfo.php?id=1 正常显示。
' o# ` V m6 T$ dhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
/ |* v- ]* R/ u, o+ M/ }! Ghttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。1 y- V4 ]- B) i" P! Q3 F
说明sql注入和xss已经被过滤了
8 s# N7 z+ D7 i+ X4 n% o, U4 \; j. ~* Y7 C" n& I, X
七、安装过程中排错
A. X- s$ c5 f& ]6 _# D
E, i7 x+ f! c ^' U1.缺少APXS会报错 s2 Y- H$ F) c! q0 Q
# B+ d$ S/ D0 ^# u9 m4 d
configure: looking for Apache module support via DSO through APXS
* S; I8 u" N) L+ k4 T& }+ fconfigure: error: couldn't find APXS7 t; ?" P, m% ]
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。3 [' ~! f. U1 ]* H& ?# h b
解决方法:
* ?0 |0 h. i5 K) k
, V' s9 y. K: B0 H% A, u3 ~yum install httpd-devel3 V: N. K: V9 \' {0 i
2.没有pcre2 _/ P6 J" W: ^5 `* @
) @+ S1 C$ c( F
configure: *** pcre library not found.
3 M) }# q+ B/ qconfigure: error: pcre library is required
' J6 w& ?3 i2 k/ {! Y解决方法:
$ _$ N, `5 v. D# }/ [! H9 v2 J; m* l6 v6 [$ _ u# ]
yum install pcre pcre-devel
: O1 i: l' L' p5 i4 z$ K3.没有libxml25 x3 b6 h1 t7 c2 C- _/ S' e
$ ~. t8 ?) T6 d, u# m
" H: I0 {! @3 ~. x( D6 J0 ?0 r* |configure: *** xml library not found.
1 s6 a% t9 K- [8 J! B# r) }configure: error: libxml2 is required, @8 q+ ?; u% H G+ L
解决方法:
4 ~& U- |) }. h9 l& }% B2 P
- C7 V! h1 U, v1 C" L9 ^yum install libxml2 libxml2-devel8 x% A/ Q% ~- b! X! @2 S! r* A9 a
4.执行 /opt/tengine/sbin/nginx -m 时有警告
( J0 u3 ^0 k1 }& M, S; g% `9 \7 o
3 z4 e# V3 \. |/ { V* UTengine version: Tengine/2.1.0 (nginx/1.6.2)
0 O, y* u/ m, p& V& dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ k/ ^" A" d4 z& _3 E, x$ {. B原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 g, z) }. a( i# O0 w- e! _
$ F! n/ f6 R0 X
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( X; S& _/ [0 X9 f5 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
1 R5 l* d% ^; I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( ]- J) _6 A8 L, c* A) Z0 c1 J! e2 V7 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, k$ P/ F& s# _3 d% ?, T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"7 b$ S6 w7 o- K4 k
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.. I" W, b6 R4 B$ b7 `6 J7 p; P. p
解决方法,移除低版本的APR (1.3.9)1 W2 ]8 j1 I, s/ m# G
$ L/ u: Q5 G! e& s3 P3 N( qyum remove apr
( q* }9 A0 q1 T5 z/ f. \3 C5.Error.log中有: Audit log: Failed to lock global mutex
5 g7 ~; K3 h# z% f1 i' F! e; E z9 O0 x& [; [) R2 }4 k
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% p* \- H* g; M6 I# Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
/ r0 n) B1 W# {9 U3 Q4 b解决方法:; U) l! o' X& P+ Z H
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ Y. m* M' e, f
" e2 m- y$ Q1 s* B- k$ vSecAuditLogDirMode 0777
" A6 d! g; P% ~7 MSecAuditLogFileMode 0550
5 r( r7 d: P f2 Y. P" h( w7 k1 YSecAuditLogStorageDir /var/log/modsecurity* u& Q. _1 J$ j" R$ W ^: @
SecAuditLogType Concurrent
?3 O9 V; X3 Y ^0 i* y& L0 M参考文章:7 ~8 e5 Y" Y5 \' k" S6 }
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- S7 i u$ j& B3 Bhttp://drops.wooyun.org/tips/2614 |
|