|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% M& M/ U* }5 K5 T9 { I/ S* ]2 d6 H/ i
一.准备工作
+ Z$ e5 c6 D7 v6 X
3 \$ i) K5 I& X8 z' N0 `' T" b* Y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
# s9 {+ ^9 [8 i! c l( r' w" c4 `' R+ y9 K4 s, V* B$ o- X( f c
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& V, r- }% y# E( P
$ Q& p, { j+ [2 J5 w6 y
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; c0 k0 p5 R9 o. I+ ^- ]$ H+ r
' d N0 s. u4 U) l; c9 S4 Q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs9 d/ N9 P% k; p- t8 r4 d
/ Q) U; ]* K8 T* T K: E; b1 S依赖关系:% E8 T, Q) Z0 F8 s+ r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 w3 S4 A% _" m6 ]( K/ K- [
0 P4 b! a" y' M' X' J1 w, Q9 E% d1 Vyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, ]6 O# V$ @; e$ k$ H" H+ _
modsecurty依赖的包:pcre httpd-devel libxml2 apr1 j% \2 s7 r! E/ n$ Y8 U/ H% O4 ^
9 V9 R K. Z$ {
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, Y0 {8 m3 j* h- h
二.启用standalone模块并编译
& f" j8 n4 u* {. s# |5 y% e! Z; O- r) j Q8 I0 g' ]
下载modsecurity for nginx 解压,进入解压后目录执行:
- b5 o; O5 V+ q, \1 n' r) e; `+ b1 ?& L$ H, _/ o, S' }
./autogen.sh z( b4 f: U7 T$ Q3 _( {
./configure --enable-standalone-module --disable-mlogc
) @. Y. t% x ~5 b& K9 b, W, w. Umake 8 ]8 g8 O; d' ~1 D
三.nginx添加modsecurity模块' h6 v: L: P- v/ `6 a3 h
: \3 D+ [ ~" |/ T( N4 f
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* O) H/ r8 Y- v8 ~# d
, J2 p" E! y6 h1 u, U* J4 R. O8 m/ V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
: a' \7 p+ q+ r& Z5 s" r! A- o2 b) o8 z5 nmake && make install$ H! W6 _( P B; K! ?3 V
四.添加规则
1 j" l. {$ e8 |
4 q( B0 w: N7 U. ~% ]* x% Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 b/ n! O$ t v* \ M8 _% w: a$ W
, u4 ]" K4 H0 C* }1 ^1.下载OWASP规则:% k! {7 b) r; W6 c9 o
$ \& X7 j9 l! m9 U z. _8 q/ s4 I( rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- I5 K9 m7 N/ j$ \2 ?% }+ c+ d( \
, p7 O- {$ U% m( S9 P. T0 r9 cmv owasp-modsecurity-crs /opt/tengine/conf/( ]0 ]/ [/ w8 [2 t8 G: o/ k
) U. e5 x. J$ f% T# U* a% jcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
- C7 {6 q- Q7 ~! J$ U2.启用OWASP规则:8 f( }4 L' i1 c( F. E. F: c
) E" ?( C3 K/ |2 V" j0 `7 @+ U5 y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' L) X' e( J( ]$ n" W3 I9 M9 K2 W# L
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" M; g- F2 R2 p2 t$ f. ?2 `' J! D$ V# T) K( k n, d
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 u0 D0 O7 [6 g1 i
# {9 G! g( m+ s- jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf( H$ w' e2 y! W6 {. X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" ?1 D* [; g9 n' wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf( i$ R" P& f0 Z' D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) u) g0 v8 y4 y9 Y2 c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf( [0 {* c( \) l
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 D9 s0 F9 @& `/ B* oInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# a8 `$ G3 k9 I, F8 L. P五.配置nginx; X% ?4 F9 n) {5 v8 C# }
. ]+ H( `" Y1 T1 T( U在需要启用modsecurity的主机的location下面加入下面两行即可:
# j9 j. K1 o" @' N3 `2 x% V
1 ]. t' v% D8 G4 d; B QModSecurityEnabled on; 5 W1 T' \) q1 i, S1 n
ModSecurityConfig modsecurity.conf;
6 f4 H" I) @5 v9 V9 i: N下面是两个示例配置,php虚拟主机:
/ X) i: C; e) h7 @0 _; p
1 c! M+ w* C4 B8 e' s Eserver {9 k( {7 N' E4 ~5 J3 D
listen 80;+ P* {7 h! I+ t( x# l
server_name 52os.net www.52os.net;+ b" Z) Z" w8 l+ N
$ e6 d+ i0 p) x
location ~ \.php$ {
V: G5 b) y& E; y i ModSecurityEnabled on; 1 K3 ]2 Y6 O# h! Q$ t
ModSecurityConfig modsecurity.conf;0 }, d8 G a5 u0 j! l
( F2 B v8 `- p3 q
root /web/wordpress;
/ f9 N- L: I# Y' L index index.php index.html index.htm;3 \ w( C' s( Z9 L' T3 z; j
, Y U2 s9 U# B$ X7 w* w5 Q: ]( P fastcgi_pass 127.0.0.1:9000;4 I. i3 [, ~4 N3 W' w
fastcgi_index index.php;
/ `: B' n3 ^% |5 ^& `5 I R fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 f: E7 }% X' i8 B: Z: ^$ P5 \& A W include fastcgi_params;
5 d' P5 V+ {$ `# s) k }
6 n7 g' b+ t, K3 I }
o& h4 Z) `" Y) Iupstream负载均衡:
3 m2 w1 T- R4 t2 e/ s; l$ C: V6 Y ^2 _8 L9 W- [
upstream 52os.net {
0 s1 }; r8 K& l2 p- j* B9 ? server 192.168.1.100:8080;
" `# I: b, ^/ h server 192.168.1.101:8080 backup;
$ p, A' T' W+ {9 V5 r}
* Y$ ]+ s3 J+ S0 A& V# ^
# j/ f" \. U. t' W! A; F J3 Fserver {
$ T, D. N* i. R- s, K0 rlisten 80;- J/ x- D2 F9 \# O
server_name 52os.net www.52os.net;7 J: S4 j) y2 F5 z! @
" ]+ X* [% t2 a8 x2 D$ [/ L v! ]location / {
& \" Z# N0 `1 I ModSecurityEnabled on;
2 ]& u. w$ F" P% M: M: c% N3 s6 n( N ModSecurityConfig modsecurity.conf;
/ e U* S! G0 c, _$ v# W% J$ x& O" k$ c: w- @) o$ z
proxy_pass http://online;
" l0 d: y8 P2 J8 N! I proxy_redirect off;$ F# M& A$ _. ?+ _
proxy_set_header Host $host;% K( n2 p8 I- y7 c( h# A
proxy_set_header X-Real-IP $remote_addr;2 C& O- h f0 {# q6 P& J7 @4 h
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 `* j/ P y7 ^, ?6 N& D- }
}
5 t0 ~! R: N7 Y}* v0 P! I( s K, }4 \
六.测试
3 W) O% B$ R/ b$ h, v3 q q( K" x) e( _+ R
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:9 w! z: V o+ G4 M' C3 H. t P7 z
4 c6 b' P9 e3 k9 ~' J! k& G
<?php
7 F f, z/ U, l5 R: U) c. ~3 G phpinfo();
6 D* |( D, b, l" p( _ ~, V?>. b0 X0 c6 O& C. B* H6 p$ ?- J
在浏览器中访问:/ P$ S; u4 D9 a: N8 ~5 F; G" I+ r6 F
! `. t3 @& X) X0 Z* dhttp://www.52os.net/phpinfo.php?id=1 正常显示。+ a2 T y8 x; W/ z
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。 c' }7 P7 T7 v( p
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
% c. O2 u9 A& R/ o$ x' P说明sql注入和xss已经被过滤了, m7 b( F( @! K$ l# U
- Q/ g% B$ H; D4 p2 v. R4 Y' E
七、安装过程中排错7 }/ R6 O1 G5 y! ]# ? U. M8 R
. M/ ^0 n* R+ `7 T* f. x
1.缺少APXS会报错7 U9 c/ K3 Q5 L1 m x- Y& c
3 i. f1 ^9 s4 r& s% X7 l0 j
configure: looking for Apache module support via DSO through APXS0 {6 }7 K! c7 O( e! u9 J4 {7 N3 G: V7 G
configure: error: couldn't find APXS; W2 H1 B9 t6 o, O: U* C: ] o
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) \+ e( X# R/ R5 M! j+ I# V
解决方法:! e. a! J3 z+ `& m5 i* T) k4 T0 E
) O1 @- U1 K, M& z. e
yum install httpd-devel
! _; b5 a. [# V; Y2.没有pcre
1 M& Y2 n: @3 D% P5 X. m- ]- \$ U& J. W: z
configure: *** pcre library not found." e2 Y/ R, ?8 M$ k
configure: error: pcre library is required
' O' C9 v, Z/ J1 R' ~8 z. A$ L解决方法:8 f7 l5 V- V& w, c( m: |
* o9 h6 e4 c% X5 q8 {
yum install pcre pcre-devel% t) _2 Y+ n- C1 d% d& Q2 J
3.没有libxml2
m3 @' z- u( B9 j6 v1 n7 J$ t$ a0 A2 k/ S
/ A8 A% a0 K9 w- N X/ n1 x( @
configure: *** xml library not found.2 ^# b4 s4 ^/ I) B% S' N$ e/ Y z
configure: error: libxml2 is required5 e' J! n+ m: ?8 V: v6 B
解决方法:
2 Y4 _ ^0 h" r
4 |, s' O" b' D' ?! h* T( oyum install libxml2 libxml2-devel) N& J! M3 k p; L, p$ i, F
4.执行 /opt/tengine/sbin/nginx -m 时有警告 t8 @6 c# K) W) ?* m' M
4 C& X7 L" v4 F l0 [Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 G3 J7 H; d; d+ b& w( Bnginx: [warn] ModSecurity: Loaded APR do not match with compiled!) |. s7 U4 K# O5 G) d, Y {4 h
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
$ h* V8 K* H3 n1 |* h% u' l# _
5 u! E- G: F& C' p% p: u# O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' S( P- J- L- H2 Q: Y: K7 H8 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( g% t( H+ W! {7 v
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!' p; G7 _% I$ i. }1 `- R+ ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% b9 {" Y: L5 ^# j+ P5 [3 }5 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 p. W: y) S7 w- T* H* ?% N' A f2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ [6 z5 k" h; F2 n3 b7 ?- [
解决方法,移除低版本的APR (1.3.9)/ n7 [( k( H) m! ]7 R: A
( M0 j6 j! q" k
yum remove apr
& e; h( h: N/ M5.Error.log中有: Audit log: Failed to lock global mutex5 U- D5 G" Q" y- ]4 I) z
- r$ U& c/ z) H4 W% V0 E: O
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
6 w0 W. A, u- A& F* T& f% z; i8 Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 J( [# X& W" G8 r0 f# J' n; ]2 j
解决方法:
+ D- b2 Z. F8 Z7 h; o* ~编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 t2 }3 x7 o N5 H' |' F
8 y% W6 [( ^/ F7 Y5 G) F- `
SecAuditLogDirMode 0777
. N6 C" D# `9 v& ^3 jSecAuditLogFileMode 0550
, b \& r6 W F( O6 k- s* i/ DSecAuditLogStorageDir /var/log/modsecurity T6 M! x( ^; y" v8 S
SecAuditLogType Concurrent" b: j; V1 A" Z% z
参考文章:
3 M) u- q4 U+ Q) G+ _* Lhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, J. E' G1 C& J1 v! ] d
http://drops.wooyun.org/tips/2614 |
|