|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 E6 A! f; C ~ ?5 @& g% ]1 a
7 h' L, _4 k# Y: r- G4 ^一.准备工作
6 ^, Z6 O! ]0 O' a. F) N2 x2 b
2 T6 |: g% D. A. R系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 ]: T4 _5 c3 V
; t k) R `: a: B# t. l; ytengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 k$ L R4 t* m* X
" t5 p) U0 c: m. Tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
6 |+ N! M( C2 |- U6 b" J, H0 ~% H# S# L% g; {( M! \& x
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# r3 {- x# F9 c8 J0 h9 N8 ~
% k/ o; ?, N( B依赖关系:& d0 ]; G0 _+ X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- k0 v' w6 Y9 [8 ]9 h
0 [ v6 C( n- M- S6 H1 t, [% hyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" Y* }4 H7 g7 G. S9 r
modsecurty依赖的包:pcre httpd-devel libxml2 apr. s5 {3 {/ H+ x7 ~% Z
0 I" |5 i& T' N: h# x, d3 r
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
/ c! I8 K) y/ ]$ \8 [- Q' n. @二.启用standalone模块并编译
( h7 U3 c' c( u `$ b- h
- _% ? _2 Q! }2 K: \; l+ H下载modsecurity for nginx 解压,进入解压后目录执行: U% R# ~! u, Y: w. j" A
; q& ^7 h4 S7 t8 e# |; x- u: O
./autogen.sh
" Z& \8 s, N) S. y0 `+ [( s./configure --enable-standalone-module --disable-mlogc
- ]9 N2 Y0 n- T3 i, W& @make
# e2 y6 G7 I. k7 x三.nginx添加modsecurity模块2 @. H1 w1 `: R; ~9 E _' `
/ }. W6 `0 U3 h在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& x; S6 p7 K4 b1 Q
# O1 K3 F7 E4 V6 k. v3 D7 R./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
4 J7 P+ I9 L( J' Q1 C5 Qmake && make install
+ q' [" P! u) X/ ^四.添加规则" V4 I' Y0 N" i% D* v5 z. T( i6 h9 C
- |; m9 u: [4 t9 f/ ~modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' b+ \- i! @6 I4 w: V4 {; c' R h1 U" O/ t1 Y: V6 n* E* X
1.下载OWASP规则:
$ f( m! {7 _+ W. W
8 t) v0 s* h! N) agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs- ?* m! e( k' T
5 }+ W/ Q: Q! P& W: C
mv owasp-modsecurity-crs /opt/tengine/conf/6 y% W+ ?# K) G% b4 |
0 E( V5 m5 g! L& w, }) m) t
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* e# I& `7 u9 z6 e
2.启用OWASP规则:7 A. x' L1 {$ N8 I
: y8 x9 k0 W7 T# `( T- h7 `4 f
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
' Q6 g5 f1 b& q/ s- n6 J1 Y! L7 [2 ` @
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# O; x# c; H, k* _1 P0 w
/ `$ |3 ~3 y' q, Eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
% B* K3 u2 K4 l+ t
' n% F1 F9 U! p) kInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf: f( S+ q2 j/ r; e9 J1 ` k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
a7 d7 S ]5 b5 L+ HInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* z2 f' W( l- q. }% Q; h
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 y: ^4 \# I" k) N4 e3 Z) M3 {Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf& Q3 ^6 x7 @ @5 r
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. G3 q5 ^1 q# C/ K) [
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 Q; o6 g" A) t1 F9 H j+ Q# R五.配置nginx
4 p8 @, @+ r* T. v
% @7 E+ d- K: N5 q在需要启用modsecurity的主机的location下面加入下面两行即可:; _ o) T% N! C, v; o% ?( I
: V8 z; K! d( r7 P' p( I
ModSecurityEnabled on;
$ f/ |3 R- J W4 B8 ]" C2 SModSecurityConfig modsecurity.conf;
N( \8 p/ \- e7 r6 W5 T下面是两个示例配置,php虚拟主机:
. B* l |7 Y& j% |2 M$ d/ p3 h, f: r2 M& Z) j# \; X4 B+ }) B
server {
# Y, D7 s) K9 r- t; q8 a listen 80;2 i. K' ]5 s5 o$ u1 L% T/ {. T% U
server_name 52os.net www.52os.net;9 ?; [4 J& ^3 L8 V; [
& m: {( b, [1 u% a; \ location ~ \.php$ {
: R* n5 v) d6 |$ @9 z* l, p ModSecurityEnabled on;
4 H' ~+ c4 y% o9 O p6 _0 R* z3 Z ModSecurityConfig modsecurity.conf;
3 j8 ]* j2 W1 U- z3 P) W2 Y2 L- D* k
root /web/wordpress;, h' |: W# m% R0 ~- x: ]' `
index index.php index.html index.htm;0 c2 D0 b) Y" o) T. ?5 h* d
: b' j& Y: a/ Q: @& I3 ~' e
fastcgi_pass 127.0.0.1:9000;
$ b. k' S; @. i8 a0 I, F fastcgi_index index.php;
* m L6 \) c$ T: U. u fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;$ H" }8 P0 }3 ?, X( q" @7 Q0 ~
include fastcgi_params;, p6 V( _- R1 @0 R$ w
}
. B; L9 y% c* S$ ]- L }
- ?$ x) e1 v" s! dupstream负载均衡:; b4 D" L* w3 S0 `1 f2 Q. Z' o+ `
' C a2 y* e+ n8 C
upstream 52os.net {
x1 Q$ t- R2 d9 L/ s& g+ i5 H- ? server 192.168.1.100:8080;
" M; }, k" {/ c3 H- S server 192.168.1.101:8080 backup;
/ K4 Z4 w s8 Z' I}
- M. D/ N: @1 M) B9 C1 {5 D* _6 V, Z/ ~! W: n3 e% A/ y
server {
6 V/ d" w- v, wlisten 80;
6 E7 w. p/ f3 R$ j2 ]server_name 52os.net www.52os.net;
" l" o0 ?8 j2 Z- Z# W4 G
5 l# ?$ Z' T. R) tlocation / { ]; z1 z0 u7 {1 C: Y7 D
ModSecurityEnabled on;
E( p. k# Q( g" I. a- Z& o+ T ModSecurityConfig modsecurity.conf;
* ^% Y" J' X$ g# |+ V, `) n4 \" |7 i% x) {9 h3 \
proxy_pass http://online;
0 i( |% |& a* ]+ w. D9 V proxy_redirect off;6 t* k: c" w# y# D2 |
proxy_set_header Host $host;
4 V4 x+ R$ H) g. c7 ` proxy_set_header X-Real-IP $remote_addr;* Y3 H- J5 |5 A7 D
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
; U7 W3 m& x! v A" i, a }' W6 o- [4 }9 e; L# i
}
1 _( G# `% A: A f) B- g7 {六.测试6 f I3 j [( I; D
) s' L+ S0 ]/ F" e7 a
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:$ X3 N C6 y. s j4 t
) f$ j1 H# g% e: M6 B2 K<?php
8 W/ W$ D* @, J+ U9 c phpinfo(); % L5 a0 N+ s! n8 y$ f. K" b
?>' w: i5 _- [9 A; i* x( {5 f
在浏览器中访问:
$ V2 l; }5 g5 y [$ U8 a8 _ s
/ h# \ f3 e9 F }; Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。
+ B" b4 T- q4 }4 thttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。; f( P% f7 I5 C( f% ?% @' A, v$ F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
6 _+ W& O% | I4 v) c1 d" v说明sql注入和xss已经被过滤了! `8 E# D }: J3 \. ^2 H
! B; u$ w+ C9 C8 v4 Q
七、安装过程中排错
6 ]! q- c8 C; G4 N# t+ x/ a5 b- i- |/ T9 ^ f4 o/ } c# K
1.缺少APXS会报错- t! v: M( J3 D' Z7 ^
/ E6 w' M' A0 w; u1 A+ u% oconfigure: looking for Apache module support via DSO through APXS
/ e0 s: D E; u' {, dconfigure: error: couldn't find APXS8 A) B- b5 C; A. U; k- `
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" ?5 I" A( S# `8 X
解决方法:
+ N. Q9 Z! P: x% {% R: [5 c
" V5 k3 R) o$ j' G- L( ?yum install httpd-devel: q3 `9 _3 w& @
2.没有pcre
) K7 m" L7 _5 \) Z- k' b! j+ x
) i& Y6 D# L0 l# A# {3 H" j$ O* \configure: *** pcre library not found.
$ A, J6 K, D- `3 V, i2 S9 kconfigure: error: pcre library is required d* o1 }' M% E0 Q: {3 U
解决方法:6 Z1 S# C4 s, z- g0 z
q. D [2 Q) M$ F' O# X1 dyum install pcre pcre-devel
' n7 l, Y. I# I) \3.没有libxml2
& [/ r+ F5 `7 L% y; h& y4 `* o' m8 \9 Q) U2 l- [- O5 m& `
; h8 T: j2 D* i7 Q0 v
configure: *** xml library not found.
' ^& e, h& Q/ {8 t0 Wconfigure: error: libxml2 is required, j# i7 I& r! I r+ g
解决方法:- F( R2 |% T4 Z; Z5 t8 A5 l5 F
2 h! f4 @% b( {3 v9 X
yum install libxml2 libxml2-devel
* y$ `, `8 {, k9 J$ \9 R: r" @" e4.执行 /opt/tengine/sbin/nginx -m 时有警告9 H* A6 m6 _5 E4 F+ k
( r; E8 V7 t* w3 j$ o
Tengine version: Tengine/2.1.0 (nginx/1.6.2); v: ^# X. G# o Y# a4 v1 X
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!8 M! D: ]9 R6 p I! h F
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' T& b) F- i, D4 e6 [8 }
. K1 ^+ [6 V9 f3 ^5 |7 [4 ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
! i1 A+ F3 p4 h( x) X' d1 e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"7 p2 {0 ^! v8 U" P9 K$ D8 G) X
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!) a- O; |" E, b4 t S# v
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ U6 |7 B( W+ _+ R& N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
4 @, A) C# `6 R E6 q. J# ~2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
$ d3 m- e9 }8 N解决方法,移除低版本的APR (1.3.9)- ~6 G- A# _! m. \
% P, [- `! h) Z
yum remove apr- p9 N& Q/ r5 v7 _3 S; B
5.Error.log中有: Audit log: Failed to lock global mutex! a6 o& V4 z/ t& I$ k
) Y) o/ P+ d+ s
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 o- ^0 I/ l) sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]4 h& Y' @' K% A' D
解决方法:, b; ^" S8 ], y8 c, h" Z I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
& J5 @2 G: o7 i' n% J2 r9 H/ _) s( _, b i/ r8 \7 H2 \
SecAuditLogDirMode 07779 t, ? B$ I: V8 T
SecAuditLogFileMode 0550* ?+ f, X! j% s4 O
SecAuditLogStorageDir /var/log/modsecurity
9 @6 r' C( \+ k, FSecAuditLogType Concurrent k% ]9 R: }( j1 e; j; G
参考文章:
* j4 ~2 }' h Mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
, @) o& ?, v/ x# whttp://drops.wooyun.org/tips/2614 |
|