|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
. m3 y" [$ _# K9 {; ?
" {: Q, m( z) v) ]! W" _一.准备工作
0 y- E( G7 }9 k# a) ^. @# ]7 i l# | p. G$ S4 ]
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
. @ ]# |7 |) ~$ B6 V
' V5 N$ t5 s3 \+ a Y7 mtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
& x6 v. P& ]! L; Z, A$ v* A! O6 x
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
& z9 O% P7 h( z7 B# s5 L; U, _( `" e; d+ S8 i. A5 x
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs. g+ R" L$ j, V0 C8 H
B( x( K& p2 g* ?5 [# Y
依赖关系:* g) E0 Z- \7 o! C g: Z
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
' T) y( `6 H. G5 N& d6 J; C8 P3 U
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
4 M. S- ? q: K5 `modsecurty依赖的包:pcre httpd-devel libxml2 apr4 [- j6 @) L# F: i O; K
Z6 G+ b2 ~' S. I8 u/ Z
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
/ e! Y# C) f. e二.启用standalone模块并编译
9 L7 S# P* L, S( J- l( @" G, O7 N5 _$ W! l) e' a
下载modsecurity for nginx 解压,进入解压后目录执行:# l$ i& ^! s1 e
$ I' T* a- g2 h& J( t
./autogen.sh
3 `' |" @/ j f( }0 C' _' ^+ @0 Y./configure --enable-standalone-module --disable-mlogc
( K* z, Y; ?2 V6 @1 Bmake 4 n. o( a) F/ B6 C
三.nginx添加modsecurity模块' d5 {# Y( @, N1 d/ I9 P: i
# u3 S6 {( o$ u3 d# m
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:1 w3 l7 }$ k0 c
6 \6 N4 V* G1 s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
. b. Q$ A) B! e8 p5 J6 Tmake && make install
5 f, T) {% |/ {6 |1 J6 ^四.添加规则7 Q, m: k& y- X0 o; h) k
# |9 V( e9 `' M/ U9 p* m
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。: h* C1 I- S7 `& i
d& M; m9 r( H( d& L
1.下载OWASP规则:# J: _: \8 V% |" g8 k
3 X6 d7 L, G, _0 f t7 o7 {5 Sgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs5 [* q" s1 |: {& y
# J& D, c; P* f8 f$ R" S8 X1 K) [
mv owasp-modsecurity-crs /opt/tengine/conf/
9 S) E" A( }/ X$ J" |1 F2 L8 _3 L& M0 N/ V
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
' t4 b, ~: A- @# u& A. V6 Z2.启用OWASP规则:) Q6 X7 W2 g& S9 q
" T9 g4 _0 H( f. l5 G7 P" g复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。& P7 h' ~( M7 a% G; U0 N m4 p% l1 I
+ c: D4 j" h. A. G, x- b% b# ~编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
$ f$ o M- E% t; N3 J, V3 S
8 f! J' R6 I2 w$ S# }owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。3 X. k0 R0 Y) a7 i( D0 m
# V5 G. o$ o7 c
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 J) P+ P3 o% v! D) IInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
2 E# ~' n1 `' y( h/ O& ^Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
- j2 w- V u: w+ VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
, m2 K5 b( U0 m9 d6 X1 d, V/ w% ^4 sInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
) \8 U, k4 Q: C! ?7 ^3 A: bInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf. I( S' `/ I+ n4 Q7 ?2 [7 X' a; u
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf1 Z0 x. O4 O0 {- K
五.配置nginx
7 w- K: u. n5 P0 o7 w
3 c! Q# m/ z. u" u r6 L+ `在需要启用modsecurity的主机的location下面加入下面两行即可:+ h! s+ I7 u4 h& I: y+ c
! N' X8 n9 i9 A- G/ |* k8 b) rModSecurityEnabled on;
' n* x5 Q& k2 F" [+ w( UModSecurityConfig modsecurity.conf;4 v+ g) E& }$ x
下面是两个示例配置,php虚拟主机:4 p. Z2 \: F3 v8 G' V
& D. x1 |7 o8 C# S. x
server {# ]& g2 v5 w; G, m Z: M6 p2 Z. p! n# H
listen 80;
5 D$ A: h) G! R3 h server_name 52os.net www.52os.net;2 T7 B- R* U5 B7 }" A1 V
- H/ Q1 ]0 J) V" [! ?( W location ~ \.php$ {
9 y. v* Y2 ^. {/ p7 e5 @ ModSecurityEnabled on;
) B( h1 e; N. h6 \ ModSecurityConfig modsecurity.conf;# n! ]" _) i( T9 f! Q
. o0 s7 b- _- U: |
root /web/wordpress;
& I$ e3 l/ R. n index index.php index.html index.htm;, y* k+ [( _: ~. }3 {
* G* M2 ^, `3 i- R( ?, F
fastcgi_pass 127.0.0.1:9000;
5 M% S. ?0 x8 n' f- h. U8 r: r fastcgi_index index.php;5 s5 D) C( d% X i% ^1 v
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;# B7 z) M. M+ F: P9 `7 [; E/ g* w; D
include fastcgi_params;' X% [+ h) d/ b- N, z. v' `6 p/ ]
}& H- Z4 F; d. @% I% b. p3 \
}$ X: \) k1 V6 i! Q4 C$ T
upstream负载均衡:
* v' }- }* K; |; k* i8 L% W
( [8 v( ?3 T D# J1 ]2 n+ [upstream 52os.net {1 b3 A3 | v1 c2 A; [& |" j
server 192.168.1.100:8080;; i+ X( o1 k7 W J- Q5 r
server 192.168.1.101:8080 backup;
9 j/ K7 G) f5 c C+ R. `}
, i( `- f. n8 r6 ^' @3 ^4 l p& d
0 q; S3 k- o+ a. f% Z3 _server {2 L% R0 [# O5 {( }, |/ f, f
listen 80;/ }% h% G3 m) T- z9 l M
server_name 52os.net www.52os.net; y9 z Y6 n1 K B
R$ Q: x* K4 R$ T& i- r
location / {3 t7 v/ t" O/ y. o
ModSecurityEnabled on; & P; k$ q6 S O1 y! v
ModSecurityConfig modsecurity.conf;
4 e7 [4 Y* ~$ v$ ~6 P+ C9 C& M+ Y% }0 S( s, i
proxy_pass http://online;2 |7 Q$ ^$ G0 V
proxy_redirect off;
9 G |; y* L! O/ P. L- H proxy_set_header Host $host;
0 D5 \4 s( C, }! e' o proxy_set_header X-Real-IP $remote_addr;% Q1 j/ W! {" @7 f4 C4 t
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
8 ~& W7 f) V0 \) p2 M! A/ J9 x }& x0 L* @' I) M# t0 }$ e$ E/ D' ^
}$ Z% I \3 }# A; Z1 I2 [
六.测试
% f' I( B8 e# B3 B3 R. B' v6 [( k0 C; M
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
! a+ v1 @2 z! M, P: ?9 ~! i' U3 s/ |" ^+ e, |
<?php
% U! G, ~# u( D( G7 x phpinfo();
! J, w/ p" \2 u?>
$ \/ R/ Y. i* c4 x( S在浏览器中访问:
) O8 R- D, p0 p0 K' u8 o+ f$ {* V/ G% ~: c" G
http://www.52os.net/phpinfo.php?id=1 正常显示。
3 b$ v; H; x+ |http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
1 m* ~; r2 l3 C* v0 Ehttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
5 U5 n, l3 r+ C说明sql注入和xss已经被过滤了) m. ^9 i) C1 S: A) T+ y* h: o
8 W& J" |. T9 f* c0 D七、安装过程中排错9 Y7 _- c/ ^& U( A& @
0 k5 m; ]0 x/ {# b6 M1.缺少APXS会报错" M* {2 h1 l4 {$ A7 ?' T
$ [/ C+ W3 I" u. ?$ @+ f; M
configure: looking for Apache module support via DSO through APXS+ s" B& H7 O% u0 |
configure: error: couldn't find APXS
# W2 l( H8 P. J4 W. g$ Zapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。, B' H4 ]5 c9 V0 ]! }: V- W, s
解决方法:. F) ]# S% g; l8 S
! _% N* g8 l. L [3 ~
yum install httpd-devel
4 A& m( L# Y, ^- | x2.没有pcre
/ u' @8 w0 n9 A6 V/ g k' ^" K* h2 z V4 ]/ R
configure: *** pcre library not found. g0 ^* z: w# y: m, O' R; Q C
configure: error: pcre library is required
' E ^9 t* X; u! E5 H解决方法:) Z" d- b3 e5 `6 C
3 T# V' S7 v( ?! U3 U$ \
yum install pcre pcre-devel
. A+ B! t8 |: k% @; s+ J3.没有libxml2; U. i8 p3 r7 ]
5 Y. P( U+ j0 ~) O( X: [0 N8 ^ c
5 O+ i+ n. |/ c2 t/ p( Lconfigure: *** xml library not found.
?# _/ c/ S2 s4 l( D+ ^1 t. |, J& Gconfigure: error: libxml2 is required
8 }0 \" Z3 ~" ~: O1 N9 B解决方法:& P# g/ P t/ z9 U
8 s* Q1 S. D, T0 hyum install libxml2 libxml2-devel( o2 ~. y/ N. w
4.执行 /opt/tengine/sbin/nginx -m 时有警告
- ]% X, |7 L0 k h; x
" W ?; l! }+ \4 P' tTengine version: Tengine/2.1.0 (nginx/1.6.2)
' }8 c/ s) g" j8 v( Inginx: [warn] ModSecurity: Loaded APR do not match with compiled!' R$ o* i) u2 k: g. g4 s; m
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log" L) T: k, L% ]. {
4 z1 ^$ ?. F7 Y1 x+ N
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
$ g9 {6 k A6 f" W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. t, k0 B/ E( K5 H. i8 Y S" a7 t2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
% t) h9 Q$ _+ {- [0 R7 R# W2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
v9 _7 @. g' W: V. k! l2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
5 A' J0 ~% D8 `2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# n& U, {) E) k1 k
解决方法,移除低版本的APR (1.3.9)
( X7 j/ V; r7 E5 k, c: A$ n0 I# ]% D& ]* L6 a* S
yum remove apr/ g1 L3 c) `' @
5.Error.log中有: Audit log: Failed to lock global mutex
8 r( P {0 [" f: K, g) w
( \6 O, L3 M, T, M% Q& X l2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock + ]3 l4 I6 G* o5 j+ R
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]1 ?2 [7 X; ^ c5 O, a9 W
解决方法:
C: k3 P$ ~9 f! Q2 S4 L编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* s* W7 V1 \$ e' b3 b' _. V
9 m, O' g" y" R" j: |SecAuditLogDirMode 0777
, T+ p( C( T) E3 C4 n; g$ WSecAuditLogFileMode 0550
# a( Q- J8 O' gSecAuditLogStorageDir /var/log/modsecurity' Q. Y4 z" Z0 z- Q5 k& g
SecAuditLogType Concurrent
: S5 o/ \$ v0 i, C参考文章:- i- {. Z% B3 t# ?
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
! L- b' U1 z2 X$ W$ y, G* m fhttp://drops.wooyun.org/tips/2614 |
|