|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
V+ m, g. n1 s4 L! S0 z( _. l* T4 o/ e/ \+ a1 {, A
一.准备工作% Q- ?- e* o5 Q. J* l- r$ C5 }& ]
' r" ]/ x: L; f; D2 p* C0 _% L6 Y
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.06 `# a8 p2 [; N1 O3 X. N, r
! i! V5 m3 c0 C" }5 V# G; }# utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 J5 \3 Q1 i u& P! c& Y
0 j& P" u& K: P; n- q# S5 Ymodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz G; p& V- G% i
: }. Q, p+ t9 e) \' s/ @OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- `4 S0 S6 X; c( u6 d0 P
3 d& \, A( r# J! |; w依赖关系:
9 ]* B- u. X( J0 D0 w" Vtengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 {+ r' S* I- c
; E* }4 v! {5 I* l' t6 Y0 }$ i
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
* v) j3 t4 Z. w, Z; amodsecurty依赖的包:pcre httpd-devel libxml2 apr
/ K7 \/ [% n4 C/ t$ m+ I
/ }% R) h1 R9 d$ }! C( B7 uyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel: _, R4 L/ M. a6 p9 b( k% q
二.启用standalone模块并编译( _# e! q. |& z6 }9 v0 z2 u
9 |& \( [" B1 R" p7 ?/ i下载modsecurity for nginx 解压,进入解压后目录执行:; k' `9 f% H) k7 A( j
% ? Z% e4 c& E: w F
./autogen.sh* M' w' j n' Q2 s8 ^
./configure --enable-standalone-module --disable-mlogc
) p2 }( O9 _/ F* \9 _make ; F# d: y( o" {' |
三.nginx添加modsecurity模块
- d% N' ~$ t0 t
, o6 m7 z3 v, F# b6 M7 K在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:5 [3 H' z6 U: D! X& P1 @
+ P! d2 j' s0 `# R1 c0 f% x./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine: y1 t- v! [9 r0 p* Q
make && make install! a ?) }: ]( ?5 r( U
四.添加规则6 j' J4 c$ _7 [6 B) }8 c9 v0 x
; K2 i+ d# F5 ~2 @+ O+ B
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ {3 {, E" @' ~$ {% y; R7 E# ?
: g" Q" m3 g9 j v5 r; C+ L7 }: _0 X1.下载OWASP规则:
" m/ s5 H6 b& {, b
, x4 e U6 W% f0 Xgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs
) `* a+ I' ^. w( I- U% t% T4 `+ f& j" S
mv owasp-modsecurity-crs /opt/tengine/conf/5 d% |7 _* s8 Y- Q8 ^/ }
$ V, d" e& o1 P. u$ A* O
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& u2 G4 X5 R& g% b. s) H) R k2.启用OWASP规则:
$ }) F6 J( x8 m' B) X& Q e9 p% r1 i3 j- K" a( |1 Z* x6 F
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 t( s. N; U; T" }! T4 m8 P' b, o
, ^9 S2 c# e# q( E编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
: Q) }5 Z5 k0 N+ u- P& W& ~7 ~9 K6 c
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。7 m' [* V, `- X0 N% |: {; ]
. H2 D# \! Z, ~" {0 x
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf4 P1 k, a# _- Q' Q% `7 v
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf$ n5 K& m% V4 i, H7 U0 _+ C
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf+ A% p' `( i0 f
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf( g8 [% n ^" F% ?4 d. K
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf5 g& k! I$ S+ U/ X3 h) {2 a' q- s
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf6 o' b$ l3 a' I; T. P+ r
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 R) C8 q1 h/ n9 H! ^- D$ B五.配置nginx5 K) Y& Q0 L/ @7 _; D" A
3 U4 z1 D1 V# @* w3 U在需要启用modsecurity的主机的location下面加入下面两行即可:+ E! y4 @( n8 Y" }& b
; {2 K+ o) s3 h5 VModSecurityEnabled on;
$ u; f# @. `. R5 C0 S$ jModSecurityConfig modsecurity.conf;
* j$ o. R, @, ^8 Q( Y下面是两个示例配置,php虚拟主机:
6 A; w# `8 B3 ^5 \3 i1 v& k+ S7 g! [2 z' Y, e* v
server {+ S+ Z9 g: ~# O) u5 I$ w
listen 80;. ~. H3 |: }" H$ D
server_name 52os.net www.52os.net;
7 L. a3 y6 x. e
D6 C; ?4 d- x& N3 y location ~ \.php$ {
0 t# L, s( u# u; }) O5 B ModSecurityEnabled on; + P& q9 A6 A2 B. I+ l5 C) t4 S
ModSecurityConfig modsecurity.conf;$ ^3 X; W" H. T
4 R; y4 {; y2 g root /web/wordpress;
: j6 T' S* K, o# C index index.php index.html index.htm;
7 M( G% @' X, i5 S( t$ ~" { 0 i, a5 p8 q- b, n9 a$ L
fastcgi_pass 127.0.0.1:9000;
+ R9 ]( z+ M$ v R fastcgi_index index.php; N2 D/ @( U2 a" s+ f" D
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
' X4 x4 \& H+ ]1 k2 }, k include fastcgi_params;3 {8 ^( |0 J5 k) b Q+ Q _ R
}" a: m, Y4 `7 f* ?
}( F5 _2 z/ ~+ E& u4 ^6 m$ G
upstream负载均衡:
9 Z- P" ]. z; g5 x6 W% R( g1 P8 e b; i3 [1 c7 G
upstream 52os.net {: ~9 h) \5 @8 V% s
server 192.168.1.100:8080;
" i( ^0 \2 G" d5 i0 a& u server 192.168.1.101:8080 backup;4 ~" k: t( E8 r2 y' I9 S+ Q7 L
}$ F* W6 C- T3 z$ b9 M
h! }0 }- x$ i! Cserver {
" K/ u3 ?# A! W1 t3 K- ]* {listen 80;0 f: }1 v( x) F( s& q; Q
server_name 52os.net www.52os.net;
6 t( ~+ Z0 u# f$ u' u& {- w$ O- s3 E, H
location / {& P: H3 u& o! L
ModSecurityEnabled on;
2 z0 w9 r9 T' ~ Z4 o ModSecurityConfig modsecurity.conf; 4 `4 }* I+ ?8 K: ]
) y6 v9 o9 ~$ o2 N# R) R
proxy_pass http://online;, _* P, H& m3 @9 b. l2 L$ L' @
proxy_redirect off;
5 X7 s! A' B4 r- t- Y9 b9 i proxy_set_header Host $host;
% p( ]- ~: ]9 X! D proxy_set_header X-Real-IP $remote_addr;
# Z; m, Z R* w; ?8 ^4 Z' x proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
0 f8 q/ `) Z" Q3 K }4 I& U* D; C* l9 j8 m Y
}
# @8 k: x& s u, ~6 e0 e2 i六.测试' j) ~6 b& u3 R. D& F
# X+ V9 y) C# E ~2 ~3 s
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:# v) q( f) z5 y
2 A, J9 o/ z" J9 R- \
<?php8 Z( {/ p) m% _6 @' y
phpinfo();
7 i' u- ]3 k/ |8 R7 m7 g2 a?>
$ B1 \6 e- y X8 {) V+ U4 m3 Z在浏览器中访问:
# k. _' t7 n4 H1 C! B7 J0 V5 s( s
, N* c Z2 Q+ q! M8 `% r' G( d4 n5 ]http://www.52os.net/phpinfo.php?id=1 正常显示。5 t' p4 d2 P& _& n! l M! M: l" z" q
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。& F1 e! r: L# C" |' M0 m
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。6 ^' M9 z- r/ }) ^5 A7 J5 \/ U
说明sql注入和xss已经被过滤了: K( l" E: u& N# a& ?6 Y8 s4 L
2 O" `( e" l4 O
七、安装过程中排错3 X' S8 {9 a G9 x, [) i$ d$ S
: c3 ^6 i: f1 V5 b+ y0 C; r1.缺少APXS会报错
5 p! i' y% F/ r- i8 N: ]7 `. z% o# [4 ?/ L! H
configure: looking for Apache module support via DSO through APXS7 D2 r7 Q# n! c- I* k
configure: error: couldn't find APXS: _/ B6 A0 Z7 k! q% A
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
% _& ?2 X) }/ U, B5 L5 ?5 B解决方法:- C$ X, L5 n) P6 Q8 ]
: e" E& r- _, d. G, t
yum install httpd-devel
& @$ p+ t" l4 M z2.没有pcre8 n) l) a+ p* ^& m9 `* d
* l; x; `% }. }3 R8 M1 n! P/ {
configure: *** pcre library not found.
7 v! r( m. f+ Z6 r- t( wconfigure: error: pcre library is required `7 d$ t u* \7 A0 e, n
解决方法:
$ F7 l3 v. p) \9 E* \# V6 I9 i
$ V c* Y4 i0 L: s fyum install pcre pcre-devel
0 F3 y) w' b4 I: {; G+ r3.没有libxml25 g8 o1 x$ a. s( R9 Z$ d4 z
3 M$ X5 _( v! r I, J) F
5 ]$ F0 [! I& H) B4 w
configure: *** xml library not found., ~, n* K( O3 x4 w- k/ m
configure: error: libxml2 is required
5 ?$ o( I8 t( J! N' Y- w8 d5 |解决方法:, {8 P, p* d* F& W! s( d
/ Q& P& w. B- q! J: q
yum install libxml2 libxml2-devel
" |( U. M* R6 g4.执行 /opt/tengine/sbin/nginx -m 时有警告
; _4 I- h% T G% n1 D3 l: H; w @' u1 r% ^- a( u* m: J
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
$ U1 t0 O. K. g) qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
; @) l9 }: U- W原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log% P1 a8 B s! g w
0 b1 s. y% Y; i! \, |2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.( E5 m% J; w$ p4 N9 w4 M5 F/ ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"" h" E% [7 ^, s+ J& L4 j+ U! ?
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
V. S/ ^: B1 n: l* ?8 Y* \2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* b2 O) B2 A; g- d- {3 S5 f. i
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"$ |% O6 U' b8 [4 t7 }
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
0 q& J0 W- Z6 b/ d解决方法,移除低版本的APR (1.3.9)
% C3 R& M) v: l% Q; V3 |- J9 ]+ t
yum remove apr
: g9 L r$ Q# u* A9 }5.Error.log中有: Audit log: Failed to lock global mutex
, y7 y U2 O: M2 x0 g9 n: s/ D9 A) E) e4 \! s: a$ k* z
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
0 V+ c. C( q2 W2 k/ O, pglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
3 [- k; J: K) m1 Y- N7 j- I解决方法:5 f8 k, K+ F: b
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:* d- O5 h& Y; s; |& D& v R3 y
! `) C6 I6 J5 j8 d0 C, q6 KSecAuditLogDirMode 0777
! u1 f# Q2 [% W. R1 NSecAuditLogFileMode 0550
7 F% y7 `, G$ h: W2 L* o6 DSecAuditLogStorageDir /var/log/modsecurity
2 G. e r8 [8 L! PSecAuditLogType Concurrent
* y, a w4 H8 S0 v z! k参考文章:
8 y( \3 _3 R! v* O' u* X3 Chttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
% e0 K5 }' F4 i3 G( _4 Yhttp://drops.wooyun.org/tips/2614 |
|