|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
# A, i8 P; L5 z9 a9 }
2 p6 X& V+ V5 U: W* N. d一.准备工作
$ @6 @& t( e* Z# U
, ?9 I; j% ?( c5 q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.08 ?/ n% ]: ~& b% l2 {( i1 o, U
% T1 ~$ ~. ?/ G6 @" G/ r8 y+ `
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz- y+ D4 m# L4 @0 ]
6 j2 k+ C! ^1 }
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz4 D2 D! c& D9 F" } m
4 z; N/ k+ C+ n' L. x( ?
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
9 v7 c: X2 u3 ^' v4 e' [+ L" v( ~$ j/ q* E! u7 u* z6 j
依赖关系:, V3 K+ p0 [. U7 j
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
( T/ @6 G* e n( h7 P) i6 |
~$ n$ E5 v* |: A7 P1 o0 i3 t' Uyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
Q2 p+ F* [4 m6 l" b3 Q: H; y# j8 jmodsecurty依赖的包:pcre httpd-devel libxml2 apr
. b- x7 I& x* _6 v: z/ h" { v2 X* v( d2 C
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
: h( Z( F$ _" e) V% N9 q# @8 d; F二.启用standalone模块并编译$ h, _$ w& R& l; l! D
5 V0 `- k4 }0 B) Q/ z) P
下载modsecurity for nginx 解压,进入解压后目录执行:' P- Z+ `, q$ B8 G
( G8 g, \! y$ e9 y& S
./autogen.sh
3 R- y' ~( y1 ? u0 x3 x) I./configure --enable-standalone-module --disable-mlogc
, I$ X* i8 Y4 ~2 {make
0 Q0 G7 ~% ]' }5 _8 D; d# l3 E三.nginx添加modsecurity模块2 i0 g6 [5 N w& x3 q q
0 S! i" X+ D9 H7 d) y7 i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 y4 H. Z0 r0 o( Z( ?9 H
8 q! f _+ g6 N" Z8 ]./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
0 M; Q3 q5 Y4 s& t/ Bmake && make install8 r' I/ U2 K5 @2 k5 t: E
四.添加规则3 s" ]3 L& h/ N! Y1 P; ]8 z
% I, C0 T# B9 o7 @
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
9 F ]1 b6 x) Y2 a! m) L. a% j, h0 v- L! Y( p9 W
1.下载OWASP规则:
3 B% u6 x1 y: k# w- B& G R3 Y! p& f. `) p& _9 ~- F$ C3 h1 b9 z
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
! h3 r6 N# J" ?8 Y. @0 B9 f. t! d4 a
mv owasp-modsecurity-crs /opt/tengine/conf/- J; c( y. j g
% k5 x0 ^' Z' T3 e* K8 k. vcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! H2 r% W; I; q# W* P4 I
2.启用OWASP规则:" D) C+ D# H! O( Q. n8 _/ l8 P4 l3 I
0 z" f$ z p0 ?" A7 T# z复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。4 B" a% u+ K% X" S
5 G6 V, v! e6 {+ c/ `7 X! H编辑modsecurity.conf 文件,将SecRuleEngine设置为 on, Q$ u: C& M @4 `8 _
' Y- n% x! i7 V& [" N6 [owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- U- s1 w+ H) y& {5 Y5 ?
1 w' K1 c$ J( @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf/ ]7 {1 L3 |+ P
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf `0 O* a( x( Z' R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ W+ N4 h3 ?$ [( m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf2 Z, i) \: \5 d) M( T: u
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! I$ W# u9 H+ f: |- X y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf9 O' I/ p; i. [0 [3 t
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf- k+ ?) `/ {3 C( k
五.配置nginx& I$ Y) i% s8 U3 h5 S6 D: B
6 F4 ~) ^/ d# M7 M% k [在需要启用modsecurity的主机的location下面加入下面两行即可:
+ K7 k, f& ] S& S% P1 @, Y- D: ~4 L6 J; \
ModSecurityEnabled on; ( S7 V! U* G5 L7 [( d$ H- ^1 [
ModSecurityConfig modsecurity.conf;1 U& B" }. T5 w7 b+ ]: y
下面是两个示例配置,php虚拟主机:
2 a5 `; e4 b8 i7 x, O7 l0 ^3 j% o
server {4 H# N* ^2 w1 t- z( u2 {$ g, B' |
listen 80;
/ H, m8 I$ R9 T$ R, Z0 j server_name 52os.net www.52os.net;
; D0 X4 u/ U5 s4 `" g9 y5 Z! a
# k1 q; v5 T6 i" H3 P& e location ~ \.php$ {
& B& ~4 I; w$ p) D1 v% A: y: ^( P ModSecurityEnabled on;
( H8 v/ r, j$ [, N, J3 r7 a ModSecurityConfig modsecurity.conf;6 o# J& N1 Y7 R. W8 m5 P
' z! c' Q/ N3 u4 |9 E3 D
root /web/wordpress;
0 _! D% G u/ q" F9 a& `9 k index index.php index.html index.htm;
, z+ n" t' x: p& c, R `2 u# J+ q5 s7 n+ m+ Y
fastcgi_pass 127.0.0.1:9000;
( e# W, m( t6 _% T fastcgi_index index.php;
8 q4 l# d. K0 I. i1 { fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;/ b! E# J. R p8 h! U' k+ S
include fastcgi_params;0 n5 g' i# l, m, J6 X: P- Y
}
3 L9 ]1 F$ U* B7 N. V' u! p }
7 I1 n+ }2 G- G/ C6 N! R: X( aupstream负载均衡:
9 Z" b5 @1 x7 W9 b# ?% m5 {7 `) z8 `( v
upstream 52os.net {! Z2 p& e0 g5 ]4 X0 l7 R2 a) b
server 192.168.1.100:8080;
^7 X# P5 S- c% p) c6 k server 192.168.1.101:8080 backup;8 W. t4 ]. J4 J& `
}
& ]2 z% [) j' |' e, b
9 ?; F$ H$ }* U v# M5 X5 cserver {7 W! l" \/ q& g- P) u0 \ B3 C
listen 80;
8 @8 }% d3 a* W# [, a' Qserver_name 52os.net www.52os.net;# K5 ]: c/ z2 U* G( m2 y4 M, M
) a) u& H3 k# e% A! x9 C. Y0 ulocation / {5 j% g. q3 H F; L m6 N
ModSecurityEnabled on;
. U; \0 r" U2 E& K* S ModSecurityConfig modsecurity.conf;
3 ?" n5 I2 e/ p$ j$ F, v! \- o) c. V& k' U! Q
proxy_pass http://online;* [" s* h; Q, u3 X: \) u" c7 o
proxy_redirect off;
7 K; Y0 H r s J; V proxy_set_header Host $host; S- q3 M; v! W9 v- {
proxy_set_header X-Real-IP $remote_addr;6 i9 l2 G* T# P- p" v' f0 P: ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8 } X+ Q. I, V3 i* | }7 x9 t0 Z. U, y+ h7 k( t L( @ W7 N
}0 E0 U2 ? w1 ?# `) U# X
六.测试
1 s; Z& r t0 {( P h5 V/ }# s( r9 S5 Q* o l. @/ n
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:, J& \1 R2 }- {' K
- ~* H& q" n' P, a$ ~" Q
<?php
& ?% y9 _; L4 R" _ phpinfo(); # C( Q x2 t/ z. V E
?>
. W& |# A, c6 c/ s在浏览器中访问:
. T: g0 q( G( p/ O% x) ]2 Y5 O& a; M3 N: u% x4 w
http://www.52os.net/phpinfo.php?id=1 正常显示。5 | m9 a7 d1 z' m N7 c B
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
; T; L1 ?- N$ [# h+ i6 G4 {3 q9 b: d# thttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
9 {/ n* K6 t' A" y: c说明sql注入和xss已经被过滤了
: L n$ [* q& `" M d- l" [5 r' a2 ?: ?6 e8 [
七、安装过程中排错, V9 c" J' N$ _2 j u' b
4 h- V" w) }' W3 n
1.缺少APXS会报错+ P J7 X! [9 i" X# [) a; K% q
) i1 F! j% Z5 {+ r' J. s0 Fconfigure: looking for Apache module support via DSO through APXS
, x, S5 f" G; O' T3 F+ s H1 `5 Dconfigure: error: couldn't find APXS* r% R7 F: K0 L: w8 B
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。; d# u4 E( z( w% j
解决方法:6 R, J/ r. u5 d! m$ j" l+ x
, q3 } C) S9 E. dyum install httpd-devel, ~( T& s* h) ~# d: H* X
2.没有pcre% M3 h# K0 w) s4 V. Z# c% P
9 w( `6 j% Z. q
configure: *** pcre library not found.# v: M7 j/ H( e" n( ?0 M$ u; {
configure: error: pcre library is required$ R% S$ J# B7 S# p
解决方法:( p4 ?, L3 A, }! F0 y. @
' T8 w" ` {# ~! N
yum install pcre pcre-devel
" q# c0 F6 G/ C( E3.没有libxml21 u- _% p& m3 d0 l) m! Y! B- q+ L% T2 x
- J2 X$ O' G# U' a( w* t6 ]: j5 H5 K5 k' `: O! b: B
configure: *** xml library not found.
# z4 n6 x+ Q4 ^' h2 J. aconfigure: error: libxml2 is required7 |) {3 r, H, X" l& e
解决方法:
+ B/ E2 L; f4 S* Y0 K8 K9 A& ]) {; \) Q4 I
yum install libxml2 libxml2-devel
8 _3 X3 q9 w0 Q* ^1 s, _4.执行 /opt/tengine/sbin/nginx -m 时有警告 R$ M9 T9 e$ y3 ^3 Z: W+ [ J. q
% ^1 r O3 B0 Y6 O4 O
Tengine version: Tengine/2.1.0 (nginx/1.6.2)8 f' {& { Q) J9 K, U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! }1 @, M B4 ~, M
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log8 g1 ?+ [6 B- D \/ k) ]. j
! h! V! v4 I3 u' G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 n- g5 r/ \1 z% G d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
W; r* a2 m5 a0 f) N2 H5 B" Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!" c. n4 Q: w4 }2 t3 R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
. h9 [/ r. o5 |* \3 w8 D2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% e4 j* s/ v0 d) K0 u
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
. o1 _- }7 Y t" Z解决方法,移除低版本的APR (1.3.9)
5 A& |( P3 o8 c; c! O
% M% Q4 h1 q; J: S7 Q3 syum remove apr
$ |6 }' `7 M" S1 V, q, }" j5.Error.log中有: Audit log: Failed to lock global mutex0 F; E& s5 q: G) D2 U9 E2 N# _
8 {3 K7 w$ S4 V M* g
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock s% a& [4 i$ Y. ^% \( `
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
" e4 ?& Z; r5 U; O- i- z0 c解决方法:
5 O, W+ j N1 I0 B) a, i9 o编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:0 o3 ]& R% s! g
: {$ \1 ^! q; d
SecAuditLogDirMode 0777
+ q6 |" d1 R, w0 pSecAuditLogFileMode 0550
" ~1 L7 `/ T. t+ y3 `* z" d2 BSecAuditLogStorageDir /var/log/modsecurity/ o5 j7 S3 D, c4 \" f
SecAuditLogType Concurrent
# N! d0 X/ L k. L( B; s0 z! M参考文章:$ W7 v6 q& x7 y1 j
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( E; Y7 F) @5 w2 c- [4 E5 M
http://drops.wooyun.org/tips/2614 |
|