|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
Z$ B$ n$ g* F5 H
) c' Q ?- ?+ v" C一.准备工作
' m4 V; f# M/ z( g3 t& s
* L( l1 P p+ b: ~# ~1 s系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0+ b$ W( K2 H6 \
7 ], ?/ d/ z2 i7 j* J( M0 ?
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* Z) o' N0 d7 S9 w0 J* J
: w5 I5 _ x$ R! Y9 o( j1 q
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# |; s+ L% n: G+ D9 U
5 j: y# h7 Q9 }- U
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
( u% w7 z: E. g5 X* i
7 e# P1 e- x( Y: [7 K依赖关系:5 l" o% |. a- g! T2 p
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:* E% B& O9 B9 Y. D( f
( [' M! |/ x6 \" U2 Oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
2 A8 S: |- |7 A* s' rmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( \8 V# }! f. |' f1 ?
; O* v0 W y1 |+ Xyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) @2 p6 u2 R/ y9 D2 d# w+ f; X; P二.启用standalone模块并编译
) V& X2 [& Q* J. `7 S$ y2 g p5 T/ Z/ `" P6 |
下载modsecurity for nginx 解压,进入解压后目录执行:
e& W; X2 o5 u
4 ^& C, J: M; c F3 [./autogen.sh n. D8 o) C1 ?! @4 y9 }
./configure --enable-standalone-module --disable-mlogc/ v! ^' X: P3 F2 o
make
3 c$ |- |9 e$ |% J; ]# R三.nginx添加modsecurity模块
p$ W. \9 ^) r" }" R% W) O. j
2 `# i, V8 E. {7 J' N. t在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:# P* `+ l) J$ d- a- _% L
. {2 P0 y2 a. |2 k5 Y./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine0 _5 }* m' J( ]& C: B/ B: K3 F
make && make install' q) N+ w/ |: Q
四.添加规则+ A! h K+ e1 j
" j- k% v3 h3 D
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" ^# E" n7 y# c4 h+ d* N4 v. v
: L4 S1 B8 T3 }! J# |1.下载OWASP规则:" B7 I7 m* n) @1 U
* T! E' V1 b9 q) k; ^: c0 e; |# E
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ N/ e7 Y ?4 g6 O( O/ v: V- M, l& A* L$ q2 e
mv owasp-modsecurity-crs /opt/tengine/conf/9 y3 Y" I$ e$ q6 X1 v+ ~2 n/ a
0 T' \( S U# Y" @cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
0 j% P" ^3 ]' V( \0 }2.启用OWASP规则:3 }1 _* z( N' i' A" |! O0 Y3 ^
% a$ V9 x2 W3 d2 w, x. S复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 J& O$ s/ F4 ?5 I. U+ Z8 b7 C ~
" `& [: h4 z8 d编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" k, V, i: t2 M) g$ c. [# [" j0 Q4 z6 f( o# G$ x
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 a# N6 Q) a. t
+ |; F% W& W4 bInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; ]2 c3 }5 a$ O7 I; W8 j0 Z6 y6 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ R8 o% {/ k7 P }# `8 g; N
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf4 |, F. a4 e# |9 s2 Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf3 m7 R, f1 \! w+ m
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
% H) m* x$ V- I1 s# \Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
5 A* Z, [8 y5 K* A4 y/ iInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 K. \2 B) l6 x" _五.配置nginx: v2 h+ W0 ^4 m$ L, Y' l* _9 H
% V+ {- T3 x5 k4 p在需要启用modsecurity的主机的location下面加入下面两行即可:$ z2 |5 x6 S- L" w- U: Q6 b, o) o1 v
$ u9 M- q7 ~; }& bModSecurityEnabled on; 9 Z, O0 B" K3 s
ModSecurityConfig modsecurity.conf;# T _$ i4 [! ?& ^
下面是两个示例配置,php虚拟主机:
" e9 I& \1 V4 O# e
7 `4 N) m: N8 Q' F0 e( z; Rserver {% C/ \. i9 ?# L U0 V9 K
listen 80;
" Q' T" m; b: x9 \( _8 p server_name 52os.net www.52os.net;
3 }& n# p% ^3 J9 s+ E( v6 U
8 ? H* x! \8 Q' A2 ^ location ~ \.php$ {
5 R; G7 }% E+ U f) s, u7 b' d+ i ModSecurityEnabled on;
& W0 r! J8 j- r' @+ [6 j: Z! \: @# k ModSecurityConfig modsecurity.conf;$ c. B2 U5 l* p& r
. E5 Z- o t0 [6 B$ L root /web/wordpress;1 ]: |* N1 h, Y
index index.php index.html index.htm;
* v2 D2 Y( z/ z4 T4 L
h4 R' H0 L$ H6 i fastcgi_pass 127.0.0.1:9000;' }" Z: K3 @8 s- K% a0 N: L- H
fastcgi_index index.php;4 j$ y* G% ?) }3 s4 @1 I: j1 ^ t
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;) B* K, N% Q o
include fastcgi_params;
$ S3 C, Y8 V _6 I: D }) k$ I& a! ]9 O5 R
}
( p$ \/ p# a) H+ q. _. k) m6 F3 `upstream负载均衡:5 \. p" T% v2 L" y+ p( ^3 {$ |8 ~
1 T1 L: t: b0 ?- s$ h- [
upstream 52os.net {
( W. U3 ~3 g9 v$ o$ B) b1 P server 192.168.1.100:8080;
7 w4 c1 S' i9 b9 o" g# s server 192.168.1.101:8080 backup;
2 S: L0 J+ {) X- t6 z2 T}% A) L, B* B. i4 ? J
- E% @4 @3 _! j( U! |/ G% v
server {
! T6 N% r* J+ Zlisten 80;
4 t) C4 B( N% G( V# ]; ~* xserver_name 52os.net www.52os.net;
( d+ B. F) |+ ?" n' f, m0 ^* M' h: d: N$ V. I, _( q3 A
location / {9 u) v, d2 b: E
ModSecurityEnabled on; 2 H' x% E0 A% w# L* {6 R' t
ModSecurityConfig modsecurity.conf; * N% M- R. W0 f4 @5 l
/ s) j$ ?! B z
proxy_pass http://online;
* N7 k) j4 X9 Q( x! s6 Q' W# k9 P proxy_redirect off;4 J9 d4 K" {3 q; u. Z
proxy_set_header Host $host;
( X8 |) h# H' i9 O proxy_set_header X-Real-IP $remote_addr;: o1 C5 E& Y; ^5 M- j5 J- B
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;7 h$ w8 S) w0 v2 `& c9 `: w
}3 U4 d0 c' U8 V- i: z' {4 `
}* E6 K4 w. B% e) T4 f* g" F/ h- m
六.测试* x0 q4 e9 J h) Q$ |' }2 c
' w, p) J0 a8 m% `
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) a1 b( m5 o( H& O/ T9 M7 K6 T: G: P
( z9 u s; ^* ^9 Z: k/ ^; G* V<?php
; H! x0 w7 P3 {5 P) M! ~ phpinfo();
( V& D) H0 U, ^6 i?>8 }4 o# I4 r! L$ F7 o, T0 v& ?1 a
在浏览器中访问:
, {% E, d/ r* B+ v
: R' N: h; ]5 j; D) Whttp://www.52os.net/phpinfo.php?id=1 正常显示。4 H" `% c$ J' F* @5 X' |6 ^- q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
5 @% e% H9 F8 U: z8 Khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。2 s( e2 M: ]( ]
说明sql注入和xss已经被过滤了
! s# j* m# P- R: D0 z1 h
: w+ C/ G e% y4 R* X七、安装过程中排错
' n3 I$ O2 T& d% c! g: S6 f" n. |
8 [, Y* ~1 O& W1.缺少APXS会报错
: \, t! c# h( G. g7 z0 Q+ r2 g. [( v: e# y4 G2 ?8 _8 s" ?% E9 Q* |4 O* q: K
configure: looking for Apache module support via DSO through APXS- p! I1 _+ J+ B7 _
configure: error: couldn't find APXS
1 e. x1 N! y% E/ t% Z; iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。& d9 E) }1 Y# l$ B9 o4 X" a
解决方法:8 q0 D2 c' L8 n6 r# U. ^. C
8 [) H3 J8 Z) D f
yum install httpd-devel
8 z" D1 X: O$ u+ I2.没有pcre* k2 U9 ` w/ I& o; W6 M
% P: t# ]4 R- c1 E( H, Q$ {configure: *** pcre library not found.' o; ?5 L: e, m4 Z
configure: error: pcre library is required: Y; m5 m7 U- N7 W1 X3 I1 ?* f
解决方法:
) M# ^! s( v$ X& l, Z6 K
% V4 e |* a8 l$ byum install pcre pcre-devel
W0 K/ C, I. y- [6 \3.没有libxml2
1 e: Z8 s3 Q7 n4 X; W9 N
7 z( U! ^) Y5 t
) B ]: B! H- |8 F& Y% L" fconfigure: *** xml library not found.
, k! i( z7 y' v) v: r# Gconfigure: error: libxml2 is required% E; |/ O6 d4 o* h: I( B3 Y
解决方法:7 x% P" R; y) j
' I# @. o# l$ @, \# y- r0 V0 [2 {
yum install libxml2 libxml2-devel
; X, b/ L. }/ D4 w5 j7 ^4.执行 /opt/tengine/sbin/nginx -m 时有警告4 A% }9 R% ~( ~; r
& P$ w: H+ L- i0 M$ ]Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ a, D5 b9 o7 g: D: K
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- U) y8 ^" A/ B+ u( b' H原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 q7 y; z+ y" w' P& y& b' s
0 L" O) L/ |; J/ @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; o2 ]3 `& h* }$ D9 n- z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". ~; ^! q& X7 R. [9 Y; l% I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& d8 f5 b5 u M' }7 ~& k: m% c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ l o4 r7 ?' ]
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 F+ E6 z% v% y! S: K+ t8 U9 c
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
& A" {. T: z' Q& f' B: ]9 i) b解决方法,移除低版本的APR (1.3.9)- R6 r1 c% W5 L4 w/ B1 f" Y) y: ]# l% @
$ X) I% s. R: n2 _ ?% ]
yum remove apr
0 }0 \4 P W( O" n5.Error.log中有: Audit log: Failed to lock global mutex" `) o6 q/ s3 `' M3 j. t
" G8 S) P+ ?7 p7 }
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ; |& }. ?+ W- B4 _$ k0 j
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ m% a; h; q" T, U8 @
解决方法:
1 j& z4 D! k8 {$ h3 ]编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
8 I D$ r' \6 E
% R9 R1 f) }0 m$ v" CSecAuditLogDirMode 0777' t- `( L1 R& Q1 K
SecAuditLogFileMode 0550' O" g# u& u' ]1 d! [# w+ J
SecAuditLogStorageDir /var/log/modsecurity' g- e; L( E& B! t* [' w" Y1 a* j
SecAuditLogType Concurrent
" p) I. w" d" x5 o) J) a参考文章:: v1 u/ H+ H- p1 l8 S3 ?
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& p- a( W% p( V* E* u+ D, i) Shttp://drops.wooyun.org/tips/2614 |
|