|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。, ]7 T8 X) ?3 F1 R# Y
2 B: j: ~, n c2 O& j
一.准备工作
2 g- A1 J0 O: t& |. U: o: {3 T( f3 e9 B8 l
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
) Y# L$ Q3 \" S$ q1 s! D' V* ~7 i' ? z* x+ d3 ^, n x
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
* y0 Y- R( Y5 u( _( R- r6 E
, S( x7 B, p. t$ B5 c- wmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz8 |! v$ U% n7 x& b9 S/ Z
" X: S: e3 u- l" ?: S
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
5 e& _6 B4 g! g# U; I a' s4 g
9 @' F) y- j/ d5 d2 J' h依赖关系:
6 {. \9 N: Y5 v% wtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:( m# X9 E- v: r3 }- ~1 h
9 L& J1 e5 c x( y! V1 t4 P
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
, c. B6 o7 P6 H" [7 Pmodsecurty依赖的包:pcre httpd-devel libxml2 apr
- C4 z L8 p* S9 y" ~
, ^6 L, s2 o( c4 F B8 Eyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel3 C9 ^+ E4 Y9 g3 K1 L# k% a4 W2 Y$ \
二.启用standalone模块并编译( Z6 t5 R1 }: H2 }2 N; m5 }0 b, S
" _: l* k) |/ d8 b" P* M* i
下载modsecurity for nginx 解压,进入解压后目录执行:
( x# \9 _! m& T# u& F# a( q# w
/ m9 u( L$ [! r, _( n) L/ [! |( }5 `./autogen.sh8 p8 ]1 r& F8 X G! ~! d, b4 ^4 v/ n6 a
./configure --enable-standalone-module --disable-mlogc
& M# H& K6 ~: A0 ?make
, G, S' F# ]5 E0 J6 y三.nginx添加modsecurity模块
- M* l1 e( ^7 `5 Z4 t- D" u% F
, B3 o9 B# ]5 @0 Q, W6 ^在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:+ M- N* c- g2 \/ B# B3 \
* W7 c% [+ u5 B/ S5 a
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# Z% U! ]1 Z( v( O& p! m1 ]
make && make install
! F3 Q, s. W9 Y, g( b* W9 V四.添加规则
6 p0 E& T8 X- K6 Y0 \- [) _# @
. o# D) B" r% Jmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
1 k: r( i9 t8 P8 X5 ]0 w( G
( J* |& M. ? j- t9 {1 T1.下载OWASP规则:
1 B0 ^! f* {0 I4 U# g- r+ M6 ?2 D; b. p' z: d u. f
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
8 m# i- G) U# d8 S3 n G
4 ^; f3 M. _* }: P+ _. J- Hmv owasp-modsecurity-crs /opt/tengine/conf/0 y- W |; _" l7 F8 C4 a3 Z. b
# a. x: F7 F8 A5 W+ E" g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
8 k& L9 [- a% `" s1 ^9 D( r2.启用OWASP规则:. T) k( A& d- Y+ v; h9 I% W
9 N2 q# V2 v r" I$ T
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 g) H& [/ t* `, t" K# e
: p1 o$ s( s3 [/ f1 `' \7 j编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
! K! C5 ?) }; H- R; f) {$ B
& @) x$ d7 U. g0 K4 ^. H6 eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。) o' N/ s) h/ t% h* M4 [- v- P
5 M5 `% W* n7 u* h# B+ C$ c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* d* q7 q3 A- ZInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
) c w) o/ a$ J2 o* _- MInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
4 E1 j" N9 G& g$ |2 dInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf0 C- I2 V! U4 u" M: J; \0 }
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
1 m; {5 b8 R( B: ~Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
; r0 W* m0 Z b+ G' d' E% W% C& SInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf! U9 ~* @; R4 T7 L# J
五.配置nginx( j) Y, {3 E( T( c3 Y4 @+ I0 R
& v/ b1 R: s' W# |! }6 o
在需要启用modsecurity的主机的location下面加入下面两行即可:
- N4 W; a. _. D { w7 l
/ F& M9 I9 Q3 kModSecurityEnabled on; A% o% N; ?9 ~% r- l' ^, _
ModSecurityConfig modsecurity.conf;' }; l$ l1 j% L/ Y# v: A, q
下面是两个示例配置,php虚拟主机:/ \6 K4 h, G! G5 ?4 W8 C
& g1 e! i0 c: V0 ]server {
" T; a9 V( H1 i" b5 c3 m( L% B listen 80;
B, e9 m* r& {/ m3 H server_name 52os.net www.52os.net;
\% Y }; L: A5 `# ? ; D, }) t' U! m8 h* g) U
location ~ \.php$ {
9 s& R( X* O% m3 c: [. O ModSecurityEnabled on;
- `/ ` {& `* H p- `& P ModSecurityConfig modsecurity.conf;+ Z" w; y! U# c0 V' l' f
. }" }+ k4 |+ r0 o4 o& R0 b. c
root /web/wordpress;
% q' ^1 q' ^7 j% V- R& [ index index.php index.html index.htm;
# k: E# @4 S! D9 ?) L4 e + S( q! H; P: ]& a5 _+ x# H
fastcgi_pass 127.0.0.1:9000;8 B" R- Q' {, U+ b: A# t7 l
fastcgi_index index.php;- r* @1 M0 F0 ~% e/ S) m
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
* I) |9 D6 l! e) m$ d' Q! L0 l' ` include fastcgi_params;, x5 `3 s! I% N- I0 W' M
}
; S s& z7 r) A: i y% o) H* } }
, F* A" h( K8 e, @0 k6 S/ x/ Q# Nupstream负载均衡:' Q8 h- f9 l% w. {
% o+ `; m/ u. S" I3 q2 E8 C9 [3 Jupstream 52os.net {0 Y& A" h q' c3 g
server 192.168.1.100:8080;
! g1 x$ T& [! }" b# d server 192.168.1.101:8080 backup;
9 _9 K2 T9 @+ Q}9 j! S8 K- ~. j0 g
- w Z1 w ?. S3 j1 P, q9 e
server { x! J4 F) ~/ h; L7 a. w
listen 80;7 k# y& \5 Z5 h/ l I
server_name 52os.net www.52os.net;+ J# X, h( H8 Y `
: F9 ~$ D* g& O
location / {
5 Q0 c4 R& m5 f+ Q; O: t: J/ e ModSecurityEnabled on; ' I: @ @3 \; h5 b3 E. U
ModSecurityConfig modsecurity.conf;
* _3 u# d- U! c4 W/ c( {9 X9 n
) i7 H5 X+ Y7 [0 F4 o- x' v) C proxy_pass http://online;8 U2 _3 L6 P( j3 {8 A
proxy_redirect off;
7 e$ g4 j* V+ j proxy_set_header Host $host;
. S1 I# V i- v" K proxy_set_header X-Real-IP $remote_addr;
- i1 a" _9 i& K8 m4 j proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" r L% g, j: j# \. s0 Q! i4 l
}% p( y9 W8 k/ b7 [2 I
}# W* e3 Q& s$ Y3 ?9 V
六.测试
" ^8 O9 _2 m2 q& H7 J7 t! U4 S+ A
/ ] k$ E0 T2 ?) j6 A5 ]$ V* b我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:) e0 u0 d; _' U) H0 v
) m3 T# Y: C" `0 R
<?php
: G) X" L5 m6 ^& Y- q phpinfo();
9 k( C- @/ r: ~0 [) q?>9 m6 s. }) p& c5 d* e
在浏览器中访问:1 h% P; L1 X4 U" I# s, Z7 a
( Q+ w6 U8 ~" a, t6 R$ x
http://www.52os.net/phpinfo.php?id=1 正常显示。( J! N1 o" f' J) h4 p0 @! j
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。8 I$ X- H6 D, {+ D: u
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。+ \8 _/ B+ i- C( ~
说明sql注入和xss已经被过滤了
5 F" @; u, w9 K" H+ ], @, v
: }2 w' Z; d1 c% u: K7 p X! z* h3 R七、安装过程中排错) _3 P0 B' V, J/ ^( Y3 }$ n; E
0 H. @4 }$ M& Z' U0 o' g( p" y$ x
1.缺少APXS会报错
! m5 Y! j; H. B$ K! Y) y
' I) f( r, ]* X5 L8 C. m$ |configure: looking for Apache module support via DSO through APXS
: o# y4 r6 i. U8 R# U# a9 i" ?# c4 Kconfigure: error: couldn't find APXS
' m: F) I% f5 L8 P gapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
' N+ ~6 V- \! g& \3 ^3 B( F; f解决方法:
! Y1 |7 d7 ^: m8 s. y# \; J* `) |& n* t5 T+ Q* P8 O1 E s, p5 D
yum install httpd-devel
; \& [5 h; N9 F; a+ ~4 C2.没有pcre: {7 ~9 e4 Y# y! j( c3 I
4 P4 o: P" J- Z; A v s2 Bconfigure: *** pcre library not found.
; \% y$ x: q' P/ @/ ~5 P; zconfigure: error: pcre library is required! e% _( A C5 l+ A2 S2 W) @6 _
解决方法:
/ q' B O' W1 w1 z! p; B7 i2 ?! Y5 {4 z7 `
yum install pcre pcre-devel
- C, K+ t$ ]+ c. ^1 |' |/ L3.没有libxml2
2 Z2 {6 |" {5 n3 J7 Q
- k5 j' P* z# X) x6 y; ?' G8 ]- y' S
configure: *** xml library not found. {$ q; I# u- i8 @6 Y/ I* a& W* m
configure: error: libxml2 is required
! a E) a/ k9 v1 g解决方法:( S9 L' [: z4 u6 e% d) v0 Y, k5 c
; o4 [+ L( v/ N0 @0 y
yum install libxml2 libxml2-devel; z' l! O: ? R, G6 u% s/ a
4.执行 /opt/tengine/sbin/nginx -m 时有警告; F* [4 o, e2 m w
' L) F$ p$ `6 NTengine version: Tengine/2.1.0 (nginx/1.6.2)6 p% ~3 X/ m: K) X0 M m& a7 @& U
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
% q: f" M' G, ^" [' m# i. Y4 T原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
9 b9 K- q" ^+ I6 L" o( P6 Q3 a" H1 e
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
0 m/ `) W& R9 X) W& o2 t2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 l1 y& ]9 j; c) Y
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
( x v4 D6 l4 p3 o% r$ a2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 h# s! O3 ? d8 ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
1 \4 i" W1 C7 ^1 u9 I" W" F; a* Q2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.! ~2 u/ S+ g2 y" d, l# m
解决方法,移除低版本的APR (1.3.9)
2 D, V+ e2 u* R. k. E- n+ `. _- c
' Q1 Z1 A1 D4 W( V& V- wyum remove apr
2 J" o' o+ q* h) ]8 m) B. W* ]7 a5.Error.log中有: Audit log: Failed to lock global mutex
$ ~* A& d! \! U0 r6 a6 [- F( t2 r& K$ @4 c
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock / Q m% V" T x4 I9 X
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 C4 C' m+ I5 ~% [; k" O4 i解决方法:3 R G+ G* m0 N+ b* B$ k% {
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
. ]3 z/ b. f6 i3 V9 Y- E
5 {6 V$ R" Y2 Z$ ]* |SecAuditLogDirMode 07774 T8 H# l/ |" U8 f6 r
SecAuditLogFileMode 0550! a( |+ P2 Y0 [& A/ L# v) d5 |! ^3 h
SecAuditLogStorageDir /var/log/modsecurity- u! k3 ?6 y% v5 l6 e8 }
SecAuditLogType Concurrent
0 E1 L( x, F) w. g# F4 g. u参考文章:
. g9 m) Y I5 |& j1 Thttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX% E9 l$ i7 I' T. `% o4 J/ p
http://drops.wooyun.org/tips/2614 |
|