|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 l9 _0 d, y [
/ w8 W e* f2 P" t; f: Z一.准备工作0 _. z$ e, B' U9 {) q
9 T0 F3 [/ }: z, e: J
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. F( x4 K+ F" e1 y* e. `8 q* f6 {3 p8 M
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 d; `4 m) a8 ] f; p- c
6 m- O4 j0 g; e# q( y6 S. Omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
' E% A+ j% m: N) c: C
* s5 ?5 [( Z! J* z! C4 E. ^OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs2 i$ ?, j, h0 ]$ M/ t7 [& y
' e3 _( L1 D0 v) O依赖关系:' T m+ A+ q' [7 Z5 _5 ]7 m
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
+ ?9 C9 P* {8 r' V. ?' H; }2 y8 L: ?0 h; P( x6 d% ?) {
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
! S0 `# j: k. T8 F; Amodsecurty依赖的包:pcre httpd-devel libxml2 apr
, A* ~+ y$ R8 B; b$ Z7 p' ?5 D; L' S6 z( ~. s- @( m. S" {
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 r0 `( b. W! x: t K$ S! d% ?二.启用standalone模块并编译
3 U: a: b" S; j( h4 i: d- y$ r" ?1 K" x7 e: l7 e% ?9 e
下载modsecurity for nginx 解压,进入解压后目录执行:
: f+ y/ k, a5 Y- c; ?
1 h$ m; y7 d6 W# x9 t./autogen.sh
& _% p+ _) L+ t4 I9 u./configure --enable-standalone-module --disable-mlogc
" l7 S' r; D* t, y8 Cmake
3 q4 l2 r8 Y. ` T& z( E三.nginx添加modsecurity模块* |9 s' }9 K |9 _
2 P) E( f+ C e7 o5 x. c在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
7 k- w* A* p0 P9 ^ Z8 s7 p9 V. k$ R1 Z, s) s: i
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
( N3 S5 u3 ?6 Amake && make install+ w+ {$ p2 a0 a# ]
四.添加规则 T: f; G" U4 p, v* U+ J, U
& H. K' |0 }( B4 q6 I9 g- ~
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 a1 O0 w) x' e0 Z3 Y
" s- K" m8 @% X Z' \- z% C% |1.下载OWASP规则: f( ?: j* p2 D& M, E F g9 o
& J4 t( n( X+ k- G6 M* Lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs& t/ |1 i, {8 }) g. J5 S" x
- d; U; X- T2 ?: K" a) _2 V# @
mv owasp-modsecurity-crs /opt/tengine/conf/
7 e' m2 f& B/ R' K; P+ ~8 o6 P% S8 E% }: u* [7 Q4 M
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf8 p6 J! l R! q
2.启用OWASP规则:
3 S# H6 ^, I. R# R( }& l
b7 v. K6 c- T! E8 a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
+ _9 l3 D3 @6 o( Y% @" k0 ]
/ a. I2 i. R7 U, ]& |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
) Q; j: l6 {" D
9 n5 W% @0 E8 t. dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 i' P3 k x) S0 j! k6 u: j' @" t
6 T L* x! D( ~Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
7 N; ]/ |- a2 U/ g5 {8 m8 Z* I$ pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
6 H7 |4 Q$ ]' z# p1 x fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 _0 e, j, D# g3 [; s# X aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 o& I1 b! b5 T8 hInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, G1 L: O& | ]! K( y; C8 F0 R
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
* O4 {/ W2 @# |8 v* |' U4 o AInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 s+ [( l2 Y, `5 y
五.配置nginx
) c( M$ Q8 Z/ F+ M+ S
' t( w+ V: B4 [. R在需要启用modsecurity的主机的location下面加入下面两行即可:
7 C, G6 y" x! s9 l+ l3 J- Y3 `6 ]: C
ModSecurityEnabled on;
9 w$ r* B" O3 d1 F/ |- j& _5 tModSecurityConfig modsecurity.conf;8 K0 g& R2 ~# `+ X {# H
下面是两个示例配置,php虚拟主机:/ d4 z4 C+ ]1 Q
' _( T% i, G; ]% v8 a
server { z8 a4 w+ O& r$ ^9 R% A
listen 80;
! l2 C9 ]$ }! s( { server_name 52os.net www.52os.net;' M$ a, W/ s9 ?2 h4 P
# o) P& N6 [( S% @9 j location ~ \.php$ {- w6 D% W2 J3 C/ G& l. k( t
ModSecurityEnabled on;
1 E2 P$ A! q- t: O* o0 R' E8 U: w ModSecurityConfig modsecurity.conf;
$ `4 B6 a, H$ N% `& r
7 A* P4 u" x5 C0 H) h* ?, `, @ root /web/wordpress;
# e: M# ~4 a7 D1 b6 { index index.php index.html index.htm;
" i, e& I' F' O4 c0 \& v 9 X( g G" S3 C N3 D
fastcgi_pass 127.0.0.1:9000;3 A! a: k8 g! W9 K" A! h2 P
fastcgi_index index.php;
4 ]& r- v7 t3 g' N9 t fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;2 I* T+ v/ _3 l6 Z
include fastcgi_params;
+ r7 ]# T$ _/ C# k- J5 b }9 l3 P+ r& ~/ ]% k3 H
}5 ]% B; G2 ?6 g) E% o: e
upstream负载均衡:
% z9 Z `: S( A( {2 @
1 Q) ]& S K4 v9 O/ x7 Qupstream 52os.net {
: ^$ b6 N' h7 x% X9 l& q$ t server 192.168.1.100:8080;" D) i3 O" D" W. b! y
server 192.168.1.101:8080 backup;
4 V2 E' A4 j8 y5 q0 O( a+ w% o}9 I" M+ u# \8 J2 @$ M; e$ N" l
) x. ~$ n5 r8 ^* K3 @3 R2 b/ `2 Xserver {1 w3 K2 j3 i4 i7 m5 Z
listen 80;/ G( A3 [3 `* D6 R5 P
server_name 52os.net www.52os.net;
& x5 ?% ^2 a3 p4 u9 S0 d4 d& d, T. k& l
location / {. o! r% i- w: V( t6 S& {
ModSecurityEnabled on;
; q9 `3 U: {# M* C! H ModSecurityConfig modsecurity.conf; 4 S6 [: h: h4 b! h0 {/ h6 u2 S7 e- r" M
. e( @9 m+ D/ h0 Z* N+ O" \0 W
proxy_pass http://online;
7 W+ W! a! k9 n/ z3 f8 M; \" a) Z proxy_redirect off;
8 L8 v5 K0 T' B' K8 G proxy_set_header Host $host;
9 P ^9 o; V6 I! n$ Y proxy_set_header X-Real-IP $remote_addr;4 o( O5 E" U3 u% m3 D. N7 j
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;$ a4 p: e# ?3 D4 f
}
! S: U/ N9 |7 L}
7 _4 q3 d4 a7 f0 {六.测试# p, H/ i9 Z' a6 X2 Y8 V
% w o" T( I8 w/ U: _我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, s& V( c% v' A# p2 o
- t+ o5 J; B8 n$ d7 |5 ?) y<?php" F6 v7 h# N @& r* ~8 g
phpinfo();
$ G/ N! `% \2 q' P# R! s?>
% W6 T0 `1 p M& B/ J在浏览器中访问:2 A2 f: ~, R/ \ Q- R. k# V
$ ?/ E- |* ^3 T1 w9 ~. {; L
http://www.52os.net/phpinfo.php?id=1 正常显示。0 p; x( u/ h0 q3 `' P+ H( E9 [
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
3 R `( Z' L9 b8 s$ xhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
! J `& W2 Q- C V7 Z$ `5 H说明sql注入和xss已经被过滤了, B. U# \; u+ f) r; M& A9 R3 F
6 v2 C5 T$ K; \$ b1 m! o, N5 G
七、安装过程中排错2 E0 Z/ k1 u. a) C
! g7 I" m4 f6 U/ F1.缺少APXS会报错
- E$ r0 B# r5 Z: t* f
2 W) B6 A$ w; b/ K4 t$ M. fconfigure: looking for Apache module support via DSO through APXS4 Q5 v% b& N0 l8 F0 F" j! f! k
configure: error: couldn't find APXS7 Q: D( ^: G7 v0 ^& r! j
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ {+ v1 X! o K% @, `解决方法:
3 V& D! q5 f9 b) E. d1 u/ u6 S9 J' g; M3 M
yum install httpd-devel) @) U% v4 o9 q; n( Q# r
2.没有pcre5 u; u" i7 }* i* a
( P, }- W1 z( f- F: N! y5 fconfigure: *** pcre library not found.4 n" D( h. s' p2 C. c7 }
configure: error: pcre library is required
1 U. C$ P3 V) B# v' f2 }2 \解决方法:/ ]- y3 p% D" l% C. t
2 C& ^ N+ i0 x% b. k, u# |
yum install pcre pcre-devel
1 D" A1 T# l* N& m7 d3.没有libxml2. @$ V8 V+ _3 H) {9 M% `+ w
8 r( }/ y1 d5 H* ~$ `1 ^( f
# h P9 [& c6 b; T+ Xconfigure: *** xml library not found.5 }, ~( S7 y) I9 S m+ c& w! \5 q
configure: error: libxml2 is required
f+ b! x R+ `" R( q( Y解决方法:
6 E' ]- b* j4 ^. i/ l1 B5 v3 r# F
( H. u6 D, C1 E8 W( Lyum install libxml2 libxml2-devel
6 | M1 h( q4 P4.执行 /opt/tengine/sbin/nginx -m 时有警告( s( o$ l; A* V% W. q
3 t& K% K% c' i! ^
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' m* Y+ P4 p" Z- t; q( j
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
: P a. j. x- ]原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
" i3 Q! d7 {7 i1 o1 n* m/ x! ]# P- x6 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.! m/ Z4 @8 f" m0 W" y8 B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( |5 e. n5 y7 _" ]: A! j0 _3 C# ~
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! d. x7 d! B0 `) E0 y- X, x" ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% S, `$ \, C7 `% j. {" D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ p! F6 {% `# y9 e, G! i
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 [$ g+ r8 A. c: n$ ]
解决方法,移除低版本的APR (1.3.9) W' t0 F; M6 @
! t3 @' H, O4 `8 j5 `0 v: zyum remove apr' T! B% D' d0 Q3 s8 {* \$ M5 H# s$ q: X
5.Error.log中有: Audit log: Failed to lock global mutex
. |" F5 y; d; L) f
- I4 Z8 ^1 V. S2 x D2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock - K& @# `) e2 c1 V4 [/ Y! W$ A
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]7 z+ `- n4 X) @9 e
解决方法:& K& {/ b- n! u5 I8 r
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ R3 [1 B9 M0 ]. B% h" V4 R0 n( O8 x) [- o; T1 w2 G8 b
SecAuditLogDirMode 0777& t! G* m/ S- y- R* e! ^4 _5 H- s
SecAuditLogFileMode 0550
- S5 c. m0 I% h- G5 [- o. w0 B0 \SecAuditLogStorageDir /var/log/modsecurity
) K+ Q! ~3 o9 g% @4 KSecAuditLogType Concurrent9 b8 e+ q) X5 L2 |% n# b9 k
参考文章:! b1 f/ v1 @& f3 k% r
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: z( o. i4 c: {2 T. b% m: }http://drops.wooyun.org/tips/2614 |
|