|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。1 o( x4 v- R* ^4 X' {& B% H$ q* G
8 n* Z: g* i3 d( C) ?1 K5 e一.准备工作
2 X, C7 i9 M$ u( u" y T
& h/ a V: Z: ]# O- |0 D系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
, E z7 {, `* v& n" }8 t( G8 q0 W' M* Z' @. X$ I- l {
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 Y( z0 I% G! O5 W Q
& c5 F5 _% M9 R' {, N* B
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ b$ k$ [2 ]1 g8 l# u
6 o% I1 s9 C9 N5 n0 E2 ]' Z- R
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ L) @$ j# i m
4 H; Q% F/ T, A: x( F7 r
依赖关系:0 M; u+ L7 c7 M: F. x
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" c8 H# c2 K# d7 B6 Y4 N c' `6 O1 ?& b+ F4 N
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, }. Y2 w! U, W& |
modsecurty依赖的包:pcre httpd-devel libxml2 apr
# Y2 t! A, }& `4 H% ` a" h' C: o' q% M( k
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
6 c' J! \7 ^( p二.启用standalone模块并编译
- m: M) p! ~9 P
/ D/ ~4 b) G& m: N( X下载modsecurity for nginx 解压,进入解压后目录执行:+ ~4 q' v. d# h2 T4 l3 ]
4 g9 X. n& Y$ e A# S
./autogen.sh
' d! ^: a: e7 W& {* u./configure --enable-standalone-module --disable-mlogc
! Q& B+ z% y) l0 ^# T: h7 t* Smake
M1 @3 ~& l; J三.nginx添加modsecurity模块
+ R A; z1 Y. H- q5 R" s) W* J' D( p! F6 N. n7 e
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 k. m' P/ E3 b) s
( V: o9 o) D9 S- B/ ^* r1 A./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine7 W v+ P4 o8 t, o8 B9 u! ~
make && make install d7 s- {7 ?9 u) S; k0 H: `1 F
四.添加规则' ^1 k( a8 n9 S. g" @7 a
& n0 E4 T7 p" z7 kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 Y0 K8 p- K4 \2 Y! Y s$ r
+ f2 r2 W) T: D. @8 A
1.下载OWASP规则:
3 }' U, h3 @6 W$ Z: | b* d1 ~# V9 m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
" G, `. s) O# o; _* R8 ?" j2 L. W' [( @' n$ x7 w
mv owasp-modsecurity-crs /opt/tengine/conf/2 R8 L \" ?# G' V, \- g
" Y/ V+ m" ~# R4 d
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 E/ r. M. F _3 P
2.启用OWASP规则:
0 {3 p7 V' N5 I. p$ c; J. u! l
, v x" b5 [3 t复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
" C3 ?' y: ^( |) b! x# X: N2 e/ C4 x: D$ |& S4 r1 K: v7 d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on3 H" ?3 j5 T. m$ R2 i& R
2 V+ g2 ]9 U: D/ m0 N
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; R$ L; N7 ^7 g0 i4 `% v) ?
# V3 v: ]: q1 R: K
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
. U5 b- X( e; s, s9 T% gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
( ^- G6 k' T/ R% NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf- L$ U& y/ ?+ y1 G/ j, I
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
$ i: v T" }: Z8 o! J) v! {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
9 {4 \( C! u s9 c/ AInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ r# R+ |% E, Z/ r% P& Z: L. K' c# k
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' R/ {! F1 B7 P% a* l7 ^五.配置nginx
+ d; E. O: E1 J1 _1 Z6 f# P8 L; d
9 K {8 B2 e( G( ~4 i# _9 i在需要启用modsecurity的主机的location下面加入下面两行即可:
# x$ c# w1 W, u1 i( ]2 `/ x8 q" e8 x( f- t) o, l
ModSecurityEnabled on; & z# M) @0 k T) t5 b" }! [( t% q; e
ModSecurityConfig modsecurity.conf;
6 `9 o2 v( D9 Y9 q/ Y- v' b下面是两个示例配置,php虚拟主机:
4 f& l6 i3 G. I2 d" S" q% [: \5 \
6 F/ d: G. E8 } H/ X0 Y: Aserver {
1 ^! Z6 S" D3 u% m% { listen 80;* O& c6 Q8 P1 J5 L f
server_name 52os.net www.52os.net;
' V3 S5 d- e: _' S" j/ |
; L) z+ ^3 ]- I7 L+ F/ P6 c location ~ \.php$ {2 v* N5 D, @9 j" U2 w4 l2 g
ModSecurityEnabled on;
: f' E- |- a! x; I9 A& Q ModSecurityConfig modsecurity.conf;
) L: k0 P! G/ f! Y
- U. r" B3 c, D root /web/wordpress;
1 l7 n( k8 U1 [; `6 l1 K index index.php index.html index.htm;
y! T- D5 E; S5 S, m 5 U7 ?. C" T# _' d d, |9 V3 k
fastcgi_pass 127.0.0.1:9000;! W7 K; r" @8 t" i& S$ C
fastcgi_index index.php;) b* z; E, x: p7 g; I. g. I
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;( g3 W9 c) X4 ^- A5 g% T7 r6 |
include fastcgi_params;
; z0 J( h: y! k M; A2 W }) q+ u% b4 X$ P4 _ T3 e
}6 |8 ` C. b6 l, y# P& \# W
upstream负载均衡:
: F3 i7 C) m% _' |" x
( b9 W9 e: r5 S3 [1 E5 Tupstream 52os.net {$ Q+ J- X" A( k1 b; [
server 192.168.1.100:8080;/ U' R0 f; }, \: m# L
server 192.168.1.101:8080 backup;9 o# p( ]6 b- c6 o( P
}
, @) N# F, z! H4 y, Y" G }+ Y; n& w% b2 O' S- t, o
server {
- H& ~% E& a8 d, f! l( @* elisten 80;% P& l; B/ i! a; a
server_name 52os.net www.52os.net;
+ K6 _+ {% G+ Q) z7 U j9 I ?7 N" O& @. _
location / {) M( u* v1 J6 _4 R% c
ModSecurityEnabled on; $ Y( U* n2 |2 R# m% m3 p
ModSecurityConfig modsecurity.conf;
C1 D& b4 {% U1 x2 L/ s+ g# a0 x5 k8 k9 _) E) h$ a. q
proxy_pass http://online;
0 a* {% A( k1 a5 q& }* A proxy_redirect off;
, t4 M- q! k. T" U proxy_set_header Host $host;
. [9 O! H2 O% I7 j7 X) `5 c$ b proxy_set_header X-Real-IP $remote_addr;" {6 i, _; F6 b6 B5 d2 c
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 {; P/ H, [* q* h6 J$ o9 Z+ P }; `! a+ G$ b! |$ N( J/ Z( u& i- I6 z
}
1 v# K8 D2 r8 d- y. J# \六.测试
1 Q8 l+ X" G# e+ Q0 n
( `4 U. L2 |5 G% \5 ]% \% t6 Q我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- L/ G# Q$ _7 e P, U
# r* E; G' \6 p+ U8 n& v4 Q$ r4 f<?php
1 x8 U, f+ A2 s! p( x phpinfo();
( j5 D" o! o" ~* s0 @1 e/ l?>/ C( \) p2 j: B7 c. A
在浏览器中访问:* g3 C2 s1 y5 f7 s& B6 S7 i) M
+ Y0 `) |$ P: s; ?4 D
http://www.52os.net/phpinfo.php?id=1 正常显示。" j% O: X" P. i& z$ u! B
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& o- z; T; E* p8 f; m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
1 b3 b0 I$ g! L- O4 W, k说明sql注入和xss已经被过滤了4 P6 J1 P( h% v4 e. ]+ ?
9 D, q9 ~! m1 P* z七、安装过程中排错8 I7 ^7 p4 g' K! S. t Q3 u( K
: t* v. V2 S) Z$ d# L0 W+ P1.缺少APXS会报错& e* t$ E5 H' Z3 Y P1 s. Y
7 A* |7 D# m1 c- `+ K- R1 x
configure: looking for Apache module support via DSO through APXS* @$ w; i' }! s& h& P* k" U
configure: error: couldn't find APXS! I4 N$ g: l; A1 y' P8 z0 y$ T
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。 k. D" |. H1 P2 r# {
解决方法:' I, d; r& _ o7 [
3 i' ?2 j, A h4 P' _
yum install httpd-devel
1 v/ l' v% P3 k. X' u, ~# E2.没有pcre
2 R" f' w& g+ K! v& H H( l& O% k% J2 Q! E# J7 i4 [
configure: *** pcre library not found.
1 L( T! _% j: f zconfigure: error: pcre library is required7 D# r1 L6 \: B
解决方法:
: g' z1 o& b9 @* L( {; M
8 \, D3 c0 B9 H# j) Oyum install pcre pcre-devel* k2 `- o3 u o: k; d5 `6 T
3.没有libxml23 f+ ~& j7 Z6 G/ {' N
* C+ [5 z+ U1 F4 q
$ T Y2 |9 o/ F+ c
configure: *** xml library not found./ K! R& ^+ z0 K7 E8 o* H8 [
configure: error: libxml2 is required% F) X& T# R5 `9 J- j6 F3 D N
解决方法:
$ o/ v, w- w& g0 Q
4 `2 @) | L: b5 J( uyum install libxml2 libxml2-devel
' s; k9 n- z) |2 A4.执行 /opt/tengine/sbin/nginx -m 时有警告% Y9 b3 |6 k& c; b, Z* i) Z! a' O. j
3 O+ W8 J3 f8 ~" ?Tengine version: Tengine/2.1.0 (nginx/1.6.2). _$ |, C: }1 u& W# k5 M
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
4 W: e- ?7 b9 Q" q原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ {- C( ^; N9 P) m
4 q' L/ j" k7 n/ |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.4 R" e! |% {- F9 B1 p/ D! Y. e0 }& ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" F9 n; Y Y, I/ \2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! V* r0 C' Q% Z/ c# _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
5 s0 ~$ s* N/ K; M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
2 f" `% `6 z. W/ D3 a6 h/ I5 O( R& S! L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
4 e% F: Z# o3 L0 l: _' d; c解决方法,移除低版本的APR (1.3.9)
" [) f$ z$ Y+ {* ]! W& p
" e5 H8 {/ o5 \yum remove apr
+ p! ?$ [% T- ]" ^' c2 n+ P# |5.Error.log中有: Audit log: Failed to lock global mutex `" S% T M- _* g9 E) z+ f
9 }1 u- S# U, ~- c" q2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
8 f: ~: v- a$ u- x4 r+ ?- I% `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; z9 Z0 N# x/ |0 S m解决方法:
2 d" t. r6 p% P8 d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 z* ]% S# `% U& V2 V5 f3 {# ^! E9 o2 ?* `0 ^0 Z
SecAuditLogDirMode 0777; I9 A2 L6 _6 E4 c
SecAuditLogFileMode 05503 `' Q' `' _$ [' O; w" U/ t
SecAuditLogStorageDir /var/log/modsecurity" [$ `+ y* k5 m/ s
SecAuditLogType Concurrent
y; c* c1 l2 G/ @参考文章:
9 P; y4 i: j9 J9 x* a' h8 Bhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: d$ ~5 }1 y+ e$ Mhttp://drops.wooyun.org/tips/2614 |
|