|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
3 Z( A, a$ |1 S; e% |/ J
3 R" s# l* }& v0 D" L一.准备工作
9 M6 g: F/ o$ ~, O8 `) t
]+ d, I9 [ u, H/ Q. Y/ o8 d系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0 s* e" g0 v+ S! [ \ b6 O
/ u, W m7 L* G* V {3 r2 Xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 ?1 j: D( s; ?5 T
& E$ i* Y. Z: i2 A& K3 i" Cmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' Q4 n+ X# R% A+ ~1 o
# p7 `5 m5 j/ h1 @/ y& \OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, ?3 d- {+ ^: T. c& q; n+ {. H9 k
* D% G) M4 J# J% ] E依赖关系:5 Q* I1 M3 V: u; F0 B6 x9 e
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( u' r4 j# ~4 k- j0 [( b0 m' `5 {. }) ]- \- ]
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel3 F% D" ?% [0 X, N( u
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% M4 ?% Y+ ^. x9 E @: t( R
4 s4 ]: u c/ Lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel- R& O6 ~: D$ a1 ~4 M$ O
二.启用standalone模块并编译
# F& ]6 }. F( |) V$ s/ c. R, l* C; w: ^5 E8 x: }
下载modsecurity for nginx 解压,进入解压后目录执行:' ^" E& T5 a9 d, g/ }3 I9 ^0 A7 f: f
* l: Y% Y' r7 _# V. d4 P# B./autogen.sh
; D+ n `# }9 N) V2 I7 |./configure --enable-standalone-module --disable-mlogc- n" q( q1 a; V& u' Q
make
& R% h' h: Q$ Q/ b+ z ?三.nginx添加modsecurity模块
! O( {) ~, E7 u9 y. y) M, @
) b8 o# S( `. \' _% [0 J, E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:" u: h: Q0 w+ }* ~$ B ?" k$ b
4 p5 R; C& W: Y& G' u) v
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 H- w6 C5 e0 {# P: a: A
make && make install- C4 [( c& s9 }; Y) _9 u1 J
四.添加规则0 j9 ^* z) b$ _$ P
6 a+ n0 }1 R2 q* w8 O' C) o/ kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。7 C' A6 t/ f% H* ^0 A* Y9 p9 k
( c1 f& |- F0 P9 [1 N& w3 ~1.下载OWASP规则:
) A- N3 ?7 Q) Z( E) X; E* Z3 N8 `: [, f7 O
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 W3 _( z; i2 }2 d4 c& ]
]% p5 M6 ^, t: t$ y$ f: P
mv owasp-modsecurity-crs /opt/tengine/conf/4 f8 ^) m7 U$ k4 ~
' P; m7 ]2 f4 H/ d0 Lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 ?5 b: j3 x `' i! V: w4 ^# t
2.启用OWASP规则:; G( k# `, p7 f2 a
- c* B+ A! m# B+ N, E1 P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! [$ z6 c$ R# \1 n6 u
% `+ O/ `" W$ l3 d+ Y! a编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 ?3 j: `6 a! L) b5 H& G* B1 m6 R
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: O" R R* c: V7 K2 K2 r M8 I0 R" j6 k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* P) e! V3 _! O. T
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
7 x3 F4 ?/ I4 o2 g$ d. c, |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
3 {: r9 R0 V! |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' C" ~, s( O! e# v! ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 V0 ~, f# _2 V" G- J% YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
. B, n! L6 P) uInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' [( E2 P& `6 }0 j' p五.配置nginx! S. [* `2 q. _' A6 A7 o, e
" a8 Q4 `" S# S& V: d0 V6 S
在需要启用modsecurity的主机的location下面加入下面两行即可:( k$ K' a( L4 i& `: D3 g7 \$ G
9 r5 e2 v& i* I- O$ ^" Y) B$ |ModSecurityEnabled on;
! P- H# [& z; \ModSecurityConfig modsecurity.conf;5 h; |) ~. @& y1 X D& }
下面是两个示例配置,php虚拟主机:
4 y. h- S; Y" B: @: }$ J) Y0 o: P. X! J! L
server {! `1 q1 A0 y: v+ S0 Z
listen 80;3 V" S: t6 r6 k. H( k+ }) j! g
server_name 52os.net www.52os.net;
9 h4 C, K) ~. @! ]& v1 ]# H $ f5 W+ Q" ~$ d9 f: x
location ~ \.php$ {
5 A6 F5 z6 p z V1 ~' X ModSecurityEnabled on;
; ^! k# R/ A% M0 k2 \8 `# M ModSecurityConfig modsecurity.conf;1 z% G1 \: d; Y; t2 C
) L8 L$ l1 C* m root /web/wordpress;
! x* i/ M( R$ M4 G* t index index.php index.html index.htm;
3 x- e9 m) k" q8 c: n; v# i1 e
1 |* U P* |7 u/ t fastcgi_pass 127.0.0.1:9000;
( \. g! ]# [, R% L0 \. m fastcgi_index index.php;4 b$ t+ ^$ U! A+ d, ~1 h
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;) u! \4 V' c, J% L; U
include fastcgi_params;
" N2 Z3 }7 Y; s% X. H- {) F" F }
# D! D. ]$ [: U- U! ^& @ }
' C2 m0 y+ t6 w8 U+ h5 H4 Fupstream负载均衡:/ H8 T3 ]- e0 s6 R U, v
* |" m4 e b- q2 z3 bupstream 52os.net { c+ S- |8 u$ e' K/ |( n( I
server 192.168.1.100:8080;/ ^7 y% t) R/ y! ?6 R" U
server 192.168.1.101:8080 backup;+ z/ _" V4 ^' O; I( A" Y/ @
} E3 p$ \0 |3 m. i: V1 I& e* P
; k" W: t( o1 ]server {
7 V9 e$ Z) d" k* J$ i# l0 Ylisten 80;
% r: @6 D% Y) `8 o* x1 sserver_name 52os.net www.52os.net;$ N" T, h. y. t* l- ?3 T3 c) S
% u. D# r7 R2 Q! v
location / {; o3 ?9 x$ u8 S0 W1 ]
ModSecurityEnabled on; 2 d4 a+ {2 x; J
ModSecurityConfig modsecurity.conf; & T+ S7 M+ K; O8 ^
; [% k( ^& J* O `# `; ?, h4 E
proxy_pass http://online;
( B2 ?& m. s' G# l0 D$ W/ c proxy_redirect off;% _& ~* m! g' M7 I8 R: u
proxy_set_header Host $host;4 }, y8 D% E+ {0 F
proxy_set_header X-Real-IP $remote_addr;
) x8 l. B- k7 G x9 A proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 G: Y2 ?: e0 q L
}; ?: _4 ?- w( q3 d
}
0 Z6 ^, N) _8 E7 |六.测试0 F! O) ~: O. X! L& M+ r, r) ~
+ i- D5 L" T5 E; h我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ ?3 |5 [' z+ ~1 c, |6 ]4 c: N: x1 L W
" I3 H( B! Q1 A5 B* _8 l<?php# @0 o7 f1 W( N I, `6 Y5 x
phpinfo();
8 R% Z( o; s+ i?>0 D. P: L, @8 ]9 I3 A* |
在浏览器中访问:) o+ N, \7 V* P8 g' p) Y
! V K! r, `- g+ R- ^1 S. phttp://www.52os.net/phpinfo.php?id=1 正常显示。
- {2 Y) P. _- ]5 T% H1 K% K1 {3 thttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
& `: t3 k7 }* J' Xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。( L/ A3 b; L0 T" k2 `! C
说明sql注入和xss已经被过滤了
u' Z0 t! p1 b* m' f6 M' f6 [. l& v' ^! p# V9 w3 n! |
七、安装过程中排错1 S i4 e |) @' r1 S, g3 g
4 z6 g L3 C* L l
1.缺少APXS会报错) c% c: p/ j; X( p) c1 }
3 V" g$ D9 S1 j7 Rconfigure: looking for Apache module support via DSO through APXS* }8 d. M( o( b8 f
configure: error: couldn't find APXS$ U+ ?% C1 a. s+ ?& K
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。! o' N- M+ a" ?3 C
解决方法:
, U9 F0 ?. Z5 w, }0 M0 F
) b3 {0 P1 }1 e6 |. iyum install httpd-devel7 t+ v" T2 B. L' s; J+ X5 h& ?- M. h
2.没有pcre
4 K* J3 g+ {' k3 x% j. R/ j2 a" p, C% L( s( d
configure: *** pcre library not found./ ?/ w9 _/ n$ I C3 I" ~" u# p: ^% g
configure: error: pcre library is required
9 `: T3 p7 \( ^ N! M. J解决方法:" N" [/ X: S7 d2 ?7 a( Q
5 Q$ }; E( B# r0 i% H. ~9 yyum install pcre pcre-devel: b# C& [$ ?" O7 D
3.没有libxml2
' ?& T4 t! r5 U n4 U2 H/ r e. s Z# Q
, O+ M# z: W3 ~7 X8 B- |configure: *** xml library not found.
- ]% W! B x1 O# } E* ~configure: error: libxml2 is required
/ K: N+ W! Z' X+ a+ _/ ?7 o+ d解决方法:
8 z; j1 ~8 _/ D# B( y- L- c: N' {3 n0 B- u; V( o X$ D2 `
yum install libxml2 libxml2-devel, |. H t) U: v2 W( x/ ]0 Z0 {6 U
4.执行 /opt/tengine/sbin/nginx -m 时有警告
) F; Y4 J% T! Q) p' ^) I8 O& b( I) ^3 {
Tengine version: Tengine/2.1.0 (nginx/1.6.2): s5 o, D! ?! r% q. u
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' Y7 ^" `' _; {9 _7 H: G2 y* R! T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log/ [: m. x) l) j$ N3 C
/ S) `. \* D$ W$ K5 J) x& U! \9 k! d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 X( ]8 R" f" i! E+ u. a: I3 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
' _& |7 K' Y: c. h2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!# [ A3 r" @8 t: a# {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
: |3 N5 K% D i5 Q6 p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 v! c' p G6 t8 |9 E
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
8 |8 k3 K. i) u% a+ d) D. m解决方法,移除低版本的APR (1.3.9)0 j f. I+ V" p8 U
- r/ P( u% z! y5 D4 Pyum remove apr# x3 w0 C9 k( ]/ f' A
5.Error.log中有: Audit log: Failed to lock global mutex
9 L9 y% y9 ?7 M4 P& w @% s" b9 U# I; E6 I/ h9 M7 O4 I
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ k4 K6 `% v. mglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 ~ D% X3 t( M& O
解决方法:9 B* @* H: ?- a5 L U# P
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 G7 N: {. \0 ?! g' g
* V/ D6 N C: g
SecAuditLogDirMode 0777 v7 w) e& v: g* E4 D3 A6 G
SecAuditLogFileMode 0550
4 H* p% w H$ W6 X0 ]# l. A+ n' TSecAuditLogStorageDir /var/log/modsecurity
+ C% G7 L: v& ~' L5 J: o+ |& nSecAuditLogType Concurrent0 b! n4 c& Q5 h& Y% J
参考文章:
6 T- Z# c5 L$ P H1 Vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 z. W3 I0 q4 X& |; ~http://drops.wooyun.org/tips/2614 |
|