|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 t$ p* [) j8 n1 O9 n( z! Z3 Y
7 M& H2 R! L7 ~: s
一.准备工作1 O. @& v) j, j. M
4 N7 p! W) k" Z( Z
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 Z. Y& Z$ i; M) _9 B/ t
: u$ q4 h: X l' }" ]0 _# w" g1 [& stengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz! S' |% ?2 q) `; o
8 [: E [+ O" s9 a" J/ S8 q8 D
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 S6 l% {0 c0 c. K3 {
$ \$ P. j6 v* j& X9 A4 nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs$ `7 n7 e* i) g8 X8 C* ?4 `
& N8 A( s( Q G4 l9 N+ Z2 K
依赖关系:' l' D" r% H5 X8 v# W7 ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 E# F/ [1 @. [
) V: ~, a; O/ }, O: Y. J5 Oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel8 F- r: Z+ e8 O' E
modsecurty依赖的包:pcre httpd-devel libxml2 apr" I9 V6 m0 N1 ~- E( K0 k
4 m( ]7 c. D0 j3 byum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
$ e% B, x8 ]5 F; J' y二.启用standalone模块并编译
2 K6 k/ q; n6 y: O+ }' b8 O1 T. `+ s: I) [
下载modsecurity for nginx 解压,进入解压后目录执行:
' a9 J+ M, z& K% v
$ s K! x+ F6 I. i3 D./autogen.sh
% Z% ~, X0 i( S. t- q./configure --enable-standalone-module --disable-mlogc
3 x! U$ J1 d4 Y( U- w0 ^2 D* d2 ~make ( |. m/ P9 w! Q5 Z* w: W% A
三.nginx添加modsecurity模块
( t% n& u, n& \( X6 T* M v" L1 j8 I0 o
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:0 p- G0 b6 a% M' I3 ~
+ O3 w3 C2 W4 J& M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% B- o5 D; Q8 S8 b
make && make install
3 V+ L+ z5 J1 A) n四.添加规则
' T! N+ B) v$ g P4 B' Y* U" `2 C1 @) S/ {2 h
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。% T7 T' H4 s" j$ t) g' }7 j; L( q7 X9 x
- ?7 d1 ~; ^! l1.下载OWASP规则:* v! t% c) O9 R( R
n Q5 \3 R. Z5 `
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs7 i7 W9 p; e& |0 ^: \$ X
. ^) \3 ^7 q' l9 S3 }# m9 p3 F0 U
mv owasp-modsecurity-crs /opt/tengine/conf/
# s) ?& O0 r( D2 X
5 E6 _0 I: y, j; Q' J, S- ^cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" E9 K; T8 Y/ u& I' ?) I! b! l2.启用OWASP规则:
. N) z3 c, `3 r( B6 N# m8 D4 s8 K# p" P1 [3 }
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 x1 K" v8 e2 K) R
" ]+ Q! z' q2 {5 X
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
" q5 F+ b& q, y& T7 m+ ]- Z: d) J* q6 ^" e! F
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: v _- y% }7 ]' r
0 H+ p6 v7 R0 P, I$ XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf. r6 Y$ f* |, o* h' Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, V8 C5 U9 [7 \, \# y( K2 mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
2 `, N- L/ c8 q/ }- Q* D6 R3 AInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 [# m& |4 N9 O0 P" f1 g# m$ s" n
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- M9 p& N; w' z5 [2 m1 L3 w1 yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf4 G& F/ f, s7 a2 D
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
1 r% g; i6 y% h& d6 X, s5 X% X五.配置nginx) @9 t4 s8 u- Q1 `1 ~
& h# @$ X/ T* T在需要启用modsecurity的主机的location下面加入下面两行即可:
* J' F. d: C2 r7 e+ `
. X% B8 P0 \9 K% g4 _, f zModSecurityEnabled on; 7 A* x( {. L+ _, y( K: Q( z
ModSecurityConfig modsecurity.conf;
% |. H: W2 V8 @, c |3 V下面是两个示例配置,php虚拟主机:3 n" I- |' n8 Y3 j
' l6 \* x" w! d' X! r0 H9 m- mserver {
" m9 B- N6 F/ U+ M listen 80;
, x8 _$ `& ]9 q, o4 ^ server_name 52os.net www.52os.net;& @8 P' f5 Z- h/ p0 ~
0 o' J- Q/ G' ]: O
location ~ \.php$ {
' {& d) |0 d$ e1 [# G ModSecurityEnabled on; / _* ?- }/ F# \' g( ^
ModSecurityConfig modsecurity.conf;: U f2 v8 z/ E
& \( z3 `% ]2 @* k3 K) E b9 h root /web/wordpress;1 |) \/ C* I E
index index.php index.html index.htm;
3 P2 v9 n+ `* r! v2 K( @
" R. ?# H7 ?9 w6 Z fastcgi_pass 127.0.0.1:9000;" M- l! _" Q0 _& |) E9 n
fastcgi_index index.php;
: H: E; a, D; A# o- y2 V0 { fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 V1 V% i9 J, j/ m6 V r8 B% E4 U include fastcgi_params;
; k5 `5 h2 V2 u5 J }
8 I5 ?, e6 l: q3 V }
) u( o2 H4 v8 G+ G! l( L: }9 i0 \upstream负载均衡:5 \1 `6 G% w& f( [
( T1 h) H) d- s' T" fupstream 52os.net {- j) H; }; J) V: N N; ?% y. f1 l, Y7 F
server 192.168.1.100:8080;
5 p; s/ d- e" A& S1 J8 d0 x server 192.168.1.101:8080 backup;
: E! J- ~: H' p4 ^+ ]}
- o+ {2 @* s, M; Q- x7 F! _1 b8 y$ L
server {
! |, g1 d! ^- w: S2 E$ O( flisten 80;
5 T" s1 ^* b" f h3 Y9 g/ H% Eserver_name 52os.net www.52os.net;3 ^" m5 h/ J7 X: E
8 f! g5 P4 d2 qlocation / {
+ w; |1 D, T- E9 V% u9 `; ^ ModSecurityEnabled on;
# ~: Y! h# m, F2 |& M ModSecurityConfig modsecurity.conf; + u4 \8 I, k0 ^: [
7 \* O% N: P8 L! e! H2 C6 m proxy_pass http://online;
' r6 C$ w. x8 T2 Z3 W( \3 h* P proxy_redirect off;
& y2 V. r" @/ {% ]& j" W' L, n* N proxy_set_header Host $host;
" Y5 U2 p ~5 H8 E* `3 z% Y) c( | proxy_set_header X-Real-IP $remote_addr;
+ n7 @+ r$ j. c2 y proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
3 b+ B# z; F, F( Y X! r1 n }
: A6 y$ q2 E1 S4 J3 q/ A}
! n. v5 C- g% x& e' c六.测试, z4 \6 h0 w8 W( n1 O
( f; Y9 B* u# \: _( O我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& z3 L/ N3 M5 g& X' m6 h
& t: H8 k* L! T! `; V/ B
<?php
" w+ i1 }) Z- y phpinfo(); . w" k' n: J3 \- P
?>4 a2 T& k$ h/ K. L0 Q; ?0 t' N/ l
在浏览器中访问:) j% ?3 R0 }" i
" [' S9 Y0 {$ R- n H1 Shttp://www.52os.net/phpinfo.php?id=1 正常显示。
, W4 `, b: E; ihttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
7 k+ L7 y1 a4 r( v* i7 m- Q# _http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
8 G5 N1 f* r1 H说明sql注入和xss已经被过滤了4 W6 u7 n0 u4 ?( C" L8 ?
" ], i- e* N3 c% w- a' |+ S6 K2 s
七、安装过程中排错
$ S. U; m7 R+ s- s* V
* H. k; e# r- b4 ~1.缺少APXS会报错
2 }) Q3 G0 [3 z' L* `, u s6 t C
- M; p8 ~" E- h1 J/ wconfigure: looking for Apache module support via DSO through APXS5 {7 b; n( l' U" x
configure: error: couldn't find APXS
+ b# R& ]5 y1 L" p0 o japxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。2 s: n# C ]2 P: ^8 s/ h$ U
解决方法:
3 d- i* F2 c$ o2 t& Z q( B8 {3 x
yum install httpd-devel% W3 \* D# }2 m0 |/ w* W! I- b& k
2.没有pcre1 Q% E1 D0 m" f3 K
0 @7 G8 f& K: E8 C, Q6 R( S! o
configure: *** pcre library not found.
; N0 D" d( b* P8 r9 D% v: s5 w8 nconfigure: error: pcre library is required: E" K: F" u0 p' S, p* \
解决方法:- H; l- o8 o$ s; z9 R6 X0 Q
7 j& |+ i/ O1 u' ^& m) E( I( {7 dyum install pcre pcre-devel
6 \& e" m6 n6 l6 e6 _3.没有libxml2* I( O" q0 s- ~4 G' i1 Q
0 ?: a# J* i: T' @3 {1 x+ a, U& O; ~( M7 ?; C( I& \/ r5 H
configure: *** xml library not found.) s& }- o, Y! O7 M; b8 u
configure: error: libxml2 is required
- Q; X9 \. ]. W+ Y% D- t解决方法:
2 Q5 s$ ~7 n, J& w2 j* X* b& L9 Z/ \: T
yum install libxml2 libxml2-devel
3 J/ H6 H1 j, q* R1 R( g9 C4.执行 /opt/tengine/sbin/nginx -m 时有警告
% ^2 j# ]; m3 `) }3 J2 F6 f& n, w
& K! B. P) U, x+ r" V: STengine version: Tengine/2.1.0 (nginx/1.6.2)( A# t# g5 b$ D6 Y- }! k8 I
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!1 Y6 _ r$ h H: F
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
/ b- I! ^, F2 d, n1 A) r7 D" g' l: k# R: K- U5 _1 u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
8 N) k# A H. `" y- x5 n- b# \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
$ u7 l3 n1 x8 O m9 p8 O" X2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!* S' d; p% l" H% }. e7 }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"# G( o) p+ Y9 h6 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 q: v% c" k/ M4 d$ o2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
- e8 v* E8 r( {9 ?8 H, Y解决方法,移除低版本的APR (1.3.9)
6 `# `# c. _! p: a, i" r# n: x4 p( N
yum remove apr
. B9 p" l2 U Q5 |2 g# z, b1 e5.Error.log中有: Audit log: Failed to lock global mutex( |2 |/ H0 I! P& m0 }* I$ A0 Y; a
: t6 s6 `3 y" K5 X9 M% g6 ^
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
# y- H, P% X& R1 w$ S$ pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]+ s/ L/ V; ^3 G! \/ L
解决方法:
1 U2 b1 \ o; j% R, w0 Y8 T) M; S编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ w+ W; s9 g# q$ \
0 D) z3 t# y. T- D+ HSecAuditLogDirMode 07779 u3 P& j9 i5 u/ D: C! K O
SecAuditLogFileMode 0550# l6 d" V# a/ ] a
SecAuditLogStorageDir /var/log/modsecurity
" O$ _! T4 D8 }" u, k, ySecAuditLogType Concurrent. y6 Z }% r, k0 O" {; q& q9 e+ z
参考文章:
# ?% P; O$ d( W; b) V* c3 nhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
; f$ n( s" L) zhttp://drops.wooyun.org/tips/2614 |
|