|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
2 G6 N% ]9 Q! O: p
/ F# q6 H1 A/ q0 q一.准备工作/ s" g+ I+ `# c- X' X2 j4 ]
9 {5 ?8 e& Y( P1 u d6 z: r( W$ ?) [
系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
( o2 j8 I* r+ r3 b5 L& Q
) s7 q$ W0 h; Y) n8 Utengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz
5 d8 u3 @4 `$ P; K- N" Q& Y7 j3 A) u! j! E+ E- c, n' t
modsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz
0 e5 E. `9 ^5 K7 |2 {# o( f3 d
& W' H } }: Z9 ~. v7 {* IOWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs" k! S5 A) J. _* l& e) N0 C2 i* N
8 Q n! }: e5 w/ E6 e, \' |' L* L/ {4 a依赖关系:
' o. [" W$ _& Q3 K8 t0 I; c# o3 S) ]tengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
4 Z7 f- q8 D8 q; C# h p
9 N. I6 @1 V; Y( m% \0 `0 byum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
& W* @) _2 @/ f0 W2 i, X: Mmodsecurty依赖的包:pcre httpd-devel libxml2 apr! ]! z N5 j f K7 ~, [1 X
K" _0 W1 W4 d: x; Y8 c7 r5 [
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel5 B( O q2 k% u( j7 S! r$ P/ V/ c" x
二.启用standalone模块并编译2 p j b$ O' X. r" g9 `7 M
' q& Z: K8 I2 {- ~下载modsecurity for nginx 解压,进入解压后目录执行:- @+ Q% W9 l6 ~
. {6 G: |" D- s7 O./autogen.sh
, F0 ^) m. a; g1 [3 C4 A2 M( U3 h/ X./configure --enable-standalone-module --disable-mlogc* ~- m7 B4 f8 r- U+ m# [& e
make
- o$ E7 |$ u9 T* Z三.nginx添加modsecurity模块
- Q0 V. V) q5 X" {) H
: Q8 U5 i- l# ?+ `+ ^7 M在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:$ [. M/ o7 r% D' c& }+ q: q
- X6 T; O) ~9 q- Y! x./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
9 e$ `+ w+ t3 [7 Q' m- l' Dmake && make install
; w; g% G( X0 F' m/ k' s' Y' E四.添加规则
/ i/ s v3 ?7 ~& w5 F# y3 s7 S3 o. x! Y# K' p) W F4 U- P
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。+ m& x9 c5 i1 { w! N! r
9 w' J& s$ e* ~& {
1.下载OWASP规则:1 h, _4 f8 e7 I+ E% Z
% w5 g# }1 i- u( x+ t, A0 ~* [+ Pgit clone https://github.com/SpiderLabs/owasp-modsecurity-crs, O( g6 v: j7 f9 D5 T% r" o
; b4 S' _* K- O+ imv owasp-modsecurity-crs /opt/tengine/conf/
* Z5 ~8 N4 i0 Z$ o" e& i R. }& k; n% I+ e6 u4 N! M6 a7 x
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf5 X! n0 l! ]5 f9 M
2.启用OWASP规则:9 T5 `; c6 C' f6 J1 v- b5 [5 `
}4 T9 }+ Q. O# V# i8 q, R复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。5 Y8 Y4 |8 I1 \1 j+ b/ X$ b$ X5 c
. Z: Y) c# e, g# P/ D. d
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
* C$ p' I1 b+ |0 w8 P% }3 [) H1 }
1 o: Z/ T3 ?# p6 k( D. a8 F- Eowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。6 T& X) ^+ V* u, f( _6 }8 Z0 `
/ l I7 u/ d- ^( D( s' k
Include owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
" |5 X) g+ d' y. t% nInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" g2 _% ^+ s$ o3 u& c# WInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf0 H8 i, P3 J1 l& v' o7 C8 U6 S
Include owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf5 E, N' ^2 d) r9 E$ S) S
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf
. ^1 {3 v' [/ V/ CInclude owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf" Y2 @: M6 ~) w
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 t3 ?3 M/ K5 n. \ ^4 P五.配置nginx3 D/ e; ~$ j' I P0 K2 c+ g
) ]9 @( X) W( ? x- o& w1 C
在需要启用modsecurity的主机的location下面加入下面两行即可:. `+ v9 r o) f4 _5 n$ f q6 ^6 a. o
% Q* ]. L. {% vModSecurityEnabled on; / {; A. K7 I# n
ModSecurityConfig modsecurity.conf;
2 L9 d0 ]: E* d4 f4 [) g( M下面是两个示例配置,php虚拟主机:
5 K; K0 |8 [, @& O8 C& E2 D s; s3 u& b5 R, o% N2 D P' o
server {5 B# z5 v: N1 t
listen 80;
& S; _ w' Y0 u/ y& ]; k server_name 52os.net www.52os.net;
/ f9 i3 [1 \# ~% Q) X- W4 |6 B0 X 6 i1 W/ z) K% C' Y
location ~ \.php$ {
4 X7 g8 M7 w3 u, r2 X# ^! M' Z ModSecurityEnabled on; # [" j; U4 V. C H& W; p, z
ModSecurityConfig modsecurity.conf;
5 H/ Q& i# Z% K
3 p. Y, O! t* o; ~4 ` root /web/wordpress;; K+ X- q, ?1 g
index index.php index.html index.htm;
) P( Q- d3 v# J& D1 j0 }, m" \# p & s- X! \1 [6 i# q
fastcgi_pass 127.0.0.1:9000;
6 l9 t- @8 X2 b7 y( x1 f fastcgi_index index.php;. u8 @2 i# i/ U& D, ~: M
fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;
4 f6 ?% t9 F0 t$ O1 w, D% K9 p4 N include fastcgi_params;6 l S8 s) k" F5 {" X3 I* ?, ?
}) \+ L) }- G! ?' C" w9 c
}) O6 V& D S2 [' R# x% R
upstream负载均衡:3 C8 x9 N$ X1 F" h
# g* ^% {0 m% I S$ f+ J) Fupstream 52os.net {
4 Q @3 E, c/ `. E. }( C server 192.168.1.100:8080;
/ M% b* I3 X3 w& e1 S& B server 192.168.1.101:8080 backup;
# N; |" W/ W/ b# l* o" [7 ?3 V4 k}- i7 k$ s. M# G5 u3 Z$ {8 y$ s
, K& o4 o5 G9 kserver {
! Y3 Z$ x$ W- ]7 d1 @listen 80;
2 h& I2 x4 `! U9 C1 h: c( h% sserver_name 52os.net www.52os.net;
( H# M( t% Q8 {5 S5 ]6 v4 d7 [) s7 Y* i5 F8 u9 q& L
location / {
$ f( T; t2 v9 b4 e ModSecurityEnabled on; 1 P p& m2 j, p F
ModSecurityConfig modsecurity.conf; $ N6 m+ D& F2 M: p0 v
% Y0 i0 Z) J$ ~! V3 X proxy_pass http://online;% M4 c' A8 ]( k" z9 U
proxy_redirect off;' m1 V7 V* V s/ @
proxy_set_header Host $host;" j1 m \! R3 P- _
proxy_set_header X-Real-IP $remote_addr;1 c$ v' o' a' T$ U1 b& |8 }
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;( ^0 M, W. n; v" E) A3 g. Y
}) F! X# i5 c( R5 T, c& U
}& U* g9 K( a" w/ E1 O) E1 [$ K
六.测试+ r, W! e3 c& r% N2 R
/ c8 v3 o0 G t: ^; A2 n6 D% Y我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
- V4 ]4 ^2 Z, B! b' e7 O
& n. Z+ c) O, B: ^3 T) B<?php
4 [/ Z$ l: m7 s4 ]( e7 i0 i4 q, N! N* M phpinfo(); ) L' f9 N$ E- R7 s- M! f
?>( x; A; z4 K8 ?( A, _4 G/ z
在浏览器中访问:5 K9 S, z7 z3 n1 d
- k _) b2 ]9 d; m/ e; d3 vhttp://www.52os.net/phpinfo.php?id=1 正常显示。: N8 \# @* D! |, i9 d
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。
+ [) g3 ?- y! Z8 k; f7 X: ]- hhttp://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。. A v5 X w6 ^) ?" i! _$ s
说明sql注入和xss已经被过滤了
5 |3 ^& I1 d4 p, P$ C
# y8 G# C( R2 j3 t, E. r七、安装过程中排错
4 o, ^; o# i7 y) N5 h4 \& O- Y/ |
1.缺少APXS会报错( u4 `8 H$ x; w- |0 u. S4 ?* e; o
. R, V( l' M/ {4 pconfigure: looking for Apache module support via DSO through APXS) a2 y! U3 z; E$ `8 }+ Z2 O
configure: error: couldn't find APXS2 ^7 o, T+ U! v6 W
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。) u" |5 L2 ` a- y
解决方法:( r8 q. S* K5 n! l
$ G* n. Y, |* S3 Vyum install httpd-devel# O& N, G1 Q. U
2.没有pcre" O' s/ M1 o, D& [2 f
( }; W5 _+ s1 }3 E, Aconfigure: *** pcre library not found.
3 {, D0 I |' m" H/ W1 f6 p2 econfigure: error: pcre library is required4 g3 s7 c9 h) u& |0 ?& W
解决方法:3 ~* E1 m0 L: \9 N8 Z. V
) J. E" o1 }5 H
yum install pcre pcre-devel4 s \) N: q, o! z. y/ b
3.没有libxml2
@; Q# q$ ^) p- ?) R, r7 H: y0 ~. S w: H
6 l2 l% o6 d x% r, b, Q. k5 z" Cconfigure: *** xml library not found.
9 t" t, S; d4 xconfigure: error: libxml2 is required
# J9 k" \, O0 U8 y4 G解决方法:
2 C0 c% A& Y" ~& u+ n( T
! j! N: C9 L$ W4 k: H7 j! Yyum install libxml2 libxml2-devel4 V5 w8 w/ K% t& A/ O$ K- |6 T: ^
4.执行 /opt/tengine/sbin/nginx -m 时有警告( f+ y. {+ B* ]: ^: l) ]5 H X9 }
. T6 H* h# \" H( G, f1 TTengine version: Tengine/2.1.0 (nginx/1.6.2)- s3 E! f5 ?) R. ^5 z! O2 w( H
nginx: [warn] ModSecurity: Loaded APR do not match with compiled!
( w9 G5 r* }4 s% p原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log# z+ S5 r% N2 w; {
: F% {" M$ c7 z5 a
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.; W( ^% S( p8 o# b0 G' {
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"& n, d: J% Q" P B+ `4 U% Z. p( r
2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!6 o0 s' h5 S/ V* h1 F
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"2 w( F) ]! N1 x1 M% S& S
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"
6 O# E* ^) ^. i4 u2 x2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.# `* ], C& t6 Q; E6 }
解决方法,移除低版本的APR (1.3.9)
2 F, V; \4 s" P1 p+ `9 t- X. }- l$ ^% N( q, k
yum remove apr
]: S$ q, B* i1 @5.Error.log中有: Audit log: Failed to lock global mutex8 o# j6 S8 G6 C& c6 j
. C" k/ ?! m' h7 h2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock 7 M6 |7 S4 ?: ?6 a- S+ U
global mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]& ~0 W- w: D4 z
解决方法:
( S$ H- U' f5 S/ L4 s编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
" u0 t! Q: d) i. o! C' X
- @2 e) n5 I4 x$ B) M# b. GSecAuditLogDirMode 07771 i& L/ p% a* V$ x' L& K0 |- ?1 Q- h5 m: s
SecAuditLogFileMode 0550
% P& A b' [( E, i+ ]2 U) x; YSecAuditLogStorageDir /var/log/modsecurity
0 r/ u! @# C6 g, G2 m! A& o1 y8 BSecAuditLogType Concurrent" V, N1 ~7 Q m# N3 v" Y. @. \
参考文章:
% l' }# z" R; hhttps://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
' Z8 W( p/ a. Y( v' Dhttp://drops.wooyun.org/tips/2614 |
|