|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。9 N% \' a5 P/ C' P+ c
3 b! Z6 M! _8 C: b, y s
一.准备工作
! x+ g# o/ K: ^7 p i
$ H7 a4 _2 v* L$ s6 u系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.09 c: m o$ t* D, P1 H' X; x! V: U- ?
- v i1 L& v, M( o5 v) i, O. x4 p/ [, Vtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* q$ R, W; v1 {" R# y# b
1 n6 F7 @, y6 _- qmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz2 y: V* N3 w6 c$ ~& T% K8 b* M- J
) S) @6 e& A) l6 L& Q- JOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 c# v/ T$ J" W# N0 z& E, r1 m# \1 b
$ t; |% ^3 F7 D/ ]依赖关系:
* m6 [8 e$ F, I0 E/ K @tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:! K( h" f. F$ e' U8 g$ D& n
0 w/ C, I. a1 V" O; o4 ~9 e1 k
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
3 r3 \6 a Q6 ]5 [, d0 X% Xmodsecurty依赖的包:pcre httpd-devel libxml2 apr
( P S4 ]$ e7 |$ M4 l; H2 s9 B% X$ ~: P3 h4 [3 R# f, f
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel& L& q) ~- ~* f5 ~9 `8 v2 e2 g* k" x
二.启用standalone模块并编译7 r* e2 x/ n$ {" n
5 _* G4 q2 c' s9 h
下载modsecurity for nginx 解压,进入解压后目录执行:8 Y, R o4 A& { n; @( H+ D
. ~# W+ u! V) J( T./autogen.sh7 V# U8 ^$ Z' F+ v
./configure --enable-standalone-module --disable-mlogc8 e8 o& P: L9 f# r
make ) y- g+ j C0 Y5 s+ M
三.nginx添加modsecurity模块
. [& Z: L: W, o* I2 g
2 a: w! Z6 D- c* q在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:. ~! u7 C( i. L5 g) u% }
; F: j# }' \! f5 q4 T./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine% l) i- z7 a3 ]+ ^% G( }5 Z8 B
make && make install
* o$ K7 F3 ?9 g5 F5 G# @四.添加规则
& a0 {; r! D) i' G- ~% y8 p8 v1 k, S+ f' L; I) |; I) n
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
7 [3 _3 S' g6 I/ R0 K. o y7 S3 W8 j+ e. x2 i3 l5 |5 P7 Q9 ]
1.下载OWASP规则:) T8 I- C6 r5 X- K7 \ r8 P& a
# ~; {* c! o/ \+ q: _
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs F7 }$ w( r$ J. x
' j0 b$ L( T/ Y8 {/ B0 l4 rmv owasp-modsecurity-crs /opt/tengine/conf/( i. K! a* \; J' a! v7 G
3 E7 d/ `8 u8 g1 c% d8 z
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" F3 F7 y+ e f! T1 O5 t! K9 z6 P2.启用OWASP规则:% f) R: F9 X+ g0 f% g
# Y9 n9 w1 n6 R! @1 S8 ]8 [
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。; u/ [8 q( }+ C+ Z( A
2 w3 v9 s( ~+ x& c6 y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on+ d; M* Y, g# x9 H
8 X7 d$ b2 B$ D5 Oowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
3 n2 N, A& ~1 g8 D5 K; E* ~5 a( a$ u( {3 a, g* l% h& l
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf$ Y4 z5 j+ S/ Y: F: q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
1 b& a1 f% O* e; U. uInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
* o/ ^6 v8 t k0 f; MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
5 \7 Y* Q7 _* l0 QInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
3 C+ B6 o, p4 g+ I+ i" gInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf( A8 P/ u5 g# D+ u# h8 U" Q
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 |5 I$ G k5 D( M; w* G2 R; s五.配置nginx/ \/ Z# h& V0 Y8 t& b$ ^; T
. a ?/ Y/ g$ T$ G$ G9 d8 T
在需要启用modsecurity的主机的location下面加入下面两行即可:
& `1 R8 O) G* d$ f* g6 A* S/ Y! s) o0 q- K$ v4 P" |
ModSecurityEnabled on;
; P- J- @+ I5 M: b& v) k2 \ModSecurityConfig modsecurity.conf;
, S- y! X3 T5 t1 J3 }3 X3 ~下面是两个示例配置,php虚拟主机:
9 {$ P% z w+ p9 _% b5 d6 d- h, o% l& f" D& r7 O
server { l' d& G% \0 C# D% y, J5 _/ c
listen 80;( I$ x: P* p$ {
server_name 52os.net www.52os.net;* o4 y E" I8 Q, |
2 F4 h- u' b( U; u0 R0 ~$ _! ^: F
location ~ \.php$ {
k) Z3 b7 `8 H, Q# ^6 _( h2 z ModSecurityEnabled on; 9 c$ \9 v' _3 v# _# T9 R
ModSecurityConfig modsecurity.conf;
4 |$ F* n+ V- k# G
8 M- ]; D2 x: P& J; o! k root /web/wordpress;
* n" e$ F8 C5 [5 J) X' n) Z) [* V index index.php index.html index.htm;
4 a6 [, O5 v O5 P r8 d 7 z( S; b( ]# H
fastcgi_pass 127.0.0.1:9000;1 z5 ?& C0 W' j
fastcgi_index index.php;
6 P1 x2 H; m" Q r fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, P( f' r7 b+ v/ R5 N7 p1 k
include fastcgi_params;- Q- [" m, `% u0 N3 @1 `
}
4 Z- g; |" m- J h }
. C( e1 s. j- k. kupstream负载均衡:, V; }" q- G5 C X# I+ w
8 n! Q: D9 B% |/ Y0 V1 ~2 y: r
upstream 52os.net {7 x6 o7 n1 M4 e* ~$ H
server 192.168.1.100:8080;
# t5 I( t- M* e; e server 192.168.1.101:8080 backup;) L' w2 S0 o' l' D6 {& X
}. L8 S9 \7 Y2 F3 z8 y: A( d
+ H# Q9 Y" b3 g! n& c$ C; ]6 oserver {- m6 z1 F3 A: \: c0 t6 w
listen 80;
9 X- i4 V: r) aserver_name 52os.net www.52os.net;9 L' U5 l" }+ X' `3 c* f( m0 U9 r# c( |
# j- I) Q+ S; r' g0 x- \ qlocation / {9 V$ _; O8 d# J4 P2 C. E; {
ModSecurityEnabled on;
& B7 H( c7 X/ r: T3 D. r ModSecurityConfig modsecurity.conf; 1 ~+ s7 U/ L1 }. o9 @
6 U+ Q& O, a$ F; {# q ^: k9 F4 f0 i proxy_pass http://online;
% O4 |$ q8 w. Y# P' {% L# T proxy_redirect off;
( @; `2 e, K5 `8 ~, d; f) k proxy_set_header Host $host;
# Q7 v; m$ r. U proxy_set_header X-Real-IP $remote_addr;$ T6 _) }3 K1 r5 S f# L5 f- _
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
& g* ?0 p/ w7 [( y( d) w. T r. G }
- }8 ?: [' Z5 N}( W3 m% ?7 u3 a% X6 s _. c
六.测试/ B$ A2 s7 I8 [, x5 @' E2 d# |# ~
# l5 Q% C, D+ z! h' p0 _
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
3 T/ b# ?4 ^% f3 C7 d' h
& H* t y" a3 R1 O/ G8 ^2 s, a9 }<?php* D2 `& D6 A' d/ M
phpinfo();
9 |3 V- ]$ m' T1 R8 E?>
+ @9 H1 H# o" D; w8 s' |在浏览器中访问:: w/ v2 ~, J! | s$ N# {
8 y: C6 c P' i$ Z4 n
http://www.52os.net/phpinfo.php?id=1 正常显示。$ P' K+ {) P! p# ?3 t# v, | Y" C
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
N/ e7 V* b5 s, {4 Qhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
5 s& p% a* u8 Y5 W- {6 R) l3 b说明sql注入和xss已经被过滤了
0 T F6 I" \! {. j8 I) E6 n2 H# s Y3 v9 B9 G) q+ u2 J. B
七、安装过程中排错4 ^- g* ], b9 a/ H$ F. M, F
' F3 B& r) L! I' o1.缺少APXS会报错
# z- I2 |) u& C, R' R9 ]4 J) z0 m4 V7 S* h' G
configure: looking for Apache module support via DSO through APXS4 o& s2 S& ^: j% p7 _
configure: error: couldn't find APXS; _6 V# J( O O2 G) z& Y$ F
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。/ I+ S$ a5 f& G$ P- Z
解决方法:
6 K1 ~5 |/ H* W$ U2 P8 ~( ]1 I+ b' a
yum install httpd-devel
/ J# R' o' q1 T& ~& U# F2.没有pcre2 K4 j2 a' N- m2 K
( Q% p$ X# L V* g+ M; k
configure: *** pcre library not found.: J3 ^" F. u2 b2 Q! g6 }) r% E2 L
configure: error: pcre library is required
& P+ ] [) f. t解决方法:9 c$ b$ A8 r p5 f5 O8 Y8 {6 u
: u$ v, ^+ q, d! L3 h5 n
yum install pcre pcre-devel
' ^; P& h1 a# z0 A+ z, Y3.没有libxml2
( b# H v( T7 q8 E7 V! S0 d! \0 s+ S3 N. Q
: \, T5 ?- `' h4 ?4 hconfigure: *** xml library not found.
( n6 S. H. S* a$ l4 h8 z: X- }configure: error: libxml2 is required; x: A' I4 M! p* @+ T0 I
解决方法:3 q" n- y8 r' |1 m+ q
0 [% v, ]; K4 ^: i; E0 E. ~
yum install libxml2 libxml2-devel2 R" q+ t. K% r2 F# w
4.执行 /opt/tengine/sbin/nginx -m 时有警告
$ Q2 V" k7 K: d- [/ K+ x' x8 v7 q: [2 L- V @
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
. ?0 {( y# j% k( w; W& Tnginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 Z. u# q) G# {5 Z9 t+ o+ W1 O- s& [
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log; C$ ~7 Y3 ?$ d
! v! t w6 y0 a. k
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
* {5 }5 X& u: |; h$ _$ f, t& ~2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"" M# W: F3 F; N, g! \7 a
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!! [. E, o& H* H1 H
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 _8 ?7 Z* c' v$ O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
$ C# x/ ?1 t: o T% K# C2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.+ p$ n' j5 w' a' g9 U: Q
解决方法,移除低版本的APR (1.3.9)7 W2 C& o7 y# g4 o4 V$ B% Q1 S/ W
3 ^5 `2 _" R" t' G
yum remove apr: `% [ ]( f+ l: L8 Y0 Z+ _ K
5.Error.log中有: Audit log: Failed to lock global mutex6 ~7 y, y3 I. R, x
+ y3 t4 p4 H! ~3 S! H$ O* _
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock $ j4 k* ^6 d# p ^3 I4 D0 S
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
- \2 v- X9 W+ D. |1 w解决方法:
: C$ ~' z& W4 o; C编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:5 o& u! x9 ~# N O1 c
" j& k2 y0 O$ b5 {; M
SecAuditLogDirMode 0777
. c' P0 J. D8 G- ^' L# [" v& vSecAuditLogFileMode 0550
& O, F$ |- j/ J1 o# C/ USecAuditLogStorageDir /var/log/modsecurity t- O# g u [/ a# I) {. v, n
SecAuditLogType Concurrent9 A# S* |3 e& M0 V: v5 t
参考文章:
5 a5 t3 |+ s2 M Q- P' j- U# Ehttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX7 L6 m. P# a7 q
http://drops.wooyun.org/tips/2614 |
|