|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。6 o% q# I( ~; @# C* L
9 v% h7 Y2 l1 J2 V- U& G6 d( e
一.准备工作1 P! W1 C# K1 p' `- [( p
% ^% R4 Y- d& n
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0, W8 q" X" w$ |
9 j/ `! J \! ]; H; M0 c4 {, @
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
7 d, @0 a3 _+ t9 v4 u
7 w( @( l/ e# n6 f9 R+ d- b3 ~modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 Z& s2 U' N: a4 m& @) y$ ], Q
+ l, J6 l5 s( H4 `, NOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
. R; I+ E$ }" A4 Y n d5 C, Y/ o' o0 R/ j4 @6 p
依赖关系:
+ `1 O' G* n! a) Ktengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:/ c: T0 `1 i, z4 U% Z
$ X* `6 h5 d! o% U, S. t* syum install zlib zlib-devel openssl openssl-devel pcre pcre-devel" u/ v; l0 b4 B+ P' T! G5 \
modsecurty依赖的包:pcre httpd-devel libxml2 apr
" }+ X U2 P8 t
& @; _2 p+ E$ S9 s+ x3 dyum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel! f5 e# w8 w7 y6 o6 C% z0 n0 @
二.启用standalone模块并编译: T6 `3 Q$ w2 v/ P+ e
! I1 [: K+ I+ P; g; f" w$ w: c/ n) [2 r
下载modsecurity for nginx 解压,进入解压后目录执行:) g9 q5 K' G2 ^; t5 q
4 M: J' h: e' R! [0 M5 N- c6 V
./autogen.sh# H9 @; }3 Y$ x" v2 H- y2 }
./configure --enable-standalone-module --disable-mlogc
2 p% L1 K- X+ w/ B& V' }make
9 i; f! s C9 R三.nginx添加modsecurity模块
5 j% ~7 P7 H0 z0 Y# f7 A
) ~, h9 @9 L0 i$ S* C' T在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
/ X1 M; T7 z" k
; }1 m- o' P5 N; W3 d2 K./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine: T6 Y/ ^, L4 T' a" I& [
make && make install
; e" q+ h6 S7 g' Q8 M四.添加规则( u# ~0 R7 {" I# j+ r/ W
W. G6 y! _6 L+ X7 R9 umodsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
4 w. Y6 I% K0 d0 Z3 ]- a- _* c3 k* {% C6 k8 \
1.下载OWASP规则:
' v0 S, _5 r1 o
8 l7 A1 M4 [0 |6 Z" @& J8 q$ o5 Z- Cgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs9 ~$ A6 B, z4 H3 {
! o$ E4 m5 J2 c& y2 F) b O
mv owasp-modsecurity-crs /opt/tengine/conf/: K4 G/ @, ?5 r) N6 @
, ^1 ^5 j8 \/ p3 I, ~1 k+ [; Qcd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf
" a8 h0 Y7 h+ m( c# {* C: j7 M: V$ Q2 }2.启用OWASP规则:
: w1 E1 ]* X3 P: k9 j' g) A8 l' W8 Z! E1 x! h5 B. R3 g
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。* `2 _$ T# d* b
0 v# ~2 m2 b/ T1 v! h编辑modsecurity.conf 文件,将SecRuleEngine设置为 on! G: g6 Z5 U, [# q N1 ~6 S
, `6 l9 _( d% B8 ~% U! n
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。* t# F1 U: s3 M" _" l
) t/ ^1 H: ]5 a( w* b' @Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
* L. ]3 A6 @1 jInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
* H6 N5 Z( z: b' mInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
( ?0 T2 `; [* J' J8 F" g1 SInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
* x6 A- `' r: T) M/ \( z1 |Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf* T. u( G# v2 y$ s4 A
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
9 L+ { W* N9 [3 v7 Y# BInclude owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; _: X4 B6 h1 W) _( q: W& j! e( i* H3 r
五.配置nginx( |; r6 A3 ]* U. n8 K6 X% u
0 r; m: L# M9 F ^3 F5 R- s
在需要启用modsecurity的主机的location下面加入下面两行即可:
0 w, o, V! m+ W! s, l! }- o3 I+ G0 g% {0 Y A
ModSecurityEnabled on;
& q+ P2 O, v2 I# g. E5 |ModSecurityConfig modsecurity.conf;
, w& j/ y; R# u- q C下面是两个示例配置,php虚拟主机:
0 D8 n, X0 \3 ~" ~4 f1 c' D9 O& o9 t7 \# }4 n0 _, {3 z0 \
server {
4 M- \4 h! L& G4 p# j8 \; Y" n listen 80;' b; T) Y. e. |( \0 G
server_name 52os.net www.52os.net;& L3 S$ g9 G$ x3 ~% C
8 W5 ]' \# V. a) H location ~ \.php$ {' ?$ u# k3 `) T" F: B3 E
ModSecurityEnabled on;
/ N& r: Q( f* U! k' |, R, Z ModSecurityConfig modsecurity.conf;
]* U2 H4 ]) s/ s* u6 }) X! H o- ]
5 T! \4 j1 C1 p, I4 @' P root /web/wordpress;
1 Y2 ^/ f2 q4 q! e# B. b0 _ index index.php index.html index.htm;2 v+ Q" t8 U& s. I) s V' I
$ z$ U8 L* h+ F' B fastcgi_pass 127.0.0.1:9000;
y {9 [2 d" Y fastcgi_index index.php;9 R1 r8 o( M% T- i' |
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;; ^7 S7 R1 ~4 c% h! B6 I1 R7 z
include fastcgi_params;
- n' ?+ D/ K$ V$ y0 G; K }
8 G n5 W0 m: p( p2 g }
- t# m; a7 |# d2 |- Uupstream负载均衡:" f/ u* @/ U8 M1 C( J1 R! n! q$ ~
* l: T3 Z2 S* v' i6 xupstream 52os.net {, X% C2 u: N' X5 r$ d. ~* E3 a$ Q
server 192.168.1.100:8080;! w( c8 Z2 i q7 O, l! n; [
server 192.168.1.101:8080 backup;
6 r6 D, m" \$ l/ O* [3 b3 ]$ W" h}7 z' A7 I# T1 z4 E% P1 ]$ z
) D: w R$ T1 P( [' P* tserver {6 u& g6 Z4 z1 C& `5 [1 t2 q
listen 80;' `' U4 ]; Z. [3 p" [. A
server_name 52os.net www.52os.net;
- E- X6 r/ f0 P4 S
6 w+ p: a; C j# \7 f& llocation / {5 a, U6 E6 n I, e
ModSecurityEnabled on; 5 s5 D7 G) v% R" I
ModSecurityConfig modsecurity.conf; 1 d" H8 r) f( N2 m
, b. J- w2 I: K4 k1 x8 D: B proxy_pass http://online;
$ l; }& {0 Z! u proxy_redirect off;
) @2 u, n- S# A0 t0 _ proxy_set_header Host $host;7 w3 `1 [( q9 r" w) t) [
proxy_set_header X-Real-IP $remote_addr;: p: b4 y6 u. y* ~
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;" t9 ~- ?" F! a/ }& e; C2 R' m
}/ T) l* b) J8 H# |5 {
}" u2 u) Q0 H$ i) o& Z. B$ q
六.测试
! ] c" G# W$ Q! S; c6 u# T* f
. N9 _2 D* g0 h/ C. i0 l- F8 n: Z; a9 k我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:( @' b0 H1 u+ a3 u( R* w6 O
3 ~2 h# A4 W( r6 }3 ^
<?php
- h/ M1 h. F* A q& L phpinfo(); 4 W7 r& C8 J+ }1 t
?>7 k! Y/ s9 y& {/ U$ W7 n0 ^
在浏览器中访问:6 }7 Z7 o+ f' \1 J4 d" G
# ]; p1 s- ^8 @3 J, S* v) k
http://www.52os.net/phpinfo.php?id=1 正常显示。
* P0 j# Z" Z$ o! V; B/ B: C' Zhttp://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。3 d/ V% s2 |& {2 O+ V; S! n
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
- W, x0 t3 q G: I' Z说明sql注入和xss已经被过滤了" ~' U& q9 \4 k* P9 g7 [
% I$ \* d8 R/ t, y- F/ `七、安装过程中排错+ l* v1 s5 ?+ _1 P
4 G; `& D# _4 f" G4 |5 F
1.缺少APXS会报错+ [2 d2 c* M4 Q; M" |( ^6 ]
% @8 I" k6 @1 B1 Wconfigure: looking for Apache module support via DSO through APXS
5 Z# n2 n ^9 p2 Dconfigure: error: couldn't find APXS( w1 w# _- h; V+ A
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
8 ~5 Q- ~; k% C7 a# P5 N解决方法:
0 a7 N- x7 L( N. g1 ~+ z/ v
- q7 _9 z; [! G- l, u0 myum install httpd-devel
8 n" B5 \% _2 R% O, r& b% K5 \4 N$ [2.没有pcre
0 J) R3 _" I+ E" K5 _
$ {- c- h* s# w( L* vconfigure: *** pcre library not found.
! q9 E6 D4 h9 e5 x& G# yconfigure: error: pcre library is required0 d. r) L$ n9 S: m0 Z. d2 I
解决方法:
8 H. t* ~2 ^4 a% q w( r8 p% [9 v ^2 b e* h) v: p/ o
yum install pcre pcre-devel
1 j5 ~3 z" Q. M" A+ M6 N3.没有libxml2% i6 f" @" u# O+ ~
) X3 t) n( F- j/ [4 y B" _
) y7 P/ ^7 B! S6 M! `# econfigure: *** xml library not found.& y; U) N+ f6 I* w3 ~2 i( h
configure: error: libxml2 is required# J! d* Q4 t9 X7 a
解决方法:
5 ] t, W) @" k
1 N4 F; T. b5 U! U/ C& _yum install libxml2 libxml2-devel" d4 x' i6 ?4 E5 L* _. r- Q: ^4 Y2 M
4.执行 /opt/tengine/sbin/nginx -m 时有警告7 g. G* d6 q. Q3 x1 k x' w( w
0 d9 k" Q# A$ a2 `0 tTengine version: Tengine/2.1.0 (nginx/1.6.2)% y$ D5 `& p: F
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!! ~1 K3 i6 _- W* L/ J
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log4 C" [% Z6 t! a% d
/ u; U2 j$ b- J! ~0 B2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.: B Y( [/ j: k! z) S3 n
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"5 k' z* z# [! w* I
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 u9 w) _$ R. Q. a/ R3 q
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"4 k" d6 A5 P: U$ W) I4 R; R- H; |
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6") C7 A+ j1 M( t9 ^
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On., T) j, S/ M2 \6 ?' B2 O1 S
解决方法,移除低版本的APR (1.3.9)
: @: q- Y% F& ]8 d
9 h4 W. o, D" C% C, eyum remove apr q4 c, J) g% Y+ Y
5.Error.log中有: Audit log: Failed to lock global mutex3 ]6 a7 ]/ x+ \: K2 [) i
! \. d8 m- P+ |' F2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
9 y0 \$ p+ c1 x+ y9 U; X% L) p7 Vglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
9 x. d# H; V+ C/ Y! ] _( X解决方法:
; u: F) j# E5 d, Y% s l# U编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
1 H8 @7 N, W6 M0 g4 u' S9 A% G3 M% z5 d1 Z5 N! B! y/ C5 j. ~
SecAuditLogDirMode 07775 m O. o G- O! ~& g7 p
SecAuditLogFileMode 0550- D( c% N+ n: g- m
SecAuditLogStorageDir /var/log/modsecurity5 f: y/ ?- y1 C( g" H6 @
SecAuditLogType Concurrent
: L# k% G: }( d( |) `: G" J5 v参考文章:, H$ b& v$ a4 N# Z
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
3 _8 h' v/ ?* h' ]8 w; ^* Ehttp://drops.wooyun.org/tips/2614 |
|