|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。. Z2 G/ N' t. f( @3 L, i+ B
" y. _8 v' R4 s3 B; J: l) J
一.准备工作
}! _8 a& p, ~0 i) L' s. t# B1 D6 z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 Y6 g: l5 p, a! [$ S& f' T+ K( G% D0 s2 m$ j) p! Q
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
6 o2 A# _9 K! P1 l( {; H/ S' T# U5 {+ P3 r1 Y3 z# Y" ?
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz- |7 ^- c0 Y7 e; D
V! F: l8 y) C
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs, Y/ `! A( r2 p c8 _
. {9 U$ V+ K( r+ L依赖关系:
0 o! e/ K5 @1 |; l) x$ Ltengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
A, y# F# M+ Z
: J9 s4 Q! D; Vyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel, [3 K: u8 |: X1 L* I3 K! g5 h
modsecurty依赖的包:pcre httpd-devel libxml2 apr
" \* J3 Q) W: a) k0 ^# Q- E8 N H
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
, D3 u" _2 X* y5 N二.启用standalone模块并编译" y8 O; ^/ p% A; ]0 y
' w9 v7 T% ?- R, n下载modsecurity for nginx 解压,进入解压后目录执行:- f( v* Y) z" L% c6 c4 j. Z
4 @( Q6 v) L2 ^ c
./autogen.sh
3 b8 P4 W" A# z- [, s1 H./configure --enable-standalone-module --disable-mlogc9 I) M$ |2 d) v7 W5 K6 d3 b; I
make 9 D# X. F1 Y; t& Y# I
三.nginx添加modsecurity模块
5 Q E7 P* F# `+ ~( N% |! n- `% K& Y* `; Y- j
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 m8 }, k" z, y# g
4 `# O1 R% j- W9 E! B1 w6 x( {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# q( S- a. ?: K7 r. q3 nmake && make install
1 i5 I: m$ y( S四.添加规则: l: i7 s2 ], R# H2 ]
* J! \2 ~+ a- I+ }; `* s5 Rmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。4 u; i& B9 k0 o2 K9 }/ ?, n
6 J# ]- g9 b- _+ c2 N, K1 Q
1.下载OWASP规则:& L% U; r7 o$ n% W
! R9 P/ ^- N' N6 t3 t' V6 Z, M: f
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 _" g. i, {8 F8 z: F1 y
/ p4 R* m( v6 @- m+ k% K2 D
mv owasp-modsecurity-crs /opt/tengine/conf/
2 B! I1 O0 T: Q. ?1 U
" u* S }# L: {# Dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf# |5 u; x4 ^( L4 F
2.启用OWASP规则:. v+ S" R9 Y. ^' c4 p2 @& J9 H
/ V- h) E3 P. O$ d# t) B+ P% A复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 R: \/ U3 Y: s9 T4 u$ C
! s E% C- Y4 {0 V编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
% D7 \0 q5 v/ [/ A2 [1 @7 ?9 j6 Z8 I* B5 [2 T
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。2 \1 l( T& ^6 U" D6 K9 A$ Y0 }" z4 t
8 z9 N) i( M, |- u0 j0 y* H, vInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf) X) w$ D; X0 z4 O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
& n) z5 I3 g. q R# L6 O; q6 n. @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ P6 q& r9 f/ mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, v5 j+ q# k2 k; rInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 G& _ F# @/ wInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf% [9 q( M8 |& X$ I
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
- a, _* c2 m; z3 o, l% c五.配置nginx5 F) d \0 y. L) F: H
; T7 A) Z7 ^3 | b3 t5 P& ~在需要启用modsecurity的主机的location下面加入下面两行即可:" t; u& a- y3 x5 R
6 p( p2 |& b' K0 UModSecurityEnabled on; 0 E& e% t7 C- a" }' u
ModSecurityConfig modsecurity.conf;
4 w- ^- R" b+ B* _1 s下面是两个示例配置,php虚拟主机:
7 X% K" D# I7 K, T7 U- x2 R$ h3 M
server {
9 U, |4 L E4 k% w! J- ^) W listen 80;
/ g! x# p8 P7 h' @% i! M server_name 52os.net www.52os.net;3 B$ k- Z) l6 a" ^! k
1 u2 O& _ z p3 { location ~ \.php$ {
! [* `0 v$ i( e9 J6 ]8 Y ModSecurityEnabled on; ' c& G9 u9 @ R) g5 P) a7 Q }
ModSecurityConfig modsecurity.conf;
* `3 h' s" M% s% ]1 Q
: `0 Z) M+ o0 L/ ]& U# [6 S' K root /web/wordpress;3 b2 s0 B+ f/ i7 |6 G6 i
index index.php index.html index.htm;* s7 `. [6 b* {4 V# L9 \
6 O" Y( b3 o1 c/ j4 E6 O; N" _/ e
fastcgi_pass 127.0.0.1:9000;5 v, H4 o3 M* |" C) j$ r! [( d
fastcgi_index index.php;
3 C( F* |5 S- V8 p6 n fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;0 L8 I) M% Z9 v0 d- _9 q
include fastcgi_params;& P- s/ d6 T# A; P9 m6 F
}
/ r3 Z8 F9 T2 E9 h! Z, o }3 O: X9 k8 N' b, K8 I! \9 C- V
upstream负载均衡:. g, I# @7 i* ? }9 S, s
9 p" p9 C' I% O5 _. c- a" uupstream 52os.net {9 i/ Z# Q p- d
server 192.168.1.100:8080;* g$ Y I7 N- m4 d% H! U& }
server 192.168.1.101:8080 backup;
: l; w6 B6 I$ g+ P7 Y& @}
- a2 |5 U0 h0 u) b; ~2 w
$ D' ?6 V3 m! o' s K0 ?% o$ R0 tserver {! A, F& ?) d; g% X, T$ t+ l+ e0 ?
listen 80;! s( C# a9 Z3 N
server_name 52os.net www.52os.net;
$ \: ]0 w7 I" E/ d- V! T, @7 R% v7 v' j) y% v" e% v8 s
location / {
/ u, t C* F- y# v) }4 ^ ModSecurityEnabled on; ; @; E. m% o; f; W+ y
ModSecurityConfig modsecurity.conf; 2 G% m4 Q0 q; p
" B7 H2 E$ E7 ?! Q0 K+ ? M0 |: }4 j proxy_pass http://online;" d8 ~1 L/ a. B8 P- l
proxy_redirect off;
4 s" t d: z) ^6 C: M! t- w+ ? proxy_set_header Host $host;
7 k; e+ r# \) z9 W, E proxy_set_header X-Real-IP $remote_addr;
' E% \' ]) M T" J$ A+ e* V7 B proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
) T7 c& K# Q6 R+ l) h4 L }2 [' t$ U$ p# j6 w' l
}
# J1 H. e, y4 S6 k! t* V六.测试, t$ e* V; n* V, q$ X
/ `0 r3 \4 n2 s C/ N# r我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
$ O' {: I6 R* O1 t& O. r
. g, b$ U0 x. T3 u3 A: Y. E<?php
, g3 `/ V* o1 R$ s% C, c/ m! H0 s5 r phpinfo();
$ D ~. `" |/ @4 A?>
7 W @* _0 k2 q+ f. C7 f0 T在浏览器中访问: i! `! G, }9 X0 c
% D2 }4 v- }& U G8 lhttp://www.52os.net/phpinfo.php?id=1 正常显示。
1 |8 G* C# {& N: Q" Jhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。8 o. M( I4 ^! N
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
) ?3 i* }- K) v说明sql注入和xss已经被过滤了4 }" X5 \3 h9 ]
+ Y. c; O, Z2 Z. f* E9 u; w' M& Z( H# K七、安装过程中排错( A; }& D+ I2 R5 m# _% Y
% S. \2 h& v: g7 {- ]) |0 L1.缺少APXS会报错
' Y' H" X) a0 H' Y5 q" t+ h! ^# r3 d5 `) F! b
configure: looking for Apache module support via DSO through APXS) ?7 d, ?8 w# M4 _% d
configure: error: couldn't find APXS `$ L1 W4 e/ {6 d# F( m
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) n. ?6 A1 Z. ~# n' N7 [( p
解决方法:4 V4 O$ l! e& y2 b+ g
, h U- f2 ?5 K3 F8 F, Tyum install httpd-devel
% D$ E% `" \7 p7 y2.没有pcre7 Z& z# V4 \1 w) f0 g' B7 M6 _
) J1 q8 E& T' t: c- G
configure: *** pcre library not found.
' ]) L, U) J' O" E/ B5 C9 Vconfigure: error: pcre library is required
/ _6 g1 t! q( ?3 U解决方法:/ V6 j7 ?- B, O0 }( r
) j2 e, K# D) B/ k2 Gyum install pcre pcre-devel. m( C# u { C3 V3 q/ N$ u! F
3.没有libxml2) P5 ^+ B2 d/ P* N' h5 A
, H: \# H: |8 T! _, d2 ^% p7 G( I6 o& W4 G+ D$ k% {
configure: *** xml library not found.
$ I$ s: g- N3 {configure: error: libxml2 is required. O9 r+ l1 i6 `( _7 ?
解决方法:/ f+ {# V6 M- F4 j$ i2 o+ ?! b
9 e; @- p# o1 x: L
yum install libxml2 libxml2-devel" P. Y( k; \. f2 H0 L5 b1 b
4.执行 /opt/tengine/sbin/nginx -m 时有警告& [- Z% g' d, l; ^0 `+ s
5 }# {# Z8 {! y0 FTengine version: Tengine/2.1.0 (nginx/1.6.2)
. p( _, [0 y' I' Ynginx: [warn] ModSecurity: Loaded APR do not match with compiled!% {+ w( f* G! q# K. L q8 h5 D
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; q3 F2 [% j. p% J
9 B( {6 h5 _, P# Z! ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& \# W3 w. M3 O+ R) H& W1 c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
4 Y1 a2 P8 ?5 B3 c; u* L) b+ @( `2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; T+ W- W/ e& u$ U
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 u/ p ^. |. Z& G+ a; t/ C
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") i+ p: G4 y/ P5 v4 N) r! d! z8 ~
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 h* R. O( J6 t: M5 x
解决方法,移除低版本的APR (1.3.9)
' O' k9 }+ K3 M) H1 I. N% e2 s# \( t, }- O# O
yum remove apr* N: Q4 @* h6 _1 D$ j
5.Error.log中有: Audit log: Failed to lock global mutex; u% n9 B1 Y, o4 d
5 q( D! v: z. P2 ?2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock , l% }. C6 o# R' ?6 X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. Y1 b5 L1 i1 M$ _6 f; o解决方法:% f7 P3 U( A$ R) l
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:3 ?, l5 N4 Q a( V
& M( Q! ^( |' z& D! fSecAuditLogDirMode 0777; G5 b, E0 u. l) |$ j3 `
SecAuditLogFileMode 05506 Y3 { \. I" |/ G; z' L! O
SecAuditLogStorageDir /var/log/modsecurity
: A. r$ D/ Z0 D" T2 fSecAuditLogType Concurrent, {! a% `% V. A6 `- l2 E- v/ C
参考文章:1 \, A6 F% C8 O
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. `9 O+ X/ h# Z# |0 Z$ s" f3 h" Thttp://drops.wooyun.org/tips/2614 |
|