|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ U/ n! J6 k$ ^! M
/ [2 s3 }; T" N0 o: p! R一.准备工作5 x. ~9 V8 j! }) `) j
3 t Q2 ~6 B; N0 c0 K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
6 X7 b2 T, p7 N2 ]( }! O7 q$ z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 {% ~" l1 s$ c4 V# \
. a1 R* j+ {7 Dmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz/ D2 T2 n3 W! F
$ f( a% N1 {- s7 {$ y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs& ~5 F6 A1 ^7 A" x
) K q: Y4 h9 [( J$ M' R
依赖关系:
2 \+ s5 }4 a: O3 M" F" K0 B2 jtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) a4 [, s3 h3 b& L0 L1 @; j. l3 B- w% Q" {+ [; N: _7 A, V. j
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
$ e/ @5 n' p( I2 {' Dmodsecurty依赖的包:pcre httpd-devel libxml2 apr# z4 C; a+ N- C6 [' a6 ~$ R+ d- |: _
- w5 \& D/ U+ I7 B1 C% W
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! c2 d8 R* S+ g% D* Z1 y! W
二.启用standalone模块并编译- |+ z S0 e- Z# V% p. H
% }6 C& ?2 J7 G8 P% Z. |9 x
下载modsecurity for nginx 解压,进入解压后目录执行:
; t8 C- l; N8 f# y7 f+ ?2 s
. [) I4 `5 V$ S# p8 Z1 [! l, k./autogen.sh
. n" z7 H- y, s7 S4 b% g./configure --enable-standalone-module --disable-mlogc7 v! ]3 D. r& b8 C5 A
make
0 G9 g, f* d" E% M3 o( ^) x5 @三.nginx添加modsecurity模块
, ]& i6 D+ G4 @ x0 O
% d5 i, Q% x( Q+ H; Z" F! p在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:6 E" C% l2 ~( a6 r8 p& u
/ O, |. G* {, `4 B
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine) c* Q& S# l. }. [% ~& \* \
make && make install
# I) Y* i4 h* o) S3 p四.添加规则
% Q, J: ~! Z _' ]$ r" \9 [! y3 u' w5 G _- R
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。' y+ U3 d$ Q. o9 M
# h0 P# K: R7 G1 `1 |: K* c) M1.下载OWASP规则: g6 l8 S' ^: }
2 x( K8 ?! a3 Z" l& F4 V" j% O6 [git clone https://github.com/SpiderLabs/owasp-modsecurity-crs8 O j5 h; D o" s
, m! E6 ^4 Q& E5 Bmv owasp-modsecurity-crs /opt/tengine/conf/
3 T4 C! {% |$ L! j1 J. E' L f0 u1 l) S
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
+ k2 g5 d! w: G1 h2.启用OWASP规则:
5 F# {# \; g- R0 v [/ h6 T6 [) c& l+ _4 F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ K3 N' @; _7 {- _# A0 H9 R8 f
( S5 o9 E& g8 D" e2 V编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
; q- z7 E2 @ u$ x" L% b3 q4 g, p. d7 ]
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。/ h/ h" ?# B1 x% a/ ^ @# ]$ r( d
) K4 e( w; q0 ~3 N' q- I
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf9 R+ l2 ~) @4 ?/ t, v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 L9 ]. i9 M+ D5 n' n2 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! U g# N; Z' TInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 W" S- J# v1 v; {
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
_" T8 ?" d, U, I3 mInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% ?3 _; Y _5 H7 q0 c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 P$ D' T. U1 m0 i: g/ Z8 f6 J% U五.配置nginx
& a! S6 @1 m1 G, |. l! A
" ^' U6 i$ I3 F7 K0 O在需要启用modsecurity的主机的location下面加入下面两行即可:* A3 ?1 z5 ]0 w) ]3 g+ S
5 R8 D9 D2 }3 W% aModSecurityEnabled on; , l6 Y$ L( S+ g' {- {
ModSecurityConfig modsecurity.conf;# l) v8 a6 m8 Y8 s$ Q+ ?* G
下面是两个示例配置,php虚拟主机:
( G7 M8 F! M% P' O1 Q% B- W2 S
8 K6 d( H* c# W2 _0 Q5 w) Userver {: i7 e) C1 Q6 {) M# n3 r$ E3 r; e, [
listen 80;0 H0 K0 e' |' h4 @! y0 @3 n0 x
server_name 52os.net www.52os.net;
& O. I/ a/ b2 w; H( k & P, W: Q) X: n/ [/ z
location ~ \.php$ {7 K, R& ~. G( X9 g1 L/ h# W' c
ModSecurityEnabled on;
- L4 C# f# V" L8 K. ~ ModSecurityConfig modsecurity.conf;
/ g+ y6 @0 }+ ^0 }* Z
' S, V- d) D) ?* }2 H+ @# _8 C5 G root /web/wordpress;5 l2 K; \9 x. {. J5 H, k' b
index index.php index.html index.htm;: K: x) t) d. J$ A1 n7 W4 y
x8 k9 `( z3 d" O+ a
fastcgi_pass 127.0.0.1:9000;2 I/ c3 z5 A3 F! c" J7 f/ G
fastcgi_index index.php;
. f* \" K) e) ]' ] fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;3 {, L! y5 I, |4 u6 c; D9 X0 Y& A
include fastcgi_params;
+ Q |; b' r/ D5 Q }
% g+ T; H6 i! ~, t* L' e- A; m6 \/ Y v }/ ^2 ]; H+ B* ^
upstream负载均衡:
5 q: L& v$ d- X) D
\& s9 w+ a! A; q8 B1 B- B' oupstream 52os.net {
; ?- U2 U5 g* j k server 192.168.1.100:8080;/ M0 b( @7 O2 V4 Z7 x* l$ b
server 192.168.1.101:8080 backup;: Q7 H! E: l: G7 W, W5 C9 S2 @
}' [" d3 o) g7 k: o. G
3 \; s$ Q5 [: {server {7 B. e( y! N! t# y
listen 80;6 X/ S; I" R% {
server_name 52os.net www.52os.net;' T D" K/ x3 a
( e6 \2 q9 i( A9 n. v( n0 e
location / {
. M$ b; b: T9 S( B9 [/ x/ } ModSecurityEnabled on; $ P2 E' ~8 [3 s: N" ^+ K: C k2 ?7 k
ModSecurityConfig modsecurity.conf; % E+ T, o% @% D# ]7 n4 t( b
- T G7 z1 m1 v$ R8 `/ O proxy_pass http://online;/ j4 l3 n; P' c- g2 |
proxy_redirect off;5 q# X( @; ?6 ^7 \' P$ y& ]
proxy_set_header Host $host;
. S$ s. W% o3 z/ |/ G- j: f! G, t proxy_set_header X-Real-IP $remote_addr;* k* k) C- j" x0 z' \/ l* R% n
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;2 _ K( i; X1 A2 Z5 f, ?, i# ]
}
0 l# p* I, h- h}
0 M' a& K# d+ x: c3 l六.测试
2 j7 a5 T8 ^% o2 l
; Y- B; E! R, ~) F' ?' l- J9 F我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
: N# c0 ]4 x+ I$ B* G: H" ]6 P& G- t9 e. [
<?php
$ F/ _, E" P; S0 t, y6 z7 b/ M; c phpinfo();
, d. `- ^' j) I4 ??>( M7 D2 X6 m! u0 s2 p0 E3 ]
在浏览器中访问:
) T2 y7 o' Q; S y
, Z: c- T3 o9 p i% w* {# jhttp://www.52os.net/phpinfo.php?id=1 正常显示。, f: w( }9 {5 i! B3 {- J
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 p, s L. K' l7 n8 z9 E
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。 o8 [. ], D$ n4 z' x+ S
说明sql注入和xss已经被过滤了
& G* a' U9 n4 X4 f2 F
! w' X4 ^* ]& d+ U! t. x/ w; Q0 Q七、安装过程中排错
% j4 w. H# E0 G \) [. R$ _
" P" S6 j( w; m$ G# N# q" U1.缺少APXS会报错
; ?# b; Q9 o& ?: V7 u) [
* e# C9 ~) m3 Econfigure: looking for Apache module support via DSO through APXS/ q6 n) k/ a$ Z" i
configure: error: couldn't find APXS9 v$ t9 n7 w4 t& l7 M
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
9 n( q% {2 h% x/ l2 @! x0 I8 \3 w解决方法:' ^+ p& L% v& D1 H d; E Y; Z, f1 N
9 p) q7 {5 X7 `yum install httpd-devel
0 G# E" u; A, Y' ]) H9 |1 _$ ^2.没有pcre
" B- [, \- ?! _0 i9 T& M8 G4 N3 K1 @1 z( ~
configure: *** pcre library not found.
; l! n: f7 h. ?0 V: W9 [3 C* r% iconfigure: error: pcre library is required
0 o( b# }7 N/ r" A) u' M$ z解决方法:
! D* _8 M1 K5 ~+ H0 c+ n( t8 w/ p. W3 N: t/ q' H
yum install pcre pcre-devel- `3 y& n( \6 f- i" Q
3.没有libxml2
: i. g# O' @( V1 H f' O9 Q2 {1 b+ Y
{3 [6 P7 c! c, y
configure: *** xml library not found.7 l& e* W9 t4 R, j
configure: error: libxml2 is required
* W. r \. w4 Y0 W# |解决方法:
2 K8 ]1 |) K f0 M3 m: h: G9 R6 J M N$ g4 v
yum install libxml2 libxml2-devel$ E7 G3 d0 e7 u
4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ l1 Q# ~6 z( q* h) A k$ m$ Z: }4 I
. U% l) P5 U8 b0 T( N7 b' gTengine version: Tengine/2.1.0 (nginx/1.6.2)& M. k) J5 y% C! _2 ^$ d" T
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 w, z9 b$ c/ ?: v* k/ X
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ C1 P m7 }' M8 t" w7 T
2 V% s; A. |2 R0 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: k- W- [' k; u2 |4 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
" `4 Z- R: p+ W2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& i: u, ?+ {6 ?/ k( s4 A2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 T& m# D7 ?3 o* J/ g9 o2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
' Z7 V7 [5 t0 ?' a4 U! c2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
3 s7 u$ B+ g' ]2 D解决方法,移除低版本的APR (1.3.9)
! B2 k) J9 g& c: B$ q/ R
$ U, s( M; L1 uyum remove apr
) _$ i9 u5 w/ F" |! ]9 H5.Error.log中有: Audit log: Failed to lock global mutex
0 K( s6 [; O' H
: f( j3 l2 {/ h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
2 U' g+ Y0 g* t! s4 Z! Tglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
' D9 k7 S$ |! o% A' g解决方法:# M" T5 W3 X% m! A) b- H1 Q
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
9 a+ O8 ^9 M, e _" `% K
" b" j1 b2 N# {% [SecAuditLogDirMode 0777* N4 Y+ j9 [4 d+ ~, y M* \
SecAuditLogFileMode 0550+ M9 b# }1 t" w1 t T6 V& H
SecAuditLogStorageDir /var/log/modsecurity
. X2 L2 I2 R% N* g# a5 hSecAuditLogType Concurrent1 q7 u. h9 \8 A$ ?, v; w
参考文章:
g9 Q8 x8 K1 U- mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
6 ^4 H5 Q& `$ r& O! Jhttp://drops.wooyun.org/tips/2614 |
|