|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ d, y+ N6 }- M& U. w: J+ S* e
7 N9 L+ \; Z$ }& I一.准备工作
. e1 u% |: K0 @, S9 E s
/ K* F& q. v" L9 O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ Z* u+ r9 ?2 P$ \
" e/ @& m2 h' ntengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, b) q8 o! y0 i( r* |& t
1 \8 x3 T# p4 R& o& |
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
/ c3 `) R" t9 G ^- `
2 x* W2 Q/ J1 |5 [% f6 z$ OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: j/ E% ^) Q% ^0 A
1 R. _1 ?6 A- J. p: U依赖关系:
! k0 E. c3 [# ztengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
/ P: @) k) z( u5 I7 X. K8 G: [- { n1 T+ K
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) X% Q' t5 A. W5 T/ `0 _4 u5 s& k
modsecurty依赖的包:pcre httpd-devel libxml2 apr5 O5 \) C1 q9 V
5 e7 m) l6 z5 p6 h1 h& X: ~
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- `- `# {; V) |6 ?3 b- L二.启用standalone模块并编译4 j7 C. s# [* b, j: k+ e: }
* w% ^: y$ [1 d) J( g下载modsecurity for nginx 解压,进入解压后目录执行:
. ~: U" W: t& ]* i$ ?; C I% k5 H# b0 w$ ~) G
./autogen.sh
$ J/ K' k5 |4 Q8 ?" i./configure --enable-standalone-module --disable-mlogc& V7 K" a' x) P" C
make
. r5 J# C$ }( z) @! j6 \三.nginx添加modsecurity模块0 ?( u$ G0 @: L/ `
( ?7 K& M5 ~; t' i* {5 @
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
6 y5 s% g, i U, S. j4 q
# A- V8 Z4 w+ p; y' l/ \+ Z9 t./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
2 Y$ M }" S# }/ G, S' J0 Pmake && make install
$ u+ v& V# V' Y: q# j$ W四.添加规则9 n6 X7 ^5 q3 R
5 k5 h- }* [* }3 g& V$ C# x- a
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" l7 R8 t& k! J, y
/ r' E& U# _/ I8 [1.下载OWASP规则:
U; q. \6 f8 f8 {, k8 D$ t
$ i! j. K5 }6 A6 ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 Z% D6 I1 _7 D7 |
9 `# s- b) M1 i6 s2 S* cmv owasp-modsecurity-crs /opt/tengine/conf/
O* u' K2 e4 W$ W7 Q8 g& u& }' s5 L/ P1 `9 Z6 }$ |
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% Z# C5 G f0 t) q- c% u Q( g( f& l/ `
2.启用OWASP规则:
4 Q2 w ^1 G8 S2 y3 L8 T- n! f; [! k) Q: i2 a( w9 N" e5 k
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, o6 ~8 o1 x: K7 O) o. m- O y
. Z Q# l' e$ L2 A+ Y q$ O3 D编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, C- w' @) Y" s R' @$ Z" \6 E
* Q: b& `: d7 o5 a( ?! U& c' r+ Wowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
# x( V) n, e8 P r( Z' v' e- o/ v% T2 n. k) }& d
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ J- }' r, M q/ X" z' gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# n! Y x/ J5 T, k6 J& I- gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; L+ F d0 ~9 o1 MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ G d3 \# _, M& r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 x" c$ j+ k1 Q5 J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
8 a* n5 y' ]( \' m- yInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
; n' Y+ H5 w, O: w* R$ k. w9 E% }五.配置nginx
) _$ Y5 n! `& K# A4 V" D9 {2 F& a5 Y: z6 _8 Q0 ~2 J: E/ G6 n) O
在需要启用modsecurity的主机的location下面加入下面两行即可:
8 F& s, M/ Y9 Y) D
( m' ]+ s0 U7 ^* aModSecurityEnabled on;
9 N9 p2 A& M! C, q/ nModSecurityConfig modsecurity.conf;
4 @: D4 Q+ U6 Z& m* K& E下面是两个示例配置,php虚拟主机:
: r! L! w7 k* s8 _1 [; L' |5 ~6 O$ o' A) P- W* K" D
server {7 V( n1 q; u) C% t; ]
listen 80;
6 _; q$ _4 g5 P% R server_name 52os.net www.52os.net;
7 v2 C9 I' a) x" ]$ E 8 ^+ |3 A8 j9 `5 Y* E/ F
location ~ \.php$ {$ ~0 j! F9 W2 |, X7 Y
ModSecurityEnabled on;
3 J4 T( n# g% I1 G( g( U ModSecurityConfig modsecurity.conf;
# j( n$ {& I( W& j8 q
1 p6 _& i5 G. A/ [! O* a: Z root /web/wordpress;7 ?. E2 T: @% o- z5 U
index index.php index.html index.htm;( J# y. s. { ]- e; t! m! ^9 Q
, T! b9 f- x% F6 Y
fastcgi_pass 127.0.0.1:9000;$ ~$ G7 i' [: y" p! x% u# v* \
fastcgi_index index.php;
1 @0 q# R! c, Y$ D3 P; `2 R1 ] fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
1 T: P' a3 ^4 t$ I( z6 P' C9 K4 h1 R include fastcgi_params;# G7 R8 |$ C3 T5 P0 ~1 p
}
) \7 Q" P5 K D1 ~1 u( R% I* i7 [' R }
9 k! [" P- H( N; @) O6 nupstream负载均衡:2 ~( y) P) V: e; {# u5 e: b8 d# ^
" @2 H: x) h5 uupstream 52os.net {
: }7 r. Z! x ~2 X m; p server 192.168.1.100:8080;4 s7 o6 [7 C" J* a7 y, h4 I
server 192.168.1.101:8080 backup;
4 [- \1 X ?% y; K# `}
. {+ a, a. F+ y, g6 F! C9 @. K& A1 R. A% k- z4 \1 L& Q
server {
1 _1 X+ u s! S4 e4 Q& ?listen 80;
% Y7 k& \$ c8 d! v0 f! I3 p, @* O: Iserver_name 52os.net www.52os.net;9 @" v: k- _+ {& [) s/ C
_7 x* h% S% b( O* `! Blocation / {
# @5 [+ J# z- \3 s; C7 T ModSecurityEnabled on;
@/ X. [. T/ F8 J- e ModSecurityConfig modsecurity.conf;
% q8 x( `9 y1 n4 M4 m8 |
. S# r2 c- \7 K; l5 _) _ proxy_pass http://online;
* i" E- A* p- c+ F! x proxy_redirect off;
/ c6 c2 H+ _& U* H# ~8 j5 V* R proxy_set_header Host $host;
& O! o i7 w& U0 R3 [ proxy_set_header X-Real-IP $remote_addr;5 ^" D" E" w; ~+ H( @% u/ E
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
, [6 o" {1 m* X/ I }
% u7 p2 h% j' L; G( r# w}
( Y5 m( e; s* x* k* m六.测试
; m' L. ]2 t! V m8 l
6 ~1 _# W$ N6 r7 d, z9 V( L我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:- ]; q' }! _) m% d2 W
! S/ {& m) w& `- x3 A
<?php* w2 O Z4 ^! [5 k. s# G
phpinfo(); 9 W3 H# r* G. E% i. h$ c% C
?>/ S" N! C) }) V3 ^' X
在浏览器中访问:% y ?4 A* i6 j) A- ?
4 G3 x! S& H2 ^$ Z) C. R/ u
http://www.52os.net/phpinfo.php?id=1 正常显示。
4 [6 e' h1 X) }$ i8 rhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 w) P) ^! s$ Q, K1 k
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 O0 h7 b0 R9 R8 s说明sql注入和xss已经被过滤了
0 @! E {0 o) |. O6 k" v# u4 j; G: e2 f" ~7 e
七、安装过程中排错
T" S& B9 e& s" P8 L6 _1 s w3 O: c# s8 Z' Z: s
1.缺少APXS会报错
; k2 G; k( @( |6 J$ S
3 S& ?' g6 I. Rconfigure: looking for Apache module support via DSO through APXS
* M+ c( f5 u4 E8 k* }configure: error: couldn't find APXS: E" {" r( l5 e. ~7 W" l9 n9 G% t
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 j- W' ?4 l5 n# a
解决方法:
* H, W* ~9 R' A
( J1 o& B5 m& B; ?( Oyum install httpd-devel
; b6 d+ i( D- x5 C2.没有pcre
. u9 S0 ]% i- }) u! r
" H# w) H8 h0 I3 p9 L: V( F) L8 z' Bconfigure: *** pcre library not found.
& K$ S% O4 m# ?! ~: H8 e: xconfigure: error: pcre library is required
1 t4 |1 R6 S1 P+ F5 O2 W解决方法:
& G) F3 m, j! p
: B7 d! M, e% `5 g- v; l1 \yum install pcre pcre-devel: P1 H1 I- M6 n! v. d
3.没有libxml2) X6 w& N$ f6 ^# A% ~
8 ^2 S! V: u) H, B9 F
. k" l) R) y1 tconfigure: *** xml library not found.
* o- `" c ]: @& P% W0 i3 econfigure: error: libxml2 is required" g: j2 X/ f" a
解决方法:4 w' P! {1 z" k W# X/ u7 R8 Q8 r
. l! h% ~# y' C+ u. }yum install libxml2 libxml2-devel; d4 d3 u8 x( o4 e
4.执行 /opt/tengine/sbin/nginx -m 时有警告 c2 G. W k% D( X+ g8 p* v
& v6 b5 Q" V" `1 v; o
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
! P. ?; |+ I( U+ p0 O4 }nginx: [warn] ModSecurity: Loaded APR do not match with compiled!9 x' v4 ?3 ]: r* l% l0 g) Z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- m4 g, M2 L* I3 [/ c" K
' S, m4 W0 S' k ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* l! V$ W' L0 Y- x; r/ ~1 w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
9 [1 o. N0 v* i% X8 X2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
9 {" l0 R3 V# j- ] }5 O0 l! H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 H% V2 k6 J; d, s% k7 [% _# P6 v/ J0 _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
) B! i' j0 q" u2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.- O' c8 P) A9 H: N/ j
解决方法,移除低版本的APR (1.3.9)
3 J& O) V" w+ @! ?/ i& P
& F" X \& Y T' V: s, Qyum remove apr
, g/ T# {# t5 f* {* O" ^5.Error.log中有: Audit log: Failed to lock global mutex% {+ o" z9 b- n+ t! z
7 ~/ L" c9 i# V( t! g
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock & X# G) |6 U% c/ I6 @7 n4 x8 y
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 n, v2 Q. e/ S: U. f
解决方法:) U; k! w: g/ m# i
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:) W5 J8 i* G4 n3 `( X
7 C- `( X+ g! x# I1 ~SecAuditLogDirMode 0777
: \6 N8 ~6 Z- d' r% ]* OSecAuditLogFileMode 0550& a' ]* T" {9 y/ |# e
SecAuditLogStorageDir /var/log/modsecurity
2 w2 Z D% B: J" C. lSecAuditLogType Concurrent
1 H: {4 \2 O6 j. z; E( e) t8 @% ~参考文章:" v# ^. J, D2 v( f
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX! }4 H& E" c+ h& A5 x
http://drops.wooyun.org/tips/2614 |
|