|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, l5 x: n4 J7 e4 g G: @ A2 L
2 s4 ^" l/ x- e4 ?一.准备工作2 o/ G& ?+ d- M) t- ]' U8 B
# F) |/ S! Y2 r; i5 R8 |) T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0 t" V8 l+ k' q6 |! u; s. |
! ^" v: ?2 p/ [& R$ x" z2 qtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz' [- T) q7 e" ~4 O0 h2 o
; y6 ~! l" H/ p: H7 D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz5 i* ~, K0 r. u/ h- z J
4 P( e5 Y8 O3 t9 X
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
: I6 F$ g2 l$ C1 r% Y6 |4 K, h1 Q4 G" ^
依赖关系:6 H ]% U$ l* K9 h" o" v: a5 y( K
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
# m, }! n- I1 d( T/ `2 B+ N2 @3 s. j4 U
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
" p, |+ o" w8 l, {modsecurty依赖的包:pcre httpd-devel libxml2 apr! c$ J: \1 b, \0 a/ Z! X
( X" U" ?! ?9 `2 K& ]
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel$ |0 H# Z! f" Z0 S& F( w
二.启用standalone模块并编译- z1 w, H$ k0 V. r, Z% @9 P
/ j( _7 i3 U! d/ `) v下载modsecurity for nginx 解压,进入解压后目录执行:2 ]' v2 \1 H7 O+ K
. }5 Z" i7 i. @' a/ S) H./autogen.sh
# N0 O- I5 f. v ]0 f) N./configure --enable-standalone-module --disable-mlogc
/ A2 n+ N/ Z6 I/ M$ d# ~; Omake $ D* ]6 Q* M2 Q4 Q3 Q* L8 t
三.nginx添加modsecurity模块8 V) o+ l2 M. |7 t
7 r* k" E0 o! V% r+ H C# W. y' B' O
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:( `1 w8 U6 }% @
& V s: T& u+ X" I& I
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
; m1 g) i# R! ?" h- `, fmake && make install
3 v# M, w }* ]& w四.添加规则* E, I' S; h. H& n
+ N% `' M$ N; |! \( Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 s! F, E- Y7 h" b
+ n' Y3 s1 j) Y6 B, y; a% [1.下载OWASP规则:) w* }. ^. T( _: m
- \& Q& S. v- i, S" o9 f; agit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 t7 s9 L0 v3 \. @: l
: s; `& N+ q6 q* tmv owasp-modsecurity-crs /opt/tengine/conf/# N8 |! q6 Q6 m) A
( [9 P' I9 M ?; z, ecd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
c# m# b w! s" Q2.启用OWASP规则:
]3 n0 ]& s8 s& j# n) U$ d; f9 u) w, J7 R; r$ c6 b8 T; G+ Q
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
, ~9 m; L, L6 d7 J$ C
( f+ t) N3 G+ g8 n" }9 N& v1 j& l+ s编辑modsecurity.conf 文件,将SecRuleEngine设置为 on& L1 T! }. i" ?$ Q- P
* Y2 c- V$ d1 h6 g2 m/ m, @ R
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 l9 V4 ?) O+ g% j$ M) D# G
9 y% X8 V; F' d6 H) h* vInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf" g& J' W8 O+ f+ m) D
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf( Q3 p0 ~: s* |) j5 l
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
! l, t+ T4 ^3 y( V, P3 ?- }Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf. t& g; x! t3 O, Q% l: K! w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf; D, `- e+ @+ z+ {6 q& a- C
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
& @5 h* }$ b6 u/ D4 L7 q) M8 iInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf3 w2 _3 i8 T4 d& _
五.配置nginx! E8 E0 w8 |* L2 C. S
& p9 n5 n5 N' A0 p& o* i' _! p
在需要启用modsecurity的主机的location下面加入下面两行即可:
2 {2 h/ Y4 w$ i) I7 E8 j" h9 A3 k/ P5 ?5 }& G; b
ModSecurityEnabled on;
! ^; o9 u; W8 T( H! }- C6 aModSecurityConfig modsecurity.conf;
8 \' U; L2 T8 p3 Q+ v下面是两个示例配置,php虚拟主机:% [5 O* p6 }7 A: \
! Q7 b" O: w. ]5 W: }- qserver {
- M. {! N O, s0 L% G( D5 \& w listen 80;; X2 k v4 Q k& V* s8 _
server_name 52os.net www.52os.net;, B3 z* d: D% w' w( J5 @& L' ^
- I5 b9 d! @" l location ~ \.php$ {
! G- D0 Y/ g; \" Q% }, W f0 z y ModSecurityEnabled on;
) e, n2 w6 @& e: o* a# A; | ModSecurityConfig modsecurity.conf;
3 l& p' k- M$ ^/ @. O. R' }- l- J) O
root /web/wordpress;
, V) a. j8 J% ] index index.php index.html index.htm;
* r* z3 X9 x7 M! S! }, ^- s+ S
. b$ @) u% F1 y! S8 h. u fastcgi_pass 127.0.0.1:9000;: D3 P6 R! L8 |6 }, P# E2 Z. Z
fastcgi_index index.php;
- U9 Z" Y" ?% H" G( M fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;$ V) o5 ~0 g# }' l* x$ c& v# ?
include fastcgi_params;
' F- y* z- O2 P3 s }6 h- X% V, p) I+ R: w
}
# T+ X7 E& h8 O6 {$ E. aupstream负载均衡:; R% M R, n! i. Q& X
9 l9 `, x5 @7 Pupstream 52os.net {' o+ e7 ^! R; q9 f
server 192.168.1.100:8080;% Z" p; X. m8 o+ v' i1 n2 [
server 192.168.1.101:8080 backup;
% |+ U/ b4 g9 {: H# m6 Q- J}
7 u I" D$ Z8 P7 x, S- {2 W! W8 ~! }" L5 g( g2 i" t
server {/ T& x- ^6 T1 D1 F4 p( K0 |2 s! D! g
listen 80;; h' e% _2 {3 H0 F. K
server_name 52os.net www.52os.net;
6 ~- b: C1 o$ [: w! v
6 x% i0 K$ ~; W, ]# Ylocation / {
0 \2 }; C) J9 z ModSecurityEnabled on; ( ?) R. c6 Y' x
ModSecurityConfig modsecurity.conf; 7 \% _8 l) H" P8 Q: a
7 ?% N4 D: D# v- p proxy_pass http://online;3 H- m- c3 T% G! L1 w! x- H7 t0 y
proxy_redirect off;
: f4 C/ m. |+ [# d7 n0 ^; M proxy_set_header Host $host;" x+ e& A, B- c' H8 x8 S
proxy_set_header X-Real-IP $remote_addr;
9 |$ o- v3 @) Y' f$ E2 Y7 a9 C proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
' u& Q2 ~! R* ~" a- ? }/ u; k3 W7 V1 D2 `+ f4 a! m1 @
}
7 z2 |' E( J' ?+ y( J; f六.测试1 f' e, t/ X8 z5 b* G
h0 ~% X0 p U5 N6 y9 Z e
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% z) ~' ^8 H2 ^
. h3 {* W% ?6 |6 K* C- L<?php; O+ b7 r1 c: n v3 A
phpinfo(); ' A# z* e7 d6 H* K1 X% g
?>! d8 x! C$ ?+ Y/ O9 e! T
在浏览器中访问:
# K# s7 ~) G# g. p0 b
8 y( p8 F q7 E+ H: U: q: ehttp://www.52os.net/phpinfo.php?id=1 正常显示。1 d0 `% F1 u4 W' w; n
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
" d# R# r& v1 G* u4 e: zhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。! S7 \2 P H6 ~$ o" m) T3 v: E" Z
说明sql注入和xss已经被过滤了+ N! O+ r7 C% I. O5 [+ J1 }
0 {% F8 F3 U- C- Z+ I) ]% g, K! Q七、安装过程中排错
4 L' o& j5 X; Q6 Y- _. T M/ }$ u* C; j k9 r) ~ m, L
1.缺少APXS会报错: _ H5 B; D0 \! \
" Q \( e/ t( G3 t. a
configure: looking for Apache module support via DSO through APXS/ X+ Z1 F% g& _7 `8 |; d
configure: error: couldn't find APXS
/ o: V$ Y( g: T1 bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 K N, H. i! j解决方法:, e t$ W+ l4 s/ @ ~' {: w/ w
6 W9 R5 d( a" k2 A
yum install httpd-devel; ]0 Y$ Z* q2 Z% ~ c: b* |1 O0 }
2.没有pcre9 M' c$ I* O* ^
m! E% m4 s7 M5 ?5 r5 V
configure: *** pcre library not found.3 a4 g3 C) |6 w9 h
configure: error: pcre library is required
. T! p0 P& B. u1 C& J! _# t7 s$ U5 S解决方法:" [8 ~# L) p% L8 M& [
: x& C: \+ Q1 B: i) Uyum install pcre pcre-devel
) @2 l' I: f& S% E! t- P' K' O3.没有libxml2: Y7 f/ B4 ?6 N' F* R- V! ~
- |1 _& V" X# E3 v$ t' P! B. C! ?
7 q# A5 T+ [- t& xconfigure: *** xml library not found.
' k2 W/ b7 h- Cconfigure: error: libxml2 is required
/ |* A" M; o$ |4 r解决方法:
6 L/ Y, v, I% i$ p) j# N
9 ]! [+ l3 a0 u# F5 s# Pyum install libxml2 libxml2-devel0 X" `8 n' `# i* J1 l8 x7 p
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 d) r( d) A, a( u% p
; Q+ z% N4 ~+ l3 r9 C: V, n: W
Tengine version: Tengine/2.1.0 (nginx/1.6.2)/ @6 Z8 K; [8 r! N* r% M
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!4 F; e, W/ L4 I2 ?- \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log2 c; A# |; S* @1 R. k6 W! V
\9 K/ P& c% U3 I' d% A [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, C0 \' q1 R. e2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
/ r+ g$ m& p, T2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!: z @7 P* G. H6 G, Y' T4 O3 r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" ~& x! L" w4 i2 ]3 T) q: R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
U4 y) Z. ~4 s, |2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
+ b& L. g' s. V B' P解决方法,移除低版本的APR (1.3.9)& ~% i1 Q5 O' k1 } J F3 k' G" U
: O! }, S: [% U' F$ g$ N+ r( lyum remove apr) s. h. q4 _2 ^
5.Error.log中有: Audit log: Failed to lock global mutex
+ g/ E, e+ S4 c' q% _" B! ?; |/ k( [* c8 |' ?# O
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock # ]7 Y* {) A' V( z! G' u; q" I+ H
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
! w; [0 P( h# F5 p4 D, x" {& ]0 u0 Q6 z解决方法:
6 w+ M; ]4 ?; C- n编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
- U- J% Q9 o. v' v- p
5 x# r% s# P# L* M2 GSecAuditLogDirMode 0777# V. h7 d( y+ p2 M& {$ }, j
SecAuditLogFileMode 0550
|( Y/ I' f' f$ ]- D! S. R9 ZSecAuditLogStorageDir /var/log/modsecurity# b0 b2 ~2 k, Y' \
SecAuditLogType Concurrent- L) \) X, n& I8 U, s! Z2 e# Q
参考文章:
. P% N9 a* `) E4 c6 t9 M- |8 Zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 [! J, c" f) ^ S8 \http://drops.wooyun.org/tips/2614 |
|