|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
6 t* k' b+ i& R, E0 e1 v' p: b" r, t* Y5 ~2 p7 t; l* E4 A. R" p
一.准备工作
& ^3 p& c4 V* V! ^8 ^/ U L% D. J& W! H' E& l. O0 Y* P/ E& c5 e
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0) y3 G: @, l3 R' J
; k3 W7 d$ q. }5 Y0 Xtengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz2 I P" D+ z- d" I1 ]
# D: D P- t6 mmodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz: A$ L$ P7 q$ _% w; g3 @, p' O2 U
3 V2 j2 q! A( }+ f: N8 c
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
6 u. \/ U: Q0 v1 o( J5 R6 `; c$ L! Z7 o
依赖关系:$ i' a1 ?2 y" q: |
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
Z: Y/ s6 B* {, P3 K6 Y. ^9 d$ j; Z- @2 b
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel! [. S9 C& R* N0 v6 L; e9 i
modsecurty依赖的包:pcre httpd-devel libxml2 apr
8 e8 i% _) n- {* G$ h x3 `! K! t$ _: @" i$ {9 _4 p
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
* j% f: X# {: M, Z+ g二.启用standalone模块并编译; K/ `* I0 {, ]2 A* @! v6 ^% r
0 p- V! Q1 ^" ]6 ^
下载modsecurity for nginx 解压,进入解压后目录执行:$ i7 P- D( i$ k1 t9 H0 I
- V) _0 ~8 o7 L
./autogen.sh: S1 S j8 n' T% ]) a& n/ Y' b( _7 K
./configure --enable-standalone-module --disable-mlogc
0 S0 i" R( X/ r- v' \( I/ vmake
" D0 j& y+ f6 c5 M. q三.nginx添加modsecurity模块% ]( ~9 u7 ?4 s9 A
' w6 Y& X' I+ g7 f* U
在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* ]. }; O3 e6 A- y! c- g; f
6 a9 @/ \/ [$ }$ j" t' }./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine# g) Y. B+ e: H: L D) C8 [" Q$ F
make && make install3 K9 w5 a: M S
四.添加规则
3 I( ~; t$ v* t! p& n" j- J+ [/ B6 X
3 S6 [4 ~ `8 y% i$ U% N0 Ymodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
: k: E, N( [% C% I( s5 H( t& I: H5 o1 h0 U5 w! q+ _$ W& \2 P
1.下载OWASP规则:
% M% o/ {" b! N% \, i
; Q0 H8 D' }3 F4 l' H$ x3 z: ^git clone https://github.com/SpiderLabs/owasp-modsecurity-crs! y' K7 @( d1 c4 v
7 H7 J! W5 V) c( ]
mv owasp-modsecurity-crs /opt/tengine/conf/
$ J6 J' W$ b: B$ M% ]( Z( B) V! I# \# d
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf) j7 L7 o& R1 u6 a2 B
2.启用OWASP规则:9 J$ f2 _6 J1 {9 O1 [6 J6 L/ l
( g+ }6 B1 D9 X. u
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。 N( s$ L4 `& |' D; y* _3 z* s, [
8 C" j3 T- U6 F" o" w; j! O
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
1 r; e0 F" C% I, T7 G7 W# l8 _7 U7 p. g( k! O
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。9 I8 J& D4 _' M$ |' x" Q& s
! _2 y- `7 w( c' |$ \) v4 Z0 ]5 @
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" L2 F+ ~ y y) FInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf/ F/ Z% {) [6 l# p7 ?
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf5 H/ D& f$ H0 Y& C- e; t* B+ x
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf/ \- m$ g) n2 l4 R) c2 T* T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf/ `1 }$ i; k0 Q; I- ~9 F3 B0 z4 w
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf) V1 @" B$ T9 a/ c
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
! U' t/ q3 b9 }( g: j; z五.配置nginx
R5 `- N. E3 {: t* u$ |) I
, z% D+ Z0 }+ d) P0 L在需要启用modsecurity的主机的location下面加入下面两行即可: d% }# v' v, n: v# B6 j
3 y4 K' f& Y/ ^: d N2 T
ModSecurityEnabled on; ; Q/ j0 F9 Y& l/ o. p4 _2 i* i
ModSecurityConfig modsecurity.conf;: e6 A; X- o. e
下面是两个示例配置,php虚拟主机:
! h+ E# M: f( ?3 g. Z9 j' P; P; m8 e; F" b0 c. D3 l$ O9 M
server {
$ w8 T, W7 G- J& Q- q- l5 m listen 80;
" @/ }0 L% @- s( o: U" s5 { server_name 52os.net www.52os.net;( Z8 R! W% I- ^2 `
7 b5 P Z9 q4 L4 a, k- [0 A
location ~ \.php$ {! ^* {* @( V4 ^- h4 q* w
ModSecurityEnabled on; 5 L" x* Y V7 @% |1 l" ]1 `% j4 j2 d
ModSecurityConfig modsecurity.conf;) H/ {+ q6 p0 D1 o! f
, u- T( S2 Y7 \/ B9 E! t$ v' H
root /web/wordpress;1 W5 m2 T r+ i" L( P2 Q
index index.php index.html index.htm;3 W& `5 T2 b" o3 s3 j
& U' s1 D0 H& Q
fastcgi_pass 127.0.0.1:9000; l# F4 W! a' l( F6 c7 n
fastcgi_index index.php;
3 r1 t% L5 L1 j$ _% Z fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
! x/ X$ Z( H! S, j include fastcgi_params;
& w; V! A7 e# d }* T. w3 e* d; D- n
}, V6 t" \- g& R, A7 @1 P" ^
upstream负载均衡:
' w; ?' X6 K+ A
* i: r+ t- ?# J% mupstream 52os.net {
7 Z9 {4 _8 a7 X1 n \: ? server 192.168.1.100:8080;/ ^3 q/ ?! M! s; d6 X0 @; n
server 192.168.1.101:8080 backup;6 k" R* F' w& ^# f. z! j8 F
}
$ o* M9 K( ^- ]( S
0 T; [' K3 m4 L1 \server {( e/ {7 A* |1 Z0 S0 n- {% I# t1 X
listen 80;; o8 t" g/ B! k. [
server_name 52os.net www.52os.net;( G: Q! z% V# N0 v
8 u6 K- g6 o9 T: Z, S
location / {" m% [$ i5 o2 p
ModSecurityEnabled on; ( s& N# x4 k3 W" I: [0 m2 M
ModSecurityConfig modsecurity.conf; - v$ I U3 h' K) b6 @
/ Z; D* E6 R; }. n7 t proxy_pass http://online;" u/ o$ ~7 f7 v8 e
proxy_redirect off;& Q! N7 _7 O9 N/ N
proxy_set_header Host $host;( a+ {4 l% @0 e* f4 `( ~3 Z ^8 H) {- L
proxy_set_header X-Real-IP $remote_addr;
3 t; {' l0 R0 D$ r proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;5 b7 Q8 M( X N3 M P
}# Y& x8 P6 Y- Z
}9 C4 x* j0 r+ O, J! K& D4 `
六.测试
! i" p2 @& P) f- B6 M& `% g9 h2 A" a/ L; ^- X' O4 m, x
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:5 E; @2 D. }7 w3 d
5 \% s7 ?: w# r- U' p$ t4 F8 [; [$ s<?php
/ s4 M- n9 `/ O phpinfo(); ( ?6 m) k+ I; F! d
?>6 c M+ I4 a: T& V* @3 @
在浏览器中访问:
4 A* q7 ^% F+ S& n. _- r' k+ e, V- v
http://www.52os.net/phpinfo.php?id=1 正常显示。/ E, a$ h, g" u4 y
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。7 @9 a0 d" W5 m' F
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
( ^9 t9 q( }$ A% a7 P; f' e: I8 J说明sql注入和xss已经被过滤了
R- _# B. x# O
# X0 N) j- u$ k$ [" d4 o3 z2 r7 S七、安装过程中排错
2 {: e8 ~# t. \- ?" E; C; Y' ]
+ t; s0 W" S8 m3 e- m1.缺少APXS会报错, w' F' j2 r& i& Z5 L% H8 C
9 m2 k, L' k6 f% T `configure: looking for Apache module support via DSO through APXS
+ F+ V i# ^/ Lconfigure: error: couldn't find APXS
! ^/ m% r3 r: O1 P) Aapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。. G) z. S+ \3 P8 ~( r
解决方法:) U4 N* X" R4 w2 J
6 l4 U7 B& ^1 z# e7 wyum install httpd-devel
/ E0 A; d% j- M7 L: h; x) y8 {2.没有pcre4 r2 U0 u+ u8 g: F! B
! J; a( @3 b J0 Nconfigure: *** pcre library not found.) g8 T* l0 g- X- Y: d
configure: error: pcre library is required4 u" G" d1 @1 b4 h9 ?0 X
解决方法:( s& n$ u8 Q. ?; u. B( K/ l, \ s
8 i$ d D; G) N5 Z6 E) z, @yum install pcre pcre-devel
6 X9 j9 c i1 ^3.没有libxml2! ?4 q, b3 D1 i ^4 [8 T
4 T, L/ ~- r) J3 {' g, o
! S$ K6 j% A7 v" M3 gconfigure: *** xml library not found.5 n% h- r+ R' t& I
configure: error: libxml2 is required
* y2 l+ m& f A( u% r, m, k解决方法:
; ^( ]3 Z& t% ~( Y
1 o5 v2 M( i/ K0 t. gyum install libxml2 libxml2-devel( O' [! N. J- o. e
4.执行 /opt/tengine/sbin/nginx -m 时有警告 B$ B9 t* Q* z7 N
+ m B8 S' {4 R3 ]6 m2 v& G
Tengine version: Tengine/2.1.0 (nginx/1.6.2)9 A/ ?7 G( E# O+ j9 Q0 X1 |
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!7 X, \8 z# d4 Q4 O" q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# ?: i5 W- o; @0 S
: r; i4 M% D2 w+ o* c2 O
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.. ]# B8 Q3 c+ d4 K" N" s
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"6 L2 ]: Z( }. G2 {! D
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!2 Y9 z& {- t6 C; l+ T
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
1 A5 |2 c$ z+ {4 |; ]2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") L4 O5 A2 t# H
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.$ e4 o( [) i% p" a3 S& T* n
解决方法,移除低版本的APR (1.3.9)
B; y! y5 a3 |$ d1 \4 X( t0 T5 j0 g' _8 z+ e ?5 H
yum remove apr2 l5 E4 ~! g1 q v" J' k; y
5.Error.log中有: Audit log: Failed to lock global mutex$ t# Q+ m, e; c- L0 S6 S! H
7 w9 d3 o; Z; W2 v' m& q
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
( C0 c1 |8 r. g0 U2 e- M3 Aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]: s {5 s$ J$ l/ r+ U, F2 g% q# M
解决方法:
4 \8 S, H' `7 L: t编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:6 x) M, @( Q, w7 C; Q- B& R
1 V& y Q& a& r6 X
SecAuditLogDirMode 0777
. D( i7 K+ V, WSecAuditLogFileMode 0550; `0 f7 ?7 B. w
SecAuditLogStorageDir /var/log/modsecurity
/ r# h5 O K+ f+ W# XSecAuditLogType Concurrent3 ^, _5 l v8 K, _) t; ?
参考文章:
# h% a/ y2 a3 phttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX) N9 H5 |1 R( J/ K1 _" p
http://drops.wooyun.org/tips/2614 |
|