|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
1 e$ F& I9 p1 @! j* `/ |! |& M/ z5 O5 `0 r
一.准备工作) k/ {$ d. n) d; e* Z! M
0 Z# h" N8 J' u( x; R+ e, S系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
5 E" l0 ^& p! }3 U7 j$ D% V- z* |. h- e$ f- s6 `! K
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
! u. q: W# f2 d% o$ z! ~& P' V* A9 T, k- h
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz) u. f. r) m% m2 M
4 |: j" U3 g9 jOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
' u' K) z' q8 {" q5 H( b" p
: Z/ L) [' d2 y9 R% t( S依赖关系:, |* }9 k2 R0 t2 y( r
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 V" z3 J$ {6 m5 m4 l# u, z9 r' T3 C9 H3 e3 D8 S4 j
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel) f1 b I. i% Q3 J a
modsecurty依赖的包:pcre httpd-devel libxml2 apr# ?- f. y& B( z2 T3 h
: f& R0 i$ _6 Vyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel. J5 d, f; P5 I2 Q! M% i
二.启用standalone模块并编译) ?, `1 Q) C6 {
; z9 H, j @9 [6 G, u下载modsecurity for nginx 解压,进入解压后目录执行:
$ i1 T" v: T# z" ]; y3 h; @) r6 y& d M( Y
./autogen.sh
8 S' H" _5 N4 k0 ~: O; ]./configure --enable-standalone-module --disable-mlogc
i3 V* t0 x+ f/ m6 L$ S. mmake ; R: A. `0 x0 s; C$ [2 o+ a
三.nginx添加modsecurity模块
' J& u1 _) t, ?' J
8 f, o" M( ^) U( w/ H+ p8 e; A在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
& |" v# Q" _9 z4 _) X+ L( n" H- ^9 ?$ k, m
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 r2 A4 p* y3 j
make && make install$ u! X& M; \7 j- {2 g, p
四.添加规则7 j" I% q. } o$ U7 |4 `
6 d0 E* ^0 ~' a" \" e! a5 W9 t
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。, W9 V5 W, x. P9 T
( k L& [# ?/ o. ]8 S! V
1.下载OWASP规则:2 J5 \; _% ]' Y2 u
- [5 o ^6 A6 b% T
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs6 c( c6 x# `* O5 D
0 U2 c4 Z% q. Q' [9 S5 Q$ k$ f1 v
mv owasp-modsecurity-crs /opt/tengine/conf/- i7 i3 K) \9 t- f" i# u
& T, x7 t/ G$ r" T6 c2 ^
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
3 q" f- z3 p% @/ l' U# F2.启用OWASP规则:" {/ R+ K/ }) o0 c8 Y7 \# r
0 W3 O8 X. _1 f' H5 f9 b
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。3 I5 s/ A' } t+ J. C. B/ E
) r2 z5 L' t1 `# f! e
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 @6 P/ L: ~! f
' W) M, G* b5 {owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。8 C4 c0 M1 Z2 |. q1 p
- F' ?* a4 S" Z& B9 ? RInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
) X$ I$ M4 }- o! \$ i; X" GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf. k `, i: _# q! m
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf* C! y# s- r, N5 w# O" O
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( J6 e- ?9 ^9 m' c
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf! H3 D5 L9 g' x% [, q7 S F
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
" H, x* s3 [4 b: g# b. eInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
2 O! C. P, V- K. @7 C7 t五.配置nginx
/ A4 L% X! k+ i8 e" s' b. ^7 O2 Y# E8 R7 J6 w+ W8 _
在需要启用modsecurity的主机的location下面加入下面两行即可:1 ]4 N; n) u1 {" G1 P! N6 Q
. G, H @) Q! e7 w |ModSecurityEnabled on; 1 j+ m0 t, H6 q$ U0 `, T* d$ a7 H
ModSecurityConfig modsecurity.conf;; Y6 I# i; U# B3 D! F
下面是两个示例配置,php虚拟主机:* i2 H$ ?/ S' t6 E
) ]- e$ l8 A# I4 Q) lserver {$ b' j r. u2 r- W& g0 E( C/ g0 V
listen 80;8 E9 G' d7 B" i4 H1 Z1 \; P" ^
server_name 52os.net www.52os.net;, l1 y2 G) b; O4 W# g: E
/ l+ V4 r+ S% N" q0 `7 m/ E U location ~ \.php$ {
; m3 w2 Z v( |' q% s% | ModSecurityEnabled on;
0 D( p1 a# d' M3 Y- j ModSecurityConfig modsecurity.conf;
$ m1 n% a8 f( w/ q" F, y1 m* X9 l7 e; M( k
root /web/wordpress;
$ h5 d8 A s4 g1 \, k! y/ q: _ index index.php index.html index.htm;
! x5 G. x3 g1 l3 M/ x B
6 y4 n* N' z% M; ^8 P. {2 A; V fastcgi_pass 127.0.0.1:9000;& n, b* `3 j# s/ b9 b0 F4 ^$ @# S, o+ J
fastcgi_index index.php;
' I C: @, [; ~3 s) y1 h2 X fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' f& B0 \, S1 [" ?! a/ x include fastcgi_params;+ d# c( O0 N/ x& O( a
}( D0 o1 A: j+ r: {$ j
}
- K v6 \$ M4 ` j0 F/ Y) _upstream负载均衡:
5 t' b; Q% I& T2 `* M5 f
' z' U1 r9 S) j, dupstream 52os.net {
" `) B' F3 p0 M& O server 192.168.1.100:8080;
$ u' z: g7 l2 |' Y# l# K server 192.168.1.101:8080 backup;* U6 N7 I. R2 Y
}
. r! V% A' H; V6 g
) ^9 X& r1 s# A! C7 q4 g% rserver {( l7 a9 z \* r0 l, N" I
listen 80;
/ S" \- Y% `6 P+ v4 F7 Z' Z; Oserver_name 52os.net www.52os.net;7 n! e$ z3 G9 D+ j
, a' o" u/ f. U# D
location / {. c5 Y+ P$ x. i u" g: B
ModSecurityEnabled on; 4 X! g" I+ b: X4 k; `4 [7 x. E
ModSecurityConfig modsecurity.conf; - l4 H+ @/ y( M7 ~% ^# I
+ D3 Z8 r7 z' }& r5 j
proxy_pass http://online;
! }. m- m/ n1 s. T2 x proxy_redirect off;
: T1 _+ b8 H- K U9 }# i. q( j proxy_set_header Host $host;
$ C! q1 O% Q% S% Y1 J( g+ F# s proxy_set_header X-Real-IP $remote_addr;
" P6 r( ?# Q K& Z8 o proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;/ ]! ~% o: H7 n( k4 b1 X
}! F. t5 X1 [) U! ^, F* N
}
; Q. n( n/ Q0 T. g* n4 t8 ~六.测试
- A) f O8 F) g. _! H
' `' v7 d9 P3 ^: r2 N- N; B我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 _4 `6 D! Z* A G' c6 `, G* n& R
2 ]# |) v6 M% o, r- k& \" Y, N<?php
$ P# C- b% ~. V! Q( n# ~2 H phpinfo(); ; N5 b8 ]' O$ n* g
?>
! f8 `; { b, L9 O# ? e d. }在浏览器中访问:
1 X9 P+ i! O* s; K2 C7 t% P6 p1 Q3 O4 T" W# n1 E% V: N9 Q, ~
http://www.52os.net/phpinfo.php?id=1 正常显示。2 G0 ]/ @8 H O6 t4 n; e( g
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
% ]! W) u( g7 l) v9 G1 H0 Vhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。2 c0 G# J& z6 K0 R" o: a
说明sql注入和xss已经被过滤了
$ N+ f0 L1 U* D' ~ K6 a
$ c- u6 a. Y2 E七、安装过程中排错
4 W6 P% \4 k, \9 U( A0 n2 L/ ?) c6 ~% Y
1.缺少APXS会报错
% q" T9 @/ U8 u0 u4 @- K4 w
5 d/ J! v5 ]) N Wconfigure: looking for Apache module support via DSO through APXS: p6 [$ _' d5 s X3 @4 F
configure: error: couldn't find APXS
7 }4 Y! Q" Y5 u, m/ |, uapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
, r$ H' [6 n. U0 P解决方法:. f7 O m" V: t+ Z. ?/ T R8 o+ w I
0 M# y" ]6 o% T8 Y2 S6 ~) r8 m
yum install httpd-devel. Z! s0 y% F. J B8 b. \+ ?
2.没有pcre
9 i$ B3 W/ Q* U, O* C% k
/ e) G V& [ m# ^' ~! a" dconfigure: *** pcre library not found.5 s. J: p( {; ]
configure: error: pcre library is required: \9 R; @5 S' C! H/ I
解决方法:
, f) D# ]6 b& L# n5 f8 H# G+ a8 v, c; M7 w: a1 Y" N
yum install pcre pcre-devel- s/ s5 N; d# A6 }5 T) }( a
3.没有libxml2
$ x7 r" d: u; K4 T/ v, n- N6 Q( r4 d/ R' { ]
. ]3 z. v5 r4 l( l1 `
configure: *** xml library not found.
8 r& ^4 U2 V+ t3 o- ~* m3 ^configure: error: libxml2 is required
2 a0 l) c: E* K解决方法:8 g" {) v! |7 s# d9 b* u X3 U
+ i( r- W: B2 `* Y3 o2 K
yum install libxml2 libxml2-devel+ a) u7 Q& v& g" o1 a
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- n( Z2 r+ U! S8 P+ o* R+ a
3 Z# H) [" i2 ETengine version: Tengine/2.1.0 (nginx/1.6.2)
/ ?1 D- l' E3 s' l" f0 Dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!- b" _. m( H6 V) t T( y) \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log9 w9 j/ P+ o! k3 a7 z
, ~/ Y" O4 D! ^& w* i1 m
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured., c- Z( y) @! ~( G, w4 n3 R1 E( _
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
3 M; N: I( Q/ N9 ~2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 ^* A9 L! a( F' B6 O+ g2 J8 D2 H, h
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 Z$ s: f6 I; g8 Y- e4 l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ R/ w: w) |3 j O- F) l* v
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
) f% ^: ?/ f6 v- [. A解决方法,移除低版本的APR (1.3.9)6 m j( e9 @1 [# R; R
4 A, c/ Z; R: U$ Z. Zyum remove apr
3 R* ^/ X" h C5 _5.Error.log中有: Audit log: Failed to lock global mutex0 F. e0 o& q+ [2 u$ g( n
5 B7 v1 j" K! }: y. b0 h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock ) v& l5 D1 g* f( g0 R- X8 C
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
4 c5 _# |& x7 |0 ?( c Z. @9 N N解决方法:
4 N/ j; \" L7 U* K# j6 E% d编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:8 N& d# s- [! k
6 S9 T/ j* s( C: N) I: u! O: C3 zSecAuditLogDirMode 0777
& R/ T2 T; E! {# [/ @SecAuditLogFileMode 05501 p% q/ f( Q* G9 |$ j" J
SecAuditLogStorageDir /var/log/modsecurity
* b+ l* {5 L0 y2 lSecAuditLogType Concurrent V* X, y& y: Q) Z# m0 w
参考文章:
2 u, d9 K0 H6 D: Xhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
: v* Z4 \- O7 g$ i S: r: Z6 H. ]5 Mhttp://drops.wooyun.org/tips/2614 |
|