|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
/ X1 e6 r( T8 t- V/ n3 t, G& ?
! N i/ J. \8 q8 I/ W: _一.准备工作
# L/ R6 U S6 s* C C5 z0 i2 w' u q5 o. }# K2 |: `! P0 E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ z; O& c& }9 O' G9 E
5 [* F3 `. f2 N. v( P) Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz/ B; Y' [* ^" j5 _7 S
- `* V8 V5 i! f& W2 E) Q1 hmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz* b, D: _) Q1 {- o/ e
: m8 d4 v$ c# \OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs6 o( R9 _3 H- Y
5 m/ w: c/ R/ u; M/ q. X依赖关系:
, i$ {' u" L% Y7 Z0 n! Atengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 w; m3 y0 P8 v% p, x+ m- `
2 ]; [8 Z# M1 D5 Ryum install zlib zlib-devel openssl openssl-devel pcre pcre-devel6 `7 b: \* Y: K9 O5 h' ~# J% F
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 N: F% w4 r+ I* o/ W6 _
. q2 W6 a" x( Q7 Iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel1 u4 ?/ a3 z& ^8 Q- }5 j
二.启用standalone模块并编译
6 b+ i4 _6 O3 L' Z9 ^! ~! U% I i3 U- H8 O- |- s" X0 ~; G
下载modsecurity for nginx 解压,进入解压后目录执行:
+ Z& v/ E. p" x) W; T ?
- ]- J3 o4 k3 [/ h./autogen.sh
U2 C6 ~9 s+ C" B" l./configure --enable-standalone-module --disable-mlogc5 I+ E( o% q/ _2 I3 w
make , i, i9 d8 M7 B* _
三.nginx添加modsecurity模块5 Q$ X' q* V+ z' V% B' R/ _
8 {; i5 a3 o& V, `7 |4 F- E2 k- r
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
% D+ W8 w$ X* F+ @3 I* t
* \; e$ q' I& L: U# M0 n& H./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
4 t J1 I. k# o5 x, ]3 x. @make && make install2 _9 F4 x( T5 g! g9 D$ x w+ U2 Y
四.添加规则
- s' k* E, ~" {+ m7 E0 t6 `5 r" s% O0 X
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 I& L' ?' U0 X( ]: Y1 E1 E! n5 i6 e4 T( x; q; Z9 L
1.下载OWASP规则:
* K1 V. ~+ h5 G/ m4 u$ b
f: e0 L) S. Q' L% Xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs3 f. h# q5 q- ?9 C) u: H
2 m: {: b% _2 Bmv owasp-modsecurity-crs /opt/tengine/conf/7 ?+ ^- D0 ~4 _, _
. P3 q3 G* n) p" h1 G. I) [% Acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 z' t' \- {) V5 [8 c2.启用OWASP规则:7 ]% ^: P5 p8 ~9 J/ T6 b- f; N7 W
' d4 P- `$ u3 L( w& z0 K
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
) l+ Q" b% p0 |- u% ~
]9 n7 m% W* x" T编辑modsecurity.conf 文件,将SecRuleEngine设置为 on @; b) N- p9 Y k# W& L9 s
: |/ P7 Y! e) D* J. zowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; a; ~1 S$ B- G8 E: C9 D* B) F- w$ P8 M% D
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* U# x2 f/ M' S1 ?' m) h9 Q% y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 z4 ]0 X, v5 v" U0 k2 oInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf n: Z9 L( k( |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) [8 i; W0 W% S9 @7 D+ Q/ L) n/ cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf) D! G+ @, X4 u \. E/ D1 ^' N
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 n' b7 P6 j6 [' U M+ lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 G* ?$ f2 a _" q2 B' t* v
五.配置nginx. _, R9 A+ T- b( D0 X+ S- p# V
, @' Y. s/ P$ q; I1 t) M! _
在需要启用modsecurity的主机的location下面加入下面两行即可:9 l. e" |6 O6 u; x* w/ |% D+ G
8 b9 V- j ?6 S$ M/ E* h7 hModSecurityEnabled on; ( U& m4 g' k- F7 R2 _- C' m
ModSecurityConfig modsecurity.conf;
2 ]6 f# j3 m/ A I5 k下面是两个示例配置,php虚拟主机:" ?' K# b [/ J
; B# B3 [- x1 ^$ w
server {
; d+ i# g0 |" N5 e listen 80;
4 x& Q" s; W" c8 | C1 t server_name 52os.net www.52os.net;# `& O/ q! \- E. |5 Q/ r
" |4 @& m; e( p7 j location ~ \.php$ {
- t( _9 s$ G! j' O; n0 Y ModSecurityEnabled on;
7 k; E6 [1 V& u# E* I ModSecurityConfig modsecurity.conf;
3 P' j; F+ t+ \( z- u( g$ \% `: p: P( J) F" m5 [
root /web/wordpress;
$ I5 W2 _" [( D index index.php index.html index.htm;
2 S0 F9 C% C- ?) | 1 z- s8 Q9 V% m0 f) D4 q1 G
fastcgi_pass 127.0.0.1:9000;1 W v$ {! i! N
fastcgi_index index.php;
& X4 e2 y2 m" t" x4 Y6 M: i3 g fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;" d5 T* z) Y! R% m4 r' b
include fastcgi_params;; t* t4 a% Q! }0 G& a) X# s3 C, t
}2 v* k% S6 S0 x/ E1 ~
}
4 f4 C" d* |( T& g `9 }upstream负载均衡:5 b' T2 W# [1 q. A
) `. X- O* v" w0 v7 q4 I
upstream 52os.net {) A6 ?1 c a& W) m c( B
server 192.168.1.100:8080;
2 S, x2 m. E! A- X server 192.168.1.101:8080 backup;
* U, ^6 C2 s) W+ S, P}
( H( g8 @! B* o9 t/ @- t' O$ ]9 Q
5 _4 V% j9 }3 kserver {8 j5 T" y, i* q) \
listen 80;6 D: |7 c$ l$ }3 e' e
server_name 52os.net www.52os.net;
8 I0 C, [8 a% g4 ]5 E. w9 G5 t( R2 a
location / {2 [! J7 n4 A4 Z3 N. l
ModSecurityEnabled on;
7 ~9 m/ w$ F5 X6 n- @4 z! g9 A6 B, t+ X ModSecurityConfig modsecurity.conf;
- ]) J% K& \+ i& t# J) {: z) o; v; I* h/ {( o+ W' q
proxy_pass http://online;4 u* v; m& W% [4 S0 K; u
proxy_redirect off;
1 c% @4 |; {/ Y proxy_set_header Host $host;) M! _$ X. T2 T D% E
proxy_set_header X-Real-IP $remote_addr;
& t5 @5 C4 ?" Z( r1 E' n proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 L- T& D" \/ E
}9 u5 f/ u E3 T& q
}
, I" U( X) w& Y# Q& v: b- P六.测试
( `! s+ g; O6 {& t
) ~# b: F p3 i2 b" v& y. i- C我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:; s: ?) c9 v! I9 ^3 n& J$ Y
^" U' ^5 T9 w3 z8 M0 c) o
<?php+ i. d" `& l; H; m9 N
phpinfo(); 6 e$ h& D& W z% _6 G
?>
& }! A& y8 t) E2 F! ^: f在浏览器中访问:1 s: x: w* j3 ?0 P
4 k) a& R% g) Q7 A
http://www.52os.net/phpinfo.php?id=1 正常显示。+ l6 n3 \: ?! p- J2 l) ^: J
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
* R8 t% h* }& Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 i" Y0 ?! }$ h# z4 ?* L1 x# k
说明sql注入和xss已经被过滤了
' I7 P; _! O0 j& v5 H6 F9 `, @# t* J/ W$ t1 M. l
七、安装过程中排错
( b2 E/ y8 I* w; D. K4 l; n& r* ^: F+ X, O
1.缺少APXS会报错
) \- w3 h; W% [' Z0 P# v" `0 D
8 c9 o! A4 j3 Wconfigure: looking for Apache module support via DSO through APXS
# u& ~; ~+ l; o2 econfigure: error: couldn't find APXS
9 t' e4 |1 L$ f- uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. m y/ @! {( @) | f# Z
解决方法:
$ `- n4 I- s5 ?* [- Y# g" K5 a1 E& {: r) \ C
yum install httpd-devel. z e4 i. P: [
2.没有pcre
! x W/ V, g1 E
7 d9 B% Z# I/ ~6 _$ hconfigure: *** pcre library not found.
# A6 z% H* O* T3 m: Z* O& I5 Mconfigure: error: pcre library is required _$ l( C" n0 P$ ^7 h0 J; a
解决方法:
5 U* _5 F, ^, E, N7 A8 n% @9 F0 N+ ~
yum install pcre pcre-devel( R3 S, c) j7 D* ~& R2 x
3.没有libxml26 u% W( }4 W) h0 ]
6 p5 {* {. J8 T5 L- p. W
0 n% k. i/ `, |" y6 Z/ C
configure: *** xml library not found.( G$ e8 x5 Y$ U% K3 T
configure: error: libxml2 is required
- u( h4 @5 e% _, t1 ?6 x解决方法:! S* M2 d! P2 g
9 E, p$ G* z- @- k1 T
yum install libxml2 libxml2-devel
, @! x1 N3 y. e, M6 ^7 p4.执行 /opt/tengine/sbin/nginx -m 时有警告6 x% @: {- i0 a' a- R" ]$ V# {3 U
e, @* a0 `% ^- m2 G! l7 OTengine version: Tengine/2.1.0 (nginx/1.6.2)
; R0 r/ X- i! Cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
8 V# u& X, z" W, C. r, u7 o原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- m6 U" J5 o6 P% j
' J0 v( u3 j$ B/ g5 b u ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 \1 r0 w' B& Q& ?1 W" `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"2 c( Y& u* G" P, P4 s1 E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
4 r" M# n1 W. S e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* B" L4 j$ v; |+ o% k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"0 k* G4 Z! B9 g; w
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: K3 v" A# u$ T6 u解决方法,移除低版本的APR (1.3.9)
9 u) |+ a! Z W
% o" I' m& i V4 U8 \& d, X. Xyum remove apr( {9 q4 a J* i; [* _+ q2 p
5.Error.log中有: Audit log: Failed to lock global mutex' e' r4 @7 W% g- o7 Q6 X6 x, G% O
5 d( i# a9 W" O. `0 q3 ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
D- V5 t; T% [+ x" E6 _global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
, D: l! d7 | d' o解决方法:
( ^0 l3 d' m- T) |2 x编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 I8 `6 e& H O' c( J
0 q/ `/ d2 I- X2 _* a+ E/ I
SecAuditLogDirMode 0777
$ {- J- {# P# ^2 D! OSecAuditLogFileMode 0550
0 ?! a5 @0 b0 v) e- W1 A$ rSecAuditLogStorageDir /var/log/modsecurity
( Y6 [0 B" N4 S% |SecAuditLogType Concurrent, B& }+ \- D* H8 N0 [% m
参考文章:
! c4 X, V+ Z6 w8 {/ O4 {https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 X: i: Y7 D9 l1 }
http://drops.wooyun.org/tips/2614 |
|