|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
q2 X* S- p/ @3 g/ P" ?/ g: g9 S/ a) v0 z" q
一.准备工作* G* q5 I# z! Q0 H
% ^: g3 b/ x) X系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 `7 }# l Q+ j) y7 t2 k/ o' g4 k( ^
3 L0 Q5 e; y8 j7 j1 w1 t
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
4 u' c' L# g/ R1 g" h8 S) p# y
B3 H% I! A% l' w. j% s2 b& ?+ hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
: V5 b- r* X p( d- z) S
' W! f; ~5 g: N' bOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# f9 A9 X6 Q/ s' ]8 o# } l4 q" O; v# R) g2 ~% E& E
依赖关系:
* t1 y- w Q5 g7 Ytengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# z8 T. D! }' |. t. P
, ?3 t4 F# }1 U6 s, S$ |% Nyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- f2 u4 \$ Y) ~modsecurty依赖的包:pcre httpd-devel libxml2 apr
/ o% K) w+ | N/ ?6 _% {6 K0 L R* O. b7 ^; p0 w6 p/ ]
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel% ?+ ?+ \; Z N1 n# J
二.启用standalone模块并编译
# I- ~0 y0 B0 M2 f# c
% V* x/ y0 ]- m; t+ Q下载modsecurity for nginx 解压,进入解压后目录执行:9 }) T" ^1 X3 m$ l5 t0 j9 }
' e, L* Q6 |0 t y./autogen.sh& _* ^: r' W( y* m
./configure --enable-standalone-module --disable-mlogc/ X# v( u2 B5 @# D" o/ ^
make
( V+ [$ T8 b9 Q8 `! u三.nginx添加modsecurity模块
; c' ~0 _4 s2 N1 }+ h4 O
* \8 W* _$ M3 i$ v' [6 J9 y1 e5 S在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 }! W# i! S4 z+ |# |$ t9 o
- r5 _- y1 G8 k1 h: y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine2 a" }! |7 e: V( j) D" \' F
make && make install m0 k# I* \# T9 g
四.添加规则
5 a0 ~9 E( w$ L! U) g! A& C" [# g" {* r( b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" H* o; L; S% y- i6 {" e' i4 x
* F5 f i& b' v1.下载OWASP规则:+ v$ D; n c2 }' s
, F! q4 _* l$ s8 q7 D: H( `' t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs+ b( g4 m# d$ E
/ a2 W" R3 Z: i0 R* f) m3 `% i9 wmv owasp-modsecurity-crs /opt/tengine/conf/2 { z1 D: o1 y1 r6 A. N( o
# R" X( k N6 L0 W' pcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 P, ~3 ?. p3 N# {9 m0 F5 R0 w* e- f
2.启用OWASP规则:4 P% z- I6 D) x' |6 y: I- i
~+ C- s0 L N% [- ^6 T" ^6 y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
0 _+ J9 R0 [; e% W
/ g" @* v u6 e6 I编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
7 C, y% J8 l p: k. e
8 E( [0 ~! p9 R0 g2 E% l; aowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 } l6 p% j; M1 j% `, m8 ?; a/ K2 l) I' v6 j) z+ q) v4 S2 a
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" z2 Z7 K' g5 V" X$ C! E$ rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ @$ g# }5 P+ _; Q8 }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
T) o5 _! t5 r/ V( @8 N7 b8 Z( mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 X L6 u) Y% o
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 z% o# ^5 ^, ] W% }2 L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 x7 v, U# z1 j; a2 C6 D( oInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
$ B# P4 @) ~" {1 K. U五.配置nginx
! W- V6 t% C: k0 o" A& J8 e6 H: k6 x; |" D6 `5 b# X
在需要启用modsecurity的主机的location下面加入下面两行即可:; O3 H$ \, S7 Q
$ D. _3 H) V! b/ l0 ~+ ?9 EModSecurityEnabled on; 0 W" ^* E* U |, ^
ModSecurityConfig modsecurity.conf;
( m7 R) y& d- ~% G% O下面是两个示例配置,php虚拟主机:
+ z! h# J% q. t8 j e% ?+ U2 e5 }$ U! g
server {
c7 G( ]" U* {( H$ u6 w listen 80;
+ [5 p+ l9 v; `+ h6 N server_name 52os.net www.52os.net;8 o) l" X+ ?% c* r
7 _1 L) l$ I1 T+ ~; N. V; W8 Z location ~ \.php$ {. \) X& T7 A8 ^. q
ModSecurityEnabled on; ; I6 s7 P! i, I9 p
ModSecurityConfig modsecurity.conf;
m* e! G+ J' W( P( j0 p
7 W8 `$ t: I: L6 K root /web/wordpress;
0 B7 F9 s1 l, Z$ O) a: ]. S; o index index.php index.html index.htm;' M3 n0 k) Y& S
+ }8 A! I, l0 A# B
fastcgi_pass 127.0.0.1:9000;! N1 s' F6 k8 l' a
fastcgi_index index.php;, U/ d H2 I+ q+ V" q! w
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
+ A# _3 h6 C0 u* `4 a) ^+ q% K include fastcgi_params;) O. H5 \ V" O# j- n1 o9 a
}
6 S1 o: h/ A9 E: ~6 _8 V+ c }
W# q/ I8 T5 y8 k- Lupstream负载均衡:
# g' E7 t- f3 H, C" s# T$ q% Z! {2 b+ z: ^1 W- I: Z1 h+ u
upstream 52os.net {
" X) h7 D' y! ^ server 192.168.1.100:8080;5 h" B% S6 U6 z! z1 c
server 192.168.1.101:8080 backup;
: z9 O' ` d. a. g}, L# n$ x% Y$ A% C8 Q: }( [
- j! d2 d$ b& ~/ h, ~server {
" N1 M- R+ b- N. olisten 80;3 G3 Z" g" I7 }2 v$ {, c
server_name 52os.net www.52os.net;
! H I! b# u1 |6 R* W. x$ c
8 z, W6 Y$ @' j1 Q" ilocation / {
0 n& g* [0 ~! y. y& C ModSecurityEnabled on; % @5 u$ I: Y4 s8 p" m; b# t0 |
ModSecurityConfig modsecurity.conf; 6 k7 v$ a4 v9 V i% C- T
4 \" a+ H. m6 o/ c4 i4 S9 b+ r proxy_pass http://online;
1 k+ W! O; o2 v. @# S% m$ b9 k proxy_redirect off;/ ~0 M9 \- o& i1 s: x$ G3 x
proxy_set_header Host $host;7 S1 u) _+ z3 B. g* o: E
proxy_set_header X-Real-IP $remote_addr;
5 l2 V/ s1 a; J( m% F2 _, O proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;% X. Y5 v7 {- I# f g4 W
} U+ F1 k& v( r7 j9 V
}
5 }* F3 g: g: b8 F4 ]% u3 E1 N* D六.测试: Z p, B" _. e- |) u
" C9 ~- {: o5 o我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* K2 r9 W0 I- O" }
$ n; v3 u* I* u6 ~7 k- P<?php+ l N5 S# T L1 }
phpinfo(); ! p4 }; B2 U3 D% d$ e7 i
?>, L' `9 N; |8 t
在浏览器中访问:
/ V( z$ r: q: w9 p
" L( B3 Q8 M, |7 [; Uhttp://www.52os.net/phpinfo.php?id=1 正常显示。
3 X- g; L4 l& P2 t2 R8 e" Ohttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。- l6 y. _) `% _; n7 o4 s
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
" v& u+ ?# B }4 R# e说明sql注入和xss已经被过滤了1 c& a% k- `8 ?/ ~, ^) T& v
4 f# N0 h6 ~; Z. |* r6 G七、安装过程中排错
& `5 C: W o5 } j+ u3 I
( u) ^, L0 p+ o. p1.缺少APXS会报错
$ [0 u; z( G9 W* |" d, m/ r' |' z4 x* v. x" `% f8 L
configure: looking for Apache module support via DSO through APXS
( w- V' m* r2 k* cconfigure: error: couldn't find APXS. }# o; W* e4 Z$ h# d* N$ g
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& f: f, g2 o% }% e/ D& U
解决方法:6 P) G$ b1 Y& z5 |
; D! |; d; d. S9 f
yum install httpd-devel
6 H2 E9 O5 K0 s7 H0 o$ K; S2.没有pcre
1 \1 d6 @5 V# y7 K7 c
/ z4 F) W$ G6 j( B+ w% Lconfigure: *** pcre library not found.
' K5 f. N1 W: L3 {6 e) |configure: error: pcre library is required" E- h9 M( V7 T8 F1 K0 x
解决方法:
2 u9 ?( G2 Z8 B8 `, D
: P/ e! @ J. h4 wyum install pcre pcre-devel
2 U0 f7 C, o+ s. _- m1 q3 @/ _' f3.没有libxml2! j7 S/ d$ g) ]4 N% i
x7 g$ q( Z( H8 E. W) `
0 I6 p9 y0 J; `/ Q% @4 ]4 \configure: *** xml library not found.
" ^, x9 p1 b0 p& iconfigure: error: libxml2 is required5 ^' M1 L+ h p3 b" z7 v% {: G) q
解决方法:+ T% O2 q9 w: {. s" i8 E
6 \$ b9 Q, G+ g) V* z3 e/ R2 qyum install libxml2 libxml2-devel% f$ h g. g: `8 d
4.执行 /opt/tengine/sbin/nginx -m 时有警告
I- d9 C2 Q8 [4 P$ o& e
# J7 `" |7 s1 K: sTengine version: Tengine/2.1.0 (nginx/1.6.2)
. c6 v5 o' o6 U, h+ y4 hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!) u0 w- F5 u* m, m9 v
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( ~( s) A# P% x2 s4 ^
5 ~7 D# Y3 D9 v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* F J% o8 K7 R7 k( Y0 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"% L* q8 k- d8 o% v- r
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! t9 D3 q% H' K7 `* e# m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
, E/ U* ?9 W( _ n' L3 j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"# \0 \ ^" j5 n# v! u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 D6 b7 ~1 L3 a. c3 A3 h9 h3 c' A K解决方法,移除低版本的APR (1.3.9)
; X( s, h' R1 _2 k) A4 D% X* X' G3 c& B* B! @' G
yum remove apr
1 l0 T+ r0 y9 e+ |5.Error.log中有: Audit log: Failed to lock global mutex
) q+ R1 ^2 z" [5 H( J* K7 m3 u% \/ P& ^, S4 I
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 ^- ~5 z4 k& C9 c- S% P4 z
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
1 c5 t( _% V S9 I/ `' k解决方法:: c3 f+ ~2 ?1 T c! l9 }5 Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:" i1 {# O- F% U" u! D$ s
, X% f C! u2 C: DSecAuditLogDirMode 0777
; {4 z9 W4 p) g" V- h/ K2 MSecAuditLogFileMode 05508 J3 O5 E5 W7 m. R0 i
SecAuditLogStorageDir /var/log/modsecurity7 A# v# J% T6 }% q* s' C* |4 C! r( d
SecAuditLogType Concurrent
4 ?6 m @. u: k/ M6 }参考文章:* o4 J- d$ Y, o# y
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
) E( W1 `% |& chttp://drops.wooyun.org/tips/2614 |
|