|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 i4 A8 s9 ?; @7 f i1 j
* x {" f; f* L4 E$ g. D7 M( `一.准备工作
: }, E6 f* e- x) J
) G8 B! z7 [% x6 H% `: M, A5 M8 F系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0 x2 X3 a6 Y3 { d& X5 h
9 x, I8 e( I/ _1 @' q% Q, h
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. c, o9 @; A# x1 l9 F8 n4 g
& F, O: b8 s3 W0 v% nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ k5 X9 [6 M4 H4 M- x, F2 O6 x; c# _0 t4 @4 P* b7 f- S! f
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs9 @/ }2 \" m' m) z3 q
) ~, s: d' z, t3 D5 f' U依赖关系:
. @- z+ @4 o/ i8 S/ Y7 mtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 x& J1 A" C( T, @2 e. ~6 g
# b; }2 _5 M2 e, [6 m8 @yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& U# \) r" G9 W! v4 Q& D, G3 [modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 {! }8 S6 w% e
0 Z; E' F9 b" n; n6 pyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
- E5 j+ e: L; W, O( @二.启用standalone模块并编译
8 ?8 A/ u3 @! R8 f5 C
7 v- m5 P- F1 r( @' o下载modsecurity for nginx 解压,进入解压后目录执行:
& c( u: C5 ^, P+ ^! f- R/ x
2 F' T7 B8 o" ~' o9 O./autogen.sh
/ M1 A" @& E: ~# [./configure --enable-standalone-module --disable-mlogc* k- h) ` t& R5 Y, A- \/ M* u
make
# X9 |7 D- x- B; V- {三.nginx添加modsecurity模块* x6 o7 E9 I. ~6 q- u
# V# M3 a9 v5 s( k. o- w$ E在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:3 M- X) b, o& Z- [, C$ N! ^0 ^; w
- O5 Y% ^! ?: g4 V" x./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
% ], Q n* ]! \0 Ymake && make install
$ b4 g6 P' `2 d$ M6 X2 D9 \四.添加规则/ V7 B9 H3 C3 y3 G8 [7 T
) O5 R( r5 k& m. I5 E' v0 Z% rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。5 R& q" H5 f5 T( M
6 @" N% E0 n8 b) H! |6 @
1.下载OWASP规则:
; c0 o) O& L) u! y
7 Y( e5 J* u6 i# I, r, jgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 N, b) `5 |: B( o
1 L k) u1 W) y4 B0 y5 Gmv owasp-modsecurity-crs /opt/tengine/conf/
$ K/ t5 |0 `( l2 D, ~; v4 a' w7 p2 w6 R8 b5 J% V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 f* Q3 x: k& }2 O6 K! k! \, ?
2.启用OWASP规则:
/ H9 ~' p0 x* @2 H/ y+ _; j( B# W
% t# `* r, ^5 {复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* j% D% C& Z9 I7 E4 N
: q- t1 A3 x1 \+ n. ]5 ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 G. J G8 H3 {% y) z$ J
& \. \5 c8 n9 H& H4 J7 @% O) F( Bowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 N* G* L# g3 n8 U: ]. e
3 U i5 y; T4 t' |, e. Z6 B
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 E2 b2 p4 E2 N2 p- d9 q5 {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ U: F+ @9 w% z, f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 W2 Y! X/ k/ SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf- z6 d5 R. V. J' y8 v' b t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 }( W. U% @% n( @0 G9 b% aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 F7 A' R0 a% ?& u/ _: H! n
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
. U5 G- p$ ] j4 x4 d五.配置nginx
6 |6 B0 w1 K2 |' |* e' a3 u$ O) w8 P- ?" _! X" f
在需要启用modsecurity的主机的location下面加入下面两行即可:
* z* W6 Z) D7 ]7 F& B+ t+ W( U# Z, K0 ^" y
ModSecurityEnabled on; 3 d/ T# J4 F9 Z- {: d3 q* k: |
ModSecurityConfig modsecurity.conf;* O$ E. j3 p9 x
下面是两个示例配置,php虚拟主机:
7 X2 f/ p6 p, E: Q& ~. s3 c
/ Y% R) ?* ]0 b m. B! \server {7 r% X Y9 v" s( N3 m$ @" |7 v
listen 80;
: r; v. D4 L* w1 S2 ~; F2 g server_name 52os.net www.52os.net;- t1 o q/ {2 V& C# [; Z6 \1 S7 _- k' M
% A4 o2 T2 N' v- V# M E+ b$ [3 h location ~ \.php$ {
6 X( c1 T& x% [0 q+ [ ModSecurityEnabled on; - s6 U% y! Q+ y5 [! t e- o- I/ S
ModSecurityConfig modsecurity.conf;" a. [: ]/ g. {2 X) T: W9 c
( [" o! f8 x5 W/ [ [, l9 E
root /web/wordpress;7 x3 k) n1 ~) ]& m! R3 X
index index.php index.html index.htm;9 [! {5 u3 U/ N8 y
7 j* _+ d2 \# W# @/ ~' @' {2 x fastcgi_pass 127.0.0.1:9000;
7 q) ~! q/ P# d fastcgi_index index.php;
. O# S' \0 [+ h( v: _! M fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
( o0 \4 L1 g7 E* W include fastcgi_params;& D$ x4 W* J7 k" U
}
4 Q( v! Q; ^# I7 b }
5 T# v7 s3 {: {8 }' B% {' zupstream负载均衡:8 _3 j+ R+ |$ o! U! y; Z7 r
+ ?! A" D0 Q7 g3 D/ h S
upstream 52os.net {: v' k% d1 L5 ]+ n3 l
server 192.168.1.100:8080;- u9 ^: u9 g8 V
server 192.168.1.101:8080 backup;: _7 {( [; L# J" G
}
# E2 I+ Y& ~/ ?( C0 I/ x2 [4 k; K
server {
! M9 R1 e4 {, blisten 80;6 s3 F E% [! E: R' i+ r5 y9 q
server_name 52os.net www.52os.net;
9 o! \) A" H) q/ _7 F" z8 s
0 Q- l! W# N- j' [4 _4 xlocation / {
, w7 m3 W6 _4 H' r3 L$ } ModSecurityEnabled on;
, q" S- d+ n5 e) s& a) Y' @ ModSecurityConfig modsecurity.conf; ) T5 k6 o- \+ g) W9 H6 H$ s
" a N- J3 M2 e, v, q proxy_pass http://online;
% Q7 ]$ ^+ ~( {; l% Y$ e- b& l; R! v- z proxy_redirect off;
7 w9 n2 e/ X W proxy_set_header Host $host;
& G; s% ]/ _" p# Y1 r proxy_set_header X-Real-IP $remote_addr;
) h0 p7 _1 e$ m2 l Z+ ~7 m proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; u" ]6 o- h9 \ E1 J0 j
}
& X. @6 Z! P! H, y. j. p8 C}4 B. z# d3 I/ q) V: W1 X4 v
六.测试: J; Y9 s9 u& R+ W$ F5 c
; u" i. d7 q+ U$ L/ o+ g
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& Y2 v+ M5 v' f0 O3 n& `! ^
4 q5 b7 f. n$ W {$ \3 {<?php+ T5 u, r2 v8 d& u6 D
phpinfo();
+ m/ I+ J# y# T' c3 z+ C5 q?>/ C- K0 `7 F" n: r
在浏览器中访问:
% [+ Y6 v9 E/ T- k, R5 t$ s4 _% L1 J' l9 W* \
http://www.52os.net/phpinfo.php?id=1 正常显示。7 V) {% |: t: z# d: H4 D
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 x6 L* y9 S) _# dhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' G- q9 {2 {$ d* k! x( A
说明sql注入和xss已经被过滤了% s# W" N7 ~, w$ i% l: w! E
& r N: I/ \7 I- D0 [8 E0 e
七、安装过程中排错8 T7 j6 I( I d) B+ S
8 H- {* S+ G1 r! X- Q8 L3 k8 ?0 \
1.缺少APXS会报错! O$ s! h5 `4 y& ^# @* e- d7 V* q
1 V, ^/ m! }2 N$ cconfigure: looking for Apache module support via DSO through APXS1 _( r9 W' ~' {- `
configure: error: couldn't find APXS
- p( ]6 w5 z+ C: U9 U' E# Bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
7 q# g8 Z! C- v6 m6 W5 t' g* v {& @& S解决方法:
0 W! V6 b* [! A6 A0 ~* f z8 h6 W; w% y# r
yum install httpd-devel) m; s! w" b6 m- e- i B0 y6 C3 {
2.没有pcre
* {. e/ `. q4 ^- j( \( b) n M
* y6 M' R4 }7 uconfigure: *** pcre library not found.
\6 `. O) Y: Y# Y$ Gconfigure: error: pcre library is required
$ `# V, C3 J: L' J解决方法:
& f2 |, A8 M3 \8 }6 I Q# ]) ~+ A; S! S. j( P: e
yum install pcre pcre-devel
# x6 c+ [+ X) [# j3.没有libxml26 h) n+ C, @* [& ^* l6 v0 D
& H7 o# X# e5 [ B R8 A" Y. `
0 X" R, W- \, n8 P# Oconfigure: *** xml library not found./ h+ p% ?1 t2 q% p1 P+ V
configure: error: libxml2 is required8 h- O: r; P* [: ~
解决方法:
; C3 g3 p4 ?( B0 b
; f+ i" Z& |" C7 z& \; P$ n7 C7 Qyum install libxml2 libxml2-devel
# l2 z' k, \8 }4.执行 /opt/tengine/sbin/nginx -m 时有警告
: K$ ^+ J2 Y f6 t8 f8 r6 q( q6 ], P+ L# u- O, V
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' G0 C7 G. w" ], i# gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!! C* L! L2 Y4 j9 e. e" c& V
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log7 v6 |4 T. y9 {3 o( J3 t
d7 B$ y# `5 u1 k/ r6 U! }2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
: |7 S& c' h1 y% R8 x% a' |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 M# o- z1 L/ n/ m! {( N/ N8 H0 n2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 R9 _9 Y9 j) V; ` ^6 |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 A, ^* N D& m; \' N' d3 ?8 T; e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"! J1 b. o! m& b3 f
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.% X Y. h; M2 P# J; Z" {
解决方法,移除低版本的APR (1.3.9)
7 D9 Q) ~; S# ^% x [3 m6 ~; a; ], r4 r) p! d) `( s" s& g
yum remove apr6 \- o- Z7 X& g3 u3 D; b% @
5.Error.log中有: Audit log: Failed to lock global mutex2 @4 t! ] W3 l
8 I+ _3 h& Y! q4 {2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 J5 r" \/ G+ _' X) K8 F
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: G# c" v6 S/ _# p0 V: v
解决方法:
6 e5 M" S5 m( O+ @$ K4 }编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( m. ]# o% k8 B( O! K
* a1 @8 E* q! l' u4 w1 e, c9 d# ?% s
SecAuditLogDirMode 07771 A. X7 e* f$ A: y6 {- I& H
SecAuditLogFileMode 0550% E/ _5 i) v/ C* O/ S- A
SecAuditLogStorageDir /var/log/modsecurity
7 l. w* h7 [7 P' rSecAuditLogType Concurrent
: v% q5 m; E, |参考文章:
4 `* s% J7 F+ U& O; @ B' d& d, vhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX1 u1 n L0 Q. B
http://drops.wooyun.org/tips/2614 |
|