|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
! R$ R" J; h) C0 ]& `1 z `5 @
4 R# t8 U4 K" f, Q/ Q7 |一.准备工作- j; Y1 l5 h( v* w
8 {" g" f* a% T( w R
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
" [5 z2 B& `, a# R$ W$ Y3 [9 S! D0 F% y, D! L" L& {
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
# M0 P: X1 n! t; h5 r0 E
! j" _* r+ C4 T- omodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz9 ~% P7 ]3 A4 j p
: g! Q, R% q1 @5 q
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs; X" \8 b7 Q9 d9 F- h
7 |: @9 q2 s- c: H* W! m; F依赖关系:
( T& a& M+ f) o# N4 g I- `tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* G/ [0 Y# U# d3 w* t7 ^1 {
U& R& z6 _- |yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 |! Z: r, k. g$ S* m
modsecurty依赖的包:pcre httpd-devel libxml2 apr% y9 m. |! w- b6 J
9 t' S' r( f5 R- k$ K: N2 q) W
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel0 d2 E8 o9 J3 Z7 y$ E
二.启用standalone模块并编译
* R( C& F6 P# C) A6 a$ T/ J! w/ h2 Y" J3 i2 J( _0 ?
下载modsecurity for nginx 解压,进入解压后目录执行:
9 d$ s' Z7 Y3 b3 t, a' `% Y- @! c- D" g: U; }. ^) h. X
./autogen.sh: S* ^. @3 e5 S
./configure --enable-standalone-module --disable-mlogc6 U! X7 D' g i" u$ A; R. E
make
7 k% p8 e) k9 j' C, ]三.nginx添加modsecurity模块
1 v* ?& Y1 G5 G7 k) E% D- k6 _+ f$ m9 L7 e( F- n3 i+ h
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: M' W( ]$ J% R
' H+ `, _0 a: M./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine E+ m; ~2 F: `' B
make && make install
& u W2 Z, _; j, M' Y' D四.添加规则
: e" ?. c5 ?! g2 D2 H% V6 }7 m
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 D) Z2 C1 C7 y- D2 j' ^5 p# q! a8 m) X; o6 z" c s$ B
1.下载OWASP规则:0 q9 T- Y+ R# A2 d/ A( ~! b
7 O7 n. `& g, Bgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs# x' _: H. H) [0 l0 a9 \
* a8 }! \( a' K7 Z, r; D
mv owasp-modsecurity-crs /opt/tengine/conf/
# ` T" M: r" b7 l( n5 T d5 O
+ c# u4 g2 h% V, L% A4 D$ {* N7 ?1 h% Scd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
# M1 D5 ~7 f+ O7 S0 h+ I2.启用OWASP规则:
% Y. M8 n& _0 x. M- M% _" J# b# k: k6 R* J q3 f6 e
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 k- M' _5 c/ y# K8 ~3 ]
P6 j g" _. i4 C* G& x
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 j2 B# p( R% N0 i+ s* O+ q- O) D) v* _
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
: G! D; |5 b& m. \$ x3 x
- `5 P6 T) z1 k, _1 m) PInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
+ L% u2 t2 _0 P4 r7 W _Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf1 I* a5 u/ e" \; X
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 p! c9 ?3 y p. ]* }8 B. E
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
8 }5 ^, O! p! V+ IInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf3 n3 a; X. B. e6 _. P
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
3 O4 F( s' a, lInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! s! N# ^8 ]. A五.配置nginx# P; y' M: L& Z8 ^6 a
9 m E7 v& i2 a7 _, |+ x在需要启用modsecurity的主机的location下面加入下面两行即可:( w. Q: G; x) ~, }" S9 c& H
: A1 [1 Z0 m9 b" [ModSecurityEnabled on; . U; u( e3 s5 P
ModSecurityConfig modsecurity.conf;
9 c8 i2 R3 Z, W. T }* q8 c2 m( d, Y下面是两个示例配置,php虚拟主机:: V( { u' f8 J+ B7 V
n0 T/ |' w; n* J* R2 vserver {
5 D+ x8 Y( U$ k- @5 v$ z listen 80;# l: K2 G, s2 @3 z: N
server_name 52os.net www.52os.net;. ]1 ^ r' z9 h \# q% H
) [. R/ `7 g6 T! S- c* i! m+ o
location ~ \.php$ {
4 N6 r" `; H9 |* T, e# n, w, _ ModSecurityEnabled on;
9 D; l: G# f' L6 T1 k0 d ModSecurityConfig modsecurity.conf;0 [7 }- X @/ U5 V
9 o% D& l% B8 y$ m root /web/wordpress;
" |! Z5 v: z+ M) @* M( q index index.php index.html index.htm;) t' l% X. P. B8 J$ i9 p1 J5 P
% I! o, V+ u+ ^* Q7 w
fastcgi_pass 127.0.0.1:9000;( J% w6 p$ z, s G: U& K
fastcgi_index index.php;
" ~1 M. c ~- z. U5 m fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
: }9 j8 Z& n. u include fastcgi_params;
; X4 J9 w. O8 \5 |: } }
) I1 Y5 v, @( |/ Q- \3 b }
: ^' \% h' L- Kupstream负载均衡:
4 t* |- [7 R; {& s+ I. b2 h6 x V$ V1 E' l
upstream 52os.net {6 N- I# K# C8 W0 p- l+ L$ d
server 192.168.1.100:8080;
" P$ x$ T4 m, E* G& {; r server 192.168.1.101:8080 backup;0 R& ]$ T# W; M, O8 w; `/ ]
}
0 e2 M; }3 q: R9 b$ A0 ~) C4 N' d( _4 g1 B/ `
server {
3 N6 T# E7 D& s0 W$ L8 |listen 80;
8 Y+ B% P+ H% Q! g7 i$ Aserver_name 52os.net www.52os.net;, M2 f7 X7 m8 H0 V, u0 @
$ t2 P/ |9 O4 c- glocation / {$ a$ h7 b1 p4 X& C9 k0 J5 G9 g
ModSecurityEnabled on; 5 h; ] x+ ?( c6 p0 D/ u
ModSecurityConfig modsecurity.conf; 8 Z" p/ o4 i) J4 [
) A, u% v, x5 x# e1 f. C proxy_pass http://online;$ e2 Y1 m3 r6 k1 g3 U( u
proxy_redirect off;$ v/ z: L. p+ e; B( P
proxy_set_header Host $host;
0 x9 f& T* ?& J" C proxy_set_header X-Real-IP $remote_addr;
6 q& e/ K0 i5 C' a proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
6 U0 A( r2 c. R9 b. Q4 y }
; Q1 v2 Q0 T% _$ \4 k9 F3 u}
; E$ ~1 W$ e0 ^六.测试) t3 W8 B+ o( P# U- I
/ @ E4 a! z( c2 y, R我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( c' Z# P; Z/ q. w: L6 }; N! R
9 l- ~* W' V4 S3 K; d8 D: [% k# y<?php1 Q" N9 F9 e4 ~7 J
phpinfo(); $ M( a9 `+ U0 o7 X/ _- U0 X
?>2 U7 x! I2 n \8 c. T4 u$ I
在浏览器中访问:
0 V' R( I8 s, v7 D
5 E9 H0 ]5 |( D5 s, s G# B8 q0 yhttp://www.52os.net/phpinfo.php?id=1 正常显示。5 x# b/ F! Z! N$ l+ |
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。$ a3 q: m6 Y* G% r" U& q m" J K
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。 T+ [! i- ~! D4 Q; Z
说明sql注入和xss已经被过滤了
% q$ r+ ]. M* T4 O. @) N+ ?) b! G5 s- I' V/ |( Z8 D: R. x
七、安装过程中排错7 T7 [8 r5 e% p6 O$ P
4 {1 e, R! ?; ?# T: t4 y3 B9 I1.缺少APXS会报错! v N$ ]3 Q6 x' M b7 b
! z# c" E* u% ~& G9 g5 kconfigure: looking for Apache module support via DSO through APXS4 @1 y( s# }! M0 i5 f! F2 i$ W
configure: error: couldn't find APXS i# A/ A; |8 ^# r
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
$ C* H9 f+ {0 b" J% L解决方法:7 G C6 }: z/ b6 r
5 ]2 e4 U- w/ x$ H6 s1 gyum install httpd-devel
( A7 K1 N9 P1 s3 Z2 t2.没有pcre+ s! d) v8 X( n' ^5 \
/ N& T" Z/ P$ {- W# rconfigure: *** pcre library not found.. J6 P) [# u g, X* p
configure: error: pcre library is required
7 |' E% I! ^- B1 n: E解决方法:
1 h" W/ y r5 z T: \0 {8 W( x' T( V2 M: ?0 j5 c; y
yum install pcre pcre-devel1 Y; Z; M0 p" j+ q9 P
3.没有libxml2
& O! W0 V' W2 q1 w& b! Z3 Y9 X9 C+ L$ Z/ r# R( w/ G" ]
* o5 P2 b! o }, d
configure: *** xml library not found." r& m; y1 s7 O- u" e
configure: error: libxml2 is required$ ^/ y( v6 P) p g4 m7 T; s: A' P
解决方法:
) y, F5 Y4 E% J3 n1 |6 @. ^/ u! U/ T( K5 c' o
yum install libxml2 libxml2-devel
& S% ?% L: S# V4.执行 /opt/tengine/sbin/nginx -m 时有警告1 r6 \) T! H' l# }
' _; u2 O, N9 C. D, F9 Q7 rTengine version: Tengine/2.1.0 (nginx/1.6.2)
: L0 k2 S% m x( w9 L J. i/ |nginx: [warn] ModSecurity: Loaded APR do not match with compiled!" H l" W' z+ B8 _4 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
6 U2 H+ }% s; O0 v" [% P1 o- F" L( t: G( }( d9 S- L/ x# R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. \) `5 g6 E! m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
, j8 ?2 g: C1 Q2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!% ^/ x' p; J9 |9 }7 M
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 a! i$ e c% ^& J& [ R
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& C8 V6 ~1 J1 q; |) ^; L
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
: G& N- J# C$ \) k' [* ~! Y解决方法,移除低版本的APR (1.3.9)4 S) c( u. a# t; u; N5 t
# `5 f9 [* J8 l3 Iyum remove apr9 q+ U+ Z% N2 E1 T( y1 E
5.Error.log中有: Audit log: Failed to lock global mutex' ] P: d( ~) X
; M+ L9 u; t+ f4 W
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 4 w' S+ v6 M# h& R- j5 I5 q
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
) G8 ]; Q$ D, |/ _: E- L解决方法:
1 W4 n6 ?# g" E6 W3 f编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
+ Y: i& L8 L/ X$ l7 t0 m
( N' N8 y/ b% D' l% B% z# iSecAuditLogDirMode 0777
( U& S9 u7 N# V- e' ESecAuditLogFileMode 0550. @% Y" b0 m6 J9 Q' k8 c
SecAuditLogStorageDir /var/log/modsecurity
2 Q: A- v: _, A& v% c4 e6 eSecAuditLogType Concurrent* P0 n; ]# d0 V- @* Z3 H
参考文章:) |; d; s$ r5 d1 g5 \
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX. E; R6 |1 K6 k4 {+ X+ R
http://drops.wooyun.org/tips/2614 |
|