|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 A- O6 c& y }7 U
3 ~/ T$ m) O6 F+ u
一.准备工作7 T4 }! ^; m m
% l& K3 T/ ~1 a% Q8 H. A3 O
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
* Y: {# I$ B, I! U* i
9 Z" K- N% @& Itengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" ]) L2 ~+ _; z$ N* @; Q5 |) ]4 x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 l& w+ l3 Z9 w
2 k) @: n# e4 ]3 i6 @
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
$ u# `. o" M4 a' S( a' G
v4 z% ]+ c' V3 h/ V! ~- A' | K依赖关系: [( ^1 j. q$ D- r3 d
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:5 w& M5 G( A* e
2 x9 R# ], l# U4 V" {# n9 G Vyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* b' x* @ L- M: I$ ?( j" |1 Smodsecurty依赖的包:pcre httpd-devel libxml2 apr
4 L9 x3 C& N" g) P% n p: O0 l* F5 ?* O$ i' j
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
0 h: ? t3 e7 j( T$ B3 _二.启用standalone模块并编译
- n9 X3 F8 r# y% M. k
6 v: V* R* t; C$ i下载modsecurity for nginx 解压,进入解压后目录执行:
8 B$ S& d& ]; | i# q8 r4 B
5 ?! X C2 J9 K+ H/ V./autogen.sh
) h6 v* v' b. B, B" q1 @0 M./configure --enable-standalone-module --disable-mlogc
5 N$ m: _# q3 r5 x/ j, i4 nmake
( H- j+ M( ]2 @7 e$ L三.nginx添加modsecurity模块# u6 x$ c5 f, F
" p( H5 g) K- q C9 q' p在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
+ R2 M$ I/ S, y e0 }( A# \0 R$ S c
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine3 v4 {5 H9 W; Q/ C
make && make install. Y' [/ [8 X8 u" h- I4 X8 P7 \9 J
四.添加规则
5 ^8 N' u5 |. F
: P0 M- M" L1 a5 q6 e: f M% M" {modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 a/ c% z& H. n. I
, z( U+ {9 v/ m6 L# [1.下载OWASP规则:2 M4 U% W# T3 L, N# \
# P7 |" B" u1 N; x' V
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
- T% r: L, ~ e# l# |2 f8 P
' W. B/ \+ K; \# L cmv owasp-modsecurity-crs /opt/tengine/conf/
n, @ Z# K A, [8 @! V$ N* G) c8 v% [ }+ S! o' T: J5 T
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 w/ E- { y4 ~. b: k2.启用OWASP规则:; s* D$ j4 _- |& }
) t! E; L3 M) Y" ]" r8 V7 P复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
5 ?# S. a' T7 \% S! a1 {% s& Y. t. X9 W) m) o- u5 v
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
- G, L I/ p9 P. h
# D5 C q6 j8 s* @% q/ Vowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: V+ g* r$ w4 _+ X0 d" ^' ~1 u9 g# K! ?7 I( G4 f/ r
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf! x+ z, M. R, C8 s, K
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 Q; T' d$ w5 KInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf) Z* \, I- v* P! K; m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
; t2 o3 ^; P' \; m/ W- N4 jInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf G" M$ w& U$ s$ ?) R+ x, T) `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% q! p* o; U- `6 DInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# Z0 }5 w+ Y/ K# X五.配置nginx
- @2 ?6 H" V/ l0 u/ I1 o u. y, Z; a$ F% w. @) y5 A* r
在需要启用modsecurity的主机的location下面加入下面两行即可:
! S. R* \- }9 A2 @( Q# m( j w5 t
4 I- V3 K* u& B9 mModSecurityEnabled on; % c' T( P" h% b5 \) c/ M
ModSecurityConfig modsecurity.conf;
" X9 A( c* `' P下面是两个示例配置,php虚拟主机:7 S* K2 m7 M! t6 p/ l
d! }* x( y' b* l6 d& I
server {
& w. i! n9 S0 E t# N. b listen 80;
' x% Z. }/ g4 |5 u1 L2 [/ { server_name 52os.net www.52os.net;( `: f- e7 Q) ?* P
. d- q2 p+ n- k( G! r" @
location ~ \.php$ {
. T- ]: y6 c3 W9 U9 p, Q4 z3 g ModSecurityEnabled on; : r/ z2 i& P. x7 y3 A& n4 u
ModSecurityConfig modsecurity.conf;
: p( B* C0 g# t( r- h& q% B! m, m+ V) X! R4 d
root /web/wordpress;
) `' q# l: {3 B: W$ C8 c7 w4 z index index.php index.html index.htm;
* F; a( B: t8 d+ g$ z; K# K
+ X, E$ d0 a' s3 j- |* B1 ~ fastcgi_pass 127.0.0.1:9000;
8 s0 c: q7 R. k; D% k' E fastcgi_index index.php;
. p) s+ _# X: s9 s fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;4 a) F, m: H5 E5 j2 L" `) j1 i
include fastcgi_params;
% i' C( L* c8 F( G' a. e8 @# v }
! z( z+ S- u- ~2 l2 E3 |/ e) k0 _ }9 u' d1 A! D6 m
upstream负载均衡:+ p5 V; c& s/ ]) Q! |/ v8 C9 C/ Z
) U8 O* D. [1 T7 @- g7 V& ?upstream 52os.net {
6 \( k1 G5 Y: K! b: b6 a* n8 h server 192.168.1.100:8080;( n9 g! R& K. x: D, J1 @ w9 t
server 192.168.1.101:8080 backup;
$ t5 y: X5 O) v& H}8 p( c) P% d R- _( x
' |1 T& ^& A- ?/ ~; lserver {
7 ~. |- }2 I6 t' Olisten 80;
/ O6 ^# I3 o# b, L6 s1 kserver_name 52os.net www.52os.net;
: W; `- G# l- y: O0 O- Y+ i; _: a; Z6 m7 B( l5 ]- w
location / {/ f/ ~& \& [: ]3 |
ModSecurityEnabled on; ; a' Q; f7 [; d- P$ Y1 P/ ]
ModSecurityConfig modsecurity.conf;
$ n( v, I6 T. e
$ Y" L3 S! u, d- y4 ^ proxy_pass http://online;
, P" X7 v; Z9 h. J# _ proxy_redirect off;: a. \5 K; U4 j+ o
proxy_set_header Host $host;
8 w2 \ G+ w7 I; {# z% H proxy_set_header X-Real-IP $remote_addr; V6 F8 m. H8 S: n1 E
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;9 r. a" o& K# P' D! u* d* `: Z. Q
}& ]$ Z, T- k5 Y, O2 x; \; z' g5 |
}3 B. K9 Z U8 a( C. a
六.测试9 {& E+ y( Y; O" R, S- J
. |8 W( \- k5 I5 z8 W7 f6 x7 B3 A
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:0 k( y# Y8 {9 M4 }# ^
, T& B0 R) a+ C" q1 n# n) [1 L+ e
<?php9 Y0 p E. G/ C- t7 G
phpinfo();
! E7 L' ]9 B/ L- _! h?> u3 g4 H3 k" ?6 k. p
在浏览器中访问:
4 {" J% X) i! k6 g0 Q$ K4 O4 `" r( I7 q" D8 ?
http://www.52os.net/phpinfo.php?id=1 正常显示。0 B( @. Z* S0 y2 v
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。6 x: C- D# g( X) x3 F# F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
: f6 p! m7 ^7 l& n+ ^6 x% a说明sql注入和xss已经被过滤了
4 @5 a v% c3 ^8 Y7 n0 q2 V
7 C4 S% f) N1 K0 m+ Q7 [+ c七、安装过程中排错+ W& y6 V1 e5 V( L* x8 A3 L; j: v
6 E( l. [( W P: A1.缺少APXS会报错
+ X' D$ J6 m$ g5 e0 c' c A. _! k8 k" `! _( ] t, d& F8 k/ h
configure: looking for Apache module support via DSO through APXS: a3 Q, W1 G/ ?7 i: i) Z
configure: error: couldn't find APXS* i/ z) \6 i, _8 z& S6 i
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。4 ^8 T; _7 N" I2 o8 z3 Z+ ^# F
解决方法:
) [" ?8 [6 m" K. ?7 G6 L4 N6 z( H: L( P
yum install httpd-devel- s0 a8 n1 X K/ E
2.没有pcre: T( o- R' X0 }
- q# {+ O+ C" j9 {) m' m8 q
configure: *** pcre library not found.
- h2 v% x; B$ a) k l: y; \$ \configure: error: pcre library is required
# {: T% p* B! C/ ]/ Z$ ], J解决方法:" o) U) q# f6 p
+ [; ]0 d7 U3 |% ~$ j
yum install pcre pcre-devel! _: B# q3 [5 s8 T3 N8 m
3.没有libxml2
6 S' T" \. X& |
( f2 Q9 j7 ^9 s1 A# w) m/ ]" R1 P6 Y# p) I/ h% P
configure: *** xml library not found.
7 Y: }% @' `1 k+ zconfigure: error: libxml2 is required) R/ M& y. X. G- Z9 h2 ^- c3 Y7 L9 j9 k
解决方法:
2 g2 Z; T- J% T# M, f7 p! T8 T- p; {3 j9 V& B
yum install libxml2 libxml2-devel
3 o% S( \; O- v: ?+ v, r4.执行 /opt/tengine/sbin/nginx -m 时有警告+ {6 y' ]: S* W. V1 [
% F8 C) L8 T. \" ~' v& n
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
+ D, j2 |5 u( Y: h5 G& b& Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!& }% N6 C' H% D+ F
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 s q) _6 d, ?$ Y
) |9 J) J! z7 ?/ B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, B4 l z' P7 R9 F. [# r- x6 E7 |/ i2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"+ L5 h; s0 L' c- j9 @
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- H) x! X/ U l: M) |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ E: q* j4 @& @$ m( R6 |7 z4 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
! R. k4 a j1 d Z# k2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2 a+ W: u; m0 \; Y/ Y7 w# u解决方法,移除低版本的APR (1.3.9)7 g5 ?3 z; O; V( y- L
, C3 |( Z1 l _9 F1 B1 Y6 R' r( |yum remove apr
- R C" ^+ I: F; J! b5.Error.log中有: Audit log: Failed to lock global mutex9 K4 _/ h4 c+ V9 O7 C
" i# C9 c5 _7 C. x$ ^* T2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
# L, W9 a* b) Wglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
2 U! O+ ~4 K. O" b4 ?$ Z解决方法:
6 x, P& \, S: |2 o编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:' D! y6 y' i3 R( w4 g/ }/ U
( @) P) e% @7 BSecAuditLogDirMode 0777
: V1 [3 b% ], p! Y0 Y g$ \SecAuditLogFileMode 0550/ N' G3 K$ l* W# i" x
SecAuditLogStorageDir /var/log/modsecurity
! d0 d6 r4 I# Z" X: |SecAuditLogType Concurrent
" G! }7 }0 ]1 a6 C7 J, k参考文章:
8 v1 p7 [) `' @/ W( J* U4 ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, b5 X4 x* N- y: f; p/ R( F, l
http://drops.wooyun.org/tips/2614 |
|