|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
( F9 c0 s6 ]9 v7 G* D% A
* U/ x: I! d' B( T5 T一.准备工作& X2 ]: k# Y. R
4 ?1 ]" n* Q6 ^0 N: }) `& Z系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
8 U/ D! N4 Y1 W5 a3 e8 F7 J9 q- J' n6 L; F
tengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz5 \9 H5 c5 @" X' K3 T
7 V0 X' o: m9 v) v9 G
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz' k b# \9 _1 H9 |9 T
" H# [, T8 c+ hOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs
& c' E' C9 c7 G' `2 ~' Y
2 I4 z' u: [/ z2 ?8 Y6 F7 r依赖关系:% b" G M# R; B0 A3 ~
tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
& S! I" Z0 G1 e/ B" E; z" N D, _' B% x1 O5 G2 w) K1 b- q
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel9 |$ E4 i5 I# Y* v2 u6 t
modsecurty依赖的包:pcre httpd-devel libxml2 apr
% d9 T& h4 u* _+ Z
% G6 H0 y1 H8 l3 g- p1 D, ?9 ayum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel
; Y1 H9 ?6 u6 @* g" ]二.启用standalone模块并编译2 G1 q6 ?; P3 a# z& I
! S" G& V- X, }; ]1 Y下载modsecurity for nginx 解压,进入解压后目录执行:+ X( `9 s8 m, g( n% v
+ l3 p, c; c( s( _9 w
./autogen.sh
" k/ y4 C0 E; R8 s- F8 V4 X/ ~./configure --enable-standalone-module --disable-mlogc8 c6 i3 J# q8 Y/ F2 z/ b
make / ]* V# W' B1 e
三.nginx添加modsecurity模块
3 k! ~% p$ y7 l9 r
: s- l3 R" q, M" n在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:
$ A, y7 c4 e# x( ^! Y4 B, \$ r: l9 [. i1 x" T, s' V' [5 W( `
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine, i" Z9 O/ ~! j/ s" Q: }
make && make install8 D" c; H& K7 {% N1 R. ~3 {, V# o
四.添加规则, |2 |0 u5 `2 {& M
) h7 o) Z" u& L( w, g% O
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
2 v! ^0 V2 \1 u
0 e8 l; }5 f0 \6 \2 ^' u& W1.下载OWASP规则:
* B) l9 `# l1 E0 A- I$ ^+ U) l" @
/ N; L( N% e6 j, m' F% kgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs" y$ K: T; ~3 s: u5 d$ t0 v, G
0 b# f( ~( V9 i& r, Gmv owasp-modsecurity-crs /opt/tengine/conf/; C( e; ]1 N# E6 i" g4 C: [8 M; `. _
6 l! u/ h, T! ]3 }* ccd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf* J# X! \0 I0 I. m$ C+ g
2.启用OWASP规则:) Y1 v6 i' d# w D4 d8 K9 c, _ T
/ p0 d7 \3 g3 L) }& M9 o
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。
3 n) Z6 T3 `% K' m& r [
0 k. |- H9 p1 T9 ~! g0 ^编辑modsecurity.conf 文件,将SecRuleEngine设置为 on$ Q& [$ ~! V8 W8 x/ D5 X
; R: k0 V+ _3 c9 i8 |
owasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。4 i4 @' z7 ]$ ^ X8 j/ c
3 w- j& J5 N i( q/ b; ZInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
9 \! J. s& B, z: `Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf& d2 X- J5 n5 ~$ g1 _* c- k
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
5 _" ]$ [6 g. p/ [; |Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf
. S4 _3 {6 v1 YInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
7 S& Y" Q2 @. \5 b7 y+ s+ |! OInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf
# f5 T% a3 _# y, m" w9 ^Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf; C8 c, I7 l _3 X! m
五.配置nginx+ A/ x6 V; {# Z
+ z+ V* t3 R: R7 x3 e3 s
在需要启用modsecurity的主机的location下面加入下面两行即可:
+ b0 Y4 A7 J% ]$ n N2 l6 J
& F/ P: J, d+ g- ]; RModSecurityEnabled on;
" D: g0 r0 x( l, \$ s4 c0 m. cModSecurityConfig modsecurity.conf;' l n. d4 l/ }# l
下面是两个示例配置,php虚拟主机:) D0 p* z- C5 }4 t" y! s
- u. g3 g4 w x. P) A% userver {
% s) W' G6 ~% w) @ e( f6 l9 h listen 80;
/ n7 V( ^% o/ q0 F' \$ v7 a server_name 52os.net www.52os.net;
; o$ k: a3 L1 ^+ V j, A0 E: z
3 g e0 j$ a( n' W0 u' m. ] location ~ \.php$ {
5 U- G* W7 b: K; Y7 c g$ B. f3 f ModSecurityEnabled on;
( m1 w' r& b$ ]' N& t9 `7 G7 U6 H, n ModSecurityConfig modsecurity.conf;9 C9 D# \, y0 O% [) B5 e
; C. `+ X; a4 O* M$ M! e Y/ C( p
root /web/wordpress;/ u& y1 P8 m7 J3 f. c
index index.php index.html index.htm;
+ W/ f8 ]' {# A- [ - `5 V/ f' G2 p7 {8 A, t f5 G
fastcgi_pass 127.0.0.1:9000;
* I% t6 T: R: C4 z- J- Y1 | fastcgi_index index.php;
5 C) s+ W6 Y9 G9 M8 ] z9 G fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
& ]4 U% Q6 [2 x include fastcgi_params;; V3 _" E3 P; G; t H( |5 o
}
, f/ K4 z4 ~$ v% B8 m+ r; q }: B* L1 b2 z* d8 H: p+ h
upstream负载均衡:+ `. _& {0 r1 [- s: g
: l* m- ]" w1 V
upstream 52os.net {) P) a# d* T E7 w
server 192.168.1.100:8080;# K, `& ?7 |, B# y
server 192.168.1.101:8080 backup;
4 |. b% A, a1 x' I}" ^' M$ N; n2 F( s
& I- L/ d& J) r: B' qserver {
1 p) E2 q' e8 Z$ W1 \1 elisten 80;
: B7 u9 @, T& c& d4 Rserver_name 52os.net www.52os.net;9 k4 a5 K) X& [; ^0 v3 l
- S8 E0 O$ a8 tlocation / {
- ?8 O, ^7 Q7 l ^8 Y0 l/ s ModSecurityEnabled on; 9 I8 x0 p: D3 z- x: a" E; f
ModSecurityConfig modsecurity.conf; 0 Z8 F: f2 e! E4 H/ l k8 P3 F8 {
% O3 `. f3 H/ U6 q+ L' R# r. S
proxy_pass http://online;$ e! I4 \9 ~1 @) B3 N
proxy_redirect off;9 H' l. Y b' k
proxy_set_header Host $host;( S1 i# k" x9 f
proxy_set_header X-Real-IP $remote_addr;% s( L% U% S. l
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
0 s* @. x9 c) n% t% ^5 Y/ M+ K$ e }* w* c. ~! x9 X( g
}
; f/ D) q+ Y1 N' X" N! D六.测试
: E/ O$ }) H; B' S% v( E
# G! M# g- e4 \9 S. i我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
7 ?" z h6 K9 f7 X; h$ w: y- ?( z& v* i+ W0 Q
<?php
& u) f$ `' l: V; w0 Y4 n+ P. m phpinfo(); 5 _/ l- q+ z1 L& u& f' c
?>
8 z' W/ d( L1 o& n( b/ R+ ~2 K在浏览器中访问:
9 y( \1 i6 Y, x& i1 B) k4 m5 a0 o0 Y! \) n: I1 V1 w0 w
http://www.52os.net/phpinfo.php?id=1 正常显示。8 P3 \. y) G+ [, G
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
2 d4 @ w1 T& d) t2 hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。
" ]# s3 P8 e6 [# r/ l说明sql注入和xss已经被过滤了9 q6 G2 o: |' x
n9 D5 c K( f- V七、安装过程中排错
( o) l4 E) p1 r; }" K" q& X) D: Z8 t. P% v H5 k' o$ P0 u- D# n+ w
1.缺少APXS会报错
: t% D5 P7 J5 w/ s- v1 G+ N7 r! F4 S
configure: looking for Apache module support via DSO through APXS& o$ f+ }# l0 c! ]( ]
configure: error: couldn't find APXS7 @+ z- \: F c) T7 ^" Q I# {
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。
n) A& x h( J j V% Z; d/ f解决方法:
7 G1 l1 n# \$ s8 i# g6 i; q6 h
1 a) Q- j$ B* V5 u vyum install httpd-devel7 ?) i$ [$ k* z
2.没有pcre
4 b4 U; J' s0 Y' M/ \% D, c7 ~( j; g; V$ x8 x, Q
configure: *** pcre library not found.
y+ E( K5 J+ i2 Econfigure: error: pcre library is required
9 B2 F$ M v9 b7 L" b解决方法:/ y2 ~3 ^2 U$ P7 D3 B; u
0 y- R+ p6 k( V
yum install pcre pcre-devel- k3 T" ~+ L$ F# F# V
3.没有libxml21 D. v2 _" Z6 ]5 C6 G
$ g) O( ] j+ l4 U% s% R
& s: {1 }/ `* S+ ?1 m' v7 Xconfigure: *** xml library not found.
, t3 K- r+ x' r' pconfigure: error: libxml2 is required4 |& b2 T) h8 M# @+ L5 Y( S
解决方法:% T( c: l5 u# c* F- Z6 k
: E1 L7 ?) s" i0 {' p! [
yum install libxml2 libxml2-devel4 N1 Q: P2 I- R
4.执行 /opt/tengine/sbin/nginx -m 时有警告
) [. i" p# G$ Y' Z: V8 ~7 M7 x& E! R2 Y9 ^- Q2 C- v4 i1 n
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
, L; l/ s+ ?6 c0 F7 D; dnginx: [warn] ModSecurity: Loaded APR do not match with compiled!: d+ D% C, N* \% ^; |; Q
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log$ f% H; g0 V) I+ b! |$ c: M! o
, g# W/ ^* d5 y( ?! q6 ?" E7 Q2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
' L# O; p; D) `( j$ w2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
6 U# d: x! V8 c2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!
! A( `- g/ E, I7 g( X/ O2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"' C. c Q; A$ L) L: b* G
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"+ Q# I* r; x- l0 N+ C2 v8 r
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.4 T( y M+ n: K Q5 H
解决方法,移除低版本的APR (1.3.9)/ @8 L1 u, n- U! i! T* b& `6 b8 N
4 z& p# B: m, |) y8 n8 vyum remove apr' O1 @* c5 C& h0 V3 i
5.Error.log中有: Audit log: Failed to lock global mutex F9 j! {. I& Q+ V3 f
* P$ e. H' l3 K/ f: ?
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
$ ]! E! a5 X+ j+ D4 ^( X: {global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
7 _: M5 {9 ^3 P, o6 q, p解决方法:
# A+ d. i2 K# C- p2 i' D9 s编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:. {& W$ I/ N& J
2 r2 H, |; v. o
SecAuditLogDirMode 0777
8 N0 T* E" T/ V; D9 mSecAuditLogFileMode 0550
9 K% `5 p& H; I' M) F8 hSecAuditLogStorageDir /var/log/modsecurity
$ W+ A3 Y* a5 B" LSecAuditLogType Concurrent8 m5 @ O2 J9 c. T- A: d+ M
参考文章:2 ~7 E& |' |) J) Y) v5 \- M
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX4 X3 V5 V; a, a1 `0 p' o
http://drops.wooyun.org/tips/2614 |
|