|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。# l0 y& i. l6 D( ?
. M1 e, ~7 T8 ^' x; f. h一.准备工作7 k6 s9 A, A+ F# Z% c+ o! z
5 | d9 c/ ~- v5 [. j0 d ~
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 z" a, P7 r' S/ q
4 s$ w2 m& F$ J4 L0 \/ G9 Ftengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz; ^" g5 u& M/ M; Z! A( w
& l* ]5 H s7 Q3 r( \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz; d! ^- X. |6 _- L) Y- [% @
6 l9 h7 ^/ g M5 \, s
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs/ F$ r# A& J; o' ?
7 B# o+ [3 I v# m( p
依赖关系:- F7 c: v9 x2 q% m1 s4 R
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
) {2 h* v/ {* S* v `" m; X5 `# s4 L! |; g. T# n; T
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
1 r* R$ }$ O$ |1 Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr& D( e% O: N4 v; X: l6 C
7 [- |) y, J9 h& M' w$ y6 N
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel9 }' R! @) }# { |
二.启用standalone模块并编译
. s9 b5 o6 n" W5 {
" J' E5 R" S) D( B' l1 L8 q7 @- S下载modsecurity for nginx 解压,进入解压后目录执行:
3 z6 z; W, I. m2 t" M p4 C/ t: [; U! V7 t- ]( R
./autogen.sh: F$ K2 M' J- \( g
./configure --enable-standalone-module --disable-mlogc
/ N1 f: z1 a. Vmake
& W4 B& H! i# s' R0 D7 e三.nginx添加modsecurity模块
" y, ^+ y9 R( d: Y& F+ r; x
2 N: t3 [+ a* d+ q3 j在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& { p9 m% f9 C- m) M- q' O; W
g- O; H' e% D3 o- i/ i./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
# k& E) U8 q3 ~2 {9 R9 c7 \make && make install
. r V/ a6 z6 q6 w4 e+ h- B, m# [四.添加规则9 j$ A" i% h% f L. ^8 R
# J' n9 n$ u5 w1 N1 V0 x
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ O6 o( ~5 p# g3 v
" N/ @1 ?6 D F) n1 G
1.下载OWASP规则:
! ^1 K! C4 ^8 G" @. e+ R
! z3 R& _* n- }" Dgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs1 J8 g& b5 m1 v* h S) r& e2 q
! |0 W: y7 x: s. Z2 x
mv owasp-modsecurity-crs /opt/tengine/conf/3 T: b2 d# ?' u
2 B$ r' J+ Z$ w) P
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf0 A3 L- I' o) C4 }( e
2.启用OWASP规则:
3 k! N" A% _! S! E3 V; y; p- R% Q2 d0 ?1 r5 n; M
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。% B' d& u* D" F: s7 \, @' Y1 N; l
/ Z' `; h3 V$ d1 I4 e2 M编辑modsecurity.conf 文件,将SecRuleEngine设置为 on* ` P5 b; l, x9 Z p: A& G' N; T
3 k' ?# T# U6 W- Z) uowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
9 R. W/ N+ a. R# d# ^+ N) Q0 X, w$ |0 i- `+ i
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
& ^; K' m$ {: }1 K6 a% lInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf3 e/ y; f) L7 q. W; O1 Y: |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ Y$ W" u6 K" G, V# M6 h% s7 r, U
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
{/ U9 h; v+ lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
8 O& W1 C- g: e7 \& `/ t+ ^Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
- P( V r5 L8 a& dInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf. Y9 U+ V: h) R/ h
五.配置nginx/ ~' E1 i4 I$ ~& n
* |- b. n- ]* y% n( {9 Z, {在需要启用modsecurity的主机的location下面加入下面两行即可:: f ` \! _* }* y" V- I* j) M+ [
' E g0 N+ V9 f- p& @. j( Z
ModSecurityEnabled on;
5 x( k; V! g/ v# g* L$ ]' aModSecurityConfig modsecurity.conf;7 [3 m8 m& P7 m
下面是两个示例配置,php虚拟主机:
* }; N0 k+ w3 @* C# E, Q3 u% ^: a7 ]+ ?) ` ^2 l& u: L
server { p$ ]- E6 g% m: s5 ]
listen 80;+ ?; |8 t. y- x3 d4 ?
server_name 52os.net www.52os.net;9 K" _1 `8 N8 p4 y" i+ i( [: D
+ i* n8 }! m( [& [7 A* ^+ v2 D
location ~ \.php$ {9 K! L% ~5 ?+ O5 o, }. `
ModSecurityEnabled on;
0 Q% Z4 U1 d' P7 P ModSecurityConfig modsecurity.conf;4 `6 B" R- v' U+ D
+ ~. D& p* F* E% }+ }, b& ^ root /web/wordpress;
, I9 m& N5 [* v* d$ d$ p index index.php index.html index.htm;9 }$ c: Z, b4 C* [
/ ~! Q3 w, q' n y8 v8 _1 E: S y
fastcgi_pass 127.0.0.1:9000;
. z1 A9 `4 M7 N! h5 x fastcgi_index index.php;7 A0 P F, v! l! y, }0 p/ `/ C. M
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name; B8 E0 i4 e# o ^
include fastcgi_params;5 _' }( ]/ B1 t& o+ _$ Z3 P2 {- J
}% O% S& q! ]+ H# ^7 y
}
# S+ M. C) X7 `, j4 X- Dupstream负载均衡:
8 E$ d f% K+ `* n8 ?
5 P' j# O* L \upstream 52os.net {
* F# z; S6 Y a9 w. i4 f1 G server 192.168.1.100:8080;; G8 G( v6 ] p7 j0 Y
server 192.168.1.101:8080 backup;* U; D- q( Y7 ]' ^7 T& f( ^( w6 i0 U
}
8 z' V2 r3 C) l) ]- S
8 @8 q' T' Q6 t7 y! a2 o) Bserver {
- G1 P C+ E# G+ v2 q1 @1 ylisten 80;
, V. g' M. `9 T( P; x8 Pserver_name 52os.net www.52os.net;- U5 C/ q5 D* l7 ?# d
7 Q! ]. H. N: _location / {. }9 w. v. V' |% ]! u9 h) i3 U
ModSecurityEnabled on; * f! R$ h5 a# Y0 ] ^; q
ModSecurityConfig modsecurity.conf; ! p9 l% \* N3 A" v2 }
; d9 h- {0 ?/ H% l. D proxy_pass http://online;/ z, ]' i# H& H
proxy_redirect off;9 {: X% P0 z3 V3 s) B( k( Y+ @
proxy_set_header Host $host;; a+ o2 |2 ^) O0 c5 D- U+ u
proxy_set_header X-Real-IP $remote_addr;
. F [" w6 C( x proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8 H; E% E- G1 f% E4 @7 m$ [3 K$ x e }) F& W, ~2 S! f* ?
}
( ^' n- y# j @; K; [% o8 T) K六.测试- C: K- |' q& l( z [' x
) t% D" z; x5 G. B5 T- S我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
" u- m& s! J, q0 q5 V
0 q) {! ~- d6 }0 L<?php% j( }- [" Y' E, u8 }$ ]
phpinfo(); - s: O* K7 B1 k1 N# E3 \' V
?>
1 F( K% _+ y* M5 U( [在浏览器中访问:
5 Q% ]: }$ E) g/ q% e3 p' Y- @6 R$ w, O0 C
http://www.52os.net/phpinfo.php?id=1 正常显示。, u3 X2 q* P9 C) a! @" S/ E
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
4 @" }2 G3 i/ S* shttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. M5 y. P: D7 B( q
说明sql注入和xss已经被过滤了1 N1 ~4 f: ~4 x, ~( w
. E5 B2 I- k5 Z$ ^' N) X1 O七、安装过程中排错$ o8 V/ C! }# l4 c; q/ ?: a/ i
e2 m% C/ B3 J+ X# f% m1.缺少APXS会报错
( ~! S5 [+ h: X0 S
7 I; _; t8 q& J/ mconfigure: looking for Apache module support via DSO through APXS
% a& }* l4 G5 `# U5 N6 X Dconfigure: error: couldn't find APXS
7 W. O, P8 U# s+ m3 g. Q/ uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。7 d+ G& L" z7 s. k5 d2 _
解决方法:
7 M) t" L2 J' R% I! T& s( @5 l
, i/ z- v) p1 b% Lyum install httpd-devel
Z4 \% g" n$ `9 U5 i2.没有pcre. [9 u0 Z5 C' |1 u% v9 W2 w: p1 s5 `1 e ]
, D* f9 g+ G, e& \configure: *** pcre library not found.
7 X6 j. \0 r! _; t3 P+ {* |1 ^5 Econfigure: error: pcre library is required
/ v7 a9 i/ @ s ? A1 L解决方法:
& H, Y, r+ f5 c: m. H& m$ T& z6 ^$ i D* I
yum install pcre pcre-devel8 O) T2 |9 T- k8 @2 n6 N
3.没有libxml2, d' b [, L' l# n, W
4 f" j6 l& r1 r5 h9 q$ d: K6 j- \7 i( S9 T$ @4 _
configure: *** xml library not found./ e& Y0 s$ G9 X
configure: error: libxml2 is required* v9 |, p5 W5 Y! j! S+ o' e: M. M
解决方法:
3 e( p" V3 G% S8 L3 a* v9 F) v( m& z' l" w6 r5 h0 L* S* }
yum install libxml2 libxml2-devel
4 T0 ]% z' ~1 L, W+ _! M4.执行 /opt/tengine/sbin/nginx -m 时有警告) {" E. h$ s+ L c- P
" J/ {% Z6 y3 v
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
' Y5 `$ ]% H. B" R, \4 d. P0 gnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# O: B! z- F. v8 y" W I原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log! _) Y7 Z s. `2 ~6 t* x
& Z7 ~( [2 c* _ }
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
M/ A I- V1 v" H6 x$ p8 I2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"; j5 Y! @. y# q( E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
0 Z- i0 `- w8 s8 ]/ D# \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
# E2 `6 z% ?0 y G7 ?) }$ R1 T2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") B& d s' A' |: L) _" \
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.1 x( G) @+ n/ D- C# K
解决方法,移除低版本的APR (1.3.9)
" P! t8 O8 N: Y$ s* D# w7 P% ~# ?- q8 F0 D; j
yum remove apr b# C# q; e4 p4 I' @; k
5.Error.log中有: Audit log: Failed to lock global mutex
& l8 r0 l! ^5 b+ ^: b& D; D+ Z
+ Q/ }9 _2 q$ g. U y m( G2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
, J4 S2 L; t1 U# K" N8 Hglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]- K; D% B9 @( o6 e' ]+ N
解决方法:
4 K7 s0 I) S8 Z Q% _5 Y6 Q- H编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:: b+ W2 l( O9 \( _7 J+ d+ D
6 z0 k4 Q/ X: QSecAuditLogDirMode 0777* v3 C) v8 |# B3 ~3 _) g. @
SecAuditLogFileMode 0550
, w& a }. [; `. V& [2 U! }7 ISecAuditLogStorageDir /var/log/modsecurity
- O+ K7 M; Z9 W, A& W. sSecAuditLogType Concurrent) b2 v6 T* R2 r; \2 l: A
参考文章:
" F8 Q- p7 F. p4 e( Yhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX, U: _+ |, Z8 Z
http://drops.wooyun.org/tips/2614 |
|