|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
- ?% k% S9 R3 K) c, m9 y' X- E% ?$ F+ Q3 E4 Q! c' x2 t* i) k
一.准备工作
1 b* `- ?% D! g$ h7 `7 O& `- K- h% L& L/ B+ P; p5 }
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ d) D. i. f9 f6 A4 ]* b
- |$ [+ b S5 X9 @7 L! Utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
0 W2 `; @1 ? b) S& d. \( G* l. l, y+ _ ~
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) |( L4 [9 |5 I/ m4 Z$ |2 \7 X' D
) J8 a- [4 c5 }* [' j3 ]OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
# C* N" E! L: Q0 D5 P p y* N7 v; j' s C
依赖关系:
5 P4 r1 [3 R' m9 r2 f! ]tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:, Z1 h# a( j+ K9 `
' q. c% e, J$ [) h7 e% Q
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel* w# N& z& } Y8 m/ ?4 q: u
modsecurty依赖的包:pcre httpd-devel libxml2 apr2 C; G4 Z0 G8 w% n$ y8 ? K
0 R+ ~& C- r. Z7 |0 O& t! qyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel, V1 X7 y; h% q6 u1 |. p: C
二.启用standalone模块并编译
8 D- u5 x, K8 Z! o0 G" \8 R
9 G: b% _5 @& V下载modsecurity for nginx 解压,进入解压后目录执行:
$ ^* l' d5 T3 }' H+ V( \( E) G3 f( s& a! _) g
./autogen.sh4 }9 m7 o$ H' H# U* ?
./configure --enable-standalone-module --disable-mlogc
- G' U" q* y) |0 s8 j5 Dmake
1 {0 Z" w S' X, r7 ~# S三.nginx添加modsecurity模块
( d' }6 N/ ?2 Z1 _/ e" G3 [) e& r1 K
4 n9 P6 \9 e' U- I m) S( R在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
D! d3 V3 }' u1 b
' I2 ^* U2 ?4 K6 F./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 C! G: {5 L. u9 {/ @. Hmake && make install
- P4 m8 b4 J8 |3 E; l四.添加规则$ }- T! v/ U( \
: g% w' q" L" P9 z5 e7 p6 smodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
( u. u8 h3 @' U
; F+ E1 f5 [5 ]7 u! w7 G1.下载OWASP规则:" ?6 M* O X8 l. m0 q
, X! j. i1 G5 f; U2 m
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
3 \, Q- E! c* R) o) ]3 V& P- x5 F2 G# l8 A4 s' X: c
mv owasp-modsecurity-crs /opt/tengine/conf/4 l2 i* l! b( B* H- v# q0 R
, b. l9 A- ~! ?0 Z+ Wcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf! [4 J; l% j) D: y
2.启用OWASP规则:
. A) w, D' P! ?+ u8 T( |3 n, G! |- ~* O `1 p
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
4 F4 s- l) l/ [! Q. }* T9 p G# k! ^- w
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
+ B9 g- L) ?7 u# K; m& @% v% A9 H
' x5 e/ o5 D; H# y9 w1 qowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。" l4 s( R% B4 Q3 o/ ~* i
& M6 e( O8 g8 l2 R) @, w, N
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf k( n6 b! l5 s* \1 J' |
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& q; h+ i# @: @7 _
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
n; C/ ^( m+ eInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( ?: x. P! w& l7 I1 Y
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf, Z0 ]( H9 n ^5 h! ~8 T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
7 { V; g; F9 h/ E( |: V9 P. Y7 dInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf8 C6 G8 U9 r4 m
五.配置nginx
) }, {: b1 W4 L3 l2 o
& l- |0 ?) K5 B' ~在需要启用modsecurity的主机的location下面加入下面两行即可:+ Z( k$ U5 z) {& b7 O. q
2 i/ K8 B3 l- ~* s
ModSecurityEnabled on;
5 ?/ d0 C9 G% qModSecurityConfig modsecurity.conf;
+ d* ]$ c* M, E0 M下面是两个示例配置,php虚拟主机:- o7 _, G7 Y5 a7 Z
$ y7 j. w8 o3 v& {8 L D/ y
server {4 i) e# D: J6 x3 l/ p8 u
listen 80;
% V( }8 b' [7 s& r server_name 52os.net www.52os.net;9 |$ R0 J, ]! b+ w9 g/ Y T, v2 ~
7 U6 ?6 \+ w2 x location ~ \.php$ {8 _9 \) g$ p& N/ S6 M4 e
ModSecurityEnabled on; : D# V4 P, o8 u- J, a
ModSecurityConfig modsecurity.conf;
v( Q6 }" M% a u: x
8 ?+ ^8 Q9 c0 R( S( ] root /web/wordpress;
# O' X- D* {" B1 Y& T index index.php index.html index.htm;* ?8 Y4 d0 v& V8 f
8 O& `$ N1 c- @# i# f
fastcgi_pass 127.0.0.1:9000;
7 y4 h' K M+ U1 ?+ r F fastcgi_index index.php;& p' w) t2 `5 T; w: `( B. y
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;+ ^4 A1 W% S8 _; a
include fastcgi_params;
/ j+ c* R8 Q1 h! N+ S }7 E" ]7 v% K; T1 B6 w9 G4 _7 j' f
}1 V& `5 ~8 T* a% a5 S
upstream负载均衡:" g) _" X1 J7 b* B g# H S$ R0 d
: \ S7 n- Y9 pupstream 52os.net {
* V$ r" `. J# L5 K3 Q( x7 v. h server 192.168.1.100:8080;5 f: @3 V- @* V/ C# j9 G4 c+ S
server 192.168.1.101:8080 backup;) z# c, o9 a# H& u9 {
}0 y' u- \. v3 [2 O! P1 J3 A) s( A+ X
' o8 L \0 j+ M) B, j3 n4 ]; d
server {
# c1 I" k+ \# R7 E) `% v7 |listen 80;, z6 j6 @! B- k( A. j! b0 X3 e
server_name 52os.net www.52os.net;
& d5 q5 U1 \/ a4 M: T" n9 s+ o" H/ c8 r+ i4 U; A) ?1 T
location / {2 ?1 |/ d7 L' t: M
ModSecurityEnabled on; + n, [% W8 h6 D
ModSecurityConfig modsecurity.conf; , _& R( j& n8 A
. |) ^, n X5 p+ Z9 O& X- W proxy_pass http://online;
9 @* [; ]9 Q& ]4 O! Y! { proxy_redirect off;) I" E7 w5 L: D3 G/ O7 S9 H+ T
proxy_set_header Host $host;; Y* R" n% W* m3 P, n' C2 @
proxy_set_header X-Real-IP $remote_addr;
/ [6 Y: c5 v, _" I) f/ p$ W) C proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
$ k" ]# \5 f: @6 B& t }3 b7 u8 S5 f# p6 y
}& U( A- B) O# |7 ]5 I9 d$ n
六.测试) `* N/ J2 M9 c% p0 X) P- Z1 ~' N
) T# c4 D0 s/ ]; ?8 T1 r4 G" e
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为: l: T0 a0 P( g+ R" ]" i
, g! b7 \) A& w# e k4 x; e1 H' [# j<?php- |4 Q( b# b6 O4 z! ~
phpinfo();
: k, A7 w, C( K( s7 o?>, K8 [8 y2 ^6 o
在浏览器中访问:
. x* B7 e4 w/ B. P v0 Z! ~* W% P" T5 P$ R. t
http://www.52os.net/phpinfo.php?id=1 正常显示。
( l B, c% ]# z0 phttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。. [2 M/ A5 q8 L& D
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
2 |+ E, n( E7 X+ l说明sql注入和xss已经被过滤了' S- s) L5 Q h2 M% Q9 H( O
" P1 Q- c8 x7 z' `0 C
七、安装过程中排错' ?" n3 b' z; A
( [4 U4 v. v6 K5 g1.缺少APXS会报错" ~) C& X } b- f1 K; {
) N( j0 F: e5 Oconfigure: looking for Apache module support via DSO through APXS9 z7 `3 }2 B6 ^+ @' m5 P0 h
configure: error: couldn't find APXS
4 c2 P. Y" @5 }( T/ j h" Iapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
# G; }0 g# D0 e% f+ R# [7 F. f6 M7 y解决方法:
- f8 f k( S6 U
& i, }% J5 p/ Z3 gyum install httpd-devel9 w- K3 X) v, r
2.没有pcre6 q3 I( _# D' ]1 q% t& [) g/ U9 @
( m. {; T% _/ q0 G0 b) o6 X% W
configure: *** pcre library not found.
. c( | W& W+ {configure: error: pcre library is required: E0 P: e0 H$ ^. R S
解决方法:9 d4 o' j$ D! H
* P% P, H5 i9 G% e' b0 s
yum install pcre pcre-devel
( R. R# |6 G- W9 l* H6 S3.没有libxml2, |0 b& |' J$ i2 f& c7 c
# M* n, ~. l. i" n1 S& ?9 j/ o' g- I6 w# a% ?& c. j; z3 a0 S% ~! c: n, X7 p4 a
configure: *** xml library not found.( j3 d/ d" {) x: d0 w- p: \- _- D
configure: error: libxml2 is required! N) p6 m3 u' n4 u' u4 T
解决方法:
" F! C$ F9 C8 U$ v9 i( q3 }
% ]" N- t" Q9 m% a$ R$ r+ qyum install libxml2 libxml2-devel
9 m9 {/ `; m8 [0 }2 J4 l y4.执行 /opt/tengine/sbin/nginx -m 时有警告
4 Q8 W) x# r. W$ M$ }
; c J4 k+ c. I4 `. U( ETengine version: Tengine/2.1.0 (nginx/1.6.2)
/ ^& _0 E5 [7 Y) r% knginx: [warn] ModSecurity: Loaded APR do not match with compiled!& {6 N) n! y! d; k: m& d( j6 E
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
5 J8 _3 Q5 s+ ~
. G: k9 B7 h, D; B Q0 L+ S. p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
; P6 J+ h# C0 Z% X5 P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"! P6 n# j' t2 H+ T& C: G' E
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
6 a) M; u2 a# ?0 j8 X0 P5 E2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05": C& l. I* ^- `% i4 Q4 k7 |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 R! J- h: v, L U, r
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.; f; m7 M1 C7 D: I% z; I
解决方法,移除低版本的APR (1.3.9)9 V0 a% f& H9 G; `
/ ^0 `+ E7 ^% A8 H: }8 x: uyum remove apr
% ]% |+ T2 |. B s p1 X) v1 H5.Error.log中有: Audit log: Failed to lock global mutex
, \9 m* U# V( ~ j8 ~. K) E/ N' I' I9 C
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
: X% m, ?7 R, @9 {6 C3 {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]2 d7 d$ M" N' [7 Z" E
解决方法:
8 ]. k4 T, j6 o& e编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:( R1 W$ h2 j e* ?$ P
. H, Q2 k9 `9 _
SecAuditLogDirMode 0777
' M+ ~. ]/ Z. V* z" s1 g/ K7 T& K4 A& wSecAuditLogFileMode 05502 h3 ^# s& l& @% a9 A6 |* m
SecAuditLogStorageDir /var/log/modsecurity% J0 d, {3 P3 q. u8 V
SecAuditLogType Concurrent
1 G2 d' Q! w9 L& \. Y参考文章:
- I! n2 ]% c( n- whttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
O8 g1 c" U- f& f, g1 q7 E9 Ihttp://drops.wooyun.org/tips/2614 |
|