|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。7 f/ d/ [& m1 S# X) t% h) C
0 U# h. T- X7 ~3 Z7 A* \一.准备工作
! T. t$ ~! N, o- U5 F) F( S3 K: G B
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
3 S5 r w% n- ?3 }! f
) w3 A U6 B4 M+ A, ]( ?tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
2 j/ u$ @6 m& K/ N( V
1 ]7 U# }3 ^9 Z( D6 o1 s! w$ Lmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. c* ]) F" g' u. w+ ^" z7 i2 x
, ~4 |. O5 l6 ?* W! M( nOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
1 X/ W H0 N) v4 q7 Y; e
" y& ? @8 Q0 g* Q+ i0 J依赖关系:+ f. G3 y' F; O' j4 Y4 D
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:' K1 L2 a( t9 l! T+ k' r( k& V
: E0 g9 K$ ?9 l# q2 k
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel' E& m2 y2 S; {. Y
modsecurty依赖的包:pcre httpd-devel libxml2 apr, Q$ h6 y* E3 Q4 |8 q
I) ]" x) v0 L( I$ N' r/ k9 ^
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
3 t8 d9 @0 {3 o) f/ [二.启用standalone模块并编译( h+ |" v: }5 S; G2 T F9 a1 _8 D' Z+ h
% G# F3 K- {% b( X. v下载modsecurity for nginx 解压,进入解压后目录执行:
6 Z: e; J; P- i1 R
. r% S8 w9 |4 w, K./autogen.sh
7 Q2 n6 F+ e+ z' {./configure --enable-standalone-module --disable-mlogc& @6 D r) N/ X8 q
make - r/ q$ u0 x, D$ O. h8 \3 s
三.nginx添加modsecurity模块
$ j# V9 D$ E0 }# M9 I B( Z4 J* y% E6 M* i
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:9 p. G4 f) ^( o5 D4 h# p
4 G6 B' z. `; H: z$ ` N
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
* { Y4 M, k) g- [, L fmake && make install
1 L$ D: ]* Y Q$ q$ R* k四.添加规则, D, r. {- F& j7 |1 p1 `
7 W( i% j' Q1 u( j2 p4 w
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
! I5 o" X+ o) `1 e, a' F* g. G" C- }& t
1.下载OWASP规则:
" {( U+ V0 v0 ?3 K
% t) ?9 g2 W* e8 e8 Y! Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs$ V( C3 X# X3 V: q! j1 q* |
4 ]. U( R) y4 t* G# Z1 nmv owasp-modsecurity-crs /opt/tengine/conf/
% e, n0 ^' A* t$ |" f0 a# t) d$ \8 D8 O! q
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, P/ o& l9 L; |& U! c! B" z+ i( y
2.启用OWASP规则:9 p$ \+ y5 T9 e. P, m
9 j+ W& G6 i2 {% [1 r
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
/ S4 v8 C8 E: a) W7 Z, b- ^
% A$ o0 e: {- p1 Y$ R3 [+ [编辑modsecurity.conf 文件,将SecRuleEngine设置为 on# z+ A* T7 K: {& d" t8 U, n
+ [1 D' z% M8 r3 ]% ?3 k0 N
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! i# _9 s/ q, e* n9 w$ j) R" [: ]
1 Z/ D. P9 J+ ?Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf3 Y$ n1 |, N& ~# t+ g$ r! q
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" b) ~! Q' x+ F3 {- YInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
; O f* e: ? M/ n/ d' m) @Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
) {9 @: q3 m; y XInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
! [8 k9 X4 T7 \4 }/ A# qInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf' j+ j0 v) f5 v4 z* }
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
: b% W; y3 y5 i% C; x( R% Z' f五.配置nginx
$ U# S! k: J- e0 t/ @' G! n1 m O4 j6 G5 T
在需要启用modsecurity的主机的location下面加入下面两行即可:
/ |7 N, s2 d) n, s) R
4 Y9 x6 O; X! x( _9 ZModSecurityEnabled on;
* [, G- c# E3 e' F5 L0 KModSecurityConfig modsecurity.conf;
' F6 H8 W7 v8 P7 B下面是两个示例配置,php虚拟主机:
0 H! B2 h1 c( q) X, _* f" D: l5 I2 Z8 @4 _9 {2 ^& R" m6 ?# n
server {' X% k& G" F0 j# _" }: i- x1 S# I8 ^
listen 80;, O1 q' d1 k) A1 K$ E3 Q
server_name 52os.net www.52os.net;
$ p; G* h# d g0 S7 N 8 R# }- c: F% c2 j/ T* o) K
location ~ \.php$ {
N( Y% r3 H3 _' y ModSecurityEnabled on;
! F/ ?3 h. D0 G; m5 ~ ModSecurityConfig modsecurity.conf;
/ Y, {0 x: @& Y* p7 ?" q6 j6 r2 m2 }" q' h
root /web/wordpress;7 W: b+ b+ L% f; i/ z1 H" r
index index.php index.html index.htm;
! c, F* _. r4 u2 `6 N
1 z/ T4 I# w& D, d% G: Q fastcgi_pass 127.0.0.1:9000;
& y# e4 J5 ~6 D' Y3 V% s fastcgi_index index.php;
3 ^0 j# H$ W# [, P" j fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
2 \: A% O. O9 S1 L0 m3 o include fastcgi_params;' X# P; k& E: q) q z. L
}: F$ o5 u2 B# B1 o# l
}
; Z o3 _* u3 ^; Yupstream负载均衡:5 G2 U. T0 J! A( z. z, `. V
% v3 c! H, N2 v4 V( ^! Gupstream 52os.net {
, J$ ~" L+ |# {- D4 E" @6 j8 f server 192.168.1.100:8080;
; ~9 ?; e2 t- j7 q! ^ server 192.168.1.101:8080 backup;
& ~, \+ r$ d; T N}
- }6 X, q7 X6 F
5 J$ q1 S6 `" e" sserver {
7 P# _# M! S( V* Flisten 80;" q! m8 v% C, ]/ E7 R! g# `+ M j
server_name 52os.net www.52os.net;
9 v( K- R+ ^8 q, t
, Y( y% q/ b7 M4 ?4 P5 R1 Y qlocation / {
, V% I' B0 E- D8 n" H& } ModSecurityEnabled on; / q8 e& j2 g- ]. e
ModSecurityConfig modsecurity.conf;
' u( v; j3 g. q2 m- j' U( g f, ]1 }0 }* B* J8 F4 K
proxy_pass http://online;
- G5 I4 Q- {. S( J2 L. r proxy_redirect off;
. E) T& Z3 P1 I0 U5 Z proxy_set_header Host $host;4 I* l/ k0 y$ V
proxy_set_header X-Real-IP $remote_addr;
1 {# O# h! K% X3 r4 b* u2 {, G proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;3 q3 \& h& L6 w
}
- `0 N0 g7 U& F) ^}
V% b0 Q: J/ O# {2 {2 F) w六.测试5 G2 |& }! B* n7 S0 G4 ~7 ?' }! U
5 P3 K* l) q0 z我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( t/ m; ^* ^4 P: G4 N _, `" i+ j
! J7 {+ u: K# d2 C2 B<?php
" d& q; u- W! T# \ phpinfo();
: |# h/ E: I* C2 A?>9 P3 \- G9 E$ |
在浏览器中访问:
2 D h _$ J9 r
6 Q2 c; E! e8 ]* R7 R4 |3 dhttp://www.52os.net/phpinfo.php?id=1 正常显示。
2 k! S) w3 A5 h2 }: k! y7 D' G' r" ]9 Whttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。1 o6 t P3 [: v
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。+ [6 e6 E& z; s: u
说明sql注入和xss已经被过滤了
9 P- e; K3 O4 b4 o" f4 l! {3 t/ E" m- d# X
七、安装过程中排错
# ]( A1 {: T1 C! h6 f+ v
4 a4 }9 s7 i% V, ?' Y$ ]1.缺少APXS会报错+ `/ F7 V9 {! A8 X: v
4 S: {7 ^( v$ T8 ~/ d9 O. ?configure: looking for Apache module support via DSO through APXS
+ N% ?6 y, B: W% cconfigure: error: couldn't find APXS
3 D; Q9 W& ^/ C/ x4 aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
" C) f. _! {* O% P) ~) Q解决方法:
7 m7 K/ O& u9 r4 ` y% p( `# I' N; o3 \+ U
yum install httpd-devel# A5 v" X( a/ M# N: G
2.没有pcre
) y4 u6 r% C8 N# T9 I% i- T) x6 T
configure: *** pcre library not found.+ q! L$ U( q* p$ i) _( U
configure: error: pcre library is required9 ^$ N o2 y- V5 U7 n
解决方法:% S. n1 I2 W; g" ?
, A/ ~% i8 t& p* S1 J. \yum install pcre pcre-devel
: K% [) _: h+ P6 H6 p7 X& d/ P3.没有libxml25 `% d; `1 e: D* K* o- j# N
5 W1 F5 Q0 { @2 s* U0 P
+ P/ {& C$ a: k$ i+ Dconfigure: *** xml library not found.
+ [" h' K7 O" s/ nconfigure: error: libxml2 is required( b# r! v N A/ u
解决方法:
3 c- X1 l1 M+ L. |# U3 O7 T) |# z/ Y, z% Y. a3 a
yum install libxml2 libxml2-devel
2 |1 D: P4 r' x# T l4.执行 /opt/tengine/sbin/nginx -m 时有警告& c$ _" p4 s5 J1 R# e; _8 l4 c
6 T7 F2 L# q3 N7 F8 v
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 ?0 F L- y! i- I) L7 E: O9 S7 k, O
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
- g6 [) ~$ `+ G3 e( N2 a7 j原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
b: D6 n! `1 X6 `& ~- Y6 U+ o; f1 u" O l
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
b0 |* Z6 E7 ]2 {2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9". K: q2 _! y: Z6 w( r" _
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
3 f) }% X3 V: l; P. S- p( @2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"3 f6 c" |: V( c1 X* Z/ L [
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"& i% P4 k& D3 W1 _- Y" F% ]
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.
2 B8 r0 _4 r. {& X解决方法,移除低版本的APR (1.3.9)/ a0 N+ C% n/ o7 P( Y- L# H# T, H
; A* n+ F1 ^( b+ {* p( |* pyum remove apr2 Z+ b2 s/ `/ t7 ^4 Q" X! h
5.Error.log中有: Audit log: Failed to lock global mutex
+ B' Q+ [# E d8 n
( W$ _) A( g3 I. M. r5 m& i2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 ]0 |# }. E5 |( E/ Uglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
@( W/ c; _ K% Y. q解决方法:& G; d D- F7 ^0 F5 Y( e2 o
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
; e8 i, v2 ~. ?" E# C# _8 y/ Z4 F8 {5 q2 v
SecAuditLogDirMode 0777
, F! g# M: [2 |& |: I( HSecAuditLogFileMode 0550 S2 I n6 i# v3 x, E3 d; x
SecAuditLogStorageDir /var/log/modsecurity* L$ {+ ?7 `) i
SecAuditLogType Concurrent
5 V% L8 {8 _9 o参考文章:, v n6 n2 l3 ?/ W: T& S7 ]$ N
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) H' U* c' d; Z
http://drops.wooyun.org/tips/2614 |
|