|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
' |0 X. D2 c; [4 ~0 J- Y" U2 j; |1 C% G" Q. |% J! h
一.准备工作
& f0 Y5 [; [9 s$ @4 `8 I
; N4 d# W) W8 w系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
& O9 I( v- m/ O1 v% S. r6 y6 f$ @) f
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
$ [9 Q/ ` m* U9 f/ h8 w$ h: T: c( \1 \
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz( u% J1 @% Y3 K% H3 @
$ P. Y- f8 O+ sOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs# c/ d+ O) F5 Y/ g# }
# \1 Q2 _. E8 ~7 s依赖关系:0 l3 H& a& V8 ^
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:" x& Z" O# Y. P) t
1 v' [9 V: ^8 e5 D3 E* c% u5 P' Syum install zlib zlib-devel openssl openssl-devel pcre pcre-devel% n; u6 P1 v8 O& ]5 h
modsecurty依赖的包:pcre httpd-devel libxml2 apr8 R) ?5 L$ ^$ a; G) c. y
) a( `" E; F d* uyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
% [$ t4 L1 Z2 p& N$ `* k0 Z9 _( p2 n二.启用standalone模块并编译- g' T. C1 N0 x9 h: v8 B2 K' q
; m: T: @/ V0 g
下载modsecurity for nginx 解压,进入解压后目录执行:8 Y) ?/ x8 I" I/ {
( b( _4 B' S( H" r' g( a& h./autogen.sh
4 I! I, b/ k; K" q( w. u8 N./configure --enable-standalone-module --disable-mlogc
2 b" B9 |8 B- r9 K; |% Smake
# k+ R: Q( S* v. t三.nginx添加modsecurity模块
- i0 R9 E- v, T
4 A% D' A) m/ X, @在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
8 E" \9 o* a! c' o' f/ v2 x# p
7 n, n4 n) c* r) Z% J S./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine5 |% h2 m% E, _0 ^8 F& {
make && make install
2 p% `9 f, L# B" r四.添加规则
$ \' T& H4 a& V; j' K* k2 G% T8 z' Q8 x2 B1 g1 b# O6 E7 K
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
% T1 B: B7 o0 K: G0 g2 }0 H8 t/ R$ L2 J0 L% r9 ~
1.下载OWASP规则:
' d4 Z9 R( O$ D) [/ \' L( e( X# K
) Q! P. }5 b' V) k4 l1 H! Lgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
( i) p8 X) @/ J4 o) h6 a7 r: _1 q& E x) P7 @
mv owasp-modsecurity-crs /opt/tengine/conf/8 r7 J5 i6 }8 P/ `
1 t" L1 d1 B; Q% ?7 p6 ]cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
. W- P& m9 J6 b, K# X( L2.启用OWASP规则:5 W6 C" R4 ~8 I: w9 u9 ]; Q1 W9 \) ^
`1 m" K p& m
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。, M; K7 o1 q* g% Y
" @+ W( e- c& w! M. o+ H6 f
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# @+ H8 q8 Z7 [
8 q* X; P; }7 ?
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。1 q7 N$ I) D/ g5 t; f
$ n9 q. E% v% R% i/ NInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
0 @/ }0 t' `) A/ i8 Q+ GInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ c4 k0 F% r9 rInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
8 ~* y& d$ r6 C4 M' t4 K2 ?" cInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 L( W% n& G W# ?Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
/ V% @2 H& O1 k% PInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf/ y: \5 z6 D' J2 C
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf0 o H$ b8 Q/ R
五.配置nginx* l ^6 |$ d2 P
) B5 i$ R. r y* l% s$ f$ a
在需要启用modsecurity的主机的location下面加入下面两行即可:
3 ~4 I3 n8 z) H6 R6 k8 c5 Y
4 U% D( o. ?1 ]/ BModSecurityEnabled on;
9 F" T- B- z% n$ F0 QModSecurityConfig modsecurity.conf;
* j& N# v) }6 \4 G4 p+ L* J下面是两个示例配置,php虚拟主机:' E, g! ?; g) G% h
7 ^3 Y6 }9 P2 S% n P
server {
- T: L. H7 G+ L4 U+ W listen 80;
5 u0 _5 n$ R) ` server_name 52os.net www.52os.net;
0 c( ^6 A8 X& b/ K; u$ F: `9 l
& K4 O# @% w6 G location ~ \.php$ {
' E/ z6 V& {& q/ ^, \8 B; m ModSecurityEnabled on; " T; h6 m; P& O
ModSecurityConfig modsecurity.conf;
( c+ w% G- K ~0 E0 B1 A' F. X1 W6 C9 c. j
root /web/wordpress;7 w; ]4 ^5 K5 t
index index.php index.html index.htm;
' x3 u) m% q+ T- G4 g" M, G ! V9 `. B$ G9 C+ U% [
fastcgi_pass 127.0.0.1:9000;+ @" k- D; K$ m C/ ~) I1 o
fastcgi_index index.php;+ w& u2 G! W+ C1 n* V) X
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;0 L. m0 {. ~9 q6 K, Q" @
include fastcgi_params;
0 [+ `8 V) E; Z) j3 R' {, D* ^7 f }$ u5 T- v4 n- H
}
. D# i+ t* e7 T$ S% X- A, O. ~upstream负载均衡:
' N$ U; o! W5 y2 X) E' E" \; v$ K& y4 C- N* N2 h) c
upstream 52os.net {8 z9 N- P! U8 \/ {
server 192.168.1.100:8080;
& E/ N$ {$ A2 C5 h- g server 192.168.1.101:8080 backup;
" G5 b% C1 t A; G# Z M}
1 c% W$ P) u# X! B8 b5 \1 s1 B7 o7 p8 a2 n% Q
server {6 G/ z3 K0 O4 P" d$ B6 Z$ Y
listen 80;, T; N7 z" b0 i+ _5 I0 m; }
server_name 52os.net www.52os.net;' |. e/ R. B, B
: k9 d h% d) |2 a2 ?5 Y" klocation / {
4 k9 C( T$ `% p ModSecurityEnabled on;
9 I! Q* i0 n/ A: X5 T: z ModSecurityConfig modsecurity.conf; , Z: A3 s; D. I7 p; G) ]
" k; h, V6 m# C; X) _ proxy_pass http://online;* ~1 U( k. w }
proxy_redirect off;
" w# y9 P' w( { proxy_set_header Host $host;9 e; [& m* D1 ~, Q; `* G
proxy_set_header X-Real-IP $remote_addr;
* m% ?' T; _. ~: w0 S proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;' M, A: n# g1 z/ h5 T9 ]
}
% T# p5 l f$ o/ S, }/ Q+ C}- s2 o m5 f0 u8 I* V5 h8 w
六.测试0 [( S$ a, W s r
+ p/ J( V4 T$ C& D% V
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
' J/ M" b8 z4 c/ \5 {, L& y- _3 o
<?php
t$ ~0 \7 ]9 p! ~) N9 t$ l0 ]! b4 l phpinfo();
1 g; Z, x0 x; e+ P4 B?>
; K+ {8 z7 {4 S: Z* m$ I8 H, C- u在浏览器中访问:
7 N* g/ g5 K$ |' E$ f
5 B7 n3 B, A/ U4 T% [, Yhttp://www.52os.net/phpinfo.php?id=1 正常显示。
$ B0 K4 e( r, ?http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。+ ]5 T5 W) q4 C2 t7 x2 b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( P* `& V! v; u* q4 S) Y说明sql注入和xss已经被过滤了
5 ]7 l9 d+ g. l( O3 ~, E, E1 ?1 _! @$ A# B- R- j
七、安装过程中排错
7 Q# c( h5 ^; Y& a
7 l8 ]& w. p* H$ }; u1.缺少APXS会报错
& ~" l7 \8 a0 }) X: j; y
0 c3 h. g2 Q2 F9 W( [% f( B- Rconfigure: looking for Apache module support via DSO through APXS
& h3 n; l7 a6 G* b4 Gconfigure: error: couldn't find APXS/ i: h2 ]+ O9 @. t7 l5 G
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 ^% f8 A* p) R3 o
解决方法:
3 t' P4 i3 K1 A8 E# D+ Y7 H/ A
7 r. [7 ]% a$ b) N1 zyum install httpd-devel* l3 M! H) w: x( ]% i/ h
2.没有pcre
7 E; e; S4 X* N1 F+ \# W& h; x- g; f% H9 R/ j2 m: `+ E' t
configure: *** pcre library not found.& P' D8 N% V. }$ Y- X
configure: error: pcre library is required
& p7 Z# x. j6 |" Y* b; d解决方法:. t" L# G3 ?; j1 p
6 H6 v* r; ~* u; `+ c( y2 Y
yum install pcre pcre-devel6 N: H( r# ~" p4 G
3.没有libxml27 M3 v3 Y# \* j" _
6 h) H, `5 N# J
9 d3 Q7 _ U; B9 f& n" Bconfigure: *** xml library not found.
9 S- Y' ?; O. gconfigure: error: libxml2 is required
. M, m$ j1 {7 ]% a3 _解决方法:0 _ V3 F1 Q1 t0 m3 e& x! Q
- x' n G/ T5 N. J& Hyum install libxml2 libxml2-devel, D* ~- v/ }/ c2 ^
4.执行 /opt/tengine/sbin/nginx -m 时有警告4 R2 |. ]. K' X9 E, k3 ^
) g( i. n0 s$ V9 _Tengine version: Tengine/2.1.0 (nginx/1.6.2)
1 o3 c1 c$ i2 inginx: [warn] ModSecurity: Loaded APR do not match with compiled!
* K, c% ?- T; T! G原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
+ t% @1 r8 h- g; k
8 C# `( A* Q! D3 s7 O9 p2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.7 U0 ?* ^ w; _1 f9 ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"- i& g8 q. Q5 m* K4 j+ [& i
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!$ \% S, Q3 k `9 w5 f/ F7 D l) T$ @
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"$ _' d6 k5 ~8 u1 R5 Q0 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
/ Q) L: F% ]- A. c5 X E2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On. d/ ~1 m. X; _* A* s
解决方法,移除低版本的APR (1.3.9)
5 E7 @' r8 @+ l* j+ M8 \# |5 ^ y* C3 k5 J
yum remove apr; t/ R2 u( f1 q! \2 x
5.Error.log中有: Audit log: Failed to lock global mutex/ ?$ `0 b2 d w( Y- U
1 N0 a( `+ e) w6 d( X/ i4 \1 N2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
5 M9 t2 n8 G7 v) h. m' U v) sglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
* I, q$ \8 u5 I" H% R7 e解决方法:
9 W* P/ y: M. w0 o编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
6 F d3 Z0 m, Q! ], V* `. U- K j# R0 |( x; M& M1 i8 L' A- f
SecAuditLogDirMode 07775 t2 \# \% }6 ~$ b) k
SecAuditLogFileMode 0550
' T* ~3 \4 [6 i GSecAuditLogStorageDir /var/log/modsecurity0 z/ g3 h7 W& q0 c# W: B
SecAuditLogType Concurrent c8 _; \8 J( O# v ?* H1 v
参考文章:
" t' {/ E, m. R7 K% _https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
& _" N6 [$ o# q3 N& E1 T m4 k/ Fhttp://drops.wooyun.org/tips/2614 |
|