|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
) a2 g$ P" J% w' i) n5 D2 U; i1 C4 C- |4 `$ w
一.准备工作
+ j2 a& r( G% ^" Y0 S9 F* ?! M* B
! E3 I, N( ^' B. c, }7 _系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0( s( Z. x0 x: R
& X1 G/ Z' B2 }' Ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz+ ]3 @! N f7 w2 K' q" G" y8 O- H
6 x: r9 g1 `4 X
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz6 h! v' t* `. B
, G4 o3 Y4 O5 _2 y6 z {% IOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
! A7 H* B- C% v7 f: J. \# d/ ] L) a+ ~/ C2 A' U8 K
依赖关系:& }) v9 M3 K+ D% e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
3 w) D( e6 J" a/ _. }6 ]9 \9 u2 g
9 O% w* O/ t0 w* {! y$ Pyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
8 H8 E$ p1 V' ?" A& X0 T. M& ]modsecurty依赖的包:pcre httpd-devel libxml2 apr
4 w9 o9 Z9 S- t2 f5 W- ?/ m1 d' j/ r ?
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
; ^' ~ k; |; z二.启用standalone模块并编译
+ k8 ?( F7 T" j9 h q) A; P5 O+ {) e$ Q3 C5 C
下载modsecurity for nginx 解压,进入解压后目录执行:. F% o5 N- U w* V3 z
5 o) K) m2 ?5 a# B2 Y9 n" o; a/ a5 z./autogen.sh( U, f( k( {3 x h
./configure --enable-standalone-module --disable-mlogc
8 {2 ?, }! S; N+ ~make 3 d% t5 {: |& F+ b; r9 {5 ^" O
三.nginx添加modsecurity模块
* q9 V7 A' }+ O% \& L" D, Z i: c) r. g/ s" T+ o0 N! C
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* E6 M. s5 o9 @5 e
, k [$ W. C# q j; W2 j./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% V( q& c4 o; D& ?6 Tmake && make install+ A# r1 j6 J) R
四.添加规则0 }# w0 |% u# H$ `9 L! v
- Z K2 C5 w5 m+ {/ E
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
/ x$ ^8 D1 d" n% ?
$ q; s3 }; Z3 L: z; ]2 |& D# g1.下载OWASP规则:. Z- T# d0 |: k3 \' e
7 F+ z4 S" |1 Q5 ~2 tgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' m" L4 B0 u4 x+ B
" ]$ K9 x5 u3 Y" s' ]/ h' cmv owasp-modsecurity-crs /opt/tengine/conf/0 b+ t: `; E9 @
, J( D, ?9 s6 J0 b8 ~: V# V: Hcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; p+ |9 \2 L, l! t( f$ x2.启用OWASP规则:- e( W3 p/ a! d! u' x; s
' }7 R, b! A, C9 L复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
2 R! C. {+ g+ s' u; b# N: R
8 d) n: w' J& m7 V8 ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( y/ t$ c' d) F ^1 N5 D! M
# u" y7 R) O- P% C! ~- `. n2 S7 U
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% L" \8 D! |$ j2 `* P# j
) w( b. [) }2 z( G, VInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
: i. k" [2 J0 p+ w5 s0 ]1 l4 iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, J% Z" a2 t0 H. C0 v a( ]Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf; d7 v/ @. V1 c0 w
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf7 X0 j' R$ H$ w, ], y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! Z8 D- N9 @1 ^, a5 u7 pInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 t" z6 f/ a2 m& E. `Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- O- H' e) h6 s& U" q
五.配置nginx
1 c/ R8 m' ?8 p6 L; f
( I% f% n7 B3 R: K$ B( z+ d在需要启用modsecurity的主机的location下面加入下面两行即可:
8 I4 n# ]2 E O# _0 s- r- n. z8 F* h8 @7 M
ModSecurityEnabled on; 2 e9 C5 H/ _3 t$ L) Z- O
ModSecurityConfig modsecurity.conf;
4 x A! f+ y: {& T下面是两个示例配置,php虚拟主机:5 F0 `5 M" C4 Z
4 p. A& u2 }6 i1 K% l2 ^3 V' }
server {0 z1 z( o3 l2 L+ j8 v& I8 q( t
listen 80;
. D- o% `3 p2 I$ Y! g! b server_name 52os.net www.52os.net;
0 Z4 b% n0 b+ L3 O/ S& _6 k 8 q) {" {3 U- a" ]% N4 `- e3 C) Q
location ~ \.php$ {! }! t5 v2 H' R
ModSecurityEnabled on;
0 z2 k/ Y! D! ?6 H+ s N0 T ModSecurityConfig modsecurity.conf;5 M- ?, q" p6 l9 O
6 S e1 c$ [5 A* ^9 U" K9 Z' @, f8 S! ~
root /web/wordpress;7 J! I- |( w$ V4 O$ a
index index.php index.html index.htm;
/ q6 G; u3 i; ]7 X+ a* F( O
9 v! q7 {0 s5 S$ _6 s4 A fastcgi_pass 127.0.0.1:9000;9 O k" U, P- S* j
fastcgi_index index.php;7 j, }. a5 E" Z) `. S# n' j
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;+ s) d* x3 l! ]" o9 w* f
include fastcgi_params;4 R+ |4 m) t" U1 O
}
7 M* Q [; ~6 R, k% g( Z7 H( v }
( e8 O" S3 T3 R' _3 N4 mupstream负载均衡:
x- ~# V6 H" m0 o
$ H! [ p/ _# F* Zupstream 52os.net {
2 o/ }# G# a! ] server 192.168.1.100:8080;
$ x5 H) X4 p* \' v server 192.168.1.101:8080 backup;7 b' s0 q# Q. o
}
& f( j" o% }8 U# B9 b! ]% L0 d- ~( m; m
7 J& x2 x( \" S' i* D& U; y- vserver {
" n4 _4 _/ G& m( Q1 [# glisten 80;
- C7 n6 u4 G/ rserver_name 52os.net www.52os.net;& `5 w5 ~& Q& b+ d; P4 S# x6 f
2 Y9 H- S( H, ~
location / {$ Z i* t! v0 k! t9 ]
ModSecurityEnabled on;
9 b' j- Z' y0 D- l3 r+ @ ModSecurityConfig modsecurity.conf;
. D% y$ o/ z; I. ^; A( a0 p, I# Y/ h7 O3 ~+ U7 I1 z
proxy_pass http://online;- L" w. L) i, p* \' G2 G
proxy_redirect off;9 N4 @/ O8 d2 `- o9 \9 k
proxy_set_header Host $host;9 s( P4 A: {9 q' U
proxy_set_header X-Real-IP $remote_addr;4 m l, r k: {/ K% i
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" i- m: ~8 ~5 B7 O- b }$ C* V% ^5 P. V( O- `* I+ O9 ^( J
}
4 T2 m" c# E* w; R5 e/ S1 _. M六.测试
: Y" z$ w) f) W; J+ j( S# g0 g0 D0 f9 W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:1 O6 H! A2 Y, L
$ H( {) z5 D( m! x, P: y<?php, x# o) R9 c7 S! ]
phpinfo();
- m5 Q$ o) d; y- }0 I* i% B?>
+ @( I H3 z7 N在浏览器中访问:) \. V8 d/ m( M* G8 ?: S
' [, e6 y, Y9 F' l! l9 F0 b+ Xhttp://www.52os.net/phpinfo.php?id=1 正常显示。
' Q! @- ~: r# o; w+ Hhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
: \* n4 \8 h0 ?$ }http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! p% H/ K* \" p6 g3 {) V
说明sql注入和xss已经被过滤了
$ @- ^' i4 l; ~7 L9 Y% j4 ]4 l, O1 J L% p9 I/ i
七、安装过程中排错
+ ^& O: T' X. r# ~" Y: ^3 w8 c9 Z
1.缺少APXS会报错
% t$ r2 k! V* D/ g1 q; W$ K% `6 N7 c2 g$ f% _3 E" j
configure: looking for Apache module support via DSO through APXS% o% I7 Q* N, V% g7 h9 `/ @
configure: error: couldn't find APXS
9 D1 p- L; t: h# \! }/ _3 bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。* O% ~% l& M3 I0 ^4 f3 T
解决方法:/ H% z0 V! i# ~' N4 O
- A( p9 k& V0 o" X, l; \7 X
yum install httpd-devel) a/ e; R' T( W
2.没有pcre
0 t. h1 B9 g; A& L4 M8 i, U* |% ]% k
8 r# k+ N- d3 k" I, `- ]# iconfigure: *** pcre library not found.
7 K f. l; M$ G; L4 o3 |configure: error: pcre library is required
' q+ w) `& s9 a ~, K' }: o: ^5 r解决方法:7 `5 z3 k$ i% }# B, Y# c
) Q- m ]/ u3 ` G+ E6 {9 ^yum install pcre pcre-devel
2 H( Z! t* R3 ^* A6 }: Q3.没有libxml2
, h; @( W5 R' S6 W4 f
9 U: l/ j- s: P% u u; x+ Q7 P s% y% j1 M
configure: *** xml library not found., j1 v* ^: G" X) K! | Q- S
configure: error: libxml2 is required
9 V+ [( _5 c1 R/ b! @解决方法: A4 Y. r0 `6 U- y
( \1 r4 ^$ D# H2 h( U+ o
yum install libxml2 libxml2-devel
3 N: K& F, ~; y4 H% U6 {0 l2 f4.执行 /opt/tengine/sbin/nginx -m 时有警告) R: D/ r+ W1 X: w# p
. i' q9 q: w! R3 ZTengine version: Tengine/2.1.0 (nginx/1.6.2)4 a: I1 S0 H3 c4 U( ]+ @! B/ k' u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) V( c9 s6 }0 j2 H& T8 z原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' p3 p& M3 Y D
7 P, t% \, M4 K& I; E. m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! Y% c+ u) O4 b: o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"0 I/ K0 i/ A' y4 [4 V, r$ X5 X# \
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!( G: D9 w0 h8 O" k$ x
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* I9 T1 T' D' P/ z, ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"' q% ?' H7 u( F7 k) d0 g4 d7 ~
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
7 ^9 A9 m( x6 V/ y+ l解决方法,移除低版本的APR (1.3.9)
& r2 P3 K8 y0 R6 s; ~$ n
* R3 o* q4 c1 p# ^$ i/ b! hyum remove apr
8 z* r$ K# o" F7 w0 Y5.Error.log中有: Audit log: Failed to lock global mutex
( K; s* p" l. N$ e- a0 f
7 I$ l h& f3 G1 e( K3 S' G2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ! P: Q3 I# X" ^6 M1 o% S
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 X, W1 i( V$ s q: o5 J6 d' ?解决方法:
; K8 A# z: {. ?; f" d# W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( o6 ~4 g y% B' f
# r+ Z; O2 P3 ]5 Z4 cSecAuditLogDirMode 0777# i" B; k5 I7 `0 ^
SecAuditLogFileMode 0550
4 d$ W6 Q* k% \- j7 \! HSecAuditLogStorageDir /var/log/modsecurity7 L1 X3 F3 R; k
SecAuditLogType Concurrent
) [) _# N" N% i5 x参考文章:
8 i9 `. \% t$ x4 ~1 Q) fhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; S5 E2 R2 F. S6 @http://drops.wooyun.org/tips/2614 |
|