|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 X+ d. d0 @8 L. [# T7 J6 ^" D4 R6 `6 T$ s" v: ~3 f
一.准备工作
/ p# g7 X' F& ]3 _! k9 t
- H6 d0 m& B6 }0 [& [% f8 }8 }% P' F系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 O3 b! }! o- b7 c& ]- ]8 B U3 N' X$ H7 o* l0 _' l6 U
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; X8 j4 D* e1 Z
3 z8 r3 g# \# t3 ^+ F9 o M9 Kmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; Z' \: [; t+ u6 }0 W
9 y+ P* {: M! R0 I$ j$ Y; COWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs5 Z* E/ p" G8 |" G4 ]
5 a8 [* x9 _; D7 m
依赖关系:' s; D: ^$ ?# @" v0 {- N* ]
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
U2 n; C- R) M2 q/ A/ c. r) c. C& J8 r+ B8 b# a2 h
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
. X, l+ C1 `4 m9 W# i6 Xmodsecurty依赖的包:pcre httpd-devel libxml2 apr
& r$ f. @) H. O# C
. \0 H- I( I% dyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel* ~/ j* _( }; E% J+ Z5 B
二.启用standalone模块并编译
8 O4 }5 B" [' o. p: b$ @! m$ v7 G8 |9 u' u1 c, t9 |# q
下载modsecurity for nginx 解压,进入解压后目录执行:
n! a8 E" Y! O' t7 l8 z
r0 A- j9 h9 Y+ p7 `% u./autogen.sh
( ~6 H7 V g0 c* {. ?& r" h./configure --enable-standalone-module --disable-mlogc" O9 j6 o; R4 `
make 4 f2 p8 ~9 ~! L0 A
三.nginx添加modsecurity模块
3 A% v) v, c/ [. B. Y- I" T6 f8 C: d0 Y3 o2 R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ k. o1 v% _, m5 [8 X
; F1 X2 c3 a( f' x1 g8 {./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
& w0 m! g) b g! A2 pmake && make install) G' W9 [, Q8 b: ^
四.添加规则
8 b4 I! b! z; Q2 @" r
+ l( \" o. Z( F, @5 D& Dmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
0 x: F0 A" Q4 v. x2 @" D
# K8 o r, W+ g/ Z* T3 g0 a& i/ Z1.下载OWASP规则:
" ]9 }. C) ?6 n6 k9 }) W1 V# v+ J# A8 U6 L# h
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 l5 A3 w9 m A2 t, \
, }! E6 u) B/ B. B. _/ I4 Dmv owasp-modsecurity-crs /opt/tengine/conf/8 R, a; `! |' Q8 \3 w3 x9 p
N, O ~7 s/ j! x- g8 v" V5 D
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf: A; f9 L) ^* ]6 r% q
2.启用OWASP规则:
; N; L: s7 C+ U" j2 h
" Y5 R$ f. R) J. Q复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。! C# ~/ Q) Y; U( x% `7 J
& \8 M) M8 R5 a8 i编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
' [% X% n" F. q3 o; h6 X" Y& w( G% l$ t+ e2 u
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 g0 _# s" w2 ~
. X8 Q6 E. P3 rInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 w) k9 P6 m! C! u$ `0 B
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
. l) u a- W( ?8 l$ yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
1 L. k; b l2 v+ m5 xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 {* u3 `/ z# k7 UInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
' C+ |# I# ?( A6 _' j% vInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
% H5 B8 c# S3 _, T0 rInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! ]: u0 Y1 A- W3 w2 G8 Z) H
五.配置nginx
- I6 N$ t7 N8 r# J/ F; w1 w3 s5 T/ Y8 h# H
在需要启用modsecurity的主机的location下面加入下面两行即可:
. D. T4 u0 B: w- D g# {& W' Q* \( q
ModSecurityEnabled on;
7 ] b+ T# q8 h1 D# j4 O$ v/ MModSecurityConfig modsecurity.conf;7 z/ B8 ^7 K1 m' A
下面是两个示例配置,php虚拟主机:
) R7 ?8 Y7 M& J" }# f: \$ t& L8 D; o
server {
8 a1 v; I6 k8 @& \ listen 80;/ W" |, ~3 z5 B! e0 M" X7 M$ F, U
server_name 52os.net www.52os.net;+ r5 R9 f* d5 B
' Z$ X$ m" i E. \% g7 U: e location ~ \.php$ {
, K* Q& r9 d% Z ModSecurityEnabled on;
2 N: M8 e% }3 M$ H/ n ModSecurityConfig modsecurity.conf;
" X1 C( M1 b1 I0 Q% J- B! ]0 |2 M) _6 Q8 i$ ^' W
root /web/wordpress; y9 I: H: _1 c& W# D: \: q; n
index index.php index.html index.htm;4 `' O: F7 W) t5 Y0 t. A1 A
: m! _1 \* i4 A3 g
fastcgi_pass 127.0.0.1:9000;6 { M0 H) t8 K+ c- \& \
fastcgi_index index.php;, D n L m% W* o
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;( m: N3 J C0 w) @5 J
include fastcgi_params;
9 Q- N) y8 J9 n, I }8 j% I; c/ M2 a0 u( }% R$ B! h
}
; W. I0 D, ^* R- ]9 [upstream负载均衡:
5 \( F# m! ~0 z" U$ y4 ^
, L! a9 P& x3 Y A4 Bupstream 52os.net {
; i, ?: b3 @) E3 V- z( N/ i6 i server 192.168.1.100:8080;
+ q" B8 i6 a, L$ \9 \" B4 V server 192.168.1.101:8080 backup;
% ]( t/ C% Q/ u}. {6 h' |9 h2 g3 d3 a% ^9 a ~
; c! p9 }% Z: {) u* y, |" K8 R& Q' }server {
9 _! A) _0 t% g& b3 u8 jlisten 80;
2 u% K7 S$ o: L, F# h& Y, Qserver_name 52os.net www.52os.net;
3 h7 o/ W' n2 \* ]* I
+ a" T4 L7 m G) @$ k- hlocation / {) Q: H/ e$ b4 x8 s1 U
ModSecurityEnabled on;
H, W% N0 r b# ]8 |% { ModSecurityConfig modsecurity.conf;
: A4 ]- k% t( k: v
+ E9 r$ d( C. _) | proxy_pass http://online;. K* [+ a/ M6 M+ O; |( }/ P* \* X
proxy_redirect off;7 |' ^3 V7 m6 i& o# y4 Y
proxy_set_header Host $host;- e7 J1 a+ n3 K2 S) Q& m/ [) |$ Y
proxy_set_header X-Real-IP $remote_addr;
5 [& ]" ^% P! P4 D proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;& T+ G( W0 j7 o' M! I' b
}
% t! \- Q; [0 n/ W4 u( u+ v' r4 Z* n% l3 @}
4 ~9 T1 {$ H& O2 O% s% g# y六.测试8 W* Q o' V5 v
5 l2 m4 ~! M* w' e8 i+ W
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
) W, h# W( Q/ p3 p4 V0 K) P$ }# N7 {/ C0 w
<?php
1 C3 P" b# {9 m phpinfo();
Z8 r+ o O+ M9 b7 M?>4 ]8 T' {/ K$ m- Y w3 Z4 E
在浏览器中访问:
0 h. {2 M% q( j$ } w8 @) e( _$ C
J1 Y: a5 N$ \$ e2 c, E/ ]http://www.52os.net/phpinfo.php?id=1 正常显示。
! G" G5 T9 h% ]+ U2 p3 L9 ~http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& G) [3 E( K! h: Y1 `, O) t% @
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 @) n& E( v8 L' W& z3 ?! V
说明sql注入和xss已经被过滤了
+ }: ?; H( f2 Z5 P/ {8 N
" } O3 M- k8 i9 w- N七、安装过程中排错
! E( s: N2 y) E" f0 T) a/ o6 B: A6 }/ n
1.缺少APXS会报错% a( H) F' P; l$ O
7 f' R8 R4 q1 @7 qconfigure: looking for Apache module support via DSO through APXS- w& `- A" S6 y# u: J0 W
configure: error: couldn't find APXS. g4 w/ X/ W& q( v' G3 P
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。' n. L v+ |7 S' m
解决方法:6 ]3 }' e K B4 f4 L) Q7 U- d& v
2 s3 [1 w9 v3 }* s- w/ G! O8 Wyum install httpd-devel
4 ~/ m4 v2 e; f& H. E+ x2.没有pcre' S8 o5 s/ n( F
7 j6 z! p# N0 q8 g
configure: *** pcre library not found.
0 K* s7 u& u% _! ]8 J6 w/ z6 rconfigure: error: pcre library is required
& q' D, E4 Q' Y" E解决方法:
' [( l5 c; y6 `3 e" Y/ X; z1 j* [6 U5 T( c! I0 m+ @1 C7 l, w
yum install pcre pcre-devel. g% J! }* O! J, l7 ]+ W7 F
3.没有libxml2. f4 {9 |/ f/ v* E1 b
4 S7 y8 y \* A' I. r; d q# X2 p
" C' g6 [3 X1 i- g5 l9 _configure: *** xml library not found.
4 D/ w g4 V& Z" Q) Y, a2 @" {3 a: rconfigure: error: libxml2 is required! c2 _- c! W/ y" n! T9 q
解决方法:
. y/ @# V: R' Q* G- b: g
, ?0 w' R# f$ b! _yum install libxml2 libxml2-devel/ L) w+ k' T" d6 i! ?
4.执行 /opt/tengine/sbin/nginx -m 时有警告
1 x" s5 ~5 t. r! | W! w" _& W; r1 h% ~- ~( f; n
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
& ^2 a; e# C% t' [) \- L: I" unginx: [warn] ModSecurity: Loaded APR do not match with compiled!
! r" V2 @0 { ~" Z2 i0 L) G: N, G9 p原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log f& Y( M* b9 s$ |* o2 L) m
( I0 W) p% p$ l0 N" r H9 O# g
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
I; N+ N* H" B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"- P# X$ i& @" m, M" ^& y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!; T U+ U8 h# y1 I& U" ^7 S' c
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"; ` `! e7 p1 f- k8 I$ V7 ]1 \
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"4 `7 \- Z! q/ |4 ?
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., Q/ B# R0 F6 P. j: ^
解决方法,移除低版本的APR (1.3.9)3 K% H0 y9 v( u, n1 k; u
4 a' N/ R* `6 q8 ^) Y; e7 h$ ?
yum remove apr
8 P4 u5 m7 N, i$ @) r' O5 S+ q6 k5.Error.log中有: Audit log: Failed to lock global mutex
' [8 e" i6 Y% \9 y* ?/ i7 { I
1 b0 Y3 w; a: R) G: b/ t2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
7 S0 W B- [# t% fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
5 w. M6 Y5 b. Y$ }解决方法:1 ]+ v, V( o, l2 U0 T
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% b( ?7 M d1 c/ A2 u3 w$ u% _. Y$ L
$ Z4 E+ A" W, U, f8 O" }SecAuditLogDirMode 0777
$ w! s2 i3 w9 Z* ?! YSecAuditLogFileMode 0550! v! M5 h! q. ^0 A) C
SecAuditLogStorageDir /var/log/modsecurity: }% X7 ^5 Z& L: z- s) c0 C; U
SecAuditLogType Concurrent
6 T* |( ~- g. n5 p+ O/ ^0 o, b参考文章:
7 B ~9 w1 T( |8 L0 D; ~https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX3 B6 d; i( Q+ P8 p9 K* E
http://drops.wooyun.org/tips/2614 |
|