|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
9 z$ P4 b" E C ]# L, f$ W
& u- x5 w( A( u( H' Q一.准备工作5 u' Z: o' a K8 p
* O/ m* \% _$ S8 |& z3 E系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0# g: V- R- {/ D2 d1 S1 c
. e& D1 k0 n# |- |5 W! y; M0 S2 C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 l) q7 A$ r- T! l" ]
) B! i$ B n) M9 O
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ U$ m5 n; ^: B
3 C+ D+ F) V- Q3 z) F% iOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 |9 L F$ `- V q5 X* G- W
8 `7 t. c# F/ j; p
依赖关系:
9 _, ^$ s: \, b- G2 E# O) D" Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
! i5 e# R6 \9 p
( E; ^. c9 Q- O8 [2 [yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 a/ {6 m7 W* {. W( P
modsecurty依赖的包:pcre httpd-devel libxml2 apr! y1 {; u* Q. F. `) U
& ?2 P( a/ d; W+ D0 _# j" ?yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel- k9 @' K, {( u/ W* v
二.启用standalone模块并编译" _( n8 v1 _+ d$ f. [
# C3 A; c- {. j: J下载modsecurity for nginx 解压,进入解压后目录执行:
5 e) l# n4 O! M0 O: J; O
C, c' T6 S! f./autogen.sh/ J+ S- S4 N6 h, ` a3 J
./configure --enable-standalone-module --disable-mlogc
& v, a$ F7 g! Z' y4 r& v7 Qmake / P, C6 H2 Z/ G5 k( h
三.nginx添加modsecurity模块
: a9 t+ _& G0 h' V: p5 P" t) z- S" e7 y" A8 E# Q( d
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:! D/ S$ z2 c' C2 E3 h+ A
2 l2 o( k; J3 g7 ?7 ] J./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine+ [# K* L6 c) h5 F
make && make install3 t; ~" i- J8 U7 ]9 }8 \
四.添加规则
8 G+ P. `& Q% b( ?0 H- z: C
1 v, o* G1 {) k5 F) c1 kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。! l9 Q. U! U" u" o+ ]
, ^" X9 M2 E8 Q: Y1.下载OWASP规则:
# R+ s. U" s. ]$ E, W) ]8 I7 v3 u# Z& u3 ?6 j
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
/ n1 F8 L- ?( n* u6 P
) t1 I- C: T8 F4 Vmv owasp-modsecurity-crs /opt/tengine/conf/
) L: X! @* g: `9 J% B4 H, a1 q u e) @5 e0 s( g3 m. ~6 C
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf( h4 i! }$ P" G4 ~1 ?
2.启用OWASP规则:+ k# X6 V. W5 n1 A% M* B5 N
) t: c* O) h1 u) c! W' G. l复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
8 `6 U$ c) w; h: w0 e& N, G. M2 T
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* @; E; M' t8 ]' s& k. \, Y. T% L" K; k2 N
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。& m, x3 l) [6 g* P) c. x* [
" w+ `1 G2 e1 V) `
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 \- S, L! O; `- pInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
; }2 X' C& i2 ]9 `* v% ~Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, C+ E, B) g$ F$ r1 b1 vInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf8 |) j# f) t" P" F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf' i- _# S% v; P$ h- w5 ^- J
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
: h, L0 B/ B/ e7 V$ u( vInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
- `4 Y1 g5 F, j5 S9 v* p五.配置nginx
+ C" p' c8 C1 K. S9 X3 I8 U' U5 ]: O0 c: _
在需要启用modsecurity的主机的location下面加入下面两行即可:: D- g' ^2 O* u k" g$ I/ m
( t9 [, O9 ^# L- h3 T0 t/ C
ModSecurityEnabled on; 6 }+ |1 C' C8 u3 k8 X7 m' Y
ModSecurityConfig modsecurity.conf;7 m& y+ {; ?- V- t. R
下面是两个示例配置,php虚拟主机:$ D) T0 s* r! T5 b9 c
) J7 x/ J# \# o8 j0 o
server {& M3 c9 S+ l. D/ Q$ j
listen 80;7 l3 I# q* G7 S* A3 Z8 j
server_name 52os.net www.52os.net;, _0 B! K! X/ g3 Y! q
, a" [" g% C) U/ Q; M! z! J location ~ \.php$ {9 b& z4 c- Z5 f8 X
ModSecurityEnabled on;
/ ^/ i# m( e, I% g ModSecurityConfig modsecurity.conf;5 U" C \6 c7 l
9 o4 z: _# |& {/ `) i' k$ ]% p
root /web/wordpress;, O# B8 _" l) U& }8 W' u+ ?
index index.php index.html index.htm;7 j9 p8 @0 d0 z7 Y K& L3 d) ]1 L
! O& k; j" X* V) |3 t) @$ K1 W fastcgi_pass 127.0.0.1:9000;
( u" M# x2 u) e5 N; ]. g& b$ \ fastcgi_index index.php;
. \ ~" A q/ ^' X7 {: b5 y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
# s, @. l# Y* q7 g, x include fastcgi_params;! W+ e+ G* B+ F( v: g
}5 v0 B6 }* U- v2 d$ {
}# B7 J1 G/ d+ k0 g5 p2 V
upstream负载均衡:
e3 R% ] R' z7 U8 e. k. l m& B- W ]9 q q5 Z) E/ C& d
upstream 52os.net {
9 M% S% t: c! Y) ^3 i! F server 192.168.1.100:8080;) A- F* W1 f* G! s; [4 f& @) J) ?
server 192.168.1.101:8080 backup;- T& Y; V( _/ C' q+ g! u+ v
}9 v# I4 B2 ?! l: c7 _% N& y
8 ~' |& B! L+ userver {
1 {2 |* p* n7 Y* Flisten 80;6 G3 C# [ A/ u# f1 F. y" a
server_name 52os.net www.52os.net;
, i8 l' D) I( D! M4 e4 Z, N0 ~: }: _+ S$ {$ O
location / {
b, {8 Y, E) v! w) X; s. g+ E ModSecurityEnabled on;
; h. p5 m% q9 e" }) d ModSecurityConfig modsecurity.conf;
, Q' V, a/ r* T l8 Q) }0 t3 w6 y7 Z
proxy_pass http://online;
, ~$ f8 @2 O% b proxy_redirect off;
& @. H: M: T8 K proxy_set_header Host $host;5 J/ Y4 v3 Z! y; Q( _0 t( p3 n
proxy_set_header X-Real-IP $remote_addr;
3 e8 a9 v9 }6 l! u( Z* i proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$ r) r0 m) k# h0 B }+ C" o' M t" d3 O
}, Y" {6 w4 r% H% z$ Y3 J
六.测试' H- K* _! a3 r; P
' l4 T6 a A# U2 f7 p
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
/ n R& R4 E5 W( [" @5 m A/ Q/ E+ g& Y/ I
<?php) h1 F+ C9 |6 J- k
phpinfo();
9 u& ~ S% ?1 U% Q?>$ A4 A3 P, \6 L3 Y7 Z' {' h
在浏览器中访问: W5 e. u6 E+ Y
$ S: l" p& n+ h. q! mhttp://www.52os.net/phpinfo.php?id=1 正常显示。
% V3 p8 c- ]2 P t J" Z4 mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
. D$ {+ f" Z8 r) V0 R+ o6 Nhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。) L' [3 U3 Y5 Q8 D" L! k0 ]
说明sql注入和xss已经被过滤了
6 R5 @0 O5 |0 f* x4 l s8 E& Y$ R$ u: X4 l* A6 h3 g
七、安装过程中排错3 o/ {9 B6 I! Y& v& o
I8 _+ Y& O$ @ t. x. f6 m# z
1.缺少APXS会报错6 g# d& U! m- {7 J( q+ ?8 Q
1 @) u8 r; @# Y5 A
configure: looking for Apache module support via DSO through APXS
$ _; k# `+ s4 e$ k* mconfigure: error: couldn't find APXS5 ?0 A& J& l; B# f5 ^" Z- [$ i1 ?. `
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
; e2 d" l9 q0 Q& ^解决方法:8 |% h" _+ d* u6 o' g3 ?" I
: B, [/ W) ~ `- \yum install httpd-devel
" O3 ~: u3 f; m* ~; L2.没有pcre" b: @" P3 | f( U
- a v$ r! Z% w) x
configure: *** pcre library not found.
* g& _, S" c1 `3 }4 E* {configure: error: pcre library is required" ? N6 h% I2 q* v) v6 P! |+ j8 \/ e
解决方法:# A, J" z! t) q; h* u
( V! }" V+ u% Byum install pcre pcre-devel
7 j* b2 m# r$ ?* i' s3.没有libxml2
I: G7 w& j- O; w7 k2 t/ ~. R3 P P$ X A5 A2 f4 q
% G, N6 u7 V3 t- r( sconfigure: *** xml library not found.% L7 k5 t& Z9 o6 ~- q& t
configure: error: libxml2 is required9 T/ a' p- p. T2 D2 T
解决方法:; O# ^2 P9 ], k6 O; {* X. }
' o3 f& M4 F+ U% J/ j
yum install libxml2 libxml2-devel
* n- S( q/ L$ e- v7 S& _( t4.执行 /opt/tengine/sbin/nginx -m 时有警告2 d* E: v% ]3 y' l' Z6 c
4 o1 e# x- t( g1 \8 l xTengine version: Tengine/2.1.0 (nginx/1.6.2)
% i7 O4 Y- }! F( Cnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
' J& [ i; A7 [( L( T z7 x原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
. M, Z2 I, b2 }0 D3 h5 f9 r
) |0 b; j3 y; h. z, U3 H. R: R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
( R% }7 k' V- U, f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"( G% m% k6 u# v4 Z5 F
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
" q5 u; ?" v. |$ B7 F4 s, ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
6 o- B! M3 N3 S* p; h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ _$ \! U, q, m5 Y1 \& h3 M {, H2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. z: x5 k/ E* X4 F1 `解决方法,移除低版本的APR (1.3.9)
2 b0 B1 k- ]' ^6 A8 K4 h5 w+ t
; s0 b5 Q& G' x+ n: h% c2 {( zyum remove apr% y8 u' j1 S# C, ^8 Y
5.Error.log中有: Audit log: Failed to lock global mutex, m2 g/ L' t1 E6 R
^' ~" s6 ]" V6 p* S0 s, M+ P+ L5 P; j
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, c. ` ~+ K5 _7 M8 M0 ]; J5 Aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. p7 {7 W; |# A8 V" {, G4 x A2 W$ T
解决方法:6 o3 G/ V' q% \: I
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
7 G5 f- Y" ]( E% A: H# L
0 Z. t6 q6 ?! `- r& a8 uSecAuditLogDirMode 0777
; Y" x4 y o% VSecAuditLogFileMode 0550* C$ F" [2 |: {9 W
SecAuditLogStorageDir /var/log/modsecurity
3 Y+ Z* Y3 D0 Y5 V6 |5 i5 C5 I9 lSecAuditLogType Concurrent! |* \0 d6 ~$ E) d5 r
参考文章:
. }' F# Z1 B- O$ ]$ U6 ~https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
* ^) [" d/ m! ^http://drops.wooyun.org/tips/2614 |
|