|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
0 e5 f3 }; K4 s5 e% O2 \" Q: g9 O7 S
一.准备工作
' g, j. E7 B5 P6 ]9 S4 @/ L
5 d) f5 v, a$ n: {( P系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
1 K; F! G! T7 J; I: w* e1 B8 Y @) v; ~
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz8 d; ^! c3 R5 W& [# U
% F6 R5 `% \8 Y7 c7 u8 j; Rmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz# b4 [5 j; O0 ?1 P) f7 ?
7 s5 M' k/ d8 o- w" X2 A
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
- P1 v( J- P1 t$ @: {! p* N4 o9 T# L. k& J) b8 `5 V
依赖关系:8 P+ S8 B- M3 d i7 x$ `. k4 H
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
* `& g# X2 g1 w0 Z) g
" {! k3 X( w$ w5 e$ m: H: ]yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel. [- J. q# m2 z$ _
modsecurty依赖的包:pcre httpd-devel libxml2 apr
' a, _# h. c( p* h6 D8 M
/ X8 G) f7 _. \ ~yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel5 [! E8 R0 }2 `3 O
二.启用standalone模块并编译# o& \& }( s' G" h
- b/ h* }: H+ H+ u4 ]
下载modsecurity for nginx 解压,进入解压后目录执行:
* Q# v% y; S8 ~2 B' e. m% x( y- P
./autogen.sh/ M1 y4 H* L: t5 m" u6 b* G
./configure --enable-standalone-module --disable-mlogc) E1 n3 W1 [/ x1 i8 b3 @
make 8 E7 d- e$ ?4 X$ X0 K3 w, Z" [
三.nginx添加modsecurity模块
4 V7 l$ c s; Q2 [; H6 c5 G4 o
! o3 F0 |; U, s G5 C- z ^1 a. t在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:: g" s8 O4 p! a; J2 A2 r
' Q4 p, z$ j# N) h6 w$ c {
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
6 G3 L4 n) N% H9 d' ?$ Rmake && make install3 u; }& a0 ]1 Z
四.添加规则
( M& J3 s( A* f* T6 L+ Y
/ m" R, }5 |7 Pmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
' J; U( b. Z0 a; k1 P" l0 z6 Z! h. |
1.下载OWASP规则:: b0 J0 |* H6 y" A; V+ v# ]
/ n/ W5 T0 U% G1 B* s/ D
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
0 N$ H% a( a/ l4 R
! m C: M" b1 D/ Kmv owasp-modsecurity-crs /opt/tengine/conf/- L7 e' r: r: m, X
: _& \* C# T( e6 a( }9 dcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf1 m2 b" U2 M# X& O% }- U
2.启用OWASP规则:
8 }& ^+ E/ R) c+ M# D) n6 I2 a
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 B. M( Z* h. y- @% O1 Y, |
# Z$ e8 p( U$ D, s/ c" l: s8 l
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on7 o# R, U9 q4 d
, `1 W) b( ^3 h0 m/ yowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
" Q+ G) I+ ?3 d8 v& n' T# C6 Z4 G6 T: j$ A, i
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf; k* v/ H7 H- _9 s6 w2 W) ]; G
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
, u% y: r$ K1 ?1 W+ ^7 XInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf$ e, C/ _2 F! j* V R$ a1 D' L/ S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
( l# I: h& U4 v7 M3 ?0 _" v# S1 VInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- e7 Q' ]$ F8 K1 v/ nInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
0 |8 l/ p& i& p0 `& C1 d6 v" cInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
" }; [" z8 v/ Y) X) T五.配置nginx
8 C8 ^' J+ H" \/ i* Y) O& P$ c y( `5 \' ?0 M& E% r3 |/ G9 y
在需要启用modsecurity的主机的location下面加入下面两行即可:
3 @4 @- k+ h ~' Y- L
& Y( l* o! R3 s3 H. g- T2 rModSecurityEnabled on; 7 Z6 A' X- L/ } Q. d
ModSecurityConfig modsecurity.conf;7 C- U- m* O" _( q$ Z- S" R
下面是两个示例配置,php虚拟主机:
N) N5 j) c9 d& ?/ q$ z3 b' T* U: U x3 t4 |% u) v
server {/ {& ?9 }6 _6 i. f( ]5 Z- }
listen 80;6 E: y8 k2 l: ~3 A* t
server_name 52os.net www.52os.net;" A8 x7 k8 ^# Z2 T2 ^3 q" Q7 b. e' x
' t# C! T* p" R3 d location ~ \.php$ {& S' Z: X1 ]* |$ r8 Y, \7 D; ~
ModSecurityEnabled on; & d& s- |* d- p; j5 ]
ModSecurityConfig modsecurity.conf;
1 g1 D( ^9 z' Z H& \) C
; M- J: q+ \; w root /web/wordpress;! L, A9 ~/ C& J' E- x. u
index index.php index.html index.htm;
! ]/ H) V7 l8 S" C3 T8 h* _" m
' F6 A: b/ |* w fastcgi_pass 127.0.0.1:9000;5 z1 @" Z" i g1 S
fastcgi_index index.php;
: ^, V8 G& S5 F3 [# E8 I fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
$ @+ O1 D( z0 ^, T* k; l+ m2 U include fastcgi_params;
* h( ]* ?) G, I }5 \1 q6 _' P' }. R% P
}
) h8 A- H- w7 v. l, k0 F B8 F1 r: Yupstream负载均衡: }- g: a5 ^8 ]2 k' j5 {5 y
; v+ h3 M# r2 O. V1 Q# \$ X e
upstream 52os.net {0 G, ?- b% N8 c w1 P: Q% J
server 192.168.1.100:8080;
, N. ] p3 M b5 |/ c! h server 192.168.1.101:8080 backup;
/ W3 _5 X2 j, a+ B% m" Z}
' g, c: @! y1 w# t# V% Y% N
& \. O' e. s% x$ H/ Rserver { f% h7 q$ y+ E" K! B% W
listen 80;
m Q; T4 c) T! [1 r, Oserver_name 52os.net www.52os.net;4 i1 r# q+ {8 E3 |
2 v/ L1 e) a1 t# z& C& f
location / {
5 E. o1 R6 H: g, b ModSecurityEnabled on;
! ]5 a8 j3 Y9 c7 `$ A# E4 f& w8 z ModSecurityConfig modsecurity.conf; , x# z" z& } q2 h/ z
8 l# e* e0 M9 N8 x+ i
proxy_pass http://online;
6 T: O. _% ]- Q" j C. J3 q i proxy_redirect off;
/ e9 H6 u! {9 g9 u0 h( {4 _$ S, l proxy_set_header Host $host;
2 |& ?) H/ T6 A! c9 G; v5 V0 k proxy_set_header X-Real-IP $remote_addr;& b, X! r# [$ V. h
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
, R8 R: X1 T2 k% j) ^# _& k }
4 i+ f& Q5 g9 c& A- i}2 O- O; k Q5 y. e; D0 Y8 a
六.测试
: j8 A4 P) H* Q4 A8 t. W& G( u/ O: K k! @ w' H
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:& x; ]3 }9 H7 U. r6 T
$ P, m( s, i5 x! S. p3 I<?php
$ m3 I3 i% ~% g phpinfo();
% U1 I2 A8 ]5 n?>" G3 s- F6 s' z2 _+ ^6 M
在浏览器中访问:
7 h$ j1 e+ M( [ i
+ m5 {3 W2 G$ ~! g7 I, Z) Hhttp://www.52os.net/phpinfo.php?id=1 正常显示。! h3 B7 z; l% M4 ]3 k: g
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
' h' C. k' ]9 v8 @http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。* [* p8 ~7 M: n( l+ n: |; \1 A
说明sql注入和xss已经被过滤了2 O! N1 Z& X) J) L" F) P( R; N/ W
/ s1 I/ m h6 E
七、安装过程中排错
* j0 r0 ]/ |6 D5 J: O0 P) v/ Z- x/ Z2 s( {' R
1.缺少APXS会报错
' y1 Q( g4 L; r ^+ v% W( }) ]- C. X% v: R3 o9 {# Q. `7 e
configure: looking for Apache module support via DSO through APXS
& z2 V E7 R& Q8 c v2 }0 C) a- [configure: error: couldn't find APXS* G, X- f! ] N3 M- y! Z1 f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。" J1 x: c4 N& I. J9 f3 @2 m
解决方法:
& d5 U7 [" V2 q5 F+ m) t" P
6 m: Y: { B4 ^8 E7 Jyum install httpd-devel
. P; M% ~7 X$ K% u2.没有pcre
% ?3 F- z% o( C
: m% [* |4 X- n: a6 r. jconfigure: *** pcre library not found." M( J8 i( S; V5 D# e d7 I, {0 H
configure: error: pcre library is required
5 n0 I" `: N i( [8 ~( r1 `解决方法:+ e0 M# N- {* Y) H
, z0 o9 {) K6 l, @$ g- [/ myum install pcre pcre-devel9 z' c5 c; w. m" h1 t
3.没有libxml22 M& l' G2 k- G
9 R3 t7 y+ ]7 X, ~- d+ q) e
4 _5 S( i u0 G% N" ?& q5 A# I, l4 R( U
configure: *** xml library not found.
2 i1 K. i. N2 _- A& P# ~configure: error: libxml2 is required- h0 Y. Z/ j% Q9 ]2 ^1 C
解决方法:
/ X6 b: w( S0 R8 n% K0 Z
' H3 c$ b" K! T& ^% hyum install libxml2 libxml2-devel
# X, Z' o$ C1 b+ i/ {4.执行 /opt/tengine/sbin/nginx -m 时有警告
% m# e& P4 k- R; f! Z$ B7 n0 i9 h& _1 Y. F, g p$ D+ d: P
Tengine version: Tengine/2.1.0 (nginx/1.6.2)( u: i+ K5 Y+ M$ U% S4 a h
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
# F. P9 o1 T3 d0 H* q4 W原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
' q0 c- o n' V3 N7 [9 I' ]. G. E3 m0 \' X6 h& F! D( [5 N! d4 Z
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 ?# r! w: ~* v. i+ V6 l, p2 q# G/ | L) H2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
! S) c2 c2 {" I2 y2 @; m/ E2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!1 e* s: z p2 c7 f
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05" o: f, Q/ Z: H7 {5 p
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
- x: O% c# c% S$ u/ r2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.0 |. x7 D6 `9 q) e$ S7 O
解决方法,移除低版本的APR (1.3.9)
# A' b( ~' p8 X, }
1 L' @5 g9 O# F% Tyum remove apr
* v5 v& j) V U" B w5.Error.log中有: Audit log: Failed to lock global mutex9 B) b. |8 j! e5 d$ k
8 j# R0 l& Q! `8 v( S4 \/ q4 y E/ w2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 9 T4 n: J2 [2 ]0 ?6 j7 ]( N
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
; k/ d; _/ j- M1 v- K( H+ X解决方法:
7 u2 _- c6 i$ t- w9 I& ]编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
5 z5 d- y5 Z( M/ W2 e; |6 Z2 u5 f) e
SecAuditLogDirMode 0777
8 }) S. X4 Y" k4 F: `SecAuditLogFileMode 0550/ B0 l. H' V+ F. c
SecAuditLogStorageDir /var/log/modsecurity2 z) k6 k) v$ j+ Z5 W) S
SecAuditLogType Concurrent9 |) W- ` n9 W* Q9 l: u
参考文章:0 r" U( A# W6 ] {# t, r
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX V Q" c/ @! g/ ^
http://drops.wooyun.org/tips/2614 |
|