|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; L# t2 I6 y/ P: ]2 B3 c5 @+ I% |/ e
% f; G3 `. y/ Z/ [/ Z" b一.准备工作
8 V0 p! P( W3 _6 S* U4 U" x0 }! k' A+ l/ R) _
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.03 \& V, t8 i, y: D
% L0 q' e$ x; X7 P
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 N% R- ~' C5 v% D; q( _! @& m- D+ m6 z3 h2 C# k4 J( v/ S( I) H+ G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz. c( c# t: n& U+ D# P- `8 g# A5 q- y
1 x5 t& X6 _5 ?OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
, Y# A& X4 i3 h. s& m) I- p! F! G2 K% l7 z$ b
依赖关系:$ E$ V% c8 ]# u
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ F) Z# q7 `( A. ~2 C6 g: x
5 y3 c7 I6 S# T; y' pyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel5 S4 g6 {0 x3 j
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 N8 l$ }1 P& b
- {1 m2 ]/ {/ H9 e5 h" {6 ayum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
9 b6 w" L: u7 h% E B5 m' a二.启用standalone模块并编译- ~* k; B0 ^3 F
8 }6 S' A/ l! W% A
下载modsecurity for nginx 解压,进入解压后目录执行:/ ]2 u3 i9 W( A0 ]* [
) l0 `4 I# \7 W- W7 y+ s
./autogen.sh
u" G4 g7 p* b+ o: }* w2 J. w./configure --enable-standalone-module --disable-mlogc
; h. C$ {" ?) z* Qmake
3 c; [$ H- i4 |- A' Y- x- I: [+ a三.nginx添加modsecurity模块# m! ]% S' R" g8 p* i' c# p1 L
5 ?- C$ T4 p3 m) i5 ] m在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:8 v6 A0 d$ B" u. G$ w& \6 S
' ?) \& R) ~) _9 N3 w' f* @./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
$ X* N6 A1 @# J8 }# z1 d& wmake && make install' \/ ?5 T, H0 r& z1 V) k( ^
四.添加规则
* k5 s, s; ]7 o5 A
7 q( S" Y& h6 F( k5 S; m" gmodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: H: i9 ]0 L% Q8 I+ e& f7 n/ P
4 t* u- `8 i J" a) n$ |1.下载OWASP规则:
8 [" C2 B. C9 ]7 }8 y# p' D/ _; U5 M# x/ L. E: Q
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. ~( |" R+ |1 L8 C( ]& N* ^$ [2 ?7 q1 z' ?6 C1 t
mv owasp-modsecurity-crs /opt/tengine/conf/) D! h2 Z) S' Z" U* {) C2 a
6 O4 f/ } O8 u; N) g
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
; j, Y6 X4 Q$ e- D2.启用OWASP规则:
) V6 y, Q$ Y4 F. ^, I2 t) t9 {; c5 c, e' h1 c# M# k
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 p' v: ] p4 l
. Z# H% V3 N& m' T编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
( E$ T, N* ]* H
. g; q1 ~$ H, _9 ~owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。( i3 _: x4 h* i% I
7 Y; e3 R5 v% q3 ~' x' U! l
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 v& u; V+ U6 Z2 T! xInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
/ z [0 E, z# u0 P9 `' bInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf8 M: L. J; w6 S a
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf+ v( W6 u; w% x9 z. K# s. `
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
- s. e0 a% b i8 s& aInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf A+ Z# Y/ B: ?: k. X* R
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf K3 x* O* h, O' w) O- t
五.配置nginx
# E$ k6 O+ K T& b/ u& v
+ J/ v9 O! h+ W. T: {. L1 V在需要启用modsecurity的主机的location下面加入下面两行即可:) P; o2 u3 a5 d4 x/ j8 |0 r
- [) n; E7 e3 D# T& J- a* Q9 S
ModSecurityEnabled on; / W8 p& Y+ [3 Y% p
ModSecurityConfig modsecurity.conf;
' U1 a! D- x3 U" h2 Z下面是两个示例配置,php虚拟主机:
, q; ~; h& V& p& M$ J( u. P. q
4 }. f8 w, c( K6 J. iserver {5 x& b9 Z7 R, r, m- I6 w. u9 |7 f
listen 80;+ @6 P: K& f. M! [
server_name 52os.net www.52os.net;
9 y- n2 K( z. m* L; v$ [+ m
/ L( H0 f- P1 [ H) A0 d location ~ \.php$ {
! p+ m5 ?, j. U+ I ModSecurityEnabled on;
; a* y1 H" p! J ModSecurityConfig modsecurity.conf;, R, l: C; B0 I0 r$ j1 g
" i4 D* A% H5 _4 D4 ]1 ?
root /web/wordpress;' |$ }4 T1 U6 V" i+ u+ k
index index.php index.html index.htm;( b, v8 S8 W \- }8 H5 x) w
3 `$ C0 m; k. r& D/ r* U fastcgi_pass 127.0.0.1:9000;
, {6 z0 P- H' |3 g" S fastcgi_index index.php;8 w$ |$ t! k& g& I
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;4 h7 o/ @7 I0 `% d* `0 W% Q
include fastcgi_params;
; W/ ], \. M6 H1 r. ]* V- l }
# F! D) g3 Y1 f9 K7 k9 u }1 \) g/ i( |$ D# ~
upstream负载均衡:* V6 f' X2 d+ u6 q4 k( s
, @0 V4 \, v+ Q6 a& L6 d
upstream 52os.net {! k/ L- a* x/ I( P# V0 |2 A! ]. n9 i
server 192.168.1.100:8080;+ i" E0 L* b6 ~9 [4 ~5 m
server 192.168.1.101:8080 backup;; }' E% K7 k8 g: v, b
}
0 R9 O3 f, t+ n& Z: O, h' l/ _5 @: u5 N; p+ W3 ], S/ F" h: S2 Q
server {
7 c+ e: i1 [: X4 G. r( d- Klisten 80;
- D& ~! Y$ N5 U4 V" C+ `7 H! U( ~server_name 52os.net www.52os.net;1 a# t* e3 B+ e& m- e8 n' x
; v; W1 @/ c1 }" jlocation / {
, e% E" O* u2 T9 w! S ModSecurityEnabled on; : T/ C; G% K& w. g/ n* @/ O( `
ModSecurityConfig modsecurity.conf; 1 u4 _! {3 l2 ?4 L
* n) M/ |7 s/ D proxy_pass http://online;
3 D4 D6 w$ V+ e# v proxy_redirect off;
6 n: Y1 ~0 x4 m9 R0 V: A proxy_set_header Host $host;6 F8 o: X- C2 ]8 n. O
proxy_set_header X-Real-IP $remote_addr;
+ B1 D8 y( u& x9 D3 { proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;, m* N s1 Q( ~. X
}
( z2 Z9 [6 y" _5 |" b4 j}
' N# R7 H0 K6 d6 B2 x六.测试. x& a5 u7 w8 C, q) G7 g* G
, g3 C7 ?, r @6 [# I9 x我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
, t2 P+ ^: \' K& k2 Q/ W% y( C# y
: s& L+ F8 w, E9 A" i<?php
7 L6 |: Q6 i* T& U Z5 f* l7 ` phpinfo();
6 ]) W; Y- T0 ?5 V7 U' L5 U) Q?>
, y. t7 X: z: _$ J( m: u5 U在浏览器中访问:
" C& e' E' F) F0 u `& u! a) E: F8 W& S3 O2 o' `
http://www.52os.net/phpinfo.php?id=1 正常显示。
4 }' {% n! v0 q& Jhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
2 w* f+ O1 W- s: Mhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。+ R7 m9 d- k* s- z. k v0 O! J
说明sql注入和xss已经被过滤了% y V; U6 P5 ^9 w1 B" H
, g: \( \% x( t七、安装过程中排错. d0 f: l$ z9 w( e
5 q7 k2 y$ E% y9 C1.缺少APXS会报错
q5 _" Y+ d( K' Q* Z8 X4 n7 t0 |# U. {( d/ c" ]- C+ d; V
configure: looking for Apache module support via DSO through APXS
* a% ^. c% b+ r* Oconfigure: error: couldn't find APXS
: A6 [1 G: k" l0 W# ]9 qapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
3 j% X: V8 I' I" Q; w' o7 U解决方法:( A5 @5 Q9 }/ p7 o8 W) i
. J$ c' q7 \$ Z, b! g! pyum install httpd-devel
! R% c6 g0 @5 N, q0 U# G2.没有pcre. V: [ Q4 ]+ l; V7 M. a8 s, n3 i3 v
0 A* ]/ r7 ~; N$ }
configure: *** pcre library not found.
* @! x9 e$ _6 V$ x4 `2 aconfigure: error: pcre library is required
" |$ Y/ q& u! H解决方法:
h1 }% ]0 u6 [7 x5 `2 k, O
8 u- ]/ C3 X# p: d5 qyum install pcre pcre-devel( O) H; i6 s; L. I" F9 C' q9 y
3.没有libxml2
( C, o- ~5 a' |' ^+ V/ d2 w( q
7 M/ \6 m7 l* W
, |8 m8 [1 G3 b7 ^' Fconfigure: *** xml library not found.5 L5 r! k" j# c+ @! I
configure: error: libxml2 is required
) W& n- o4 m0 B7 r8 K* @# s8 m' j解决方法:
2 p: B& R5 `6 {( o4 d# }( X7 E6 ^3 g4 D8 u5 g! ]
yum install libxml2 libxml2-devel( ~0 I9 P. q% t, K* C# `: p
4.执行 /opt/tengine/sbin/nginx -m 时有警告1 I9 r8 j- n1 p
" I5 \# i* _" @8 e Y3 u6 X- K1 L
Tengine version: Tengine/2.1.0 (nginx/1.6.2)2 R* ]( R- L% ?. o: `
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!( d5 [8 R( e8 a- I7 z
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
; g1 O& ~8 S( f$ F2 w3 T3 m2 K( A4 K2 [6 s# N. L. Q5 r" E# S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
6 o/ t$ m" a7 J) k' P2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9" ^6 i4 O* I5 f* u* [
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
; x. @9 A% K) ?0 I: N2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
0 Q: E: M; j' j- k3 p W8 H$ @1 J' K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"8 T+ A; I; x- G$ t
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# W0 z- m1 ~ R2 C9 J6 D
解决方法,移除低版本的APR (1.3.9)7 A3 ]( b. i/ t9 n9 J( K8 p" p
7 ~/ t# Q2 `% n6 B% h
yum remove apr
( X/ V* ]# Z" h) s7 Z: {5.Error.log中有: Audit log: Failed to lock global mutex* {( f. v* C4 D* w5 F6 v+ E
7 a4 g. Q# ?( [5 `$ I) f
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
! d* R: e) ~: \1 T3 l+ jglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]0 T; P' R0 ]& Y4 m; _- w5 p! E
解决方法:; f: Y$ l: w; `* ^' V5 e4 F7 i# v
编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% o9 U: p" ?. y& F0 u, V* x/ }
6 w* P8 F% t1 mSecAuditLogDirMode 0777
+ A8 o: Z+ d& g2 m5 B- KSecAuditLogFileMode 05508 P' _1 _, O6 x
SecAuditLogStorageDir /var/log/modsecurity8 X1 G) Y' T) w# } z" @
SecAuditLogType Concurrent' P( u9 A" W" i3 I2 I3 U; l7 ?
参考文章:
& c# F( a, `5 F! q& Ahttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX9 K2 Q( s, k4 t6 D. W
http://drops.wooyun.org/tips/2614 |
|