|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。 ?# G& b$ M" P8 S9 X' P; o8 U
R7 v- r% I0 R; U o0 x一.准备工作
8 e- Z% c: ~$ y
7 {1 G; {, F- E/ c+ |系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ J8 N0 c; s" V" _" P, `1 ^0 x [3 i9 {1 H9 a4 J
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz7 I. i3 x) N& W. ^. O8 [8 p
6 b" w& C' [$ I2 U
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' Q3 T# F) g# ~( {2 l( X' B: ]
- f2 l6 V; o9 d1 t: @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
/ k& j+ b3 B0 |( ]# T+ j
1 ], A! W& p5 p) s2 q: N依赖关系:
1 ?6 }; I+ o( O: x; m2 x1 Utengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
$ O6 i; K7 K( G1 q' K5 B8 A9 b c6 B1 C* [! k# k
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
) Q8 j& W7 H' F5 o0 ]) H' fmodsecurty依赖的包:pcre httpd-devel libxml2 apr
8 K1 x7 h1 f6 t/ j1 r4 F) n9 P$ ^' V
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel; V: I4 l7 v+ H+ U4 v
二.启用standalone模块并编译( f1 c( {% {8 z2 H
& M9 \: \& v! \$ \; U7 l
下载modsecurity for nginx 解压,进入解压后目录执行:5 J2 w7 u) ^# v; I9 K( i G4 J
9 H) R1 z" g- e8 F3 }./autogen.sh
) c0 B' w- Z! d! B; [./configure --enable-standalone-module --disable-mlogc
' G7 s5 o! Q6 ?) hmake 6 c: k8 ^9 X4 f/ a- E1 k
三.nginx添加modsecurity模块
# B( y/ \( A5 V' O; D
. y% Y, ]8 r9 p7 E! T- E( O" B$ ^在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* W- B5 p# A6 j: Q$ ?; F" y, h2 ?4 @- J% x( t% @
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine9 {7 q" p9 B, U- \. G$ X2 G) O
make && make install7 Y' _. B7 z( T! H9 g8 c" m K
四.添加规则
- O9 \) y( I* J8 f6 E8 Z* E$ ]
6 Y' f/ ]/ p" ?2 X0 Zmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 d* f. a9 `0 X: W+ A
8 m0 Y' k- Q0 M6 t1.下载OWASP规则:$ P* i% f8 c9 m
% C# s7 V/ L; ?8 ^* r! H% p
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
# _/ C$ X8 j" x$ Q# c& B8 m% w" Z" e8 {0 X2 A5 j. Y
mv owasp-modsecurity-crs /opt/tengine/conf/2 [* ]5 K/ l. O' t) `
1 R; U! k$ n4 V% U* acd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 R& ^4 Y9 x O2.启用OWASP规则:* \- t6 Q9 l7 O4 m+ j
2 ~7 }" C4 j4 @; P! h! F* [8 s复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ o; t8 I5 c$ D$ |+ V; T2 Z
" k$ }$ n+ |7 c6 ?
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* R- w, j+ c K8 w! `$ j) Z$ e1 f
8 m! Y& k; w3 H' a! r
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。! W k# B, e# A! c8 r
! n: o; P! J: K$ c1 lInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& j; T- Q! v" a: @4 D4 d [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# g: S7 k8 O) \; C! X( gInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; [+ h' _& g; AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. z, o2 i# L ~. c3 _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf6 Y8 f- X# z9 [
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf; o9 d0 o8 ~7 g6 z( l% C+ ^! S
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 r2 i: |5 i6 {0 O% |" W) k
五.配置nginx4 p r+ L5 b- `: \8 l, t+ h j
. y8 {) w$ J4 [2 G5 S0 Q( u在需要启用modsecurity的主机的location下面加入下面两行即可:
1 m! i* I- P; w9 m# X8 p1 [. m% f* U0 l: G5 {% V; o
ModSecurityEnabled on;
* a [3 i/ K( g) r! n4 T" ~! f, e: t# AModSecurityConfig modsecurity.conf;/ I6 z7 I6 N0 t" u3 M2 K
下面是两个示例配置,php虚拟主机:
. Q! A3 m1 y" Z9 t- s4 `
: [9 `7 L2 X9 v# R3 vserver {, ~! j6 I5 p+ ^8 J |7 m0 O+ M
listen 80;
; D& u" H5 w" ]" n# d. a: G0 c server_name 52os.net www.52os.net;
! i+ o' l5 }* a9 ~6 i % s" C7 m; f- O. M
location ~ \.php$ {
" m# v S7 T7 d% u4 z. c ModSecurityEnabled on; ) ^; v* G3 S; a, @! N: E
ModSecurityConfig modsecurity.conf; W! p: l9 d1 `/ n3 z
$ n! @; B( A* k9 [ root /web/wordpress;* u$ f* g; ^9 L
index index.php index.html index.htm;. u9 o! Z M* x( M
* X& C: N3 ~4 b* e- D0 r1 A; z
fastcgi_pass 127.0.0.1:9000;
9 x3 T, l: D' V7 E6 O4 [2 V7 x fastcgi_index index.php;
6 j# s: d& J/ | fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- p# m. b8 `% ~# _& B include fastcgi_params;
1 e; }- K& ]0 g0 U }( \ m" U4 `- n8 @0 D
}2 Q. E) @2 Z9 g d( I- @
upstream负载均衡:
9 e& e; o5 N8 w3 O
' D: X/ c/ ], ^6 uupstream 52os.net {* e+ a( g& a& H; t. V
server 192.168.1.100:8080;! W# x2 L* G; S ~7 n
server 192.168.1.101:8080 backup;, d6 I' o2 |+ O( I. L
}1 m0 Y' x' ^2 W6 A$ `
% N2 A& }" |0 }# b: Q
server {
" L% w9 _' o8 n# J+ B# ulisten 80;3 v" M. s8 ]4 `; h! a [
server_name 52os.net www.52os.net;( J6 S; s5 t1 d- c) o4 [$ N0 ]
2 N1 Z' V, l: f' ~& n
location / {
" v7 T. h' {$ j+ H ModSecurityEnabled on; ' _' g1 {$ ?( N7 m
ModSecurityConfig modsecurity.conf;
2 N# C2 ]# J7 |0 r% R$ _+ ]' k7 J, h/ l1 s* |$ z
proxy_pass http://online;
1 A. T, d; g" ?" s b3 { proxy_redirect off; R9 U* A& @/ f2 Q+ h: `4 `# [
proxy_set_header Host $host;/ t" C3 V: ?8 s3 S' W, ]
proxy_set_header X-Real-IP $remote_addr;
6 M" H* @- f. [. u proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;4 L- b- w3 t& q6 P# L
}) \6 p6 w: n0 }- O6 c
}
% x3 Z" s7 k. z1 z8 c六.测试
+ L) G7 E( W6 L6 h+ ^$ L
6 Y' c) ~0 `- d, P5 z, a" b我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
& Q* Y6 z; G4 V3 t# P" |: H5 E" [0 K
5 F0 c7 k8 l7 c- l; W3 S<?php. Y" W5 g4 O0 p. A$ _5 M' e* Q0 e
phpinfo();
' t6 g: t; E8 r, {! p" j7 A; Q: K?>
& }0 X5 s* p) w3 O' G& L在浏览器中访问:
1 q! D5 @4 T, s% k: [0 ~0 G2 M! v+ \$ T, A1 t
http://www.52os.net/phpinfo.php?id=1 正常显示。
* J6 v5 E. T& R: h( `# Lhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。) O# T0 L+ t( C: o) [% [3 [
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
3 \0 s' u9 G4 e0 J- {) L. c" \( I说明sql注入和xss已经被过滤了
( y5 _8 \! r$ v2 _+ u% o6 c, K5 m" M9 _
七、安装过程中排错
6 e# N' Y6 W$ r" z% v4 Q* B( b/ Y0 V+ t
1.缺少APXS会报错
[7 F4 Y$ g$ o* H1 B( l& \% ~6 ~: Q' w+ N. S8 s# N3 _2 {
configure: looking for Apache module support via DSO through APXS
: L0 d8 X' d$ B/ M$ Mconfigure: error: couldn't find APXS: S# D9 ]3 V$ Z' [0 C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, v, F2 W, Q8 c c- |% F, ]
解决方法:
0 D; ]4 F# r- W8 g5 Z8 w7 U; P+ `4 _/ l$ v& u
yum install httpd-devel3 d5 d( v1 b! }
2.没有pcre
/ ~8 a" R; @- D) P/ W0 H, M: g' k( L
configure: *** pcre library not found.9 b1 b: e4 f z t& ^
configure: error: pcre library is required# ^% C3 P" d, T) a
解决方法:& E* T& _' w0 t! F3 n
" V) t3 D. ~1 n0 k5 r- y( Gyum install pcre pcre-devel1 z' Y4 o7 ]/ E$ g9 @
3.没有libxml2
$ U, P8 W5 m! `7 X* O
3 X5 J: d8 E3 X2 J6 J0 W7 X) n
configure: *** xml library not found./ b$ x) c/ B( f) R Y; d
configure: error: libxml2 is required
" E; ~) o2 w2 ?; k解决方法:- J' Y- m" D+ B# S7 q. c1 o
! I p: j% f! H( G6 @
yum install libxml2 libxml2-devel
) i! T- a1 b4 e& U$ |4.执行 /opt/tengine/sbin/nginx -m 时有警告
' I4 `1 u2 n0 Q) l3 i5 z7 H/ _
# }' x1 M0 E, _1 ]% o* M1 Y S, LTengine version: Tengine/2.1.0 (nginx/1.6.2)8 v4 |0 e5 ^: ^8 E; h- R
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 l* Q l5 b$ u8 k! V8 ?% _, _
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
% g1 ]; ]; N$ I* ]7 O
2 U* A% u$ y6 c; Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.3 d$ l! g/ r. G* S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
( B6 T; G( W( x3 k2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" i5 h$ _. Q* A/ S" D
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
7 E( x* r* b5 a4 w) { |+ R6 d* ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6" T- B& x8 Z; W$ M- @/ ?! |# @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
% o- I! o5 c: C; U1 x+ r" ?解决方法,移除低版本的APR (1.3.9)
$ ~* W% p" s$ L0 F7 g4 q$ I i" V1 I8 P& j2 x& o
yum remove apr" M; a) B$ A4 Z, F% q* e; N
5.Error.log中有: Audit log: Failed to lock global mutex
9 K: a. f% Z: B2 M2 {7 J* y b5 L0 e! p: t# {0 K( y9 r9 M! h8 G
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
% x8 _6 }4 U5 H2 qglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) z2 @ L1 ~% O" }. h解决方法:+ y/ ^6 W) Y8 o9 H V' Q: t
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
0 N1 Y, U* ~$ C$ @
% J. Q" t: L- L' p; oSecAuditLogDirMode 0777* O: N* |# \0 w2 p( G
SecAuditLogFileMode 05505 c4 j3 d; z3 L* }# o4 P% w
SecAuditLogStorageDir /var/log/modsecurity& [ k/ M/ S# G8 Q# H3 a8 F
SecAuditLogType Concurrent5 R1 Y# ] e- _3 U
参考文章:
) y7 v) i* b! y+ B) g+ B3 Shttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX/ M' l( W: v3 a& y' o
http://drops.wooyun.org/tips/2614 |
|