|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。3 k1 I" B3 o& C
9 w8 D+ d, |; E1 c% E
一.准备工作. b( e/ b% T+ m* Y5 _
d* x: _0 o& R4 E! v- m
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
/ T7 Q& }; y! n1 t8 d `
( t/ s8 ?' v& G: f7 r% B3 ztengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz. m8 s( X$ U5 c
/ Z* r$ V0 q, R( g, a! J8 `
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
+ @5 x) H! s0 _& @9 t9 A% E1 o3 g+ Q& V2 Z3 Y
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" X$ f6 f( \: P; \# n; s
( M6 ?8 X' H' h8 E) ?
依赖关系:
h, y# ~; L* o9 K0 ~tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. ?+ g2 I p Q. l! w0 [
8 V2 `; }: y5 u, oyum install zlib zlib-devel openssl openssl-devel pcre pcre-devel0 U' E. E) r( Q+ Z+ ^- K
modsecurty依赖的包:pcre httpd-devel libxml2 apr
6 q4 V6 |" h. s, F) }* q* v) U$ @8 v" J
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel+ w) z# R9 P+ O1 n9 v# Q& [' d: |
二.启用standalone模块并编译
) l, e! b, t; b1 z
; A" q$ L! w1 W C1 s( T/ C下载modsecurity for nginx 解压,进入解压后目录执行:6 Z5 F2 Q" f" n% |' z
+ U; n1 p) V0 S" B3 j2 T3 p./autogen.sh
& ]+ S- h5 S/ K H) h./configure --enable-standalone-module --disable-mlogc
5 j2 Q$ d' j6 Zmake
9 z1 _% ~, k' @2 k- \三.nginx添加modsecurity模块
7 A U; s; Y, d+ D8 I* T1 [1 p( Q
0 i4 e/ {) m' A7 l* G8 b在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
4 l0 r2 m; E& J( b6 N+ s, H9 a4 n
# G& }% h5 B- q3 [4 I. q./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine; [) v/ ?' c' H& E* _/ z
make && make install: n- R9 s$ S7 ?
四.添加规则
5 I1 |5 M5 S2 [ C6 v, ?* T$ e6 q/ d" g
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
5 I7 Y- o5 R! S# c
0 [$ j$ y) m$ w) o0 T- i% ?/ G1.下载OWASP规则:
, `. q/ Z+ p$ L- W p: d) E3 E6 L. P8 v
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
& I0 g& Q, C( d5 L; y( q$ y4 P
5 r8 p* m! g% p& X* D" vmv owasp-modsecurity-crs /opt/tengine/conf/
8 Y1 t9 K+ x7 K; a! q% s! I. U
2 o# Q* P6 Z3 m# ^" c" i9 {cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
4 F; q! v8 x9 P! ~2.启用OWASP规则:6 g1 v% B6 R N
1 ?! @' {9 y! z9 v3 l' a复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。8 w1 ~5 s, y: E' c4 ]
/ N; v5 p* R& J编辑modsecurity.conf 文件,将SecRuleEngine设置为 on" U4 M, W2 @: B( N' X+ r) @) I
a7 D3 _+ z cowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 y& D( J# S& F, K1 l# i" o
5 j3 @) u _+ v4 w$ d j* ]6 AInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf1 |1 _; \: n4 f! H# _& E$ Z
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf8 T5 U: t- M! R" \; f* E0 u
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
& I( t" t" C: J& v& Z4 V) mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf; ?9 p$ s2 L! f" o3 _1 n8 a
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf4 I) k+ R4 E2 ~2 o) }3 {+ o" T
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf# A& E2 v3 }2 k/ |
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
# `( I& c& h3 U6 b* M' U& a6 e五.配置nginx
3 ]7 F5 h: a) s, x4 _+ a$ L @. F' K9 b8 D( o# Q+ ]( ?: _! E" o
在需要启用modsecurity的主机的location下面加入下面两行即可:' B. j" @$ A8 H+ u+ h4 m0 C+ N
& g( l" R! ]9 U+ K9 b+ A5 s: g0 L* f
ModSecurityEnabled on; ' T8 C& v8 Z! n% K I" T4 J
ModSecurityConfig modsecurity.conf;4 z5 M w8 C& m0 u. R
下面是两个示例配置,php虚拟主机:; }# }0 Z8 M: i o: g$ p6 |1 P& b
+ `+ D4 |6 u. ~server {0 @- {& R6 F& l0 [5 i
listen 80;' H* \1 d6 `" K
server_name 52os.net www.52os.net;
0 b' } h# s; l% G, M, z * p; b2 t1 B3 S; l$ b& n. c
location ~ \.php$ {
4 O! T* G" m. d0 j ModSecurityEnabled on; : ~1 [4 ~6 [) r$ h" l$ }/ P
ModSecurityConfig modsecurity.conf;
" B- ^0 O( p. W7 `$ r
) L$ J" g7 \ l& h* T8 \2 ^ root /web/wordpress;
/ A6 D- r8 m% }9 \$ B0 L& C- x index index.php index.html index.htm;3 y! }* y+ m2 P3 W/ x
: c/ Z" D4 h7 o6 J/ P( n1 A/ U
fastcgi_pass 127.0.0.1:9000;6 M4 _ |- E1 @: L
fastcgi_index index.php;5 T& O Y2 O2 X5 t M
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;3 O( Y0 L* X$ g! W
include fastcgi_params;+ L* Z5 G: R% b, E
}
/ W1 [1 O: x! H( |8 L5 w g/ c0 ? }
, D/ r' i2 z/ z& p! g, D" x9 \( @* `upstream负载均衡:& J/ [/ b- [8 D7 T- y
1 e# T! A( j' V' W' ?+ U
upstream 52os.net {0 f( Y& l4 [; l& h+ Z2 k
server 192.168.1.100:8080;
* w+ I. X! I3 I/ z# T2 Z server 192.168.1.101:8080 backup;
; i6 [4 S6 p6 W& {4 b4 c}
3 x" R7 E- P* k9 W( e1 B) u$ L6 J L) g9 r' P$ V* F% E
server {/ k6 _& O$ S& j* `4 f: ^! s
listen 80;
% R5 o9 H- L) o- I' j$ gserver_name 52os.net www.52os.net;
" }& `( l. K \$ X3 N/ D- m1 L9 A4 N. Q0 s7 h8 ]' n
location / {
; ^& |6 w* } x5 o. t ModSecurityEnabled on; $ m# k/ b. }1 I6 p* d5 J
ModSecurityConfig modsecurity.conf;
/ V- d% j w1 T5 m" X7 L9 `' G( E: ^* c1 J, N
proxy_pass http://online;& w3 [: K( [/ u1 _& ?
proxy_redirect off;
- H& ?. r+ }3 E/ |' l1 _ proxy_set_header Host $host;# W- A4 e" B% M1 n2 ?# d" m
proxy_set_header X-Real-IP $remote_addr;
* ^ n6 k: O; P1 M8 k. V- b proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
) \* \' x p& i1 ]; V9 u9 h }
1 E# N: N' r& \5 W2 \, E( t}
3 x% U% k: Z1 s5 B4 C5 G六.测试8 u! b) A- w# `+ g. G; [0 k* \, }
' J2 c6 n" n2 A8 N3 z
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
+ S/ j% x0 P3 q3 H! N/ g3 g- H \8 w0 a$ T0 ^, X; R( u
<?php0 G0 k# {1 X' p8 h( p
phpinfo();
. p9 ]9 R3 |6 @?>; s* L9 h) X7 d# m$ |7 m' s; z
在浏览器中访问:! W* v; J2 o% v1 ?6 c0 x
. X6 }. h8 L( i3 {+ `http://www.52os.net/phpinfo.php?id=1 正常显示。, f8 O+ n5 t$ }- ]) W" N3 H
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。; f0 h/ U/ X/ g- p; U) ^/ _
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。0 L9 Z9 @9 R2 P# u
说明sql注入和xss已经被过滤了! @ c1 q7 `* n. |9 N; A
1 g+ A5 m. a* }/ W8 j8 o七、安装过程中排错# ?( u% _ t k8 u, E
7 X5 s6 ~ ]# W& J
1.缺少APXS会报错
* U- f& G* g0 d: Z0 Y: p4 v% k; \9 u, l: d
configure: looking for Apache module support via DSO through APXS
: O8 Y# l2 x& Z/ w: Qconfigure: error: couldn't find APXS/ ~# T2 ^& w. C
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
/ A& v- N5 \" _+ s+ B解决方法:
4 J. S2 u% }* B! X4 s9 H( G
4 @5 x3 w& @4 M \& ~ i$ f+ Fyum install httpd-devel- u+ Z, y# t, H. ^5 l4 u7 ^& A
2.没有pcre8 e+ n) r! i% J* L7 z% v( \
. A2 c# m2 D5 U5 z/ w
configure: *** pcre library not found.6 n1 j) J5 b# d# g# T+ h; N
configure: error: pcre library is required
! f: ?/ z T" v/ P) O2 {: U E解决方法:
) R X) Q+ ]! |$ u' X& ?9 Y9 i( P9 a1 s8 c0 V
yum install pcre pcre-devel3 E' S$ ~: L# g. v& I
3.没有libxml2# l+ t/ N, b1 D
' T# ~7 A% {. h7 k
! w2 e7 W9 D! I' k- V1 e& w1 W8 J
configure: *** xml library not found.( `8 z9 v$ q* c' f( d! {, N
configure: error: libxml2 is required
% `+ n' l% Y: S0 E4 @- X6 f解决方法:1 F' y! l% u# x, D$ K2 z
0 G* V1 Y8 i& w! N _9 E% w; k
yum install libxml2 libxml2-devel
s; P6 A, T0 N/ z! a4.执行 /opt/tengine/sbin/nginx -m 时有警告
1 b0 v. x4 V$ p8 K. @6 s3 p. P4 f2 O& ~/ k6 O% I7 e2 k3 P. v+ m4 r
Tengine version: Tengine/2.1.0 (nginx/1.6.2)' T- K! u( J8 Y/ C4 d) z; _
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!5 Z( D& V! z, A2 I) ^
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
1 Z. G/ K* k, s5 F4 T) e' m7 n/ [+ q8 \0 q2 _! F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.1 ~9 S, z$ y& u3 o( x7 w r
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
0 L4 V$ v2 l- M/ F2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled! i- H. o# q; m! |" S% R$ ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
3 i' f. m" J: ^! K2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"6 d# b# i4 \+ H1 a- A0 D
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.8 M9 V5 p. B' b8 z. ]" c
解决方法,移除低版本的APR (1.3.9)& I. j2 c) U0 W4 B* E2 c5 R
' l; }' M5 Q( V1 A# c1 h
yum remove apr
v! v* o+ k- T U9 @3 p& l5.Error.log中有: Audit log: Failed to lock global mutex
- B- W7 |; G& R7 t0 H# ^; M" r
J3 F- l; @7 }7 _. V) q; F f* p2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
. B! U( x1 `3 ^0 Q: Z1 {0 P0 Rglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
" K% H; j. K. d' W- M5 G2 d+ p解决方法:
' A8 {% X) I+ T: h编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
% I5 ^* s9 ^( @0 l
" I1 N6 Z! v8 GSecAuditLogDirMode 0777
/ C- ~) O% p5 L2 j1 k! f" WSecAuditLogFileMode 0550
s& W9 x8 E; U+ lSecAuditLogStorageDir /var/log/modsecurity
1 [; Z `) T1 R D; z3 V9 ^SecAuditLogType Concurrent
' z) p* P4 i z/ b9 Q4 @参考文章:& q; }# O5 z( L4 | _! i! k
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX( S+ u0 p; `/ k* p1 s8 E
http://drops.wooyun.org/tips/2614 |
|