|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
+ Z) r1 b5 M* d& Y' f2 C, S( t* B
( g$ `- H4 q. N; {# m' K一.准备工作7 e/ @. D! g7 @9 S$ \
: [2 M" ]' Q Z; |5 R. X系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ N1 t F9 u# L; \6 Q" V
) r" [7 U) C) Ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" b# X7 b! x2 ~4 q+ `& {6 ~. l0 d7 o& @: ]3 H
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz! K/ z9 Z z+ G* X( Y9 ^4 [
& X3 A2 b# n4 ]* a/ R! q6 @OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs1 t5 X1 p1 Z; Q& n: @
# h5 E7 z' h0 A
依赖关系:3 L4 S9 u$ ]6 C9 P& ]: T
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:7 q' R& b7 t7 {% k. L' O9 [
- x; {$ |: I: p) @ S
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- T# @ g3 B: [/ p) Q$ p: ]; y
modsecurty依赖的包:pcre httpd-devel libxml2 apr
. {- I" g3 Y, K; ?( f( C/ h
% ^. k; {4 ]' p6 D4 iyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, n" H9 u/ m4 z* ^& ?# q6 c( o
二.启用standalone模块并编译! q- f9 q# I! X' J6 }: q$ z4 T
3 O0 R' Q1 V7 h0 {/ Q- F9 T& G
下载modsecurity for nginx 解压,进入解压后目录执行:* q9 r2 W# x1 |2 j* U
2 t- d6 G; x4 V% k# w# E7 J! O& e& c
./autogen.sh4 K3 N$ I5 d$ X" c& f
./configure --enable-standalone-module --disable-mlogc4 l( K; K! G: y% @( v# g9 d
make + R3 Z/ [5 ^2 R! p$ X* L
三.nginx添加modsecurity模块* O/ g# u: e- _5 f& ~
; ?" ~0 p) \7 }8 W/ |% I* [. N, s在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
9 o7 P9 [2 a* Q$ h2 E/ k* P
6 _3 _6 F2 m6 e9 F. q6 v) k./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
5 g# v- e: K; }6 N+ {0 m( ^2 m2 dmake && make install
' R3 x: ]- ~8 C2 i四.添加规则* @3 M9 P& {3 J0 ?4 C5 M8 _
$ S! t3 _5 M; {modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 }1 O8 u! Q! y" n" W
4 B F# t/ H3 ]) ]& {* u7 B1.下载OWASP规则:
+ G; L/ q7 B& T9 h6 _0 c# @9 W5 K
# N7 b6 D7 ?/ P9 S3 Qgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs0 C v; y2 d# Q, R6 A1 F
0 ^' U4 _/ S3 w6 M7 q, omv owasp-modsecurity-crs /opt/tengine/conf/
' l5 X, W0 }( M4 p
* B" P: u6 c' B" x- O6 d+ N8 ?+ |cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf. p# S K% \/ s5 `+ t
2.启用OWASP规则:+ Z) D9 n, \9 f( Y/ l
9 w: b, n+ K! F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
m' P) z& q) B6 d* q/ I1 u5 L8 X3 z" s+ p r6 o4 Y, b- A
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
0 T5 ]4 d- W1 g4 \" x$ z/ e% |6 c- v; g. c$ a- `: U2 \
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
C9 o6 E+ z3 p! C% `/ m# j, F# K$ G. r5 u7 V$ X3 w
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf, R2 k; B. P' z* _( z- w1 i% {
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf; S; Y7 {" I6 v! f$ H* F
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 c8 x7 X- \. A4 c" x1 C* UInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' n0 K9 {6 S* t* LInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! m' Q+ }- O5 s; Q3 Q* ]: UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. z5 K* \/ u3 I9 E% e$ e
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 I' z' U$ ^5 U* ^
五.配置nginx3 b. e |1 Y9 B! i1 @
+ z. D7 n8 S+ f+ i
在需要启用modsecurity的主机的location下面加入下面两行即可:
4 ]9 c$ B7 \( e) }* ~
/ w9 q! m6 I0 k7 m6 a' x" i/ oModSecurityEnabled on;
: p" Z8 h) I! C3 m0 c. XModSecurityConfig modsecurity.conf;# q0 p6 l* i8 M' m n4 i. W+ z ~
下面是两个示例配置,php虚拟主机:
x; t, V7 `, m; Z7 K: m& s. }
* `$ ]$ ]0 S% B: P' Mserver {8 `( ]8 z+ }3 O
listen 80;
8 g5 s% z) K: d! z5 \ server_name 52os.net www.52os.net;, h1 c$ _) C G1 N) J+ x8 f
. C6 B) X7 d* P8 X location ~ \.php$ {
& u5 s$ N L! t" R5 S ModSecurityEnabled on; % A) ~, n* ~$ a0 P1 P
ModSecurityConfig modsecurity.conf;7 I1 O5 S7 O0 j5 @$ C
0 F- _# r! o: d; S! _& b
root /web/wordpress;" U, _1 u2 ]$ }+ ]. k" y4 o
index index.php index.html index.htm;1 I0 O, e$ u$ m- i
9 Z/ n: J# B! Y- ] fastcgi_pass 127.0.0.1:9000;. K: u* X! R5 O
fastcgi_index index.php;( d D: Q9 _- r) r( J, A3 R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
/ e3 H; X4 |) e$ H: ~ include fastcgi_params;
! C% y9 i. G+ O }
' x) L N, X( c1 D }8 E" m+ U" P7 W) s
upstream负载均衡:- C( {8 Z7 D+ {1 S
: d- ^" W; z5 _/ S, Hupstream 52os.net {
0 _0 }/ L0 G }+ F( } server 192.168.1.100:8080;/ H! V0 N+ A3 \; u
server 192.168.1.101:8080 backup;
8 \8 b4 u6 @$ s}
1 C* K8 ?0 e& Q3 ~
& N: j: ?6 I3 _" O$ N: x7 T- Jserver {/ ^$ b' W: v0 G9 u
listen 80;
, Q) q4 ?; q# ]6 F; u* o; ]server_name 52os.net www.52os.net;
# u: K; a% q* u/ p+ ]" b7 k, u" o! q$ w- c# T9 F, _$ j
location / {
' U H' P/ K, S- b+ N" I ModSecurityEnabled on; , y a" i( p. h+ ?+ Q- a L* O1 ~
ModSecurityConfig modsecurity.conf;
3 i8 g/ t" @# `' T$ A5 [' K
3 |& A" Z+ o6 {1 R; w( A, E" @6 t& h2 s proxy_pass http://online;; v8 e: K0 \- v' F$ R4 t
proxy_redirect off;
3 Q; d U# O5 x proxy_set_header Host $host;
, ^* I: N. H i proxy_set_header X-Real-IP $remote_addr;6 \5 v$ o( y- z6 E
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;2 k3 x' x/ G$ b2 X8 H! \1 k2 l. h
}
; e% j8 z3 D! F/ K9 D" X}; m. h. P. c) I2 t+ S, C
六.测试0 k' p+ V5 n- w- \9 a8 `5 d6 R( w; c
- p* @, C- r, z! n% ?我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* E" C7 H$ G. }
1 f4 t( }0 O( e: e<?php W, Y/ b/ z5 U* J: D, h
phpinfo(); & `$ j; i4 L4 Z" x7 ?! H8 ~! H
?>
! a* [, S: A z# |. ^3 S |在浏览器中访问:
" h5 O: e5 o* }4 R$ b& U( m3 Q2 X: ]
http://www.52os.net/phpinfo.php?id=1 正常显示。; b. c* A& F5 U m
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。/ O5 J- M5 i5 O6 S, K/ I) q
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( `9 t5 y/ b* A4 K说明sql注入和xss已经被过滤了: K" O {! z. O3 z
+ D( L* `' t# b8 ^. p- P* V& |
七、安装过程中排错- ~/ e3 U. O1 m0 v8 s
' J3 }+ b- R' f$ }; g* i
1.缺少APXS会报错
9 ~& u/ B# f8 D. w7 x/ U, K# X- Q y6 Y2 s
configure: looking for Apache module support via DSO through APXS
4 k' x+ }. ^; X N2 Y8 i( Lconfigure: error: couldn't find APXS
* g1 i1 X4 {+ s' V" Lapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。+ O* Z @$ u6 e1 C' @9 a m- ]
解决方法:( q4 b7 A3 ~* _0 Q+ X- V6 v3 r6 J
* W7 G3 t' Z1 h* J _- m
yum install httpd-devel4 u1 [" F; P# ?* u; f h# `# L7 J
2.没有pcre8 V" _2 ~. v: P) ]4 ^. e
+ ~$ C. v; c8 \& i3 p
configure: *** pcre library not found.
! d) V% A8 h. D9 \: E7 W, ]' X+ zconfigure: error: pcre library is required1 k; U0 o5 s8 R
解决方法:
" e. S5 I, o. C) O) m, Q& p1 j( b$ U6 Q0 D6 O6 G
yum install pcre pcre-devel. G* Q/ h% l* {, `$ a6 c
3.没有libxml2
0 H l& e: ~2 O6 N1 j3 `; G" \( c9 X% j( N& |
7 B' j; }# g- g- \' T* v& q7 N' C/ hconfigure: *** xml library not found.# T. ]* P9 H: H6 f9 w: w
configure: error: libxml2 is required% X9 V- G. o; ^- l
解决方法:
6 A' g! s: T$ T% f" r v8 T# j
! j% R' x) [1 t$ R% O% N1 ~1 @yum install libxml2 libxml2-devel1 J' J0 q) \. M) C1 k
4.执行 /opt/tengine/sbin/nginx -m 时有警告: q. z W1 U0 [) I3 L2 k
9 Q% z/ E! u1 G$ j# M+ X# y0 p
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
2 M4 E8 |0 k4 E; f2 t; |nginx: [warn] ModSecurity: Loaded APR do not match with compiled!- V7 P4 |; c, A+ c
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
* \& ^. T1 ?# H1 ?: d, ]8 n& G/ y& s5 `0 a7 l# s1 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
4 X$ ~" s2 E* y/ }9 ^2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; a0 o" l# B; d4 x, U7 ~$ M
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ ^6 [0 b! M5 \! P7 n( T7 a6 K" `4 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
n; x1 i( g' N+ ~8 s& g6 L9 x2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ s5 u; L- s8 W0 t, \9 K: @
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.6 g( x6 j# w1 }* B S* w
解决方法,移除低版本的APR (1.3.9)
+ z7 R: \' d6 P7 X& C: L) u( ^/ ]0 N& }/ i* i- P/ H
yum remove apr, J8 L% u. x j ?5 Q
5.Error.log中有: Audit log: Failed to lock global mutex3 c4 x+ w- t7 u. a/ s% [ p% D9 M& Q8 y
% Z+ Z7 u. w$ f8 L3 }2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. u% b8 z# h! c6 `, [global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- g; g0 z( m9 _$ [6 e' Q" z! v! e' G解决方法:
' |, Q4 c, \& M, z: Q4 J编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:; G! _3 w+ \8 [0 f; c
7 l+ v- o% Y h, @, `: y8 o
SecAuditLogDirMode 0777
+ ^8 e- K. h$ N6 r/ G* s4 ?SecAuditLogFileMode 0550
" M/ L' u4 p: b# rSecAuditLogStorageDir /var/log/modsecurity" u: B F* W7 [/ i( U1 c2 P7 q" V7 O# h
SecAuditLogType Concurrent
2 J' e$ {: N5 Z3 c' L: s参考文章:
h( f9 `# ?" X6 [ m. qhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX y3 O% S! V+ w1 q4 v
http://drops.wooyun.org/tips/2614 |
|