|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# Y; t6 D* j, H9 V+ R
s% T. V' o0 y) n( J6 p一.准备工作
0 e* A& Y j* z7 i) [8 V3 `' G* g$ B
6 [8 m7 N9 W% }+ ~6 S( H0 A$ a系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ L5 H7 k# N3 | C/ r2 q
0 _6 P0 y; Z1 p+ T) j
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
. i8 i) S$ J0 _4 r" Q* i3 v1 L. ?: A* A$ i
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
8 O% \: Y& H% w
# ^) m6 M- g- H- T$ K- }! tOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& L" c5 V7 ~# S' Z
! z9 p1 Y( u7 l, g依赖关系:6 |1 m8 R6 g9 m& M; k
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 Z2 }+ f/ m8 j: C- j6 M
/ M' D7 C! u) @( w5 i9 l$ v, ~& zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
' j' i$ Y+ C4 V( ~* V; Fmodsecurty依赖的包:pcre httpd-devel libxml2 apr! e w7 ^$ b2 W- E3 H1 E# |+ Q
) p6 B" A. \% ~2 d' E7 W
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* F6 ~6 q# F5 y9 m& \& q) e
二.启用standalone模块并编译% Q, u) R3 L- k# p' ?7 Q
. C4 |. H1 z$ G下载modsecurity for nginx 解压,进入解压后目录执行:
. _5 a5 u M& B% ?: O; w: ^: T6 b$ f% n& K6 s c" \
./autogen.sh
9 ^4 Q2 l1 U! N2 P7 Z. `3 M* r: \$ m./configure --enable-standalone-module --disable-mlogc
3 I5 v2 q+ o7 L# W' Emake
8 G/ u! w8 C2 } P三.nginx添加modsecurity模块* W% O' P1 r3 z3 b
3 k, i7 c7 p6 T- X' p( J" C* w
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 w2 T8 L& Y* b/ |3 X
" p4 F. d$ @9 F6 V! [! r3 T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine' M+ J" e% j1 z8 m
make && make install
0 j7 E' Z$ a3 d& w2 A. s* W0 q* B四.添加规则
' v" W9 V L+ z( u
d; Z" I% U0 [8 }' j. g( _modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 ~! c0 Z: c& U9 }1 C1 B6 ?5 Y5 p' ?2 E g
1.下载OWASP规则:( n0 x0 V% S, h$ b
6 T+ r; J% C) u1 Ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
; _) O* K) c1 i" }
1 b+ M9 ^2 x2 k# W( fmv owasp-modsecurity-crs /opt/tengine/conf// A" e5 V" y% N J% f" T7 s" V
; Y6 @- e) g/ U5 j, w
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# C1 ]- u0 w. f4 ?- N
2.启用OWASP规则:7 U( e1 E0 h' C$ W3 l: ~: m
0 b9 b' m0 Y" M. ^( x复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 Z& |' W( f- O
4 Z! n2 [) P; ^2 `, o4 E编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
# W, G, ^/ q$ ]- p M- _% A% Y& r6 e# z7 I# w$ e' t0 T$ Q/ D
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
- t* D& ^; z6 Z) c8 H3 `0 t
% r6 \, i- R9 X$ C7 TInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 X* G; _: m; o g2 }
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; k4 v; F" q/ @- \Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- @/ Q2 A2 c3 w- m! Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
4 N$ }) ]6 m' P1 N' h3 e. FInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf. `9 W4 z8 S. r9 m2 B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 Q( X7 N: `& b& R _0 W+ ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: z' g, t2 f; u五.配置nginx
# O5 n5 I V* Z3 U/ }2 v; ~
9 G* z7 _2 T) A- g# f在需要启用modsecurity的主机的location下面加入下面两行即可:
# ^% \1 N+ Y# u1 F% y4 e9 x4 N7 ]
# p' i3 W' H6 r8 P1 ?' F/ |ModSecurityEnabled on; + m" l" [+ ]3 i" Y" J6 C
ModSecurityConfig modsecurity.conf;$ P7 |. b7 Y/ I1 M' s2 Z+ y
下面是两个示例配置,php虚拟主机:
' z5 w( {1 O; Y1 h' b8 K- W4 g
/ O4 A* a8 b, N* j/ j# wserver {
& n. H! s/ {+ R. |! u listen 80;
- Y3 D a* j( P0 r5 B+ }4 e' z b server_name 52os.net www.52os.net;
2 {* A! Z9 w/ M
1 p" \5 _2 V* ?2 W location ~ \.php$ {
" u0 X$ @0 {& `# o! R+ e6 V ModSecurityEnabled on;
( z! p4 ^9 f) t# K0 L8 d ModSecurityConfig modsecurity.conf;- Z3 u+ t6 b$ [: _
3 M$ s/ N1 O0 u$ s4 E: E1 j( I4 _! x' Y( S root /web/wordpress;
^0 ? `$ h) L' K% F index index.php index.html index.htm;
8 @9 J) R9 o L; q) A% Q 8 Y, D1 T J7 o9 @! ~2 N: ^ k
fastcgi_pass 127.0.0.1:9000;4 q% V. t& n' R2 K1 V
fastcgi_index index.php;
) @2 a. M; b) O3 W4 l8 I fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 r" Q K2 _9 n% J9 K8 Z" D include fastcgi_params;' `* G# A- a6 q
}
8 V% n7 [7 f' F1 U% {7 O+ R }
. d0 q5 ~2 K& b* tupstream负载均衡:' o3 D- G8 t7 t ]3 M
6 P: ?% I* U1 p' N$ M
upstream 52os.net {
3 H" w8 b) F# y) g# C" o server 192.168.1.100:8080;
* q7 |) z/ Q$ I$ ?- x server 192.168.1.101:8080 backup;: D* F% F3 R# F' @- U
}- I3 ?: \+ l* y0 l1 E [) u9 |" H
% [: p3 F; ^- h, ~. M3 H1 Lserver {( z+ U6 v- A8 q9 Z
listen 80;; ~) t* D, V( B
server_name 52os.net www.52os.net;& _$ p* S1 \0 j) K: H
+ M& e6 r" g$ O8 V5 Olocation / {, ^' b# [" F5 [& E `
ModSecurityEnabled on; & ]5 O! ~5 n* i1 G6 K
ModSecurityConfig modsecurity.conf;
6 h/ d: ]8 w7 v9 a
' _% e/ k' a4 v) i6 ~' ? proxy_pass http://online;
; x8 Y: _! d/ R3 a. w1 T" o proxy_redirect off;6 W* L4 o( Z2 [- d
proxy_set_header Host $host;) J' E- E5 A% j. }1 C
proxy_set_header X-Real-IP $remote_addr;
. z3 D9 z2 ^& n+ v m$ k+ q1 A% H8 D6 v proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
& \. H" y: q2 g; Z } l) h. T/ d2 f' c, b
}
6 _8 b& \% n& F! y6 x六.测试5 Q$ _" h, d' X0 Z
+ F6 b' W. \! B" F
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 }* D6 u, i* W a% k; k
( k m: q' P5 Q. A j/ x0 Q
<?php
8 V ~- ]. ~0 H: Z; p9 J) H phpinfo(); ; D; C8 ~+ A7 b4 t# o
?>
: k6 e8 Z: L( Q$ l4 M6 ], q在浏览器中访问:- N+ x7 O d; e- P" K
5 u6 L0 \' K3 q' Uhttp://www.52os.net/phpinfo.php?id=1 正常显示。) m4 D+ {+ w5 @6 }
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
* r# I2 x' c* H8 d! ]+ W% x: Ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 J& T4 w+ _/ e说明sql注入和xss已经被过滤了
* a& I7 }. ]1 _$ g5 e+ g8 d! G9 r/ T% A- ]; \4 U: p0 `! h
七、安装过程中排错
/ S/ o: E" Y8 _
: |6 e6 D f# E, n1.缺少APXS会报错
% L& } i. k4 |+ S$ M+ Y" |/ u) b7 H( ]- K) v( L1 K
configure: looking for Apache module support via DSO through APXS
( ]2 Y- z. h7 Wconfigure: error: couldn't find APXS+ V. `: \' D" t: M6 F
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。# m9 a3 h9 c% c7 r0 x
解决方法:5 \- s3 ^9 K e0 }/ M0 V% Z& }) Z
u5 E8 N! b0 f; j; a v/ u6 ]3 byum install httpd-devel3 [7 g( G9 @1 l. K( U7 |
2.没有pcre
- ?9 G( |! L# h3 w1 [
4 P/ L; ~% x2 o) h u- J0 pconfigure: *** pcre library not found.: y5 ]6 D4 n# P
configure: error: pcre library is required7 R0 Z+ |7 w. c- M' s+ u1 _
解决方法:
. G* h% ]& N! y. P1 n- N: A j0 P
( d3 Z* c9 g+ h) Kyum install pcre pcre-devel
M% b9 A- T6 {3.没有libxml2
6 {) ^& A3 s$ N3 Z! m c8 w Y8 E% t+ j8 f* U* m, c3 Y- R1 I
M% l% e# i2 J2 n, h: n
configure: *** xml library not found.6 U( ?) h8 i! N* Z& N2 p/ V
configure: error: libxml2 is required
0 d6 \5 i# G2 q7 s: a' \' p5 x1 C解决方法: i" A0 S* K7 l* B! x+ T
4 q: { m& v' L* P6 J0 eyum install libxml2 libxml2-devel
2 s; G: M; H3 j! T: H4.执行 /opt/tengine/sbin/nginx -m 时有警告4 `& D7 u' R6 t
7 A4 n. R; m, z* F# J/ @' Q! v' DTengine version: Tengine/2.1.0 (nginx/1.6.2)4 J0 \1 t: R1 E$ j$ S0 ~" A
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( W9 G: {& F3 k) u
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
) j3 \ x6 a1 o6 Y1 x9 {: c C q6 ?9 R0 ]3 @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 G- s# h9 o5 `% y7 Z1 F& c2 g2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; U. G. f& z& R) f
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
5 k. H- n1 [1 N. F. P+ D( J2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"+ W: g: h% w. [* J0 }; f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& e' X/ }+ K! y9 J1 Q
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' w( w. T" b3 D' [. P
解决方法,移除低版本的APR (1.3.9)4 J% j* f5 r0 \* I
) H% u f1 h) C1 e: cyum remove apr' H( ?7 K! c/ V. J; \, I# j
5.Error.log中有: Audit log: Failed to lock global mutex
V" w* w+ i/ \$ k( r
( J4 ~& Y) c/ b& u1 B2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% y) \, @; \ u7 U& Z1 Mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
% h+ o( {2 M! [$ V1 f) ^8 h解决方法:2 }0 U3 C; ?& _+ Q& V, B2 y
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" Y# n. N5 c ]; ~5 E/ U) g$ p$ |8 ` j S$ ?! N& W
SecAuditLogDirMode 0777
( e/ P0 I' ?/ w. C: bSecAuditLogFileMode 05504 V" g! w) N; U7 ^) n
SecAuditLogStorageDir /var/log/modsecurity! i9 `' f. ~4 m* ~
SecAuditLogType Concurrent5 L, F0 N' v( f( s; [
参考文章:! D& j( }/ W+ B) S9 n" k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
j$ {: B+ t! t Lhttp://drops.wooyun.org/tips/2614 |
|