|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。' W' r9 _. J# u W1 w. M' T! N! s
' R. l; |7 l8 i |* `9 ]2 G0 p
一.准备工作: s6 x* M$ n5 I- l/ T
" @( T3 r1 q1 _6 P) I系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 A* {* N5 |5 X& l' H7 f$ `- v( G3 S# W" L
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz i' ~- V% m; I7 R+ R4 q. l. J
+ s3 K7 } w) b5 A# d' tmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 J. L9 S- W5 z/ ^* Z: o
: b5 a2 a3 J' M/ j4 {
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 ~5 W& E! g2 N- o% Q
' e& N0 u3 N* b( E: p0 `0 }# p
依赖关系:
H0 R: r5 {" ]" j3 R8 l5 B; Htengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
7 e( o" g: A4 W% C# n2 A. e
+ x" s) g- w: p3 _* Kyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
- e5 B( o3 ^* ]; f4 I! x% @modsecurty依赖的包:pcre httpd-devel libxml2 apr. w9 p I6 S4 @* [7 E# S! H
0 K$ a) @' X3 x8 ~# z2 yyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel8 y1 u f, l+ P/ b- c6 i0 @( F$ `
二.启用standalone模块并编译+ ~+ C$ c6 Q2 [$ E
( f8 c d/ q; k' v下载modsecurity for nginx 解压,进入解压后目录执行:5 ~2 M# D0 s3 c4 f, o( ~
3 [4 Y9 p7 f* A s./autogen.sh
! X' U# K7 J" Z8 _./configure --enable-standalone-module --disable-mlogc" y/ B4 R s. b% b! n& K2 ?, y
make , f, E7 h! q, _. V8 `
三.nginx添加modsecurity模块
( @! H& t0 [% k3 v8 ^# P
4 k! s0 a: B, r在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
( o# }2 v* f/ v+ t$ X# u2 r. A$ \+ P5 p% Z7 ^8 H; e3 e
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
) a3 C. o4 D% |8 [make && make install
5 U* S9 u* p' O/ t/ \& h+ @7 S3 y四.添加规则
r7 _) x6 H0 c, W! e# r# X9 c% J* w( Y! X, l* R
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。2 @% L' ?& L9 U7 g1 i
& g' V3 u& v( Z. V N% a1.下载OWASP规则:( Z/ l4 K+ e' ?3 z* w/ J8 d! N
; m, ^% i- z8 G# d) Q$ C
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs! l" \; }% z. H: n+ {( H
?3 ]9 V# _% }) Y4 b7 c2 Xmv owasp-modsecurity-crs /opt/tengine/conf/$ B7 ?% d3 |: k2 {. W. `
( N0 S8 n7 W+ B; a$ ~
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
2 y% d* w1 ?0 w" A6 J+ ~8 [0 d& d- @2.启用OWASP规则:' r5 v$ B* X8 ~$ f5 w
, f6 e5 }* A) T4 t [ @. p; S! X复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 @3 B# Q9 d$ l+ X' G
+ }9 R: d: a2 x7 y( O# l$ R编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
2 m8 f( T5 M- r1 p: L1 q p0 k
# c& o. g3 u4 I( r W! ]/ E( F- sowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 ?2 b& B, ]. w& ]" _, B3 P; ?6 p' P: |3 _ _; {5 }% N3 ~+ x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf6 f F# o# M6 Z$ ]6 ~
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf |+ n' J: j0 e4 Q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% `% ]! o4 q$ z' o" f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
& S5 i C8 n9 h' \- IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
5 A2 q9 E1 k5 C: k' `Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
q* g1 U9 X' C# l" ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf* z4 Z Q7 @4 c2 H0 y4 `% @
五.配置nginx* o& n- Q8 b( G5 g, P$ k; y0 {
- h; h, Q: {( {$ ]8 |
在需要启用modsecurity的主机的location下面加入下面两行即可:
2 O% j0 ^, |" {1 Y/ P- o" C$ l! h
; f2 x- O/ k5 B2 F; fModSecurityEnabled on;
, K5 f5 Z/ c3 c, `( X- a. A6 SModSecurityConfig modsecurity.conf;$ _# s; N5 b. H* q/ [) }1 b
下面是两个示例配置,php虚拟主机:+ E# J h1 @. w5 i: ?! J, H
$ N8 c0 {$ t+ Mserver {
' n9 W/ b+ s5 i$ \! S listen 80;
# W! N4 @. M8 h a0 t server_name 52os.net www.52os.net;! H D0 a0 D8 \* |& U$ t5 q
, _8 J# {; C6 m. \ location ~ \.php$ {- X; _+ a3 F: t, k6 v0 k0 \1 l
ModSecurityEnabled on; ! l j# s4 W7 G% j' C
ModSecurityConfig modsecurity.conf;: G5 V7 N* a" [, E
9 p3 z3 D X% d* z2 z2 N3 k% u root /web/wordpress;
& F2 z2 f N2 t( m index index.php index.html index.htm;
) _/ |' \9 M2 u
, P) h# n6 I! [* d% X6 o- Z fastcgi_pass 127.0.0.1:9000;
/ _. y& w/ O" Q# f9 O2 X) i5 K; q fastcgi_index index.php;
1 i/ n$ s, b" q* Z fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
: v. c. |* _! N; C9 |0 z include fastcgi_params;
$ y1 z+ ?8 Z/ i# ]( c }7 h7 T+ N. H2 h0 A, I
}
[- Y, R' ^2 L( }" ~1 [upstream负载均衡:
( J% k. @* ] f' I5 k/ F
' o7 { ]$ ?# \0 U, H/ rupstream 52os.net {5 V6 z, O: T+ y2 X/ m% q5 ~
server 192.168.1.100:8080;
- [$ N2 } ?; T5 @* r- H server 192.168.1.101:8080 backup;( g) P+ s. \' r4 G* x9 S
}/ [2 m. I5 L' Y I+ ?( }6 c" ~
" e' a9 d1 l1 U& a
server {
; e/ f/ N1 o; e) e# xlisten 80;
+ N1 Q2 E4 F# y2 @6 [" m7 zserver_name 52os.net www.52os.net;
3 W' c, E p4 \4 ?: \! i& `7 R! Q2 e
location / {3 \' J' _, C4 a! K
ModSecurityEnabled on;
) s$ e" W4 }1 f2 Y ModSecurityConfig modsecurity.conf; 0 `; P6 N# j) P4 f$ O
: v6 Z5 b9 m6 ?, Q' f4 F j proxy_pass http://online;
1 [7 S' A2 I: y/ ]" v% {+ K% l proxy_redirect off;$ `! C( S* l, C! E! ^9 D
proxy_set_header Host $host;, o; I: S& O0 o( J9 K
proxy_set_header X-Real-IP $remote_addr;2 s/ o0 O, k% x$ r
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
9 {0 F/ o4 w9 {$ @0 n# x) z! F }4 R* a: f# U- S3 o! u
}5 e3 K/ U* Q s. Y; q! g
六.测试; I/ e1 Q# F& |" k$ J
0 p9 I' f0 a3 |我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
d/ y4 |) d1 B7 P1 n; Q5 F
* i4 z4 A0 H. c" r6 C/ K<?php
( p" O% y4 k2 r phpinfo(); . K- i$ Y6 |, H
?> ~# L4 [) I( y0 V
在浏览器中访问:
4 ]* f6 V' S2 u1 H% U3 i
$ S. q3 s$ L% ]& [http://www.52os.net/phpinfo.php?id=1 正常显示。& V; h) G l4 m, w0 @) A8 D
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。4 k1 _4 k7 e) i! L! D
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。$ f k2 N, N6 A* n+ J1 a
说明sql注入和xss已经被过滤了
. ?. p3 ^" G) ]% x6 E
, V: `$ P6 ~0 J- @# {! K* a七、安装过程中排错( Y/ V) Y, h; z3 E7 f
' L! S& k) m2 w0 y3 c& @+ f1.缺少APXS会报错: z' C; ]- L* ~6 F7 Z! z* F; M1 U
/ I( R6 ]! I: s6 L4 l$ e
configure: looking for Apache module support via DSO through APXS% J. ^( B8 N3 X2 R% q# \' i' j
configure: error: couldn't find APXS
1 u0 L: v1 M3 K: Yapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# Y3 W1 X/ k8 r: k k! K. @# g解决方法:
6 J. U4 p9 n5 m; G; A* |3 C4 u" r7 x
yum install httpd-devel* \" p! Y) o. r" l/ n/ Z
2.没有pcre
' _* _8 h: ]1 s e
% D* I- N# K8 R9 @configure: *** pcre library not found.3 p9 |4 M' B( @
configure: error: pcre library is required
2 A( Q# u. _2 s解决方法:, d# A2 E' w# f' v* z: v
# k: \1 F' M- N$ S; V& S9 Myum install pcre pcre-devel
0 ^0 Q, a7 C- K% ?- C+ U8 U3.没有libxml2
* Z/ \* h& G3 {/ e7 o
% b) d0 ^5 m. p& i( s0 u1 r0 ]: ?/ V; A5 D/ l, r( m7 q2 x
configure: *** xml library not found.- M5 `6 p& O5 I9 w
configure: error: libxml2 is required' I& |, G% X! f
解决方法:, }4 u* D8 ~0 @
2 C% U4 K9 u, V# l5 a
yum install libxml2 libxml2-devel
1 w7 C& ~% I7 c$ D4.执行 /opt/tengine/sbin/nginx -m 时有警告# I [# w4 D8 y7 c6 c- j
# D' }5 r% a1 D7 ]Tengine version: Tengine/2.1.0 (nginx/1.6.2)% A; X0 c% i; O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
& [7 L' F5 l. a8 l; Z$ x) O原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
4 p/ d6 f& b7 v) p/ d# ]
; N+ E* P; t, f1 S$ a4 d2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
, }6 Y1 g& U# c' B/ Y% r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9", m0 y. a" h: T9 \& B: W
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
& M& L) S5 H4 f% b; L2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"6 }* `+ Q8 _6 ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"% V& ~7 n% {+ B% P& w, h$ \+ Y- d
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
5 z/ }2 @7 S. s4 M" t# V. X: R# ]; ~解决方法,移除低版本的APR (1.3.9)
+ A8 g2 u# O; T/ G
: M3 ~/ [5 Z( d8 \1 p+ l& ]) {yum remove apr" |1 `8 t+ Q! n
5.Error.log中有: Audit log: Failed to lock global mutex
- G; a5 W+ R, n/ A6 q
$ B, V2 l, g/ j; T5 B, t* N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
# ^$ e* c: M: t" fglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]3 e0 F* |7 M, ~$ B$ h
解决方法:) f5 ~/ j1 p; ~$ Z8 d, W0 {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
( t: B! [9 p: Q( Q2 P. h: v- p% @6 g8 C1 E
SecAuditLogDirMode 07777 x9 v: M6 I# |3 H2 x8 Q
SecAuditLogFileMode 0550% y6 H: k Q% d. b
SecAuditLogStorageDir /var/log/modsecurity
# n1 {: ~4 |' L4 a7 H3 U3 ~SecAuditLogType Concurrent8 e( v; T' R8 ], i" r
参考文章:% \5 M/ \: G7 [& ^
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
/ [# Y# C$ E4 V# S" S2 O K& Zhttp://drops.wooyun.org/tips/2614 |
|