|
|
modsecurity原本是Apache上的一款开源waf,可以有效的增强web安全性,目前已经支持nginx和IIS,配合nginx的灵活和高效,可以打造成生产级的WAF,是保护和审核web安全的利器。
; Y, J z" E# u, d- \, n, u5 @* D. t( a' H% E
一.准备工作+ K& j, p8 L. T; s7 X) X
* |+ l$ [# c; v D系统:centos 6.5 64位、 tengine 2.1.0, modsecurity 2.8.0
$ t* W- G; V, a: S8 x
! W. U: o$ y. B7 G+ |+ o! etengine : http://tengine.taobao.org/download/tengine-2.1.0.tar.gz: F( k" M1 j% r A
! d \! C7 v+ n8 z' B; Imodsecurity for Nginx: https://www.modsecurity.org/tarball/2.8.0/modsecurity-2.8.0.tar.gz$ L7 C8 u$ u/ u" [' p) D
5 Q# u5 d- R* R, ~2 I. ^$ T9 N) x' [
OWASP规则集: https://github.com/SpiderLabs/owasp-modsecurity-crs! J" Q. k3 A) b; o$ [
' n! T6 G0 E; m' Q; e依赖关系:
$ f8 U5 Y5 o9 ~4 M r8 `8 Btengine(nginx)依赖: pcre 、zlib、 openssl, 这三个包centos 6.5 系统源里都有:
. \9 P) t+ @" m# |: ^* l1 t9 v" Y/ C/ @5 F
yum install zlib zlib-devel openssl openssl-devel pcre pcre-devel
) z' ^3 M. H6 R; D9 [& X: emodsecurty依赖的包:pcre httpd-devel libxml2 apr
- T: u( P+ G. Q5 h' O4 M5 G4 }/ C: T& ?9 N8 D# ]! ^
yum install httpd-devel apr apr-util-devel apr-devel pcre pcre-devel libxml2 libxml2-devel0 M7 z. g5 D' P4 |& l
二.启用standalone模块并编译5 F# J6 N" d j
W' l: N, B0 b* c) h b下载modsecurity for nginx 解压,进入解压后目录执行:
1 e! q& B7 m2 k% \: V6 J
. L3 |5 q+ z. }. K% ^! W./autogen.sh
6 n: z* M. W/ X7 k1 y) }/ ~5 T./configure --enable-standalone-module --disable-mlogc$ k" ^" Y- t- G; t0 J5 e+ [
make , w% T& l4 b1 o3 ]3 b! B+ T* f/ Y
三.nginx添加modsecurity模块1 R4 i& z* c" D( o$ D' \5 {& f
$ ^4 O4 U2 R- a/ O在编译standalone后,nginx编译时可以通过"--add-module"添加modsecurity模块:* s& V& N: y2 r
) h) {4 Y, l% x, G% s
./configure --add-module=/root/modsecurity-2.8.0/nginx/modsecurity/ --prefix=/opt/tengine
. ]% I& Z8 a1 k }8 imake && make install& u' X* G2 u4 h) I( A* ^9 V
四.添加规则, S8 \6 R' q" C" T- T$ F. m$ F2 i2 l
8 `7 k9 {" i! v7 V! z
modsecurity倾向于过滤和阻止web危险,之所以强大就在于规则,OWASP提供的规则是于社区志愿者维护的,被称为核心规则CRS(corerules),规则可靠强大,当然也可以自定义规则来满足各种需求。
$ Z, I" O2 l& C+ Z0 U& G
& f/ n! b) Q' c& j2 F1.下载OWASP规则:# c7 {( U- O1 Z' r6 s0 k1 T# n
. v& q0 s6 a% r4 [& ?git clone https://github.com/SpiderLabs/owasp-modsecurity-crs
. Z, j6 c( O% n R2 F3 H6 ^1 O8 E8 w) n
mv owasp-modsecurity-crs /opt/tengine/conf/
! N- z3 F) v4 c- D+ E. r% ~6 M/ B& R$ t. o+ ~- G4 t$ u0 d) e1 ]6 }
cd /opt/tengine/conf/owasp-modsecurity-crs && mv modsecurity_crs_10_setup.conf.example modsecurity_crs_10_setup.conf, U& [# q; ]7 S1 l5 x5 _0 g
2.启用OWASP规则:
0 V( H/ _ ~' k( B7 U: Z4 Z+ ^6 s. X) E
复制modsecurity源码目录下的modsecurity.conf-recommended和unicode.mapping到nginx的conf目录下,并将modsecurity.conf-recommended重新命名为modsecurity.conf。0 z+ H+ x; q0 {. n
" m- Q: z6 ` Y! E" y
编辑modsecurity.conf 文件,将SecRuleEngine设置为 on
8 U1 `, P2 }& L& Q" s$ ~# V8 R4 e
( @8 |& F7 P7 Y2 fowasp-modsecurity-crs下有很多存放规则的文件夹,例如base_rules、experimental_rules、optional_rules、slr_rules,里面的规则按需要启用,需要启用的规则使用Include进来即可。
! \9 E1 \6 y/ L" B
6 K: w, ?9 h2 W# U6 EInclude owasp-modsecurity-crs/modsecurity_crs_10_setup.conf
6 D2 U( @0 ?0 f8 \' fInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_41_sql_injection_attacks.conf
" l3 x0 l" q' U6 J( {Include owasp-modsecurity-crs/base_rules/modsecurity_crs_41_xss_attacks.conf
' J3 f2 I- P$ ^! C5 P$ E. JInclude owasp-modsecurity-crs/base_rules/modsecurity_crs_40_generic_attacks.conf) B) z8 J& I# \
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_dos_protection.conf+ O# h3 j" s( _ V- b
Include owasp-modsecurity-crs/experimental_rules/modsecurity_crs_11_brute_force.conf, G+ j" s' N- I; U% L
Include owasp-modsecurity-crs/optional_rules/modsecurity_crs_16_session_hijacking.conf
3 Z/ ~! M3 o4 g5 F" S8 |! {% v五.配置nginx
7 G- K8 M( s6 l2 F$ }; V
5 f J( M% v; e1 _( M在需要启用modsecurity的主机的location下面加入下面两行即可:4 c5 O# k5 e O) a) _# z5 a3 Y
. n7 a, x$ C7 h1 [, m
ModSecurityEnabled on;
: y" @+ Y6 a8 T# UModSecurityConfig modsecurity.conf;
6 P8 D8 \8 I( y8 @( G. z/ d下面是两个示例配置,php虚拟主机:
a* {- w! u7 t z, H( q( k5 O+ ^0 C9 }) G9 f( i; o
server {
7 V" }6 T3 G& H3 ~ Z0 D" B listen 80;
. w' L9 E% v/ E! Q2 D L8 p2 @ server_name 52os.net www.52os.net;
( N: c4 A& v2 n1 s. X4 v 1 o3 L9 w0 Z% d" G; h4 \
location ~ \.php$ {+ O6 S% W# E* }! D7 P% T! X$ o( e! H
ModSecurityEnabled on;
$ `6 y; A& c: c+ m& G, o ModSecurityConfig modsecurity.conf;( j3 I3 O5 `5 C3 ~" W' V) A
$ F' G7 L( v6 Y |' {
root /web/wordpress;6 M& g+ ~* O5 b7 N4 F! ?
index index.php index.html index.htm;
9 P6 U: P" d; ]: P7 U8 y4 T
8 @( E8 u& X( B" z fastcgi_pass 127.0.0.1:9000;
* ]' B6 b- Z6 m' o fastcgi_index index.php;
. {! M1 Q# c) X. D/ O$ i# s& v% H fastcgi_param SCRIPT_FILENAME $Document_root$fastcgi_script_name;, x) b1 _3 c2 W6 U
include fastcgi_params;7 p$ f; W- X/ p8 Q ?" X
}
& X" z; ~$ m, B) ~ }3 v8 F5 o' }# V: l8 D
upstream负载均衡:
$ K9 w5 |( L0 S- g o" H6 ?3 ~* V* \# @1 p! a% U
upstream 52os.net {" M2 w/ o& M# ]" b& @0 E
server 192.168.1.100:8080;1 ?8 q' g. s9 P }
server 192.168.1.101:8080 backup;$ H; B% V$ Y, w0 F2 ^
}
2 J, M1 q$ t$ D
7 k3 B, P; u9 S7 Nserver {
: E2 [4 m6 t3 T: elisten 80;* ^& e+ P; \. E& o" [( q. T% i
server_name 52os.net www.52os.net;8 J+ @3 Y' V" v5 q X
8 e8 P9 T4 J# g0 y: m; Clocation / {
+ Q; e# {. c) s. i4 Y( z7 O+ j ModSecurityEnabled on;
# X) F# D- D. i. y ModSecurityConfig modsecurity.conf; % n/ ^; B6 c, h4 d5 u' V
! v; J7 Z, }5 h' M+ [
proxy_pass http://online;
2 |( Y. `% `/ F/ q5 Q6 |& N proxy_redirect off;
s) K* e8 e+ J& T- _ proxy_set_header Host $host;
: m0 f9 O5 k+ t/ Y6 } proxy_set_header X-Real-IP $remote_addr;
. T8 h; M* B5 R* N+ \" E proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
5 ]! E2 r3 J* e; Q- ~ }/ c' b$ ]9 l0 K
}
: t z9 W* C1 c2 e6 Q六.测试 E( l0 m# }- H& U' V
L3 i* |! j8 l. Q1 ? K( E6 Z Q
我们启用了xss和sql注入的过滤,不正常的请求会直接返回403。以php环境为例,新建一个phpinfo.php内容为:
( D' ^6 V7 r: S1 x' h4 H: z4 X4 g/ Y# a! m0 n* z6 N& ]3 P
<?php4 ?% V ~' R+ `' X% A- j8 z
phpinfo(); . y, v# [0 c( N- r
?>
' n. E- @) e) a% `1 O在浏览器中访问:
. s9 D1 m P" w2 z) }
# U' N4 P/ r* l, r3 @http://www.52os.net/phpinfo.php?id=1 正常显示。" ]8 D8 R4 `4 \5 L. C: s
http://www.52os.net/phpinfo.php?id=1 and 1=1 返回403。' A) m$ o' }, q+ F5 P% Q$ m/ G
http://www.52os.net/phpinfo.php?search=<scritp>alert('xss');</script> 返回403。% w6 N( X- T. r3 q
说明sql注入和xss已经被过滤了1 X, h2 [) o3 a t
! w# n0 Q: |8 B. {9 H( {; d$ E9 n
七、安装过程中排错 u) q7 I5 X& n/ Y
1 Y' N7 a+ O. j$ L$ Y0 X# O1.缺少APXS会报错
) z& P# Z" k: ~7 V8 }. q5 l* t( e4 o
configure: looking for Apache module support via DSO through APXS
/ Y( X) D& X+ \1 y) |& Xconfigure: error: couldn't find APXS' A) \% ^" B0 U# N6 u- l" e, f
apxs是一个为Apache HTTP服务器编译和安装扩展模块的工具,用于编译一个或多个源程序或目标代码文件为动态共享对象。8 f8 t( _! K5 t" s" C
解决方法:
" M% c0 F+ j" f) n5 g9 {3 k ~% R$ o6 C
yum install httpd-devel
* @5 K& z3 {" t2.没有pcre
$ ?! N1 q Q# G$ k* |3 o& ]/ A
$ O v, I3 u' g( \. j& _0 m3 [configure: *** pcre library not found.2 U: v4 w/ U* R4 e. Y
configure: error: pcre library is required! i) ^1 \6 {) ]4 W
解决方法:
+ J, b( C. b$ u k7 y) K0 X
9 `! T" b+ W8 p! Q7 ayum install pcre pcre-devel$ ~5 b- E! f2 t/ e+ F$ x9 Y5 X1 t/ q
3.没有libxml26 t$ B5 r6 i( g8 _( y' u3 @
" x# y) z1 q" O8 Z4 ^! w9 b$ O( n$ {* B0 D9 |; ~7 m& P$ R9 j6 D
configure: *** xml library not found.) g' p! q' J2 i, W; T
configure: error: libxml2 is required: [6 e) p: P! V/ M" E' M, I
解决方法:
% N& l- t& H) b+ ]; ~: _8 y
% E1 W/ V8 o( w$ q/ g6 \2 kyum install libxml2 libxml2-devel- w b& K# |* r( l8 Y
4.执行 /opt/tengine/sbin/nginx -m 时有警告
! v( s! [( b' z8 Z7 G9 z) R7 Z5 h! l/ Y
Tengine version: Tengine/2.1.0 (nginx/1.6.2)
8 F5 ^, |# J1 v* enginx: [warn] ModSecurity: Loaded APR do not match with compiled!. R s8 g6 ?8 r: b+ \
原因:modsecurity编译时和加载时的apr版本不一致造成的,并且会有以下error.log
, `5 G" L; Z7 f' W2 m8 F7 V
0 W2 F4 X5 w! X; l/ l% ]" G2015/01/26 02:04:18 [notice] 29036#0: ModSecurity for nginx (STABLE)/2.8.0 () configured.
& K( X8 D0 `2 k) `" [+ K% r2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: APR compiled version="1.5.0"; loaded version="1.3.9"
. `9 }, Q" r8 [1 @ v7 v- G. h2015/01/26 02:04:18 [warn] 29036#0: ModSecurity: Loaded APR do not match with compiled!0 _$ L1 r: l/ {9 \" r4 ?2 ` `
2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: PCRE compiled version="7.8 "; loaded version="7.8 2008-09-05"
+ e1 A4 _; J/ B; O$ ]/ I0 [2015/01/26 02:04:18 [notice] 29036#0: ModSecurity: LIBXML compiled version="2.7.6"/ [$ O' n' j# E7 B
2015/01/26 02:04:18 [notice] 29036#0: Status engine is currently disabled, enable it by set SecStatusEngine to On.7 ~ j, @; j8 S: Z2 m: M
解决方法,移除低版本的APR (1.3.9)
& f% P, ?3 L' D
1 c( d% z j$ W3 U( K- z' m h3 Xyum remove apr6 C0 W0 S( E7 x8 y
5.Error.log中有: Audit log: Failed to lock global mutex
; |. G* W7 e) w% d+ n4 E* S/ w, D) x6 I6 s8 O! @6 N
2015/01/26 04:15:42 [error] 61610#0: [client 10.11.15.161] ModSecurity: Audit log: Failed to lock
4 @6 q* m6 P4 K$ gglobal mutex: Permission denied [hostname ""] [uri "/i.php"] [unique_id "AcAcAcAcAcAcAcA4DcA7AcAc"]
# H( D& b8 F9 J! x* E/ c6 s解决方法:
. a* m; P0 f8 f- Y4 h! W! A编辑modsecurity.conf,注释掉默认的SecAuditLogType和SecAuditLog,添加以下内容:
2 k# C; j, z- e# m6 Q5 e3 r' {8 J- b* r$ l# d
SecAuditLogDirMode 0777. f( w! e4 a* M( |+ J
SecAuditLogFileMode 0550 L$ }. o' ~7 V7 v* R
SecAuditLogStorageDir /var/log/modsecurity
" N5 P# e8 n$ P8 E& s9 E$ W* `SecAuditLogType Concurrent0 @' D. J5 g* K; R
参考文章:" P+ V" t7 Q5 L% _; `8 P) L
https://github.com/SpiderLabs/ModSecurity/wiki/Reference-Manual#Installation_for_NGINX
5 c* C/ g/ ]' Z" Y# f; {http://drops.wooyun.org/tips/2614 |
|