|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# e- h% |2 ]! {7 O
2 J! p0 g1 m) `- K# S# }+ A一.准备工作$ S0 N0 s# o$ P" H" e0 j1 D x
0 j& |7 w- L$ G( x$ Y5 B, c
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0/ T2 T: q ^0 M1 L+ @; R
4 H. l% d; G- Atengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ h" {# ~7 k3 i$ |
3 L1 y9 O6 x9 Amodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
. n3 H& w3 S" \, X; B+ T. r" Q# F8 k! y' c2 U8 \: s4 h
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 p! W6 m0 B- x, T/ l6 t4 q* e
/ T7 L. _9 ^5 [' R依赖关系:2 C4 X+ K+ H* b9 g
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
" Q: J4 o" ?( P' Q1 V0 t9 O" `& f/ B1 w3 k2 q* Z+ r, ]
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel* c8 S, m* c/ C- l- L m
modsecurty依赖的包:pcre httpd-devel libxml2 apr
! P% J9 n y0 ~6 w. A0 |5 W2 V
@& b* q3 X( @ {" T$ F0 L6 Oyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
+ k, M6 H6 {- M* m0 R M二.启用standalone模块并编译. N+ F- ?" m& J& B" |/ o
1 x1 L$ Y' y I" s S$ g1 R下载modsecurity for nginx 解压,进入解压后目录执行:
6 m# N+ ^* k% a( E
9 l0 e. l0 r5 G0 W./autogen.sh5 R9 c5 h9 Z6 x9 {
./configure --enable-standalone-module --disable-mlogc$ O: m5 |% X0 B
make
& \1 Z* W) d# o6 ^. A) F6 l2 |& I三.nginx添加modsecurity模块, X D: b% F7 s
, K6 q8 f+ C& J8 n' s& P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, ^7 I' o) O$ A: x; s
$ i5 v i4 m! V+ m" [1 v& ~( R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% }% w& |; L+ G; g
make && make install
) c- U" P0 f/ E- f; w% A b6 Y四.添加规则
T. d2 L' R3 T: @, }3 y
* `; f, I# @9 M q3 pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 j: u* W3 ^ H, w7 r7 v/ ~
: t! f5 h( p* T- P1.下载OWASP规则:
# O' F7 i. V3 E$ i! ~/ J0 m: }/ ?/ w* h1 \6 G% {5 Y
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs/ H" H9 c/ A% n
. ` @' }4 w# s+ T
mv owasp-modsecurity-crs /opt/tengine/conf/1 t+ ?7 J0 j7 a9 U) V: G3 k! O
/ E/ j7 K( @! F0 l3 }" L1 u4 Q l
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf' ?$ U, O. S' ^: o* F# M
2.启用OWASP规则:' O: @. Y* T! b& M0 q% n# s
9 l8 X' v3 Q: b" x4 g$ [: p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; x, X' M) L0 `
0 c( [' r% @: [. {) r( _, A( l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! Q {. J: ~' s2 {* Y' q5 Z
f! J$ z- _' Fowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* n Y9 _9 d6 R' |! H
7 l4 L/ q |( _
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; o9 ~" T9 J6 m. g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& k) N* l/ }6 q) ~/ n, S! i
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
$ C9 T8 h6 L1 ~3 ~: j/ B) DInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! s+ |0 F8 q5 H, E2 ]/ a8 m3 b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
" N" P1 m! ]% }' LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
! r7 N+ Z" Z) c0 K7 G2 N" FInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
0 v2 R$ a: I0 y% R, t五.配置nginx
& K$ V6 Z- |+ N2 X; C! |
8 ^# V/ `3 N% V( T$ @' R/ i6 m! f在需要启用modsecurity的主机的location下面加入下面两行即可:9 [6 ]/ E! {+ {
/ W5 ] L; v/ ~1 t9 RModSecurityEnabled on;
/ n1 ?$ c z, ]ModSecurityConfig modsecurity.conf;
- o8 V- t- |$ {& i5 S4 ^下面是两个示例配置,php虚拟主机:
( s* j3 q- Q% f: _
$ G. M" m# b5 Y4 x u1 b' Cserver {
% o' Y7 y- }' \. T# Y listen 80;
# m, C% H2 C/ R* \0 f* E server_name 52os.net www.52os.net;
4 ~# d3 y1 }. s* S
q l. \# k' @# n9 D: ?# I location ~ \.php$ {
1 C: h) {& B V: c9 r ModSecurityEnabled on;
! m# y$ M: ^ |6 O1 @6 z9 C ModSecurityConfig modsecurity.conf;) W6 U6 J. C/ \3 ]7 @5 ^
1 l% L3 S4 e& C1 A root /web/wordpress;: V C. V5 Z, J. K
index index.php index.html index.htm;8 j3 h7 {. _ W" X* g* J
1 S0 _& [: [! C- A& a r4 `1 v6 ` fastcgi_pass 127.0.0.1:9000;, K) `0 u, o# r) o3 B; L* n+ O
fastcgi_index index.php;, i+ H: N5 Y8 R$ f3 i
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;! R& U6 a) p2 q: U' z% V
include fastcgi_params;
. b' I* f. j) ?' ]- C& }2 W }" i2 d' j1 P. P% u# g! j6 ^
}
" r8 ^' S+ A' M) c6 r: ]' z& mupstream负载均衡:
+ G0 n8 z1 @7 o& {1 g1 ?. R& g4 m
upstream 52os.net {' d: c+ |6 x2 X6 P7 g, n
server 192.168.1.100:8080;( _+ l: A) I( ] { J) o) f% A
server 192.168.1.101:8080 backup;9 A! a7 f" o2 q m1 d! F$ Z g
}! y8 Y( A+ }) y8 N$ K1 @# q# R
+ U+ U8 @4 o e; o9 s
server {) r2 ^& {" D5 f* B( h1 u+ A& n
listen 80;
( u7 P! E+ c {4 ~# t! a0 sserver_name 52os.net www.52os.net;
$ S8 T. J6 _! |( ~+ d$ {0 T/ x- p- V) w; A/ o. j
location / {
/ J" I) u: B% J- C4 h3 g B ModSecurityEnabled on;
' A- _8 f% T. C5 ~ ModSecurityConfig modsecurity.conf; d0 h' z( x4 A9 U7 }, v1 r; L
9 S' Q" a, k3 |4 \
proxy_pass http://online;
1 Q N, M E% ]) p4 _ proxy_redirect off;
% t- u* T, A1 X, m/ Q3 ^3 X proxy_set_header Host $host;
8 [, p, H6 B3 ~7 ~& }1 ? proxy_set_header X-Real-IP $remote_addr;/ \( k6 O) K; y1 |
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 D" Z2 k' X$ g8 L' b/ Z% ]4 n
}3 Y3 k! _4 ~# H2 C: }& ?9 h! H
}
2 q w( J& y6 D) A六.测试
( c/ O G0 `% k _
8 S7 _& \, Q1 F我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ J5 ~( S+ P& H w6 z2 r
% j3 b, ]+ G; C, k: A<?php
7 ^ C. y8 _9 a- y phpinfo();
5 T9 R" f7 b5 v* r, [7 X+ k?>
( W9 D# r, \# Y L( c: }在浏览器中访问:
F) R& ]2 [! U4 D! M
$ u; o( E9 G1 Qhttp://www.52os.net/phpinfo.php?id=1 正常显示。! {2 K- b+ b, x7 y' Q. j
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
2 G; O. F- g% [" zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
" {5 x* k: k& n' k说明sql注入和xss已经被过滤了
8 i, Z, X$ l# V2 \! C/ a
9 @; }+ c# T+ O8 _7 ^. I七、安装过程中排错
: s. t9 S5 H c3 G' @$ K$ L- Z# E: @) S0 c) z' c6 z
1.缺少APXS会报错
! h5 u! c; d' V( T
* V3 t: L& ^! _3 D1 g: tconfigure: looking for Apache module support via DSO through APXS; S: m7 }& X0 k" T6 _% K( q. l
configure: error: couldn't find APXS
4 V/ B( B& y9 Kapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
1 `1 h( Z8 Z1 u# ^解决方法:
: h7 N1 X# B9 V D
6 D, y. @1 v/ wyum install httpd-devel
; F9 r$ p5 z$ O2 [" S# @2.没有pcre& m9 A; o, S( G6 C# p
9 ? d0 Z/ G7 p& ^5 V, E( j# Yconfigure: *** pcre library not found.
$ i, Q T8 {, q: P/ K& S, }9 \6 econfigure: error: pcre library is required/ _: I, C7 t" l& j8 e4 V
解决方法:
0 q+ T2 e+ H2 \8 w. g
. K! v$ o/ K4 lyum install pcre pcre-devel y+ g+ F8 J- N
3.没有libxml2
3 F X2 E$ H Q- x8 h. N- [
( \3 r, T# W0 _
6 ]; `8 P S9 ~9 R7 o8 u/ yconfigure: *** xml library not found.+ H# U% t# v* c# J, H% m
configure: error: libxml2 is required4 ^& y% _' b+ a9 Z9 v1 F3 D2 a U
解决方法:
( j/ w- C# `7 q; m" C5 f9 J4 `
yum install libxml2 libxml2-devel
' y3 K; I7 k( L) n& J6 A4.执行 /opt/tengine/sbin/nginx -m 时有警告
+ M R M, l2 q" }- M4 _7 X0 Q
9 s8 [! t# W$ C1 ~: z+ z ZTengine version: Tengine/2.1.0 (nginx/1.6.2)& d6 \* z& _7 u3 k Q
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
. @+ S! T# H0 O' w2 l. A; ?原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ K. P+ P+ U$ c. T
8 Q) E! a( Z0 Z& E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; l8 a F% x B8 A6 ]- ?
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! i' h {# ~; F: h
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 E1 j7 y4 i" ^' t% `' Y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"1 o' I/ {8 V& M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ t5 m {' ^$ R% W2 W9 x# k0 E# w
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.9 q0 B4 ^. @! n! S
解决方法,移除低版本的APR (1.3.9) }5 _6 `. j6 g% r* u0 m, d
3 d3 ^! J7 w( H0 T' y8 {5 {$ F
yum remove apr/ T4 M+ }# i2 Q+ h4 E8 y+ [( k- [
5.Error.log中有: Audit log: Failed to lock global mutex6 t2 a; U* [, P9 o
8 |. @) t' D5 p ^1 I% I! e; J2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 L9 W+ x) k6 y( I& `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]$ H: ]7 v5 y9 z* [
解决方法:
7 ?& S$ N$ |! u3 U" V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: n4 Q0 C% @ b6 a# e F* ~( H7 ]1 f
8 H( Q% ~( j* k& w& `6 W* n! mSecAuditLogDirMode 07772 F9 n( ^) p. c* h. R
SecAuditLogFileMode 0550% A$ J' W8 F- L7 P+ Y; @) I1 w. [
SecAuditLogStorageDir /var/log/modsecurity* ]6 h4 Z4 l0 R9 t. ~
SecAuditLogType Concurrent
' j! d4 A" ?) O) q* U6 Q; k参考文章:0 u8 v# |+ K+ @: ^( N8 K. P5 [1 w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX& v8 y- J/ ~3 g+ o* K. H, z
http://drops.wooyun.org/tips/2614 |
|