|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。/ o2 D& J7 }$ @* }5 g' ^
8 i& T% L9 X& q; ~
一.准备工作
/ i( e5 A1 v; e* `4 m O. Q6 U
3 P4 P4 t& @# `( h系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.05 b! E; e6 E! A( R
. J2 s! i6 ?9 S2 E4 ?$ E7 S# `tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz3 q' t" R+ V' |, w: Z) C
6 b% m5 A: I: z. nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz0 P' n# v" ~/ ]+ z( _
; q# d5 G t2 gOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! u; k" Q) B+ x, \* d
5 Z& r6 D2 I- B4 q/ ]& H5 Y) W9 y& v
依赖关系:
0 L M2 s6 |4 ~+ [$ y) K" v/ Rtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:# ?8 \1 |- P3 V! _
$ b2 e1 E- g9 oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
4 G4 y* j' ]" o- X \6 ^modsecurty依赖的包:pcre httpd-devel libxml2 apr/ G% f9 U& l+ o& O7 Y
* I) c/ ?4 L( x, ?2 I
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel$ x7 B( w; a- M1 C. `" H
二.启用standalone模块并编译* ^3 D5 g6 ~3 C$ ^
, T2 p9 L) s$ s- K
下载modsecurity for nginx 解压,进入解压后目录执行:
, l8 t8 s: }, i6 L, P4 V7 o' |
2 w) h1 h3 d( d) ~/ ? K./autogen.sh
! ]* e5 [! C/ a& s p6 g./configure --enable-standalone-module --disable-mlogc
9 s4 u' Y7 R Y% }& R: G: emake
7 Q9 D) M5 k* o三.nginx添加modsecurity模块/ V# u& a. ]; K
4 ~0 V# u" O$ @+ w在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
0 }3 Q6 s0 L' e: i& `1 }
% f+ ~+ _- i$ Z. o6 m, K2 j. D+ r5 k./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine" D5 {: a# b& J
make && make install- E) @6 B( x, d: Y
四.添加规则
, ?6 Y7 I5 Y% N6 U! ^6 a
0 P% b5 I# `( T8 qmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
& E' R) l8 @: N, G3 O% y
% z5 e# Q c0 `5 x" h1.下载OWASP规则:
' l+ h+ R- E8 `7 k8 b5 b
]) j0 I' y* N9 P2 Bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs& W& z; f9 o! Z9 i
D y7 K- w2 X1 Kmv owasp-modsecurity-crs /opt/tengine/conf/
+ y* f$ v1 G; z& k4 P( V
+ q6 S! T$ q$ Wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 Q$ `: e4 ^( u$ K0 d1 V( {2.启用OWASP规则:# L4 B: ~0 ]. G. J1 ]" O
, Q7 G, c# T( y4 \8 x1 z0 M" I复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。( E' }+ v8 O4 J: S7 o
2 N$ @3 o, g3 \6 h" M& ]1 W
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 d. J: F7 R2 r. y6 ?* B( K$ B0 S
+ F% P! S+ v3 s2 Q3 s; Lowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; P6 L" u: v2 t7 h7 w9 z9 N. d4 @5 j- a9 ], o7 E; p$ X- u1 _
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
- L6 J( {; f6 ?, E/ T; h' F, WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 M' q6 B! X0 d
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf. Q" p7 N0 w8 g+ H ]# x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
7 p2 a$ A) K4 M% F6 h% Z5 h; ?3 T" BInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf- A; O9 o0 X/ V C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf, l) R* m) L, l6 ~( z. b
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* o+ E, M* l. C3 ?5 g/ l
五.配置nginx
8 N6 g$ w7 [5 p ? i( }4 H4 x5 H9 {! B0 p
在需要启用modsecurity的主机的location下面加入下面两行即可:* p; f, P: V7 G" w$ L
# s) x' t l# F5 A) hModSecurityEnabled on; 7 U- w! Z: p! X- d5 J$ Y) x
ModSecurityConfig modsecurity.conf;
/ g0 u. ~, @( U/ w# M7 Y下面是两个示例配置,php虚拟主机:" g% d+ K- g8 j" |4 ?4 ]
$ O. ?; b8 R4 j( Z' _server {
+ _2 Z" h' V: p1 I; @ listen 80;
3 r4 t. ]" c8 g# S8 \ w server_name 52os.net www.52os.net;
5 e$ B) Y% ?/ Z5 \: t+ N5 F4 |
3 C6 y+ ~ [- v, P$ a( r/ O location ~ \.php$ {+ j& [9 Q! e* C& q
ModSecurityEnabled on;
/ E& Q2 g* [ k' u4 a7 o9 m ModSecurityConfig modsecurity.conf;
2 q: n' \5 G6 [; @
$ r+ A: |$ U1 Z! g$ t- \: {' S root /web/wordpress;: H7 m6 |" j8 q! P8 }, |* M
index index.php index.html index.htm;. v" z3 P: {& _* d3 ~
5 Y) b0 r( C( i2 d
fastcgi_pass 127.0.0.1:9000;
# D" j4 K3 _0 x K fastcgi_index index.php;
1 [5 u+ M# }6 J& U- U* m% H4 V fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;4 B3 C2 l" p! Z4 ?; c7 h
include fastcgi_params; e3 O: L; O1 M. v# Y
}, O$ G, N( R$ i; I2 k" w- R8 P& P
}
9 \) S/ a; Q) tupstream负载均衡:
1 ~8 c/ E* f$ [
' v- X4 T v: m. J) _upstream 52os.net {" {4 o' e' F/ d1 `8 R
server 192.168.1.100:8080;. X3 ~/ p" }8 }1 n$ f: H
server 192.168.1.101:8080 backup; o2 O% z0 w4 U' t
}8 D' e" v: Y4 ?
2 t+ `; M! q3 b' b
server {
' Z) `2 N' {; N$ O, P8 W8 D" j, @1 l% blisten 80;
! \: o4 Z' Y8 t- {server_name 52os.net www.52os.net;
% n6 V. f4 U# R+ w; J! K5 h9 o5 {9 [1 Z/ t" j
location / {7 C/ R3 h( U3 `
ModSecurityEnabled on;
3 l/ h3 d* Y7 a% a5 q: k- F ModSecurityConfig modsecurity.conf; ( _9 ^% Q- y" @3 }$ `3 }
& B7 J6 ~2 A7 [. w) k
proxy_pass http://online;4 p. f/ u2 k1 R/ V( V- z
proxy_redirect off;: B# J% ~+ O: z7 R2 q, \ w% a
proxy_set_header Host $host;2 h( B5 \% G: m' E; H( [% a
proxy_set_header X-Real-IP $remote_addr;
: H$ s" M5 O, i& s# W( E, ? B- u' g; W proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! X, M; q P4 N$ m+ V }2 W; E* N$ y: f6 L8 O! {/ N
}0 \/ x3 X/ P8 ^! @4 ]& d& ]; |9 H1 g: s+ Q
六.测试1 |! ^/ w$ N3 a E# C0 O8 p
) F( B0 N) v5 {我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( G5 D6 b% Q% }' R% o, T* w0 H0 u
<?php3 [4 U Y- K- C$ c+ \
phpinfo();
- J$ p' C5 K& J; U# @; T?>: c3 a& J& q. A; H9 \# ~# Z, y
在浏览器中访问:, c* q T) }. R0 W0 B# \
* x- ^+ z) \1 Y( F& _, H7 ohttp://www.52os.net/phpinfo.php?id=1 正常显示。
# Y3 q7 G/ P! ]2 K( Q: r. R6 Ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
8 ^1 l% X/ P4 ihttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
7 H/ m. o5 d% z8 l说明sql注入和xss已经被过滤了( ~0 U0 z4 P; w! z0 a
% }" n8 [1 _7 K2 n七、安装过程中排错# y' y3 c% G6 Q) A5 Y3 i1 g& @+ R
( X/ n- q, C, F" ~
1.缺少APXS会报错
7 ~; G+ Q5 s. l' @7 w" R& T
/ J3 }0 V( D, Y H5 `$ M7 {configure: looking for Apache module support via DSO through APXS; H% ]8 ?) R B5 s( p$ R+ k$ Y
configure: error: couldn't find APXS
( R2 j, [$ ^: s. g& M. ^apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
: x0 b+ k6 ?! j) _6 \* W, ?4 A解决方法:& c6 L2 T0 Z* [
* g# y! z4 i- E: u1 U
yum install httpd-devel
' Y" ?7 u/ l' m2.没有pcre
M+ \5 t0 b+ @" v0 f! a' ~* I5 F7 f3 j% c: L/ X
configure: *** pcre library not found.
1 `& g- |' E6 i4 ^6 O Qconfigure: error: pcre library is required
9 r* m2 J- O( b' j8 b4 Y' T- _$ ~解决方法:
' R8 }0 {7 z9 G0 A# A$ d) y7 T" L; [" f' [5 V' q! B
yum install pcre pcre-devel0 Q6 p: s$ x7 O5 `2 Z" s8 d
3.没有libxml2
6 k0 O9 M, X/ j5 m
- C8 o, \- f7 T8 o n9 D" C/ c; }7 g
configure: *** xml library not found.
% L ?5 f' O) _& v4 Mconfigure: error: libxml2 is required
2 {, b$ S5 |8 o: J: n解决方法:1 T* ?! t( ]. W7 O0 r% T5 X0 i
. y; w, {- O- R1 f8 O8 E' m
yum install libxml2 libxml2-devel
/ m* t5 b2 R6 d4 s) b8 c. u1 e4.执行 /opt/tengine/sbin/nginx -m 时有警告! _/ E! i; E/ j; r8 R
6 e% B% \, w. e5 }: @Tengine version: Tengine/2.1.0 (nginx/1.6.2)
/ ^/ E6 l2 D, @$ dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; I% l$ ], B3 P/ i& q$ M原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- S8 d2 y2 V, c3 M
4 {% z- _2 J2 T: A9 Q6 B' P' k1 P
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.0 d( O& B, x& T( S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 l5 ?- P8 ~7 _6 T1 i; g
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 d8 w. I: g4 U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 K7 Y" Q/ ]7 Z/ q2 z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& S- ~9 { P& C3 d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.2 ? J$ o- P; P# F* _9 C+ Y1 L+ w/ O7 N! D
解决方法,移除低版本的APR (1.3.9)4 w1 S; W. |) a- w4 z! t6 ?
0 y, w: ]3 U* ~* Q. ?yum remove apr7 `( k- w5 n; t) L. O" @2 P& i
5.Error.log中有: Audit log: Failed to lock global mutex# ^9 t/ Z+ n/ g. T
% {: h4 D" C9 s* O" {/ ~2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
/ x* f* M/ r# R' hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 I, [0 J! q3 m3 R% O: F
解决方法:
" i; ?7 u7 @6 L+ k编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
' S; S9 c" K& b" V
+ d3 ]6 m! t0 {+ K6 nSecAuditLogDirMode 07779 a; Z; S& W# G: o
SecAuditLogFileMode 0550
5 T; |$ L( g' k1 x: @SecAuditLogStorageDir /var/log/modsecurity, O! }6 A6 y+ B- b5 J {
SecAuditLogType Concurrent# H) B* ~! n; v4 \0 s" U) B3 ]
参考文章:6 Z% n1 j) o2 r" E! E5 X
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
x1 \ |4 B' U7 @http://drops.wooyun.org/tips/2614 |
|