|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。 s3 s) q7 R! c q( |# L
! C2 [: K, W1 J8 _
一.准备工作6 c% J( b2 d2 W9 ~% @6 k8 L0 ?
; ` ^* _; l3 Q$ H
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0$ Q+ S* a; d& K0 Y( u" f- g
3 k1 h' `! |( {! x# i4 q: stengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz g; H5 W, W7 G$ y4 |7 s
* C3 Q" z3 \0 D0 f* Fmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
" H! H! A4 B: T# q) I# u4 Y* C) r9 W. \# C0 d( B8 G( L5 X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
3 g. _: o* n$ n& o U; a" h- V6 W5 Z; ~6 E2 ^1 {6 _
依赖关系:& E! D8 K! c1 M0 Q9 z2 [8 h
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:& ~- R, l) U' w4 |8 ]/ M# H
# d. v# f$ I4 }
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& C& ~8 `. K/ g0 [4 amodsecurty依赖的包:pcre httpd-devel libxml2 apr" L7 K6 k3 f* W
$ j* T$ l+ w3 d: r- ?9 tyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' X+ p5 @# b/ V, P3 L1 r8 Q n1 [+ }二.启用standalone模块并编译
* [$ a$ b8 t* e; w4 X/ B6 g: q; a" {: ]5 r
下载modsecurity for nginx 解压,进入解压后目录执行:+ C8 ~1 Z, D" R2 G- Y1 ?
' s( U5 u" Z( A! C: `! z
./autogen.sh3 g" |* m+ N, Y7 A" @: S0 R& o
./configure --enable-standalone-module --disable-mlogc" z* [# j6 f2 S7 @
make 3 Y7 i* r+ [, M
三.nginx添加modsecurity模块$ H% n% a1 U; R4 P+ b
! Q4 L$ t4 {0 z: r
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* [. o: ~# i! ~- Q& H5 }6 _
- ]; ?! g2 o' {, r% t. `7 B./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
8 C( o+ ^9 s; ^& T' @make && make install
) v! X K8 q" Q+ }9 O5 Z四.添加规则
) t2 o7 g% p* K3 ~
4 b( ]8 t i1 v4 [+ Amodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
- ~8 R4 F2 K& J: J
! \$ u" d' q0 ]1.下载OWASP规则:5 {) k; u8 W O
9 b$ P" A5 ?- u$ i. ? D# J8 Kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
7 } o; H' n/ P* g
. y) W, k. W: U6 \1 N1 t; c9 Gmv owasp-modsecurity-crs /opt/tengine/conf/
5 ]8 t* j3 ~' ]4 n! O! F( p1 }. f9 ]% A" d3 s- z5 b" |
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf4 ^2 r; v* y7 e. V" V
2.启用OWASP规则:
1 I* A" J; V) G- i0 O" j6 {1 m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, |( x7 }, X; u7 o4 p/ C; Z
! r6 i" g% b' |编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( ], o' g* d4 _8 g7 L- g
+ j0 U# k# E8 d& a2 dowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
$ Y: C) C6 ~9 I' ?
3 \; C* R1 n. Y2 Q1 l0 B' n2 JInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf* F9 c9 l" Q# g+ t- ~) W' S. K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
- H+ Z- L5 @3 p4 D8 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' F. h' o. z/ R! w) ~2 ]2 jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 {! R# }, B# @0 L
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- C1 A- P9 B# _Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf5 V& ~- D# B4 q/ n9 z
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf% }' R/ s& Y+ y1 w
五.配置nginx1 X6 N7 _7 a+ U$ J U' w
: |: b" j' g/ M) f; y在需要启用modsecurity的主机的location下面加入下面两行即可:, T& I3 O, G( m4 N/ G
: D( P, @1 {, I% H4 X# S; Y6 G
ModSecurityEnabled on; - _9 a# _/ Q: k7 q7 @
ModSecurityConfig modsecurity.conf;2 x& @, Y5 ~$ K& \ X1 ?5 A( Y
下面是两个示例配置,php虚拟主机:: |: G U- h7 m# s$ d. W
/ ^* h; n) F( n9 j
server {) c0 H& T! B& n; }7 i
listen 80;( e$ A7 r9 }3 Y1 {( ?+ N
server_name 52os.net www.52os.net;" t/ u3 d3 {" v$ T! }- ?
( E z o: u( q- M! o7 V8 ~ location ~ \.php$ {
7 a; Y' ~0 L6 |* A ModSecurityEnabled on; * G" W5 t8 _3 u
ModSecurityConfig modsecurity.conf;1 h' y7 J$ z- S1 d2 {0 a, w" t
/ ~% T2 T" x0 {3 Z) b+ a. Q; i0 { root /web/wordpress;
- Z$ c7 E( ^/ r: }# ^! M index index.php index.html index.htm;6 { L. O+ O" Q" L i9 j- ]
! Y1 E B7 b( T/ J3 R+ Q% }: } fastcgi_pass 127.0.0.1:9000;
; ]- r+ K4 x, m7 T. Q6 ~# t fastcgi_index index.php;0 K- [+ K/ n6 d- ]2 r. u
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# I! o. [7 N9 ?& v3 d! C0 O* o
include fastcgi_params;8 i, v; ]+ b# Q6 Q/ a! B: ?" {$ H
}
$ c+ s/ D" _/ Q& T) q: V }
/ `+ K* A( f1 J! |6 \upstream负载均衡:5 O: U+ D2 V% J" | o @* A& G. O) Z
9 U: F2 S+ D! `6 k
upstream 52os.net {
# E2 H) g* ~. u5 ?) T- J K ~ server 192.168.1.100:8080;
& r& y8 y1 a% }" c' g0 | server 192.168.1.101:8080 backup;+ p, \6 f7 s4 a
}/ A$ z- j5 c9 H" l# x5 W+ d
2 A1 z# A4 N' \5 X: |9 M
server {
% U/ w) Y1 }: i$ T# b* \( ilisten 80;$ y5 f/ x2 Y7 V2 \
server_name 52os.net www.52os.net;/ @- T( u l' u: v; w8 \1 Q- Q% I
7 e( T0 a9 o7 u/ r5 I# r* Q
location / {
# W+ \9 d$ E! u. u) c' | ModSecurityEnabled on; ; C) }- i P( u9 o3 o
ModSecurityConfig modsecurity.conf; - \4 B- A2 B) Y7 }- G" U h1 }! }) R5 e
5 X5 S+ C: y; _; K
proxy_pass http://online;
" p( p# E( C$ U0 |0 m# [1 M proxy_redirect off;5 V/ h5 A, g9 }/ g9 m4 U' c9 m
proxy_set_header Host $host;7 d2 t7 H9 X6 c3 z* ]" ]4 F
proxy_set_header X-Real-IP $remote_addr;5 r& Z8 n) h: a1 e
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
( T# i% K: m+ V% r" s5 k }+ p( `& L1 U# t( z6 u1 q5 {5 P; ]
}4 M, ]! T/ G) ]
六.测试
# w3 m+ a' k" h7 g' T% G, y" {7 B
; R. [* P1 n7 V0 x3 H* n7 D我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:3 m# \/ ^8 y6 Z/ R" E1 w
8 u1 z6 \4 Y! Q
<?php( Z# D0 ?1 N2 c U& }
phpinfo(); 2 _6 P: D' |# m c
?>( Y1 V/ H" Y7 }: [
在浏览器中访问:
) X2 o4 {' }, ]% Q! W; x7 m% z* U6 M; \- s6 U8 \
http://www.52os.net/phpinfo.php?id=1 正常显示。
2 Q ~5 D+ D: h7 _http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' r8 w; N" a" N$ T& k x, h) m1 m4 @http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ R/ K9 z, e7 ~( i$ b说明sql注入和xss已经被过滤了
/ m: r( H+ @! r @
" k" |! J) \4 Y) u$ V8 }$ r七、安装过程中排错
* c8 H1 b5 P+ A" z" [3 @. p
* N% y. y& o+ t! Q, f1.缺少APXS会报错
" u0 j8 X& t4 o/ M1 E- R! {1 ?/ y* C. j% C( s7 m; h
configure: looking for Apache module support via DSO through APXS" ~7 H% P, ?/ e5 F4 o
configure: error: couldn't find APXS! W; z, L) |0 \ a6 B' `3 I" ]5 `% a6 V
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 V; c3 K0 t" @
解决方法:) Q6 |) h# a6 n, _: l5 F( N
$ u( d# c" W0 v8 V4 t5 ?" ~yum install httpd-devel
" e) ~6 g4 g7 e. k2.没有pcre
/ b) b9 C& ~6 u$ k% b" {, |/ K: }7 [' W
configure: *** pcre library not found.
6 A" a" ~8 v/ [) T6 A* C! T% I5 N- wconfigure: error: pcre library is required
+ ^9 s% o. ~5 }* _8 p解决方法:7 G7 z j1 ~' l/ {: @
7 x, a# m( U& m8 k9 g+ {' _& d" ~
yum install pcre pcre-devel
2 e( d. x1 `+ ~" J/ q% ]3.没有libxml2
) V; h' d9 Z$ g* f; _ i* X T/ C6 a* T
) D6 _: y* x, p! U& ?; c% m8 Q D- u
configure: *** xml library not found.
) }/ [; S4 `& E/ t1 p4 X+ _, Dconfigure: error: libxml2 is required
, {6 M# ?( B: H/ Z0 G, D解决方法:
( [# g5 T3 x3 o1 v) B6 I8 ]' w! J% A* h! W" j6 j) c
yum install libxml2 libxml2-devel
# L2 @5 a* J. I4.执行 /opt/tengine/sbin/nginx -m 时有警告
/ M% l0 R) K/ p, H
4 N& }. i- x7 H3 R# [Tengine version: Tengine/2.1.0 (nginx/1.6.2)
* O# u2 D; a8 n* Z Mnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& g# [5 Q1 [1 [- G6 r5 N: ?6 @# m5 ~原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log( o1 g# q% L) ^8 o ]9 ]7 c
/ M3 U- K" F+ W. U" Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 Y2 I# M# U9 _2 j7 S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 @1 u8 m; F1 M, b5 e* d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
f1 g' H- T" Z }7 Z6 J( m2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 a' e; e" K8 a( R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
^+ Z. g- L/ h: `; Q7 n: b7 ]2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.3 E5 Z- C6 Y0 ?8 ?
解决方法,移除低版本的APR (1.3.9)" g: ?$ \6 j3 p. s5 _
6 q$ v: q$ ~# a! m: {
yum remove apr! w7 n5 @# j8 S) k7 i
5.Error.log中有: Audit log: Failed to lock global mutex
7 J% K/ A$ J/ L5 _! g; u6 G8 ~/ w# i3 d: {$ }
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 g J% @2 {0 m5 h f8 s% Eglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* Y' O1 j5 z, q% h* s解决方法:
; ?. ?8 ~: y5 P1 |& a) A编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 O0 @* f5 ^/ o# P% N1 A2 @& R
& }% z, g O* _; M/ o
SecAuditLogDirMode 07776 n1 p' [/ d, `7 ^" Z: v1 d5 [
SecAuditLogFileMode 0550
: J) p& M- o. ?SecAuditLogStorageDir /var/log/modsecurity
. i4 I& ?" _( ?' N1 k# ySecAuditLogType Concurrent6 R! B& t/ u4 q3 N- K! z
参考文章:
! a* P+ K% `! d$ g, j9 [ ]https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% f4 E2 F( @3 W2 Yhttp://drops.wooyun.org/tips/2614 |
|