|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
4 w( {6 t* D( g1 q- l8 R) s9 n( i% T; u
一.准备工作6 O. g; U- k q8 \% P" `
5 M0 Q7 H6 l2 T: G5 j! c3 p
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, M3 Z: E5 ~, g' B; C$ ^, G Y
- H. \0 \" _) ?9 E2 g; \ C
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& G) k5 f: W* J$ S/ t% m0 N5 [3 M; k
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz c( ^# m! O7 R# R/ N7 [' c
* H$ u: V% H! V; \+ d* g/ ^8 |( x z
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& t+ l& e. C; t' K. x6 p8 K3 ?! U) `+ X; K
依赖关系:
3 }2 ]5 d; V' Y' c. {9 ntengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
5 E( L, {( v5 S! A( N+ e) G' B0 l: O/ |; N9 ^( `3 H
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& o+ r& b Z3 _4 L @3 N/ k- N( wmodsecurty依赖的包:pcre httpd-devel libxml2 apr
- S# c: ?) z# I+ a2 s
/ \0 s' r4 u" B* q- D, s4 g8 [yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
3 B0 _% F7 ?( {% G9 G- m! h0 M& o二.启用standalone模块并编译# E; T2 a. Z( x
n# Y. Y" D9 R+ d
下载modsecurity for nginx 解压,进入解压后目录执行:+ o. G2 e7 O9 }3 S0 x; `) K
- l, v& t5 ^" X4 l, X; P./autogen.sh2 p* t9 X* H1 M! Z) i
./configure --enable-standalone-module --disable-mlogc2 F1 D1 `+ H+ a1 r" t1 f
make
& o) F3 Q" R( \1 a4 ^三.nginx添加modsecurity模块
/ o+ n V# H4 q2 W$ k. t: @ B3 }9 w8 C9 r, _) q7 P
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* w- W% m# s+ Y8 n# R
! ~( k4 I: H! Z9 X6 w$ M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
U" ?% r% Y' E$ N0 N1 qmake && make install- K9 Q& }2 v9 j" V, `
四.添加规则
+ d6 {* }& {; ^3 S9 f/ H& p
. b+ q1 O3 B' K- S! z6 Pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。" g9 {3 @) L4 ^* l z" a
7 L+ @) m2 j: L: n/ n7 ?0 Y/ z' Q1.下载OWASP规则:% b* Y' G& Y0 F/ y( _; @4 L2 u
& _" q( K2 f/ R4 D& j$ o D9 c
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
' w: Z" n h0 b; w
8 S6 p4 z+ h( M( B+ fmv owasp-modsecurity-crs /opt/tengine/conf/# X) @' z! F* U- S9 C0 R0 `
% S: a9 P1 ^6 u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
5 s5 C& @3 F, H2.启用OWASP规则:" l' @- B( r2 ? M
6 d8 ?/ k- o8 V3 f9 w+ G复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
- Q4 x' q& z/ J' W, I* g+ O- {: }1 ~3 W6 D2 D' B9 w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on4 c. w, R" s W
% O/ W7 O& I' Y$ p- @: G6 cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 x U4 k% o: x/ {
! j, G4 U0 O0 A( jInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 G7 j+ \, v) x2 [/ z0 F- z' [Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
! k% w4 {4 R! j' z1 P2 cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf6 i) e- E* _- m! k7 t, Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 ^2 x; w9 i/ y' H1 \+ B
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
; a5 K7 A! G) o$ {$ J* m" N, iInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf, Q1 W0 }" t/ w& {- T# J ]
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
/ F- v% A* U( e/ W6 E+ {. O: |7 H. I5 Z/ ?五.配置nginx
; Q# B' ?0 H" ~6 _5 g f6 o/ _! v5 G, K$ `$ G' I0 g
在需要启用modsecurity的主机的location下面加入下面两行即可:
. y* F. A( g, I! P2 r5 K
( T; R6 G! S+ i4 X! S9 AModSecurityEnabled on;
7 ~: }3 d" L+ ^0 Z8 B W+ {$ MModSecurityConfig modsecurity.conf;4 n: m) X/ r9 a) I2 ?
下面是两个示例配置,php虚拟主机:: c! e: f2 v* S5 ~7 o4 h
' R1 y+ U5 d) c7 k& w
server {- y+ z5 x; a8 Y( D, t$ J' v
listen 80;
" u' I0 z) t7 d: E1 e server_name 52os.net www.52os.net;
1 z" e: E. x5 U7 l / o2 a1 g0 W( M& j/ H
location ~ \.php$ {
, u! g$ x+ D) d" _3 b ModSecurityEnabled on;
2 Z7 A4 g1 }/ Q1 i$ H* q ModSecurityConfig modsecurity.conf;
; O- D1 i6 T1 u$ L- i- I- w( i
4 F {) u" t! k r' ? root /web/wordpress;2 ^# c) a1 k4 z6 g; z& [. F
index index.php index.html index.htm;
! U$ A' K3 G6 i4 q H+ X! x
6 B+ @/ D! H1 o fastcgi_pass 127.0.0.1:9000;
* @2 J9 o1 ~( x' A; h3 ^ fastcgi_index index.php;' Y1 R$ P2 ?7 T3 x: y' ]& K7 E
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;& F) ]( U* G7 d; g2 M2 t
include fastcgi_params;
; j: h: u4 }2 X# L# t. W }
1 m9 X' I% ^) u( q& q0 |: A2 u }
8 K |+ b ?; r; o( R. {upstream负载均衡:
) Y- g5 U. v* @ a/ x* H/ e1 X
7 F. `2 S( |( \ V* ~3 kupstream 52os.net {& A0 [9 ~$ D5 f. C+ C9 p* {) m7 \
server 192.168.1.100:8080;5 ? d0 ^5 e* J; C3 h( c
server 192.168.1.101:8080 backup;
! q' g& N2 K* D7 `5 n- V0 Z}
7 \( |+ P8 e6 N3 Q; y1 _9 w5 o r: c( |% E, z
server {3 @' Y8 b7 b8 s9 u
listen 80;
* f& h9 c2 q; Q+ `* Z ~server_name 52os.net www.52os.net;
, G, V$ p' E8 X8 h9 Q5 Q7 \* t8 N% O8 c; j. b0 k
location / {6 V/ \' S; u- A! n' A/ c* H
ModSecurityEnabled on; 7 e/ s+ O# v; }. d! v
ModSecurityConfig modsecurity.conf;
# M/ r+ G0 B9 s" {; S, U, c9 b" y4 b7 B i
proxy_pass http://online;. A& }3 g# U! K/ O4 R
proxy_redirect off;, T/ B; I0 p- X. K' C
proxy_set_header Host $host;
3 b& o6 D1 g# w! X2 Q% H( L$ F6 x+ { proxy_set_header X-Real-IP $remote_addr;
" ~9 o* H/ ]8 y; e+ Q; f/ N$ [ proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
! C$ L4 |& D1 X& e+ p }+ x! [" G, F9 Z! ]" l, v% s
}$ ]3 Q% `4 Y# ]7 h% S3 {. U
六.测试# ^( c* l _# N/ \7 M
1 D6 i- c- w7 c$ C% c0 S; g' _
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ E3 Z' [7 m: K t5 n) r; f: A, o* Z' s2 u* O2 B/ Y
<?php
) |$ x0 Y7 ] ]4 @+ Q& P' P" ` phpinfo(); # S" }- y2 o: c$ {0 T* a9 E G* X
?>
+ a. V. W: ?9 ^/ Z- c7 M7 Y8 s在浏览器中访问:0 a+ V+ s6 g! _% k
3 |/ l7 c( o0 p4 F1 L5 i& t
http://www.52os.net/phpinfo.php?id=1 正常显示。# L' `0 o/ ?( P/ `
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
8 b, S' q1 j+ F; H9 e' Vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. r1 w7 }6 d) ^1 `/ I1 I! `
说明sql注入和xss已经被过滤了
: _$ P8 O/ O: c e+ f% T: p( j* i9 w, q4 i. }7 C1 P4 E; [7 L* J* [
七、安装过程中排错- y7 Q1 r6 b. b
+ I8 y. }) A1 b. ?. ^3 v
1.缺少APXS会报错
7 O h+ \! \! a% }; r+ }+ B+ A' g3 s/ h% {
configure: looking for Apache module support via DSO through APXS2 ]& _- j( M! s$ i4 j
configure: error: couldn't find APXS0 o' D4 D6 p, \, Y' V0 ]! h
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
& D( ^# E% \0 Y解决方法:
8 N+ g; J6 F: h N+ h; B
( Z* c& H/ C) X# G% L. g% S. byum install httpd-devel! u0 u0 l7 L) B( B7 J$ [3 d
2.没有pcre
" k! b0 u- k. Q5 [5 P" J2 g9 e+ A( e; E& }
configure: *** pcre library not found.
& o& p# B3 b. a: `8 m7 Wconfigure: error: pcre library is required
; [$ p0 ]# f' N! ?/ K6 ]5 s6 {解决方法:
# C! O' D& o) Y8 b d, _. B
! l/ h/ |# o8 }! Tyum install pcre pcre-devel
& J7 A% ]3 D, K: g4 z3.没有libxml2, V& w/ b& p) k* M
( E3 P0 f8 |# q. A8 E5 \7 I
, {3 ?. D. J* P sconfigure: *** xml library not found.) u: k# e( w6 B) z' Q
configure: error: libxml2 is required
- V! w* X7 o+ R @& h( _) d+ M" f9 M解决方法:
7 O. I) X. ^0 m8 }( S6 Y3 y; B- c* C6 x
yum install libxml2 libxml2-devel
+ I, A C6 D4 ]4 z! N2 U- X4 n y4.执行 /opt/tengine/sbin/nginx -m 时有警告
1 w5 g" r* j* X; W9 I/ z. r1 h9 @/ Q/ m0 l
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% Z! n |$ k5 W, B" C$ M$ V; Lnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
) g0 l$ R8 i' C3 u1 u9 ^4 e: k原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log- e p) k; r$ [, h* l, A) W
% k2 W( q( ]% ~ f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.& Q" ^2 j/ f* c& E
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
% F& v# w# K3 }0 C C1 f) Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 I0 S, p) P! z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; w& {6 f: j6 b# a8 Y) c2 e u9 e, N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6": t" }$ D5 H, a, J
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; W+ B4 U. C7 R# s
解决方法,移除低版本的APR (1.3.9)# ?" }0 I, k+ d7 a2 @& N5 S6 V
7 j6 O% A5 w7 y6 W- d4 v! byum remove apr
( j) N* S9 u- T) x3 j5.Error.log中有: Audit log: Failed to lock global mutex# t/ z9 P7 `& n6 r
z0 t) d: I/ a& W0 J8 ~+ o
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
' m/ W* w) J% K6 }global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]5 N F" `. h1 T, ~& w1 e
解决方法:
6 P. N1 A5 g0 e$ {% w3 q3 V编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 t( c; f0 q4 r0 h; S2 k
+ G) p. _! d4 e. F1 lSecAuditLogDirMode 0777
i# ^7 @) Y5 I* QSecAuditLogFileMode 05502 X# n5 K8 ]" F: k% M; r6 V
SecAuditLogStorageDir /var/log/modsecurity. I. i& ? I3 g7 C9 X, F
SecAuditLogType Concurrent% ?; |3 H6 P# e. }: U) n; Q o
参考文章:
! ?. {% G( u" w" A. jhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
1 P, Y& o9 g& k {7 P: U" Z2 l- xhttp://drops.wooyun.org/tips/2614 |
|