|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。4 ?: H5 R3 k: T T e6 z
& Z' U% y1 Z1 ?* v8 M1 N: a- A3 X1 p1 z
一.准备工作0 \+ d7 [- @+ n# v" d+ G# y
( Q4 S2 l' z. u6 e6 T
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
0 C# k# v7 ^: G# m: L7 K" h- Z3 D: Z6 t/ O) l- `
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz, x9 {3 h4 m2 V; L" x
, V1 I; @8 A# z) }6 I/ D; P
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
9 }' c! `6 {2 ~4 q: \# |
: ^ j- o! \4 E- F9 i' c. ?. eOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
% Z# e d6 _7 I# ?- d/ z5 c( d
# q. ~% N+ K' I& o依赖关系:; R4 G- D j; l) _- N) w6 t' E
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
2 y# o, J! o/ x& I
9 C. B' B) t. g V1 dyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
% m, }$ Y' [9 Nmodsecurty依赖的包:pcre httpd-devel libxml2 apr
3 X+ i. q# g6 }
8 d/ P. x+ j* o3 M+ wyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
7 Z2 g3 U$ k' Y8 m3 G* p( e6 ^7 w1 j二.启用standalone模块并编译
' g3 ?7 N5 x+ f X8 k1 I/ f2 y2 `5 W5 X' v( s S# Z- W: {1 ^
下载modsecurity for nginx 解压,进入解压后目录执行:; C6 W* A4 n8 H
: D3 A: X- G, k! f4 t A
./autogen.sh1 n, n6 c! _7 t0 O
./configure --enable-standalone-module --disable-mlogc& |/ m$ A: M+ P& h7 _5 }
make ; T# d! Y2 t5 I# c
三.nginx添加modsecurity模块
" o. M6 ~8 k0 e$ ^: \
; ?3 ?2 y; v* e; x3 }% i在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ ]. V, g3 w) j
& W" Y, h {6 b./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
- K, n+ U, Y0 I. w0 C9 b2 y; imake && make install
- G3 ?1 O# @+ r! \四.添加规则
3 y* g! v6 _* H9 k. F8 N
4 ~2 }7 D& G- z( B5 y* umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
# x! ^$ V, [: F7 A0 H
/ K2 f! k+ k0 Q! F- R1.下载OWASP规则:
- q/ T' V8 f9 L E$ \+ d8 Z: m, V4 I- U' z
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs! p/ \( b' w( Y/ S
1 [: S' C% V" E7 j8 T& H, v* S
mv owasp-modsecurity-crs /opt/tengine/conf/
& n3 X, E& r6 c% @
) n5 H, ^" u9 F3 f& Q1 y, mcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
& m3 r6 \1 o1 N' ^* A" d2.启用OWASP规则:
4 A9 V: b0 _" `2 f, d3 ?
) J. p5 S: _7 F% P1 F复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。$ n. ~! s7 L' X& f4 T6 Z3 ^. H9 t
* V& J& I. l5 `编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! L8 Z/ I& g' D9 K! }. u3 j" m
. K2 x- K+ p t7 }8 ]* B% f5 Z
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
; g" x4 R4 w0 q$ Z* c3 s
6 j- Y, P+ N; S4 I: D; {0 X) XInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 ?! u, n$ }0 g0 ^, N1 K- ?Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
0 Y' C4 M( _# c* z4 n) Y$ yInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
7 o! s# b+ K. B, VInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
' V6 v# n& b- ~' @Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
# o0 v5 L' j: s; V; r( u6 lInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 T. e% \+ k" q* P
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf& x! w+ V3 o0 S3 n" P' H
五.配置nginx
) U* D. @7 G; R$ ^
3 ]! t7 @5 v6 u+ ~在需要启用modsecurity的主机的location下面加入下面两行即可:. r$ g+ u T1 N$ P8 T
, g# D7 G# c# jModSecurityEnabled on; + E2 }8 h& H2 t" Y4 \( K# @" R+ d
ModSecurityConfig modsecurity.conf;+ g' @8 A0 a' j- d
下面是两个示例配置,php虚拟主机:
1 Y; z2 G: x. F% i* w% s1 n
, s' ]( j5 e- zserver {
: X) V" z; r* L listen 80; C$ H, x' P K
server_name 52os.net www.52os.net;" Y, V2 ~ F& m! b3 M) ~ q
0 Q$ h) p! I- v5 j( H: t location ~ \.php$ {
& V% x7 G* \, ]; t) w4 }! u ModSecurityEnabled on; & Y1 k" {, g! ?# ?/ t6 m
ModSecurityConfig modsecurity.conf;
$ C6 v& L7 A) b+ z& T9 a2 C- ~
) z" Z$ N7 G# P root /web/wordpress;
7 l0 l5 n0 t9 b2 o+ C index index.php index.html index.htm;& \2 E% d2 H+ D2 I: |
7 {. h7 h T& _5 E& _* E. s( b# F fastcgi_pass 127.0.0.1:9000;% u/ b: B0 [% l, S0 z: J$ |; o2 Z
fastcgi_index index.php;
) X6 d$ v0 q$ V$ _, H# ~* h! |0 J fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
- M. @8 I) S C6 b include fastcgi_params;
0 L. Q/ B$ x. n4 ]: e7 P* l }
0 Y8 Z2 R- E; P- a }$ W- @" l: v6 M% C
upstream负载均衡:5 T0 \* W9 ~: b
w: r2 m" |3 e8 Y5 z
upstream 52os.net {! H: P' B# R$ s7 u
server 192.168.1.100:8080;; P6 D& Y6 Y0 {8 u) `$ m/ A% |; d
server 192.168.1.101:8080 backup;6 k l: L. `- V* y3 E) k/ w
}
3 j0 x Y* e' d6 Z4 @3 I8 v7 C" y# c6 c' ]" M
server {
) v( J$ T. ]) v H* [listen 80;# d5 Z* h w: ~( `+ J/ [
server_name 52os.net www.52os.net;' } S& o! ]4 c( `+ |. i8 @9 e
' y6 M* ^0 e2 A- v' F5 ]
location / {
: W" ~# n, f7 b+ q) |8 O, |& E ModSecurityEnabled on;
& h" K; h# J% H3 ~( D4 } ModSecurityConfig modsecurity.conf; 6 T* Q: f" @) u5 B& D4 k: x! [' z
: }" o$ T; }+ o* n8 g, A
proxy_pass http://online;6 U8 x8 k+ h. T" Z2 ]
proxy_redirect off;. B# R7 p8 K6 I9 w6 ^* i, k- _
proxy_set_header Host $host;8 v+ A. C' A6 q+ X
proxy_set_header X-Real-IP $remote_addr;" t$ c5 M- b7 V" ?
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
& `4 M, }5 A, U+ J }3 A x2 T6 f2 C# \# F: [$ [
}7 \5 i/ m; I5 P. [, [9 f% v" t/ U
六.测试 Q# |( Z* J6 G0 |7 x9 n( R: N2 X
! V; @! }9 p$ ]7 e- B2 `& m9 }
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:/ {& R' {" M/ t+ V
/ C9 b4 Q6 t, b' k3 q# [8 n& v
<?php
$ `/ p4 @* }+ ?+ V phpinfo(); / i9 l5 [9 O! t; f
?>) _0 H8 i+ T# d7 \: W S
在浏览器中访问:
" U, j/ q. X0 x* F$ h4 F c# E! `8 b) @7 q9 ~
http://www.52os.net/phpinfo.php?id=1 正常显示。- s- q+ d& [& Y4 x
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。" K& H! @. ~2 H2 K% q2 d0 u% J- R5 M) b
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 E: S2 j+ r1 l g8 \+ k. K
说明sql注入和xss已经被过滤了) ^! y( ~8 x; D# k, }
6 d5 {# I! a$ n5 }% Q
七、安装过程中排错5 z. {, i' N' h5 _( ]
/ J! t! ^! s, p ~1.缺少APXS会报错; g/ D2 L' A2 h2 z: w1 R
0 [. V. K0 Z2 \" H' r7 wconfigure: looking for Apache module support via DSO through APXS0 w, j% p! o# V9 q I. }
configure: error: couldn't find APXS
; g5 U: g4 L4 D, \3 k3 a' bapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
4 \) E- f, C: p/ a/ H7 A解决方法:; J/ H9 q* p O& t7 P, P
8 O1 Z0 ~- h: M5 Nyum install httpd-devel" q, y2 ~$ B# [+ ^! U5 E
2.没有pcre* B% q' X8 K2 a2 k; Y& \" t7 O
* V" o; n- @2 i/ G: Gconfigure: *** pcre library not found.) [3 b6 Z7 y7 q& _# F( k; ~/ P
configure: error: pcre library is required2 y. p! A) _. w ~1 P
解决方法:
+ b5 a( W1 ]& o$ c+ G7 `. n8 Q7 N {
yum install pcre pcre-devel9 c3 j) R1 ^+ `1 L
3.没有libxml2' C8 _1 G. }5 u6 o P* C+ Z7 F
# I9 l1 @9 C) m! z R4 B* }
P+ f7 F8 [" \- c$ j$ Rconfigure: *** xml library not found.0 D# U" D8 [' k% y3 W6 ]. N
configure: error: libxml2 is required
+ l- p& r" m& G, \6 t解决方法:
h: `3 }% H7 G M3 X
Q+ N. N1 m% T Z4 Ryum install libxml2 libxml2-devel
4 M- V1 L* S H7 l0 i: z: K% f4.执行 /opt/tengine/sbin/nginx -m 时有警告
, ~8 z# T% Y P1 Z( k/ o( g/ V3 l' f$ [' l) n1 Y' R
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
% k* Y, b9 {3 Qnginx: [warn] ModSecurity: Loaded APR do not match with compiled!
+ c" f5 M. K7 ~原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
S r9 U% W; H a+ _3 p- E6 r) \% q2 P4 M* k. g" u
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.8 n& t& u7 `% D4 m5 q$ t
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"$ g3 V7 F5 t& W1 ~
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
l! n2 J o) V/ D! X2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
% |0 ^; X- K) L$ u1 ]! l/ C2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6", W! ~! m* D$ l1 u, T2 l( _0 B4 `
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.' h3 o+ N) t5 D7 m( Q! M% c
解决方法,移除低版本的APR (1.3.9)
# U# i2 T* j" `
" a+ H2 c3 W; A2 F% E4 Q1 Qyum remove apr* b8 K) ]7 T1 {* U; A. \" D' T9 E
5.Error.log中有: Audit log: Failed to lock global mutex& N4 x+ Q# {4 l# @7 x. {
" J B& i, F5 y% Q$ j! X
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 2 h) W) K7 c$ u, {: V
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
6 y9 T) h, p1 C7 [# m6 Y' \! T4 n解决方法:
+ a% ^, `' W# x" h" L4 J. i4 S5 L6 W编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
, Y. e3 b# R2 s4 w3 b5 ~! r7 L4 }0 H, C0 S4 Z0 o& d% _' {
SecAuditLogDirMode 0777
- g4 u! i# Z6 g# B2 G' v1 gSecAuditLogFileMode 0550
/ E F' ?+ U- rSecAuditLogStorageDir /var/log/modsecurity& q* Q: G4 d+ ~3 F$ V
SecAuditLogType Concurrent
9 b( F% p8 g% d R9 h" x参考文章:: {1 V& U$ \0 o m0 U+ }: w
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX8 f! T4 [0 F! p G6 Q+ v. N
http://drops.wooyun.org/tips/2614 |
|