|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, M% r: s$ X" S. @1 _2 f
+ {( M5 |2 U3 v. R4 P一.准备工作
" u' b9 Y" i( m1 L' _& Q7 m% U- F; c7 [+ u* j
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ z; g+ C v4 @% d# _# ?+ H- l3 C9 Z* S/ d5 @# N4 A0 T- ~& n
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz$ B1 l* e" l- f7 d3 r+ G
+ m7 h- e+ ^$ X& |4 f. j8 c0 a5 K# \modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; ^) t' e) A: r3 J* ^/ B, D2 G, e) U; w2 V/ l/ V
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs4 Y- C9 M S7 A' H. b
6 s+ j/ l: i9 U/ t6 @, a& T c& e
依赖关系:: b- O3 E% D- N. }1 v* D
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:3 M! d8 M }4 R3 I
, i+ `" K u, @5 x: N3 zyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
/ f) N$ f5 f. Y% m+ U [modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 E4 ^* x% x Q5 L- K2 |- D
' Q, C: n0 H) g E j+ @yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
' I0 T3 J+ u4 q1 ~* q二.启用standalone模块并编译6 G0 v; ]- ^0 X b
/ V0 d3 D1 M! K p+ n! U+ M+ A
下载modsecurity for nginx 解压,进入解压后目录执行:
' v" g4 J7 T6 _3 |# i5 m9 C; c& {. M7 P6 z, W
./autogen.sh
4 \# m) S" w2 A2 B./configure --enable-standalone-module --disable-mlogc
# a3 @7 @, T, i4 ~9 U& Vmake 0 p8 B: Q0 W# T
三.nginx添加modsecurity模块
: s- y8 ]5 s( w* L* _8 r* I
- _1 x8 [% M: S) n+ P' S在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
* D3 R0 a ]" q( y( T$ r; J4 F' F, E" H) y- z$ C+ ~! D8 J
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine' [8 o* P. |* G0 h+ \; B' u
make && make install1 n# J G' n# w' {6 K
四.添加规则
6 L5 m3 M$ k! N1 i/ P7 x. b& ?; v5 b
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
" H3 A5 i1 A# x7 I/ g! g- Y7 i
; m3 i. n' D, {; `$ U) Y4 R/ _$ N7 G1.下载OWASP规则:# V1 `; Q# c1 s; o) ?
3 i4 A# N/ U, T$ T
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ Q0 [% c' L+ T L9 Z+ s! g1 w2 r& D
mv owasp-modsecurity-crs /opt/tengine/conf/
/ q4 r5 {; ~8 e! c; Y. O+ Y/ [
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; s7 ^4 ` _: M3 h$ Z6 ?2.启用OWASP规则:
* h% I, G0 D" P! g
* b( A+ P9 B0 u! d" R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& \; \- E! ]4 Y/ u2 W, W, {: Q
) D, {: S9 X7 }) } z. Y2 X S7 i
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on% h$ [6 d) |) t+ @( X: @8 p
# c' t9 W6 }, a6 Z! `2 g' d
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。0 }$ [' ]0 }2 k; W+ X
, q7 |, v; F, Y! H( E* m7 W
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf# u4 W# d9 v! x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ ~6 A. Q& W& y0 R7 }* X h9 k% E- NInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
6 Q( _9 Q3 W, M8 R0 M3 YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ A2 @: b" Z% X9 G! r% t
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 T5 P2 o: C, @/ r a4 j4 z
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf! G( ^0 q3 N. k9 g |- y
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
4 ~: j& i' F7 r( w3 U1 a* x五.配置nginx
1 z5 e5 }: `, u/ ?; h$ h& y5 D& X' C
在需要启用modsecurity的主机的location下面加入下面两行即可:9 [" t! S: y0 j. v
& J! x( X# }" {& mModSecurityEnabled on; 0 O" c! ]9 Y: x( ~" f1 o' L$ P$ v
ModSecurityConfig modsecurity.conf;3 q2 [, e2 X) T
下面是两个示例配置,php虚拟主机:2 Y3 V" I& C4 b h! q J3 F3 }
) a: e: ~% N6 c4 R/ V! }3 y' lserver {+ V4 t! r) Z" y; Q" f, m
listen 80;
; J% U/ A, E# M8 [6 o server_name 52os.net www.52os.net;
! ^, s( q& }. W$ l : {+ f ~2 Z \( q# \0 c3 g* E
location ~ \.php$ {
! v# ]/ [$ N( |' x ModSecurityEnabled on;
. d1 L5 u4 u8 W ModSecurityConfig modsecurity.conf;
* p% y5 f- V4 t7 n1 }! T0 i6 F, j" Y. p! M2 a3 r
root /web/wordpress;8 \# }5 z% q" N$ _
index index.php index.html index.htm;
4 m4 h- Z4 W' p1 D1 C 0 a3 {* S5 t) V9 ]7 Q D/ [
fastcgi_pass 127.0.0.1:9000;0 n8 R# e! J$ W x$ h
fastcgi_index index.php;
: P% n* u8 R2 o' _1 g+ x fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
7 C0 j6 [$ J6 k$ M6 O! { include fastcgi_params;
% h9 z4 \& w- b+ l' E, O }! {% ?+ h1 ?" ]( I
}& s7 x- W5 r. |6 Y; a
upstream负载均衡:
. M1 _0 F7 ]: W: i/ [! r H0 {1 u' S: @/ h
upstream 52os.net {
! t- l2 [6 M5 j0 i K; i9 p server 192.168.1.100:8080;1 `2 E( |- l) W2 T+ `- y
server 192.168.1.101:8080 backup;" e' N& Q0 A0 q* f
}% a! S" y7 _, l
" C- \$ r) d% I5 l, h/ W' r* [
server {
" m$ t) P9 N* hlisten 80;/ t9 E7 b/ e, G* [) h
server_name 52os.net www.52os.net;
2 t$ S/ o' n% m5 S, @1 o( m$ ]
3 R) s! v& D% _location / {
+ v6 D- I, V- f ModSecurityEnabled on; / f- u% k, Y! A8 }% C7 F9 D, G8 Z5 X( f
ModSecurityConfig modsecurity.conf; 1 n2 i2 J' D* N5 D. O1 F- v
# B% ~ O( a. D! S0 a& d
proxy_pass http://online;
* a& C3 O; K/ J5 _; g" s proxy_redirect off;2 t- w; g6 n' n! o
proxy_set_header Host $host;2 w5 I7 \+ W* } W8 G" v6 V: L
proxy_set_header X-Real-IP $remote_addr;1 D* D, r6 B& T2 \, c2 G. ]/ A
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;: l5 A g5 f2 q! }! n
}9 T* W6 W) y0 K; C7 m7 {! q
}
+ Y- P: n/ c9 F六.测试
, F0 Q5 A0 R0 Q# O) b" x' s2 K- ?7 h# y h8 a" N" {
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
* V0 {/ o5 ]" o: k, z. Z8 t& q- Z; z7 t }* b7 T8 v9 a) s
<?php
8 g" j) e( Q5 t; b phpinfo(); G# L$ D) x- d( z. C; Q) u2 ]
?>
# ^2 O- @3 H' |- z在浏览器中访问:! o9 ~3 R5 [) v
9 c) G, n% T3 _7 V) ~. j' b" Shttp://www.52os.net/phpinfo.php?id=1 正常显示。5 X' G. q, V, ]" L# F& m$ M
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。4 K5 n; c! }1 |' A5 u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
! W, f$ X: C: q* X' |) g% D说明sql注入和xss已经被过滤了
y3 ~6 t v% ^; t
+ t& N- q# t+ A6 S# f2 K七、安装过程中排错- l& w [/ A, N
* `8 X+ }1 X) a8 C/ Z1 P1.缺少APXS会报错
. a4 W+ S% l$ f/ Q1 m
& i- ]2 w1 ?4 D1 d$ \. I7 M Cconfigure: looking for Apache module support via DSO through APXS
; x I( ^; l5 @$ F) E+ ^: Qconfigure: error: couldn't find APXS7 @* Y4 `" L+ h, {% K& e) o$ x( I
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ _# e2 R/ i5 H' k8 j& B" r! G解决方法:6 Z9 _6 L9 i! B# m' r8 K
7 O, \( }% E G$ E
yum install httpd-devel& x2 H! t% a) e+ n+ i
2.没有pcre
: q) O1 D) J6 m1 q2 o
. o2 s, b9 i2 ]2 [3 I$ D& y' Pconfigure: *** pcre library not found.
/ y$ ] L; U- q/ o" l. e! V: fconfigure: error: pcre library is required1 m' b$ _# M9 ^7 k( K
解决方法:
X0 j6 f: g2 [9 t
0 L8 \5 \2 M: L3 r# |yum install pcre pcre-devel
! I$ Y9 n5 U+ A, E' _3.没有libxml2) C& {3 E; G4 E6 L3 Z
6 N- n* @; }$ L* l% W V
9 U! Q' r% W, [/ Aconfigure: *** xml library not found.6 ^6 `, O+ y9 b, ^/ W( |
configure: error: libxml2 is required
Y$ B& [/ I% R4 j解决方法:
1 C4 D% e d) T) i! q
1 I. y$ d1 M+ o1 G: O/ |' G: J5 zyum install libxml2 libxml2-devel
8 I$ g3 B) i) _$ c% k6 t4.执行 /opt/tengine/sbin/nginx -m 时有警告5 j5 }' I& N9 v" R1 L8 O" e5 k
3 V& z. Q/ l8 R& h" t! X5 r& sTengine version: Tengine/2.1.0 (nginx/1.6.2)6 u# A0 C6 m, ~1 z6 c% ]+ }+ P1 r
nginx: [warn] ModSecurity: Loaded APR do not match with compiled! Q B- V% B3 `7 i
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
& q- z+ j) E; X7 C3 n
1 j" @/ Q) R: a& V& N. l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.5 ^7 }8 }5 o# O- c7 W& s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
- g: M8 \) G y3 Z R! G2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!7 Y3 G' j( k; g9 R) H. E9 l! a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
$ J* I" W1 l- p' Y2 y3 M+ Y2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 L* ]* P! Q( h, \
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
# A; k% f/ a/ Y解决方法,移除低版本的APR (1.3.9)
0 s9 b2 y7 v) F, ]$ J" Y4 o5 I2 j' S! `
yum remove apr
: q; B n7 v% L& g G" @5.Error.log中有: Audit log: Failed to lock global mutex% Y' L, q! W3 ~9 f0 ^6 O! m
$ w1 H# S1 @6 |, _( V' K8 ?2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
6 p* C. w; p- i7 J' w$ }6 `global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
: I" }9 F* u/ t0 j解决方法:
5 u' ?0 D; W9 o2 F编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" V1 |: X9 \9 Q3 U. A6 ^: }. X; X, l& o U
SecAuditLogDirMode 07773 L' N, R! u# @+ D9 _( w8 e) P4 @8 o
SecAuditLogFileMode 05507 ~% ?! j5 H1 e$ R+ L
SecAuditLogStorageDir /var/log/modsecurity
' K/ p; G1 l% | eSecAuditLogType Concurrent
; O9 u p. F0 D8 q7 f0 o; B5 R参考文章:
" S. x9 m8 l' i. t3 n; @# zhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX- l6 R+ c* U2 h/ ~
http://drops.wooyun.org/tips/2614 |
|