|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 R T- b1 _' ?7 K# h3 `, B% A0 |0 y5 _. m% K
一.准备工作1 A& r) |- k4 l
3 p# [" b$ @7 q" b1 r$ o; m系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
4 r' I. `4 l# ~, p5 M( t/ R2 Y+ y" x& o2 I3 Q4 @
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz% ]2 c4 m- p+ Q) s4 |: ~, k0 z
D' z& k- r8 f5 w2 ]
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
; `+ c* ^# w( B% @1 J+ Z. _
) _2 e' l1 u" R6 o8 rOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
2 \$ [# N9 L9 x
9 p/ {- b1 V6 _) E2 |, C7 A依赖关系:# D0 ?" v: B8 u5 B
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:4 k/ G6 t% i( E" Q0 a: M+ p1 B0 N
9 i5 q1 k5 r& v7 B7 n0 M Ryum install zlib zlib-devel openssl openssl-devel pcre pcre-devel X8 w9 p% o7 b2 i3 J4 X9 ^
modsecurty依赖的包:pcre httpd-devel libxml2 apr; @% @% x3 Q: c3 J& E% U
3 F1 T- D. d% B+ P' v
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel y/ s: D- i& ]2 @
二.启用standalone模块并编译
/ t7 ^' l* I3 X
" ^8 _6 `: T& }下载modsecurity for nginx 解压,进入解压后目录执行:8 E8 P3 m7 e6 N+ y. _
e; b D' o# E( D/ |. D
./autogen.sh8 T: d4 w. x2 u3 Y/ j) S& X
./configure --enable-standalone-module --disable-mlogc
+ N$ ]8 O4 u' f m: fmake : l/ A9 F. g1 j1 F& t5 y4 w
三.nginx添加modsecurity模块
' p, m* @) K e4 J8 X
2 k/ x# `( S% A# Q* u在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
: J7 p& S4 U$ E; I0 X4 I
# [0 }3 O7 T% N: u./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine6 Z5 K* \; J2 O; g+ K+ ~' W7 L
make && make install2 M( I4 F. `4 o3 h9 a! L
四.添加规则7 G6 H2 \5 K5 b+ H
7 s+ N% p9 A: `1 P ^modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。1 S9 K k0 r8 S T/ K
7 d! e; G% e9 P6 y1.下载OWASP规则:
# D7 v9 r+ n- ~0 \" z+ r/ N8 G6 `1 |2 `, Q! N
git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
$ r5 R- a1 `/ E1 |7 i" j f! i! h+ o
mv owasp-modsecurity-crs /opt/tengine/conf/
6 P4 u* M) _) k5 U! e U+ G) S& r' h$ {" u
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf3 h7 U3 w6 y% B
2.启用OWASP规则:
. h+ `- d; p( {. H1 _ P0 W! m: i
' @. A0 D! L( i复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
% A, T+ E L& q( R: ^/ z7 _8 f/ ~" y( \. S4 n; j
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on1 V/ L9 \# ~ t8 u! K# y
" {+ l6 x6 B( q/ y* x! m4 q4 P( j$ x2 `
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。- N9 @" e0 G+ [" j$ }; y
: G5 D; ~6 r) w
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
4 A; N6 k3 Z6 Z4 }& LInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf! b9 `& L8 a6 h" Y
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf% ^9 U4 ~1 e) r6 [
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
0 \( R: [3 \5 |: R8 YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
$ I/ v. C8 w' Z- v1 `/ I+ yInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf3 Y6 |# [, A6 i: l
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf# f( o% e9 r/ X/ L2 z
五.配置nginx1 g- E! S' ^1 b/ s4 J- W
& ^! q2 P3 T# A p5 J7 z在需要启用modsecurity的主机的location下面加入下面两行即可:: x( H8 A: Q! C) f
- M! V! G$ E6 W" w$ ? _1 D3 e
ModSecurityEnabled on;
' j: N/ T6 V. ^6 c& B! |ModSecurityConfig modsecurity.conf;
7 _* b7 B7 ]# m0 R4 O下面是两个示例配置,php虚拟主机:
1 J* C! o3 E8 s: D* |4 B# X' @9 K8 N( K% H
server {; ?1 B* d1 t1 X+ Y0 d
listen 80;3 f# ^% i; O: W
server_name 52os.net www.52os.net;, p8 C( Z7 S, C
, s" r9 B& G+ h- [; w# m8 _$ f
location ~ \.php$ {
0 T% |/ I1 s9 E V ModSecurityEnabled on;
& }( v2 P/ l' Q# M3 T$ f: J ModSecurityConfig modsecurity.conf;( r7 H3 i1 n5 F- D" ]7 H9 h
. J2 C. S5 C) K
root /web/wordpress;
: Q! g8 z1 v0 K. {; } index index.php index.html index.htm;
5 E2 ]" e* o. Z! y; B5 i
9 E+ H; ^* k2 x* m" u( ` fastcgi_pass 127.0.0.1:9000;( g" q( n3 ]4 N. M0 l) }
fastcgi_index index.php;/ {; `; Y( S/ `$ v! s8 ~9 R
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
. L. H* z* F( ]# \+ v: y include fastcgi_params;
6 V* T7 U) k: C' F6 h }
n7 A2 h( o# C; e$ G }
9 d2 l" q q7 |- P+ Dupstream负载均衡:
. o3 K% ^8 V/ c* C
/ M3 y& B% n$ ^2 c3 V ~# tupstream 52os.net {: S" y4 G: F4 I5 V( ^: A$ X
server 192.168.1.100:8080;
/ g4 a" I6 P* A server 192.168.1.101:8080 backup;
9 ~7 m# W: h a" D}
5 f% |8 ~5 C0 [. q' y8 L; D0 ^2 l# S. E* ~) O) X6 }7 q
server {
) p0 k! Y/ E6 ?. I) e" clisten 80;
3 c2 ?3 j: v5 T7 a% C4 X# y( zserver_name 52os.net www.52os.net;% f9 F+ Y- f6 Y5 ~" ]2 t
2 h( H4 ~* c9 K2 `. \3 Q0 l6 B7 Y1 Slocation / {
* j/ D U" g+ \" B [; _ ModSecurityEnabled on; 6 c8 T+ Y, A8 _
ModSecurityConfig modsecurity.conf;
# t* f) e" h' H n* l% o4 ]- ~( v; I1 {9 A& @2 m8 x& Y
proxy_pass http://online;
3 u: @/ w4 k; ]; K proxy_redirect off;
8 d+ c V! u1 F. A+ H proxy_set_header Host $host;4 j9 W4 V' W2 f' \ x: C/ n
proxy_set_header X-Real-IP $remote_addr;
8 \1 z$ i5 r4 `% M' n7 X proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
1 x" L& N6 W+ [. s) ~& ? }$ U. q# t5 T; [2 y5 T% @% |
}6 k+ e, x; {7 ]4 l3 V; W
六.测试1 V/ k/ |% H U" c
J. J8 ^% T a: Y: B/ ]$ n5 t
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
9 B5 G* r5 u# h, F$ E% \$ R9 p- K4 u: c1 b% Q' V8 R
<?php
- p# R6 x! R4 h! W# k2 a phpinfo(); ; g! H/ V+ x8 C |9 W' m7 {
?>
9 g. _& _; k; S. h0 I# Y; Y3 x在浏览器中访问:1 z, {1 Z$ o5 S* V
6 v9 m3 W: k' m- u& d5 m4 phttp://www.52os.net/phpinfo.php?id=1 正常显示。0 O) Q2 l) r3 {& I7 o% ?8 ?
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
& L5 W9 d5 ]' L( w4 khttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。5 z0 n# P# ^: e
说明sql注入和xss已经被过滤了
/ d+ } V2 @& M9 Y7 \! z2 X3 ]( C! ^- f
七、安装过程中排错
4 D) `' [" _& t1 J" x* h$ p4 E
% R9 ^) z: @& O1 `0 M1 h/ y1.缺少APXS会报错3 u/ l: \3 e, X$ C; g2 @* S! r3 J' K
$ p4 Q' |! F/ i
configure: looking for Apache module support via DSO through APXS8 z [5 k1 |( ]9 H- ]1 ` T
configure: error: couldn't find APXS
& z2 _6 Z! b( K# z+ z0 Xapxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
8 f( r$ U# E3 d/ _解决方法:
1 ~7 X7 F- B, b6 ~9 n* k* ]3 g& b' ^
; r# b- W( [. D7 F5 \1 byum install httpd-devel# j/ b" i( D7 j6 w9 `
2.没有pcre8 r6 {0 t. O2 R3 W& ]
+ J1 ^; f4 x" f( ]- C
configure: *** pcre library not found.: V8 W! m$ r+ ^/ A7 H1 x
configure: error: pcre library is required
- G( j! ^5 S% Z% U k: |, H解决方法:8 a8 Z% o4 m+ ~; r2 B }/ h
) g' o& }: U$ t t& I
yum install pcre pcre-devel
9 n! |6 \! D+ X( ^3.没有libxml2
0 {+ a6 a/ b* V9 `1 U' u- x5 w& b2 @* ?, c. H
& E. v! i% c; W9 q, `
configure: *** xml library not found.
2 a; ` O7 J& Q. I L2 ]* `configure: error: libxml2 is required! S5 C0 c2 N) ~+ i3 B. ]5 T
解决方法:
( e- H* `1 b( M
$ N. j" U- q* xyum install libxml2 libxml2-devel( d) |" w3 l$ M F+ y1 u6 s7 Q. i9 c
4.执行 /opt/tengine/sbin/nginx -m 时有警告# I# o; k4 d5 y. {6 k, ]" \
3 n9 t+ T) T- Q- Q
Tengine version: Tengine/2.1.0 (nginx/1.6.2)$ U( N, b' }4 y/ j. {* r0 N
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!: U P! C, i- z3 w% p3 ~
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# n9 p' W% i# R. u- x" h
2 b3 h$ }4 ]9 C. [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
5 I" Y: G0 o& w$ j2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
+ G& w1 X8 ~+ _& U- u9 t) o8 x; |2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
- _6 K* S+ y$ g4 v3 w+ h2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"* u9 W0 S9 n" v9 ~
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
+ h; F% J5 O) j6 L3 @0 L2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 A, l e# `0 P* N
解决方法,移除低版本的APR (1.3.9)" C- i/ b2 X& K7 Q1 l' p: C
0 n' g( f8 i" X7 y
yum remove apr
/ G# @- X. a8 K$ v7 P% f5.Error.log中有: Audit log: Failed to lock global mutex! i4 t+ z- H8 H+ N
2 F3 o u z' i( F) I" @2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
+ j" b) ?$ w9 b2 @6 j3 Aglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
. u6 _3 x% V3 H解决方法:
4 L/ o) e, H& ]/ a$ c编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:7 Y3 f/ \* l4 P5 {& ~/ w u ?4 i& Z3 B
; a2 G! g! l1 S; dSecAuditLogDirMode 0777
9 Z" T5 D* Y+ E8 e& C2 n4 eSecAuditLogFileMode 0550
5 `+ M' g# y( _SecAuditLogStorageDir /var/log/modsecurity9 D4 _3 m' P% H* f8 d
SecAuditLogType Concurrent; z# d' {, h. C% r) U- M, b
参考文章:
6 n" q5 }5 E$ v( _8 ohttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
. ?9 E8 ^- S& E8 s1 H- E1 I) L/ nhttp://drops.wooyun.org/tips/2614 |
|