|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。 N, }2 z* N. Z5 m9 q _
1 V, w/ C7 A# }( n* O
一.准备工作
q! K' `) f$ e7 `
4 M5 x. @/ E6 @* S. f2 Y系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
+ ?! N* V, R+ l! x1 b
3 c* h$ n; D& \tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
" B" {) h7 L; u
/ a6 a/ s. x( x" O+ _+ mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
( x1 C$ k4 R& ?& \8 Z& }/ k& ]. |
. t% N+ Y& w+ N# F OOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
8 C0 o$ n" H- D: P$ A
\7 r4 K6 L! E! [& p9 Q0 H; `依赖关系:
) s2 z" C' S& h& ?2 ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:9 Q' N* P& \: N* `; @
$ U* ?* h0 h9 c5 O6 s! Qyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
9 [* c! y3 K& s* Rmodsecurty依赖的包:pcre httpd-devel libxml2 apr
7 B) ^' H4 A6 u# y* {
. J3 _. ?6 U. T& N+ _$ N" Z) L# Yyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
( a4 `$ s& q* z; M6 Y: G4 n二.启用standalone模块并编译# G3 m2 c$ _6 h5 v- K; V
: b, ^: V* o2 ]: |下载modsecurity for nginx 解压,进入解压后目录执行:
5 |; c" K3 f/ x# i+ x. A" ~2 P ]: n! z2 t
./autogen.sh
& H5 _; r! O6 h/ A! H# F& ]./configure --enable-standalone-module --disable-mlogc
L- G3 V2 d* H7 S# C, `. b$ m) u+ Pmake , D I( n5 N9 ~! t
三.nginx添加modsecurity模块! |5 ]6 D) k( G) H7 p
* q; @; ~; R# i" s& @! o8 h: X在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, ~/ r$ }( j& { [8 N5 @, O7 K/ `
, Z- h& x$ g0 h./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine( T/ d% x) L* m* R6 i* f3 ?, d
make && make install/ \; T5 C0 s/ Q% s4 ~; J3 [- u: q
四.添加规则4 ~ G" M! ?* |" w) G8 n$ ?
4 s. `+ F+ b0 V8 a4 Kmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。 Q: V. [' g( k& j# A/ c# i0 c
7 Y* k: |; L) j8 J
1.下载OWASP规则:2 x4 b& o) R( z1 b& S
3 v/ T( \; D: x* e" C& rgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs& Y$ }+ M$ W7 X. O* l( p/ w
) s( O7 O" f( A/ g3 Q' ?/ Y- N1 `1 umv owasp-modsecurity-crs /opt/tengine/conf/% ^# J- m2 ^, G+ f+ @ o$ m
/ H7 Z! W. w6 Lcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# D: I6 {! V+ E! R3 e# s% v2.启用OWASP规则:
9 z8 d+ n5 i& s5 d, J1 a! t4 [$ ?* U& B' j. m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。6 q5 i5 V$ H; y6 Z
$ V& [5 c. _ @1 A: e" U
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 D' a. L& s; I$ }
' x/ Z- a7 P& @3 Iowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
' r- V9 J( S% \) J& Q. G
( L8 i1 g3 K$ g: [" WInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf5 l7 ^0 l* d0 Z, c. y0 @
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. s# f# W" Z" o0 V+ B9 b
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 d( E! S T* Q3 k ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf$ W8 C( ?$ T" `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf# E( x8 \; v8 j
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 h7 l, ^* |- X& n" J5 xInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
8 v3 ]+ o# e5 U# M五.配置nginx# G0 @' P/ n# k/ Y x* @/ e6 a
; [4 q) c/ F: U* U! f- S" f在需要启用modsecurity的主机的location下面加入下面两行即可:
! s# X# e) b7 r8 X) u
$ ~- T5 f/ N& a' V3 u( d: ^+ I% R. MModSecurityEnabled on; # h5 N! v$ @+ x! T" G
ModSecurityConfig modsecurity.conf;
, G- Y2 u% ^! @下面是两个示例配置,php虚拟主机:; c4 D. e$ v: ]9 Q l0 H- z
6 R" O( e2 G ~7 |: `server {
; q. X; \/ u8 _) ^4 w2 p1 o& | listen 80;# ^8 Y( k4 P7 @3 L. f/ x" c
server_name 52os.net www.52os.net;' T4 j L/ Q6 N
. P! ~" Z) r5 y. ~ location ~ \.php$ {# K7 `$ L/ y0 i% S
ModSecurityEnabled on; 5 t. \8 j2 B* e+ p
ModSecurityConfig modsecurity.conf;
! [' ]% E2 m( I$ S+ W# [
6 ]2 y3 i' u x' M root /web/wordpress;( @4 h7 G' j2 ]/ P
index index.php index.html index.htm;
F6 s4 G- H+ N# c ^5 }$ Q
W' Z9 n0 z5 o6 Q4 \ fastcgi_pass 127.0.0.1:9000;
" c$ Q' Q# t Q) S5 j# i* A fastcgi_index index.php;& W9 a% H4 H. [" i
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;9 J: ?4 h7 k( @7 ]/ p# e
include fastcgi_params;
4 o% r) E1 V/ e0 y9 A }! P, }% e. u Z0 I5 ` Y. y
}) G5 W% o9 q9 s
upstream负载均衡:+ B5 B' `! B4 W
`+ y9 s* b V! Z1 T( j
upstream 52os.net {2 k# m8 n0 t: J! ]. b1 ^8 F
server 192.168.1.100:8080;7 o( t& M( d n$ A: D/ ]
server 192.168.1.101:8080 backup;
9 A8 E- p9 M" E2 O9 {}
- a3 G5 l, I6 a. o! o6 x- V2 L+ t& M Q* L
server {
" u6 o$ [! m* M' i9 o, b$ y3 C* a8 Alisten 80;% {; n, n, v! W; O, g" G+ O& w8 x* t
server_name 52os.net www.52os.net; d4 {' O; w+ [, y6 r( b
, ^9 Q/ X: k. D* `0 H2 n6 F$ z( Tlocation / {4 q7 o$ b. r5 _ @5 D/ m7 [
ModSecurityEnabled on;
+ g( s+ B" x R7 I! { u2 t0 ?% z3 S7 S ModSecurityConfig modsecurity.conf; 5 \, V8 h# p' J) [; m: W, x
* W; G i1 d A- S; ? proxy_pass http://online;
( B3 }. h2 |2 ? s proxy_redirect off;& W3 s( |$ f8 B8 }
proxy_set_header Host $host;
% Y" i% H( h4 n/ l proxy_set_header X-Real-IP $remote_addr;: W" Q; H4 @6 E3 v6 _& i) ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$ B) b' A) e4 a+ `( B; n9 { }4 c: C3 K! B! F
}
& C: s- ~% ?2 X& o六.测试% Z$ S3 N& t1 H9 F* e6 K0 o1 M$ ^
4 U4 ?+ n. w6 h0 h, G我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) Z2 g; p/ ^4 w$ i+ L* S5 r$ U5 r
$ ~2 @) G* E2 p3 A' R& j" O<?php
' g" Y4 F* D9 r6 _: K4 o. B phpinfo();
/ j7 V* V9 S& Z N?>: k8 W) p: |& o4 S+ ]2 V
在浏览器中访问:3 z$ i: J6 X7 T. C
$ Z" t& r/ @! O k$ u2 c! F4 s' Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
7 |. }8 o( i* Z# }8 Q+ Mhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
, O9 V$ w8 U6 M& n4 d9 L& C( ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
/ ?* {$ I7 X- B% ?. q6 O说明sql注入和xss已经被过滤了" U9 j! _ t& E: d6 ?, k
}' z7 }0 P1 g* X4 j. z七、安装过程中排错9 D9 m: ]/ S0 L; L0 |% U
: V) u% h5 P+ g+ x f6 E6 b# t
1.缺少APXS会报错9 w5 c+ D/ \6 V- w$ x
" E7 Y9 h" u6 N4 w
configure: looking for Apache module support via DSO through APXS; ^1 u9 M& X- {( X. a
configure: error: couldn't find APXS5 |) [, p6 P# n9 @" f' b# N
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。6 N/ i& U2 ^" u+ U2 r5 ^1 }
解决方法:9 E' P9 |3 V+ o4 p
C1 H% B- ]1 D7 O# F- g0 ^yum install httpd-devel( x9 \9 o' D4 o" b, q1 W! o
2.没有pcre% R1 G9 b5 g1 d' l6 H: i
% ^' b0 h3 e! u/ N4 |configure: *** pcre library not found.
: Z% Q+ K$ g5 S a7 b0 Econfigure: error: pcre library is required) v& W* F8 C+ m, E
解决方法:
" o' [: O! S! G$ _3 w# ?: K% K* w: T! A3 L @& I# L. K0 H9 }7 z
yum install pcre pcre-devel0 z Z4 i1 q7 f
3.没有libxml2
) q4 k. }1 C' l0 [7 G2 s; @9 K' b# o' R, D U
( z. n, T" G) u5 A
configure: *** xml library not found.
2 V1 m2 \6 A1 a0 s' z7 }3 ~+ Kconfigure: error: libxml2 is required: O; z C3 k0 l" D& C O
解决方法:$ b' U" O- K- ?
; Y; X9 Q w: t/ ?
yum install libxml2 libxml2-devel
% K: l+ O9 F' L. r4.执行 /opt/tengine/sbin/nginx -m 时有警告
9 |- H5 j/ K/ Q' }7 j8 ^, [% p3 U1 y7 I7 ^" T
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
9 S/ c- W$ b% _- e. Hnginx: [warn] ModSecurity: Loaded APR do not match with compiled!3 N$ l5 b0 s: q" U. @: E2 Y
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 k4 C* u0 H* u
, O+ \* O2 s5 ]1 v" F: y% f2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.' ]$ ?+ e# ?8 q1 W
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
8 z d- G+ K$ u% [* ?5 O" d2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
4 K. o+ i: n% {- {( u/ W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"0 p9 k- h# j! a+ H/ b. n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
" L- V- b: a) E5 ~& J# h3 } Q- H2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
6 ^% p6 B" B- F M2 Q8 \2 t9 y( s解决方法,移除低版本的APR (1.3.9)
8 }& R' M ~: y3 d( Q. v' @. R M; N2 F7 x
yum remove apr
+ }4 y& Q% q* `& t$ ?5.Error.log中有: Audit log: Failed to lock global mutex
1 g! z+ l. ^6 p( N1 g0 r, S0 s. v& |" W
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 `4 Y; r! r8 a1 @0 _9 V- F, ^( j
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]9 ?- r! l/ `& D. W$ h4 d- ~
解决方法:9 N8 N3 h( l; Z8 r9 h4 w
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
3 }2 ~: n6 f4 m( n' k) m. H1 ^1 N8 |) h2 u) v
SecAuditLogDirMode 0777, }# U! e& \8 j% p: u% _
SecAuditLogFileMode 0550
$ v- _& W' C7 ]SecAuditLogStorageDir /var/log/modsecurity
7 ]/ L7 |1 o, Q9 w7 |9 oSecAuditLogType Concurrent
% f, g' _/ t1 U* U9 r参考文章:
5 O- b0 v* q- M5 L/ c% Uhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX+ Y& S7 K/ R5 ]9 _; {7 N+ H
http://drops.wooyun.org/tips/2614 |
|