|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 t* l1 s; V$ [5 q; j! `0 q8 C3 t8 ^
一.准备工作! @; d6 k" u2 e9 }5 b% k1 C0 ~" g
$ Z. @0 G' {: F# q2 F: | t
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0- E5 \. X: I0 t& |# N- o
! e( [1 v8 K4 E+ m1 T: m7 E
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
- D3 f" Z* v! d, k% ^5 H- b
1 C/ p" [* ~' e: ~' N% G9 imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ s# | p* R; n8 h
1 D* X9 b: i: n4 L' G! kOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' E6 s4 K. \8 n( w2 l2 G7 a8 E8 W: l7 |
依赖关系:" j1 Q X$ l) m* n# ~- I, M
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
- {8 r/ F/ D- x" g* i5 v# n# l& } }1 `7 }$ X
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
/ y5 x- p3 Y9 ~6 F5 @$ Ymodsecurty依赖的包:pcre httpd-devel libxml2 apr4 W8 V7 z: s/ G7 u+ F
4 }: ~6 g8 U- m0 T' E7 `# }8 D
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
) O5 W9 O: o% {二.启用standalone模块并编译
1 Y9 S" m, |, l1 o" t7 v
8 p! X/ ?! R3 k; e2 b: G下载modsecurity for nginx 解压,进入解压后目录执行: m. c/ m/ j" b. D/ ]
4 Z' I2 Q3 P# M' P a5 M" ~9 I0 i
./autogen.sh
; H4 I# N# A' [; E./configure --enable-standalone-module --disable-mlogc! N# Z, @: t: O. Z8 Z1 B
make
: U( @$ N; R) o, U" b# c) Y三.nginx添加modsecurity模块6 G: t: W3 o) f/ Q
: s# d3 Y G! z/ m% [* }
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* n/ ^% v; f+ R% Z8 Y
6 o2 ^+ k" A% \# n# I4 d4 g: u
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- \; @/ s% i) D& {6 zmake && make install' H2 G- r- J/ p8 \3 j- a
四.添加规则
0 R z+ L2 M6 Z, _1 {8 H, @- i$ F$ p. k0 r0 c0 z5 X
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% Z7 B. a- w( E6 t& P' S4 a" ~
. u7 [' D4 F$ T Z/ \* @1.下载OWASP规则:
5 a$ t! A3 G. i! x! z0 W& J
0 X# N, r" X- C& ?# cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs2 N1 p- _3 S* w2 g
) d- f# n& y7 Hmv owasp-modsecurity-crs /opt/tengine/conf/
# H/ M! b! R+ j Y1 W1 G
% Z9 l! r+ T( Qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf- |2 }' G4 [, o3 t0 v
2.启用OWASP规则:5 V0 i# Y2 M3 l. O x
6 I3 ^9 Q" j4 w# j9 f7 H
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
1 k$ D P; `, e1 S/ A% a( m4 }, Z' M6 R; r& K* }1 D
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ g" q& c/ p1 w7 M) Y
! h0 K `& J, z7 f0 E z: P( E
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 \. Z1 @# ~1 Y6 Y2 v H
1 ~) [9 L; ], Q( s6 n% z& eInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf% T" @4 [( Z: ?# w- R
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
8 z# E# N0 }$ H* ?. sInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* x1 F; \0 c, R+ |' {. r2 E) yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
3 c K, t" I& M* u( dInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* A1 [4 X3 K( v/ k, x* G* ]
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 i" { ?1 }# i2 A5 L" H$ wInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
' r# d% ]* ~. l, W+ v五.配置nginx6 X& b' u2 u6 X6 b# C, h. s' f k
' A% B+ v2 q8 @* E
在需要启用modsecurity的主机的location下面加入下面两行即可:
- G" j5 ?! I, d5 ?
" Z& a# r, e. d) ~# [* pModSecurityEnabled on;
$ r6 |0 z ^* E+ g. [2 U4 eModSecurityConfig modsecurity.conf;. i. l3 ^( u" p7 s* D
下面是两个示例配置,php虚拟主机:
" m4 l9 |0 ~/ s! a9 i5 P+ i5 M( |( R0 P* m- E1 f$ s
server { _6 l. c2 }: m6 G/ X( x
listen 80;
& P5 A2 q" t, l" C1 C server_name 52os.net www.52os.net;- ?; B. q- a" V# X ]0 J# `
* a$ b: c$ p% p: _: a, z location ~ \.php$ {
) F5 f- q! Q( ~# ? ModSecurityEnabled on; 2 j! @+ C% o, u& a
ModSecurityConfig modsecurity.conf;. T3 e, \0 ^% f# c- g+ Q, D# ?- Q
1 G9 x! u* ^- R! f1 E root /web/wordpress;' {& j, U% r( v. d" q8 k
index index.php index.html index.htm;2 x7 a( m( K* ~8 U% u2 o
7 D) u, x0 f; _7 \! Y, a8 B fastcgi_pass 127.0.0.1:9000;2 c. D+ S! d5 ?3 a Y. P, _
fastcgi_index index.php;, `- ]. @6 k" W4 q) K* C
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- M; _5 d& l2 H+ a9 R include fastcgi_params;( {. _' O5 a+ b# E1 K8 k
}! [1 |! [* i6 k3 N; `- G( d- i
}1 y9 {3 P/ K# X( k
upstream负载均衡:- e+ \. f; m, U# y1 G! g5 r$ j
$ t& c6 G! |* u6 B5 g9 P2 X
upstream 52os.net {
4 Y- B6 B4 [2 a! d: o server 192.168.1.100:8080;9 O" ^/ ]0 G; G8 K# T, ^4 r
server 192.168.1.101:8080 backup;
* C4 J3 F- j1 Y, \! W" X}- V/ T1 F3 e1 p
0 s! X# ]- T# w* Y: C8 _9 X3 m
server {' z. t/ Z, E! N. x
listen 80;) |9 ~7 L2 G$ b: k2 C5 v
server_name 52os.net www.52os.net; K! `- ?- q1 q
/ H% S3 t3 e; Z4 D$ @$ Q
location / {
9 P; Y+ N& S& p6 S( q% F ModSecurityEnabled on;
8 j- E0 g* q2 u6 Z" B O7 z ModSecurityConfig modsecurity.conf;
+ J& L4 u# _' r* }" G) U& K! N+ l+ H8 p7 t( |+ t2 V
proxy_pass http://online;4 H1 A1 C) |: U- ]1 G$ ~! b# y
proxy_redirect off;
0 G0 T9 J8 R; f z9 A" C. Z proxy_set_header Host $host;
6 X# U) M) a" D; p proxy_set_header X-Real-IP $remote_addr;- v. _9 i0 W" g w* N
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;; Q( u" s0 t7 P4 v; b
}5 \8 y9 \" U8 t/ b) R
}
$ a- s3 _# g) m+ E; \$ \六.测试8 W( Z6 f8 D5 Q c4 U
3 }4 M1 B0 b7 g( I. ^
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
% ^2 H) Y D8 a& w w! q- h
1 K, u5 O0 P2 Y o8 x$ O$ n# ^<?php
8 j% h8 y/ j. v* v; P2 i1 d* y& ?5 N6 Y phpinfo(); $ f3 j. K# _# k* q! s0 l- k
?>* E) B6 x4 f% Q, E% d) g) C* Z
在浏览器中访问:2 Y. I& _- |$ o0 V- P I
! U/ k l) D' L) R* Y* a+ ?0 ^; Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。3 b! E" Y1 q0 T9 `
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
5 |, ?) s: \% D2 D0 Z9 {http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
& E: S% A3 Q! g1 c3 x4 ?/ C说明sql注入和xss已经被过滤了
5 T7 l, ~* G( c; H& e3 U# p
. Z- `, X# D, F# F+ W, G4 Q七、安装过程中排错
" K! W: a7 d4 J2 z9 }$ L" {, \
5 E, k( {2 h ?4 b( T5 I+ ~1.缺少APXS会报错
" N7 }( l, n6 \0 |! q% z. ~& N% |3 t+ `
1 ~+ I$ a/ e1 v0 Tconfigure: looking for Apache module support via DSO through APXS# u! P& `1 ?4 s7 B& v( e. _% \
configure: error: couldn't find APXS9 I3 I' |" f" U7 Z
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。( @) C3 W$ L; A7 W. I- V+ y3 F& N
解决方法:
`, X7 p q; D8 _& w W" z) w0 g
yum install httpd-devel
, u1 N6 l$ W& Y6 s. a j9 m2.没有pcre2 y6 ^1 ?4 a- I% m7 A, K) @
( K% `% A% p! e& H/ ^+ aconfigure: *** pcre library not found.
0 g$ u; P8 k- M; [configure: error: pcre library is required v* t0 b) |- V
解决方法:
- H8 l. T8 K5 i+ B9 D: u$ E& x) C, U r2 _8 o
yum install pcre pcre-devel; o$ Z3 a4 b) |
3.没有libxml2
3 [3 W* o* e0 v3 m e
" c4 k5 q# j7 ^# Y9 U5 |& \% { K. Y( t7 [
configure: *** xml library not found.. O# x5 Y; I! s2 E9 x& u' Z
configure: error: libxml2 is required
( H3 `8 t9 p- V8 V8 a解决方法:
+ t+ U2 u8 Q3 O1 s; l. E Q6 u8 G% R8 l
yum install libxml2 libxml2-devel
( {, }9 ?! G1 Z/ E5 d+ ?4.执行 /opt/tengine/sbin/nginx -m 时有警告
" @ @; J, b# v, L' h% X o. N# a ` O4 d+ w
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ g: F2 y2 Q0 k+ w/ t. d* Lnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
$ m0 t2 d! A; { p& l原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log& |" e0 S" v) Q' D4 m
* s$ o) n. R; A/ k$ A# E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
+ L+ B7 L7 D( g E- p& J9 Z2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
0 c" q; }1 r5 H. K! [9 |" X2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( E+ R+ H+ d" h- m- o; q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"9 c2 U; G, a% a. A$ ]$ J* Q+ n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"3 F% b) X" p [8 `0 R `0 A* f
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
( g6 o; ^4 q k& K解决方法,移除低版本的APR (1.3.9)
- F+ I4 @4 I. J! h) a% {. }) u. K# C2 Q6 T: A$ d
yum remove apr
! A$ c4 Z0 \( F5.Error.log中有: Audit log: Failed to lock global mutex
4 b5 D% o- ]+ L" C. i3 g6 B% E
. V7 q$ Y$ W9 v2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
) U- f8 l9 F1 b) `* R# G4 jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]. p: k$ d) N, D6 i
解决方法:% E5 `- ?+ h' X1 r6 [8 m2 l9 u1 S
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:+ Q5 D6 j; H' d! v* P$ X
, v8 o2 U* u. K6 E' J+ x( BSecAuditLogDirMode 07773 `; A& H( c" q5 L0 _2 a
SecAuditLogFileMode 0550
; V7 V# x0 a, Z0 N' dSecAuditLogStorageDir /var/log/modsecurity# r" K/ e/ I& j
SecAuditLogType Concurrent0 c; s( g8 k5 @
参考文章:3 v4 w5 f( _& j6 ~9 K
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% f! \9 v- N5 n# B( ]8 ghttp://drops.wooyun.org/tips/2614 |
|