|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
% {. @+ s; Q$ }9 U( }0 j; i
$ B( w6 h2 F, x9 m5 w0 z一.准备工作
8 o' Z$ f# S! ?+ j8 g9 d5 [8 L! S7 t1 e4 |; h
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 a" q8 ~: f4 W9 p) [' @
# j/ P3 I9 J6 ]% d3 Z
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz9 s% A7 Y6 t+ I# I: V
; \2 P6 ^0 `6 F M
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
$ e" u- o+ Q4 ]/ Q0 d
8 j) B$ w0 M" |9 r5 wOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs1 m m$ m1 g+ k( @
1 J" M) C0 u$ d依赖关系:
$ n1 L9 r- E$ ~# q. t# stengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:: {- [7 }# M7 c8 p0 e! @
6 V: t. W+ c" ~. f! R8 ^
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. e9 \0 t3 ^" V omodsecurty依赖的包:pcre httpd-devel libxml2 apr3 h" y# F3 m/ t& I& }6 N( |# c" U
% h, g8 g9 U$ B* J
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
. P/ B- m3 F! B. X) ]- a二.启用standalone模块并编译
; d, Z+ o% o2 t* ^# M# ?7 W4 e# \! J O7 a3 B5 q3 ^
下载modsecurity for nginx 解压,进入解压后目录执行:
7 P+ @! g3 x) h8 V4 c9 k4 n9 `3 b+ J* S" X6 G# k
./autogen.sh
+ H& k" o8 }- F3 i; G./configure --enable-standalone-module --disable-mlogc
0 a2 {+ i! S7 K! V/ A! X0 ~& fmake
9 y8 Q" @) D" n三.nginx添加modsecurity模块
; k* v+ g9 l! e( I- S" o7 d/ l
* M" e! f, N9 c. F在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:, b+ s' r( m# B6 b' d! D2 v
5 A" f4 C, p7 o4 _& V./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 a) ^ ]6 D/ s4 lmake && make install7 w7 S" t% v7 [/ X6 p/ x6 _
四.添加规则
0 |) u' K; q1 Q! B# q
; J+ W) J$ Q1 Tmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# F# |% l- k* l `" x' Q) C& r+ H. W6 T; q+ m1 u6 M
1.下载OWASP规则:
3 `4 u8 \2 N5 W8 K& g" y: I' i" }" R' O# k* s( Q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
9 V, L5 U P9 Q' M) |9 v6 G+ r" F4 ] c- W: q, X0 e
mv owasp-modsecurity-crs /opt/tengine/conf/
/ C4 F; l/ d0 U$ w! B6 r- [
- W2 U- d5 X: K4 b+ I/ Kcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
6 S$ M& y- C5 i$ K2.启用OWASP规则:
2 w& p0 m. a7 W9 Y
. ~( Z8 @4 q: c复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
* h' l9 \6 R, q4 F, h* a3 Y X R9 ^! H7 A- g
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 c. C0 m w3 c0 p: _+ ?) j* E
2 B% z- W( B6 }6 I5 K" S
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
8 m/ \9 R2 _4 O t& k2 X( l
J1 j4 w/ Z, \8 gInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
; B" |1 z- g7 m6 q6 G nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, H- z8 x4 K- n$ W1 S! sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
, w# i1 s& |: sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf! @. Q. e+ ^ c) d: Q9 Q4 ]$ S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* T1 S! i( `3 N% X
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
4 m+ Q4 s2 c, Q6 YInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
- |) z, W( F2 Y% v( ?/ J( l五.配置nginx
% G% J# M5 B, C- p5 c/ m
" o$ S" F5 H ^在需要启用modsecurity的主机的location下面加入下面两行即可:) |9 ^# m1 B5 [4 W
4 A5 o' T* l3 x# B7 _
ModSecurityEnabled on;
+ l. q- K* \2 ?ModSecurityConfig modsecurity.conf;( i: q1 ~4 D* X2 m% P1 v" d8 M
下面是两个示例配置,php虚拟主机:
( o/ O, I* C) B5 E: c6 F. @+ \
) [6 Z! z9 A) O& N. ~' n9 gserver {
1 X; ` l F9 l" E" n- p listen 80;7 ]0 w; O9 ]9 J+ B1 r3 J! V8 o' y
server_name 52os.net www.52os.net;
J6 f7 a( A) x, g9 j
2 b9 s+ r& O& F# r; @9 a6 m) f location ~ \.php$ {( o+ g- }0 M+ O1 G+ _" ]
ModSecurityEnabled on; ( t. i# V9 O1 b" F! r
ModSecurityConfig modsecurity.conf;2 C; k5 L2 `% W7 n7 x
& R+ ^' a# a% l, {& V5 d5 D0 [ root /web/wordpress;6 |+ x- ^+ ]/ A8 r" d' H8 G+ K
index index.php index.html index.htm;3 R$ Z, \1 b0 x& Z1 M6 T
" F5 d8 n+ E; |, U/ v, k6 c5 m* e fastcgi_pass 127.0.0.1:9000;) c* ]1 u0 u" G& m' D+ l
fastcgi_index index.php;, |, \* X6 C( y j
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
) Q2 R; w' p& [9 k include fastcgi_params;
' ?; a& @5 C! c/ m% Q9 B: k, B8 n }
C# J. b% b( C$ f1 Y }8 m3 f3 h' U2 |8 B( u3 Q+ ~
upstream负载均衡:' \: B% G- N& V8 s5 j
h9 f) A( ~* q! s. g3 U4 V* r0 `, y& O
upstream 52os.net {6 K5 m4 j4 n* j. C H
server 192.168.1.100:8080;
, _% U" D( \/ o: r4 Y1 P5 H server 192.168.1.101:8080 backup;. Q4 F8 N1 p8 h9 X8 U1 G
}
& {& }& M p7 X5 ?7 p7 V7 w! h! R4 a, a2 B+ F/ t9 h/ ^4 y- k, ]
server {
3 D( U0 @' i8 n/ M; {: `, M zlisten 80;
% @9 H/ }, ]* K& ] [" p$ f$ Sserver_name 52os.net www.52os.net;
6 W$ r% J z" P2 k) q, h1 {0 u1 C: S
location / {
/ V+ q2 M. y' [' M8 r ModSecurityEnabled on;
# l: Q- [) P! e9 N! B& p ModSecurityConfig modsecurity.conf; $ j* G% d' y. Y, H
. |3 f6 Z& |7 _ proxy_pass http://online;- O" Q- k& M B/ {) l) }
proxy_redirect off;6 O, o4 {5 o/ Y- }; l
proxy_set_header Host $host;& N2 M; Z. ^# @2 ~
proxy_set_header X-Real-IP $remote_addr;
# i! J) h* t8 \" {3 Q3 ^ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
" u) Z* I7 i3 y& |8 Y }4 y/ @2 J5 s6 o! l& D6 z% x8 c
}* e% i; m2 t, A# _; y$ M) W; [& O
六.测试
8 u& B- F4 ?: {6 D
( r2 S: z3 ?7 q+ Z3 Q8 n我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:. I3 \/ n# Z" d I, @/ Y4 d
, L' J i: r. p. ~2 ]6 }
<?php
1 j0 f" @' p) N. R& f/ H' M phpinfo(); : Z4 [/ ~5 d/ V6 w
?>1 K; c$ Y! r- }+ E" ~1 s3 L* e
在浏览器中访问:
" p& Q2 i3 [8 ?! M. F; q: \% A
b$ ~" x, H- F. B, k8 whttp://www.52os.net/phpinfo.php?id=1 正常显示。
% `, T. J8 O/ w) X2 C/ Y0 f; x" B7 Dhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。5 n4 |7 O. f8 O. n4 J
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' d" w, A2 n6 }
说明sql注入和xss已经被过滤了
& e* j8 l2 X; W! b/ w" Q1 A9 @7 k& z7 A/ ?% Q
七、安装过程中排错
. }: F- }- m( p( e7 ~1 d
) X" e, H, P: [! P1.缺少APXS会报错
4 L! }( x- a5 M' E8 a
! `9 V% X. w! D( L3 [configure: looking for Apache module support via DSO through APXS
$ R0 N) E4 @% r( T, s) oconfigure: error: couldn't find APXS
- j) d3 r! S' h \- b8 K4 j. Hapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' ]6 x# Y/ a, v5 Y: t解决方法:
" t4 a1 |. D2 s+ ? ^# p) r
. U7 ~+ A- _! Z7 Dyum install httpd-devel
5 I- m; K+ G% i% z* V! r& f4 N2.没有pcre
" Z3 ~8 |( F! w1 p
9 F3 R. \8 {1 m, ]* w: }configure: *** pcre library not found.
& X/ P/ F, ]$ ], ^7 Fconfigure: error: pcre library is required
& z& V9 O. B4 g9 E3 w解决方法:
6 i- b8 @& w* O* h2 b
6 }, |4 w9 e; H- }; B# Wyum install pcre pcre-devel$ t9 Q2 o- @: M( P# P
3.没有libxml2' R; E* I1 _4 z3 h9 \
& t b4 }# f6 E. ?6 O7 u3 e2 J/ y0 h5 J/ a! g, {. e& F
configure: *** xml library not found.8 j5 T; Z& b- o; m3 }. a
configure: error: libxml2 is required
3 B4 W( {+ G( h/ ~ k& {解决方法:
. p3 r1 ?: {$ z1 B) m- l
6 m- n9 [3 d1 ]! Syum install libxml2 libxml2-devel9 a; u, O% _8 ?' x$ Y1 a$ [
4.执行 /opt/tengine/sbin/nginx -m 时有警告
" y4 r' K% E; g( Y$ l. r/ u1 [0 k A2 ~" B9 `9 |
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
v6 ^' G! ~$ `; I# [/ znginx: [warn] ModSecurity: Loaded APR do not match with compiled!
/ v$ E' |0 r# Z! \- \* P. w9 o, {原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# b! ~8 y* |1 `+ u2 T( K
6 Z( s0 ]" B4 n" B
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.$ t. Y7 M2 P7 D4 o& x6 u+ A5 l$ T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"' F: C, q2 }2 A; q: P
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& Y9 t4 O- M# L$ B3 r. y: f- m6 R2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
8 u8 p K5 P5 p$ |- f7 @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ j. m5 A$ j- U, Q5 P
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., ^3 B6 m0 ^* Y- n9 c& p& f
解决方法,移除低版本的APR (1.3.9)9 w0 l( _/ P! \( M
& K2 G' V0 [; G; d. I- Fyum remove apr
8 v7 ?0 T M) x5.Error.log中有: Audit log: Failed to lock global mutex
' j; J* {! {# ]$ U- m! o9 `. n1 X! u" S1 d4 j' U( Y$ i
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock + s+ H: G+ T: c/ u8 @% t
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ `; V9 t- ]( ?. Z
解决方法:
0 Z+ u- `- y9 w2 `6 ?* z编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, S4 x/ O4 }8 g0 l" l- t
" i2 f- l0 g* ?2 J; a* X1 D. JSecAuditLogDirMode 07774 V) {+ A0 e% ~8 V
SecAuditLogFileMode 05502 A7 ~1 N: K" j; T
SecAuditLogStorageDir /var/log/modsecurity
Q; A ~0 P. P4 z# D8 h# i& r; qSecAuditLogType Concurrent# P" ^7 A+ z3 K$ z7 O
参考文章:; A5 _' t0 Q$ B
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; A1 {- n# P' a# _$ R+ p( Rhttp://drops.wooyun.org/tips/2614 |
|