|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ e3 v9 Q3 X5 P4 {' t
" v' M; C4 m& [. s& y一.准备工作" `! G2 _( y o1 `; a$ B. `; p8 o, E
8 G4 |5 T& y8 B* y8 Z1 d4 ]系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
! W' n& b" D) w; f& u) ]1 H, d0 q& y2 T* t; e( ^ B" V* ]5 z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz& V5 D- s* T7 j. S) ~4 m5 A7 P
/ @+ r+ {" z1 c7 x( @. b" G$ g& V& |modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 q K/ E( E% @0 f* X% @8 t; ] }
/ w5 S) x2 J$ `
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
0 V3 c2 S) @0 i1 C4 u2 {% @' y
, d+ C& X9 }7 q/ ^; z9 o) s依赖关系:
' }6 L1 g, Q/ E6 b# z! Z) dtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:1 m- E; ]2 ?$ m3 L
9 F) X: h3 S, e* d
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
_ U! x2 V6 J8 D3 |modsecurty依赖的包:pcre httpd-devel libxml2 apr$ T( z5 D, |& R2 @
& c; R* _+ E3 [. q, vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel7 a* n) Q3 r1 {
二.启用standalone模块并编译# w4 S' O9 ^* [- w/ h: Z0 i4 S
+ f) X( y9 c6 u i5 K
下载modsecurity for nginx 解压,进入解压后目录执行:
7 s5 L- M8 U: I! t) }; w) e( B4 [5 [8 s q' N/ n- j
./autogen.sh
: i& F7 A- W) P H./configure --enable-standalone-module --disable-mlogc
. E4 y [1 c2 ?% U- S# q( j. Kmake : i/ d# V( B( l, `. X- G7 B: r
三.nginx添加modsecurity模块
* E# q% G+ n i _
4 }1 c% _, r& i" ?7 r在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 M* K9 z- {$ i1 B u9 `
" z8 P+ ]! }7 O1 w
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
" M5 r7 n& I" r( ?, [0 W% Lmake && make install
D$ Z7 Y) k+ w$ \) x, C b( Q四.添加规则3 z6 ~$ Z8 q N" n
7 a3 D+ h A. N) W' f, ~6 u
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% p3 A. E! n- X4 O3 J
9 [5 A5 x8 K1 K d
1.下载OWASP规则:
( \) g$ u: B" Z
# `; A) I: e# `% f; N2 Ugit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
* V* B: V: h, y; c2 F4 f& t$ A3 M2 ]$ o5 v; `8 p8 J: W# I2 b
mv owasp-modsecurity-crs /opt/tengine/conf/
( G+ q0 t2 n7 ?' h0 B6 j* Y& ~, q4 a/ c" R# J' ?( U
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
9 x$ l( S2 x: w2.启用OWASP规则:' y: b* O5 w7 ]5 I
. H8 F+ ]* n$ j9 w复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。: o9 T! B# A/ e) A* B& l( x3 _& W4 W
5 Q8 w" x( X+ F$ D/ f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on/ F" m$ M5 v) i) X3 ?( R+ |, F5 o2 s Q
5 x& i; ^* c* ]+ i0 B8 B% O
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
) P& ?- O: v8 ^. h3 H8 }* m2 T- u, \2 y6 }0 a L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ c. I7 K% P% wInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( N& Y+ T5 p; V$ |9 G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 d% k5 V* q: x. w3 ]
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 l2 a) F0 V5 R' z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
0 @9 d/ @7 Y! f( x% z$ XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: B/ D4 J: i0 b m' TInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf" {5 K% {- ~, ]
五.配置nginx
9 J2 T( N; L6 n8 w
3 r1 ?. t* N* w: r! j. O: O在需要启用modsecurity的主机的location下面加入下面两行即可:
5 q- E! |; I0 Q& Z. k1 ~; [, P% N4 r3 w- h0 d6 a+ g
ModSecurityEnabled on;
" P1 ^! S" c2 }/ r/ u& vModSecurityConfig modsecurity.conf;+ F) m; M, @5 @1 }" |. i9 R
下面是两个示例配置,php虚拟主机:+ r# H# X, j0 w) p7 i2 X! q C
9 }4 w3 L; ~ q, G. I( A
server {& u: Q% V- D6 F1 }0 \/ p
listen 80;: D; g8 J; } A! U
server_name 52os.net www.52os.net;) R& j8 V/ L9 N8 g7 z: H* |
6 T3 M) {+ A a9 m5 V( a# D0 s/ `
location ~ \.php$ {$ J' ?0 @' b0 r8 @1 U
ModSecurityEnabled on;
" c, O! ]4 t: H ModSecurityConfig modsecurity.conf;* d9 u) _( N% H3 R4 c
8 u$ }+ q3 I o# F root /web/wordpress;* I, s6 f5 F% u8 x! v* l
index index.php index.html index.htm;2 p( ^$ G" ^' l, O8 N* Z4 [4 I
/ C6 n7 Q1 k' n/ h
fastcgi_pass 127.0.0.1:9000;
! M/ f. K! ~, X% `) o/ s7 [' { fastcgi_index index.php;
( ~/ d2 ?0 o* j- B( O& L fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;/ D0 a: {5 Q* s
include fastcgi_params;
0 [% m j( C( |9 h }
; T5 V' P0 L# Z0 ^$ I }3 w0 L! G' A/ v/ J
upstream负载均衡:- y: B) \8 X! L2 z( ~$ q! W
; @8 \3 v; @. aupstream 52os.net {
6 M" R1 o. a: o; M: a+ K& \ server 192.168.1.100:8080;) |; e1 f, P$ O
server 192.168.1.101:8080 backup;9 C) P& i7 F$ ?+ f! m; v3 z. y! F
}
. m6 Z2 e- J+ \! y
8 B6 [8 B7 f. N Qserver {* ~: U/ m0 i8 e7 W
listen 80;0 p( g7 A) h& ? z* `
server_name 52os.net www.52os.net;9 c7 U6 B# |, G5 A$ |# ^3 C) t
. p6 t, W7 ], r4 m, ?9 o
location / {
% R; Q7 J0 Q8 D! O ModSecurityEnabled on;
0 c( n) u! ^/ Q. x- h0 z1 A6 u( B ModSecurityConfig modsecurity.conf;
|6 T( V' ~' [' l, H$ Y. Y: y" J9 d. L4 O
proxy_pass http://online;
) U0 I, G& @$ S& }7 Z, E# o proxy_redirect off;: B0 |3 e5 g2 t+ K" H
proxy_set_header Host $host;
" o, l1 k; J: v( z, J9 k G; \ proxy_set_header X-Real-IP $remote_addr;+ }! v, f4 A( n1 |0 c
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
+ |, O$ c! {0 N$ w% p! G( } i u }
1 z. c N- |" L0 B0 K}6 P' h. N7 ]% z( \6 ~+ C+ U( l c
六.测试7 F+ O) s# W: b
/ G0 F8 Z# r! }& ?8 ~/ k" _2 ]我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& k! g* Y+ S& D, |4 h; f0 t
" L- P: e" y0 }; H7 W<?php
' K! q: A0 K! n phpinfo(); 4 ] x. R5 P+ _8 ^; e' P+ c
?>
% X$ w2 l6 ?+ _8 G/ P0 I& `在浏览器中访问:
* n, \* P/ H0 C, s! s- d! L3 j; l; O5 H2 j* o, n b
http://www.52os.net/phpinfo.php?id=1 正常显示。
! N8 `' ?7 p% _ phttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
( q( A! g/ y" {4 Dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. w/ T$ z4 Y3 v9 [7 E; {7 G8 n
说明sql注入和xss已经被过滤了
2 F. u' I5 b* m$ }0 d& n* i9 a7 H# b( k. k( m% D* ^
七、安装过程中排错
" I3 f4 A3 P0 ?4 F( _2 d
* L9 r3 \$ E3 m8 w4 p; M' z1.缺少APXS会报错
% P2 s; A) h, @- Q4 w) i0 A$ @, Z1 q3 p
configure: looking for Apache module support via DSO through APXS1 N F% L( {8 W3 a/ ^. ~4 G
configure: error: couldn't find APXS
6 }& r# t7 h4 I- hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
! q9 ?6 h' @8 G6 r5 c8 _! S/ d解决方法:
7 H- k( P! z- t5 v) O
6 i6 [7 j6 z: syum install httpd-devel
0 e' D$ U. \' C2.没有pcre4 P, A/ ?: l0 R, X
- @' g, |8 M) @+ L) S; d+ lconfigure: *** pcre library not found.
/ L6 W" K6 |3 |# T6 O( Wconfigure: error: pcre library is required" c" E! ?8 @. G9 Y# C4 H3 Z E# W
解决方法:8 h1 W7 E# o' T
3 {! ]; ~7 H: p; t ^5 A0 e
yum install pcre pcre-devel
1 i6 Z' g! C7 q3.没有libxml2
( I' U* ]7 `+ `! Z, l
( K" |* U2 K8 C2 k0 C
' N/ W2 V( O/ @$ y) tconfigure: *** xml library not found.: j4 z- Y4 ~& s3 x% K
configure: error: libxml2 is required
! B% t: K) N, U0 v8 n8 R解决方法:
) e6 \# k6 i% v8 W, X8 W( O p' E7 V: t V }. Q
yum install libxml2 libxml2-devel$ j6 C( ~% ?* C; U* R4 T
4.执行 /opt/tengine/sbin/nginx -m 时有警告
; @# a5 J5 _* A/ P; V# S
- a$ f0 ~' i2 cTengine version: Tengine/2.1.0 (nginx/1.6.2)8 }$ z" ^1 s/ F7 Z" e! ~' X; H
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!+ R+ N2 k% a; i4 `5 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
: p2 e* T6 [. i, ?( k' {$ K
3 o8 p- |" I2 N7 E5 Y1 z; M5 O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.6 M, I1 t/ {9 y" h1 _9 ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" l3 f8 D R) q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
) T/ v. ^7 U" X! [: i( f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ T! Z) G8 @5 H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ @. @" b$ e g. M0 [2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ P3 w- b- Y8 j. V6 M6 n
解决方法,移除低版本的APR (1.3.9)
: K. n" }& U2 p2 K% q, h8 ?# F1 G; @3 \ {% S, K
yum remove apr2 n& M7 \: I7 t
5.Error.log中有: Audit log: Failed to lock global mutex4 e o" l+ S* [( r
0 e: w+ [/ Q% }- q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
# x" x1 E/ |! B- S$ z: a& b4 cglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 a7 h; V5 @. j$ L
解决方法:
- b: K4 T7 Y. S. o编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:- {9 d/ X2 I: d! p- D% a" \
! t; z, I: P0 e; ]+ Q2 ~5 |
SecAuditLogDirMode 0777. [3 K2 `; g/ |7 K0 {# B; F: {
SecAuditLogFileMode 05507 ^& H/ I5 W! d9 X4 S
SecAuditLogStorageDir /var/log/modsecurity/ n6 J7 T3 U6 i; t
SecAuditLogType Concurrent0 t7 }( Q0 ]5 J& p
参考文章:1 p1 E% x3 E( h8 ?' I0 j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
8 n7 H* ^% {" C: D" C. y, \http://drops.wooyun.org/tips/2614 |
|