|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
5 E2 q( q( b1 y
; ^ a8 L0 m0 U$ @0 f9 K9 t' l一.准备工作2 P! ^9 V: O% ]
; ?7 J$ @9 T o5 b5 A: O7 c* E
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" w! L; \0 v* j1 Q( K5 b
" U( [, v6 z4 E# a, ?% e; Qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
% M: s1 \: ^6 z6 G- j1 S" I, U y( P: n4 s. _$ W( i6 g
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz+ F1 s# q6 F, m8 N& |; L9 r! u
# u) }- P) g7 q6 @9 m5 @OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ M2 u# f& R7 m+ w8 ?. H/ k
3 V' N% \1 f( C' h* a6 F& c
依赖关系:3 _' `, X* B1 q: c6 E* e# a
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 L2 {. s: L- q% v; o. c0 X2 r3 k
: f) a* L- z6 F8 w
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel/ C d$ w/ J: f7 O4 `) ~) U
modsecurty依赖的包:pcre httpd-devel libxml2 apr
; `' g5 {- P2 O7 }* C, @' Z' G8 D0 ?; R
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel. A# j X) e% G$ `& M* q
二.启用standalone模块并编译
$ j; J, Q5 W. k2 Q, \
! E' I% k8 X: i( x" v下载modsecurity for nginx 解压,进入解压后目录执行:
+ y" l5 R& |# ]1 g! x2 T- F
1 g8 J! N7 y/ Q- H1 ^6 K: [( S2 K./autogen.sh* H a9 j, P" _% E( ^! |& L* u
./configure --enable-standalone-module --disable-mlogc7 V; r9 ^$ G4 Q2 H/ b+ C
make
7 q. R3 }$ N Y0 m三.nginx添加modsecurity模块
Q+ H; F- X6 g E/ L! l' m5 w! S- m8 e, b
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:& H6 W/ [$ [+ t: U* u
0 N9 r d+ s6 j$ W) r- a/ I2 \7 u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 P% f% A# O$ k1 b, q
make && make install
* v7 k, K& v: N% S9 W" `) O% E四.添加规则& h" g+ G) E- b6 C1 y; r+ A" e+ \( S
, Y4 ~! K( I3 `3 y# S; K* _% [9 [
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。/ h. X$ H) G3 Y9 c$ a
B7 }4 Z. Z: I
1.下载OWASP规则:
% t D* d! q! e4 E9 J/ M% ?7 U0 r, t& K* Y- ]# Y. X& t
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 z8 P$ H1 k- {0 l
" e0 o% I3 W! Y3 _3 o- L) [ ymv owasp-modsecurity-crs /opt/tengine/conf/
0 Y `; S- T# T3 J2 _. n1 a/ u9 ^; \ [ x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
: z8 t5 Z% ~$ Q$ j2.启用OWASP规则:( l0 x3 _2 z1 h8 \! q% q2 w+ ?
# s6 f# o6 |5 b# Q! B8 h- O* Y
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
9 A9 ^. I& Z1 p1 t: f" l; X7 r; ^; c \* H- t' u
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
4 E7 n5 A' M4 C4 W1 D* T' q% I& F! C$ Q. c J1 ?' v! Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! q P! r; F7 y/ F% g% q; j V8 F# N. [4 W4 L9 L
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf- W5 E) t4 P" j" Q5 c
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
$ p! M$ Q8 {7 C T7 CInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 X. _( ^) C! @; b* W* n# l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
6 ^1 {1 Q. t1 mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 T0 C2 j. ?- G$ {6 k9 dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: a; Y% E4 h& B2 @Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf2 h9 N" x$ ^3 o
五.配置nginx
B$ T2 v1 j2 l& A0 |3 t; A' L7 C% C; H" i
在需要启用modsecurity的主机的location下面加入下面两行即可:2 h' S7 e( n! N+ i; e, ^+ M
# [' Y+ e+ o; c
ModSecurityEnabled on; - ~& R: G" M( t6 L2 P
ModSecurityConfig modsecurity.conf;
: t( h+ }6 s6 b2 O3 y下面是两个示例配置,php虚拟主机:
& ^( T5 i" R- n9 U; f
+ a$ t% B+ Z+ a, mserver {3 r& y" M1 T+ T$ k( b% N; u
listen 80;0 a- g6 y9 \- C( Y
server_name 52os.net www.52os.net;
( t9 Z) J! M$ N9 c
0 \7 z; O6 }8 f$ ] location ~ \.php$ {
$ b: ^! _ C, n! Z4 k1 }5 ] ModSecurityEnabled on; 7 e, W5 }$ q3 d, i) n* m# v
ModSecurityConfig modsecurity.conf;
# R0 X; m& w6 Z% W
. j: [- C* b9 E$ U. H root /web/wordpress;
. Y( v& C- Q" b7 u4 L4 C index index.php index.html index.htm;
8 J6 |& y; p% Z' M K) {& l& A6 j3 c9 r
* ^5 Z% [5 ~+ } fastcgi_pass 127.0.0.1:9000;
( B+ D# \- K1 w/ ^' K7 y7 t fastcgi_index index.php;
$ z1 ? `5 S, X& {: o" M0 { fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
0 i8 S5 J3 W# I: a include fastcgi_params;
9 m& q8 Q0 [& B0 ~) m7 o }. o, l; O$ M1 Y; n9 G* I' b4 t
}- _5 \/ k' Z* D6 l( i: L- \, C
upstream负载均衡:* `$ G/ Z, w- Y
4 \& a. z- F2 ` q8 H/ E+ xupstream 52os.net {
9 Q) O# E6 \! A( c8 E server 192.168.1.100:8080;
4 L* w) @5 S/ _1 O) ^; ] server 192.168.1.101:8080 backup;
4 W! Z) h' z% @( x}" W) i+ e/ U: }$ _# a8 E2 y
$ c- `0 d& n) u# s) s0 t! Kserver {
" Q6 Z! r3 F* v1 l* a+ i% Y' Klisten 80; I7 y6 j$ J( C0 i$ n1 {# ]# e
server_name 52os.net www.52os.net;" h# m& ]/ Q# ~+ c
1 n1 ~7 F9 h2 ^# E/ Y9 p. mlocation / {
; V0 A5 o' I( r) y6 f3 B4 d ModSecurityEnabled on;
# S8 w2 v; l$ h% W$ I0 g" s1 C' i3 ~" N ModSecurityConfig modsecurity.conf; , ]1 o0 f' \1 p, s+ Y' b
+ I3 F5 H) h" |% I proxy_pass http://online;, u( ^7 L6 a) @& G& L1 [- j
proxy_redirect off;
- X5 P; L- ^; R proxy_set_header Host $host;
7 ?& G/ K* y( q+ Y2 w3 [ proxy_set_header X-Real-IP $remote_addr;& g3 z4 Z' W. D" C1 O
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;1 d }0 x9 |- m' f
}* v% S. S0 {' I& t" v8 | ]. W
}
. k& g; Q* Z1 w六.测试* W# m+ [, b8 {7 `
- j: Z4 F6 F0 }/ g5 C. z4 r, c8 w# K我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:8 Z1 {5 n5 U, {& s Z
$ ?& i# e" x4 a' s7 Z<?php
4 f, _# N4 R/ B4 F# u' x* [6 @2 z phpinfo();
2 Y+ g6 U# W c; Z# `?>
7 g" u8 _, y7 {2 g) {在浏览器中访问: Y5 T I9 ^9 ]" G
4 Z' h6 }- N7 S- @# s: F% @8 y0 a
http://www.52os.net/phpinfo.php?id=1 正常显示。
8 I3 \- i+ Q2 h! thttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ D! U9 j% A+ j& h
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。8 P; w) b' a& _8 T
说明sql注入和xss已经被过滤了: r* E4 `* u4 V f# R; h1 ^8 l t
- D8 h) h0 c5 k- m6 v6 O9 @/ l
七、安装过程中排错7 K' B: a W) \
# K1 r& B( ^, \0 p4 v" M1.缺少APXS会报错7 ?. m8 [: y. U. w/ m! R2 J& i
$ K% q. Q1 b: G: b U3 m- B
configure: looking for Apache module support via DSO through APXS! ~4 ~9 m. ?: @* W3 D0 m
configure: error: couldn't find APXS
5 ], y( a- t- W l9 N4 i( lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。% @3 B+ x/ ]7 e& n5 \) g# Q
解决方法:5 ?9 p& z2 }4 Q$ t- x3 u
+ ~# {# Z: H* Q; V% f8 T
yum install httpd-devel6 e9 B7 `2 ?& z) r( I( p
2.没有pcre/ ?4 [" ^/ j( w7 }. L
* L3 |. o" k- Y5 E2 Rconfigure: *** pcre library not found.' `" Z" [; k6 ^: o' }/ i2 l
configure: error: pcre library is required
; \* @+ A; B! |& m解决方法:4 p% s" l& l: F' I$ o3 s
3 L& R7 u8 s% F: ^% z4 f" X6 @
yum install pcre pcre-devel
8 Z. O9 v. K: d0 ?$ X3.没有libxml2/ d% U; ]% W9 n$ e
7 D0 h5 T% M; o# v9 l( r- D
6 o( d# P& E7 Z: B1 [5 R8 B! M
configure: *** xml library not found.# L( e/ r! `0 J! k( j
configure: error: libxml2 is required
& Y! ?3 ^6 \5 s0 N6 X解决方法:) \1 e0 V4 W4 I2 w
- L. w, u( U4 ?+ ]* o
yum install libxml2 libxml2-devel
+ a- ?# X/ o2 ]2 H7 b- B4.执行 /opt/tengine/sbin/nginx -m 时有警告
v- p: c6 Y( J* _3 _, y2 f2 x+ a' E5 N% t/ V S1 V- ?
Tengine version: Tengine/2.1.0 (nginx/1.6.2)- S. }2 p. {& d- a. f
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( X" g6 _2 B( g! j( M4 c7 _原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, }& U4 q: F8 k7 r( o
8 t" Y: O8 Y5 F* j- L; }0 F1 v8 M2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
2 k; \9 N y8 V' \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" A* t- R/ F- V/ O# ?2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!5 Y N7 q, ?# h$ f9 P; j8 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( X% s' x0 k0 R6 G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
, b$ Q- K, O p1 E; ^2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 V& l& \4 e Q) O* u) H
解决方法,移除低版本的APR (1.3.9)
8 y5 O" c4 A/ ?. M
5 O: x0 z$ A% L% wyum remove apr" ?# W1 T% H4 Q
5.Error.log中有: Audit log: Failed to lock global mutex: P. K" t$ }% D, d4 G
6 j4 Y+ }# K9 @5 a6 v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ _# l- k% R* e( M; x4 F5 sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& b% Q$ d" ^# D; z$ M4 g
解决方法:0 g& h2 K; H9 ]% x, ?7 _* ?
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:& k* k( o- e$ J, U; _" a/ d" b& h
$ A) V+ ?; `% e2 ]SecAuditLogDirMode 07774 [* ], T/ l6 Q7 h" x! N5 m
SecAuditLogFileMode 0550& I8 Q! R* b u4 ^! W7 r5 i# {
SecAuditLogStorageDir /var/log/modsecurity
! i2 T" n! W% w; P9 C, u2 o& M6 vSecAuditLogType Concurrent
) c+ E. Q9 E" X2 u9 I( i+ q( i! q( S3 u参考文章:6 `/ K% S- V/ y S& B& {
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX5 S4 f9 M3 i) |4 Q: f. {- L
http://drops.wooyun.org/tips/2614 |
|