|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。! v! j8 |9 _& V1 r, U! @/ q
6 N+ L2 g A( e' M
一.准备工作/ s" z+ k. \7 b' j. j
/ s! s3 S3 \; ]' y* q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ W) N, ^& m" n! p( h+ i
" z B8 u) s4 a% [' [
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! V2 {* f' T# N: F t, ~; h
( \; r- w8 \5 E# W& @* w# Gmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 ^$ H/ e/ ^' m, Y, A; |2 L/ I* Q. }
5 I& d/ P3 ]0 F9 w1 VOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs( E& L/ g6 w3 ^, k1 @1 E
/ ^0 z) _$ P3 ^ \3 ~) C' J; k
依赖关系:
' G, r5 _6 ^3 Q" X' e2 q; |6 y0 Z+ Y+ r+ htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:+ k! l" ~0 a( ?7 e$ [1 l- o- c4 U# ?
+ Z2 Z* \8 T/ h+ y$ G
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel/ N0 m7 r8 W2 T
modsecurty依赖的包:pcre httpd-devel libxml2 apr
3 ]- {) L" Z2 F. }: C$ |- I( `0 }, z( t# [& y9 t& l/ G0 A/ J
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
! D: n1 q" X% \/ u* m+ ^5 C& m二.启用standalone模块并编译" T- C6 F& o5 n( @
8 K: O1 f& \, w" ]7 C4 b& H- Y下载modsecurity for nginx 解压,进入解压后目录执行:" W" i5 {4 @# f/ `8 h
, [2 t" w9 @7 _* c% |
./autogen.sh/ b2 Z7 h; g6 p: c" P8 I
./configure --enable-standalone-module --disable-mlogc6 X" p8 ^4 \* ^& M, U
make
3 X. G1 D- g: s8 L$ J) @: b6 ^" d三.nginx添加modsecurity模块! E1 Q- h+ u7 g; R2 P
# B" |$ @4 _" K5 O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& Z; y1 T4 g( W9 z9 \
/ w- \# O8 I* g- P./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 f0 c" y3 w x8 R9 C2 \- v, q+ n1 O( }make && make install$ D7 _1 u2 U; D9 T
四.添加规则- z5 _0 H: ]3 o
1 R& H) r/ S- ~* s6 g+ n
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。$ r7 [+ ]. s3 Z; Y4 y" G4 Z
4 \0 p1 F* S7 |( |: m+ l$ J$ {5 e3 s1.下载OWASP规则:
1 \1 P, d) ], W9 ~3 H) @/ d
$ T! G j$ t3 o: w0 s$ kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
4 F' L9 g& W, a% f! f: a; T! ?- F) q; D1 K$ D" s
mv owasp-modsecurity-crs /opt/tengine/conf/7 C. \. W/ m* d+ m1 @5 \6 J
$ S ?, z4 y3 C. Ecd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( {1 p, ~* r6 K5 l4 I
2.启用OWASP规则:) W+ W( i7 c" I8 {4 n
/ z+ r$ K1 ?$ @9 V) `/ h) d复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, {, n" S2 ^2 C5 c( E1 h9 k
7 t; I P$ n6 @- o
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: `7 [( J6 ~: b+ F0 {1 E4 U1 C. a
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。 q0 {' h) Q+ J% }3 w4 a5 t1 I7 ^/ |" I
* p4 Y. h v$ B7 B* Z2 j
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" ~! ?* a4 I! `8 D1 a3 d! hInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
% t$ [2 Y* t) EInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- j+ @) o% X% GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 H+ F5 M% h0 o8 lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf" Y1 [: ~% ~+ U$ S) {1 O
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf0 s/ V9 r; D# j; w( H. L
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 g8 N0 M- F& k, B
五.配置nginx5 b: ~. m" W Q4 }% E
+ R6 w( D+ h4 k在需要启用modsecurity的主机的location下面加入下面两行即可:
. n; q4 w2 Q) ~/ ?) ]5 p! h0 h
0 g& t2 m, @. ]6 y1 {ModSecurityEnabled on; $ g: d# T% q1 r; E# N# }
ModSecurityConfig modsecurity.conf;! T( r0 M5 t' H V' b% X `5 ]6 X
下面是两个示例配置,php虚拟主机:
' m7 k+ e$ X* K2 G% U" u5 B$ n- [
: L( P2 e, |1 F% Mserver {& P) R8 u/ R M4 G' r
listen 80;
5 A0 z$ b5 W* _; } server_name 52os.net www.52os.net;! ], [# X6 d; T
( W& H6 `0 ?; e0 I+ Z location ~ \.php$ {
% H% y9 ?* u: H: a U ModSecurityEnabled on;
, o9 O6 Z/ M" O; q# G ModSecurityConfig modsecurity.conf;, _ g% X# m5 r- z
9 |; \- G9 x4 t2 F! g+ `4 ^7 d root /web/wordpress;* a- v8 ^0 @' E3 Y0 t
index index.php index.html index.htm;
" j5 E, h) v$ R# `* _
/ w/ z. `: P+ {! k9 \$ v3 q. h fastcgi_pass 127.0.0.1:9000;
3 h' `& l6 O4 y, o" w0 Q7 |) ` fastcgi_index index.php;
1 f$ }( N! {$ Z% f fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
5 l m, S; @! u: A2 R# H3 C include fastcgi_params;
/ F! n4 O; |. O }
) |# V5 C6 C/ g0 r& y5 g }
g! K9 F; S0 |) z ]$ P. ^8 mupstream负载均衡:1 a3 P! m0 D( f9 L1 ^' k
% L7 I, z0 B* c( v7 N9 C+ Z
upstream 52os.net {
% P8 R$ s2 v# `8 X server 192.168.1.100:8080;* m7 ]5 E1 A5 ]6 {1 m) A
server 192.168.1.101:8080 backup;
1 w3 x) g( \. E" ^4 o* a% o' i: t}" r {7 o0 |5 s% ^( e( c1 u
" i2 E% @+ {2 P& u
server {
$ F/ S q" r3 u; G% u6 K. Klisten 80;
: a d$ p+ C2 v7 t. @server_name 52os.net www.52os.net;
+ x$ M. V6 G9 N0 H) ~" h( T
0 e. v7 v& T5 P8 s3 ^/ a: l/ R, h5 X/ xlocation / {, [; e# p) }; v ~
ModSecurityEnabled on; 2 [" N! R1 Q' @7 W4 w. [# W
ModSecurityConfig modsecurity.conf;
/ ~9 a5 G) x9 h) Q2 P, n# i
9 p! p. b6 y" X- N1 T# s proxy_pass http://online;4 \+ ?# _- V( [! M' V. i
proxy_redirect off;7 W) J: R) s% U* f
proxy_set_header Host $host;
) G' E9 A- s& t2 V0 }. f proxy_set_header X-Real-IP $remote_addr;
# D& G8 l: s- P proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;. u: w3 q! G6 A$ {4 j. b
}
c$ K. r1 O0 _: {. G1 [$ Y}5 c8 z& f" D. ^& c
六.测试
9 T- C4 T3 m& s% O0 O y
6 e) E) u) u6 ^0 v我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
6 u ]# D" F) j M2 R- M
0 `& m6 m+ X8 F6 E<?php
5 ^- l& x7 r' m4 b: F5 g' R phpinfo(); ; h% {" V6 o: d* M! n3 p" H7 f s5 k
?>
. _( |* b/ u ^6 O在浏览器中访问:
- u" N9 t) v+ G. i9 T+ v5 j8 Z6 j5 e4 [5 s0 y3 Q V
http://www.52os.net/phpinfo.php?id=1 正常显示。# z6 e6 M( ]3 f+ z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 |6 E2 b' R2 p+ G1 ^
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
# X1 |' [6 r" d: D说明sql注入和xss已经被过滤了
; H5 I! W( n( Y7 K5 S4 J% v
, N* C: \# I: S4 w. X; C七、安装过程中排错
3 C& s" [4 q' A: x( N/ Q- I: [: T+ |- l# J0 X, V: R: O/ o% t
1.缺少APXS会报错
4 W6 |. Y- j, O
; y' I- p2 ]7 Jconfigure: looking for Apache module support via DSO through APXS
6 ]0 _7 N3 v& f5 Z: a8 u0 Z& C) w: Fconfigure: error: couldn't find APXS
9 o2 a- i7 A2 S- K4 R' w- w4 Eapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 V# K6 V! K6 c# V# C
解决方法:# j" y1 t7 w( E5 Z! f
; M( W. L- e# |; O0 g% N
yum install httpd-devel
5 y' ]. v7 D& u7 i% E9 W2.没有pcre( V5 x2 s- A7 ?- P1 W- y6 M6 f
4 c" P2 t2 k9 q6 Z3 A( A# O
configure: *** pcre library not found.$ s8 v+ y' }9 y% N
configure: error: pcre library is required* }1 M# P! G7 U; L+ A8 k
解决方法:
4 x% j/ @% k1 e$ j
- X# m P8 c0 Zyum install pcre pcre-devel
2 a, O9 @9 j9 m& d3.没有libxml2
2 o& I- z* y+ O7 g; A# ^' m8 e/ l7 u
" o/ N4 g2 k9 P0 o% U
configure: *** xml library not found.* R& w- h2 `4 D+ X: d' G
configure: error: libxml2 is required; G" r# b5 o' B) A& r6 ]
解决方法:
, @; g' z, I8 T3 }8 \8 J5 @& O. I
+ R1 n1 e2 h5 byum install libxml2 libxml2-devel
2 O! i3 @, y: X9 _4.执行 /opt/tengine/sbin/nginx -m 时有警告, Z) `, F) g& z5 b: j4 E
9 A7 w! H4 F. l9 {/ V$ D/ s
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( }0 X/ R1 q0 X
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!/ o, y- C) O# @2 i/ U2 o
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 O" }$ H; q# l6 F9 L
K6 I$ v3 y( z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
. u0 T$ P6 H) |0 y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9": D2 P. o6 c4 B
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!4 O2 a5 z v8 v2 v d) f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"5 l0 c- N" h& ~% ]. A% W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ j, K4 c5 f3 B" H, g7 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ L( g$ M9 @0 {- x! Q* h
解决方法,移除低版本的APR (1.3.9)
7 ~& r. L# {8 E. B
+ K4 C5 ~5 V+ ?. V7 S/ |- n, Iyum remove apr0 U/ c4 |0 t; X" R0 U( U
5.Error.log中有: Audit log: Failed to lock global mutex
: e6 |, w9 _: X
/ e8 e* g- \$ z- e& r6 O2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , ]5 _/ A. r6 X6 d K
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]6 b9 I: r9 `, P5 g; O: R3 H, h
解决方法:* M; E7 ~% @. J& o% @7 g
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" `- {% g+ V0 |# h% Q! S
! p7 J/ k% m. D6 @SecAuditLogDirMode 0777$ ~4 h# ]* F- Q2 } Y
SecAuditLogFileMode 0550$ }; ]2 A' \( l. U: P
SecAuditLogStorageDir /var/log/modsecurity+ e$ Q5 x- V- g' O% k
SecAuditLogType Concurrent8 Q' k2 _" p4 r* a" A5 I
参考文章:
: R5 B. ~% ~( ?2 e8 X. Y4 J3 vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 ^8 W! C+ Y0 V& c
http://drops.wooyun.org/tips/2614 |
|