|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 @' U/ k( p3 y
0 T: S3 q8 D6 h. ?' @' Z1 I8 J8 q一.准备工作2 t' ?3 p" e/ O# O0 f
' z& `+ b% C' C6 e0 q系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
; P5 B! D8 f P" c! t2 [2 y2 \+ O: d4 Z; q6 r) X
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz* W, P: K0 ^) E$ B2 K% v
/ |4 A9 k. x5 \0 |$ Nmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz, r7 `& F* m1 A
( D8 S9 `& b8 s G! AOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# |& [3 W0 _/ G/ ]8 i& F
- G1 H0 o7 t0 `% D7 m8 w" U$ g依赖关系:
+ n$ C9 R+ G9 n0 M itengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:+ o8 i! N9 l M4 j7 x$ j& i
9 d2 F# R5 u$ m6 Y6 u+ I
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel- `# L( I# X ]
modsecurty依赖的包:pcre httpd-devel libxml2 apr% d: w+ B0 ?) Y
) ]: I) z: g9 x5 G. ~yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ N( `; Y4 }3 E7 ~6 a% i
二.启用standalone模块并编译& R( a s8 A! _( _, }/ z
/ |% d3 T, e' J8 U) [% ^9 H下载modsecurity for nginx 解压,进入解压后目录执行:& V1 t) H M% l B; b+ D
% a# [8 \! i- i* j {./autogen.sh/ Y& |+ w; G; E) l$ `! e/ \
./configure --enable-standalone-module --disable-mlogc# G" L: }3 P8 W8 B$ ]+ R
make " x3 G7 \5 b7 B z/ l
三.nginx添加modsecurity模块
6 ]( b- f9 k; y) G0 m# e6 r+ T0 t) U* p/ j& w% R
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:- D8 u3 b9 }' u
! d s4 ^* H6 ^9 s$ Q. N" T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
; v r! n y# i/ Q$ [make && make install8 X( |5 T' b8 @" R8 B9 O
四.添加规则; Z" l# B8 u+ ]" Z" e
2 p/ }1 c' l7 A$ X7 K$ V Smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。9 F) a6 u# @: Q/ o# E; [7 H' I8 O
9 ^; j5 x$ a! n1.下载OWASP规则:0 G. Q4 X4 C4 w6 o" M
) Q( V$ N8 x% @: b: E! R5 ?
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs% B5 _* V x \% o9 ]3 y
5 G: Q6 m+ S6 k6 w, S8 @; b
mv owasp-modsecurity-crs /opt/tengine/conf/2 _) V1 u: y: y3 u& V# o. R
3 K3 D5 G) s' U! t1 I" \
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf% n4 x2 `6 u6 F* ~
2.启用OWASP规则:( d% u; Y6 {- F7 a0 {
1 V5 b7 V8 v u2 B) c- c
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* k# t: r5 ?$ p( O1 g8 _, I
; b( {" u& n* F; B0 D4 \' G0 \ @编辑modsecurity.conf 文件,将SecRuleEngine设置为 on( ?, [3 X0 v" a! u
% c5 f* U4 Q- [& H7 ]9 z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
7 F$ b6 Z2 f2 v9 b, M; `
7 T! L5 t; Z9 q m3 G, _# Y* CInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
8 M6 t2 e4 E( Y9 \! L0 N( l% R6 aInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
' I& ~0 q: D& ], ^ U, a; ] iInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf' v0 n8 J' d0 d O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( ~$ Q( A! |+ _( Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
, H! h6 L0 [: T; M uInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf+ V0 j+ {! D( O% Q" X5 P1 z4 S4 q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
5 E* Z0 j, [+ e! r3 O( {五.配置nginx
) O7 T$ p& u+ ?7 p8 s5 l
' r! h3 m+ E W( x5 b在需要启用modsecurity的主机的location下面加入下面两行即可:
8 a) N. M# o, ^; Y' d5 I1 v0 o. O1 a: ^% {7 h
ModSecurityEnabled on;
& d, c" I: |# b% M7 m, e( H6 tModSecurityConfig modsecurity.conf;# t: T9 ]( ^/ t
下面是两个示例配置,php虚拟主机:
0 W {! A8 x- y" w! x) B) c7 m1 O `% p0 j' {( M( o" H$ A- }
server { B0 @+ E$ ~( D
listen 80;
0 x9 u/ } p5 f# S. J& `$ c server_name 52os.net www.52os.net;* x/ H2 e+ n9 f+ M; p2 c* m
" q9 g& Q \+ j location ~ \.php$ {2 }0 a! i" U9 @2 H& n1 H
ModSecurityEnabled on; ; R2 l- n) B& h0 _
ModSecurityConfig modsecurity.conf;( H& E# ^) T3 @7 [
0 U( T9 d4 M1 D/ E! X5 _- D! h root /web/wordpress;2 K8 o+ s# U$ u4 b# [
index index.php index.html index.htm;- H# s* ~' W" ^
! B2 E3 `2 }- F* ~ fastcgi_pass 127.0.0.1:9000;
5 E- u, E( F& b8 v) N* L2 v fastcgi_index index.php;
+ g- o( T. J& o. c) r* B/ U* Y fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
8 L. M0 [) b' u# A# f6 [4 O& F' [ include fastcgi_params;6 t1 B" Q+ Y8 L4 u
}
) X& X, m* t' w8 T( J+ U }* p' `/ T% C V |0 ^
upstream负载均衡:3 U# q- @. H: p& ^8 u
6 v0 I$ O1 X j# T/ k; _% S
upstream 52os.net {
; }" W& P' H/ T U server 192.168.1.100:8080;
6 u& \) s# X: ?3 r5 P7 U0 l server 192.168.1.101:8080 backup;- G' X0 K7 N" z; E
}
( F. i& S) B4 m' H6 H& D" u1 e. C& x# A1 Y
server {2 G1 l; c" c4 l& Q! P% t6 r4 A
listen 80;
& ~, W8 F/ s+ p+ Z6 cserver_name 52os.net www.52os.net;
9 _3 \6 `! i }/ Y* k) ~' F1 y
: x# E. S4 t$ F9 klocation / { o. k3 o( [/ ^0 D i, _' W
ModSecurityEnabled on;
/ _2 y4 N3 N# V3 _& |+ J ModSecurityConfig modsecurity.conf; ! Y1 f5 ?3 ~9 |
# R$ ^+ N0 b* d3 |
proxy_pass http://online;
8 N" k" I2 {! {, v3 ^1 Z; G, K proxy_redirect off;
+ S" H3 ?% o" }: w proxy_set_header Host $host;
' o( U; ^5 Z% R0 i z' F) ] proxy_set_header X-Real-IP $remote_addr;. q# s2 N% S L, f4 _: b+ z0 V
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;8 m2 o9 k2 i5 q6 a) ~1 j. r; J
}# X# x3 r7 S* U/ _! j0 g
}1 ]6 h( B6 U' P4 K$ c
六.测试9 ?; c: x$ S4 g( M! g( H, B3 N6 s" z
) G+ ?' C$ o1 ]' ^
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:6 Y3 R) l5 R% T- c! j5 l6 {0 e# o. R
7 v! X+ r/ L0 p9 C( M6 w- M6 `5 w<?php
7 ?- G' v- G; y: d8 S phpinfo();
2 R0 W) x2 |5 u4 P?>
q7 `+ G% s2 O$ Q6 o, \6 y3 Q4 Y在浏览器中访问:
3 ?% Q; G ~* E$ Z
& O' g1 Y$ D# fhttp://www.52os.net/phpinfo.php?id=1 正常显示。! y* G2 i8 D* v" ]! h
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& c& v0 v4 W5 F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。' P" e7 t1 x% B r6 w( ]/ Z3 P# l
说明sql注入和xss已经被过滤了
& _# E' T0 a9 Y" @& z6 q, l: Q _' @" O, \
七、安装过程中排错
# M% K( q' R% ?) b8 M- E
& _" d5 ^% r. C4 t A5 {1.缺少APXS会报错
9 h$ B8 {. r- ]4 g
) q" D/ j* `( a% d5 l7 S- @4 Mconfigure: looking for Apache module support via DSO through APXS) x# d# w* W- o6 B& m B9 O y! ]
configure: error: couldn't find APXS8 U5 Q+ }# l' L$ J* |% a0 U
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。% d8 Y7 q% p+ c: O% ^2 p7 h
解决方法:/ \$ m* A" n% ~. _% O- ]6 ?1 H
! v: u7 W* q. s! a! X# V. X; j" {yum install httpd-devel
) Z. v- n! _3 @- Q& n& S6 u2.没有pcre
$ x8 Z# }1 X, T; z
; v" G% |& s; g, Oconfigure: *** pcre library not found.
# Z; E; W6 V; Vconfigure: error: pcre library is required
1 E" g0 S/ g. J# V解决方法:
0 Z7 h& L* @0 |3 ?( G: g& H. `8 n9 A/ ~2 [5 ?% `
yum install pcre pcre-devel( D+ C: ^( z$ I4 E
3.没有libxml2
$ X( e$ M$ f* W3 H& e% c% j
1 ?9 ^, c' T, C6 b3 J1 I5 s0 z; w
0 R: V7 N" H, C4 a7 g: s5 X1 ~8 Zconfigure: *** xml library not found.$ U5 u$ `6 v8 j0 D
configure: error: libxml2 is required
: [0 x6 _% \# L& u解决方法:( s* @4 Q- p5 {4 h
- [3 C* A& t/ `& A+ G
yum install libxml2 libxml2-devel" ?, F- P# R. Q' q( \8 U
4.执行 /opt/tengine/sbin/nginx -m 时有警告( z' S" c$ F9 e7 B% R) D
0 s: l- P. e2 b x* ~7 z
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
/ t9 I# Z3 s" N" x! jnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
" ]/ s9 s/ B( a5 X$ a原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log+ {1 i6 W: C& @) \
1 B4 k3 |. a; r9 a( `0 @$ ?) K
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ J3 @" j6 L1 L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! M5 j8 s6 w0 Y q
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
: t( |7 e6 ]" K M& |3 d% P5 t. ?2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 V1 u$ Z$ S# _3 \9 F, Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ h( H# j- F2 E: v
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ ^5 u1 v6 R2 o h0 A Z* }) M
解决方法,移除低版本的APR (1.3.9)/ ^% g/ P; O0 S- b% |# x `* _
" H5 Z. A/ J7 @, c
yum remove apr4 i& D% q# U! a3 S
5.Error.log中有: Audit log: Failed to lock global mutex! i8 B: ]( K2 k/ A! D4 @
/ R9 g8 Y* O$ o' k8 U2 G2 }2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
* m, q5 F) `! F: L3 E9 [global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]) ]( l$ e* ~( C$ l! M' S
解决方法:7 } \6 ?- ~# e9 l% X
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
C+ F% m# _8 [1 W
8 M) }/ q; x4 ^SecAuditLogDirMode 0777
0 Z, s) m1 L/ jSecAuditLogFileMode 05507 M+ j# h! @& ^' Y9 O: |6 F9 r
SecAuditLogStorageDir /var/log/modsecurity* R8 J) K, s s4 M1 H6 x
SecAuditLogType Concurrent" V+ a+ H2 b, O9 @' A% P- N
参考文章:
0 Q {, b$ Q: j' \ U. Mhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
- S# v4 F! x3 ]2 i) lhttp://drops.wooyun.org/tips/2614 |
|