|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 Z* f# L, R3 D" z6 B! Z: U
3 |% z) _) O. v7 ?" O8 U一.准备工作
3 F$ G3 B% C; [' D5 K) n( L, R
; D5 s4 U, z, Z( t; A, K系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0' J7 t3 H6 ~# N" a
) x: \. E+ T* e7 f2 x: p Y% X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- a+ c9 r2 r* d6 @" n5 K( F+ i( Z. A) U
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
# E4 e: T: T! S/ j+ e; a/ y# `; v$ N; w: [3 n1 `0 k0 m
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs: Z9 `0 m7 B5 W3 ~* d* l
& e% d9 t& k% S- s& p依赖关系:7 L5 o) m. T1 w3 X
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" a* s7 p' \: ^6 p0 L- D! g
( K: ^: [$ z. C) A" nyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- z$ V& l1 p7 s+ m# Gmodsecurty依赖的包:pcre httpd-devel libxml2 apr
; ?5 m- x) u4 ]4 a
, C* y$ ]7 X/ A& c+ s% w2 Qyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
: n |2 a+ q: I7 Q, r6 [# W二.启用standalone模块并编译' B" o, H6 Z! l9 M
6 h/ G# q. i1 `' h下载modsecurity for nginx 解压,进入解压后目录执行:
% l* s% A9 H) Y& @ K- v- ^7 p8 _7 y: ^# n, m+ C: B+ i
./autogen.sh& B0 g# a2 d4 m3 y: I8 O3 {
./configure --enable-standalone-module --disable-mlogc0 U' U3 o* Y* r9 z' W( Z% y
make
; v- U" V0 ?4 ^! i! M# h三.nginx添加modsecurity模块
4 M3 H: c& V7 @- L* q! x7 J" T; e
* {/ A n4 A- b6 _+ x; J: w4 y! f在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 U' E$ D4 T0 c( |2 E
/ ^1 r9 i7 X8 t. `; s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
0 Z! B' U" {6 D; `' o4 Jmake && make install
) `( g s- B; o四.添加规则+ d) f: Q$ \" R* l! a
1 a" ?: f5 ^( h8 J# C0 O
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
8 @9 f5 H$ Q! _1 H( M
) {7 @7 m6 U9 b F; x1.下载OWASP规则:
: S9 @$ j# ?# `2 L
" C1 a" R+ d: Z) J& M: x; M7 x+ Fgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs# m* Q c2 i8 p
$ Q; q# C! h8 B/ \; R) Ymv owasp-modsecurity-crs /opt/tengine/conf/! g4 i5 N( w$ m. H/ R$ z
: G; } |% T* D2 E" W6 A I9 C
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
$ f# |4 A* G/ A- u! O0 h/ w- {2.启用OWASP规则:% O6 s: h7 v. ]( e, T0 I
% z# Z- y: O8 o0 C7 t$ u8 Y复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& g! H2 k: R6 p) A# `* ]
. a/ Y! p3 O- I/ r- U# g
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 ?$ c/ m: }+ ]) C9 z$ H$ O+ y1 B5 s+ P0 K3 Y
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
6 x' m& J6 o' n! d7 {; @: x/ k8 _0 R2 m6 {
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
/ |( o: {& e' h& Y' RInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf2 A' |$ q. B4 e% g
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
" h$ b. r4 ?3 r! X7 WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 i* F0 n" ]) cInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf8 G. Q! Z! w, t/ l! @/ q
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf H$ E; x8 o0 a/ |- {; q% R( \
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! w" b W: J) h5 I, ?' H3 ~7 |五.配置nginx
- D- x, P/ C% Y4 u. U0 j
' i9 S. j, Y- }' g c, |( x在需要启用modsecurity的主机的location下面加入下面两行即可:
$ h5 i5 ~& t5 ^ x% G# W
( \/ x. D! y8 c) I# p6 v$ e( J/ {ModSecurityEnabled on; ( I; B- W3 I+ W
ModSecurityConfig modsecurity.conf;& v3 e B" q0 z, z
下面是两个示例配置,php虚拟主机:
9 S' O) v/ z3 |
& H7 @& |5 m& x0 }% O$ s: H4 Rserver {
; W4 u' J' D. @# x listen 80;
, t' G1 o Q7 }1 a- D server_name 52os.net www.52os.net;0 f: z* _+ n& N: `1 A
& ~) F& x+ o4 _! D- N. }' n4 [ location ~ \.php$ {. {8 D/ @7 `, L8 t' a5 I1 X
ModSecurityEnabled on; & J. l: \$ B- A z3 T
ModSecurityConfig modsecurity.conf;, l1 {( ?- {! ~* _
& U4 s$ }7 m. C' E' _
root /web/wordpress;9 Y& @. G2 k4 K! [
index index.php index.html index.htm;
$ M5 L1 H- ]8 j/ y
, Y1 t4 i/ L4 m& l2 Q* S; V5 ? fastcgi_pass 127.0.0.1:9000;; m* I- t% I* u6 r" d+ K. N
fastcgi_index index.php;
4 g7 b/ R( ~ T* u fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& `/ m4 J& A. l8 {+ ~4 p4 I6 z
include fastcgi_params;6 v# z( J! L% d: P n( M1 L
}
/ M) f2 ?, N( r1 b# l+ u }
! P8 ^% I9 u5 G& _upstream负载均衡:' T3 X2 `; x# Z) P+ M
! Z, B& m5 P) K. \" d7 z& S$ E4 N/ \upstream 52os.net {
3 f/ U9 W0 ~ C3 v server 192.168.1.100:8080; Q, L4 Z' S Q. [0 q
server 192.168.1.101:8080 backup;
$ a7 b4 o* {$ E, {7 C5 ~: s# k}: V. U0 e* y) R; k, e4 l$ j2 v
9 d" f( I4 ?( qserver {
. U/ O% `. g+ Z1 U; `, k9 _" |+ |( hlisten 80;& o) m8 E: e' {
server_name 52os.net www.52os.net;
" _( T5 U, j+ [" |% s' t% s: M
0 V+ I/ ]' z: _. ~# V) Flocation / {
( F! G) j$ q, v# Y2 i ModSecurityEnabled on; % |# T- H) Y0 p' \
ModSecurityConfig modsecurity.conf; 4 [7 p' ?6 F1 H! y2 V; k* v
1 s$ m7 B! i! W7 ^* |# k3 _6 {; n
proxy_pass http://online;
7 _, U7 {! `4 h T* _" g proxy_redirect off;' A+ k, a! x# F6 H" E5 a
proxy_set_header Host $host;- v1 C2 N U, v! K/ T0 [0 a
proxy_set_header X-Real-IP $remote_addr;
- R! y: [: P% k- q ?6 Q$ O1 |- c proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;! B% c7 ~" x4 m( Y
}/ t9 ?; K5 P: z$ n' a. [) {
}
- P( ]8 x3 S5 J六.测试3 l- t9 a" B/ U6 t: Y
$ z" w9 ^, ^$ {" ]
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 l" B, d! h9 @9 [
9 O4 C( V. {+ g: i3 G6 A C% F6 o0 H
<?php
# W8 O' G8 L% H9 z) r phpinfo(); . K u( a3 {3 K2 o3 L5 M
?>: c( B8 a9 ?. E3 K* x" {
在浏览器中访问:, W( ]7 c Y# ~6 V, m2 o
& H6 ^# \- ^% c1 y. }$ zhttp://www.52os.net/phpinfo.php?id=1 正常显示。" b* a0 K. b8 K% ?$ T1 n
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 Z; C. `8 t6 p2 D+ Bhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。8 x+ }% T' y2 Z/ o+ [+ D
说明sql注入和xss已经被过滤了
" s: t/ w" C4 ]; Q8 f$ X3 U, V
& U! ?& ` n) M2 G* H6 P& }七、安装过程中排错. s; U* z' r1 Q, ]; X( _$ m, h
2 ^2 H" j$ v5 y! [: v6 |. Z
1.缺少APXS会报错2 c+ S+ [3 \ i f
; ~* X( F" C- U8 k3 Y; h- b$ s
configure: looking for Apache module support via DSO through APXS; |" K4 i9 o% [2 i" _9 [6 g" F
configure: error: couldn't find APXS
/ B6 J$ D$ S1 w1 C3 ^. A& u# d8 I9 Vapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 w# u( a" |% ?1 ~+ N u* g
解决方法:7 Q( b: Q% S) z8 `0 T- ]/ h ~
, v. ~& C1 j ^) d5 q; fyum install httpd-devel* Z( {* H, L( l1 G) K" a8 y
2.没有pcre M5 E2 L2 q/ t# _
# N. y; }9 m \4 G2 V
configure: *** pcre library not found.
! B; s: h, l) r9 x0 f' uconfigure: error: pcre library is required
! `7 S" A! ]# Y3 A1 O, m解决方法:
, [5 I+ P: e4 N( K: o, j3 r' c* I5 o) V4 }( V! u
yum install pcre pcre-devel
$ J/ ?! R O) ]( b3 M) a3.没有libxml2
`- ^0 l k% x9 C, d% ~
2 k/ @% ~7 K! l6 c: q' T* m# ?! T, o2 h0 v# Y
configure: *** xml library not found.
+ {' F0 g7 w! Q3 A! F8 zconfigure: error: libxml2 is required
+ X; X1 G" c( H解决方法:1 k8 U' @' w4 P
0 ~ u5 z! J/ a: c/ _2 o1 r) M) f
yum install libxml2 libxml2-devel* N% z7 C, r" X, b+ N
4.执行 /opt/tengine/sbin/nginx -m 时有警告
3 P; E3 {: R/ v$ j. C0 P; {$ z) J: }" ?7 x
Tengine version: Tengine/2.1.0 (nginx/1.6.2)5 [& I. L6 K. x' M
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!2 n2 R! `# `9 f- n' j0 i9 G
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log0 x; e$ l9 K8 i
R* V4 t! D; _# M0 E' f1 `2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* j- b: Q/ H% V y( b2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ p3 e* x u/ p( \: D2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
. d; i7 Q- H% i) m/ {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
( j# O {" }; Y# M* b4 ^% r3 c2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& w9 t5 z6 \3 O. W6 B" C, m7 q9 }: g* d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On./ E! Y" r! V, k. E6 ~' X
解决方法,移除低版本的APR (1.3.9)' I6 z2 U1 X$ m3 V& ~
/ }' U0 F% J% S. ~7 T
yum remove apr
6 c% @( l! ^2 M5.Error.log中有: Audit log: Failed to lock global mutex9 ~. Q U) X( n2 q5 A* n1 _
' ]; T" [% H* E+ o% Q8 Y# ]) O2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ g$ ^7 q/ s$ ]5 `- H$ A# t- \* uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]$ ?& S' M: x6 L% c0 S3 i
解决方法:
+ R# I/ [( l4 }编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 c/ y9 N0 B" X8 w1 M. [, e$ C. C$ C! _0 ], c3 V( F4 M# T
SecAuditLogDirMode 07779 p6 |6 L# n& \" D! Y3 s ]
SecAuditLogFileMode 05508 e2 y& F# B( _1 s4 I9 J
SecAuditLogStorageDir /var/log/modsecurity
) G- U: e i7 u7 a8 o9 `SecAuditLogType Concurrent, z4 U% \) x" Q
参考文章:
1 M6 x, Z! ?/ ?https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX W+ u2 d5 x9 s+ E0 c( F
http://drops.wooyun.org/tips/2614 |
|