|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
* \7 E% P( j" t! K- r
: y( r$ p& @1 |, R/ j5 o一.准备工作
. V1 x l- H: Z) t
) }5 R5 \' |* G+ O系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0: E5 W6 J& z: k) T C6 w& p
6 l1 Q/ B) l$ \4 `. E2 ~1 \% T3 ^
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
1 w/ I# P7 i8 d y& T( D7 M4 i# ~) _2 R' ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# z! S7 Q( h8 z8 s4 `, s8 t) K
8 c% H* Y& \6 X: Q) r: E9 o$ N; MOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& v' e6 b5 l, l7 w: l% J$ i( l% r
依赖关系:
# J- G) o& c$ _- m- J# ]tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:6 w3 B; ]6 l, @' C2 i# c! m
3 S. N- a x; ^/ M# [4 p% r4 _% V0 Z
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel7 s0 [. i; ^# [8 R$ t* T
modsecurty依赖的包:pcre httpd-devel libxml2 apr
( x( }& K0 d) f1 h* E
5 k5 V; A/ c$ Z. S! U& K( p( Lyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel" b% b1 j) ~$ N+ v/ _
二.启用standalone模块并编译
/ J, C) O2 h& Q4 u; c$ w& U: T
! x7 J% {- Y/ M下载modsecurity for nginx 解压,进入解压后目录执行:
2 o$ ^: H/ \0 K* k. n+ e: E" C; i. W* }8 W( \0 ^/ O$ Y
./autogen.sh
" _) i- N" @2 p, A2 m6 l./configure --enable-standalone-module --disable-mlogc
4 N$ A) D- Q: s' ]2 V, u/ h8 Omake
: d' e q" |5 c; ^6 s9 z' a三.nginx添加modsecurity模块- ~: k; _# M9 r- _ n
- g. }! E- n% \: l& k! D" ~在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
2 _% b5 j$ D o+ h) Y- l8 a
; K+ s& Q' h& u4 `./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- b* z' q+ p6 g, N# {2 k4 qmake && make install" v, N" N/ L9 X q5 C* R
四.添加规则/ a! ?) ~/ [( A) p. [" O
& \, c- N8 O8 k6 P( vmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
. l7 m: t. p" w- A
; x( W' i) Q1 q1 n1 L3 A1.下载OWASP规则:
' q1 s4 R0 j& v5 f3 e3 l4 O; V; O# B- L2 e/ u. K- ~. F
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
@9 `' {1 d1 K" O5 F" h" V
7 D( c( I4 [3 q( M8 Q6 u5 |7 [9 b6 a. Lmv owasp-modsecurity-crs /opt/tengine/conf/6 ^- U+ T% `* a, B% Z+ C Y
7 [# Q, n, W2 G! ]) E" u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
1 s" Y0 {8 C# y2.启用OWASP规则:3 T" T' h- R; Q. J6 E1 Z. X
0 h/ N5 ]2 F( I7 S8 f# ]复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
; F% D$ _+ Z. }! f* m0 H) E9 }8 t( j- U6 ? i' |
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on6 s! ?7 j8 b& ^' b7 T% ~' c7 ^
5 N1 u9 H% b$ \& e1 a% B) l
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。. |) ^) c: q- K* L' h# Y8 i. R
: Z: l1 _* w4 ~7 o' t3 L/ K
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf0 c `( F& V& U( }$ O# I [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
# Z9 R" R S, i8 @3 [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf7 Y; _5 c7 L/ U1 ?/ T; X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 s& a* o" k6 C8 H( c% c6 h9 ]/ H
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
4 \! }$ V- Q2 A+ E1 ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf8 D0 C5 A) X# V
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf7 x8 n" W1 o/ A6 k6 K
五.配置nginx
8 M1 J O) }, k/ q
& S$ l+ w. R" z4 a/ V7 j+ [/ k+ C在需要启用modsecurity的主机的location下面加入下面两行即可:
8 g9 k1 F! R# P1 {5 C
* _( r/ |$ G2 eModSecurityEnabled on;
) x% R0 C$ W/ M( aModSecurityConfig modsecurity.conf; B/ e. Q* ` P2 K U( l
下面是两个示例配置,php虚拟主机:8 p( i) I, ~! z, F
: Q9 o- b$ b* I# g. g8 Iserver {
4 K+ x, ?+ r+ {+ Q listen 80;
3 k$ ?8 U8 A& u4 F3 x2 O$ [7 ^ server_name 52os.net www.52os.net;
, E: K6 x ?, `2 X# U 8 F! W9 Q6 M' }/ m' T
location ~ \.php$ {( q$ v' j4 o! y+ N, V8 P
ModSecurityEnabled on; ' L, S- {7 t2 j
ModSecurityConfig modsecurity.conf;8 {' \; M' J. A# x. [
E* i3 V8 W% ^* u( ?
root /web/wordpress;
* s" k) U6 t( F. m* D+ r% R index index.php index.html index.htm;
' ~: f/ {. v9 W# J
, w. L. t/ ~4 u, [& Z/ ~ fastcgi_pass 127.0.0.1:9000;
# b7 k% F7 L. Y! m6 K fastcgi_index index.php;
$ e/ }1 h$ `" w4 R" h$ u5 W% g7 |5 o fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& b# S- r/ P9 I/ M7 s
include fastcgi_params;! c3 |8 [( l' O" w
}* q4 S. E) g, Q& ?9 N4 ^' m
}
+ `+ ^/ g/ A( M+ k* E/ Lupstream负载均衡:( t" Q3 U; O% ^6 @& f/ _
" E& W% t8 h# j& Y' `
upstream 52os.net {
: U$ a" y$ P$ e4 m& S D server 192.168.1.100:8080;% @# r8 d9 B0 p7 M
server 192.168.1.101:8080 backup;
+ ?! a7 b( g5 H. ]4 D1 j3 Y}
; j4 v1 N3 f2 N9 c. o0 \% ^
" T" \0 \2 K/ }server {) i' B; P9 O2 o5 Y2 L
listen 80;
7 F1 u$ G4 g/ a5 W* L9 I4 R4 yserver_name 52os.net www.52os.net;
) l8 {9 h$ A9 z( Q- }5 |
: s& E6 b& a8 `$ w# Clocation / {& s) B" c: |1 H& D! ^, M
ModSecurityEnabled on;
% n+ X- O& u5 d7 K6 ?2 H0 C ModSecurityConfig modsecurity.conf; - |3 k" \9 v7 b) q3 q% b' c
# Q& ]7 a/ w- }* s6 D
proxy_pass http://online;; m/ F! ]7 R$ @0 p
proxy_redirect off;: h1 f# s) {- m# s) f5 K% ^
proxy_set_header Host $host;0 c' [; m- k8 o( T. @0 A4 e
proxy_set_header X-Real-IP $remote_addr;
) v6 U8 [9 o* b! l C proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/ i" s1 R; Y; c: u7 ]$ a
}" z9 R2 w, ]; W8 Z. c
}6 e9 I. _) K3 c" S. z f4 m
六.测试
1 d+ C3 `. P1 g7 t; q
* G3 u8 z B5 |5 c我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- U1 z% Z( L G% F Q! O7 X. j; x2 `
* B, u0 C& Y! X2 h<?php
1 l8 ]3 [5 R; I' ]7 ~1 Z0 N3 t phpinfo(); ' E1 V/ v" x* F C7 k$ e
?>: K: _- B) }' ^) I3 Z
在浏览器中访问:
+ d! A0 d7 j V+ Q$ G4 f- o/ e4 r" {) R2 u3 g6 Z
http://www.52os.net/phpinfo.php?id=1 正常显示。
0 u9 S8 F8 B/ E V6 Z9 shttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 X6 x, ]! Z+ c! Q4 V* q5 u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
x5 Q( G2 f2 ?5 G4 }说明sql注入和xss已经被过滤了
8 ~& ^3 {0 x7 G' c2 c% ^: L* Z2 U0 [
七、安装过程中排错
6 |4 q; q" V% t* T3 `5 D$ w* U2 u3 z. b( M+ M2 v" C
1.缺少APXS会报错
0 x2 E# V& ]/ ?/ H; [& @4 s1 D: ]% M8 B/ e
configure: looking for Apache module support via DSO through APXS" Z/ S5 d4 M* V& W Y
configure: error: couldn't find APXS
/ \+ @% n' j7 xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; {( ]; [" h+ v. s
解决方法:( G& j6 j1 |( S# U, T
- @) D2 ~1 L% ^: ^* s- G% N
yum install httpd-devel+ D5 Z. B# x7 ]+ h
2.没有pcre
9 |% W% t' U+ m/ C1 ?! O. U# t- ?, L
. |( b8 ?+ A! Bconfigure: *** pcre library not found.
9 e1 C; l8 L0 c7 qconfigure: error: pcre library is required
5 ?+ i' w- y5 F7 a3 N1 U解决方法:. e* W! N' ~2 v
) J0 g7 Q; r- g' Byum install pcre pcre-devel
% ~' H# V# O! M; B& ^9 d _/ w3.没有libxml2
0 X3 t" W# w3 r: ~4 t3 V
5 f) \% b/ K7 Q, x+ T. g2 `0 d7 h9 Y+ c9 Y* M. m, t$ c
configure: *** xml library not found.2 n6 r; M- S4 t \. o
configure: error: libxml2 is required: o- ~' Q) c( W* ]/ ~" ?/ K$ M0 b
解决方法:, V/ g# V; x! H
% Z, B7 I! \# q1 ]) Iyum install libxml2 libxml2-devel
- x% e; `: J& K" b/ q; s- s4.执行 /opt/tengine/sbin/nginx -m 时有警告
& p$ f: s d" l _: x# f
' V4 ~" J5 P0 |/ ~4 p: W' U& zTengine version: Tengine/2.1.0 (nginx/1.6.2)" P" P2 ~ s- P8 ?. I
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
0 ~7 f7 _+ J# L原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log' S3 U& {" W9 o3 r/ ^. j
& [2 x/ U' |$ i7 }# i7 f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 a0 Y/ b. v% N1 y
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"0 Z# k2 @! O9 T I: u6 H3 {! ` @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! K6 ]+ `1 R2 b) c1 f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
* H& w# {* a' I) J3 e/ N0 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 X# h+ q! ^4 e- _
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On. N, D- S+ Z. H, P1 y7 r' ]$ L. }
解决方法,移除低版本的APR (1.3.9)
: ]0 n8 G ? A- R+ k5 g$ c' I
. l* i9 M1 m# K( Ryum remove apr
# E3 c- d& I' E+ H5.Error.log中有: Audit log: Failed to lock global mutex
+ c. @- j6 ]" |* S9 p, f
7 Y" `) a# a1 h* F2 u: E# E/ u2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
: _0 ^ W8 Z6 X& bglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 Y: v% _3 S) W( u解决方法:
3 g c! @! w' m编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' G2 Q: P& ~+ A& P$ l
; W# i: u5 R4 i O0 h5 X8 CSecAuditLogDirMode 0777
$ H3 b+ l" k8 S% T' X2 k6 GSecAuditLogFileMode 0550
# a: x! F" h$ ]8 Z+ mSecAuditLogStorageDir /var/log/modsecurity
6 O9 b& J( }: w6 OSecAuditLogType Concurrent/ G6 X3 d8 v! { @
参考文章:
. Y4 A8 Q) }' O2 x; o! Ihttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ h1 J1 h* {" d' E( k3 S3 U! b# Whttp://drops.wooyun.org/tips/2614 |
|