|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。8 M) |; X- i* Y, b: P
, ]. v- V4 N) q
一.准备工作1 |0 Y+ `" {5 r9 H1 D' M
1 S2 @4 n0 L* [7 i5 o系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0; Q8 F0 {9 k/ Z$ [/ S9 y
: T1 r( F( L9 c2 ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 g, t. z# \4 M0 {# @
, @% Q# l* z/ S/ C5 _( Zmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
1 ?6 U6 s9 X, J3 G, z9 K* y8 b Y9 n( n* [% o6 t( o# y3 u
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs' }$ s$ `, h( G2 u' Q7 \
7 ~0 c1 l. G. ^- p5 {依赖关系:
" N: [1 r# O0 u n8 `tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 J+ ?% S; }/ z
_- P: q( P+ G+ ~6 e5 W5 j1 byum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
# o) p9 O5 ]* E7 ]modsecurty依赖的包:pcre httpd-devel libxml2 apr
# ]2 H6 v+ p6 O7 u
! b( P8 @/ \% i5 fyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel- W2 [, Z& m! Q% ^( K# L: V1 R
二.启用standalone模块并编译; E# d2 d* n) U' F2 o6 h: s
/ Y( U$ |- W# R+ k1 q/ q4 F8 z
下载modsecurity for nginx 解压,进入解压后目录执行:
) K' Q/ f! Y' P& J1 d' y i" x. Z% n! O7 x1 Y
./autogen.sh
: @1 I3 ]( _. q- b4 G. l7 e& f* k# R./configure --enable-standalone-module --disable-mlogc
0 I: i' l+ \, emake
& w6 l2 j! i% F* l1 B三.nginx添加modsecurity模块
5 w( d& m- U+ X( q: {) `% c% q. i4 ~2 c# a$ z B; O; y
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( P3 d/ z; X; c
$ f \% `- s' ^& x9 t b./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine, ?* f% _% E. o
make && make install
: T. k" L7 c1 G L1 U4 P四.添加规则
8 D, e% I& A/ S" }! Y3 C! _
, |3 S/ X% a6 \modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( j3 |9 b$ V& _' o1 u2 s
; d( d# Q+ M1 O- s1.下载OWASP规则:
; P- ~$ `) ]* @: [
+ g: D. Q! a$ H! L2 agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& F- [1 D& R& ~% _' ]
H# I X4 k' F& O3 ~: t' I# Tmv owasp-modsecurity-crs /opt/tengine/conf/6 d; v1 i. v% i; W
7 t2 S2 F! @* c" u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 ?5 E3 T: r* l. v- ]4 [% {2.启用OWASP规则:
% j* R7 P9 K3 P
! v2 Y( M& C7 r复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。. K" Q$ O& e L+ A
+ D- A$ j4 k. q7 g- _编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# B3 H" \9 e' P2 Z2 P+ i2 s9 r6 w
7 ~4 d; n0 K, ~/ n+ V, I/ d+ oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。; v; f+ B* e( a
' C, e: i" a5 N7 LInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# G2 E. @' R; [. ]. v" L$ ^: A
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
9 X$ ^9 ]) R+ p1 r! MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf, r( w9 r" H: v" V% H2 [0 `! P/ p
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf' [2 O! l5 E0 O; ?0 ?1 x7 D& u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! c1 P6 @/ b/ i9 w7 P L' |
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 ?' ?& |& F2 D0 B% Q/ jInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf) z" x" j% [5 i0 }
五.配置nginx9 `3 h4 G# w5 w: Y Z$ v
# D9 o/ z# y1 A
在需要启用modsecurity的主机的location下面加入下面两行即可:
% V' r" f+ f1 K9 J
% P8 ]2 a" B& |+ ~5 e, T5 Y) Z- dModSecurityEnabled on;
! A: [! M$ b4 H2 j Q! {1 T, Q" aModSecurityConfig modsecurity.conf;
1 l: x. o# q- J. o$ g) d下面是两个示例配置,php虚拟主机:
0 E; j/ q% d( [9 |' F- H7 N9 \
- m: [/ ]9 ]3 d9 U. ]; P" n1 fserver {1 K+ P* x5 M5 l
listen 80;
. J2 E4 K/ K& G# @) K server_name 52os.net www.52os.net;5 ~( R; u; U: R2 G8 y2 y: B) ~
5 R" f' V) k+ Z location ~ \.php$ {3 W4 V2 f: ^9 P
ModSecurityEnabled on;
+ q6 A5 Y% y' B4 ^9 y ModSecurityConfig modsecurity.conf;
' ?2 ]8 N5 S8 t! t+ J0 P6 H
1 R; ?3 ]4 B5 M' i3 C# a. e root /web/wordpress; Y! o+ V: P% s
index index.php index.html index.htm;( T! q: E: i9 p% Q9 A E9 b
7 @) u P1 e2 r$ x$ o& h g" z
fastcgi_pass 127.0.0.1:9000;
3 a; W: z9 u; [2 q. Y0 l0 I fastcgi_index index.php;
7 B& \! f4 v0 R. Q fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
1 E- Y, d6 F+ K5 w/ g( M8 m include fastcgi_params;0 P8 l1 X5 u0 ?+ Z
}
# J$ C& I. _7 U1 i; c$ W }1 `1 D5 z6 i( g0 ?. }
upstream负载均衡:
/ y. t( A& L I
+ G6 T* J- y8 u: z7 \6 pupstream 52os.net {1 y$ n) N; R% W; v2 r$ b, l+ ]
server 192.168.1.100:8080;3 K2 K) W/ Z5 ?* u! T- E6 [; r9 c
server 192.168.1.101:8080 backup;
5 q& ~+ o- R" B7 @$ z. n1 j}
% I7 S0 A- s0 Y! Q
( M+ ~ l0 k+ y3 m, F: b" Mserver {! ~4 v0 r% [1 S' A, p
listen 80;, K7 O" L6 x" Z2 C9 i
server_name 52os.net www.52os.net;# u2 a3 N- }+ q# h" E/ x
# o1 d% t [) K8 O, tlocation / {; j- }" \ e. l3 C- i j+ e
ModSecurityEnabled on; 0 O5 [* z4 T9 `" O2 U! g+ l, R
ModSecurityConfig modsecurity.conf;
1 ^8 g* G$ r. n, C$ d5 p& e4 r) C) t2 \2 U* S, f9 S% q
proxy_pass http://online;- y4 c# U. `! ~8 M+ j5 i& O# y
proxy_redirect off;
/ l3 ~5 {: F0 G proxy_set_header Host $host;0 K L6 X' R7 m
proxy_set_header X-Real-IP $remote_addr;
1 |4 `+ m- _4 n4 o" P proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;+ O" H5 [! |3 L
}
, Q) e O! o& ]/ l. H; C}
7 E5 p& H* j8 X9 H J% p六.测试 l. \, d p; O! h8 ^4 q
- Y B% D9 q6 o7 t+ V( q) \" \( v我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:7 W0 Z, |# K( F
* d* ?9 i( g' a3 i V<?php
: s# F7 }& w& H% r phpinfo();
: a* w* u j6 W' O?>; E8 l! y) h \) t5 W4 x7 K& V1 R8 c4 F1 N( t
在浏览器中访问:2 {8 \0 v; N9 R, Q1 l# `
: I+ T$ x* b; S C( o
http://www.52os.net/phpinfo.php?id=1 正常显示。
. u, H, i1 W' l& X- H9 nhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
% t; k7 ~) G3 Y/ uhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
4 h6 @% K4 |3 Q# l; z. {& ?说明sql注入和xss已经被过滤了0 `* M& D+ O1 K% ^9 F0 [
0 k, |6 |7 g" q/ h! L8 A0 Q七、安装过程中排错+ G+ a! p5 i. y
2 r+ l/ w, e0 `) }1.缺少APXS会报错, X. d* y/ `1 Q/ F5 p) J
) }* p/ w1 Y7 X4 n* Y, G/ kconfigure: looking for Apache module support via DSO through APXS
@+ z7 B4 c# Y% |% r% Mconfigure: error: couldn't find APXS
3 H4 y# z8 J s) p, ^7 Iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。- s7 ?+ w2 U1 k' T' b7 ~
解决方法:
/ p' A! A8 |) a U; U( @1 w& J \. z- i4 u
yum install httpd-devel
) r# m( i, ?3 S$ T$ P0 N2.没有pcre+ x6 Y- L) w( [0 x
? o% U; i6 R6 S% _. G4 F
configure: *** pcre library not found.
) d# n3 G. \2 n3 W6 [" oconfigure: error: pcre library is required& y+ D/ Z+ G" e/ U. X
解决方法:
' C4 U4 R$ o# D6 E8 R$ A; E; A8 ~7 L
yum install pcre pcre-devel" b6 x- i7 [/ }+ L# X" ?
3.没有libxml28 v' @ A" I* J8 q
: L2 j" K% \) [# {) _0 d
1 c, D# C* b' F8 V E! ^) U* [
configure: *** xml library not found.
3 b. l0 g) t/ s& D9 B7 M: g5 rconfigure: error: libxml2 is required. v$ R5 [: W6 S# Y
解决方法:; f, A7 q- S" u. Z0 {7 [
$ A4 W8 Z) a& Z# a% E
yum install libxml2 libxml2-devel
8 S0 ^& f7 d% E2 E+ R9 u1 j4.执行 /opt/tengine/sbin/nginx -m 时有警告0 f/ J5 z, ~* m
5 G6 r% a# q J- o8 g1 [: Z \Tengine version: Tengine/2.1.0 (nginx/1.6.2)
3 S7 F, n. M: i* V3 @2 R6 C6 ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ V! Y" \2 B7 S7 o5 d R原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log) `0 z( J6 g! J* c9 Q8 O- r' Q% D# l+ \
( E( n! g! }8 ^+ W" W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' z8 D8 B* x2 f4 ]# n9 z n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
`9 Q; o% k3 C5 U. I2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* u# Z) G) O4 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* _* y u1 N% y2 {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ d7 H- n. }1 w+ r( c0 I" L- z2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ p. T1 M% i! |! N
解决方法,移除低版本的APR (1.3.9)! H6 k6 j& k8 ~, v
+ b. x4 W! e0 c. A- A! [( s- t( |+ ]yum remove apr) b7 _% |+ ~ t L; P" {' I7 j
5.Error.log中有: Audit log: Failed to lock global mutex, u9 P+ H+ b1 g e* m5 P& s+ @% G
' D6 ?) z; a2 c3 F* L4 O$ j5 w
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
# d5 v; B" |6 H' R! B- i, A$ Wglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- X. F9 @8 w8 R9 V f! S( h解决方法:
, G$ m0 R! I3 L. G8 D; ?$ v" _编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( s! l2 T; b' }) Z( a
4 ?! r3 m, r" rSecAuditLogDirMode 0777
, D! v6 ~$ p! }SecAuditLogFileMode 0550
8 j4 J' ~1 j: M' Q( B& V0 m, NSecAuditLogStorageDir /var/log/modsecurity
3 G4 h8 R p6 p* T3 {SecAuditLogType Concurrent8 A( `# I& G0 a/ b1 q5 |
参考文章:6 `' {8 S& @" T3 I
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
# X& z" x# P& T5 g4 D: ~http://drops.wooyun.org/tips/2614 |
|